From 2175a8a0397e843d288e7e1e1b758aa6c01a4add Mon Sep 17 00:00:00 2001 From: raphjaph Date: Sun, 2 Mar 2025 02:16:37 +0100 Subject: [PATCH] I'm a genius --- src/subcommand/wallet/receive.rs | 2 ++ src/wallet.rs | 7 +++++++ src/wallet/database.rs | 8 +++++++- src/wallet/wallet_constructor.rs | 1 + tests/lib.rs | 1 + tests/wallet/receive.rs | 18 +++++++++++++----- 6 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/subcommand/wallet/receive.rs b/src/subcommand/wallet/receive.rs index 1c58960f37..b829be5da0 100644 --- a/src/subcommand/wallet/receive.rs +++ b/src/subcommand/wallet/receive.rs @@ -26,6 +26,8 @@ impl Receive { ); } + wallet.persist()?; + Ok(Some(Box::new(Output { addresses }))) } } diff --git a/src/wallet.rs b/src/wallet.rs index d8f26840f7..fe6de14487 100644 --- a/src/wallet.rs +++ b/src/wallet.rs @@ -68,6 +68,7 @@ pub(crate) struct Wallet { has_rune_index: bool, has_sat_index: bool, ord_client: reqwest::blocking::Client, + persister: DatabasePersister, rpc_url: Url, settings: Settings, pub(crate) wallet: PersistedWallet, @@ -126,6 +127,12 @@ impl Wallet { Ok(descriptor) } + pub(crate) fn persist(&mut self) -> Result { + self.wallet.persist(&mut self.persister)?; + + Ok(()) + } + pub(crate) fn get_wallet_sat_ranges(&self) -> Result)>> { ensure!( self.has_sat_index, diff --git a/src/wallet/database.rs b/src/wallet/database.rs index b8ee202a0f..34d00c6a68 100644 --- a/src/wallet/database.rs +++ b/src/wallet/database.rs @@ -1,3 +1,5 @@ +use bdk_wallet::chain::Merge; + use super::*; pub(crate) struct TransactionPersister<'a>(pub(crate) &'a mut WriteTransaction); @@ -21,11 +23,15 @@ impl WalletPersister for TransactionPersister<'_> { } fn persist(persister: &mut Self, changeset: &ChangeSet) -> std::result::Result<(), Self::Error> { + let mut current = Self::initialize(persister)?; + + current.merge(changeset.clone()); + let wtx = &persister.0; wtx .open_table(CHANGESET)? - .insert((), serde_json::to_string(changeset)?.as_str())?; + .insert((), serde_json::to_string(¤t)?.as_str())?; Ok(()) } diff --git a/src/wallet/wallet_constructor.rs b/src/wallet/wallet_constructor.rs index 9285280f09..ee681fec48 100644 --- a/src/wallet/wallet_constructor.rs +++ b/src/wallet/wallet_constructor.rs @@ -79,6 +79,7 @@ impl WalletConstructor { has_rune_index: status.rune_index, has_sat_index: status.sat_index, ord_client: self.ord_client, + persister, rpc_url: self.rpc_url, settings: self.settings, wallet, diff --git a/tests/lib.rs b/tests/lib.rs index 73812cff7e..cc8b663049 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -95,6 +95,7 @@ fn create_wallet(core: &mockcore::Handle, ord: &TestServer) -> Arc { .core(core) .ord(ord) .stdout_regex(".*") + .stderr_regex(".*") .run_and_extract_stdout(); tempdir diff --git a/tests/wallet/receive.rs b/tests/wallet/receive.rs index e857067211..3cacad54fc 100644 --- a/tests/wallet/receive.rs +++ b/tests/wallet/receive.rs @@ -7,15 +7,23 @@ fn receive() { let tempdir = create_wallet(&core, &ord); + let output = CommandBuilder::new("wallet receive") + .temp_dir(tempdir.clone()) + .core(&core) + .ord(&ord) + .run_and_deserialize_output::(); + + let first_address = output.addresses.first().unwrap(); + + assert!(first_address.is_valid_for_network(Network::Bitcoin)); + let output = CommandBuilder::new("wallet receive") .temp_dir(tempdir) .core(&core) .ord(&ord) .run_and_deserialize_output::(); - assert!(output - .addresses - .first() - .unwrap() - .is_valid_for_network(Network::Bitcoin)); + let second_address = output.addresses.first().unwrap(); + + assert!(second_address != first_address); }