From e5a10f7cc2e60de4d4f0417f30e3c43f736f7672 Mon Sep 17 00:00:00 2001 From: Steve Myers Date: Fri, 31 May 2024 17:40:58 -0500 Subject: [PATCH] refactor(persist): update file_store, sqlite, wallet to use bdk_chain::persist Also update examples and remove bdk_persist crate. --- Cargo.toml | 1 - crates/chain/src/persist.rs | 7 +- crates/file_store/Cargo.toml | 2 - crates/file_store/src/store.rs | 15 +- crates/persist/Cargo.toml | 22 - crates/persist/README.md | 5 - crates/persist/src/changeset.rs | 73 ---- crates/persist/src/lib.rs | 8 - crates/persist/src/persist.rs | 106 ----- crates/sqlite/Cargo.toml | 2 - crates/sqlite/src/store.rs | 41 +- crates/wallet/Cargo.toml | 2 - crates/wallet/examples/compiler.rs | 4 +- crates/wallet/src/wallet/error.rs | 9 - crates/wallet/src/wallet/export.rs | 2 +- crates/wallet/src/wallet/mod.rs | 404 +++++++----------- crates/wallet/tests/common.rs | 2 +- crates/wallet/tests/wallet.rs | 308 +++++++------ example-crates/example_cli/Cargo.toml | 1 - example-crates/example_cli/src/lib.rs | 26 +- example-crates/wallet_electrum/src/main.rs | 15 +- .../wallet_esplora_async/src/main.rs | 11 +- .../wallet_esplora_blocking/src/main.rs | 11 +- example-crates/wallet_rpc/src/main.rs | 16 +- 24 files changed, 392 insertions(+), 701 deletions(-) delete mode 100644 crates/persist/Cargo.toml delete mode 100644 crates/persist/README.md delete mode 100644 crates/persist/src/changeset.rs delete mode 100644 crates/persist/src/lib.rs delete mode 100644 crates/persist/src/persist.rs diff --git a/Cargo.toml b/Cargo.toml index ea1b8f5048..201bb21f3b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,6 @@ members = [ "crates/esplora", "crates/bitcoind_rpc", "crates/hwi", - "crates/persist", "crates/testenv", "example-crates/example_cli", "example-crates/example_electrum", diff --git a/crates/chain/src/persist.rs b/crates/chain/src/persist.rs index 3abdd7c114..fd3f90f9d7 100644 --- a/crates/chain/src/persist.rs +++ b/crates/chain/src/persist.rs @@ -9,10 +9,10 @@ use crate::{indexed_tx_graph, keychain, local_chain, Anchor, Append}; use bitcoin::Network; +use core::convert::Infallible; use core::default::Default; use core::fmt::{Debug, Display}; -use std::convert::Infallible; -use std::mem; +use core::mem; /// A changeset containing [`crate`] structures typically persisted together. #[derive(Debug, Clone, PartialEq)] @@ -214,6 +214,7 @@ mod test { use crate::persist::{Persist, StagedPersist}; use crate::Append; + use std::error::Error; use std::fmt::{self, Display, Formatter}; use std::prelude::rust_2015::{String, ToString}; use TestError::FailedWrite; @@ -234,6 +235,8 @@ mod test { } } + impl Error for TestError {} + #[derive(Clone, Default)] struct TestChangeSet(Option); diff --git a/crates/file_store/Cargo.toml b/crates/file_store/Cargo.toml index 09b16c140b..e323660c6c 100644 --- a/crates/file_store/Cargo.toml +++ b/crates/file_store/Cargo.toml @@ -11,9 +11,7 @@ authors = ["Bitcoin Dev Kit Developers"] readme = "README.md" [dependencies] -anyhow = { version = "1", default-features = false } bdk_chain = { path = "../chain", version = "0.15.0", features = [ "serde", "miniscript" ] } -bdk_persist = { path = "../persist", version = "0.3.0"} bincode = { version = "1" } serde = { version = "1", features = ["derive"] } diff --git a/crates/file_store/src/store.rs b/crates/file_store/src/store.rs index 6cea927657..09ed0e0c36 100644 --- a/crates/file_store/src/store.rs +++ b/crates/file_store/src/store.rs @@ -1,7 +1,6 @@ use crate::{bincode_options, EntryIter, FileError, IterError}; -use anyhow::anyhow; +use bdk_chain::persist::Persist; use bdk_chain::Append; -use bdk_persist::PersistBackend; use bincode::Options; use std::{ fmt::{self, Debug}, @@ -22,22 +21,24 @@ where marker: PhantomData, } -impl PersistBackend for Store +impl Persist for Store where C: Append + + Debug + serde::Serialize + serde::de::DeserializeOwned + core::marker::Send + core::marker::Sync, { - fn write_changes(&mut self, changeset: &C) -> anyhow::Result<()> { + type WriteError = io::Error; + type LoadError = AggregateChangesetsError; + + fn write_changes(&mut self, changeset: &C) -> Result<(), Self::WriteError> { self.append_changeset(changeset) - .map_err(|e| anyhow!(e).context("failed to write changes to persistence backend")) } - fn load_from_persistence(&mut self) -> anyhow::Result> { + fn load_changes(&mut self) -> Result, Self::LoadError> { self.aggregate_changesets() - .map_err(|e| anyhow!(e.iter_error).context("error loading from persistence backend")) } } diff --git a/crates/persist/Cargo.toml b/crates/persist/Cargo.toml deleted file mode 100644 index 446eea4afd..0000000000 --- a/crates/persist/Cargo.toml +++ /dev/null @@ -1,22 +0,0 @@ -[package] -name = "bdk_persist" -homepage = "https://bitcoindevkit.org" -version = "0.3.0" -repository = "https://github.com/bitcoindevkit/bdk" -documentation = "https://docs.rs/bdk_persist" -description = "Types that define data persistence of a BDK wallet" -keywords = ["bitcoin", "wallet", "persistence", "database"] -readme = "README.md" -license = "MIT OR Apache-2.0" -authors = ["Bitcoin Dev Kit Developers"] -edition = "2021" -rust-version = "1.63" - -[dependencies] -anyhow = { version = "1", default-features = false } -bdk_chain = { path = "../chain", version = "0.15.0", default-features = false } - -[features] -default = ["bdk_chain/std", "miniscript"] -serde = ["bdk_chain/serde"] -miniscript = ["bdk_chain/miniscript"] diff --git a/crates/persist/README.md b/crates/persist/README.md deleted file mode 100644 index c82354860a..0000000000 --- a/crates/persist/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# BDK Persist - -This crate is home to the [`PersistBackend`] trait which defines the behavior of a database to perform the task of persisting changes made to BDK data structures. - -The [`Persist`] type provides a convenient wrapper around a [`PersistBackend`] that allows staging changes before committing them. diff --git a/crates/persist/src/changeset.rs b/crates/persist/src/changeset.rs deleted file mode 100644 index b796b07f0e..0000000000 --- a/crates/persist/src/changeset.rs +++ /dev/null @@ -1,73 +0,0 @@ -#![cfg(feature = "miniscript")] - -use bdk_chain::{bitcoin::Network, indexed_tx_graph, keychain, local_chain, Anchor, Append}; - -/// Changes from a combination of [`bdk_chain`] structures. -#[derive(Debug, Clone, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(bdk_chain::serde::Deserialize, bdk_chain::serde::Serialize), - serde( - crate = "bdk_chain::serde", - bound( - deserialize = "A: Ord + bdk_chain::serde::Deserialize<'de>, K: Ord + bdk_chain::serde::Deserialize<'de>", - serialize = "A: Ord + bdk_chain::serde::Serialize, K: Ord + bdk_chain::serde::Serialize", - ), - ) -)] -pub struct CombinedChangeSet { - /// Changes to the [`LocalChain`](local_chain::LocalChain). - pub chain: local_chain::ChangeSet, - /// Changes to [`IndexedTxGraph`](indexed_tx_graph::IndexedTxGraph). - pub indexed_tx_graph: indexed_tx_graph::ChangeSet>, - /// Stores the network type of the transaction data. - pub network: Option, -} - -impl Default for CombinedChangeSet { - fn default() -> Self { - Self { - chain: Default::default(), - indexed_tx_graph: Default::default(), - network: None, - } - } -} - -impl Append for CombinedChangeSet { - fn append(&mut self, other: Self) { - Append::append(&mut self.chain, other.chain); - Append::append(&mut self.indexed_tx_graph, other.indexed_tx_graph); - if other.network.is_some() { - debug_assert!( - self.network.is_none() || self.network == other.network, - "network type must either be just introduced or remain the same" - ); - self.network = other.network; - } - } - - fn is_empty(&self) -> bool { - self.chain.is_empty() && self.indexed_tx_graph.is_empty() && self.network.is_none() - } -} - -impl From for CombinedChangeSet { - fn from(chain: local_chain::ChangeSet) -> Self { - Self { - chain, - ..Default::default() - } - } -} - -impl From>> - for CombinedChangeSet -{ - fn from(indexed_tx_graph: indexed_tx_graph::ChangeSet>) -> Self { - Self { - indexed_tx_graph, - ..Default::default() - } - } -} diff --git a/crates/persist/src/lib.rs b/crates/persist/src/lib.rs deleted file mode 100644 index e3824e1adc..0000000000 --- a/crates/persist/src/lib.rs +++ /dev/null @@ -1,8 +0,0 @@ -#![doc = include_str!("../README.md")] -#![no_std] -#![warn(missing_docs)] - -mod changeset; -mod persist; -pub use changeset::*; -pub use persist::*; diff --git a/crates/persist/src/persist.rs b/crates/persist/src/persist.rs deleted file mode 100644 index 5d9df3bf6d..0000000000 --- a/crates/persist/src/persist.rs +++ /dev/null @@ -1,106 +0,0 @@ -extern crate alloc; -use alloc::boxed::Box; -use bdk_chain::Append; -use core::fmt; - -/// `Persist` wraps a [`PersistBackend`] to create a convenient staging area for changes (`C`) -/// before they are persisted. -/// -/// Not all changes to the in-memory representation needs to be written to disk right away, so -/// [`Persist::stage`] can be used to *stage* changes first and then [`Persist::commit`] can be used -/// to write changes to disk. -pub struct Persist { - backend: Box + Send + Sync>, - stage: C, -} - -impl fmt::Debug for Persist { - fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> { - write!(fmt, "{:?}", self.stage)?; - Ok(()) - } -} - -impl Persist -where - C: Default + Append, -{ - /// Create a new [`Persist`] from [`PersistBackend`]. - pub fn new(backend: impl PersistBackend + Send + Sync + 'static) -> Self { - let backend = Box::new(backend); - Self { - backend, - stage: Default::default(), - } - } - - /// Stage a `changeset` to be committed later with [`commit`]. - /// - /// [`commit`]: Self::commit - pub fn stage(&mut self, changeset: C) { - self.stage.append(changeset) - } - - /// Get the changes that have not been committed yet. - pub fn staged(&self) -> &C { - &self.stage - } - - /// Commit the staged changes to the underlying persistence backend. - /// - /// Changes that are committed (if any) are returned. - /// - /// # Error - /// - /// Returns a backend-defined error if this fails. - pub fn commit(&mut self) -> anyhow::Result> { - if self.stage.is_empty() { - return Ok(None); - } - self.backend - .write_changes(&self.stage) - // if written successfully, take and return `self.stage` - .map(|_| Some(core::mem::take(&mut self.stage))) - } - - /// Stages a new changeset and commits it (along with any other previously staged changes) to - /// the persistence backend - /// - /// Convenience method for calling [`stage`] and then [`commit`]. - /// - /// [`stage`]: Self::stage - /// [`commit`]: Self::commit - pub fn stage_and_commit(&mut self, changeset: C) -> anyhow::Result> { - self.stage(changeset); - self.commit() - } -} - -/// A persistence backend for [`Persist`]. -/// -/// `C` represents the changeset; a datatype that records changes made to in-memory data structures -/// that are to be persisted, or retrieved from persistence. -pub trait PersistBackend { - /// Writes a changeset to the persistence backend. - /// - /// It is up to the backend what it does with this. It could store every changeset in a list or - /// it inserts the actual changes into a more structured database. All it needs to guarantee is - /// that [`load_from_persistence`] restores a keychain tracker to what it should be if all - /// changesets had been applied sequentially. - /// - /// [`load_from_persistence`]: Self::load_from_persistence - fn write_changes(&mut self, changeset: &C) -> anyhow::Result<()>; - - /// Return the aggregate changeset `C` from persistence. - fn load_from_persistence(&mut self) -> anyhow::Result>; -} - -impl PersistBackend for () { - fn write_changes(&mut self, _changeset: &C) -> anyhow::Result<()> { - Ok(()) - } - - fn load_from_persistence(&mut self) -> anyhow::Result> { - Ok(None) - } -} diff --git a/crates/sqlite/Cargo.toml b/crates/sqlite/Cargo.toml index fbbfc374f6..b986cbf6ef 100644 --- a/crates/sqlite/Cargo.toml +++ b/crates/sqlite/Cargo.toml @@ -11,9 +11,7 @@ authors = ["Bitcoin Dev Kit Developers"] readme = "README.md" [dependencies] -anyhow = { version = "1", default-features = false } bdk_chain = { path = "../chain", version = "0.15.0", features = ["serde", "miniscript"] } -bdk_persist = { path = "../persist", version = "0.3.0", features = ["serde"] } rusqlite = { version = "0.31.0", features = ["bundled"] } serde = { version = "1", features = ["derive"] } serde_json = "1" \ No newline at end of file diff --git a/crates/sqlite/src/store.rs b/crates/sqlite/src/store.rs index beeb9e0aa9..be1d512f6d 100644 --- a/crates/sqlite/src/store.rs +++ b/crates/sqlite/src/store.rs @@ -12,10 +12,10 @@ use std::str::FromStr; use std::sync::{Arc, Mutex}; use crate::Error; +use bdk_chain::persist::{CombinedChangeSet, Persist}; use bdk_chain::{ indexed_tx_graph, keychain, local_chain, tx_graph, Anchor, Append, DescriptorExt, DescriptorId, }; -use bdk_persist::CombinedChangeSet; /// Persists data in to a relational schema based [SQLite] database file. /// @@ -57,21 +57,23 @@ where } } -impl bdk_persist::PersistBackend for Store +impl Persist> for Store where K: Ord + for<'de> Deserialize<'de> + Serialize + Send, A: Anchor + for<'de> Deserialize<'de> + Serialize + Send, - C: Clone + From> + Into>, { - fn write_changes(&mut self, changeset: &C) -> anyhow::Result<()> { - self.write(&changeset.clone().into()) - .map_err(|e| anyhow::anyhow!(e).context("unable to write changes to sqlite database")) + type WriteError = Error; + type LoadError = Error; + + fn write_changes( + &mut self, + changeset: &CombinedChangeSet, + ) -> Result<(), Self::WriteError> { + self.write(changeset) } - fn load_from_persistence(&mut self) -> anyhow::Result> { - self.read() - .map(|c| c.map(Into::into)) - .map_err(|e| anyhow::anyhow!(e).context("unable to read changes from sqlite database")) + fn load_changes(&mut self) -> Result>, Self::LoadError> { + self.read().map(|c| c.map(Into::into)) } } @@ -561,11 +563,11 @@ mod test { use bdk_chain::bitcoin::Network::Testnet; use bdk_chain::bitcoin::{secp256k1, BlockHash, OutPoint}; use bdk_chain::miniscript::Descriptor; + use bdk_chain::persist::{CombinedChangeSet, Persist}; use bdk_chain::{ indexed_tx_graph, keychain, tx_graph, BlockId, ConfirmationHeightAnchor, ConfirmationTimeHeightAnchor, DescriptorExt, }; - use bdk_persist::PersistBackend; use std::str::FromStr; use std::sync::Arc; @@ -576,8 +578,7 @@ mod test { } #[test] - fn insert_and_load_aggregate_changesets_with_confirmation_time_height_anchor( - ) -> anyhow::Result<()> { + fn insert_and_load_aggregate_changesets_with_confirmation_time_height_anchor() { let (test_changesets, agg_test_changesets) = create_test_changesets(&|height, time, hash| ConfirmationTimeHeightAnchor { confirmation_height: height, @@ -593,15 +594,13 @@ mod test { store.write_changes(changeset).expect("write changeset"); }); - let agg_changeset = store.load_from_persistence().expect("aggregated changeset"); + let agg_changeset = store.load_changes().expect("aggregated changeset"); assert_eq!(agg_changeset, Some(agg_test_changesets)); - Ok(()) } #[test] - fn insert_and_load_aggregate_changesets_with_confirmation_height_anchor() -> anyhow::Result<()> - { + fn insert_and_load_aggregate_changesets_with_confirmation_height_anchor() { let (test_changesets, agg_test_changesets) = create_test_changesets(&|height, _time, hash| ConfirmationHeightAnchor { confirmation_height: height, @@ -616,14 +615,13 @@ mod test { store.write_changes(changeset).expect("write changeset"); }); - let agg_changeset = store.load_from_persistence().expect("aggregated changeset"); + let agg_changeset = store.load_changes().expect("aggregated changeset"); assert_eq!(agg_changeset, Some(agg_test_changesets)); - Ok(()) } #[test] - fn insert_and_load_aggregate_changesets_with_blockid_anchor() -> anyhow::Result<()> { + fn insert_and_load_aggregate_changesets_with_blockid_anchor() { let (test_changesets, agg_test_changesets) = create_test_changesets(&|height, _time, hash| BlockId { height, hash }); @@ -634,10 +632,9 @@ mod test { store.write_changes(changeset).expect("write changeset"); }); - let agg_changeset = store.load_from_persistence().expect("aggregated changeset"); + let agg_changeset = store.load_changes().expect("aggregated changeset"); assert_eq!(agg_changeset, Some(agg_test_changesets)); - Ok(()) } fn create_test_changesets( diff --git a/crates/wallet/Cargo.toml b/crates/wallet/Cargo.toml index 2a129e9595..178307eac3 100644 --- a/crates/wallet/Cargo.toml +++ b/crates/wallet/Cargo.toml @@ -13,14 +13,12 @@ edition = "2021" rust-version = "1.63" [dependencies] -anyhow = { version = "1", default-features = false } rand = "^0.8" miniscript = { version = "11.0.0", features = ["serde"], default-features = false } bitcoin = { version = "0.31.0", features = ["serde", "base64", "rand-std"], default-features = false } serde = { version = "^1.0", features = ["derive"] } serde_json = { version = "^1.0" } bdk_chain = { path = "../chain", version = "0.15.0", features = ["miniscript", "serde"], default-features = false } -bdk_persist = { path = "../persist", version = "0.3.0", features = ["miniscript", "serde"], default-features = false } # Optional dependencies bip39 = { version = "2.0", optional = true } diff --git a/crates/wallet/examples/compiler.rs b/crates/wallet/examples/compiler.rs index 116df4733a..5e14534ab1 100644 --- a/crates/wallet/examples/compiler.rs +++ b/crates/wallet/examples/compiler.rs @@ -46,11 +46,11 @@ fn main() -> Result<(), Box> { println!("Compiled into following Descriptor: \n{}", descriptor); // Create a new wallet from this descriptor - let mut wallet = Wallet::new_no_persist(&format!("{}", descriptor), None, Network::Regtest)?; + let mut wallet = Wallet::new(&format!("{}", descriptor), None, Network::Regtest)?; println!( "First derived address from the descriptor: \n{}", - wallet.next_unused_address(KeychainKind::External)?, + wallet.next_unused_address(KeychainKind::External), ); // BDK also has it's own `Policy` structure to represent the spending condition in a more diff --git a/crates/wallet/src/wallet/error.rs b/crates/wallet/src/wallet/error.rs index eaf811d6fc..4ae93c1edd 100644 --- a/crates/wallet/src/wallet/error.rs +++ b/crates/wallet/src/wallet/error.rs @@ -50,8 +50,6 @@ impl std::error::Error for MiniscriptPsbtError {} pub enum CreateTxError { /// There was a problem with the descriptors passed in Descriptor(DescriptorError), - /// We were unable to load wallet data from or write wallet data to the persistence backend - Persist(anyhow::Error), /// There was a problem while extracting and manipulating policies Policy(PolicyError), /// Spending policy is not compatible with this [`KeychainKind`] @@ -123,13 +121,6 @@ impl fmt::Display for CreateTxError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::Descriptor(e) => e.fmt(f), - Self::Persist(e) => { - write!( - f, - "failed to load wallet data from or write wallet data to persistence backend: {}", - e - ) - } Self::Policy(e) => e.fmt(f), CreateTxError::SpendingPolicyRequired(keychain_kind) => { write!(f, "Spending policy required: {:?}", keychain_kind) diff --git a/crates/wallet/src/wallet/export.rs b/crates/wallet/src/wallet/export.rs index 9eea7bd6a9..d84ef8fe7c 100644 --- a/crates/wallet/src/wallet/export.rs +++ b/crates/wallet/src/wallet/export.rs @@ -228,7 +228,7 @@ mod test { change_descriptor: Option<&str>, network: Network, ) -> Wallet { - let mut wallet = Wallet::new_no_persist(descriptor, change_descriptor, network).unwrap(); + let mut wallet = Wallet::new(descriptor, change_descriptor, network).unwrap(); let transaction = Transaction { input: vec![], output: vec![], diff --git a/crates/wallet/src/wallet/mod.rs b/crates/wallet/src/wallet/mod.rs index e80584dc31..e0af7f260f 100644 --- a/crates/wallet/src/wallet/mod.rs +++ b/crates/wallet/src/wallet/mod.rs @@ -31,7 +31,6 @@ use bdk_chain::{ Append, BlockId, ChainPosition, ConfirmationTime, ConfirmationTimeHeightAnchor, FullTxOut, IndexedTxGraph, }; -use bdk_persist::{Persist, PersistBackend}; use bitcoin::secp256k1::{All, Secp256k1}; use bitcoin::sighash::{EcdsaSighashType, TapSighashType}; use bitcoin::{ @@ -44,6 +43,7 @@ use core::fmt; use core::ops::Deref; use descriptor::error::Error as DescriptorError; use miniscript::psbt::{PsbtExt, PsbtInputExt, PsbtInputSatisfier}; +use std::mem; use bdk_chain::tx_graph::CalculateFeeError; @@ -90,7 +90,7 @@ pub struct Wallet { change_signers: Arc, chain: LocalChain, indexed_graph: IndexedTxGraph>, - persist: Persist, + staged: ChangeSet, network: Network, secp: SecpCtx, } @@ -134,7 +134,8 @@ impl From for Update { } /// The changes made to a wallet by applying an [`Update`]. -pub type ChangeSet = bdk_persist::CombinedChangeSet; +pub type ChangeSet = + bdk_chain::persist::CombinedChangeSet; /// A derived address and the index it was found at. /// For convenience this automatically derefs to `Address` @@ -162,36 +163,6 @@ impl fmt::Display for AddressInfo { } } -impl Wallet { - /// Creates a wallet that does not persist data. - pub fn new_no_persist( - descriptor: E, - change_descriptor: Option, - network: Network, - ) -> Result { - Self::new(descriptor, change_descriptor, (), network).map_err(|e| match e { - NewError::NonEmptyDatabase => unreachable!("mock-database cannot have data"), - NewError::Descriptor(e) => e, - NewError::Persist(_) => unreachable!("mock-write must always succeed"), - }) - } - - /// Creates a wallet that does not persist data, with a custom genesis hash. - pub fn new_no_persist_with_genesis_hash( - descriptor: E, - change_descriptor: Option, - network: Network, - genesis_hash: BlockHash, - ) -> Result { - Self::new_with_genesis_hash(descriptor, change_descriptor, (), network, genesis_hash) - .map_err(|e| match e { - NewError::NonEmptyDatabase => unreachable!("mock-database cannot have data"), - NewError::Descriptor(e) => e, - NewError::Persist(_) => unreachable!("mock-write must always succeed"), - }) - } -} - /// The error type when constructing a fresh [`Wallet`]. /// /// Methods [`new`] and [`new_with_genesis_hash`] may return this error. @@ -200,23 +171,14 @@ impl Wallet { /// [`new_with_genesis_hash`]: Wallet::new_with_genesis_hash #[derive(Debug)] pub enum NewError { - /// Database already has data. - NonEmptyDatabase, /// There was problem with the passed-in descriptor(s). Descriptor(crate::descriptor::DescriptorError), - /// We were unable to write the wallet's data to the persistence backend. - Persist(anyhow::Error), } impl fmt::Display for NewError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - NewError::NonEmptyDatabase => write!( - f, - "database already has data - use `load` or `new_or_load` methods instead" - ), NewError::Descriptor(e) => e.fmt(f), - NewError::Persist(e) => e.fmt(f), } } } @@ -233,10 +195,6 @@ impl std::error::Error for NewError {} pub enum LoadError { /// There was a problem with the passed-in descriptor(s). Descriptor(crate::descriptor::DescriptorError), - /// Loading data from the persistence backend failed. - Persist(anyhow::Error), - /// Wallet not initialized, persistence backend is empty. - NotInitialized, /// Data loaded from persistence is missing network type. MissingNetwork, /// Data loaded from persistence is missing genesis hash. @@ -249,10 +207,6 @@ impl fmt::Display for LoadError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { LoadError::Descriptor(e) => e.fmt(f), - LoadError::Persist(e) => e.fmt(f), - LoadError::NotInitialized => { - write!(f, "wallet is not initialized, persistence backend is empty") - } LoadError::MissingNetwork => write!(f, "loaded data is missing network type"), LoadError::MissingGenesis => write!(f, "loaded data is missing genesis hash"), LoadError::MissingDescriptor => write!(f, "loaded data is missing descriptor"), @@ -273,10 +227,6 @@ impl std::error::Error for LoadError {} pub enum NewOrLoadError { /// There is a problem with the passed-in descriptor. Descriptor(crate::descriptor::DescriptorError), - /// Either writing to or loading from the persistence backend failed. - Persist(anyhow::Error), - /// Wallet is not initialized, persistence backend is empty. - NotInitialized, /// The loaded genesis hash does not match what was provided. LoadedGenesisDoesNotMatch { /// The expected genesis block hash. @@ -304,14 +254,6 @@ impl fmt::Display for NewOrLoadError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { NewOrLoadError::Descriptor(e) => e.fmt(f), - NewOrLoadError::Persist(e) => write!( - f, - "failed to either write to or load from persistence, {}", - e - ), - NewOrLoadError::NotInitialized => { - write!(f, "wallet is not initialized, persistence backend is empty") - } NewOrLoadError::LoadedGenesisDoesNotMatch { expected, got } => { write!(f, "loaded genesis hash is not {}, got {:?}", expected, got) } @@ -399,11 +341,10 @@ impl Wallet { pub fn new( descriptor: E, change_descriptor: Option, - db: impl PersistBackend + Send + Sync + 'static, network: Network, ) -> Result { let genesis_hash = genesis_block(network).block_hash(); - Self::new_with_genesis_hash(descriptor, change_descriptor, db, network, genesis_hash) + Self::new_with_genesis_hash(descriptor, change_descriptor, network, genesis_hash) } /// Initialize an empty [`Wallet`] with a custom genesis hash. @@ -413,15 +354,9 @@ impl Wallet { pub fn new_with_genesis_hash( descriptor: E, change_descriptor: Option, - mut db: impl PersistBackend + Send + Sync + 'static, network: Network, genesis_hash: BlockHash, ) -> Result { - if let Ok(changeset) = db.load_from_persistence() { - if changeset.is_some() { - return Err(NewError::NonEmptyDatabase); - } - } let secp = Secp256k1::new(); let (chain, chain_changeset) = LocalChain::from_genesis_hash(genesis_hash); let mut index = KeychainTxOutIndex::::default(); @@ -432,13 +367,11 @@ impl Wallet { let indexed_graph = IndexedTxGraph::new(index); - let mut persist = Persist::new(db); - persist.stage(ChangeSet { + let staged = ChangeSet { chain: chain_changeset, indexed_tx_graph: indexed_graph.initial_changeset(), network: Some(network), - }); - persist.commit().map_err(NewError::Persist)?; + }; Ok(Wallet { signers, @@ -446,12 +379,24 @@ impl Wallet { network, chain, indexed_graph, - persist, + staged, secp, }) } - /// Load [`Wallet`] from the given persistence backend. + /// Stage a ['ChangeSet'] to be persisted later. + /// + /// [`commit`]: Self::commit + fn stage(&mut self, changeset: ChangeSet) { + self.staged.append(changeset) + } + + /// Take the staged ['ChangeSet'] to be persisted now. + pub fn take_staged(&mut self) -> ChangeSet { + mem::take(&mut self.staged) + } + + /// Load [`Wallet`] from the given previously persisted ['ChangeSet']. /// /// Note that the descriptor secret keys are not persisted to the db; this means that after /// calling this method the [`Wallet`] **won't** know the secret keys, and as such, won't be @@ -493,20 +438,7 @@ impl Wallet { /// /// Alternatively, you can call [`Wallet::new_or_load`], which will add the private keys of the /// passed-in descriptors to the [`Wallet`]. - pub fn load( - mut db: impl PersistBackend + Send + Sync + 'static, - ) -> Result { - let changeset = db - .load_from_persistence() - .map_err(LoadError::Persist)? - .ok_or(LoadError::NotInitialized)?; - Self::load_from_changeset(db, changeset) - } - - fn load_from_changeset( - db: impl PersistBackend + Send + Sync + 'static, - changeset: ChangeSet, - ) -> Result { + pub fn load_from_changeset(changeset: ChangeSet) -> Result { let secp = Secp256k1::new(); let network = changeset.network.ok_or(LoadError::MissingNetwork)?; let chain = @@ -533,14 +465,14 @@ impl Wallet { let mut indexed_graph = IndexedTxGraph::new(index); indexed_graph.apply_changeset(changeset.indexed_tx_graph); - let persist = Persist::new(db); + let staged = ChangeSet::default(); Ok(Wallet { signers, change_signers, chain, indexed_graph, - persist, + staged, network, secp, }) @@ -552,14 +484,14 @@ impl Wallet { pub fn new_or_load( descriptor: E, change_descriptor: Option, - db: impl PersistBackend + Send + Sync + 'static, + changeset: Option, network: Network, ) -> Result { let genesis_hash = genesis_block(network).block_hash(); Self::new_or_load_with_genesis_hash( descriptor, change_descriptor, - db, + changeset, network, genesis_hash, ) @@ -574,127 +506,111 @@ impl Wallet { pub fn new_or_load_with_genesis_hash( descriptor: E, change_descriptor: Option, - mut db: impl PersistBackend + Send + Sync + 'static, + changeset: Option, network: Network, genesis_hash: BlockHash, ) -> Result { - let changeset = db - .load_from_persistence() - .map_err(NewOrLoadError::Persist)?; - match changeset { - Some(changeset) => { - let mut wallet = Self::load_from_changeset(db, changeset).map_err(|e| match e { - LoadError::Descriptor(e) => NewOrLoadError::Descriptor(e), - LoadError::Persist(e) => NewOrLoadError::Persist(e), - LoadError::NotInitialized => NewOrLoadError::NotInitialized, - LoadError::MissingNetwork => NewOrLoadError::LoadedNetworkDoesNotMatch { - expected: network, - got: None, - }, - LoadError::MissingGenesis => NewOrLoadError::LoadedGenesisDoesNotMatch { - expected: genesis_hash, - got: None, - }, - LoadError::MissingDescriptor => NewOrLoadError::LoadedDescriptorDoesNotMatch { - got: None, - keychain: KeychainKind::External, - }, - })?; - if wallet.network != network { - return Err(NewOrLoadError::LoadedNetworkDoesNotMatch { - expected: network, - got: Some(wallet.network), - }); - } - if wallet.chain.genesis_hash() != genesis_hash { - return Err(NewOrLoadError::LoadedGenesisDoesNotMatch { - expected: genesis_hash, - got: Some(wallet.chain.genesis_hash()), - }); - } - - let (expected_descriptor, expected_descriptor_keymap) = descriptor - .into_wallet_descriptor(&wallet.secp, network) - .map_err(NewOrLoadError::Descriptor)?; - let wallet_descriptor = wallet.public_descriptor(KeychainKind::External).cloned(); - if wallet_descriptor != Some(expected_descriptor.clone()) { - return Err(NewOrLoadError::LoadedDescriptorDoesNotMatch { - got: wallet_descriptor, - keychain: KeychainKind::External, - }); - } - // if expected descriptor has private keys add them as new signers - if !expected_descriptor_keymap.is_empty() { - let signer_container = SignersContainer::build( - expected_descriptor_keymap, - &expected_descriptor, - &wallet.secp, - ); - signer_container.signers().into_iter().for_each(|signer| { - wallet.add_signer( - KeychainKind::External, - SignerOrdering::default(), - signer.clone(), - ) - }); - } + if let Some(changeset) = changeset { + let mut wallet = Self::load_from_changeset(changeset).map_err(|e| match e { + LoadError::Descriptor(e) => NewOrLoadError::Descriptor(e), + LoadError::MissingNetwork => NewOrLoadError::LoadedNetworkDoesNotMatch { + expected: network, + got: None, + }, + LoadError::MissingGenesis => NewOrLoadError::LoadedGenesisDoesNotMatch { + expected: genesis_hash, + got: None, + }, + LoadError::MissingDescriptor => NewOrLoadError::LoadedDescriptorDoesNotMatch { + got: None, + keychain: KeychainKind::External, + }, + })?; + if wallet.network != network { + return Err(NewOrLoadError::LoadedNetworkDoesNotMatch { + expected: network, + got: Some(wallet.network), + }); + } + if wallet.chain.genesis_hash() != genesis_hash { + return Err(NewOrLoadError::LoadedGenesisDoesNotMatch { + expected: genesis_hash, + got: Some(wallet.chain.genesis_hash()), + }); + } - let expected_change_descriptor = if let Some(c) = change_descriptor { - Some( - c.into_wallet_descriptor(&wallet.secp, network) - .map_err(NewOrLoadError::Descriptor)?, + let (expected_descriptor, expected_descriptor_keymap) = descriptor + .into_wallet_descriptor(&wallet.secp, network) + .map_err(NewOrLoadError::Descriptor)?; + let wallet_descriptor = wallet.public_descriptor(KeychainKind::External).cloned(); + if wallet_descriptor != Some(expected_descriptor.clone()) { + return Err(NewOrLoadError::LoadedDescriptorDoesNotMatch { + got: wallet_descriptor, + keychain: KeychainKind::External, + }); + } + // if expected descriptor has private keys add them as new signers + if !expected_descriptor_keymap.is_empty() { + let signer_container = SignersContainer::build( + expected_descriptor_keymap, + &expected_descriptor, + &wallet.secp, + ); + signer_container.signers().into_iter().for_each(|signer| { + wallet.add_signer( + KeychainKind::External, + SignerOrdering::default(), + signer.clone(), ) - } else { - None - }; - let wallet_change_descriptor = - wallet.public_descriptor(KeychainKind::Internal).cloned(); + }); + } - match (expected_change_descriptor, wallet_change_descriptor) { - (Some((expected_descriptor, expected_keymap)), Some(wallet_descriptor)) - if wallet_descriptor == expected_descriptor => - { - // if expected change descriptor has private keys add them as new signers - if !expected_keymap.is_empty() { - let signer_container = SignersContainer::build( - expected_keymap, - &expected_descriptor, - &wallet.secp, - ); - signer_container.signers().into_iter().for_each(|signer| { - wallet.add_signer( - KeychainKind::Internal, - SignerOrdering::default(), - signer.clone(), - ) - }); - } - } - (None, None) => (), - (_, wallet_descriptor) => { - return Err(NewOrLoadError::LoadedDescriptorDoesNotMatch { - got: wallet_descriptor, - keychain: KeychainKind::Internal, + let expected_change_descriptor = if let Some(c) = change_descriptor { + Some( + c.into_wallet_descriptor(&wallet.secp, network) + .map_err(NewOrLoadError::Descriptor)?, + ) + } else { + None + }; + let wallet_change_descriptor = + wallet.public_descriptor(KeychainKind::Internal).cloned(); + + match (expected_change_descriptor, wallet_change_descriptor) { + (Some((expected_descriptor, expected_keymap)), Some(wallet_descriptor)) + if wallet_descriptor == expected_descriptor => + { + // if expected change descriptor has private keys add them as new signers + if !expected_keymap.is_empty() { + let signer_container = SignersContainer::build( + expected_keymap, + &expected_descriptor, + &wallet.secp, + ); + signer_container.signers().into_iter().for_each(|signer| { + wallet.add_signer( + KeychainKind::Internal, + SignerOrdering::default(), + signer.clone(), + ) }); } } - - Ok(wallet) - } - None => Self::new_with_genesis_hash( - descriptor, - change_descriptor, - db, - network, - genesis_hash, - ) - .map_err(|e| match e { - NewError::NonEmptyDatabase => { - unreachable!("database is already checked to have no data") + (None, None) => (), + (_, wallet_descriptor) => { + return Err(NewOrLoadError::LoadedDescriptorDoesNotMatch { + got: wallet_descriptor, + keychain: KeychainKind::Internal, + }); } - NewError::Descriptor(e) => NewOrLoadError::Descriptor(e), - NewError::Persist(e) => NewOrLoadError::Persist(e), - }), + } + + Ok(wallet) + } else { + Self::new_with_genesis_hash(descriptor, change_descriptor, network, genesis_hash) + .map_err(|e| match e { + NewError::Descriptor(e) => NewOrLoadError::Descriptor(e), + }) } } @@ -747,22 +663,22 @@ impl Wallet { /// # Errors /// /// If writing to persistent storage fails. - pub fn reveal_next_address(&mut self, keychain: KeychainKind) -> anyhow::Result { + pub fn reveal_next_address(&mut self, keychain: KeychainKind) -> AddressInfo { let keychain = self.map_keychain(keychain); - let ((index, spk), index_changeset) = self - .indexed_graph - .index + let index = &mut self.indexed_graph.index; + let staged = &mut self.staged; + + let ((index, spk), index_changeset) = index .reveal_next_spk(&keychain) .expect("Must exist (we called map_keychain)"); - self.persist - .stage_and_commit(indexed_tx_graph::ChangeSet::from(index_changeset).into())?; + staged.append(indexed_tx_graph::ChangeSet::from(index_changeset).into()); - Ok(AddressInfo { + AddressInfo { index, address: Address::from_script(spk, self.network).expect("must have address form"), keychain, - }) + } } /// Reveal addresses up to and including the target `index` and return an iterator @@ -779,7 +695,7 @@ impl Wallet { &mut self, keychain: KeychainKind, index: u32, - ) -> anyhow::Result + '_> { + ) -> impl Iterator + '_ { let keychain = self.map_keychain(keychain); let (spk_iter, index_changeset) = self .indexed_graph @@ -787,14 +703,13 @@ impl Wallet { .reveal_to_target(&keychain, index) .expect("must exist (we called map_keychain)"); - self.persist - .stage_and_commit(indexed_tx_graph::ChangeSet::from(index_changeset).into())?; + self.stage(indexed_tx_graph::ChangeSet::from(index_changeset).into()); - Ok(spk_iter.map(move |(index, spk)| AddressInfo { + spk_iter.map(move |(index, spk)| AddressInfo { index, address: Address::from_script(&spk, self.network).expect("must have address form"), keychain, - })) + }) } /// Get the next unused address for the given `keychain`, i.e. the address with the lowest @@ -806,22 +721,22 @@ impl Wallet { /// # Errors /// /// If writing to persistent storage fails. - pub fn next_unused_address(&mut self, keychain: KeychainKind) -> anyhow::Result { + pub fn next_unused_address(&mut self, keychain: KeychainKind) -> AddressInfo { let keychain = self.map_keychain(keychain); - let ((index, spk), index_changeset) = self - .indexed_graph - .index + let index = &mut self.indexed_graph.index; + + let ((index, spk), index_changeset) = index .next_unused_spk(&keychain) .expect("must exist (we called map_keychain)"); - self.persist - .stage_and_commit(indexed_tx_graph::ChangeSet::from(index_changeset).into())?; + self.staged + .append(indexed_tx_graph::ChangeSet::from(index_changeset).into()); - Ok(AddressInfo { + AddressInfo { index, address: Address::from_script(spk, self.network).expect("must have address form"), keychain, - }) + } } /// Marks an address used of the given `keychain` at `index`. @@ -974,7 +889,7 @@ impl Wallet { /// [`commit`]: Self::commit pub fn insert_txout(&mut self, outpoint: OutPoint, txout: TxOut) { let additions = self.indexed_graph.insert_txout(outpoint, txout); - self.persist.stage(ChangeSet::from(additions)); + self.stage(ChangeSet::from(additions)); } /// Calculates the fee of a given transaction. Returns 0 if `tx` is a coinbase transaction. @@ -1141,7 +1056,7 @@ impl Wallet { ) -> Result { let changeset = self.chain.insert_block(block_id)?; let changed = !changeset.is_empty(); - self.persist.stage(changeset.into()); + self.stage(changeset.into()); Ok(changed) } @@ -1200,7 +1115,7 @@ impl Wallet { } let changed = !changeset.is_empty(); - self.persist.stage(changeset); + self.stage(changeset); Ok(changed) } @@ -1549,11 +1464,9 @@ impl Wallet { .expect("Keychain exists (we called map_keychain)"); let spk = spk.into(); self.indexed_graph.index.mark_used(change_keychain, index); - self.persist - .stage(ChangeSet::from(indexed_tx_graph::ChangeSet::from( - index_changeset, - ))); - self.persist.commit().map_err(CreateTxError::Persist)?; + self.stage(ChangeSet::from(indexed_tx_graph::ChangeSet::from( + index_changeset, + ))); spk } }; @@ -2381,27 +2294,10 @@ impl Wallet { changeset.append(ChangeSet::from( self.indexed_graph.apply_update(update.graph), )); - self.persist.stage(changeset); + self.stage(changeset); Ok(()) } - /// Commits all currently [`staged`] changed to the persistence backend returning and error when - /// this fails. - /// - /// This returns whether the `update` resulted in any changes. - /// - /// [`staged`]: Self::staged - pub fn commit(&mut self) -> anyhow::Result { - self.persist.commit().map(|c| c.is_some()) - } - - /// Returns the changes that will be committed with the next call to [`commit`]. - /// - /// [`commit`]: Self::commit - pub fn staged(&self) -> &ChangeSet { - self.persist.staged() - } - /// Get a reference to the inner [`TxGraph`]. pub fn tx_graph(&self) -> &TxGraph { self.indexed_graph.graph() @@ -2467,7 +2363,7 @@ impl Wallet { .apply_block_relevant(block, height) .into(), ); - self.persist.stage(changeset); + self.stage(changeset); Ok(()) } @@ -2486,7 +2382,7 @@ impl Wallet { let indexed_graph_changeset = self .indexed_graph .batch_insert_relevant_unconfirmed(unconfirmed_txs); - self.persist.stage(ChangeSet::from(indexed_graph_changeset)); + self.stage(ChangeSet::from(indexed_graph_changeset)); } } diff --git a/crates/wallet/tests/common.rs b/crates/wallet/tests/common.rs index a51dcafb3a..8b44dcc4df 100644 --- a/crates/wallet/tests/common.rs +++ b/crates/wallet/tests/common.rs @@ -19,7 +19,7 @@ pub fn get_funded_wallet_with_change( descriptor: &str, change: Option<&str>, ) -> (Wallet, bitcoin::Txid) { - let mut wallet = Wallet::new_no_persist(descriptor, change, Network::Regtest).unwrap(); + let mut wallet = Wallet::new(descriptor, change, Network::Regtest).unwrap(); let change_address = wallet.peek_address(KeychainKind::External, 0).address; let sendto_address = Address::from_str("bcrt1q3qtze4ys45tgdvguj66zrk4fu6hq3a3v9pfly5") .expect("address") diff --git a/crates/wallet/tests/wallet.rs b/crates/wallet/tests/wallet.rs index 9b27a2795d..245ba6eb66 100644 --- a/crates/wallet/tests/wallet.rs +++ b/crates/wallet/tests/wallet.rs @@ -1,11 +1,11 @@ +use anyhow::anyhow; use std::path::Path; use std::str::FromStr; use assert_matches::assert_matches; use bdk_chain::collections::BTreeMap; use bdk_chain::COINBASE_MATURITY; -use bdk_chain::{BlockId, ConfirmationTime}; -use bdk_persist::PersistBackend; +use bdk_chain::{persist::Persist, BlockId, ConfirmationTime}; use bdk_sqlite::rusqlite::Connection; use bdk_wallet::descriptor::{calc_checksum, IntoWalletDescriptor}; use bdk_wallet::psbt::PsbtUtils; @@ -13,7 +13,6 @@ use bdk_wallet::signer::{SignOptions, SignerError}; use bdk_wallet::wallet::coin_selection::{self, LargestFirstCoinSelection}; use bdk_wallet::wallet::error::CreateTxError; use bdk_wallet::wallet::tx_builder::AddForeignUtxoError; -use bdk_wallet::wallet::NewError; use bdk_wallet::wallet::{AddressInfo, Balance, Wallet}; use bdk_wallet::KeychainKind; use bitcoin::hashes::Hash; @@ -31,7 +30,7 @@ mod common; use common::*; fn receive_output(wallet: &mut Wallet, value: u64, height: ConfirmationTime) -> OutPoint { - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let tx = Transaction { version: transaction::Version::ONE, lock_time: absolute::LockTime::ZERO, @@ -75,7 +74,7 @@ const DB_MAGIC: &[u8] = &[0x21, 0x24, 0x48]; fn load_recovers_wallet() -> anyhow::Result<()> { fn run(filename: &str, create_new: FN, recover: FR) -> anyhow::Result<()> where - B: PersistBackend + Send + Sync + 'static, + B: Persist + Send + Sync + 'static, FN: Fn(&Path) -> anyhow::Result, FR: Fn(&Path) -> anyhow::Result, { @@ -84,18 +83,30 @@ fn load_recovers_wallet() -> anyhow::Result<()> { // create new wallet let wallet_spk_index = { - let db = create_new(&file_path).expect("must create db"); - let mut wallet = Wallet::new(get_test_tr_single_sig_xprv(), None, db, Network::Testnet) + let mut wallet = Wallet::new(get_test_tr_single_sig_xprv(), None, Network::Testnet) .expect("must init wallet"); - wallet.reveal_next_address(KeychainKind::External).unwrap(); + wallet.reveal_next_address(KeychainKind::External); + + // persist new wallet changes + let staged_changeset = wallet.take_staged(); + let db = &mut create_new(&file_path).expect("must create db"); + db.write_changes(&staged_changeset) + .map_err(|e| anyhow!("write changes error: {}", e))?; + wallet.spk_index().clone() }; // recover wallet { - let db = recover(&file_path).expect("must recover db"); - let wallet = Wallet::load(db).expect("must recover wallet"); + // load persisted wallet changes + let db = &mut recover(&file_path).expect("must recover db"); + let changeset = db + .load_changes() + .expect("must recover wallet") + .expect("changeset"); + + let wallet = Wallet::load_from_changeset(changeset).expect("must recover wallet"); assert_eq!(wallet.network(), Network::Testnet); assert_eq!( wallet.spk_index().keychains().collect::>(), @@ -115,13 +126,6 @@ fn load_recovers_wallet() -> anyhow::Result<()> { ); } - // `new` can only be called on empty db - { - let db = recover(&file_path).expect("must recover db"); - let result = Wallet::new(get_test_tr_single_sig_xprv(), None, db, Network::Testnet); - assert!(matches!(result, Err(NewError::NonEmptyDatabase))); - } - Ok(()) } @@ -143,7 +147,7 @@ fn load_recovers_wallet() -> anyhow::Result<()> { fn new_or_load() -> anyhow::Result<()> { fn run(filename: &str, new_or_load: F) -> anyhow::Result<()> where - B: PersistBackend + Send + Sync + 'static, + B: Persist + Send + Sync + 'static, F: Fn(&Path) -> anyhow::Result, { let temp_dir = tempfile::tempdir().expect("must create tempdir"); @@ -151,16 +155,22 @@ fn new_or_load() -> anyhow::Result<()> { // init wallet when non-existent let wallet_keychains: BTreeMap<_, _> = { - let db = new_or_load(&file_path).expect("must create db"); - let wallet = Wallet::new_or_load(get_test_wpkh(), None, db, Network::Testnet) + let wallet = &mut Wallet::new_or_load(get_test_wpkh(), None, None, Network::Testnet) .expect("must init wallet"); + let staged_changeset = wallet.take_staged(); + let db = &mut new_or_load(&file_path).expect("must create db"); + db.write_changes(&staged_changeset) + .map_err(|e| anyhow!("write changes error: {}", e))?; wallet.keychains().map(|(k, v)| (*k, v.clone())).collect() }; // wrong network { - let db = new_or_load(&file_path).expect("must create db"); - let err = Wallet::new_or_load(get_test_wpkh(), None, db, Network::Bitcoin) + let db = &mut new_or_load(&file_path).expect("must create db"); + let changeset = db + .load_changes() + .map_err(|e| anyhow!("load changes error: {}", e))?; + let err = Wallet::new_or_load(get_test_wpkh(), None, changeset, Network::Bitcoin) .expect_err("wrong network"); assert!( matches!( @@ -181,11 +191,14 @@ fn new_or_load() -> anyhow::Result<()> { let got_blockhash = bitcoin::blockdata::constants::genesis_block(Network::Testnet).block_hash(); - let db = new_or_load(&file_path).expect("must open db"); + let db = &mut new_or_load(&file_path).expect("must open db"); + let changeset = db + .load_changes() + .map_err(|e| anyhow!("load changes error: {}", e))?; let err = Wallet::new_or_load_with_genesis_hash( get_test_wpkh(), None, - db, + changeset, Network::Testnet, exp_blockhash, ) @@ -209,8 +222,11 @@ fn new_or_load() -> anyhow::Result<()> { .unwrap() .0; - let db = new_or_load(&file_path).expect("must open db"); - let err = Wallet::new_or_load(exp_descriptor, None, db, Network::Testnet) + let db = &mut new_or_load(&file_path).expect("must open db"); + let changeset = db + .load_changes() + .map_err(|e| anyhow!("load changes error: {}", e))?; + let err = Wallet::new_or_load(exp_descriptor, None, changeset, Network::Testnet) .expect_err("wrong external descriptor"); assert!( matches!( @@ -228,9 +244,13 @@ fn new_or_load() -> anyhow::Result<()> { let exp_descriptor = Some(get_test_tr_single_sig()); let got_descriptor = None; - let db = new_or_load(&file_path).expect("must open db"); - let err = Wallet::new_or_load(get_test_wpkh(), exp_descriptor, db, Network::Testnet) - .expect_err("wrong internal descriptor"); + let db = &mut new_or_load(&file_path).expect("must open db"); + let changeset = db + .load_changes() + .map_err(|e| anyhow!("load changes error: {}", e))?; + let err = + Wallet::new_or_load(get_test_wpkh(), exp_descriptor, changeset, Network::Testnet) + .expect_err("wrong internal descriptor"); assert!( matches!( err, @@ -244,8 +264,11 @@ fn new_or_load() -> anyhow::Result<()> { // all parameters match { - let db = new_or_load(&file_path).expect("must open db"); - let wallet = Wallet::new_or_load(get_test_wpkh(), None, db, Network::Testnet) + let db = &mut new_or_load(&file_path).expect("must open db"); + let changeset = db + .load_changes() + .map_err(|e| anyhow!("load changes error: {}", e))?; + let wallet = Wallet::new_or_load(get_test_wpkh(), None, changeset, Network::Testnet) .expect("must recover wallet"); assert_eq!(wallet.network(), Network::Testnet); assert!(wallet @@ -436,7 +459,7 @@ fn test_create_tx_empty_recipients() { #[should_panic(expected = "NoUtxosSelected")] fn test_create_tx_manually_selected_empty_utxos() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) @@ -447,7 +470,7 @@ fn test_create_tx_manually_selected_empty_utxos() { #[test] fn test_create_tx_version_0() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) @@ -458,7 +481,7 @@ fn test_create_tx_version_0() { #[test] fn test_create_tx_version_1_csv() { let (mut wallet, _) = get_funded_wallet(get_test_single_sig_csv()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) @@ -469,7 +492,7 @@ fn test_create_tx_version_1_csv() { #[test] fn test_create_tx_custom_version() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) @@ -483,7 +506,7 @@ fn test_create_tx_custom_version() { fn test_create_tx_default_locktime_is_last_sync_height() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let psbt = builder.finish().unwrap(); @@ -496,7 +519,7 @@ fn test_create_tx_default_locktime_is_last_sync_height() { #[test] fn test_create_tx_fee_sniping_locktime_last_sync() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); @@ -512,7 +535,7 @@ fn test_create_tx_fee_sniping_locktime_last_sync() { #[test] fn test_create_tx_default_locktime_cltv() { let (mut wallet, _) = get_funded_wallet(get_test_single_sig_cltv()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let psbt = builder.finish().unwrap(); @@ -523,7 +546,7 @@ fn test_create_tx_default_locktime_cltv() { #[test] fn test_create_tx_custom_locktime() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) @@ -540,7 +563,7 @@ fn test_create_tx_custom_locktime() { #[test] fn test_create_tx_custom_locktime_compatible_with_cltv() { let (mut wallet, _) = get_funded_wallet(get_test_single_sig_cltv()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) @@ -553,7 +576,7 @@ fn test_create_tx_custom_locktime_compatible_with_cltv() { #[test] fn test_create_tx_custom_locktime_incompatible_with_cltv() { let (mut wallet, _) = get_funded_wallet(get_test_single_sig_cltv()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) @@ -566,7 +589,7 @@ fn test_create_tx_custom_locktime_incompatible_with_cltv() { #[test] fn test_create_tx_no_rbf_csv() { let (mut wallet, _) = get_funded_wallet(get_test_single_sig_csv()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let psbt = builder.finish().unwrap(); @@ -577,7 +600,7 @@ fn test_create_tx_no_rbf_csv() { #[test] fn test_create_tx_with_default_rbf_csv() { let (mut wallet, _) = get_funded_wallet(get_test_single_sig_csv()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) @@ -591,7 +614,7 @@ fn test_create_tx_with_default_rbf_csv() { #[test] fn test_create_tx_with_custom_rbf_csv() { let (mut wallet, _) = get_funded_wallet(get_test_single_sig_csv()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) @@ -604,7 +627,7 @@ fn test_create_tx_with_custom_rbf_csv() { #[test] fn test_create_tx_no_rbf_cltv() { let (mut wallet, _) = get_funded_wallet(get_test_single_sig_cltv()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let psbt = builder.finish().unwrap(); @@ -615,7 +638,7 @@ fn test_create_tx_no_rbf_cltv() { #[test] fn test_create_tx_invalid_rbf_sequence() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) @@ -626,7 +649,7 @@ fn test_create_tx_invalid_rbf_sequence() { #[test] fn test_create_tx_custom_rbf_sequence() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) @@ -639,7 +662,7 @@ fn test_create_tx_custom_rbf_sequence() { #[test] fn test_create_tx_default_sequence() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let psbt = builder.finish().unwrap(); @@ -650,7 +673,7 @@ fn test_create_tx_default_sequence() { #[test] fn test_create_tx_change_policy_no_internal() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) @@ -673,7 +696,7 @@ macro_rules! check_fee { #[test] fn test_create_tx_drain_wallet_and_drain_to() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(addr.script_pubkey()).drain_wallet(); let psbt = builder.finish().unwrap(); @@ -692,7 +715,7 @@ fn test_create_tx_drain_wallet_and_drain_to_and_with_recipient() { let addr = Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt") .unwrap() .assume_checked(); - let drain_addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let drain_addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(20_000)) @@ -718,7 +741,7 @@ fn test_create_tx_drain_wallet_and_drain_to_and_with_recipient() { #[test] fn test_create_tx_drain_to_and_utxos() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let utxos: Vec<_> = wallet.list_unspent().map(|u| u.outpoint).collect(); let mut builder = wallet.build_tx(); builder @@ -739,7 +762,7 @@ fn test_create_tx_drain_to_and_utxos() { #[should_panic(expected = "NoRecipients")] fn test_create_tx_drain_to_no_drain_wallet_no_utxos() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let drain_addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let drain_addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(drain_addr.script_pubkey()); builder.finish().unwrap(); @@ -748,7 +771,7 @@ fn test_create_tx_drain_to_no_drain_wallet_no_utxos() { #[test] fn test_create_tx_default_fee_rate() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let psbt = builder.finish().unwrap(); @@ -760,7 +783,7 @@ fn test_create_tx_default_fee_rate() { #[test] fn test_create_tx_custom_fee_rate() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) @@ -774,7 +797,7 @@ fn test_create_tx_custom_fee_rate() { #[test] fn test_create_tx_absolute_fee() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .drain_to(addr.script_pubkey()) @@ -794,7 +817,7 @@ fn test_create_tx_absolute_fee() { #[test] fn test_create_tx_absolute_zero_fee() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .drain_to(addr.script_pubkey()) @@ -815,7 +838,7 @@ fn test_create_tx_absolute_zero_fee() { #[should_panic(expected = "InsufficientFunds")] fn test_create_tx_absolute_high_fee() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .drain_to(addr.script_pubkey()) @@ -829,7 +852,7 @@ fn test_create_tx_add_change() { use bdk_wallet::wallet::tx_builder::TxOrdering; let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) @@ -848,7 +871,7 @@ fn test_create_tx_add_change() { #[test] fn test_create_tx_skip_change_dust() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.add_recipient(addr.script_pubkey(), Amount::from_sat(49_800)); let psbt = builder.finish().unwrap(); @@ -863,7 +886,7 @@ fn test_create_tx_skip_change_dust() { #[should_panic(expected = "InsufficientFunds")] fn test_create_tx_drain_to_dust_amount() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); // very high fee rate, so that the only output would be below dust let mut builder = wallet.build_tx(); builder @@ -876,7 +899,7 @@ fn test_create_tx_drain_to_dust_amount() { #[test] fn test_create_tx_ordering_respected() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(30_000)) @@ -897,7 +920,7 @@ fn test_create_tx_ordering_respected() { #[test] fn test_create_tx_default_sighash() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.add_recipient(addr.script_pubkey(), Amount::from_sat(30_000)); let psbt = builder.finish().unwrap(); @@ -908,7 +931,7 @@ fn test_create_tx_default_sighash() { #[test] fn test_create_tx_custom_sighash() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(30_000)) @@ -927,7 +950,7 @@ fn test_create_tx_input_hd_keypaths() { use core::str::FromStr; let (mut wallet, _) = get_funded_wallet("wpkh([d34db33f/44'/0'/0']tpubDEnoLuPdBep9bzw5LoGYpsxUQYheRQ9gcgrJhJEcdKFB9cWQRyYmkCyRoTqeD4tJYiVVgt6A3rN6rWn9RYhR9sBsGxji29LYWHuKKbdb1ev/0/*)"); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(addr.script_pubkey()).drain_wallet(); let psbt = builder.finish().unwrap(); @@ -949,7 +972,7 @@ fn test_create_tx_output_hd_keypaths() { let (mut wallet, _) = get_funded_wallet("wpkh([d34db33f/44'/0'/0']tpubDEnoLuPdBep9bzw5LoGYpsxUQYheRQ9gcgrJhJEcdKFB9cWQRyYmkCyRoTqeD4tJYiVVgt6A3rN6rWn9RYhR9sBsGxji29LYWHuKKbdb1ev/0/*)"); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(addr.script_pubkey()).drain_wallet(); let psbt = builder.finish().unwrap(); @@ -971,7 +994,7 @@ fn test_create_tx_set_redeem_script_p2sh() { let (mut wallet, _) = get_funded_wallet("sh(pk(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW))"); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(addr.script_pubkey()).drain_wallet(); let psbt = builder.finish().unwrap(); @@ -994,7 +1017,7 @@ fn test_create_tx_set_witness_script_p2wsh() { let (mut wallet, _) = get_funded_wallet("wsh(pk(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW))"); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(addr.script_pubkey()).drain_wallet(); let psbt = builder.finish().unwrap(); @@ -1015,7 +1038,7 @@ fn test_create_tx_set_witness_script_p2wsh() { fn test_create_tx_set_redeem_witness_script_p2wsh_p2sh() { let (mut wallet, _) = get_funded_wallet("sh(wsh(pk(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW)))"); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(addr.script_pubkey()).drain_wallet(); let psbt = builder.finish().unwrap(); @@ -1033,7 +1056,7 @@ fn test_create_tx_set_redeem_witness_script_p2wsh_p2sh() { fn test_create_tx_non_witness_utxo() { let (mut wallet, _) = get_funded_wallet("sh(pk(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW))"); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(addr.script_pubkey()).drain_wallet(); let psbt = builder.finish().unwrap(); @@ -1046,7 +1069,7 @@ fn test_create_tx_non_witness_utxo() { fn test_create_tx_only_witness_utxo() { let (mut wallet, _) = get_funded_wallet("wsh(pk(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW))"); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .drain_to(addr.script_pubkey()) @@ -1062,7 +1085,7 @@ fn test_create_tx_only_witness_utxo() { fn test_create_tx_shwpkh_has_witness_utxo() { let (mut wallet, _) = get_funded_wallet("sh(wpkh(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW))"); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(addr.script_pubkey()).drain_wallet(); let psbt = builder.finish().unwrap(); @@ -1074,7 +1097,7 @@ fn test_create_tx_shwpkh_has_witness_utxo() { fn test_create_tx_both_non_witness_utxo_and_witness_utxo_default() { let (mut wallet, _) = get_funded_wallet("wsh(pk(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW))"); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(addr.script_pubkey()).drain_wallet(); let psbt = builder.finish().unwrap(); @@ -1091,7 +1114,6 @@ fn test_create_tx_add_utxo() { output: vec![TxOut { script_pubkey: wallet .next_unused_address(KeychainKind::External) - .unwrap() .script_pubkey(), value: Amount::from_sat(25_000), }], @@ -1141,7 +1163,6 @@ fn test_create_tx_manually_selected_insufficient() { output: vec![TxOut { script_pubkey: wallet .next_unused_address(KeychainKind::External) - .unwrap() .script_pubkey(), value: Amount::from_sat(25_000), }], @@ -1187,7 +1208,7 @@ fn test_create_tx_policy_path_required() { #[test] fn test_create_tx_policy_path_no_csv() { let descriptors = get_test_wpkh(); - let mut wallet = Wallet::new_no_persist(descriptors, None, Network::Regtest).unwrap(); + let mut wallet = Wallet::new(descriptors, None, Network::Regtest).unwrap(); let tx = Transaction { version: transaction::Version::non_standard(0), @@ -1196,7 +1217,6 @@ fn test_create_tx_policy_path_no_csv() { output: vec![TxOut { script_pubkey: wallet .next_unused_address(KeychainKind::External) - .unwrap() .script_pubkey(), value: Amount::from_sat(50_000), }], @@ -1270,7 +1290,7 @@ fn test_create_tx_global_xpubs_with_origin() { use bitcoin::hex::FromHex; let (mut wallet, _) = get_funded_wallet("wpkh([73756c7f/48'/0'/0'/2']tpubDCKxNyM3bLgbEX13Mcd8mYxbVg9ajDkWXMh29hMWBurKfVmBfWAM96QVP3zaUcN51HvkZ3ar4VwP82kC8JZhhux8vFQoJintSpVBwpFvyU3/0/*)"); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) @@ -1533,7 +1553,7 @@ fn test_get_psbt_input() { )] fn test_create_tx_global_xpubs_origin_missing() { let (mut wallet, _) = get_funded_wallet("wpkh(tpubDCKxNyM3bLgbEX13Mcd8mYxbVg9ajDkWXMh29hMWBurKfVmBfWAM96QVP3zaUcN51HvkZ3ar4VwP82kC8JZhhux8vFQoJintSpVBwpFvyU3/0/*)"); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) @@ -1547,7 +1567,7 @@ fn test_create_tx_global_xpubs_master_without_origin() { use bitcoin::hex::FromHex; let (mut wallet, _) = get_funded_wallet("wpkh(tpubD6NzVbkrYhZ4Y55A58Gv9RSNF5hy84b5AJqYy7sCcjFrkcLpPre8kmgfit6kY1Zs3BLgeypTDBZJM222guPpdz7Cup5yzaMu62u7mYGbwFL/0/*)"); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) @@ -1568,7 +1588,7 @@ fn test_create_tx_global_xpubs_master_without_origin() { #[should_panic(expected = "IrreplaceableTransaction")] fn test_bump_fee_irreplaceable_tx() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let psbt = builder.finish().unwrap(); @@ -1585,7 +1605,7 @@ fn test_bump_fee_irreplaceable_tx() { #[should_panic(expected = "TransactionConfirmed")] fn test_bump_fee_confirmed_tx() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let psbt = builder.finish().unwrap(); @@ -1609,7 +1629,7 @@ fn test_bump_fee_confirmed_tx() { #[test] fn test_bump_fee_low_fee_rate() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) @@ -1643,7 +1663,7 @@ fn test_bump_fee_low_fee_rate() { #[should_panic(expected = "FeeTooLow")] fn test_bump_fee_low_abs() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) @@ -1666,7 +1686,7 @@ fn test_bump_fee_low_abs() { #[should_panic(expected = "FeeTooLow")] fn test_bump_fee_zero_abs() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) @@ -1888,7 +1908,6 @@ fn test_bump_fee_drain_wallet() { output: vec![TxOut { script_pubkey: wallet .next_unused_address(KeychainKind::External) - .unwrap() .script_pubkey(), value: Amount::from_sat(25_000), }], @@ -1954,7 +1973,6 @@ fn test_bump_fee_remove_output_manually_selected_only() { output: vec![TxOut { script_pubkey: wallet .next_unused_address(KeychainKind::External) - .unwrap() .script_pubkey(), value: Amount::from_sat(25_000), }], @@ -2011,7 +2029,6 @@ fn test_bump_fee_add_input() { output: vec![TxOut { script_pubkey: wallet .next_unused_address(KeychainKind::External) - .unwrap() .script_pubkey(), value: Amount::from_sat(25_000), }], @@ -2530,7 +2547,7 @@ fn test_fee_amount_negative_drain_val() { #[test] fn test_sign_single_xprv() { let (mut wallet, _) = get_funded_wallet("wpkh(tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/*)"); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(addr.script_pubkey()).drain_wallet(); let mut psbt = builder.finish().unwrap(); @@ -2545,7 +2562,7 @@ fn test_sign_single_xprv() { #[test] fn test_sign_single_xprv_with_master_fingerprint_and_path() { let (mut wallet, _) = get_funded_wallet("wpkh([d34db33f/84h/1h/0h]tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/*)"); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(addr.script_pubkey()).drain_wallet(); let mut psbt = builder.finish().unwrap(); @@ -2560,7 +2577,7 @@ fn test_sign_single_xprv_with_master_fingerprint_and_path() { #[test] fn test_sign_single_xprv_bip44_path() { let (mut wallet, _) = get_funded_wallet("wpkh(tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/44'/0'/0'/0/*)"); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(addr.script_pubkey()).drain_wallet(); let mut psbt = builder.finish().unwrap(); @@ -2575,7 +2592,7 @@ fn test_sign_single_xprv_bip44_path() { #[test] fn test_sign_single_xprv_sh_wpkh() { let (mut wallet, _) = get_funded_wallet("sh(wpkh(tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/*))"); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(addr.script_pubkey()).drain_wallet(); let mut psbt = builder.finish().unwrap(); @@ -2591,7 +2608,7 @@ fn test_sign_single_xprv_sh_wpkh() { fn test_sign_single_wif() { let (mut wallet, _) = get_funded_wallet("wpkh(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW)"); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(addr.script_pubkey()).drain_wallet(); let mut psbt = builder.finish().unwrap(); @@ -2606,7 +2623,7 @@ fn test_sign_single_wif() { #[test] fn test_sign_single_xprv_no_hd_keypaths() { let (mut wallet, _) = get_funded_wallet("wpkh(tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/*)"); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(addr.script_pubkey()).drain_wallet(); let mut psbt = builder.finish().unwrap(); @@ -2691,7 +2708,7 @@ fn test_remove_partial_sigs_after_finalize_sign_option() { let (mut wallet, _) = get_funded_wallet("wpkh(tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/*)"); for remove_partial_sigs in &[true, false] { - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(addr.script_pubkey()).drain_wallet(); let mut psbt = builder.finish().unwrap(); @@ -2721,7 +2738,7 @@ fn test_try_finalize_sign_option() { let (mut wallet, _) = get_funded_wallet("wpkh(tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/*)"); for try_finalize in &[true, false] { - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(addr.script_pubkey()).drain_wallet(); let mut psbt = builder.finish().unwrap(); @@ -2755,7 +2772,7 @@ fn test_sign_nonstandard_sighash() { let sighash = EcdsaSighashType::NonePlusAnyoneCanPay; let (mut wallet, _) = get_funded_wallet("wpkh(tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/*)"); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .drain_to(addr.script_pubkey()) @@ -2798,7 +2815,7 @@ fn test_sign_nonstandard_sighash() { #[test] fn test_unused_address() { - let mut wallet = Wallet::new_no_persist("wpkh(tpubEBr4i6yk5nf5DAaJpsi9N2pPYBeJ7fZ5Z9rmN4977iYLCGco1VyjB9tvvuvYtfZzjD5A8igzgw3HeWeeKFmanHYqksqZXYXGsw5zjnj7KM9/*)", + let mut wallet = Wallet::new("wpkh(tpubEBr4i6yk5nf5DAaJpsi9N2pPYBeJ7fZ5Z9rmN4977iYLCGco1VyjB9tvvuvYtfZzjD5A8igzgw3HeWeeKFmanHYqksqZXYXGsw5zjnj7KM9/*)", None, Network::Testnet).unwrap(); // `list_unused_addresses` should be empty if we haven't revealed any @@ -2810,7 +2827,6 @@ fn test_unused_address() { assert_eq!( wallet .next_unused_address(KeychainKind::External) - .unwrap() .to_string(), "tb1q6yn66vajcctph75pvylgkksgpp6nq04ppwct9a" ); @@ -2827,13 +2843,12 @@ fn test_unused_address() { #[test] fn test_next_unused_address() { let descriptor = "wpkh(tpubEBr4i6yk5nf5DAaJpsi9N2pPYBeJ7fZ5Z9rmN4977iYLCGco1VyjB9tvvuvYtfZzjD5A8igzgw3HeWeeKFmanHYqksqZXYXGsw5zjnj7KM9/*)"; - let mut wallet = Wallet::new_no_persist(descriptor, None, Network::Testnet).unwrap(); + let mut wallet = Wallet::new(descriptor, None, Network::Testnet).unwrap(); assert_eq!(wallet.derivation_index(KeychainKind::External), None); assert_eq!( wallet .next_unused_address(KeychainKind::External) - .unwrap() .to_string(), "tb1q6yn66vajcctph75pvylgkksgpp6nq04ppwct9a" ); @@ -2842,7 +2857,6 @@ fn test_next_unused_address() { assert_eq!( wallet .next_unused_address(KeychainKind::External) - .unwrap() .to_string(), "tb1q6yn66vajcctph75pvylgkksgpp6nq04ppwct9a" ); @@ -2850,11 +2864,11 @@ fn test_next_unused_address() { // test mark used / unused assert!(wallet.mark_used(KeychainKind::External, 0)); - let next_unused_addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let next_unused_addr = wallet.next_unused_address(KeychainKind::External); assert_eq!(next_unused_addr.index, 1); assert!(wallet.unmark_used(KeychainKind::External, 0)); - let next_unused_addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let next_unused_addr = wallet.next_unused_address(KeychainKind::External); assert_eq!(next_unused_addr.index, 0); // use the above address @@ -2863,7 +2877,6 @@ fn test_next_unused_address() { assert_eq!( wallet .next_unused_address(KeychainKind::External) - .unwrap() .to_string(), "tb1q4er7kxx6sssz3q7qp7zsqsdx4erceahhax77d7" ); @@ -2875,7 +2888,7 @@ fn test_next_unused_address() { #[test] fn test_peek_address_at_index() { - let mut wallet = Wallet::new_no_persist("wpkh(tpubEBr4i6yk5nf5DAaJpsi9N2pPYBeJ7fZ5Z9rmN4977iYLCGco1VyjB9tvvuvYtfZzjD5A8igzgw3HeWeeKFmanHYqksqZXYXGsw5zjnj7KM9/*)", + let mut wallet = Wallet::new("wpkh(tpubEBr4i6yk5nf5DAaJpsi9N2pPYBeJ7fZ5Z9rmN4977iYLCGco1VyjB9tvvuvYtfZzjD5A8igzgw3HeWeeKFmanHYqksqZXYXGsw5zjnj7KM9/*)", None, Network::Testnet).unwrap(); assert_eq!( @@ -2897,7 +2910,6 @@ fn test_peek_address_at_index() { assert_eq!( wallet .reveal_next_address(KeychainKind::External) - .unwrap() .to_string(), "tb1q6yn66vajcctph75pvylgkksgpp6nq04ppwct9a" ); @@ -2905,7 +2917,6 @@ fn test_peek_address_at_index() { assert_eq!( wallet .reveal_next_address(KeychainKind::External) - .unwrap() .to_string(), "tb1q4er7kxx6sssz3q7qp7zsqsdx4erceahhax77d7" ); @@ -2913,8 +2924,8 @@ fn test_peek_address_at_index() { #[test] fn test_peek_address_at_index_not_derivable() { - let wallet = Wallet::new_no_persist("wpkh(tpubEBr4i6yk5nf5DAaJpsi9N2pPYBeJ7fZ5Z9rmN4977iYLCGco1VyjB9tvvuvYtfZzjD5A8igzgw3HeWeeKFmanHYqksqZXYXGsw5zjnj7KM9/1)", - None, Network::Testnet).unwrap(); + let wallet = Wallet::new("wpkh(tpubEBr4i6yk5nf5DAaJpsi9N2pPYBeJ7fZ5Z9rmN4977iYLCGco1VyjB9tvvuvYtfZzjD5A8igzgw3HeWeeKFmanHYqksqZXYXGsw5zjnj7KM9/1)", + None, Network::Testnet).unwrap(); assert_eq!( wallet.peek_address(KeychainKind::External, 1).to_string(), @@ -2934,12 +2945,12 @@ fn test_peek_address_at_index_not_derivable() { #[test] fn test_returns_index_and_address() { - let mut wallet = Wallet::new_no_persist("wpkh(tpubEBr4i6yk5nf5DAaJpsi9N2pPYBeJ7fZ5Z9rmN4977iYLCGco1VyjB9tvvuvYtfZzjD5A8igzgw3HeWeeKFmanHYqksqZXYXGsw5zjnj7KM9/*)", + let mut wallet = Wallet::new("wpkh(tpubEBr4i6yk5nf5DAaJpsi9N2pPYBeJ7fZ5Z9rmN4977iYLCGco1VyjB9tvvuvYtfZzjD5A8igzgw3HeWeeKFmanHYqksqZXYXGsw5zjnj7KM9/*)", None, Network::Testnet).unwrap(); // new index 0 assert_eq!( - wallet.reveal_next_address(KeychainKind::External).unwrap(), + wallet.reveal_next_address(KeychainKind::External), AddressInfo { index: 0, address: Address::from_str("tb1q6yn66vajcctph75pvylgkksgpp6nq04ppwct9a") @@ -2951,7 +2962,7 @@ fn test_returns_index_and_address() { // new index 1 assert_eq!( - wallet.reveal_next_address(KeychainKind::External).unwrap(), + wallet.reveal_next_address(KeychainKind::External), AddressInfo { index: 1, address: Address::from_str("tb1q4er7kxx6sssz3q7qp7zsqsdx4erceahhax77d7") @@ -2975,7 +2986,7 @@ fn test_returns_index_and_address() { // new index 2 assert_eq!( - wallet.reveal_next_address(KeychainKind::External).unwrap(), + wallet.reveal_next_address(KeychainKind::External), AddressInfo { index: 2, address: Address::from_str("tb1qzntf2mqex4ehwkjlfdyy3ewdlk08qkvkvrz7x2") @@ -3001,7 +3012,7 @@ fn test_sending_to_bip350_bech32m_address() { fn test_get_address() { use bdk_wallet::descriptor::template::Bip84; let key = bitcoin::bip32::Xpriv::from_str("tprv8ZgxMBicQKsPcx5nBGsR63Pe8KnRUqmbJNENAfGftF3yuXoMMoVJJcYeUw5eVkm9WBPjWYt6HMWYJNesB5HaNVBaFc1M6dRjWSYnmewUMYy").unwrap(); - let wallet = Wallet::new_no_persist( + let wallet = Wallet::new( Bip84(key, KeychainKind::External), Some(Bip84(key, KeychainKind::Internal)), Network::Regtest, @@ -3030,8 +3041,7 @@ fn test_get_address() { } ); - let wallet = - Wallet::new_no_persist(Bip84(key, KeychainKind::External), None, Network::Regtest).unwrap(); + let wallet = Wallet::new(Bip84(key, KeychainKind::External), None, Network::Regtest).unwrap(); assert_eq!( wallet.peek_address(KeychainKind::Internal, 0), @@ -3049,14 +3059,10 @@ fn test_get_address() { #[test] fn test_reveal_addresses() { let desc = get_test_tr_single_sig_xprv(); - let mut wallet = Wallet::new_no_persist(desc, None, Network::Signet).unwrap(); + let mut wallet = Wallet::new(desc, None, Network::Signet).unwrap(); let keychain = KeychainKind::External; - let last_revealed_addr = wallet - .reveal_addresses_to(keychain, 9) - .unwrap() - .last() - .unwrap(); + let last_revealed_addr = wallet.reveal_addresses_to(keychain, 9).last().unwrap(); assert_eq!(wallet.derivation_index(keychain), Some(9)); let unused_addrs = wallet.list_unused_addresses(keychain).collect::>(); @@ -3064,7 +3070,7 @@ fn test_reveal_addresses() { assert_eq!(unused_addrs.last().unwrap(), &last_revealed_addr); // revealing to an already revealed index returns nothing - let mut already_revealed = wallet.reveal_addresses_to(keychain, 9).unwrap(); + let mut already_revealed = wallet.reveal_addresses_to(keychain, 9); assert!(already_revealed.next().is_none()); } @@ -3075,21 +3081,15 @@ fn test_get_address_no_reuse_single_descriptor() { let key = bitcoin::bip32::Xpriv::from_str("tprv8ZgxMBicQKsPcx5nBGsR63Pe8KnRUqmbJNENAfGftF3yuXoMMoVJJcYeUw5eVkm9WBPjWYt6HMWYJNesB5HaNVBaFc1M6dRjWSYnmewUMYy").unwrap(); let mut wallet = - Wallet::new_no_persist(Bip84(key, KeychainKind::External), None, Network::Regtest).unwrap(); + Wallet::new(Bip84(key, KeychainKind::External), None, Network::Regtest).unwrap(); let mut used_set = HashSet::new(); (0..3).for_each(|_| { - let external_addr = wallet - .reveal_next_address(KeychainKind::External) - .unwrap() - .address; + let external_addr = wallet.reveal_next_address(KeychainKind::External).address; assert!(used_set.insert(external_addr)); - let internal_addr = wallet - .reveal_next_address(KeychainKind::Internal) - .unwrap() - .address; + let internal_addr = wallet.reveal_next_address(KeychainKind::Internal).address; assert!(used_set.insert(internal_addr)); }); } @@ -3098,7 +3098,7 @@ fn test_get_address_no_reuse_single_descriptor() { fn test_taproot_remove_tapfields_after_finalize_sign_option() { let (mut wallet, _) = get_funded_wallet(get_test_tr_with_taptree()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(addr.script_pubkey()).drain_wallet(); let mut psbt = builder.finish().unwrap(); @@ -3123,7 +3123,7 @@ fn test_taproot_remove_tapfields_after_finalize_sign_option() { #[test] fn test_taproot_psbt_populate_tap_key_origins() { let (mut wallet, _) = get_funded_wallet(get_test_tr_single_sig_xprv()); - let addr = wallet.reveal_next_address(KeychainKind::External).unwrap(); + let addr = wallet.reveal_next_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); @@ -3158,7 +3158,7 @@ fn test_taproot_psbt_populate_tap_key_origins() { #[test] fn test_taproot_psbt_populate_tap_key_origins_repeated_key() { let (mut wallet, _) = get_funded_wallet(get_test_tr_repeated_key()); - let addr = wallet.reveal_next_address(KeychainKind::External).unwrap(); + let addr = wallet.reveal_next_address(KeychainKind::External); let path = vec![("rn4nre9c".to_string(), vec![0])] .into_iter() @@ -3224,7 +3224,7 @@ fn test_taproot_psbt_input_tap_tree() { use bitcoin::taproot; let (mut wallet, _) = get_funded_wallet(get_test_tr_with_taptree()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(addr.script_pubkey()).drain_wallet(); @@ -3267,7 +3267,7 @@ fn test_taproot_psbt_input_tap_tree() { #[test] fn test_taproot_sign_missing_witness_utxo() { let (mut wallet, _) = get_funded_wallet(get_test_tr_single_sig()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(addr.script_pubkey()).drain_wallet(); let mut psbt = builder.finish().unwrap(); @@ -3307,7 +3307,7 @@ fn test_taproot_sign_missing_witness_utxo() { #[test] fn test_taproot_sign_using_non_witness_utxo() { let (mut wallet, prev_txid) = get_funded_wallet(get_test_tr_single_sig()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.drain_to(addr.script_pubkey()).drain_wallet(); let mut psbt = builder.finish().unwrap(); @@ -3375,7 +3375,7 @@ fn test_taproot_foreign_utxo() { } fn test_spend_from_wallet(mut wallet: Wallet) { - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); @@ -3399,7 +3399,7 @@ fn test_spend_from_wallet(mut wallet: Wallet) { #[test] fn test_taproot_no_key_spend() { let (mut wallet, _) = get_funded_wallet(get_test_tr_with_taptree_both_priv()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); @@ -3434,7 +3434,7 @@ fn test_taproot_script_spend() { fn test_taproot_script_spend_sign_all_leaves() { use bdk_wallet::signer::TapLeavesOptions; let (mut wallet, _) = get_funded_wallet(get_test_tr_with_taptree_both_priv()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); @@ -3465,7 +3465,7 @@ fn test_taproot_script_spend_sign_include_some_leaves() { use bitcoin::taproot::TapLeafHash; let (mut wallet, _) = get_funded_wallet(get_test_tr_with_taptree_both_priv()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); @@ -3505,7 +3505,7 @@ fn test_taproot_script_spend_sign_exclude_some_leaves() { use bitcoin::taproot::TapLeafHash; let (mut wallet, _) = get_funded_wallet(get_test_tr_with_taptree_both_priv()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); @@ -3543,7 +3543,7 @@ fn test_taproot_script_spend_sign_exclude_some_leaves() { fn test_taproot_script_spend_sign_no_leaves() { use bdk_wallet::signer::TapLeavesOptions; let (mut wallet, _) = get_funded_wallet(get_test_tr_with_taptree_both_priv()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); @@ -3566,15 +3566,14 @@ fn test_taproot_script_spend_sign_no_leaves() { fn test_taproot_sign_derive_index_from_psbt() { let (mut wallet, _) = get_funded_wallet(get_test_tr_single_sig_xprv()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let mut psbt = builder.finish().unwrap(); // re-create the wallet with an empty db - let wallet_empty = - Wallet::new_no_persist(get_test_tr_single_sig_xprv(), None, Network::Regtest).unwrap(); + let wallet_empty = Wallet::new(get_test_tr_single_sig_xprv(), None, Network::Regtest).unwrap(); // signing with an empty db means that we will only look at the psbt to infer the // derivation index @@ -3587,7 +3586,7 @@ fn test_taproot_sign_derive_index_from_psbt() { #[test] fn test_taproot_sign_explicit_sighash_all() { let (mut wallet, _) = get_funded_wallet(get_test_tr_single_sig()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .drain_to(addr.script_pubkey()) @@ -3607,7 +3606,7 @@ fn test_taproot_sign_non_default_sighash() { let sighash = TapSighashType::NonePlusAnyoneCanPay; let (mut wallet, _) = get_funded_wallet(get_test_tr_single_sig()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); builder .drain_to(addr.script_pubkey()) @@ -3674,7 +3673,7 @@ fn test_taproot_sign_non_default_sighash() { #[test] fn test_spend_coinbase() { let descriptor = get_test_wpkh(); - let mut wallet = Wallet::new_no_persist(descriptor, None, Network::Regtest).unwrap(); + let mut wallet = Wallet::new(descriptor, None, Network::Regtest).unwrap(); let confirmation_height = 5; wallet @@ -3693,7 +3692,6 @@ fn test_spend_coinbase() { output: vec![TxOut { script_pubkey: wallet .next_unused_address(KeychainKind::External) - .unwrap() .script_pubkey(), value: Amount::from_sat(25_000), }], @@ -3783,7 +3781,7 @@ fn test_spend_coinbase() { fn test_allow_dust_limit() { let (mut wallet, _) = get_funded_wallet(get_test_single_sig_cltv()); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let mut builder = wallet.build_tx(); @@ -3809,7 +3807,7 @@ fn test_fee_rate_sign_no_grinding_high_r() { // instead of 70). We then check that our fee rate and fee calculation is // alright. let (mut wallet, _) = get_funded_wallet("wpkh(tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/*)"); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let fee_rate = FeeRate::from_sat_per_vb_unchecked(1); let mut builder = wallet.build_tx(); let mut data = PushBytesBuf::try_from(vec![0]).unwrap(); @@ -3875,7 +3873,7 @@ fn test_fee_rate_sign_grinding_low_r() { // We then check that our fee rate and fee calculation is alright and that our // signature is 70 bytes. let (mut wallet, _) = get_funded_wallet("wpkh(tprv8ZgxMBicQKsPd3EupYiPRhaMooHKUHJxNsTfYuScep13go8QFfHdtkG9nRkFGb7busX4isf6X9dURGCoKgitaApQ6MupRhZMcELAxTBRJgS/*)"); - let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); + let addr = wallet.next_unused_address(KeychainKind::External); let fee_rate = FeeRate::from_sat_per_vb_unchecked(1); let mut builder = wallet.build_tx(); builder diff --git a/example-crates/example_cli/Cargo.toml b/example-crates/example_cli/Cargo.toml index 42a0b51b09..c85d2e9963 100644 --- a/example-crates/example_cli/Cargo.toml +++ b/example-crates/example_cli/Cargo.toml @@ -7,7 +7,6 @@ edition = "2021" [dependencies] bdk_chain = { path = "../../crates/chain", features = ["serde", "miniscript"]} -bdk_persist = { path = "../../crates/persist" } bdk_file_store = { path = "../../crates/file_store" } bdk_tmp_plan = { path = "../../nursery/tmp_plan" } bdk_coin_select = { path = "../../nursery/coin_select" } diff --git a/example-crates/example_cli/src/lib.rs b/example-crates/example_cli/src/lib.rs index 319c23805f..30a877839b 100644 --- a/example-crates/example_cli/src/lib.rs +++ b/example-crates/example_cli/src/lib.rs @@ -3,6 +3,7 @@ use anyhow::Context; use bdk_coin_select::{coin_select_bnb, CoinSelector, CoinSelectorOpt, WeightedValue}; use bdk_file_store::Store; use serde::{de::DeserializeOwned, Serialize}; +use std::fmt::Debug; use std::{cmp::Reverse, collections::BTreeMap, path::PathBuf, sync::Mutex, time::Duration}; use bdk_chain::{ @@ -22,9 +23,9 @@ use bdk_chain::{ Anchor, Append, ChainOracle, DescriptorExt, FullTxOut, }; pub use bdk_file_store; -use bdk_persist::{Persist, PersistBackend}; pub use clap; +use bdk_chain::persist::{Persist, StagedPersist}; use clap::{Parser, Subcommand}; pub type KeychainTxGraph = IndexedTxGraph>; @@ -449,7 +450,7 @@ pub fn planned_utxos( graph: &Mutex>, - db: &Mutex>, + db: &Mutex>>, chain: &Mutex, keymap: &BTreeMap, network: Network, @@ -458,7 +459,14 @@ pub fn handle_commands anyhow::Result<()> where O::Error: std::error::Error + Send + Sync + 'static, - C: Default + Append + DeserializeOwned + Serialize + From>, + C: Default + + Append + + DeserializeOwned + + Serialize + + From> + + Send + + Sync + + Debug, { match cmd { Commands::ChainSpecific(_) => unreachable!("example code should handle this!"), @@ -669,7 +677,10 @@ where } /// The initial state returned by [`init`]. -pub struct Init { +pub struct Init +where + C: Default + Append + Serialize + DeserializeOwned + Debug + Send + Sync + 'static, +{ /// Arguments parsed by the cli. pub args: Args, /// Descriptor keymap. @@ -677,7 +688,7 @@ pub struct Init { /// Keychain-txout index. pub index: KeychainTxOutIndex, /// Persistence backend. - pub db: Mutex>, + pub db: Mutex>>, /// Initial changeset. pub init_changeset: C, } @@ -693,6 +704,7 @@ where + Append + Serialize + DeserializeOwned + + Debug + core::marker::Send + core::marker::Sync + 'static, @@ -727,13 +739,13 @@ where Err(err) => return Err(anyhow::anyhow!("failed to init db backend: {:?}", err)), }; - let init_changeset = db_backend.load_from_persistence()?.unwrap_or_default(); + let init_changeset = db_backend.load_changes()?.unwrap_or_default(); Ok(Init { args, keymap, index, - db: Mutex::new(Persist::new(db_backend)), + db: Mutex::new(StagedPersist::new(db_backend)), init_changeset, }) } diff --git a/example-crates/wallet_electrum/src/main.rs b/example-crates/wallet_electrum/src/main.rs index c411713ffa..7abcc9fc1e 100644 --- a/example-crates/wallet_electrum/src/main.rs +++ b/example-crates/wallet_electrum/src/main.rs @@ -3,6 +3,7 @@ const SEND_AMOUNT: Amount = Amount::from_sat(5000); const STOP_GAP: usize = 50; const BATCH_SIZE: usize = 5; +use anyhow::anyhow; use std::io::Write; use std::str::FromStr; @@ -13,24 +14,28 @@ use bdk_electrum::{ use bdk_file_store::Store; use bdk_wallet::bitcoin::{Address, Amount}; use bdk_wallet::chain::collections::HashSet; +use bdk_wallet::chain::persist::Persist; use bdk_wallet::{bitcoin::Network, Wallet}; use bdk_wallet::{KeychainKind, SignOptions}; fn main() -> Result<(), anyhow::Error> { let db_path = std::env::temp_dir().join("bdk-electrum-example"); - let db = + let mut db = Store::::open_or_create_new(DB_MAGIC.as_bytes(), db_path)?; let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)"; let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)"; - + let changeset = db + .load_changes() + .map_err(|e| anyhow!("load changes error: {}", e))?; let mut wallet = Wallet::new_or_load( external_descriptor, Some(internal_descriptor), - db, + changeset, Network::Testnet, )?; - let address = wallet.next_unused_address(KeychainKind::External)?; + let address = wallet.next_unused_address(KeychainKind::External); + db.write_changes(&wallet.take_staged())?; println!("Generated Address: {}", address); let balance = wallet.get_balance(); @@ -63,7 +68,7 @@ fn main() -> Result<(), anyhow::Error> { println!(); wallet.apply_update(update)?; - wallet.commit()?; + db.write_changes(&wallet.take_staged())?; let balance = wallet.get_balance(); println!("Wallet balance after syncing: {} sats", balance.total()); diff --git a/example-crates/wallet_esplora_async/src/main.rs b/example-crates/wallet_esplora_async/src/main.rs index 914bc89035..36312e036a 100644 --- a/example-crates/wallet_esplora_async/src/main.rs +++ b/example-crates/wallet_esplora_async/src/main.rs @@ -7,6 +7,7 @@ use bdk_wallet::{ }; use bdk_sqlite::{rusqlite::Connection, Store}; +use bdk_wallet::chain::persist::Persist; const SEND_AMOUNT: Amount = Amount::from_sat(5000); const STOP_GAP: usize = 50; @@ -16,18 +17,20 @@ const PARALLEL_REQUESTS: usize = 5; async fn main() -> Result<(), anyhow::Error> { let db_path = "bdk-esplora-async-example.sqlite"; let conn = Connection::open(db_path)?; - let db = Store::new(conn)?; + let mut db = Store::new(conn)?; let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)"; let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)"; + let changeset = db.load_changes()?; let mut wallet = Wallet::new_or_load( external_descriptor, Some(internal_descriptor), - db, + changeset, Network::Signet, )?; - let address = wallet.next_unused_address(KeychainKind::External)?; + let address = wallet.next_unused_address(KeychainKind::External); + db.write_changes(&wallet.take_staged())?; println!("Generated Address: {}", address); let balance = wallet.get_balance(); @@ -75,7 +78,7 @@ async fn main() -> Result<(), anyhow::Error> { let _ = update.graph_update.update_last_seen_unconfirmed(now); wallet.apply_update(update)?; - wallet.commit()?; + db.write_changes(&wallet.take_staged())?; println!(); let balance = wallet.get_balance(); diff --git a/example-crates/wallet_esplora_blocking/src/main.rs b/example-crates/wallet_esplora_blocking/src/main.rs index f42cb9d82f..a6269d22d7 100644 --- a/example-crates/wallet_esplora_blocking/src/main.rs +++ b/example-crates/wallet_esplora_blocking/src/main.rs @@ -7,6 +7,7 @@ use std::{collections::BTreeSet, io::Write, str::FromStr}; use bdk_esplora::{esplora_client, EsploraExt}; use bdk_file_store::Store; +use bdk_wallet::chain::persist::Persist; use bdk_wallet::{ bitcoin::{Address, Amount, Network}, KeychainKind, SignOptions, Wallet, @@ -14,19 +15,21 @@ use bdk_wallet::{ fn main() -> Result<(), anyhow::Error> { let db_path = std::env::temp_dir().join("bdk-esplora-example"); - let db = + let mut db = Store::::open_or_create_new(DB_MAGIC.as_bytes(), db_path)?; let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)"; let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)"; + let changeset = db.load_changes()?; let mut wallet = Wallet::new_or_load( external_descriptor, Some(internal_descriptor), - db, + changeset, Network::Testnet, )?; - let address = wallet.next_unused_address(KeychainKind::External)?; + let address = wallet.next_unused_address(KeychainKind::External); + db.write_changes(&wallet.take_staged())?; println!("Generated Address: {}", address); let balance = wallet.get_balance(); @@ -52,7 +55,7 @@ fn main() -> Result<(), anyhow::Error> { let _ = update.graph_update.update_last_seen_unconfirmed(now); wallet.apply_update(update)?; - wallet.commit()?; + db.write_changes(&wallet.take_staged())?; println!(); let balance = wallet.get_balance(); diff --git a/example-crates/wallet_rpc/src/main.rs b/example-crates/wallet_rpc/src/main.rs index 3bdd515c31..bdad754f3d 100644 --- a/example-crates/wallet_rpc/src/main.rs +++ b/example-crates/wallet_rpc/src/main.rs @@ -3,6 +3,7 @@ use bdk_bitcoind_rpc::{ Emitter, }; use bdk_file_store::Store; +use bdk_wallet::chain::persist::Persist; use bdk_wallet::{ bitcoin::{Block, Network, Transaction}, wallet::Wallet, @@ -86,13 +87,16 @@ fn main() -> anyhow::Result<()> { ); let start_load_wallet = Instant::now(); + let mut db = Store::::open_or_create_new( + DB_MAGIC.as_bytes(), + args.db_path, + )?; + let changeset = db.load_changes()?; + let mut wallet = Wallet::new_or_load( &args.descriptor, args.change_descriptor.as_ref(), - Store::::open_or_create_new( - DB_MAGIC.as_bytes(), - args.db_path, - )?, + changeset, args.network, )?; println!( @@ -143,7 +147,7 @@ fn main() -> anyhow::Result<()> { let connected_to = block_emission.connected_to(); let start_apply_block = Instant::now(); wallet.apply_block_connected_to(&block_emission.block, height, connected_to)?; - wallet.commit()?; + db.write_changes(&wallet.take_staged())?; let elapsed = start_apply_block.elapsed().as_secs_f32(); println!( "Applied block {} at height {} in {}s", @@ -153,7 +157,7 @@ fn main() -> anyhow::Result<()> { Emission::Mempool(mempool_emission) => { let start_apply_mempool = Instant::now(); wallet.apply_unconfirmed_txs(mempool_emission.iter().map(|(tx, time)| (tx, *time))); - wallet.commit()?; + db.write_changes(&wallet.take_staged())?; println!( "Applied unconfirmed transactions in {}s", start_apply_mempool.elapsed().as_secs_f32()