diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index 2b7e5dffbd..4a32074c59 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -4814,6 +4814,7 @@ version = "2.0.0" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "derive_more 0.99.2 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "futures-timer 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "libp2p 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/substrate/client/authority-discovery/Cargo.toml b/substrate/client/authority-discovery/Cargo.toml index 0b1dad3744..1f88f6619f 100644 --- a/substrate/client/authority-discovery/Cargo.toml +++ b/substrate/client/authority-discovery/Cargo.toml @@ -27,6 +27,7 @@ serde_json = "1.0.41" sp-runtime = { path = "../../primitives/sr-primitives" } [dev-dependencies] +env_logger = "0.7.0" parking_lot = "0.9.0" peerset = { package = "sc-peerset", path = "../peerset" } test-client = { package = "substrate-test-runtime-client", path = "../../test/utils/runtime/client" } diff --git a/substrate/client/authority-discovery/src/lib.rs b/substrate/client/authority-discovery/src/lib.rs index 7752b8dc9b..8f16dddcd5 100644 --- a/substrate/client/authority-discovery/src/lib.rs +++ b/substrate/client/authority-discovery/src/lib.rs @@ -86,6 +86,12 @@ const LIBP2P_KADEMLIA_BOOTSTRAP_TIME: Duration = Duration::from_secs(30); /// discovery module. const AUTHORITIES_PRIORITY_GROUP_NAME: &'static str = "authorities"; +/// The maximum number of sentry node public addresses that we accept per authority. +/// +/// Everything above this threshold should be dropped to prevent a single authority from filling up +/// our peer set priority group. +const MAX_NUM_SENTRY_ADDRESSES_PER_AUTHORITY: usize = 5; + /// An `AuthorityDiscovery` makes a given authority discoverable and discovers other authorities. pub struct AuthorityDiscovery where @@ -316,7 +322,7 @@ where return Err(Error::VerifyingDhtPayload); } - let addresses: Vec = schema::AuthorityAddresses::decode(addresses) + let mut addresses: Vec = schema::AuthorityAddresses::decode(addresses) .map(|a| a.addresses) .map_err(Error::DecodingProto)? .into_iter() @@ -324,6 +330,18 @@ where .collect::>() .map_err(Error::ParsingMultiaddress)?; + if addresses.len() > MAX_NUM_SENTRY_ADDRESSES_PER_AUTHORITY { + warn!( + target: "sub-authority-discovery", + "Got more than MAX_NUM_SENTRY_ADDRESSES_PER_AUTHORITY ({:?}) for Authority + '{:?}' from DHT, dropping the remainder.", + MAX_NUM_SENTRY_ADDRESSES_PER_AUTHORITY, authority_id, + ); + addresses = addresses.into_iter() + .take(MAX_NUM_SENTRY_ADDRESSES_PER_AUTHORITY) + .collect(); + } + self.address_cache.insert(authority_id.clone(), addresses); } @@ -800,6 +818,7 @@ mod tests { #[test] fn handle_dht_events_with_value_found_should_call_set_priority_group() { + let _ = ::env_logger::try_init(); // Create authority discovery. let (mut dht_event_tx, dht_event_rx) = channel(1000);