mirror of
https://github.com/serai-dex/serai.git
synced 2025-01-03 17:40:34 +00:00
Randomly select an addr from the authority discovery
This commit is contained in:
parent
4914420a37
commit
bca3728a10
3 changed files with 13 additions and 4 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -7625,6 +7625,7 @@ dependencies = [
|
||||||
"hex",
|
"hex",
|
||||||
"jsonrpsee",
|
"jsonrpsee",
|
||||||
"pallet-transaction-payment-rpc",
|
"pallet-transaction-payment-rpc",
|
||||||
|
"rand_core",
|
||||||
"sc-authority-discovery",
|
"sc-authority-discovery",
|
||||||
"sc-basic-authorship",
|
"sc-basic-authorship",
|
||||||
"sc-cli",
|
"sc-cli",
|
||||||
|
|
|
@ -23,6 +23,7 @@ name = "serai-node"
|
||||||
zeroize = "1"
|
zeroize = "1"
|
||||||
hex = "0.4"
|
hex = "0.4"
|
||||||
|
|
||||||
|
rand_core = "0.6"
|
||||||
schnorrkel = "0.11"
|
schnorrkel = "0.11"
|
||||||
|
|
||||||
sp-core = { git = "https://github.com/serai-dex/substrate" }
|
sp-core = { git = "https://github.com/serai-dex/substrate" }
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
use std::{sync::Arc, collections::HashSet};
|
use std::{sync::Arc, collections::HashSet};
|
||||||
|
|
||||||
|
use rand_core::{RngCore, OsRng};
|
||||||
|
|
||||||
use sp_blockchain::{Error as BlockchainError, HeaderBackend, HeaderMetadata};
|
use sp_blockchain::{Error as BlockchainError, HeaderBackend, HeaderMetadata};
|
||||||
use sp_block_builder::BlockBuilder;
|
use sp_block_builder::BlockBuilder;
|
||||||
use sp_api::ProvideRuntimeApi;
|
use sp_api::ProvideRuntimeApi;
|
||||||
|
@ -72,14 +74,19 @@ where
|
||||||
.get_addresses_by_authority_id(validator.into())
|
.get_addresses_by_authority_id(validator.into())
|
||||||
.await
|
.await
|
||||||
.unwrap_or_else(HashSet::new)
|
.unwrap_or_else(HashSet::new)
|
||||||
.into_iter();
|
.into_iter()
|
||||||
// Only take a single address
|
.collect::<Vec<_>>();
|
||||||
|
// Randomly select an address
|
||||||
// There should be one, there may be two if their IP address changed, and more should only
|
// 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
|
// occur if they have multiple proxies/an IP address changing frequently/some issue
|
||||||
// preventing consistent self-identification
|
// preventing consistent self-identification
|
||||||
// It isn't beneficial to use multiple addresses for a single peer here
|
// It isn't beneficial to use multiple addresses for a single peer here
|
||||||
if let Some(address) = returned_addresses.next() {
|
if !returned_addresses.is_empty() {
|
||||||
all_p2p_addresses.push(address);
|
all_p2p_addresses.push(
|
||||||
|
returned_addresses.remove(
|
||||||
|
usize::try_from(OsRng.next_u64() >> 32).unwrap() % returned_addresses.len(),
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(all_p2p_addresses)
|
Ok(all_p2p_addresses)
|
||||||
|
|
Loading…
Reference in a new issue