fix: also set configured bootnodes for discv5 (#9885)

This commit is contained in:
Matthias Seitz
2024-07-30 01:40:24 +02:00
committed by GitHub
parent 8bd5cafe9a
commit eb2d0a22d9
5 changed files with 53 additions and 17 deletions

View File

@ -299,6 +299,27 @@ impl Config {
}
}
/// Inserts a new boot node to the list of boot nodes.
pub fn insert_boot_node(&mut self, boot_node: BootNode) {
self.bootstrap_nodes.insert(boot_node);
}
/// Inserts a new unsigned enode boot node to the list of boot nodes if it can be parsed, see
/// also [`BootNode::from_unsigned`].
pub fn insert_unsigned_boot_node(&mut self, node_record: NodeRecord) {
let _ = BootNode::from_unsigned(node_record).map(|node| self.insert_boot_node(node));
}
/// Extends the list of boot nodes with a list of enode boot nodes if they can be parsed.
pub fn extend_unsigned_boot_nodes(
&mut self,
node_records: impl IntoIterator<Item = NodeRecord>,
) {
for node_record in node_records {
self.insert_unsigned_boot_node(node_record);
}
}
/// Returns the discovery (UDP) socket contained in the [`discv5::Config`]. Returns the IPv6
/// socket, if both IPv4 and v6 are configured. This socket will be advertised to peers in the
/// local [`Enr`](discv5::enr::Enr).

View File

@ -385,12 +385,17 @@ impl NetworkConfigBuilder {
self.boot_nodes(sepolia_nodes())
}
/// Sets the boot nodes.
/// Sets the boot nodes to use to bootstrap the configured discovery services (discv4 + discv5).
pub fn boot_nodes<T: Into<TrustedPeer>>(mut self, nodes: impl IntoIterator<Item = T>) -> Self {
self.boot_nodes = nodes.into_iter().map(Into::into).collect();
self
}
/// Returns an iterator over all configured boot nodes.
pub fn boot_nodes_iter(&self) -> impl Iterator<Item = &TrustedPeer> + '_ {
self.boot_nodes.iter()
}
/// Disable the DNS discovery.
pub fn disable_dns_discovery(mut self) -> Self {
self.dns_discovery_config = None;

View File

@ -173,7 +173,7 @@ impl NetworkManager {
secret_key,
discovery_v4_addr,
mut discovery_v4_config,
discovery_v5_config,
mut discovery_v5_config,
listener_addr,
peers_config,
sessions_config,
@ -208,12 +208,16 @@ impl NetworkManager {
resolved_boot_nodes.push(resolved);
}
discovery_v4_config = discovery_v4_config.map(|mut disc_config| {
if let Some(disc_config) = discovery_v4_config.as_mut() {
// merge configured boot nodes
disc_config.bootstrap_nodes.extend(resolved_boot_nodes.clone());
disc_config.add_eip868_pair("eth", status.forkid);
disc_config
});
}
if let Some(discv5) = discovery_v5_config.as_mut() {
// merge configured boot nodes
discv5.extend_unsigned_boot_nodes(resolved_boot_nodes)
}
let discovery = Discovery::new(
listener_addr,

View File

@ -118,6 +118,13 @@ pub struct NetworkArgs {
}
impl NetworkArgs {
/// Returns the resolved bootnodes if any are provided.
pub fn resolved_bootnodes(&self) -> Option<Vec<NodeRecord>> {
self.bootnodes.clone().map(|bootnodes| {
bootnodes.into_iter().filter_map(|node| node.resolve_blocking().ok()).collect()
})
}
/// Build a [`NetworkConfigBuilder`] from a [`Config`] and a [`ChainSpec`], in addition to the
/// values in this option struct.
///
@ -137,14 +144,7 @@ impl NetworkArgs {
default_peers_file: PathBuf,
) -> NetworkConfigBuilder {
let chain_bootnodes = self
.bootnodes
.clone()
.map(|bootnodes| {
bootnodes
.into_iter()
.filter_map(|trusted_peer| trusted_peer.resolve_blocking().ok())
.collect()
})
.resolved_bootnodes()
.unwrap_or_else(|| chain_spec.bootnodes().unwrap_or_else(mainnet_nodes));
let peers_file = self.peers_file.clone().unwrap_or(default_peers_file);

View File

@ -300,10 +300,16 @@ where
let rlpx_socket = (args.addr, args.port).into();
if !args.discovery.disable_discovery {
builder = builder.discovery_v5(args.discovery.discovery_v5_builder(
rlpx_socket,
ctx.chain_spec().bootnodes().unwrap_or_default(),
));
builder = builder.discovery_v5(
args.discovery.discovery_v5_builder(
rlpx_socket,
ctx.config()
.network
.resolved_bootnodes()
.or_else(|| ctx.chain_spec().bootnodes())
.unwrap_or_default(),
),
);
}
builder