use bdk_chain::{
bitcoin::{OutPoint, ScriptBuf, Transaction, Txid},
- keychain::WalletUpdate,
local_chain::{self, CheckPoint},
tx_graph::{self, TxGraph},
Anchor, BlockId, ConfirmationHeightAnchor, ConfirmationTimeAnchor,
/// 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
client: &Client,
seen_at: Option<u64>,
missing: Vec<Txid>,
- ) -> Result<WalletUpdate<K, A>, Error> {
+ ) -> Result<(TxGraph<A>, BTreeMap<K, u32>, local_chain::CheckPoint), Error> {
let new_txs = client.batch_transaction_get(&missing)?;
let mut graph_update = TxGraph::<A>::new(new_txs);
for (txid, anchors) in self.graph_update {
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))
}
}
client: &Client,
seen_at: Option<u64>,
missing: Vec<Txid>,
- ) -> Result<WalletUpdate<K, ConfirmationTimeAnchor>, Error> {
- let update = self.finalize(client, seen_at, missing)?;
+ ) -> Result<
+ (
+ TxGraph<ConfirmationTimeAnchor>,
+ BTreeMap<K, u32>,
+ 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())
.collect::<HashMap<u32, u64>>();
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,
}
};
- 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))
}
}
};
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);
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::{
.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::<ConfirmationHeightAnchor, _>::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
};
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;
println!();
let missing = electrum_update.missing_full_txs(wallet.as_ref());
- let update = electrum_update.finalize_as_confirmation_time(&client, None, missing)?;
-
- wallet.apply_update(update)?;
+ let (graph_update, keychain_update, update_tip) =
+ electrum_update.finalize_as_confirmation_time(&client, None, missing)?;
+
+ 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();