From aba88130d91b329d8637c450c84fd10af508bdac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=97=E5=AE=87?= Date: Thu, 11 May 2023 18:46:41 +0800 Subject: [PATCH] [wallet_redesign] Move the majority of `Update` to `bdk_chain` This is to make it easier for chain source crates to formulate updates. --- crates/bdk/src/wallet/mod.rs | 13 +++---------- crates/chain/src/keychain.rs | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/crates/bdk/src/wallet/mod.rs b/crates/bdk/src/wallet/mod.rs index c894d6ba..2d2a70f9 100644 --- a/crates/bdk/src/wallet/mod.rs +++ b/crates/bdk/src/wallet/mod.rs @@ -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 { } /// 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 { - keychain: BTreeMap, - graph: TxGraph, - chain: LocalChain, -} +pub type Update = LocalUpdate; -/// 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 { pub chain_changeset: local_chain::ChangeSet, pub indexed_additions: IndexedAdditions>, diff --git a/crates/chain/src/keychain.rs b/crates/chain/src/keychain.rs index f4d398ab..0f108b2d 100644 --- a/crates/chain/src/keychain.rs +++ b/crates/chain/src/keychain.rs @@ -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 AsRef> for DerivationAdditions { } } +/// A structure to update [`KeychainTxOutIndex`], [`TxGraph`] and [`LocalChain`] +/// atomically. +#[derive(Debug, Clone, PartialEq)] +pub struct LocalUpdate { + /// Last active derivation index per keychain (`K`). + pub keychain: BTreeMap, + /// Update for the [`TxGraph`]. + pub graph: TxGraph, + /// Update for the [`LocalChain`]. + pub chain: LocalChain, +} + +impl Default for LocalUpdate { + 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 {