]> Untitled Git - bdk/commitdiff
[bdk_chain_redesign] Introduce `Append` trait for additions
author志宇 <hello@evanlinjin.me>
Wed, 5 Apr 2023 09:29:20 +0000 (17:29 +0800)
committer志宇 <hello@evanlinjin.me>
Wed, 5 Apr 2023 09:29:20 +0000 (17:29 +0800)
Before, we were using `core::ops::AddAsign` but it was not the most
appropriate.

crates/chain/src/indexed_tx_graph.rs
crates/chain/src/keychain.rs
crates/chain/src/keychain/txout_index.rs
crates/chain/src/tx_data_traits.rs
example-crates/keychain_tracker_example_cli/src/lib.rs

index e2d71af1cc8d655b2ddd54dc3789ed1e46e15bc9..450d02b82752878fc012b9883ac84fa1330ccc53 100644 (file)
@@ -1,11 +1,11 @@
-use core::{convert::Infallible, ops::AddAssign};
+use core::convert::Infallible;
 
 use bitcoin::{OutPoint, Script, Transaction, TxOut};
 
 use crate::{
     keychain::Balance,
     tx_graph::{Additions, TxGraph, TxNode},
-    BlockAnchor, ChainOracle, FullTxOut, ObservedAs, TxIndex,
+    Append, BlockAnchor, ChainOracle, FullTxOut, ObservedAs, TxIndex,
 };
 
 /// An outwards-facing view of a transaction that is part of the *best chain*'s history.
@@ -50,18 +50,14 @@ impl<A, IA: Default> Default for IndexedAdditions<A, IA> {
     }
 }
 
-impl<A: BlockAnchor, IA: AddAssign> AddAssign for IndexedAdditions<A, IA> {
-    fn add_assign(&mut self, rhs: Self) {
-        let Self {
-            graph_additions,
-            index_additions: index_delta,
-            last_height,
-        } = rhs;
-        self.graph_additions.append(graph_additions);
-        self.index_additions += index_delta;
-        if self.last_height < last_height {
-            let last_height =
-                last_height.expect("must exist as it is larger than self.last_height");
+impl<A: BlockAnchor, IA: Append> Append for IndexedAdditions<A, IA> {
+    fn append(&mut self, other: Self) {
+        self.graph_additions.append(other.graph_additions);
+        self.index_additions.append(other.index_additions);
+        if self.last_height < other.last_height {
+            let last_height = other
+                .last_height
+                .expect("must exist as it is larger than self.last_height");
             self.last_height.replace(last_height);
         }
     }
@@ -201,7 +197,7 @@ impl<A: BlockAnchor, I: TxIndex> IndexedTxGraph<A, I> {
     ) -> IndexedAdditions<A, I::Additions>
     where
         T: Iterator<Item = &'t Transaction>,
-        I::Additions: Default + AddAssign,
+        I::Additions: Default + Append,
     {
         txs.filter_map(|tx| {
             if self.index.is_tx_relevant(tx) {
@@ -211,7 +207,7 @@ impl<A: BlockAnchor, I: TxIndex> IndexedTxGraph<A, I> {
             }
         })
         .fold(IndexedAdditions::default(), |mut acc, other| {
-            acc += other;
+            acc.append(other);
             acc
         })
     }
index 53da284f2545ba845fbf28fa0932f6fc3ae407b0..81503049bd9fac3ef1bfced1922368e5bc493e6a 100644 (file)
 //! [`KeychainChangeSet`]s.
 //!
 //! [`SpkTxOutIndex`]: crate::SpkTxOutIndex
-use core::ops::AddAssign;
 
 use crate::{
     chain_graph::{self, ChainGraph},
     collections::BTreeMap,
     sparse_chain::ChainPosition,
     tx_graph::TxGraph,
-    ForEachTxOut,
+    Append, ForEachTxOut,
 };
 
 #[cfg(feature = "miniscript")]
@@ -71,12 +70,12 @@ impl<K> DerivationAdditions<K> {
     }
 }
 
-impl<K: Ord> DerivationAdditions<K> {
+impl<K: Ord> Append for DerivationAdditions<K> {
     /// Append another [`DerivationAdditions`] into self.
     ///
     /// If the keychain already exists, increase the index when the other's index > self's index.
     /// If the keychain did not exist, append the new keychain.
-    pub fn append(&mut self, mut other: Self) {
+    fn append(&mut self, mut other: Self) {
         self.0.iter_mut().for_each(|(key, index)| {
             if let Some(other_index) = other.0.remove(key) {
                 *index = other_index.max(*index);
@@ -87,12 +86,6 @@ impl<K: Ord> DerivationAdditions<K> {
     }
 }
 
-impl<K: Ord> AddAssign for DerivationAdditions<K> {
-    fn add_assign(&mut self, rhs: Self) {
-        self.append(rhs)
-    }
-}
-
 impl<K> Default for DerivationAdditions<K> {
     fn default() -> Self {
         Self(Default::default())
index 101278b7a54844a21fd379e993aced5b82bffa14..fc4c4e62f2a78210fd06a9f64b65dee950560e8c 100644 (file)
@@ -7,6 +7,8 @@ use alloc::{borrow::Cow, vec::Vec};
 use bitcoin::{secp256k1::Secp256k1, OutPoint, Script, TxOut};
 use core::{fmt::Debug, ops::Deref};
 
+use crate::Append;
+
 use super::DerivationAdditions;
 
 /// Maximum [BIP32](https://bips.xyz/32) derivation index.
index 366fc34b8526625b8c78ce7ec2173add5070724c..716b45f18010fa81f15eb85e3ca62100a927a056 100644 (file)
@@ -56,6 +56,16 @@ impl BlockAnchor for (u32, BlockHash) {
     }
 }
 
+/// Trait that makes an object appendable.
+pub trait Append {
+    /// Append another object of the same type onto `self`.
+    fn append(&mut self, other: Self);
+}
+
+impl Append for () {
+    fn append(&mut self, _other: Self) {}
+}
+
 /// Represents an index of transaction data.
 pub trait TxIndex {
     /// The resultant "additions" when new transaction data is indexed.
index df42df1ac0691ca1049d06d85d3c487cc68f2d78..702cc2a255ab05148033aab54ab6ffe8250c7204 100644 (file)
@@ -13,7 +13,7 @@ use bdk_chain::{
         Descriptor, DescriptorPublicKey,
     },
     sparse_chain::{self, ChainPosition},
-    DescriptorExt, FullTxOut,
+    Append, DescriptorExt, FullTxOut,
 };
 use bdk_coin_select::{coin_select_bnb, CoinSelector, CoinSelectorOpt, WeightedValue};
 use bdk_file_store::KeychainStore;