Skip to content

Commit

Permalink
feat: add sibling_blocks to /block_info rpc
Browse files Browse the repository at this point in the history
The BlockInfo struct now has a sibling_blocks field that contains
digest of any other blocks at the same height.
  • Loading branch information
dan-da committed Dec 18, 2024
1 parent 3995e39 commit 0f0871b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/models/blockchain/block/block_info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! BlockInfo is a concise summary of a block intended for human
//! consumption/reporting in block explorers, cli, dashboard, etc.
use itertools::Itertools;
use serde::Deserialize;
use serde::Serialize;
use twenty_first::math::digest::Digest;
Expand Down Expand Up @@ -29,6 +30,7 @@ pub struct BlockInfo {
pub is_genesis: bool,
pub is_tip: bool,
pub is_canonical: bool,
pub sibling_blocks: Vec<Digest>, // blocks at same height
}

// note: this is used by neptune-cli block-info command.
Expand All @@ -50,17 +52,22 @@ impl std::fmt::Display for BlockInfo {
+ &format!("fee: {}\n", self.fee)
+ &format!("is_genesis: {}\n", self.is_genesis)
+ &format!("is_tip: {}\n", self.is_tip)
+ &format!("is_canonical: {}\n", self.is_canonical);
+ &format!("is_canonical: {}\n", self.is_canonical)
+ &format!(
"sibling_blocks: {}\n",
self.sibling_blocks.iter().map(|d| d.to_hex()).join(",")
);

write!(f, "{}", buf)
}
}

impl BlockInfo {
pub fn from_block_and_digests(
pub fn new(
block: &Block,
genesis_digest: Digest,
tip_digest: Digest,
sibling_blocks: Vec<Digest>, // other blocks at same height
is_canonical: bool,
) -> Self {
let body = block.body();
Expand All @@ -80,6 +87,7 @@ impl BlockInfo {
is_genesis: digest == genesis_digest,
is_tip: digest == tip_digest,
is_canonical,
sibling_blocks,
}
}
}
17 changes: 14 additions & 3 deletions src/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,10 +829,19 @@ impl RPC for NeptuneRPCServer {
.block_belongs_to_canonical_chain(digest, tip_digest)
.await;

Some(BlockInfo::from_block_and_digests(
// sibling blocks are those at the same height, with different digest
let sibling_blocks = archival_state
.block_height_to_block_digests(block.header().height)
.await
.into_iter()
.filter(|d| *d != digest)
.collect();

Some(BlockInfo::new(
&block,
archival_state.genesis_block().hash(),
tip_digest,
sibling_blocks,
is_canonical,
))
}
Expand Down Expand Up @@ -2119,21 +2128,23 @@ mod rpc_server_tests {
let genesis_hash = global_state.chain.archival_state().genesis_block().hash();
let tip_hash = global_state.chain.light_state().hash();

let genesis_block_info = BlockInfo::from_block_and_digests(
let genesis_block_info = BlockInfo::new(
global_state.chain.archival_state().genesis_block(),
genesis_hash,
tip_hash,
vec![],
global_state
.chain
.archival_state()
.block_belongs_to_canonical_chain(genesis_hash, tip_hash)
.await,
);

let tip_block_info = BlockInfo::from_block_and_digests(
let tip_block_info = BlockInfo::new(
global_state.chain.light_state(),
genesis_hash,
tip_hash,
vec![],
global_state
.chain
.archival_state()
Expand Down

0 comments on commit 0f0871b

Please sign in to comment.