fix: docs and some minor refactoring
Shout out to @LLFourn for these suggestions. * Improve/fix `LocalChain` documentation * Refactor `TxGraph::missing_blocks` to make it more explicit that `last_block` has state. * `update_local_chain` method of `EsploraExt` and `EsploraAsyncExt` now returns a `local_chain::Update` instead of just a `CheckPoint`.
This commit is contained in:
		
							parent
							
								
									8bf7a997f7
								
							
						
					
					
						commit
						95312d4d05
					
				@ -113,17 +113,25 @@ impl IntoIterator for CheckPoint {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Represents an update to [`LocalChain`].
 | 
					/// A struct to update [`LocalChain`].
 | 
				
			||||||
 | 
					///
 | 
				
			||||||
 | 
					/// This is used as input for [`LocalChain::apply_update`]. It contains the update's chain `tip` and
 | 
				
			||||||
 | 
					/// a `bool` which signals whether this update can introduce blocks below the original chain's tip
 | 
				
			||||||
 | 
					/// without invalidating blocks residing on the original chain. Block-by-block syncing mechanisms
 | 
				
			||||||
 | 
					/// would typically create updates that builds upon the previous tip. In this case, this paramater
 | 
				
			||||||
 | 
					/// would be `false`. Script-pubkey based syncing mechanisms may not introduce transactions in a
 | 
				
			||||||
 | 
					/// chronological order so some updates require introducing older blocks (to anchor older
 | 
				
			||||||
 | 
					/// transactions). For script-pubkey based syncing, this parameter would typically be `true`.
 | 
				
			||||||
#[derive(Debug, Clone)]
 | 
					#[derive(Debug, Clone)]
 | 
				
			||||||
pub struct Update {
 | 
					pub struct Update {
 | 
				
			||||||
    /// The update's new [`CheckPoint`] tip.
 | 
					    /// The update chain's new tip.
 | 
				
			||||||
    pub tip: CheckPoint,
 | 
					    pub tip: CheckPoint,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Whether the update allows for introducing older blocks.
 | 
					    /// Whether the update allows for introducing older blocks.
 | 
				
			||||||
    ///
 | 
					    ///
 | 
				
			||||||
    /// Refer to [`LocalChain::apply_update`] for more.
 | 
					    /// Refer to [struct-level documentation] for more.
 | 
				
			||||||
    ///
 | 
					    ///
 | 
				
			||||||
    /// [`LocalChain::apply_update`]: crate::local_chain::LocalChain::apply_update
 | 
					    /// [struct-level documentation]: Update
 | 
				
			||||||
    pub introduce_older_blocks: bool,
 | 
					    pub introduce_older_blocks: bool,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -146,12 +154,6 @@ impl From<LocalChain> for BTreeMap<u32, BlockHash> {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl From<ChangeSet> for LocalChain {
 | 
					 | 
				
			||||||
    fn from(value: ChangeSet) -> Self {
 | 
					 | 
				
			||||||
        Self::from_changeset(value)
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
impl From<BTreeMap<u32, BlockHash>> for LocalChain {
 | 
					impl From<BTreeMap<u32, BlockHash>> for LocalChain {
 | 
				
			||||||
    fn from(value: BTreeMap<u32, BlockHash>) -> Self {
 | 
					    fn from(value: BTreeMap<u32, BlockHash>) -> Self {
 | 
				
			||||||
        Self::from_blocks(value)
 | 
					        Self::from_blocks(value)
 | 
				
			||||||
@ -244,18 +246,9 @@ impl LocalChain {
 | 
				
			|||||||
        self.tip.is_none()
 | 
					        self.tip.is_none()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Updates [`Self`] with the given `update_tip`.
 | 
					    /// Applies the given `update` to the chain.
 | 
				
			||||||
    ///
 | 
					    ///
 | 
				
			||||||
    /// `introduce_older_blocks` specifies whether the `update_tip`'s history can introduce blocks
 | 
					    /// The method returns [`ChangeSet`] on success. This represents the applied changes to `self`.
 | 
				
			||||||
    /// below the original chain's tip without invalidating blocks. Block-by-block syncing
 | 
					 | 
				
			||||||
    /// mechanisms would typically create updates that builds upon the previous tip. In this case,
 | 
					 | 
				
			||||||
    /// this paramater would be false. Script-pubkey based syncing mechanisms may not introduce
 | 
					 | 
				
			||||||
    /// transactions in a chronological order so some updates require introducing older blocks (to
 | 
					 | 
				
			||||||
    /// anchor older transactions). For script-pubkey based syncing, this parameter would typically
 | 
					 | 
				
			||||||
    /// be true.
 | 
					 | 
				
			||||||
    ///
 | 
					 | 
				
			||||||
    /// The method returns [`ChangeSet`] on success. This represents the applied changes to
 | 
					 | 
				
			||||||
    /// [`Self`].
 | 
					 | 
				
			||||||
    ///
 | 
					    ///
 | 
				
			||||||
    /// To update, the `update_tip` must *connect* with `self`. If `self` and `update_tip` has a
 | 
					    /// To update, the `update_tip` must *connect* with `self`. If `self` and `update_tip` has a
 | 
				
			||||||
    /// mutual checkpoint (same height and hash), it can connect if:
 | 
					    /// mutual checkpoint (same height and hash), it can connect if:
 | 
				
			||||||
@ -275,7 +268,7 @@ impl LocalChain {
 | 
				
			|||||||
    ///
 | 
					    ///
 | 
				
			||||||
    /// An error will occur if the update does not correctly connect with `self`.
 | 
					    /// An error will occur if the update does not correctly connect with `self`.
 | 
				
			||||||
    ///
 | 
					    ///
 | 
				
			||||||
    /// Refer to [module-level documentation] for more.
 | 
					    /// Refer to [`Update`] for more about the update struct.
 | 
				
			||||||
    ///
 | 
					    ///
 | 
				
			||||||
    /// [module-level documentation]: crate::local_chain
 | 
					    /// [module-level documentation]: crate::local_chain
 | 
				
			||||||
    pub fn apply_update(&mut self, update: Update) -> Result<ChangeSet, CannotConnectError> {
 | 
					    pub fn apply_update(&mut self, update: Update) -> Result<ChangeSet, CannotConnectError> {
 | 
				
			||||||
 | 
				
			|||||||
@ -603,19 +603,17 @@ impl<A: Anchor> TxGraph<A> {
 | 
				
			|||||||
    /// This works by scanning through anchors, and seeing whether the anchor block of the anchor
 | 
					    /// This works by scanning through anchors, and seeing whether the anchor block of the anchor
 | 
				
			||||||
    /// exists in the [`LocalChain`].
 | 
					    /// exists in the [`LocalChain`].
 | 
				
			||||||
    pub fn missing_blocks<'a>(&'a self, chain: &'a LocalChain) -> impl Iterator<Item = u32> + 'a {
 | 
					    pub fn missing_blocks<'a>(&'a self, chain: &'a LocalChain) -> impl Iterator<Item = u32> + 'a {
 | 
				
			||||||
 | 
					        let mut last_block = Option::<BlockId>::None;
 | 
				
			||||||
        self.anchors
 | 
					        self.anchors
 | 
				
			||||||
            .iter()
 | 
					            .iter()
 | 
				
			||||||
            .map(|(a, _)| a.anchor_block())
 | 
					            .map(|(a, _)| a.anchor_block())
 | 
				
			||||||
            .filter({
 | 
					            .filter(move |block| {
 | 
				
			||||||
                let mut last_block = Option::<BlockId>::None;
 | 
					 | 
				
			||||||
                move |block| {
 | 
					 | 
				
			||||||
                if last_block.as_ref() == Some(block) {
 | 
					                if last_block.as_ref() == Some(block) {
 | 
				
			||||||
                    false
 | 
					                    false
 | 
				
			||||||
                } else {
 | 
					                } else {
 | 
				
			||||||
                    last_block = Some(*block);
 | 
					                    last_block = Some(*block);
 | 
				
			||||||
                    true
 | 
					                    true
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            })
 | 
					            })
 | 
				
			||||||
            .filter_map(|block| match chain.heights().get(&block.height) {
 | 
					            .filter_map(|block| match chain.heights().get(&block.height) {
 | 
				
			||||||
                Some(chain_hash) if *chain_hash == block.hash => None,
 | 
					                Some(chain_hash) if *chain_hash == block.hash => None,
 | 
				
			||||||
 | 
				
			|||||||
@ -107,11 +107,11 @@ fn insert_relevant_txs() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
fn test_list_owned_txouts() {
 | 
					fn test_list_owned_txouts() {
 | 
				
			||||||
    // Create Local chains
 | 
					    // Create Local chains
 | 
				
			||||||
 | 
					    let local_chain = LocalChain::from(
 | 
				
			||||||
    let local_chain = (0..150)
 | 
					        (0..150)
 | 
				
			||||||
        .map(|i| (i as u32, Some(h!("random"))))
 | 
					            .map(|i| (i as u32, h!("random")))
 | 
				
			||||||
        .collect::<BTreeMap<u32, Option<BlockHash>>>();
 | 
					            .collect::<BTreeMap<u32, BlockHash>>(),
 | 
				
			||||||
    let local_chain = LocalChain::from(local_chain);
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Initiate IndexedTxGraph
 | 
					    // Initiate IndexedTxGraph
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -6,8 +6,8 @@ use bdk_chain::collections::{BTreeMap, BTreeSet};
 | 
				
			|||||||
use bdk_chain::{
 | 
					use bdk_chain::{
 | 
				
			||||||
    bitcoin::{BlockHash, Script},
 | 
					    bitcoin::{BlockHash, Script},
 | 
				
			||||||
    local_chain::{self, CheckPoint},
 | 
					    local_chain::{self, CheckPoint},
 | 
				
			||||||
 | 
					    BlockId, ConfirmationTimeAnchor, TxGraph,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
use bdk_chain::{BlockId, ConfirmationTimeAnchor, TxGraph};
 | 
					 | 
				
			||||||
use esplora_client::{Error, TxStatus};
 | 
					use esplora_client::{Error, TxStatus};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::{anchor_from_status, ASSUME_FINAL_DEPTH};
 | 
					use crate::{anchor_from_status, ASSUME_FINAL_DEPTH};
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user