diff --git a/crates/electrum/src/electrum_ext.rs b/crates/electrum/src/electrum_ext.rs index c7859bdf..e81ef1d3 100644 --- a/crates/electrum/src/electrum_ext.rs +++ b/crates/electrum/src/electrum_ext.rs @@ -1,6 +1,5 @@ use bdk_chain::{ bitcoin::{OutPoint, ScriptBuf, Transaction, Txid}, - keychain::WalletUpdate, local_chain::{self, CheckPoint}, tx_graph::{self, TxGraph}, Anchor, BlockId, ConfirmationHeightAnchor, ConfirmationTimeAnchor, @@ -15,7 +14,8 @@ use std::{ /// We assume that a block of this depth and deeper cannot be reorged. const ASSUME_FINAL_DEPTH: u32 = 8; -/// Represents an update fetched from an Electrum server, but excludes full transactions. +/// Represents an update fetched from an Electrum server, but excludes full +/// transactions. /// /// To provide a complete update to [`TxGraph`], you'll need to call [`Self::missing_full_txs`] to /// determine the full transactions missing from [`TxGraph`]. Then call [`Self::finalize`] to fetch @@ -58,7 +58,7 @@ impl ElectrumUpdate { client: &Client, seen_at: Option, missing: Vec, - ) -> Result, Error> { + ) -> Result<(TxGraph, BTreeMap, local_chain::CheckPoint), Error> { let new_txs = client.batch_transaction_get(&missing)?; let mut graph_update = TxGraph::::new(new_txs); for (txid, anchors) in self.graph_update { @@ -69,14 +69,7 @@ impl ElectrumUpdate { let _ = graph_update.insert_anchor(txid, anchor); } } - Ok(WalletUpdate { - last_active_indices: self.keychain_update, - graph: graph_update, - chain: local_chain::Update { - tip: self.new_tip, - introduce_older_blocks: true, - }, - }) + Ok((graph_update, self.keychain_update, self.new_tip)) } } @@ -92,13 +85,19 @@ impl ElectrumUpdate { client: &Client, seen_at: Option, missing: Vec, - ) -> Result, Error> { - let update = self.finalize(client, seen_at, missing)?; + ) -> Result< + ( + TxGraph, + BTreeMap, + local_chain::CheckPoint, + ), + Error, + > { + let (graph, keychain_update, update_tip) = self.finalize(client, seen_at, missing)?; let relevant_heights = { let mut visited_heights = HashSet::new(); - update - .graph + graph .all_anchors() .iter() .map(|(a, _)| a.confirmation_height_upper_bound()) @@ -118,7 +117,7 @@ impl ElectrumUpdate { .collect::>(); let graph_changeset = { - let old_changeset = TxGraph::default().apply_update(update.graph.clone()); + let old_changeset = TxGraph::default().apply_update(graph.clone()); tx_graph::ChangeSet { txs: old_changeset.txs, txouts: old_changeset.txouts, @@ -140,15 +139,10 @@ impl ElectrumUpdate { } }; - Ok(WalletUpdate { - last_active_indices: update.last_active_indices, - graph: { - let mut graph = TxGraph::default(); - graph.apply_changeset(graph_changeset); - graph - }, - chain: update.chain, - }) + let mut update = TxGraph::default(); + update.apply_changeset(graph_changeset); + + Ok((update, keychain_update, update_tip)) } } @@ -457,7 +451,6 @@ fn populate_with_outpoints( }; let anchor = determine_tx_anchor(cps, res.height, res.tx_hash); - let tx_entry = update.graph_update.entry(res.tx_hash).or_default(); if let Some(anchor) = anchor { tx_entry.insert(anchor); diff --git a/example-crates/example_electrum/src/main.rs b/example-crates/example_electrum/src/main.rs index 4c5fde6a..56cc144c 100644 --- a/example-crates/example_electrum/src/main.rs +++ b/example-crates/example_electrum/src/main.rs @@ -8,7 +8,7 @@ use bdk_chain::{ bitcoin::{Address, Network, OutPoint, ScriptBuf, Txid}, indexed_tx_graph::{self, IndexedTxGraph}, keychain::WalletChangeSet, - local_chain::LocalChain, + local_chain::{self, LocalChain}, Append, ConfirmationHeightAnchor, }; use bdk_electrum::{ @@ -269,25 +269,27 @@ fn main() -> anyhow::Result<()> { .expect("must get time") .as_secs(); - let final_update = response.finalize(&client, Some(now), missing_txids)?; + let (graph_update, keychain_update, update_tip) = + response.finalize(&client, Some(now), missing_txids)?; let db_changeset = { let mut chain = chain.lock().unwrap(); let mut graph = graph.lock().unwrap(); - let chain = chain.apply_update(final_update.chain)?; + let chain = chain.apply_update(local_chain::Update { + tip: update_tip, + introduce_older_blocks: true, + })?; let indexed_tx_graph = { let mut changeset = indexed_tx_graph::ChangeSet::::default(); - let (_, indexer) = graph - .index - .reveal_to_target_multi(&final_update.last_active_indices); + let (_, indexer) = graph.index.reveal_to_target_multi(&keychain_update); changeset.append(indexed_tx_graph::ChangeSet { indexer, ..Default::default() }); - changeset.append(graph.apply_update(final_update.graph)); + changeset.append(graph.apply_update(graph_update)); changeset }; diff --git a/example-crates/wallet_electrum/src/main.rs b/example-crates/wallet_electrum/src/main.rs index 52def58e..3ba7e5f4 100644 --- a/example-crates/wallet_electrum/src/main.rs +++ b/example-crates/wallet_electrum/src/main.rs @@ -9,6 +9,7 @@ use std::str::FromStr; use bdk::bitcoin::Address; use bdk::SignOptions; use bdk::{bitcoin::Network, Wallet}; +use bdk_electrum::bdk_chain::{keychain::WalletUpdate, local_chain}; use bdk_electrum::electrum_client::{self, ElectrumApi}; use bdk_electrum::ElectrumExt; use bdk_file_store::Store; @@ -57,9 +58,18 @@ fn main() -> Result<(), Box> { println!(); let missing = electrum_update.missing_full_txs(wallet.as_ref()); - let update = electrum_update.finalize_as_confirmation_time(&client, None, missing)?; + let (graph_update, keychain_update, update_tip) = + electrum_update.finalize_as_confirmation_time(&client, None, missing)?; - wallet.apply_update(update)?; + let wallet_update = WalletUpdate { + last_active_indices: keychain_update, + graph: graph_update, + chain: local_chain::Update { + tip: update_tip, + introduce_older_blocks: true, + }, + }; + wallet.apply_update(wallet_update)?; wallet.commit()?; let balance = wallet.get_balance();