]> Untitled Git - bdk/commitdiff
[wallet_redesign] Move the majority of `Update` to `bdk_chain`
author志宇 <hello@evanlinjin.me>
Thu, 11 May 2023 10:46:41 +0000 (18:46 +0800)
committer志宇 <hello@evanlinjin.me>
Sat, 3 Jun 2023 19:32:17 +0000 (03:32 +0800)
This is to make it easier for chain source crates to formulate updates.

crates/bdk/src/wallet/mod.rs
crates/chain/src/keychain.rs

index c894d6ba2a0454b8f9ffb71764d28772ce663674..2d2a70f92938796c794969d849e3e11a3cfe5dfe 100644 (file)
@@ -22,7 +22,7 @@ use alloc::{
 pub use bdk_chain::keychain::Balance;
 use bdk_chain::{
     indexed_tx_graph::{IndexedAdditions, IndexedTxGraph},
-    keychain::{DerivationAdditions, KeychainTxOutIndex},
+    keychain::{DerivationAdditions, KeychainTxOutIndex, LocalUpdate},
     local_chain::{self, LocalChain, UpdateNotConnectedError},
     tx_graph::{CanonicalTx, TxGraph},
     Anchor, Append, BlockId, ConfirmationTime, ConfirmationTimeAnchor, FullTxOut, ObservedAs,
@@ -94,21 +94,14 @@ pub struct Wallet<D = ()> {
 }
 
 /// The update to a [`Wallet`] used in [`Wallet::apply_update`]. This is usually returned from blockchain data sources.
-/// The type parameter `T` indicates the kind of transaction contained in the update. It's usually a [`bitcoin::Transaction`].
-#[derive(Debug, Default, PartialEq)]
-pub struct Update<K = KeychainKind, A = ConfirmationTimeAnchor> {
-    keychain: BTreeMap<K, u32>,
-    graph: TxGraph<A>,
-    chain: LocalChain,
-}
+pub type Update = LocalUpdate<KeychainKind, ConfirmationTimeAnchor>;
 
-/// The changeset produced internally by applying an update
+/// The changeset produced internally by applying an update.
 #[derive(Debug, PartialEq, serde::Deserialize, serde::Serialize)]
 #[serde(bound(
     deserialize = "A: Ord + serde::Deserialize<'de>, K: Ord + serde::Deserialize<'de>",
     serialize = "A: Ord + serde::Serialize, K: Ord + serde::Serialize"
 ))]
-// #[cfg_attr(predicate, attr)]
 pub struct ChangeSet<K = KeychainKind, A = ConfirmationTimeAnchor> {
     pub chain_changeset: local_chain::ChangeSet,
     pub indexed_additions: IndexedAdditions<A, DerivationAdditions<K>>,
index f4d398ab0c359c2b69abe78f652f36791f372b9b..0f108b2d46ad369fb302c7a9058463d296b12e5d 100644 (file)
@@ -18,6 +18,7 @@
 use crate::{
     chain_graph::{self, ChainGraph},
     collections::BTreeMap,
+    local_chain::LocalChain,
     sparse_chain::ChainPosition,
     tx_graph::TxGraph,
     Append, ForEachTxOut,
@@ -102,6 +103,28 @@ impl<K> AsRef<BTreeMap<K, u32>> for DerivationAdditions<K> {
     }
 }
 
+/// A structure to update [`KeychainTxOutIndex`], [`TxGraph`] and [`LocalChain`]
+/// atomically.
+#[derive(Debug, Clone, PartialEq)]
+pub struct LocalUpdate<K, A> {
+    /// Last active derivation index per keychain (`K`).
+    pub keychain: BTreeMap<K, u32>,
+    /// Update for the [`TxGraph`].
+    pub graph: TxGraph<A>,
+    /// Update for the [`LocalChain`].
+    pub chain: LocalChain,
+}
+
+impl<K, A> Default for LocalUpdate<K, A> {
+    fn default() -> Self {
+        Self {
+            keychain: Default::default(),
+            graph: Default::default(),
+            chain: Default::default(),
+        }
+    }
+}
+
 #[derive(Clone, Debug, PartialEq)]
 /// An update that includes the last active indexes of each keychain.
 pub struct KeychainScan<K, P> {