From: 志宇 Date: Sun, 11 Aug 2024 09:28:53 +0000 (+0000) Subject: feat(wallet)!: remove dependency on `bdk_chain::Staged` X-Git-Tag: v1.0.0-beta.2~7^2~3 X-Git-Url: http://internal-gitweb-vhost/script/%22https:/database/scripts/rpc/static/gitweb.js?a=commitdiff_plain;h=a9c5f761c4c6834f8a05f2eb64feb39d23b74c2a;p=bdk feat(wallet)!: remove dependency on `bdk_chain::Staged` Introduce `Wallet::staged_mut` method. --- diff --git a/crates/wallet/src/wallet/mod.rs b/crates/wallet/src/wallet/mod.rs index f98b16e9..4cd721ba 100644 --- a/crates/wallet/src/wallet/mod.rs +++ b/crates/wallet/src/wallet/mod.rs @@ -45,7 +45,6 @@ use bitcoin::{ use bitcoin::{consensus::encode::serialize, transaction, BlockHash, Psbt}; use bitcoin::{constants::genesis_block, Amount}; use bitcoin::{secp256k1::Secp256k1, Weight}; -use chain::Staged; use core::fmt; use core::mem; use core::ops::Deref; @@ -123,14 +122,6 @@ pub struct Wallet { secp: SecpCtx, } -impl Staged for Wallet { - type ChangeSet = ChangeSet; - - fn staged(&mut self) -> &mut Self::ChangeSet { - &mut self.stage - } -} - /// An update to [`Wallet`]. /// /// It updates [`KeychainTxOutIndex`], [`bdk_chain::TxGraph`] and [`local_chain::LocalChain`] atomically. @@ -2303,7 +2294,7 @@ impl Wallet { Ok(()) } - /// Get a reference of the staged [`ChangeSet`] that are yet to be committed (if any). + /// Get a reference of the staged [`ChangeSet`] that is yet to be committed (if any). pub fn staged(&self) -> Option<&ChangeSet> { if self.stage.is_empty() { None @@ -2312,6 +2303,15 @@ impl Wallet { } } + /// Get a mutable reference of the staged [`ChangeSet`] that is yet to be commited (if any). + pub fn staged_mut(&mut self) -> Option<&mut ChangeSet> { + if self.stage.is_empty() { + None + } else { + Some(&mut self.stage) + } + } + /// Take the staged [`ChangeSet`] to be persisted now (if any). pub fn take_staged(&mut self) -> Option { self.stage.take() diff --git a/crates/wallet/src/wallet/persisted.rs b/crates/wallet/src/wallet/persisted.rs index bef34a1f..cebc3fbd 100644 --- a/crates/wallet/src/wallet/persisted.rs +++ b/crates/wallet/src/wallet/persisted.rs @@ -6,7 +6,7 @@ use core::{ }; use alloc::boxed::Box; -use chain::{Merge, Staged}; +use chain::Merge; use crate::{descriptor::DescriptorError, ChangeSet, CreateParams, LoadParams, Wallet}; @@ -206,13 +206,14 @@ impl PersistedWallet { where P: WalletPersister, { - let stage = Staged::staged(&mut self.0); - if stage.is_empty() { - return Ok(false); + match self.0.staged_mut() { + Some(stage) => { + P::persist(persister, &*stage)?; + let _ = stage.take(); + Ok(true) + } + None => Ok(false), } - P::persist(persister, &*stage)?; - stage.take(); - Ok(true) } /// Persist staged changes of wallet into an async `persister`. @@ -222,13 +223,14 @@ impl PersistedWallet { where P: AsyncWalletPersister, { - let stage = Staged::staged(&mut self.0); - if stage.is_empty() { - return Ok(false); + match self.0.staged_mut() { + Some(stage) => { + P::persist(persister, &*stage).await?; + let _ = stage.take(); + Ok(true) + } + None => Ok(false), } - P::persist(persister, &*stage).await?; - stage.take(); - Ok(true) } }