Skip to content

Commit 9d2b996

Browse files
committed
feat: add height_of_hash to Requester API
Expose BlockTree::height_of_hash through the Requester channel. Given a block hash, returns its height in the chain of most work, or None if the hash is not in the chain. Zero-cost local HashMap lookup — no network fetch needed.
1 parent 511e94a commit 9d2b996

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/client.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,24 @@ impl Requester {
221221
rx.await.map_err(|_| ClientError::RecvError)
222222
}
223223

224+
/// Look up the height of a block hash in the locally synced chain of most work.
225+
/// Returns `None` if the hash is not in the header chain.
226+
///
227+
/// # Errors
228+
///
229+
/// If the node has stopped running.
230+
pub async fn height_of_hash(
231+
&self,
232+
hash: BlockHash,
233+
) -> Result<Option<u32>, ClientError> {
234+
let (tx, rx) = tokio::sync::oneshot::channel::<Option<u32>>();
235+
let request = ClientRequest::new(hash, tx);
236+
self.ntx
237+
.send(ClientMessage::HeightOfHash(request))
238+
.map_err(|_| ClientError::SendError)?;
239+
rx.await.map_err(|_| ClientError::RecvError)
240+
}
241+
224242
/// Check if the node is running.
225243
pub fn is_running(&self) -> bool {
226244
self.ntx.send(ClientMessage::NoOp).is_ok()

src/messages.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ pub(crate) enum ClientMessage {
153153
GetBroadcastMinFeeRate(ClientRequest<(), FeeRate>),
154154
/// Get info on connections
155155
GetPeerInfo(ClientRequest<(), Vec<(AddrV2, ServiceFlags)>>),
156+
/// Look up the height of a block hash in the chain of most work.
157+
HeightOfHash(ClientRequest<BlockHash, Option<u32>>),
156158
/// Send an empty message to see if the node is running.
157159
NoOp,
158160
}

src/node.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,14 @@ impl Node {
268268
self.dialog.send_warning(Warning::ChannelDropped);
269269
};
270270
}
271+
ClientMessage::HeightOfHash(request) => {
272+
let (hash, oneshot) = request.into_values();
273+
let height =
274+
self.chain.header_chain.height_of_hash(hash);
275+
if oneshot.send(height).is_err() {
276+
self.dialog.send_warning(Warning::ChannelDropped);
277+
};
278+
}
271279
ClientMessage::NoOp => (),
272280
}
273281
}

0 commit comments

Comments
 (0)