Skip to content

Commit

Permalink
feat: add discovery port function (#4543)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Sep 10, 2023
1 parent e1d6686 commit fc9f116
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
8 changes: 7 additions & 1 deletion crates/net/discv4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use std::{
cell::RefCell,
collections::{btree_map, hash_map::Entry, BTreeMap, HashMap, VecDeque},
io,
net::{IpAddr, SocketAddr},
net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4},
pin::Pin,
rc::Rc,
sync::Arc,
Expand Down Expand Up @@ -96,6 +96,12 @@ pub use reth_net_nat::{external_ip, NatResolver};
/// Note: the default TCP port is the same.
pub const DEFAULT_DISCOVERY_PORT: u16 = 30303;

/// The default address for discv4 via UDP: "0.0.0.0:30303"
///
/// Note: The default TCP address is the same.
pub const DEFAULT_DISCOVERY_ADDRESS: SocketAddr =
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, DEFAULT_DISCOVERY_PORT));

/// The maximum size of any packet is 1280 bytes.
const MAX_PACKET_SIZE: usize = 1280;

Expand Down
35 changes: 17 additions & 18 deletions crates/net/network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
session::SessionsConfig,
NetworkHandle, NetworkManager,
};
use reth_discv4::{Discv4Config, Discv4ConfigBuilder, DEFAULT_DISCOVERY_PORT};
use reth_discv4::{Discv4Config, Discv4ConfigBuilder, DEFAULT_DISCOVERY_ADDRESS};
use reth_dns_discovery::DnsDiscoveryConfig;
use reth_ecies::util::pk2id;
use reth_eth_wire::{HelloMessage, Status};
Expand All @@ -17,11 +17,7 @@ use reth_primitives::{
use reth_provider::{BlockReader, HeaderProvider};
use reth_tasks::{TaskSpawner, TokioTaskExecutor};
use secp256k1::SECP256K1;
use std::{
collections::HashSet,
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
sync::Arc,
};
use std::{collections::HashSet, net::SocketAddr, sync::Arc};
// re-export for convenience
pub use secp256k1::SecretKey;

Expand Down Expand Up @@ -244,26 +240,25 @@ impl NetworkConfigBuilder {
/// This is a convenience function for both [NetworkConfigBuilder::listener_addr] and
/// [NetworkConfigBuilder::discovery_addr].
///
/// By default, both are on the same port: [DEFAULT_DISCOVERY_PORT]
/// By default, both are on the same port:
/// [DEFAULT_DISCOVERY_PORT](reth_discv4::DEFAULT_DISCOVERY_PORT)
pub fn set_addrs(self, addr: SocketAddr) -> Self {
self.listener_addr(addr).discovery_addr(addr)
}

/// Sets the socket address the network will listen on.
///
/// By default, this is [Ipv4Addr::UNSPECIFIED] on [DEFAULT_DISCOVERY_PORT]
/// By default, this is [DEFAULT_DISCOVERY_ADDRESS]
pub fn listener_addr(mut self, listener_addr: SocketAddr) -> Self {
self.listener_addr = Some(listener_addr);
self
}

/// Sets the port of the address the network will listen on.
///
/// By default, this is [DEFAULT_DISCOVERY_PORT]
/// By default, this is [DEFAULT_DISCOVERY_PORT](reth_discv4::DEFAULT_DISCOVERY_PORT)
pub fn listener_port(mut self, port: u16) -> Self {
self.listener_addr
.get_or_insert(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, DEFAULT_DISCOVERY_PORT).into())
.set_port(port);
self.listener_addr.get_or_insert(DEFAULT_DISCOVERY_ADDRESS).set_port(port);
self
}

Expand All @@ -273,6 +268,14 @@ impl NetworkConfigBuilder {
self
}

/// Sets the port of the address the discovery network will listen on.
///
/// By default, this is [DEFAULT_DISCOVERY_PORT](reth_discv4::DEFAULT_DISCOVERY_PORT)
pub fn discovery_port(mut self, port: u16) -> Self {
self.discovery_addr.get_or_insert(DEFAULT_DISCOVERY_ADDRESS).set_port(port);
self
}

/// Sets the discv4 config to use.
pub fn discovery(mut self, builder: Discv4ConfigBuilder) -> Self {
self.discovery_v4_builder = Some(builder);
Expand Down Expand Up @@ -369,9 +372,7 @@ impl NetworkConfigBuilder {
head,
} = self;

let listener_addr = listener_addr.unwrap_or_else(|| {
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, DEFAULT_DISCOVERY_PORT))
});
let listener_addr = listener_addr.unwrap_or(DEFAULT_DISCOVERY_ADDRESS);

let mut hello_message =
hello_message.unwrap_or_else(|| HelloMessage::builder(peer_id).build());
Expand Down Expand Up @@ -408,9 +409,7 @@ impl NetworkConfigBuilder {
boot_nodes,
dns_discovery_config,
discovery_v4_config: discovery_v4_builder.map(|builder| builder.build()),
discovery_addr: discovery_addr.unwrap_or_else(|| {
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, DEFAULT_DISCOVERY_PORT))
}),
discovery_addr: discovery_addr.unwrap_or(DEFAULT_DISCOVERY_ADDRESS),
listener_addr,
peers_config: peers_config.unwrap_or_default(),
sessions_config: sessions_config.unwrap_or_default(),
Expand Down

0 comments on commit fc9f116

Please sign in to comment.