diff --git a/chronik/chronik-bridge/src/ffi.rs b/chronik/chronik-bridge/src/ffi.rs --- a/chronik/chronik-bridge/src/ffi.rs +++ b/chronik/chronik-bridge/src/ffi.rs @@ -116,6 +116,9 @@ /// Bridge bitcoind's classes to the shared struct [`Block`]. fn bridge_block(block: &CBlock, block_index: &CBlockIndex) -> Block; + /// Get a BlockInfo for this CBlockIndex. + fn get_block_info(block_index: &CBlockIndex) -> BlockInfo; + /// CBlockIndex::GetAncestor fn get_block_ancestor( block_index: &CBlockIndex, diff --git a/chronik/chronik-cpp/chronik_bridge.h b/chronik/chronik-cpp/chronik_bridge.h --- a/chronik/chronik-cpp/chronik_bridge.h +++ b/chronik/chronik-cpp/chronik_bridge.h @@ -55,6 +55,8 @@ Block bridge_block(const CBlock &block, const CBlockIndex &bindex); +BlockInfo get_block_info(const CBlockIndex &index); + const CBlockIndex &get_block_ancestor(const CBlockIndex &index, int32_t height); bool init_error(const rust::Str msg); diff --git a/chronik/chronik-cpp/chronik_bridge.cpp b/chronik/chronik-cpp/chronik_bridge.cpp --- a/chronik/chronik-cpp/chronik_bridge.cpp +++ b/chronik/chronik-cpp/chronik_bridge.cpp @@ -75,6 +75,13 @@ .undo_pos = bindex.nUndoPos}; } +BlockInfo get_block_info(const CBlockIndex &bindex) { + return { + .hash = chronik::util::HashToArray(bindex.GetBlockHash()), + .height = bindex.nHeight, + }; +} + const CBlockIndex &get_block_ancestor(const CBlockIndex &index, int32_t height) { const CBlockIndex *pindex = index.GetAncestor(height); diff --git a/chronik/test/chronikbridge_tests.cpp b/chronik/test/chronikbridge_tests.cpp --- a/chronik/test/chronikbridge_tests.cpp +++ b/chronik/test/chronikbridge_tests.cpp @@ -119,4 +119,22 @@ chronik_bridge::block_index_not_found); } +BOOST_FIXTURE_TEST_CASE(test_get_block_info, TestChain100Setup) { + const chronik_bridge::ChronikBridge bridge(m_node); + ChainstateManager &chainman = *Assert(m_node.chainman); + const CBlockIndex &tip = *chainman.ActiveTip(); + + chronik_bridge::BlockInfo expected_genesis_info{ + .hash = chronik::util::HashToArray( + GetConfig().GetChainParams().GenesisBlock().GetHash()), + .height = 0}; + BOOST_CHECK(chronik_bridge::get_block_info(*tip.GetAncestor(0)) == + expected_genesis_info); + + chronik_bridge::BlockInfo expected_tip_info{ + .hash = chronik::util::HashToArray(tip.GetBlockHash()), + .height = tip.nHeight}; + BOOST_CHECK(chronik_bridge::get_block_info(tip) == expected_tip_info); +} + BOOST_AUTO_TEST_SUITE_END()