Skip to content

Commit

Permalink
feat(RpcServer): Show block digests by height
Browse files Browse the repository at this point in the history
Add an endpoint that returns known block digests by height. This can be
used to observe any reorganizations and block races happening.
  • Loading branch information
Sword-Smith committed Dec 12, 2024
1 parent f84c59c commit f5ee1b0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/models/blockchain/block/block_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ impl FromStr for BlockSelector {
}

impl BlockSelector {
/// returns Digest for this selector, if it exists.
/// returns Digest for this selector, if it exists. Returns the digest of
/// the block belonging to the canonical chain if multiple blocks with
/// same height is found.
pub async fn as_digest(&self, state: &GlobalState) -> Option<Digest> {
match self {
BlockSelector::Digest(d) => Some(*d),
Expand Down
28 changes: 28 additions & 0 deletions src/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ pub trait RPC {
/// Returns information about the specified block if found
async fn block_info(block_selector: BlockSelector) -> Option<BlockInfo>;

/// Return the digests of known blocks with specified height.
async fn block_digests_by_height(height: BlockHeight) -> Vec<Digest>;

/// Return the digest for the specified block if found
async fn block_digest(block_selector: BlockSelector) -> Option<Digest>;

Expand Down Expand Up @@ -828,6 +831,23 @@ impl RPC for NeptuneRPCServer {
))
}

// documented in trait. do not add doc-comment.
async fn block_digests_by_height(
self,
_: context::Context,
height: BlockHeight,
) -> Vec<Digest> {
log_slow_scope!(fn_name!());

self.state
.lock_guard()
.await
.chain
.archival_state()
.block_height_to_block_digests(height)
.await
}

// documented in trait. do not add doc-comment.
async fn latest_tip_digests(self, _context: tarpc::context::Context, n: usize) -> Vec<Digest> {
log_slow_scope!(fn_name!());
Expand Down Expand Up @@ -1646,6 +1666,14 @@ mod rpc_server_tests {
let _ = rpc_server.clone().own_instance_id(ctx).await;
let _ = rpc_server.clone().block_height(ctx).await;
let _ = rpc_server.clone().peer_info(ctx).await;
let _ = rpc_server
.clone()
.block_digests_by_height(ctx, 42u64.into())
.await;
let _ = rpc_server
.clone()
.block_digests_by_height(ctx, 0u64.into())
.await;
let _ = rpc_server.clone().all_punished_peers(ctx).await;
let _ = rpc_server.clone().latest_tip_digests(ctx, 2).await;
let _ = rpc_server
Expand Down

0 comments on commit f5ee1b0

Please sign in to comment.