Skip to content

Commit

Permalink
feat(wallet)!: remove dependency on bdk_chain::Staged
Browse files Browse the repository at this point in the history
Introduce `Wallet::staged_mut` method.
  • Loading branch information
evanlinjin committed Aug 15, 2024
1 parent 06a9d6c commit a9c5f76
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
20 changes: 10 additions & 10 deletions crates/wallet/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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<ChangeSet> {
self.stage.take()
Expand Down
28 changes: 15 additions & 13 deletions crates/wallet/src/wallet/persisted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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`.
Expand All @@ -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)
}
}

Expand Down

0 comments on commit a9c5f76

Please sign in to comment.