Skip to content

Commit

Permalink
feat: add node identity to networking stack (#2758)
Browse files Browse the repository at this point in the history
  • Loading branch information
onbjerg authored May 20, 2023
1 parent 2416756 commit 238eea3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
16 changes: 13 additions & 3 deletions bin/reth/src/args/network_args.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/net/eth-wire/src/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl HelloMessageBuilder {
self
}

/// Sets client version.
/// Sets protocol version.
pub fn protocol_version(mut self, protocol_version: ProtocolVersion) -> Self {
self.protocol_version = Some(protocol_version);
self
Expand Down
2 changes: 1 addition & 1 deletion crates/net/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,4 @@ pub use network::NetworkHandle;
pub use peers::PeersConfig;
pub use session::{PeerInfo, SessionsConfig};

pub use reth_eth_wire::DisconnectReason;
pub use reth_eth_wire::{DisconnectReason, HelloBuilder, HelloMessage};

0 comments on commit 238eea3

Please sign in to comment.