Skip to content

Commit

Permalink
Randomly select an addr from the authority discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
kayabaNerve committed Mar 23, 2024
1 parent 4914420 commit bca3728
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions substrate/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
15 changes: 11 additions & 4 deletions substrate/node/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit bca3728

Please sign in to comment.