Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact: remove Option from whitelist #164

Merged
merged 1 commit into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl NodeBuilder {

/// Add preferred peers to try to connect to.
pub fn add_peers(mut self, whitelist: Vec<TrustedPeer>) -> Self {
self.config.white_list = Some(whitelist);
self.config.white_list = whitelist;
self
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const TIMEOUT_SECS: u64 = 5;

pub(crate) struct NodeConfig {
pub required_peers: u8,
pub white_list: Option<Vec<TrustedPeer>>,
pub white_list: Vec<TrustedPeer>,
pub addresses: HashSet<ScriptBuf>,
pub data_path: Option<PathBuf>,
pub header_checkpoint: Option<HeaderCheckpoint>,
Expand Down
4 changes: 2 additions & 2 deletions src/core/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use super::{
pub(crate) const ADDR_V2_VERSION: u32 = 70015;
const LOOP_TIMEOUT: u64 = 1;

type Whitelist = Option<Vec<TrustedPeer>>;
type Whitelist = Vec<TrustedPeer>;
type PeerRequirement = usize;

/// The state of the node with respect to connected peers.
Expand Down Expand Up @@ -315,7 +315,7 @@ impl<H: HeaderStore, P: PeerStore> Node<H, P> {
},
ClientMessage::AddPeer(peer) => {
let mut peer_map = self.peer_map.lock().await;
peer_map.add_peer(peer);
peer_map.add_trusted_peer(peer);
}
}
}
Expand Down
33 changes: 13 additions & 20 deletions src/core/peer_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use super::{
const MAX_TRIES: usize = 50;

// Preferred peers to connect to based on the user configuration
type Whitelist = Option<Vec<TrustedPeer>>;
type Whitelist = Vec<TrustedPeer>;

// A peer that is or was connected to the node
#[derive(Debug)]
Expand Down Expand Up @@ -159,13 +159,8 @@ impl<P: PeerStore> PeerMap<P> {
}

// 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
Expand Down Expand Up @@ -265,18 +260,16 @@ impl<P: PeerStore> PeerMap<P> {
// 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<PersistedPeer, PeerManagerError<P::Error>> {
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;
Expand Down
Loading