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 } ;
7+ use crate :: { Anchor , BlockId , CanonicalParams , CanonicalView , Merge , TxGraph } ;
98use bdk_core:: { ChainQuery , ToBlockHash } ;
109pub use bdk_core:: { CheckPoint , CheckPointIter } ;
1110use bitcoin:: block:: Header ;
5756 Ok ( init_cp)
5857}
5958
60- /// This is a local implementation of [`ChainOracle`] .
59+ /// A local chain of checkpoints .
6160#[ derive( Debug , Clone ) ]
6261pub struct LocalChain < D = BlockHash > {
6362 tip : CheckPoint < D > ,
@@ -69,62 +68,37 @@ impl<D> PartialEq for LocalChain<D> {
6968 }
7069}
7170
72- impl < D > ChainOracle for LocalChain < D > {
73- type Error = Infallible ;
74-
75- fn is_block_in_chain (
76- & self ,
77- block : BlockId ,
78- chain_tip : BlockId ,
79- ) -> Result < Option < bool > , Self :: Error > {
71+ // Methods for `LocalChain<BlockHash>`
72+ impl LocalChain < BlockHash > {
73+ /// Check if a block is in the chain.
74+ ///
75+ /// # Arguments
76+ /// * `block` - The block to check
77+ /// * `chain_tip` - The chain tip to check against
78+ ///
79+ /// # Returns
80+ /// * `Some(true)` if the block is in the chain
81+ /// * `Some(false)` if the block is not in the chain
82+ /// * `None` if it cannot be determined
83+ pub fn is_block_in_chain ( & self , block : BlockId , chain_tip : BlockId ) -> Option < bool > {
8084 let chain_tip_cp = match self . tip . get ( chain_tip. height ) {
8185 // we can only determine whether `block` is in chain of `chain_tip` if `chain_tip` can
8286 // be identified in chain
8387 Some ( cp) if cp. hash ( ) == chain_tip. hash => cp,
84- _ => return Ok ( None ) ,
88+ _ => return None ,
8589 } ;
86- match chain_tip_cp. get ( block. height ) {
87- Some ( cp) => Ok ( Some ( cp. hash ( ) == block. hash ) ) ,
88- None => Ok ( None ) ,
89- }
90+ chain_tip_cp
91+ . get ( block. height )
92+ . map ( |cp| cp. hash ( ) == block. hash )
9093 }
9194
92- fn get_chain_tip ( & self ) -> Result < BlockId , Self :: Error > {
93- Ok ( self . tip . block_id ( ) )
95+ /// Get the chain tip.
96+ ///
97+ /// # Returns
98+ /// The [`BlockId`] of the chain tip.
99+ pub fn chain_tip ( & self ) -> BlockId {
100+ self . tip . block_id ( )
94101 }
95- }
96-
97- // Methods for `LocalChain<BlockHash>`
98- impl LocalChain < BlockHash > {
99- // /// Check if a block is in the chain.
100- // ///
101- // /// # Arguments
102- // /// * `block` - The block to check
103- // /// * `chain_tip` - The chain tip to check against
104- // ///
105- // /// # Returns
106- // /// * `Some(true)` if the block is in the chain
107- // /// * `Some(false)` if the block is not in the chain
108- // /// * `None` if it cannot be determined
109- // pub fn is_block_in_chain(&self, block: BlockId, chain_tip: BlockId) -> Option<bool> {
110- // let chain_tip_cp = match self.tip.get(chain_tip.height) {
111- // // we can only determine whether `block` is in chain of `chain_tip` if `chain_tip`
112- // can // be identified in chain
113- // Some(cp) if cp.hash() == chain_tip.hash => cp,
114- // _ => return None,
115- // };
116- // chain_tip_cp
117- // .get(block.height)
118- // .map(|cp| cp.hash() == block.hash)
119- // }
120-
121- // /// Get the chain tip.
122- // ///
123- // /// # Returns
124- // /// The [`BlockId`] of the chain tip.
125- // pub fn chain_tip(&self) -> BlockId {
126- // self.tip.block_id()
127- // }
128102
129103 /// Canonicalize a transaction graph using this chain.
130104 ///
@@ -151,11 +125,7 @@ impl LocalChain<BlockHash> {
151125 while let Some ( request) = task. next_query ( ) {
152126 let mut best_block_id = None ;
153127 for block_id in & request {
154- if self
155- . is_block_in_chain ( * block_id, chain_tip)
156- . expect ( "infallible" )
157- == Some ( true )
158- {
128+ if self . is_block_in_chain ( * block_id, chain_tip) == Some ( true ) {
159129 best_block_id = Some ( * block_id) ;
160130 break ;
161131 }
0 commit comments