Skip to content
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
16 changes: 16 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use bitcoin::p2p::address::AddrV2;
use bitcoin::p2p::ServiceFlags;
use bitcoin::{Amount, Transaction, Wtxid};
use bitcoin::{BlockHash, FeeRate};
use tokio::sync::mpsc;
Expand Down Expand Up @@ -166,6 +168,20 @@ impl Requester {
Ok(FeeRate::from_sat_per_kwu(fee_rate))
}

/// Get the current peer connections.
///
/// # Errors
///
/// If the node has stopped running.
pub async fn peer_info(&self) -> Result<Vec<(AddrV2, ServiceFlags)>, ClientError> {
let (tx, rx) = tokio::sync::oneshot::channel::<Vec<(AddrV2, ServiceFlags)>>();
let request = ClientRequest::new((), tx);
self.ntx
.send(ClientMessage::GetPeerInfo(request))
.map_err(|_| ClientError::SendError)?;
rx.await.map_err(|_| ClientError::RecvError)
}

/// Starting after the configured checkpoint, re-emit all block filters.
///
/// # Errors
Expand Down
4 changes: 4 additions & 0 deletions src/messages.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::collections::BTreeMap;
use std::ops::Div;

use bitcoin::p2p::address::AddrV2;
use bitcoin::p2p::ServiceFlags;
use bitcoin::{
block::Header, p2p::message_network::RejectReason, BlockHash, FeeRate, Transaction, Wtxid,
};
Expand Down Expand Up @@ -151,6 +153,8 @@ pub(crate) enum ClientMessage {
AddPeer(TrustedPeer),
/// Request the broadcast minimum fee rate.
GetBroadcastMinFeeRate(ClientRequest<(), FeeRate>),
/// Get info on connections
GetPeerInfo(ClientRequest<(), Vec<(AddrV2, ServiceFlags)>>),
/// Send an empty message to see if the node is running.
NoOp,
}
Expand Down
7 changes: 7 additions & 0 deletions src/network/peer_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ impl PeerMap {
.unwrap_or(FeeRate::BROADCAST_MIN)
}

pub fn peer_info(&self) -> Vec<(AddrV2, ServiceFlags)> {
self.map
.values()
.map(|peer| (peer.record.network_addr().0, peer.record.service_flags()))
.collect()
}

// Send a message to the specified peer
pub async fn send_message(&self, nonce: PeerId, message: MainThreadMessage) {
if let Some(peer) = self.map.get(&nonce) {
Expand Down
8 changes: 8 additions & 0 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ impl Node {
self.dialog.send_warning(Warning::ChannelDropped);
};
}
ClientMessage::GetPeerInfo(request) => {
let (_, oneshot) = request.into_values();
let peers = self.peer_map.peer_info();
let send_result = oneshot.send(peers);
if send_result.is_err() {
self.dialog.send_warning(Warning::ChannelDropped);
};
}
ClientMessage::NoOp => (),
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ async fn various_client_methods() {
let _ = requester.broadcast_min_feerate().await.unwrap();
let cp = requester.chain_tip().await.unwrap();
assert_eq!(cp.hash, best);
let peers = requester.peer_info().await.unwrap();
assert_eq!(peers.len(), 1);
assert!(requester.is_running());
requester.shutdown().unwrap();
rpc.stop().unwrap();
Expand Down