1- //! The [`LocalChain`] is a local implementation of [`ChainOracle`] .
1+ //! The [`LocalChain`] is a local chain of checkpoints .
22
3- use core:: convert:: Infallible ;
43use core:: fmt;
54use core:: ops:: RangeBounds ;
65
76use crate :: collections:: BTreeMap ;
8- use crate :: { Anchor , BlockId , CanonicalParams , CanonicalView , ChainOracle , Merge , TxGraph } ;
9- use bdk_core:: { ChainQuery , CheckPointEntry , ToBlockHash } ;
10- pub use bdk_core:: { CheckPoint , CheckPointIter } ;
7+ use crate :: { Anchor , BlockId , CanonicalParams , CanonicalView , Merge , TxGraph } ;
8+ use bdk_core:: { ChainQuery , ToBlockHash } ;
9+ pub use bdk_core:: { CheckPoint , CheckPointEntry , CheckPointIter } ;
1110use bitcoin:: block:: Header ;
1211use bitcoin:: BlockHash ;
1312
6160 Ok ( init_cp)
6261}
6362
64- /// This is a local implementation of [`ChainOracle`] .
63+ /// A local chain of checkpoints .
6564#[ derive( Debug , Clone ) ]
6665pub struct LocalChain < D = BlockHash > {
6766 tip : CheckPoint < D > ,
@@ -73,33 +72,38 @@ impl<D> PartialEq for LocalChain<D> {
7372 }
7473}
7574
76- impl < D > ChainOracle for LocalChain < D > {
77- type Error = Infallible ;
78-
79- fn is_block_in_chain (
80- & self ,
81- block : BlockId ,
82- chain_tip : BlockId ,
83- ) -> Result < Option < bool > , Self :: Error > {
75+ // Methods for `LocalChain<BlockHash>`
76+ impl LocalChain < BlockHash > {
77+ /// Check if a block is in the chain.
78+ ///
79+ /// # Arguments
80+ /// * `block` - The block to check
81+ /// * `chain_tip` - The chain tip to check against
82+ ///
83+ /// # Returns
84+ /// * `Some(true)` if the block is in the chain
85+ /// * `Some(false)` if the block is not in the chain
86+ /// * `None` if it cannot be determined
87+ pub fn is_block_in_chain ( & self , block : BlockId , chain_tip : BlockId ) -> Option < bool > {
8488 let chain_tip_cp = match self . tip . get ( chain_tip. height ) {
8589 // we can only determine whether `block` is in chain of `chain_tip` if `chain_tip` can
8690 // be identified in chain
8791 Some ( cp) if cp. hash ( ) == chain_tip. hash => cp,
88- _ => return Ok ( None ) ,
92+ _ => return None ,
8993 } ;
90- match chain_tip_cp. get ( block. height ) {
91- Some ( cp) => Ok ( Some ( cp. hash ( ) == block. hash ) ) ,
92- None => Ok ( None ) ,
93- }
94+ chain_tip_cp
95+ . get ( block. height )
96+ . map ( |cp| cp. hash ( ) == block. hash )
9497 }
9598
96- fn get_chain_tip ( & self ) -> Result < BlockId , Self :: Error > {
97- Ok ( self . tip . block_id ( ) )
99+ /// Get the chain tip.
100+ ///
101+ /// # Returns
102+ /// The [`BlockId`] of the chain tip.
103+ pub fn chain_tip ( & self ) -> BlockId {
104+ self . tip . block_id ( )
98105 }
99- }
100106
101- // Methods for `LocalChain<BlockHash>`
102- impl LocalChain < BlockHash > {
103107 /// Canonicalize a transaction graph using this chain.
104108 ///
105109 /// This method processes any type implementing [`ChainQuery`], handling all its requests
@@ -125,11 +129,7 @@ impl LocalChain<BlockHash> {
125129 while let Some ( request) = task. next_query ( ) {
126130 let mut best_block_id = None ;
127131 for block_id in & request {
128- if self
129- . is_block_in_chain ( * block_id, chain_tip)
130- . expect ( "infallible" )
131- == Some ( true )
132- {
132+ if self . is_block_in_chain ( * block_id, chain_tip) == Some ( true ) {
133133 best_block_id = Some ( * block_id) ;
134134 break ;
135135 }
0 commit comments