Skip to content
Open
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: 15 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,21 @@ impl Requester {
rx.await.map_err(|_| ClientError::RecvError)
}

/// Look up the height of a block hash in the locally synced chain of most work.
/// Returns `None` if the hash is not in the header chain.
///
/// # Errors
///
/// If the node has stopped running.
pub async fn height_of_hash(&self, hash: BlockHash) -> Result<Option<u32>, ClientError> {
let (tx, rx) = tokio::sync::oneshot::channel::<Option<u32>>();
let request = ClientRequest::new(hash, tx);
self.ntx
.send(ClientMessage::HeightOfHash(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 @@ -153,6 +153,8 @@ pub(crate) enum ClientMessage {
GetBroadcastMinFeeRate(ClientRequest<(), FeeRate>),
/// Get info on connections
GetPeerInfo(ClientRequest<(), Vec<(AddrV2, ServiceFlags)>>),
/// Look up the height of a block hash in the chain of most work.
HeightOfHash(ClientRequest<BlockHash, Option<u32>>),
/// Send an empty message to see if the node is running.
NoOp,
}
Expand Down
8 changes: 8 additions & 0 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,14 @@ impl Node {
self.dialog.send_warning(Warning::ChannelDropped);
};
}
ClientMessage::HeightOfHash(request) => {
let (hash, oneshot) = request.into_values();
let height =
self.chain.header_chain.height_of_hash(hash);
if oneshot.send(height).is_err() {
self.dialog.send_warning(Warning::ChannelDropped);
};
}
ClientMessage::NoOp => (),
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@ async fn various_client_methods() {
let peers = requester.peer_info().await.unwrap();
assert_eq!(peers.len(), 1);
assert!(requester.is_running());
// height_of_hash for the chain tip should return the tip height
let tip_height = requester.height_of_hash(cp.hash).await.unwrap();
assert!(tip_height.is_some());
assert_eq!(tip_height.unwrap(), cp.height);
// height_of_hash for an unknown hash should return None
let fake_hash: BlockHash = bitcoin::hashes::Hash::all_zeros();
let unknown = requester.height_of_hash(fake_hash).await.unwrap();
assert!(unknown.is_none());
requester.shutdown().unwrap();
rpc.stop().unwrap();
}
Expand Down