Skip to content

Commit

Permalink
Merge pull request #163 from rustaceanrob/peer-10-13
Browse files Browse the repository at this point in the history
feat: add trusted peers with client message
  • Loading branch information
rustaceanrob authored Oct 13, 2024
2 parents ac6d43d + 35eaa4b commit 8d1bb6c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/core/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tokio::sync::broadcast;
pub use tokio::sync::broadcast::Receiver;
use tokio::sync::mpsc::Sender;

use crate::{IndexedBlock, TxBroadcast};
use crate::{IndexedBlock, TrustedPeer, TxBroadcast};

use super::{
error::ClientError,
Expand Down Expand Up @@ -186,6 +186,7 @@ macro_rules! impl_core_client {
.await
.map_err(|_| ClientError::SendError)
}

/// Set a new connection timeout for peers to respond to messages.
///
/// # Errors
Expand All @@ -201,6 +202,18 @@ macro_rules! impl_core_client {
.map_err(|_| ClientError::SendError)
}

/// Add another known peer to connect to.
///
/// # Errors
///
/// If the node has stopped running.
pub async fn add_peer(&self, peer: TrustedPeer) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::AddPeer(peer))
.await
.map_err(|_| ClientError::SendError)
}

/// Explicitly start the block filter syncing process. Note that the node will automatically download and check
/// filters unless the policy is to explicitly halt.
///
Expand Down
4 changes: 3 additions & 1 deletion src/core/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bitcoin::{block::Header, p2p::message_network::RejectReason, ScriptBuf, Txid
use crate::IndexedFilter;
use crate::{
chain::checkpoints::HeaderCheckpoint, DisconnectedHeader, IndexedBlock, IndexedTransaction,
TxBroadcast,
TrustedPeer, TxBroadcast,
};

use super::node::NodeState;
Expand Down Expand Up @@ -113,6 +113,8 @@ pub(crate) enum ClientMessage {
GetBlock(BlockHash),
/// Set a new connection timeout.
SetDuration(Duration),
/// Add another known peer to connect to.
AddPeer(TrustedPeer),
}

/// Warnings a node may issue while running.
Expand Down
4 changes: 4 additions & 0 deletions src/core/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ impl<H: HeaderStore, P: PeerStore> Node<H, P> {
ClientMessage::SetDuration(duration) => {
let mut peer_map = self.peer_map.lock().await;
peer_map.set_duration(duration);
},
ClientMessage::AddPeer(peer) => {
let mut peer_map = self.peer_map.lock().await;
peer_map.add_peer(peer);
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/core/peer_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ impl<P: PeerStore> PeerMap<P> {
self.response_timeout = duration;
}

// 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]),
}
}

// Send out a TCP connection to a new peer and begin tracking the task
pub async fn dispatch(&mut self, loaded_peer: PersistedPeer) -> Result<(), PeerError> {
let (ptx, prx) = mpsc::channel::<MainThreadMessage>(32);
Expand Down

0 comments on commit 8d1bb6c

Please sign in to comment.