Skip to content

Commit 9371625

Browse files
vladimirfomeneevanlinjin
authored andcommitted
refactor: Edit ElectrumExt not to use WalletUpdate
1 parent 8f978f8 commit 9371625

File tree

3 files changed

+41
-36
lines changed

3 files changed

+41
-36
lines changed

crates/electrum/src/electrum_ext.rs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use bdk_chain::{
22
bitcoin::{OutPoint, ScriptBuf, Transaction, Txid},
3-
keychain::WalletUpdate,
43
local_chain::{self, CheckPoint},
54
tx_graph::{self, TxGraph},
65
Anchor, BlockId, ConfirmationHeightAnchor, ConfirmationTimeAnchor,
@@ -15,7 +14,8 @@ use std::{
1514
/// We assume that a block of this depth and deeper cannot be reorged.
1615
const ASSUME_FINAL_DEPTH: u32 = 8;
1716

18-
/// Represents an update fetched from an Electrum server, but excludes full transactions.
17+
/// Represents an update fetched from an Electrum server, but excludes full
18+
/// transactions.
1919
///
2020
/// To provide a complete update to [`TxGraph`], you'll need to call [`Self::missing_full_txs`] to
2121
/// determine the full transactions missing from [`TxGraph`]. Then call [`Self::finalize`] to fetch
@@ -58,7 +58,7 @@ impl<K, A: Anchor> ElectrumUpdate<K, A> {
5858
client: &Client,
5959
seen_at: Option<u64>,
6060
missing: Vec<Txid>,
61-
) -> Result<WalletUpdate<K, A>, Error> {
61+
) -> Result<(TxGraph<A>, BTreeMap<K, u32>, local_chain::CheckPoint), Error> {
6262
let new_txs = client.batch_transaction_get(&missing)?;
6363
let mut graph_update = TxGraph::<A>::new(new_txs);
6464
for (txid, anchors) in self.graph_update {
@@ -69,14 +69,7 @@ impl<K, A: Anchor> ElectrumUpdate<K, A> {
6969
let _ = graph_update.insert_anchor(txid, anchor);
7070
}
7171
}
72-
Ok(WalletUpdate {
73-
last_active_indices: self.keychain_update,
74-
graph: graph_update,
75-
chain: local_chain::Update {
76-
tip: self.new_tip,
77-
introduce_older_blocks: true,
78-
},
79-
})
72+
Ok((graph_update, self.keychain_update, self.new_tip))
8073
}
8174
}
8275

@@ -92,13 +85,19 @@ impl<K> ElectrumUpdate<K, ConfirmationHeightAnchor> {
9285
client: &Client,
9386
seen_at: Option<u64>,
9487
missing: Vec<Txid>,
95-
) -> Result<WalletUpdate<K, ConfirmationTimeAnchor>, Error> {
96-
let update = self.finalize(client, seen_at, missing)?;
88+
) -> Result<
89+
(
90+
TxGraph<ConfirmationTimeAnchor>,
91+
BTreeMap<K, u32>,
92+
local_chain::CheckPoint,
93+
),
94+
Error,
95+
> {
96+
let (graph, keychain_update, update_tip) = self.finalize(client, seen_at, missing)?;
9797

9898
let relevant_heights = {
9999
let mut visited_heights = HashSet::new();
100-
update
101-
.graph
100+
graph
102101
.all_anchors()
103102
.iter()
104103
.map(|(a, _)| a.confirmation_height_upper_bound())
@@ -118,7 +117,7 @@ impl<K> ElectrumUpdate<K, ConfirmationHeightAnchor> {
118117
.collect::<HashMap<u32, u64>>();
119118

120119
let graph_changeset = {
121-
let old_changeset = TxGraph::default().apply_update(update.graph.clone());
120+
let old_changeset = TxGraph::default().apply_update(graph.clone());
122121
tx_graph::ChangeSet {
123122
txs: old_changeset.txs,
124123
txouts: old_changeset.txouts,
@@ -140,15 +139,10 @@ impl<K> ElectrumUpdate<K, ConfirmationHeightAnchor> {
140139
}
141140
};
142141

143-
Ok(WalletUpdate {
144-
last_active_indices: update.last_active_indices,
145-
graph: {
146-
let mut graph = TxGraph::default();
147-
graph.apply_changeset(graph_changeset);
148-
graph
149-
},
150-
chain: update.chain,
151-
})
142+
let mut update = TxGraph::default();
143+
update.apply_changeset(graph_changeset);
144+
145+
Ok((update, keychain_update, update_tip))
152146
}
153147
}
154148

@@ -457,7 +451,6 @@ fn populate_with_outpoints<K>(
457451
};
458452

459453
let anchor = determine_tx_anchor(cps, res.height, res.tx_hash);
460-
461454
let tx_entry = update.graph_update.entry(res.tx_hash).or_default();
462455
if let Some(anchor) = anchor {
463456
tx_entry.insert(anchor);

example-crates/example_electrum/src/main.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use bdk_chain::{
88
bitcoin::{Address, Network, OutPoint, ScriptBuf, Txid},
99
indexed_tx_graph::{self, IndexedTxGraph},
1010
keychain::WalletChangeSet,
11-
local_chain::LocalChain,
11+
local_chain::{self, LocalChain},
1212
Append, ConfirmationHeightAnchor,
1313
};
1414
use bdk_electrum::{
@@ -269,25 +269,27 @@ fn main() -> anyhow::Result<()> {
269269
.expect("must get time")
270270
.as_secs();
271271

272-
let final_update = response.finalize(&client, Some(now), missing_txids)?;
272+
let (graph_update, keychain_update, update_tip) =
273+
response.finalize(&client, Some(now), missing_txids)?;
273274

274275
let db_changeset = {
275276
let mut chain = chain.lock().unwrap();
276277
let mut graph = graph.lock().unwrap();
277278

278-
let chain = chain.apply_update(final_update.chain)?;
279+
let chain = chain.apply_update(local_chain::Update {
280+
tip: update_tip,
281+
introduce_older_blocks: true,
282+
})?;
279283

280284
let index_tx_graph = {
281285
let mut changeset =
282286
indexed_tx_graph::ChangeSet::<ConfirmationHeightAnchor, _>::default();
283-
let (_, indexer) = graph
284-
.index
285-
.reveal_to_target_multi(&final_update.last_active_indices);
287+
let (_, indexer) = graph.index.reveal_to_target_multi(&keychain_update);
286288
changeset.append(indexed_tx_graph::ChangeSet {
287289
indexer,
288290
..Default::default()
289291
});
290-
changeset.append(graph.apply_update(final_update.graph));
292+
changeset.append(graph.apply_update(graph_update));
291293
changeset
292294
};
293295

example-crates/wallet_electrum/src/main.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::str::FromStr;
99
use bdk::bitcoin::Address;
1010
use bdk::SignOptions;
1111
use bdk::{bitcoin::Network, Wallet};
12+
use bdk_electrum::bdk_chain::{keychain::WalletUpdate, local_chain};
1213
use bdk_electrum::electrum_client::{self, ElectrumApi};
1314
use bdk_electrum::ElectrumExt;
1415
use bdk_file_store::Store;
@@ -57,9 +58,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5758
println!();
5859

5960
let missing = electrum_update.missing_full_txs(wallet.as_ref());
60-
let update = electrum_update.finalize_as_confirmation_time(&client, None, missing)?;
61-
62-
wallet.apply_update(update)?;
61+
let (graph_update, keychain_update, update_tip) =
62+
electrum_update.finalize_as_confirmation_time(&client, None, missing)?;
63+
64+
let wallet_update = WalletUpdate {
65+
last_active_indices: keychain_update,
66+
graph: graph_update,
67+
chain: local_chain::Update {
68+
tip: update_tip,
69+
introduce_older_blocks: true,
70+
},
71+
};
72+
wallet.apply_update(wallet_update)?;
6373
wallet.commit()?;
6474

6575
let balance = wallet.get_balance();

0 commit comments

Comments
 (0)