Skip to content

Commit dc1bc11

Browse files
TO_BE_SQUASHED: Review fixes
- Fix nomenclature after rebase - Flush stderr, not stdout - Always flush after printing in map/inspect - Fetch all the block anchors, not only the missing heights
1 parent b3c59c7 commit dc1bc11

File tree

1 file changed

+44
-26
lines changed
  • example-crates/example_esplora/src

1 file changed

+44
-26
lines changed

example-crates/example_esplora/src/main.rs

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use std::{
66

77
use bdk_chain::{
88
bitcoin::{Address, Network, OutPoint, ScriptBuf, Txid},
9-
indexed_tx_graph::{IndexedAdditions, IndexedTxGraph},
10-
keychain::LocalChangeSet,
9+
indexed_tx_graph::{self, IndexedTxGraph},
10+
keychain::WalletChangeSet,
1111
local_chain::{CheckPoint, LocalChain},
1212
Append, ConfirmationTimeAnchor,
1313
};
@@ -62,20 +62,20 @@ pub struct ScanOptions {
6262
fn main() -> anyhow::Result<()> {
6363
let (args, keymap, index, db, init_changeset) = example_cli::init::<
6464
EsploraCommands,
65-
LocalChangeSet<Keychain, ConfirmationTimeAnchor>,
65+
WalletChangeSet<Keychain, ConfirmationTimeAnchor>,
6666
>(DB_MAGIC, DB_PATH)?;
6767

6868
// Contruct `IndexedTxGraph` and `LocalChain` with our initial changeset. They are wrapped in
6969
// `Mutex` to display how they can be used in a multithreaded context. Technically the mutexes
7070
// aren't strictly needed here.
7171
let graph = Mutex::new({
7272
let mut graph = IndexedTxGraph::new(index);
73-
graph.apply_additions(init_changeset.indexed_additions);
73+
graph.apply_changeset(init_changeset.index_tx_graph);
7474
graph
7575
});
7676
let chain = Mutex::new({
7777
let mut chain = LocalChain::default();
78-
chain.apply_changeset(&init_changeset.chain_changeset);
78+
chain.apply_changeset(&init_changeset.chain);
7979
chain
8080
});
8181

@@ -84,7 +84,7 @@ fn main() -> anyhow::Result<()> {
8484
Network::Testnet => "https://blockstream.info/testnet/api",
8585
Network::Regtest => "http://localhost:3002",
8686
Network::Signet => "https://mempool.space/signet/api",
87-
_ => panic!("unsuported network"),
87+
_ => panic!("unsupported network"),
8888
};
8989

9090
let client = esplora_client::Builder::new(esplora_url).build_blocking()?;
@@ -116,7 +116,7 @@ fn main() -> anyhow::Result<()> {
116116

117117
// This is where we will accumulate changes of `IndexedTxGraph` and `LocalChain` and persist
118118
// these changes as a batch at the end.
119-
let mut changeset = LocalChangeSet::<Keychain, ConfirmationTimeAnchor>::default();
119+
let mut changeset = WalletChangeSet::<Keychain, ConfirmationTimeAnchor>::default();
120120

121121
// Prepare the `IndexedTxGraph` update based on whether we are scanning or syncing.
122122
// Scanning: We are iterating through spks of all keychains and scanning for transactions for
@@ -135,7 +135,7 @@ fn main() -> anyhow::Result<()> {
135135
.index
136136
.spks_of_all_keychains()
137137
.into_iter()
138-
// This `map` is purely for printing to stdout.
138+
// This `map` is purely for logging.
139139
.map(|(keychain, iter)| {
140140
let mut first = true;
141141
let spk_iter = iter.inspect(move |(i, _)| {
@@ -145,7 +145,7 @@ fn main() -> anyhow::Result<()> {
145145
}
146146
eprint!("{} ", i);
147147
// Flush early to ensure we print at every iteration.
148-
let _ = io::stdout().flush();
148+
let _ = io::stderr().flush();
149149
});
150150
(keychain, spk_iter)
151151
})
@@ -173,7 +173,7 @@ fn main() -> anyhow::Result<()> {
173173
.expect("mutex must not be poisoned")
174174
.index
175175
.reveal_to_target_multi(&keychain_indices_update);
176-
LocalChangeSet::from(IndexedAdditions::from(index_additions))
176+
WalletChangeSet::from(indexed_tx_graph::ChangeSet::from(index_additions))
177177
});
178178

179179
graph_update
@@ -217,6 +217,8 @@ fn main() -> anyhow::Result<()> {
217217
.collect::<Vec<_>>();
218218
spks = Box::new(spks.chain(all_spks.into_iter().map(|(index, script)| {
219219
eprintln!("scanning {:?}", index);
220+
// Flush early to ensure we print at every iteration.
221+
let _ = io::stderr().flush();
220222
script
221223
})));
222224
}
@@ -232,6 +234,8 @@ fn main() -> anyhow::Result<()> {
232234
Address::from_script(&script, args.network).unwrap(),
233235
index
234236
);
237+
// Flush early to ensure we print at every iteration.
238+
let _ = io::stderr().flush();
235239
script
236240
})));
237241
}
@@ -253,6 +257,8 @@ fn main() -> anyhow::Result<()> {
253257
"Checking if outpoint {} (value: {}) has been spent",
254258
utxo.outpoint, utxo.txout.value
255259
);
260+
// Flush early to ensure we print at every iteration.
261+
let _ = io::stderr().flush();
256262
})
257263
.map(|utxo| utxo.outpoint),
258264
);
@@ -264,11 +270,13 @@ fn main() -> anyhow::Result<()> {
264270
let unconfirmed_txids = graph
265271
.graph()
266272
.list_chain_txs(&*chain, chain_tip)
267-
.filter(|canonical_tx| !canonical_tx.observed_as.is_confirmed())
268-
.map(|canonical_tx| canonical_tx.node.txid)
273+
.filter(|canonical_tx| !canonical_tx.chain_position.is_confirmed())
274+
.map(|canonical_tx| canonical_tx.tx_node.txid)
269275
.collect::<Vec<Txid>>();
270276
txids = Box::new(unconfirmed_txids.into_iter().inspect(|txid| {
271277
eprintln!("Checking if {} is confirmed yet", txid);
278+
// Flush early to ensure we print at every iteration.
279+
let _ = io::stderr().flush();
272280
}));
273281
}
274282
}
@@ -284,15 +292,29 @@ fn main() -> anyhow::Result<()> {
284292

285293
println!();
286294

287-
// Up to this point, we have only created a `TxGraph` update, but not an update for our
288-
// `ChainOracle` implementation (`LocalChain`). The `TxGraph` update may contain chain anchors,
289-
// so we need the corresponding blocks to exist in our `LocalChain`. Here, we find the heights
290-
// of missing blocks in `LocalChain`.
291-
//
292-
// Getting the local chain tip is only for printing to stdout.
295+
// We apply the `TxGraph` update, and append the resultant changes to `changeset`
296+
// (for persistance)
297+
changeset.append({
298+
let indexed_graph_additions = graph.lock().unwrap().apply_update(graph_update);
299+
WalletChangeSet::from(indexed_graph_additions)
300+
});
301+
302+
// Now that we're done updating the `TxGraph`, it's time to update the `LocalChain`!
303+
// We want the `LocalChain` to have data about all the anchors in the `TxGraph` - for this
304+
// reason, we want retrieve the heights of the newly added anchors, and fetch the corresponding
305+
// blocks.
306+
307+
// We get the heights of all the anchors introduced by the changeset, and the local chain tip.
308+
// Note that the latter is only used for logging.
293309
let (missing_block_heights, tip) = {
294310
let chain = &*chain.lock().unwrap();
295-
let heights_to_fetch = graph_update.missing_heights(chain).collect::<Vec<_>>();
311+
let heights_to_fetch = changeset
312+
.index_tx_graph
313+
.graph
314+
.anchors
315+
.iter()
316+
.map(|(a, _)| a.confirmation_height)
317+
.collect::<Vec<_>>();
296318
let tip = chain.tip();
297319
(heights_to_fetch, tip)
298320
};
@@ -307,15 +329,11 @@ fn main() -> anyhow::Result<()> {
307329

308330
println!("new tip: {}", chain_update.tip.height());
309331

310-
// We apply the `LocalChain` and `TxGraph` updates, and append the resultant changes to
311-
// `changeset` (for persistance).
332+
// We apply the `LocalChain` update, and append the resultant changes to `changeset`
333+
// (for persistance).
312334
changeset.append({
313335
let chain_additions = chain.lock().unwrap().apply_update(chain_update)?;
314-
LocalChangeSet::from(chain_additions)
315-
});
316-
changeset.append({
317-
let indexed_graph_additions = graph.lock().unwrap().apply_update(graph_update);
318-
LocalChangeSet::from(indexed_graph_additions)
336+
WalletChangeSet::from(chain_additions)
319337
});
320338

321339
// We persist `changeset`.

0 commit comments

Comments
 (0)