Randomly select an addr from the authority discovery

This commit is contained in:
Luke Parker 2024-03-23 00:09:23 -04:00
parent 4914420a37
commit bca3728a10
No known key found for this signature in database
3 changed files with 13 additions and 4 deletions

1
Cargo.lock generated
View file

@ -7625,6 +7625,7 @@ dependencies = [
"hex",
"jsonrpsee",
"pallet-transaction-payment-rpc",
"rand_core",
"sc-authority-discovery",
"sc-basic-authorship",
"sc-cli",

View file

@ -23,6 +23,7 @@ name = "serai-node"
zeroize = "1"
hex = "0.4"
rand_core = "0.6"
schnorrkel = "0.11"
sp-core = { git = "https://github.com/serai-dex/substrate" }

View file

@ -1,5 +1,7 @@
use std::{sync::Arc, collections::HashSet};
use rand_core::{RngCore, OsRng};
use sp_blockchain::{Error as BlockchainError, HeaderBackend, HeaderMetadata};
use sp_block_builder::BlockBuilder;
use sp_api::ProvideRuntimeApi;
@ -72,14 +74,19 @@ where
.get_addresses_by_authority_id(validator.into())
.await
.unwrap_or_else(HashSet::new)
.into_iter();
// Only take a single address
.into_iter()
.collect::<Vec<_>>();
// Randomly select an address
// There should be one, there may be two if their IP address changed, and more should only
// occur if they have multiple proxies/an IP address changing frequently/some issue
// preventing consistent self-identification
// It isn't beneficial to use multiple addresses for a single peer here
if let Some(address) = returned_addresses.next() {
all_p2p_addresses.push(address);
if !returned_addresses.is_empty() {
all_p2p_addresses.push(
returned_addresses.remove(
usize::try_from(OsRng.next_u64() >> 32).unwrap() % returned_addresses.len(),
),
);
}
}
Ok(all_p2p_addresses)