refactor: Edit ElectrumExt not to use WalletUpdate

This commit is contained in:
Vladimir Fomene 2023-08-21 11:20:38 +03:00 committed by 志宇
parent 2867e88b64
commit 6bcbb93233
No known key found for this signature in database
GPG Key ID: F6345C9837C2BDE8
3 changed files with 40 additions and 35 deletions

View File

@ -1,6 +1,5 @@
use bdk_chain::{ use bdk_chain::{
bitcoin::{OutPoint, ScriptBuf, Transaction, Txid}, bitcoin::{OutPoint, ScriptBuf, Transaction, Txid},
keychain::WalletUpdate,
local_chain::{self, CheckPoint}, local_chain::{self, CheckPoint},
tx_graph::{self, TxGraph}, tx_graph::{self, TxGraph},
Anchor, BlockId, ConfirmationHeightAnchor, ConfirmationTimeAnchor, Anchor, BlockId, ConfirmationHeightAnchor, ConfirmationTimeAnchor,
@ -15,7 +14,8 @@ use std::{
/// We assume that a block of this depth and deeper cannot be reorged. /// We assume that a block of this depth and deeper cannot be reorged.
const ASSUME_FINAL_DEPTH: u32 = 8; 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 /// 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 /// determine the full transactions missing from [`TxGraph`]. Then call [`Self::finalize`] to fetch
@ -58,7 +58,7 @@ impl<K, A: Anchor> ElectrumUpdate<K, A> {
client: &Client, client: &Client,
seen_at: Option<u64>, seen_at: Option<u64>,
missing: Vec<Txid>, 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 new_txs = client.batch_transaction_get(&missing)?;
let mut graph_update = TxGraph::<A>::new(new_txs); let mut graph_update = TxGraph::<A>::new(new_txs);
for (txid, anchors) in self.graph_update { for (txid, anchors) in self.graph_update {
@ -69,14 +69,7 @@ impl<K, A: Anchor> ElectrumUpdate<K, A> {
let _ = graph_update.insert_anchor(txid, anchor); let _ = graph_update.insert_anchor(txid, anchor);
} }
} }
Ok(WalletUpdate { Ok((graph_update, self.keychain_update, self.new_tip))
last_active_indices: self.keychain_update,
graph: graph_update,
chain: local_chain::Update {
tip: self.new_tip,
introduce_older_blocks: true,
},
})
} }
} }
@ -92,13 +85,19 @@ impl<K> ElectrumUpdate<K, ConfirmationHeightAnchor> {
client: &Client, client: &Client,
seen_at: Option<u64>, seen_at: Option<u64>,
missing: Vec<Txid>, missing: Vec<Txid>,
) -> Result<WalletUpdate<K, ConfirmationTimeAnchor>, Error> { ) -> Result<
let update = self.finalize(client, seen_at, missing)?; (
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 relevant_heights = {
let mut visited_heights = HashSet::new(); let mut visited_heights = HashSet::new();
update graph
.graph
.all_anchors() .all_anchors()
.iter() .iter()
.map(|(a, _)| a.confirmation_height_upper_bound()) .map(|(a, _)| a.confirmation_height_upper_bound())
@ -118,7 +117,7 @@ impl<K> ElectrumUpdate<K, ConfirmationHeightAnchor> {
.collect::<HashMap<u32, u64>>(); .collect::<HashMap<u32, u64>>();
let graph_changeset = { 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 { tx_graph::ChangeSet {
txs: old_changeset.txs, txs: old_changeset.txs,
txouts: old_changeset.txouts, txouts: old_changeset.txouts,
@ -140,15 +139,10 @@ impl<K> ElectrumUpdate<K, ConfirmationHeightAnchor> {
} }
}; };
Ok(WalletUpdate { let mut update = TxGraph::default();
last_active_indices: update.last_active_indices, update.apply_changeset(graph_changeset);
graph: {
let mut graph = TxGraph::default(); Ok((update, keychain_update, update_tip))
graph.apply_changeset(graph_changeset);
graph
},
chain: update.chain,
})
} }
} }
@ -457,7 +451,6 @@ fn populate_with_outpoints<K>(
}; };
let anchor = determine_tx_anchor(cps, res.height, res.tx_hash); let anchor = determine_tx_anchor(cps, res.height, res.tx_hash);
let tx_entry = update.graph_update.entry(res.tx_hash).or_default(); let tx_entry = update.graph_update.entry(res.tx_hash).or_default();
if let Some(anchor) = anchor { if let Some(anchor) = anchor {
tx_entry.insert(anchor); tx_entry.insert(anchor);

View File

@ -8,7 +8,7 @@ use bdk_chain::{
bitcoin::{Address, Network, OutPoint, ScriptBuf, Txid}, bitcoin::{Address, Network, OutPoint, ScriptBuf, Txid},
indexed_tx_graph::{self, IndexedTxGraph}, indexed_tx_graph::{self, IndexedTxGraph},
keychain::WalletChangeSet, keychain::WalletChangeSet,
local_chain::LocalChain, local_chain::{self, LocalChain},
Append, ConfirmationHeightAnchor, Append, ConfirmationHeightAnchor,
}; };
use bdk_electrum::{ use bdk_electrum::{
@ -269,25 +269,27 @@ fn main() -> anyhow::Result<()> {
.expect("must get time") .expect("must get time")
.as_secs(); .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 db_changeset = {
let mut chain = chain.lock().unwrap(); let mut chain = chain.lock().unwrap();
let mut graph = graph.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 indexed_tx_graph = {
let mut changeset = let mut changeset =
indexed_tx_graph::ChangeSet::<ConfirmationHeightAnchor, _>::default(); indexed_tx_graph::ChangeSet::<ConfirmationHeightAnchor, _>::default();
let (_, indexer) = graph let (_, indexer) = graph.index.reveal_to_target_multi(&keychain_update);
.index
.reveal_to_target_multi(&final_update.last_active_indices);
changeset.append(indexed_tx_graph::ChangeSet { changeset.append(indexed_tx_graph::ChangeSet {
indexer, indexer,
..Default::default() ..Default::default()
}); });
changeset.append(graph.apply_update(final_update.graph)); changeset.append(graph.apply_update(graph_update));
changeset changeset
}; };

View File

@ -9,6 +9,7 @@ use std::str::FromStr;
use bdk::bitcoin::Address; use bdk::bitcoin::Address;
use bdk::SignOptions; use bdk::SignOptions;
use bdk::{bitcoin::Network, Wallet}; use bdk::{bitcoin::Network, Wallet};
use bdk_electrum::bdk_chain::{keychain::WalletUpdate, local_chain};
use bdk_electrum::electrum_client::{self, ElectrumApi}; use bdk_electrum::electrum_client::{self, ElectrumApi};
use bdk_electrum::ElectrumExt; use bdk_electrum::ElectrumExt;
use bdk_file_store::Store; use bdk_file_store::Store;
@ -57,9 +58,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!(); println!();
let missing = electrum_update.missing_full_txs(wallet.as_ref()); 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()?; wallet.commit()?;
let balance = wallet.get_balance(); let balance = wallet.get_balance();