Skip to content

Commit 0afdace

Browse files
committed
Improve logging around subnet discoveries
1 parent 9d960ba commit 0afdace

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

beacon_node/eth2_libp2p/src/discovery/mod.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ use futures::prelude::*;
1818
use futures::stream::FuturesUnordered;
1919
use libp2p::core::PeerId;
2020
use lru::LruCache;
21-
use slog::{crit, debug, error, info, warn};
21+
use slog::{crit, debug, error, info, trace, warn};
2222
use ssz::Encode;
23+
use std::fmt::Display;
2324
use std::{
2425
collections::{HashMap, VecDeque},
2526
net::{IpAddr, SocketAddr},
@@ -71,6 +72,19 @@ struct SubnetQuery {
7172
retries: usize,
7273
}
7374

75+
impl Display for SubnetQuery {
76+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77+
let min_ttl_secs = self
78+
.min_ttl
79+
.map(|ttl| ttl.saturating_duration_since(Instant::now()).as_secs());
80+
write!(
81+
f,
82+
"Subnet: {:?}, ttl_seconds: {:?}, attempt: {}",
83+
self.subnet, min_ttl_secs, self.retries
84+
)
85+
}
86+
}
87+
7488
#[derive(Debug, Clone, PartialEq)]
7589
enum QueryType {
7690
/// We are searching for subnet peers.
@@ -317,7 +331,7 @@ impl<TSpec: EthSpec> Discovery<TSpec> {
317331
if !self.started {
318332
return;
319333
}
320-
debug!(
334+
trace!(
321335
self.log,
322336
"Making discovery query for subnets";
323337
"subnets" => ?subnets_to_discover.iter().map(|s| s.subnet).collect::<Vec<_>>()
@@ -601,7 +615,7 @@ impl<TSpec: EthSpec> Discovery<TSpec> {
601615
retries,
602616
});
603617
// update the metrics and insert into the queue.
604-
debug!(self.log, "Queuing subnet query"; "subnet" => ?subnet, "retries" => retries);
618+
trace!(self.log, "Queuing subnet query"; "subnet" => ?subnet, "retries" => retries);
605619
self.queued_queries.push_back(query);
606620
metrics::set_gauge(&metrics::DISCOVERY_QUEUE, self.queued_queries.len() as i64);
607621
}
@@ -655,11 +669,6 @@ impl<TSpec: EthSpec> Discovery<TSpec> {
655669
// This query is for searching for peers of a particular subnet
656670
// Drain subnet_queries so we can re-use it as we continue to process the queue
657671
let grouped_queries: Vec<SubnetQuery> = subnet_queries.drain(..).collect();
658-
debug!(
659-
self.log,
660-
"Starting grouped subnet query";
661-
"subnets" => ?grouped_queries.iter().map(|q| q.subnet).collect::<Vec<_>>(),
662-
);
663672
self.start_subnet_query(grouped_queries);
664673
processed = true;
665674
}
@@ -704,13 +713,10 @@ impl<TSpec: EthSpec> Discovery<TSpec> {
704713
}
705714

706715
let target_peers = TARGET_SUBNET_PEERS - peers_on_subnet;
707-
debug!(self.log, "Discovery query started for subnet";
708-
"subnet_id" => ?subnet_query.subnet,
716+
trace!(self.log, "Discovery query started for subnet";
717+
"subnet_query" => %subnet_query,
709718
"connected_peers_on_subnet" => peers_on_subnet,
710-
"target_subnet_peers" => TARGET_SUBNET_PEERS,
711719
"peers_to_find" => target_peers,
712-
"attempt" => subnet_query.retries,
713-
"min_ttl" => ?subnet_query.min_ttl,
714720
);
715721

716722
filtered_subnets.push(subnet_query.subnet);
@@ -723,6 +729,11 @@ impl<TSpec: EthSpec> Discovery<TSpec> {
723729
// build the subnet predicate as a combination of the eth2_fork_predicate and the subnet predicate
724730
let subnet_predicate = subnet_predicate::<TSpec>(filtered_subnets, &self.log);
725731

732+
debug!(
733+
self.log,
734+
"Starting grouped subnet query";
735+
"subnets" => ?filtered_subnet_queries.iter().map(|q| q.to_string()).collect::<Vec<_>>(),
736+
);
726737
self.start_query(
727738
GroupedQueryType::Subnet(filtered_subnet_queries),
728739
TARGET_PEERS_FOR_GROUPED_QUERY,

beacon_node/eth2_libp2p/src/peer_manager/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,11 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
957957

958958
// request the subnet query from discovery
959959
if !subnets_to_discover.is_empty() {
960+
debug!(
961+
self.log,
962+
"Making subnet queries for maintaining sync committee peers";
963+
"subnets" => ?subnets_to_discover.iter().map(|s| s.subnet).collect::<Vec<_>>()
964+
);
960965
self.discovery.discover_subnet_peers(subnets_to_discover);
961966
}
962967
}

beacon_node/eth2_libp2p/src/rpc/codec/ssz_snappy.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,6 @@ impl<TSpec: EthSpec> Decoder for SSZSnappyInboundCodec<TSpec> {
161161
self.len = None;
162162
let _read_bytes = src.split_to(n as usize);
163163

164-
// We need not check that decoded_buffer.len() is within bounds here
165-
// since we have already checked `length` above.
166164
match self.protocol.version {
167165
Version::V1 => handle_v1_request(self.protocol.message_name, &decoded_buffer),
168166
Version::V2 => handle_v2_request(self.protocol.message_name, &decoded_buffer),
@@ -298,8 +296,6 @@ impl<TSpec: EthSpec> Decoder for SSZSnappyOutboundCodec<TSpec> {
298296
self.len = None;
299297
let _read_bytes = src.split_to(n as usize);
300298

301-
// We need not check that decoded_buffer.len() is within bounds here
302-
// since we have already checked `length` above.
303299
match self.protocol.version {
304300
Version::V1 => handle_v1_response(self.protocol.message_name, &decoded_buffer),
305301
Version::V2 => handle_v2_response(
@@ -532,7 +528,7 @@ fn handle_v1_response<T: EthSpec>(
532528
// length = length-prefix received in the beginning of the stream.
533529
///
534530
/// For BlocksByRange/BlocksByRoot reponses, decodes the appropriate response
535-
/// according to the received fork_name.
531+
/// according to the received `ForkName`.
536532
fn handle_v2_response<T: EthSpec>(
537533
protocol: Protocol,
538534
decoded_buffer: &[u8],
@@ -886,6 +882,7 @@ mod tests {
886882
);
887883
}
888884

885+
// Test RPCResponse encoding/decoding for V2 messages
889886
#[test]
890887
fn test_context_bytes_v2() {
891888
let fork_context = fork_context();
@@ -1001,8 +998,10 @@ mod tests {
1001998
)
1002999
}
10031000

1001+
/// Test a malicious snappy encoding for a V1 `Status` message where the attacker
1002+
/// sends a valid message filled with a stream of useless padding before the actual message.
10041003
#[test]
1005-
fn test_decode_malicious_status_message() {
1004+
fn test_decode_malicious_v1_message() {
10061005
// 10 byte snappy stream identifier
10071006
let stream_identifier: &'static [u8] = b"\xFF\x06\x00\x00sNaPpY";
10081007

@@ -1055,6 +1054,8 @@ mod tests {
10551054
);
10561055
}
10571056

1057+
/// Test a malicious snappy encoding for a V2 `BlocksByRange` message where the attacker
1058+
/// sends a valid message filled with a stream of useless padding before the actual message.
10581059
#[test]
10591060
fn test_decode_malicious_v2_message() {
10601061
let fork_context = Arc::new(fork_context());

beacon_node/eth2_libp2p/src/types/topics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl GossipTopic {
142142
PROPOSER_SLASHING_TOPIC => GossipKind::ProposerSlashing,
143143
ATTESTER_SLASHING_TOPIC => GossipKind::AttesterSlashing,
144144
topic => match committee_topic_index(topic) {
145-
Some(subnet_id) => match subnet_id {
145+
Some(subnet) => match subnet {
146146
Subnet::Attestation(s) => GossipKind::Attestation(s),
147147
Subnet::SyncCommittee(s) => GossipKind::SyncCommitteeMessage(s),
148148
},

0 commit comments

Comments
 (0)