fix: Skip PeerNetwork::bind() if re-using object

This commit is contained in:
Jeff Bencin
2024-05-09 10:44:44 -04:00
parent a5f4151c1b
commit c65fa5ce59
2 changed files with 25 additions and 2 deletions

View File

@@ -646,6 +646,24 @@ impl PeerNetwork {
Ok(())
}
/// Call `bind()` only if not already bound
/// Returns:
/// - `Ok(true)` if `bind()` call was successful
/// - `Ok(false)` if `bind()` call was skipped
/// - `Err()` if `bind()`` failed
#[cfg_attr(test, mutants::skip)]
pub fn try_bind(
&mut self,
my_addr: &SocketAddr,
http_addr: &SocketAddr,
) -> Result<bool, net_error> {
if self.network.is_some() {
// Already bound
return Ok(false);
}
self.bind(my_addr, http_addr).map(|()| true)
}
/// Get bound neighbor key. This is how this PeerNetwork appears to other nodes.
pub fn bound_neighbor_key(&self) -> &NeighborKey {
&self.bind_nk

View File

@@ -182,8 +182,13 @@ impl PeerThread {
.parse()
.unwrap_or_else(|_| panic!("Failed to parse socket: {}", &config.node.rpc_bind));
net.bind(&p2p_sock, &rpc_sock)
.expect("BUG: PeerNetwork could not bind or is already bound");
let did_bind = net
.try_bind(&p2p_sock, &rpc_sock)
.expect("BUG: PeerNetwork could not bind");
if !did_bind {
info!("`PeerNetwork::bind()` skipped, already bound");
}
let poll_timeout = cmp::min(5000, config.miner.first_attempt_time_ms / 2);