diff --git a/Cargo.lock b/Cargo.lock index 9c5d097e2..1ae1d4633 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7625,6 +7625,7 @@ dependencies = [ "hex", "jsonrpsee", "pallet-transaction-payment-rpc", + "rand_core", "sc-authority-discovery", "sc-basic-authorship", "sc-cli", diff --git a/substrate/node/Cargo.toml b/substrate/node/Cargo.toml index f66a9705b..12ba4d17a 100644 --- a/substrate/node/Cargo.toml +++ b/substrate/node/Cargo.toml @@ -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" } diff --git a/substrate/node/src/rpc.rs b/substrate/node/src/rpc.rs index f5ed25820..d07778ccf 100644 --- a/substrate/node/src/rpc.rs +++ b/substrate/node/src/rpc.rs @@ -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::>(); + // 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)