]> Untitled Git - bdk/commitdiff
[bdk_chain_redesign] Remove incomplete logic
author志宇 <hello@evanlinjin.me>
Wed, 19 Apr 2023 04:21:39 +0000 (12:21 +0800)
committer志宇 <hello@evanlinjin.me>
Wed, 19 Apr 2023 04:21:39 +0000 (12:21 +0800)
`ObservedAs::ConfirmedImplicit` is incomplete, remove for now.

`local_chain::ChangeSet` does not need to be a single-element tuple
struct.

crates/chain/src/chain_data.rs
crates/chain/src/indexed_tx_graph.rs
crates/chain/src/local_chain.rs
crates/chain/src/tx_graph.rs

index d8ce8cdad4fd7f28cf62798a797eefdf05cf42ca..33381ab737abd35c320f8521c2c8f483999d28bd 100644 (file)
@@ -12,9 +12,6 @@ use crate::{
 pub enum ObservedAs<A> {
     /// The chain data is seen as confirmed, and in anchored by `A`.
     Confirmed(A),
-    /// The chain data is assumed to be confirmed, because a transaction that spends it is anchored
-    /// by `A`.
-    ConfirmedImplicit(A),
     /// The chain data is seen in mempool at this given timestamp.
     Unconfirmed(u64),
 }
@@ -23,7 +20,6 @@ impl<A: Clone> ObservedAs<&A> {
     pub fn cloned(self) -> ObservedAs<A> {
         match self {
             ObservedAs::Confirmed(a) => ObservedAs::Confirmed(a.clone()),
-            ObservedAs::ConfirmedImplicit(a) => ObservedAs::ConfirmedImplicit(a.clone()),
             ObservedAs::Unconfirmed(last_seen) => ObservedAs::Unconfirmed(last_seen),
         }
     }
@@ -259,9 +255,6 @@ impl<A: Anchor + ConfirmationHeight> FullTxOut<ObservedAs<A>> {
 
         let tx_height = match &self.chain_position {
             ObservedAs::Confirmed(anchor) => anchor.confirmation_height(),
-            // although we do not know the exact confirm height, the returned height here is the
-            // "upper bound" so only false-negatives are possible
-            ObservedAs::ConfirmedImplicit(anchor) => anchor.confirmation_height(),
             ObservedAs::Unconfirmed(_) => {
                 debug_assert!(false, "coinbase tx can never be unconfirmed");
                 return false;
@@ -291,9 +284,6 @@ impl<A: Anchor + ConfirmationHeight> FullTxOut<ObservedAs<A>> {
 
         let confirmation_height = match &self.chain_position {
             ObservedAs::Confirmed(anchor) => anchor.confirmation_height(),
-            // although we do not know the exact confirm height, the returned height here is the
-            // "upper bound" so only false-negatives are possible
-            ObservedAs::ConfirmedImplicit(anchor) => anchor.confirmation_height(),
             ObservedAs::Unconfirmed(_) => return false,
         };
         if confirmation_height > tip {
index 00137c418f34f16faae71e703ec7ba6d21611139..fd5aa6d974a9759c06d9536e7d48562a2523c7f4 100644 (file)
@@ -251,7 +251,7 @@ impl<A: Anchor + ConfirmationHeight, I: OwnedIndexer> IndexedTxGraph<A, I> {
             let txout = res?;
 
             match &txout.chain_position {
-                ObservedAs::Confirmed(_) | ObservedAs::ConfirmedImplicit(_) => {
+                ObservedAs::Confirmed(_) => {
                     if txout.is_on_coinbase {
                         if txout.is_observed_as_mature(tip) {
                             confirmed += txout.txout.value;
index e3385e1b4fe0ce8b5414eca64f4cc794f3a2afe8..9ba64b284a1ae33370d90b1b3770e15f7d63c6cc 100644 (file)
@@ -1,21 +1,23 @@
-use core::{convert::Infallible, ops::Deref};
+use core::convert::Infallible;
 
 use alloc::collections::{BTreeMap, BTreeSet};
 use bitcoin::BlockHash;
 
 use crate::{Append, BlockId, ChainOracle};
 
+/// This is a local implementation of [`ChainOracle`].
+///
+/// TODO: We need a cache/snapshot thing for chain oracle.
+/// * Minimize calls to remotes.
+/// * Can we cache it forever? Should we drop stuff?
+/// * Assume anything deeper than (i.e. 10) blocks won't be reorged.
+/// * Is this a cache on txs or block? or both?
+/// TODO: Parents of children are confirmed if children are confirmed.
 #[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
 pub struct LocalChain {
     blocks: BTreeMap<u32, BlockHash>,
 }
 
-// [TODO] We need a cache/snapshot thing for chain oracle.
-// * Minimize calls to remotes.
-// * Can we cache it forever? Should we drop stuff?
-// * Assume anything deeper than (i.e. 10) blocks won't be reorged.
-// * Is this a cache on txs or block? or both?
-// [TODO] Parents of children are confirmed if children are confirmed.
 impl ChainOracle for LocalChain {
     type Error = Infallible;
 
@@ -114,12 +116,12 @@ impl LocalChain {
                 changeset.insert(*height, *new_hash);
             }
         }
-        Ok(ChangeSet(changeset))
+        Ok(changeset)
     }
 
     /// Applies the given `changeset`.
     pub fn apply_changeset(&mut self, mut changeset: ChangeSet) {
-        self.blocks.append(&mut changeset.0)
+        self.blocks.append(&mut changeset)
     }
 
     /// Updates [`LocalChain`] with an update [`LocalChain`].
@@ -135,7 +137,7 @@ impl LocalChain {
     }
 
     pub fn initial_changeset(&self) -> ChangeSet {
-        ChangeSet(self.blocks.clone())
+        self.blocks.clone()
     }
 
     pub fn heights(&self) -> BTreeSet<u32> {
@@ -146,25 +148,11 @@ impl LocalChain {
 /// This is the return value of [`determine_changeset`] and represents changes to [`LocalChain`].
 ///
 /// [`determine_changeset`]: LocalChain::determine_changeset
-#[derive(Debug, Default, Clone, PartialEq)]
-#[cfg_attr(
-    feature = "serde",
-    derive(serde::Deserialize, serde::Serialize),
-    serde(crate = "serde_crate")
-)]
-pub struct ChangeSet(pub(crate) BTreeMap<u32, BlockHash>);
-
-impl Deref for ChangeSet {
-    type Target = BTreeMap<u32, BlockHash>;
-
-    fn deref(&self) -> &Self::Target {
-        &self.0
-    }
-}
+type ChangeSet = BTreeMap<u32, BlockHash>;
 
 impl Append for ChangeSet {
     fn append(&mut self, mut other: Self) {
-        BTreeMap::append(&mut self.0, &mut other.0)
+        BTreeMap::append(self, &mut other)
     }
 }
 
index 7c32606a506ec21335b7596b49ea5fb580175dea..5d06bbda658ca8964892ccf5608dfddb2e37a374 100644 (file)
@@ -644,24 +644,6 @@ impl<A: Anchor> TxGraph<A> {
             }
         }
 
-        // If we cannot determine whether tx is in best chain, we can check whether a spending tx is
-        // confirmed and in best chain, and if so, it is guaranteed that this tx is in the best
-        // chain.
-        //
-        // [TODO] This logic is incomplete as we do not check spends of spends.
-        let spending_anchors = self
-            .spends
-            .range(OutPoint::new(txid, u32::MIN)..=OutPoint::new(txid, u32::MAX))
-            .flat_map(|(_, spending_txids)| spending_txids)
-            .filter_map(|spending_txid| self.txs.get(spending_txid))
-            .flat_map(|(_, spending_anchors, _)| spending_anchors);
-        for spending_anchor in spending_anchors {
-            match chain.is_block_in_chain(spending_anchor.anchor_block(), chain_tip)? {
-                Some(true) => return Ok(Some(ObservedAs::ConfirmedImplicit(spending_anchor))),
-                _ => continue,
-            }
-        }
-
         // The tx is not anchored to a block which is in the best chain, let's check whether we can
         // ignore it by checking conflicts!
         let tx = match tx_node {