From 0ec163790485c59b78df4358a7bce4e1fcf42035 Mon Sep 17 00:00:00 2001 From: rustaceanrob Date: Sun, 13 Oct 2024 10:11:02 -0700 Subject: [PATCH] refact: remove `Option` from whitelist --- src/core/builder.rs | 2 +- src/core/config.rs | 2 +- src/core/node.rs | 4 ++-- src/core/peer_map.rs | 33 +++++++++++++-------------------- 4 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src/core/builder.rs b/src/core/builder.rs index 4390539..300be20 100644 --- a/src/core/builder.rs +++ b/src/core/builder.rs @@ -83,7 +83,7 @@ impl NodeBuilder { /// Add preferred peers to try to connect to. pub fn add_peers(mut self, whitelist: Vec) -> Self { - self.config.white_list = Some(whitelist); + self.config.white_list = whitelist; self } diff --git a/src/core/config.rs b/src/core/config.rs index 0d03e27..6b4fb8a 100644 --- a/src/core/config.rs +++ b/src/core/config.rs @@ -12,7 +12,7 @@ const TIMEOUT_SECS: u64 = 5; pub(crate) struct NodeConfig { pub required_peers: u8, - pub white_list: Option>, + pub white_list: Vec, pub addresses: HashSet, pub data_path: Option, pub header_checkpoint: Option, diff --git a/src/core/node.rs b/src/core/node.rs index ce1e95f..b40265b 100644 --- a/src/core/node.rs +++ b/src/core/node.rs @@ -49,7 +49,7 @@ use super::{ pub(crate) const ADDR_V2_VERSION: u32 = 70015; const LOOP_TIMEOUT: u64 = 1; -type Whitelist = Option>; +type Whitelist = Vec; type PeerRequirement = usize; /// The state of the node with respect to connected peers. @@ -315,7 +315,7 @@ impl Node { }, ClientMessage::AddPeer(peer) => { let mut peer_map = self.peer_map.lock().await; - peer_map.add_peer(peer); + peer_map.add_trusted_peer(peer); } } } diff --git a/src/core/peer_map.rs b/src/core/peer_map.rs index eacb884..5650a28 100644 --- a/src/core/peer_map.rs +++ b/src/core/peer_map.rs @@ -40,7 +40,7 @@ use super::{ const MAX_TRIES: usize = 50; // Preferred peers to connect to based on the user configuration -type Whitelist = Option>; +type Whitelist = Vec; // A peer that is or was connected to the node #[derive(Debug)] @@ -159,13 +159,8 @@ impl PeerMap

{ } // Add a new trusted peer to the whitelist - pub fn add_peer(&mut self, peer: TrustedPeer) { - match &mut self.whitelist { - Some(peers) => { - peers.push(peer); - } - None => self.whitelist = Some(vec![peer]), - } + pub fn add_trusted_peer(&mut self, peer: TrustedPeer) { + self.whitelist.push(peer); } // Send out a TCP connection to a new peer and begin tracking the task @@ -265,18 +260,16 @@ impl PeerMap

{ // Pull a peer from the configuration if we have one. If not, select a random peer from the database, // as long as it is not from the same netgroup. If there are no peers in the database, try DNS. pub async fn next_peer(&mut self) -> Result> { - if let Some(whitelist) = &mut self.whitelist { - if let Some(peer) = whitelist.pop() { - self.dialog - .send_dialog("Using a configured peer.".into()) - .await; - let port = peer - .port - .unwrap_or(default_port_from_network(&self.network)); - let peer = - PersistedPeer::new(peer.address, port, peer.known_services, PeerStatus::Tried); - return Ok(peer); - } + if let Some(peer) = self.whitelist.pop() { + self.dialog + .send_dialog("Using a configured peer.".into()) + .await; + let port = peer + .port + .unwrap_or(default_port_from_network(&self.network)); + let peer = + PersistedPeer::new(peer.address, port, peer.known_services, PeerStatus::Tried); + return Ok(peer); } let current_count = { let mut peer_manager = self.db.lock().await;