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

feat: add trusted peers with client message #163

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
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
Loading