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: 15 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tokio::sync::oneshot;

use crate::chain::block_subsidy;
use crate::messages::ClientRequest;
use crate::{Event, Info, TrustedPeer, Warning};
use crate::{Event, HeaderCheckpoint, Info, TrustedPeer, Warning};

use super::{error::ClientError, messages::ClientMessage};
use super::{error::FetchBlockError, IndexedBlock};
Expand Down Expand Up @@ -188,6 +188,20 @@ impl Requester {
.map_err(|_| ClientError::SendError)
}

/// The height and hash of the block in the chain of most work.
///
/// # Errors
///
/// If the node has stopped running.
pub async fn chain_tip(&self) -> Result<HeaderCheckpoint, ClientError> {
let (tx, rx) = tokio::sync::oneshot::channel::<HeaderCheckpoint>();
let request = ClientRequest::new((), tx);
self.ntx
.send(ClientMessage::BestBlock(request))
.map_err(|_| ClientError::SendError)?;
rx.await.map_err(|_| ClientError::RecvError)
}

/// Check if the node is running.
pub fn is_running(&self) -> bool {
self.ntx.send(ClientMessage::NoOp).is_ok()
Expand Down
2 changes: 2 additions & 0 deletions src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ pub(crate) enum ClientMessage {
Rescan,
/// Explicitly request a block from the node.
GetBlock(ClientRequest<BlockHash, Result<IndexedBlock, FetchBlockError>>),
/// Get the chain tip.
BestBlock(ClientRequest<(), HeaderCheckpoint>),
/// Add another known peer to connect to.
AddPeer(TrustedPeer),
/// Request the broadcast minimum fee rate.
Expand Down
11 changes: 11 additions & 0 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ impl Node {
self.block_queue.add(request);
}
},
ClientMessage::BestBlock(request) => {
let (_, oneshot) = request.into_values();
let block_tree = &self.chain.header_chain;
let hash = block_tree.tip_hash();
let height = block_tree.height();
let checkpoint = HeaderCheckpoint::new(height, hash);
let send_result = oneshot.send(checkpoint);
if send_result.is_err() {
self.dialog.send_warning(Warning::ChannelDropped);
};
},
ClientMessage::AddPeer(peer) => {
self.peer_map.add_trusted_peer(peer);
},
Expand Down
2 changes: 2 additions & 0 deletions tests/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ async fn various_client_methods() {
tokio::task::spawn(async move { print_logs(info_rx, warn_rx).await });
sync_assert(&best, &mut channel).await;
let _ = requester.broadcast_min_feerate().await.unwrap();
let cp = requester.chain_tip().await.unwrap();
assert_eq!(cp.hash, best);
assert!(requester.is_running());
requester.shutdown().unwrap();
rpc.stop().unwrap();
Expand Down