From 0f0871bf3bc25a92725447034d1014e77c8170e5 Mon Sep 17 00:00:00 2001 From: danda Date: Wed, 18 Dec 2024 12:59:05 +0800 Subject: [PATCH] feat: add sibling_blocks to /block_info rpc The BlockInfo struct now has a sibling_blocks field that contains digest of any other blocks at the same height. --- src/models/blockchain/block/block_info.rs | 12 ++++++++++-- src/rpc_server.rs | 17 ++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/models/blockchain/block/block_info.rs b/src/models/blockchain/block/block_info.rs index fcf178566..6ac654839 100644 --- a/src/models/blockchain/block/block_info.rs +++ b/src/models/blockchain/block/block_info.rs @@ -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; @@ -29,6 +30,7 @@ pub struct BlockInfo { pub is_genesis: bool, pub is_tip: bool, pub is_canonical: bool, + pub sibling_blocks: Vec, // blocks at same height } // note: this is used by neptune-cli block-info command. @@ -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, // other blocks at same height is_canonical: bool, ) -> Self { let body = block.body(); @@ -80,6 +87,7 @@ impl BlockInfo { is_genesis: digest == genesis_digest, is_tip: digest == tip_digest, is_canonical, + sibling_blocks, } } } diff --git a/src/rpc_server.rs b/src/rpc_server.rs index e66ad7145..5df6a1ee8 100644 --- a/src/rpc_server.rs +++ b/src/rpc_server.rs @@ -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, )) } @@ -2119,10 +2128,11 @@ 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() @@ -2130,10 +2140,11 @@ mod rpc_server_tests { .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()