Skip to content

Commit

Permalink
support 127.x.y.z isonets on custom genesis blocks with custom bootst…
Browse files Browse the repository at this point in the history
…rap peers
  • Loading branch information
Meshiest committed Sep 11, 2024
1 parent 0cc6fe3 commit f0ca2ed
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
9 changes: 8 additions & 1 deletion cli/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ pub struct Start {
/// If development mode is enabled, specify the custom bonded balances as a JSON object (default: None)
#[clap(long)]
pub dev_bonded_balances: Option<BondedBalances>,
/// Specify the path to an alternative genesis block
#[clap(long)]
pub genesis: Option<PathBuf>,
}

impl Start {
Expand Down Expand Up @@ -474,7 +477,11 @@ impl Start {
eprintln!("The '--dev-num-validators' flag is ignored because '--dev' is not set");
}

Block::from_bytes_le(N::genesis_bytes())
if let Some(path) = self.genesis.as_ref() {
Block::read_le(std::fs::File::open(path)?)
} else {
Block::from_bytes_le(N::genesis_bytes())
}
}
}

Expand Down
10 changes: 7 additions & 3 deletions node/bft/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,16 @@ impl<N: Network> Gateway<N> {
/// Returns `true` if the given IP is this node.
pub fn is_local_ip(&self, ip: SocketAddr) -> bool {
ip == self.local_ip()
|| (ip.ip().is_unspecified() || ip.ip().is_loopback()) && ip.port() == self.local_ip().port()
|| (ip.ip().is_unspecified() || ip.ip().is_loopback() && ip.ip() == self.local_ip().ip())
&& ip.port() == self.local_ip().port()
}

/// Returns `true` if the given IP is not this node, is not a bogon address, and is not unspecified.
pub fn is_valid_peer_ip(&self, ip: SocketAddr) -> bool {
!self.is_local_ip(ip) && !is_bogon_ip(ip.ip()) && !is_unspecified_or_broadcast_ip(ip.ip())
!self.is_local_ip(ip)
// Ignore bogon case for unique loopback ip connections (127.0.0.1 -> 127.0.0.2)
&& (!is_bogon_ip(ip.ip()) || self.local_ip().ip().is_loopback() && ip.ip().is_loopback())
&& !is_unspecified_or_broadcast_ip(ip.ip())
}

/// Returns the resolver.
Expand Down Expand Up @@ -448,7 +452,7 @@ impl<N: Network> Gateway<N> {
bail!("{CONTEXT} Dropping connection request from '{peer_ip}' (already connected)")
}
// Ensure the peer is not spamming connection attempts.
if !peer_ip.ip().is_loopback() {
if !self.tcp.is_self_connect(peer_ip) {
// Add this connection attempt and retrieve the number of attempts.
let num_attempts = self.cache.insert_inbound_connection(peer_ip.ip(), RESTRICTED_INTERVAL);
// Ensure the connecting peer has not surpassed the connection attempt limit.
Expand Down
2 changes: 1 addition & 1 deletion node/router/src/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ impl<N: Network> Router<N> {
bail!("Dropping connection request from '{peer_ip}' (restricted)")
}
// Ensure the peer is not spamming connection attempts.
if !peer_ip.ip().is_loopback() {
if !self.tcp.is_self_connect(peer_ip) {
// Add this connection attempt and retrieve the number of attempts.
let num_attempts = self.cache.insert_inbound_connection(peer_ip.ip(), Self::RADIO_SILENCE_IN_SECS as i64);
// Ensure the connecting peer has not surpassed the connection attempt limit.
Expand Down
13 changes: 10 additions & 3 deletions node/router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,16 @@ impl<N: Network> Router<N> {
/// Returns `true` if the given IP is this node.
pub fn is_local_ip(&self, ip: &SocketAddr) -> bool {
*ip == self.local_ip()
|| (ip.ip().is_unspecified() || ip.ip().is_loopback()) && ip.port() == self.local_ip().port()
|| (ip.ip().is_unspecified() || ip.ip().is_loopback() && ip.ip() == self.local_ip().ip())
&& ip.port() == self.local_ip().port()
}

/// Returns `true` if the given IP is not this node, is not a bogon address, and is not unspecified.
pub fn is_valid_peer_ip(&self, ip: &SocketAddr) -> bool {
!self.is_local_ip(ip) && !is_bogon_ip(ip.ip()) && !is_unspecified_or_broadcast_ip(ip.ip())
!self.is_local_ip(ip)
// Ignore bogon case for unique loopback ip connections (127.0.0.1 -> 127.0.0.2)
&& (!is_bogon_ip(ip.ip()) || self.local_ip().ip().is_loopback() && ip.ip().is_loopback())
&& !is_unspecified_or_broadcast_ip(ip.ip())
}

/// Returns the node type.
Expand Down Expand Up @@ -392,7 +396,10 @@ impl<N: Network> Router<N> {
/// Returns the list of bootstrap peers.
#[allow(clippy::if_same_then_else)]
pub fn bootstrap_peers(&self) -> Vec<SocketAddr> {
if cfg!(feature = "test") || self.is_dev {
// If the BOOTSTRAP_PEERS environment variable is set, use it.
if let Ok(bootstraps) = std::env::var("BOOTSTRAP_PEERS") {
bootstraps.split(',').filter_map(|peer| SocketAddr::from_str(peer).ok()).collect()
} else if cfg!(feature = "test") || self.is_dev {
// Development testing contains no bootstrap peers.
vec![]
} else if N::ID == snarkvm::console::network::MainnetV0::ID {
Expand Down
9 changes: 6 additions & 3 deletions node/tcp/src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,16 +393,19 @@ impl Tcp {
}

/// Checks if the given IP address is the same as the listening address of this `Tcp`.
fn is_self_connect(&self, addr: SocketAddr) -> bool {
pub fn is_self_connect(&self, addr: SocketAddr) -> bool {
// SAFETY: if we're opening connections, this should never fail.
let listening_addr = self.listening_addr().unwrap();
let is_same_ip = listening_addr.ip() == addr.ip();

match listening_addr.ip().is_loopback() {
// If localhost, check the ports, this only works on outbound connections, since we
// don't know the ephemeral port a peer might be using if they initiate the connection.
true => listening_addr.port() == addr.port(),
//
// The is_same_ip check is significant as 127.0.0.1 != 127.0.0.2
true => is_same_ip && listening_addr.port() == addr.port(),
// If it's not localhost, matching IPs indicate a self-connect in both directions.
false => listening_addr.ip() == addr.ip(),
false => is_same_ip,
}
}

Expand Down

0 comments on commit f0ca2ed

Please sign in to comment.