Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

client/authority-discovery: Limit number of addresses per authority #6947

Merged
2 commits merged into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions client/authority-discovery/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ const LIBP2P_KADEMLIA_BOOTSTRAP_TIME: Duration = Duration::from_secs(30);
/// discovery module.
const AUTHORITIES_PRIORITY_GROUP_NAME: &'static str = "authorities";

/// Maximum number of addresses cached per authority. Additional addresses are discarded.
const MAX_ADDRESSES_PER_AUTHORITY: usize = 10;

/// Role an authority discovery module can run as.
pub enum Role {
/// Actual authority as well as a reference to its key store.
Expand Down Expand Up @@ -496,6 +499,7 @@ where

false // `protocol` is not a [`Protocol::P2p`], let's keep looking.
}))
.take(MAX_ADDRESSES_PER_AUTHORITY)
.collect();

if !remote_addresses.is_empty() {
Expand Down
66 changes: 66 additions & 0 deletions client/authority-discovery/src/worker/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,72 @@ fn never_add_own_address_to_priority_group() {
);
}

#[test]
fn limit_number_of_addresses_added_to_cache_per_authority() {
let remote_key_store = KeyStore::new();
let remote_public = remote_key_store
.write()
.sr25519_generate_new(key_types::AUTHORITY_DISCOVERY, None)
.unwrap();

let dht_event = {
let addresses = (0..100).map(|_| {
let peer_id = PeerId::random();
let address: Multiaddr = "/ip6/2001:db8:0:0:0:0:0:1/tcp/30333".parse().unwrap();
address.with(multiaddr::Protocol::P2p(
peer_id.into(),
)).to_vec()
}).collect();

let mut serialized_addresses = vec![];
schema::AuthorityAddresses { addresses }
.encode(&mut serialized_addresses)
.map_err(Error::EncodingProto)
.unwrap();

let signature = remote_key_store.read()
.sign_with(
key_types::AUTHORITY_DISCOVERY,
&remote_public.clone().into(),
serialized_addresses.as_slice(),
)
.map_err(|_| Error::Signing)
.unwrap();

let mut signed_addresses = vec![];
schema::SignedAuthorityAddresses {
addresses: serialized_addresses.clone(),
signature,
}
.encode(&mut signed_addresses)
.map_err(Error::EncodingProto)
.unwrap();

let key = hash_authority_id(&remote_public.to_raw_vec());
let value = signed_addresses;
(key, value)
};

let (_dht_event_tx, dht_event_rx) = channel(1);

let (_to_worker, from_service) = mpsc::channel(0);
let mut worker = Worker::new(
from_service,
Arc::new(TestApi { authorities: vec![remote_public.into()] }),
Arc::new(TestNetwork::default()),
vec![],
dht_event_rx.boxed(),
Role::Sentry,
None,
);

worker.handle_dht_value_found_event(vec![dht_event]).unwrap();
assert_eq!(
MAX_ADDRESSES_PER_AUTHORITY,
worker.addr_cache.get_addresses_by_authority_id(&remote_public.into()).unwrap().len(),
);
}

#[test]
fn do_not_cache_addresses_without_peer_id() {
let remote_key_store = KeyStore::new();
Expand Down