feat: add node identity to networking stack (#2758)

This commit is contained in:
Bjerg
2023-05-20 09:24:31 +02:00
committed by GitHub
parent 2416756b20
commit 238eea37cf
3 changed files with 15 additions and 5 deletions

View File

@ -1,8 +1,9 @@
//! clap [Args](clap::Args) for network related arguments.
use crate::version::P2P_VERSION;
use clap::Args;
use reth_net_nat::NatResolver;
use reth_network::NetworkConfigBuilder;
use reth_network::{HelloMessage, NetworkConfigBuilder};
use reth_primitives::{mainnet_nodes, ChainSpec, NodeRecord};
use reth_staged_sync::Config;
use secp256k1::SecretKey;
@ -36,6 +37,10 @@ pub struct NetworkArgs {
#[arg(long, value_name = "FILE", verbatim_doc_comment, conflicts_with = "no_persist_peers")]
pub peers_file: Option<PathBuf>,
/// Custom node identity
#[arg(long, value_name = "IDENTITY", default_value = P2P_VERSION)]
pub identity: String,
/// Secret key to use for this node.
///
/// This will also deterministically set the peer ID. If not specified, it will be set in the
@ -71,14 +76,19 @@ impl NetworkArgs {
default_peers_file: PathBuf,
) -> NetworkConfigBuilder {
let chain_bootnodes = chain_spec.chain.bootnodes().unwrap_or_else(mainnet_nodes);
let peers_file = self.peers_file.clone().unwrap_or(default_peers_file);
let network_config_builder = config
// Configure basic network stack.
let mut network_config_builder = config
.network_config(self.nat, self.persistent_peers_file(peers_file), secret_key)
.boot_nodes(self.bootnodes.clone().unwrap_or(chain_bootnodes))
.chain_spec(chain_spec);
// Configure node identity
let peer_id = network_config_builder.get_peer_id();
network_config_builder = network_config_builder
.hello_message(HelloMessage::builder(peer_id).client_version(&self.identity).build());
self.discovery.apply_to_builder(network_config_builder)
}
}