Skip to content

Commit

Permalink
ref: don't track node type on Peer struct
Browse files Browse the repository at this point in the history
The only purpose of this was for logging during the handshake and
creates a node-level dependency on the peer. The log detail will likely
be reinstated in future (perhaps by seeding the peerbook with the
configured beacons, sync providers and crawlers).
  • Loading branch information
niklaslong committed Sep 16, 2021
1 parent ab6e909 commit f347c8f
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 34 deletions.
3 changes: 0 additions & 3 deletions network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ pub const NOISE_BUF_LEN: usize = 65535;
/// The spec-compliant size of the noise tag field.
pub const NOISE_TAG_LEN: usize = 16;

/// The maximum amount of time in which a handshake with a bootnode can conclude before dropping the
/// connection; it should be no greater than the `peer_sync_interval`.
pub const HANDSHAKE_BOOTNODE_TIMEOUT_SECS: u8 = 10;
/// The maximum amount of time in which a handshake with a regular node can conclude before dropping the
/// connection; it should be no greater than the `peer_sync_interval`.
pub const HANDSHAKE_PEER_TIMEOUT_SECS: u8 = 5;
Expand Down
13 changes: 5 additions & 8 deletions network/src/peers/peer/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the snarkOS library. If not, see <https://www.gnu.org/licenses/>.

use std::net::SocketAddr;
use std::{net::SocketAddr, time::Duration};

use snow::TransportState;
use tokio::{
Expand Down Expand Up @@ -168,7 +168,7 @@ impl Peer {
let (mut reader, mut writer) = stream.into_split();

let result = tokio::time::timeout(
self.handshake_timeout(),
Duration::from_secs(crate::HANDSHAKE_PEER_TIMEOUT_SECS as u64),
initiator_handshake(self.address, &our_version, &mut writer, &mut reader),
)
.await;
Expand All @@ -185,10 +185,7 @@ impl Peer {
}
};

match self.is_beacon {
true => info!("Connected to peer discovery node {}", self.address),
false => info!("Connected to peer {}", self.address),
};
info!("Connected to peer {}", self.address);

Ok(PeerIOHandle {
reader: Some(reader),
Expand All @@ -205,7 +202,7 @@ impl Peer {
let (mut reader, mut writer) = stream.into_split();

let result = tokio::time::timeout(
Peer::peer_handshake_timeout(),
Duration::from_secs(crate::HANDSHAKE_PEER_TIMEOUT_SECS as u64),
responder_handshake(address, &our_version, &mut writer, &mut reader),
)
.await;
Expand All @@ -224,7 +221,7 @@ impl Peer {

let mut peer_address = address;
peer_address.set_port(data.version.listening_port);
let peer = Peer::new(peer_address, false);
let peer = Peer::new(peer_address);

info!("Connected to peer {}", peer_address);

Expand Down
16 changes: 1 addition & 15 deletions network/src/peers/peer/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use super::{network::*, outbound_handler::*};
pub struct Peer {
pub address: SocketAddr,
pub quality: PeerQuality,
pub is_beacon: bool,
#[serde(skip)]
pub queued_inbound_message_count: Arc<AtomicUsize>,
#[serde(skip)]
Expand All @@ -50,11 +49,10 @@ const FAILURE_EXPIRY_TIME: Duration = Duration::from_secs(15 * 60);
const FAILURE_THRESHOLD: usize = 5;

impl Peer {
pub fn new(address: SocketAddr, is_beacon: bool) -> Self {
pub fn new(address: SocketAddr) -> Self {
Self {
address,
quality: Default::default(),
is_beacon,
queued_inbound_message_count: Default::default(),
queued_outbound_message_count: Default::default(),

Expand Down Expand Up @@ -93,18 +91,6 @@ impl Peer {
self.quality.failures.len()
}

pub fn handshake_timeout(&self) -> Duration {
if self.is_beacon {
Duration::from_secs(crate::HANDSHAKE_BOOTNODE_TIMEOUT_SECS as u64)
} else {
Self::peer_handshake_timeout()
}
}

pub fn peer_handshake_timeout() -> Duration {
Duration::from_secs(crate::HANDSHAKE_PEER_TIMEOUT_SECS as u64)
}

pub(super) async fn run(
&mut self,
node: Node,
Expand Down
6 changes: 3 additions & 3 deletions network/src/peers/peer_book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl PeerBook {
metrics::decrement_gauge!(DISCONNECTED, 1.0);
peer
} else {
Peer::new(address, node.config.beacons().contains(&address))
Peer::new(address)
};
self.pending_connections.fetch_add(1, Ordering::SeqCst);
peer.connect(node, self.peer_events.clone());
Expand Down Expand Up @@ -235,15 +235,15 @@ impl PeerBook {
///
/// Adds the given address to the disconnected peers in this `PeerBook`.
///
pub async fn add_peer(&self, address: SocketAddr, is_beacon: bool) {
pub async fn add_peer(&self, address: SocketAddr) {
if self.connected_peers.contains_key(&address) || self.disconnected_peers.contains_key(&address) {
return;
}

// Add the given address to the map of disconnected peers.
if self
.disconnected_peers
.insert(address, Peer::new(address, is_beacon))
.insert(address, Peer::new(address))
.await
.is_none()
{
Expand Down
4 changes: 1 addition & 3 deletions network/src/peers/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,7 @@ impl Node {
// Inform the peer book that we found a peer.
// The peer book will determine if we have seen the peer before,
// and include the peer if it is new.
self.peer_book
.add_peer(*peer_address, self.config.beacons().contains(peer_address))
.await;
self.peer_book.add_peer(*peer_address).await;
}

if let Some(known_network) = self.known_network() {
Expand Down
4 changes: 2 additions & 2 deletions rpc/src/rpc_impl_protected.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ impl RpcImpl {
.map_err(|e| JsonRPCError::invalid_params(format!("Invalid params: {}.", e)))?;

for addr in &addresses {
self.node.peer_book.add_peer(*addr, false).await;
self.node.peer_book.add_peer(*addr).await;
}
self.node.connect_to_addresses(&addresses).await;

Expand Down Expand Up @@ -697,7 +697,7 @@ impl ProtectedRpcFunctions for RpcImpl {
let node = self.node.clone();
tokio::spawn(async move {
for addr in &addresses {
node.peer_book.add_peer(*addr, false).await;
node.peer_book.add_peer(*addr).await;
}
node.connect_to_addresses(&addresses).await
});
Expand Down

0 comments on commit f347c8f

Please sign in to comment.