Skip to content
Snippets Groups Projects
Commit 2b473e4c authored by Max Inden's avatar Max Inden Committed by Gavin Wood
Browse files

client/authority-discovery: Limit the amount of sentries per authority (#4302)

When receiving more addresses for a given authority than a defined
threshold (5), the authority discovery drops the remaining in order to
prevent a single authority to fill all priority group slots.
parent 75dadcf9
Branches
Tags
No related merge requests found
......@@ -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)",
......
......@@ -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" }
......
......@@ -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<Client, Network, Block>
where
......@@ -316,7 +322,7 @@ where
return Err(Error::VerifyingDhtPayload);
}
let addresses: Vec<libp2p::Multiaddr> = schema::AuthorityAddresses::decode(addresses)
let mut addresses: Vec<libp2p::Multiaddr> = schema::AuthorityAddresses::decode(addresses)
.map(|a| a.addresses)
.map_err(Error::DecodingProto)?
.into_iter()
......@@ -324,6 +330,18 @@ where
.collect::<std::result::Result<_, _>>()
.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);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment