• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bitcoindevkit / bdk / 9740859344

01 Jul 2024 08:30AM UTC coverage: 83.173% (+0.1%) from 83.029%
9740859344

Pull #1478

github

web-flow
Merge eb10e4379 into 22368ab7b
Pull Request #1478: Make `bdk_esplora` more modular

351 of 415 new or added lines in 2 files covered. (84.58%)

213 existing lines in 7 files now uncovered.

11141 of 13395 relevant lines covered (83.17%)

16583.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.1
/crates/esplora/src/blocking_ext.rs
1
use std::collections::BTreeSet;
2
use std::thread::JoinHandle;
3

4
use bdk_chain::collections::BTreeMap;
5
use bdk_chain::spk_client::{FullScanRequest, FullScanResult, SyncRequest, SyncResult};
6
use bdk_chain::{
7
    bitcoin::{Amount, BlockHash, OutPoint, ScriptBuf, TxOut, Txid},
8
    local_chain::CheckPoint,
9
    BlockId, ConfirmationTimeHeightAnchor, TxGraph,
10
};
11
use bdk_chain::{Anchor, Indexed};
12
use esplora_client::{OutputStatus, Tx, TxStatus};
13

14
use crate::anchor_from_status;
15

16
/// [`esplora_client::Error`] wrapped in a [`Box`].
17
pub type Error = Box<esplora_client::Error>;
18

19
/// Trait to extend the functionality of [`esplora_client::BlockingClient`].
20
///
21
/// Refer to [crate-level documentation](crate) for more.
22
pub trait EsploraExt {
23
    /// Scan keychain scripts for transactions against Esplora, returning an update that can be
24
    /// applied to the receiving structures.
25
    ///
26
    /// `request` provides the data required to perform a script-pubkey-based full scan
27
    /// (see [`FullScanRequest`]). The full scan for each keychain (`K`) stops after a gap of
28
    /// `stop_gap` script pubkeys with no associated transactions. `parallel_requests` specifies
29
    /// the maximum number of HTTP requests to make in parallel.
30
    ///
31
    /// Refer to [crate-level docs](crate) for more.
32
    fn full_scan<K: Ord + Clone>(
33
        &self,
34
        request: FullScanRequest<K>,
35
        stop_gap: usize,
36
        parallel_requests: usize,
37
    ) -> Result<FullScanResult<K>, Error>;
38

39
    /// Sync a set of scripts, txids, and/or outpoints against Esplora.
40
    ///
41
    /// `request` provides the data required to perform a script-pubkey-based sync (see
42
    /// [`SyncRequest`]). `parallel_requests` specifies the maximum number of HTTP requests to make
43
    /// in parallel.
44
    ///
45
    /// Refer to [crate-level docs](crate) for more.
46
    fn sync(&self, request: SyncRequest, parallel_requests: usize) -> Result<SyncResult, Error>;
47

48
    /// Fetch transactions and associated [`ConfirmationTimeHeightAnchor`]s by scanning
49
    /// `keychain_spks` against Esplora.
50
    ///
51
    /// `keychain_spks` is an *unbounded* indexed-[`ScriptBuf`] iterator that represents scripts
52
    /// derived from a keychain. The scanning logic stops after a `stop_gap` number of consecutive
53
    /// scripts with no transaction history is reached. `parallel_requests` specifies the maximum
54
    /// number of HTTP requests to make in parallel.
55
    ///
56
    /// A [`TxGraph`] (containing the fetched transactions and anchors) and the last active keychain
57
    /// index (if any) is returned. The last active keychain index is the keychain's last script
58
    /// pubkey that contains a non-empty transaction history.
59
    ///
60
    /// Refer to [crate-level docs](crate) for more.
61
    fn fetch_txs_with_keychain_spks<I: Iterator<Item = Indexed<ScriptBuf>>>(
62
        &self,
63
        keychain_spks: I,
64
        stop_gap: usize,
65
        parallel_requests: usize,
66
    ) -> Result<(TxGraph<ConfirmationTimeHeightAnchor>, Option<u32>), Error>;
67

68
    /// Fetch transactions and associated [`ConfirmationTimeHeightAnchor`]s by scanning `spks`
69
    /// against Esplora.
70
    ///
71
    /// Unlike with [`EsploraExt::populate_using_keychain_spks`], `spks` must be *bounded* as all
72
    /// contained scripts will be scanned. `parallel_requests` specifies the maximum number of HTTP
73
    /// requests to make in parallel.
74
    ///
75
    /// Refer to [crate-level docs](crate) for more.
76
    fn fetch_txs_with_spks<I: IntoIterator<Item = ScriptBuf>>(
77
        &self,
78
        spks: I,
79
        parallel_requests: usize,
80
    ) -> Result<TxGraph<ConfirmationTimeHeightAnchor>, Error>
81
    where
82
        I::IntoIter: ExactSizeIterator;
83

84
    /// Fetch transactions and associated [`ConfirmationTimeHeightAnchor`]s by scanning `txids`
85
    /// against Esplora.
86
    ///
87
    /// `parallel_requests` specifies the maximum number of HTTP requests to make in parallel.
88
    ///
89
    /// Refer to [crate-level docs](crate) for more.
90
    fn fetch_txs_with_txids<I: IntoIterator<Item = Txid>>(
91
        &self,
92
        txids: I,
93
        parallel_requests: usize,
94
    ) -> Result<TxGraph<ConfirmationTimeHeightAnchor>, Error>
95
    where
96
        I::IntoIter: ExactSizeIterator;
97

98
    /// Fetch transactions and [`ConfirmationTimeHeightAnchor`]s that contain and spend the provided
99
    /// `outpoints`.
100
    ///
101
    /// `parallel_requests` specifies the maximum number of HTTP requests to make in parallel.
102
    ///
103
    /// Refer to [crate-level docs](crate) for more.
104
    fn fetch_txs_with_outpoints<I: IntoIterator<Item = OutPoint>>(
105
        &self,
106
        outpoints: I,
107
        parallel_requests: usize,
108
    ) -> Result<TxGraph<ConfirmationTimeHeightAnchor>, Error>
109
    where
110
        I::IntoIter: ExactSizeIterator;
111
}
112

113
impl EsploraExt for esplora_client::BlockingClient {
114
    fn full_scan<K: Ord + Clone>(
4✔
115
        &self,
4✔
116
        request: FullScanRequest<K>,
4✔
117
        stop_gap: usize,
4✔
118
        parallel_requests: usize,
4✔
119
    ) -> Result<FullScanResult<K>, Error> {
4✔
120
        let latest_blocks = fetch_latest_blocks(self)?;
4✔
121
        let mut graph_update = TxGraph::default();
4✔
122
        let mut last_active_indices = BTreeMap::<K, u32>::new();
4✔
123
        for (keychain, keychain_spks) in request.spks_by_keychain {
8✔
124
            let (tx_graph, last_active_index) =
4✔
125
                self.fetch_txs_with_keychain_spks(keychain_spks, stop_gap, parallel_requests)?;
4✔
126
            let _ = graph_update.apply_update(tx_graph);
4✔
127
            if let Some(last_active_index) = last_active_index {
4✔
128
                last_active_indices.insert(keychain, last_active_index);
3✔
129
            }
3✔
130
        }
131
        let chain_update = chain_update(
4✔
132
            self,
4✔
133
            &latest_blocks,
4✔
134
            &request.chain_tip,
4✔
135
            graph_update.all_anchors(),
4✔
136
        )?;
4✔
137
        Ok(FullScanResult {
4✔
138
            chain_update,
4✔
139
            graph_update,
4✔
140
            last_active_indices,
4✔
141
        })
4✔
142
    }
4✔
143

144
    fn sync(&self, request: SyncRequest, parallel_requests: usize) -> Result<SyncResult, Error> {
2✔
145
        let latest_blocks = fetch_latest_blocks(self)?;
2✔
146
        let mut graph_update = TxGraph::default();
2✔
147
        let _ =
2✔
148
            graph_update.apply_update(self.fetch_txs_with_spks(request.spks, parallel_requests)?);
2✔
149
        let _ =
150
            graph_update.apply_update(self.fetch_txs_with_txids(request.txids, parallel_requests)?);
2✔
151
        let _ = graph_update
2✔
152
            .apply_update(self.fetch_txs_with_outpoints(request.outpoints, parallel_requests)?);
2✔
153
        let chain_update = chain_update(
2✔
154
            self,
2✔
155
            &latest_blocks,
2✔
156
            &request.chain_tip,
2✔
157
            graph_update.all_anchors(),
2✔
158
        )?;
2✔
159
        Ok(SyncResult {
2✔
160
            chain_update,
2✔
161
            graph_update,
2✔
162
        })
2✔
163
    }
2✔
164

165
    fn fetch_txs_with_keychain_spks<I: Iterator<Item = Indexed<ScriptBuf>>>(
6✔
166
        &self,
6✔
167
        mut keychain_spks: I,
6✔
168
        stop_gap: usize,
6✔
169
        parallel_requests: usize,
6✔
170
    ) -> Result<(TxGraph<ConfirmationTimeHeightAnchor>, Option<u32>), Error> {
6✔
171
        type TxsOfSpkIndex = (u32, Vec<esplora_client::Tx>);
6✔
172

6✔
173
        let mut tx_graph = TxGraph::default();
6✔
174
        let mut last_index = Option::<u32>::None;
6✔
175
        let mut last_active_index = Option::<u32>::None;
6✔
176

177
        loop {
37✔
178
            let handles = keychain_spks
37✔
179
                .by_ref()
37✔
180
                .take(parallel_requests)
37✔
181
                .map(|(spk_index, spk)| {
37✔
182
                    std::thread::spawn({
34✔
183
                        let client = self.clone();
34✔
184
                        move || -> Result<TxsOfSpkIndex, Error> {
34✔
185
                            let mut last_seen = None;
34✔
186
                            let mut spk_txs = Vec::new();
34✔
187
                            loop {
188
                                let txs = client.scripthash_txs(&spk, last_seen)?;
34✔
189
                                let tx_count = txs.len();
34✔
190
                                last_seen = txs.last().map(|tx| tx.txid);
34✔
191
                                spk_txs.extend(txs);
34✔
192
                                if tx_count < 25 {
34✔
193
                                    break Ok((spk_index, spk_txs));
34✔
NEW
UNCOV
194
                                }
×
195
                            }
196
                        }
34✔
197
                    })
34✔
198
                })
37✔
199
                .collect::<Vec<JoinHandle<Result<TxsOfSpkIndex, Error>>>>();
37✔
200

37✔
201
            if handles.is_empty() {
37✔
202
                break;
3✔
203
            }
34✔
204

205
            for handle in handles {
68✔
206
                let (index, txs) = handle.join().expect("thread must not panic")?;
34✔
207
                last_index = Some(index);
34✔
208
                if !txs.is_empty() {
34✔
209
                    last_active_index = Some(index);
8✔
210
                }
28✔
211
                for tx in txs {
42✔
212
                    let _ = tx_graph.insert_tx(tx.to_tx());
8✔
213
                    if let Some(anchor) = anchor_from_status(&tx.status) {
8✔
214
                        let _ = tx_graph.insert_anchor(tx.txid, anchor);
8✔
215
                    }
8✔
216

217
                    let previous_outputs = tx.vin.iter().filter_map(|vin| {
8✔
218
                        let prevout = vin.prevout.as_ref()?;
8✔
219
                        Some((
8✔
220
                            OutPoint {
8✔
221
                                txid: vin.txid,
8✔
222
                                vout: vin.vout,
8✔
223
                            },
8✔
224
                            TxOut {
8✔
225
                                script_pubkey: prevout.scriptpubkey.clone(),
8✔
226
                                value: Amount::from_sat(prevout.value),
8✔
227
                            },
8✔
228
                        ))
8✔
229
                    });
8✔
230

231
                    for (outpoint, txout) in previous_outputs {
16✔
232
                        let _ = tx_graph.insert_txout(outpoint, txout);
8✔
233
                    }
8✔
234
                }
235
            }
236

237
            let last_index = last_index.expect("Must be set since handles wasn't empty.");
34✔
238
            let gap_limit_reached = if let Some(i) = last_active_index {
34✔
239
                last_index >= i.saturating_add(stop_gap as u32)
22✔
240
            } else {
241
                last_index + 1 >= stop_gap as u32
12✔
242
            };
243
            if gap_limit_reached {
34✔
244
                break;
3✔
245
            }
31✔
246
        }
247

248
        Ok((tx_graph, last_active_index))
6✔
249
    }
6✔
250

251
    fn fetch_txs_with_spks<I: IntoIterator<Item = ScriptBuf>>(
2✔
252
        &self,
2✔
253
        spks: I,
2✔
254
        parallel_requests: usize,
2✔
255
    ) -> Result<TxGraph<ConfirmationTimeHeightAnchor>, Error>
2✔
256
    where
2✔
257
        I::IntoIter: ExactSizeIterator,
2✔
258
    {
2✔
259
        self.fetch_txs_with_keychain_spks(
2✔
260
            spks.into_iter().enumerate().map(|(i, spk)| (i as u32, spk)),
4✔
261
            usize::MAX,
2✔
262
            parallel_requests,
2✔
263
        )
2✔
264
        .map(|(tx_graph, _)| tx_graph)
2✔
265
    }
2✔
266

267
    fn fetch_txs_with_txids<I: IntoIterator<Item = Txid>>(
6✔
268
        &self,
6✔
269
        txids: I,
6✔
270
        parallel_requests: usize,
6✔
271
    ) -> Result<TxGraph<ConfirmationTimeHeightAnchor>, Error>
6✔
272
    where
6✔
273
        I::IntoIter: ExactSizeIterator,
6✔
274
    {
6✔
275
        enum EsploraResp {
6✔
276
            TxStatus(TxStatus),
6✔
277
            Tx(Option<Tx>),
6✔
278
        }
6✔
279

6✔
280
        let mut tx_graph = TxGraph::default();
6✔
281
        let mut txids = txids.into_iter();
6✔
282
        loop {
6✔
283
            let handles = txids
6✔
284
                .by_ref()
6✔
285
                .take(parallel_requests)
6✔
286
                .map(|txid| {
6✔
NEW
287
                    let client = self.clone();
×
NEW
288
                    let tx_already_exists = tx_graph.get_tx(txid).is_some();
×
NEW
289
                    std::thread::spawn(move || {
×
NEW
UNCOV
290
                        if tx_already_exists {
×
NEW
291
                            client
×
NEW
292
                                .get_tx_status(&txid)
×
NEW
293
                                .map_err(Box::new)
×
NEW
294
                                .map(|s| (txid, EsploraResp::TxStatus(s)))
×
295
                        } else {
NEW
UNCOV
296
                            client
×
NEW
UNCOV
297
                                .get_tx_info(&txid)
×
NEW
298
                                .map_err(Box::new)
×
NEW
299
                                .map(|t| (txid, EsploraResp::Tx(t)))
×
300
                        }
NEW
301
                    })
×
302
                })
6✔
303
                .collect::<Vec<JoinHandle<Result<(Txid, EsploraResp), Error>>>>();
6✔
304

6✔
305
            if handles.is_empty() {
6✔
306
                break;
6✔
NEW
UNCOV
307
            }
×
308

NEW
309
            for handle in handles {
×
NEW
UNCOV
310
                let (txid, resp) = handle.join().expect("thread must not panic")?;
×
NEW
UNCOV
311
                match resp {
×
NEW
312
                    EsploraResp::TxStatus(status) => {
×
NEW
313
                        if let Some(anchor) = anchor_from_status(&status) {
×
NEW
314
                            let _ = tx_graph.insert_anchor(txid, anchor);
×
NEW
315
                        }
×
316
                    }
NEW
317
                    EsploraResp::Tx(Some(tx_info)) => {
×
NEW
318
                        let _ = tx_graph.insert_tx(tx_info.to_tx());
×
NEW
319
                        if let Some(anchor) = anchor_from_status(&tx_info.status) {
×
NEW
320
                            let _ = tx_graph.insert_anchor(txid, anchor);
×
NEW
UNCOV
321
                        }
×
322
                    }
NEW
UNCOV
323
                    _ => continue,
×
324
                }
325
            }
326
        }
327
        Ok(tx_graph)
6✔
328
    }
6✔
329

330
    fn fetch_txs_with_outpoints<I: IntoIterator<Item = OutPoint>>(
2✔
331
        &self,
2✔
332
        outpoints: I,
2✔
333
        parallel_requests: usize,
2✔
334
    ) -> Result<TxGraph<ConfirmationTimeHeightAnchor>, Error>
2✔
335
    where
2✔
336
        I::IntoIter: ExactSizeIterator,
2✔
337
    {
2✔
338
        let outpoints = outpoints.into_iter().collect::<Vec<_>>();
2✔
339

340
        // make sure txs exists in graph and tx statuses are updated
341
        // TODO: We should maintain a tx cache (like we do with Electrum).
342
        let mut tx_graph =
2✔
343
            self.fetch_txs_with_txids(outpoints.iter().map(|op| op.txid), parallel_requests)?;
2✔
344

345
        // get outpoint spend-statuses
346
        let mut outpoints = outpoints.into_iter();
2✔
347
        let mut missing_txs = Vec::<Txid>::with_capacity(outpoints.len());
2✔
348
        loop {
2✔
349
            let handles = outpoints
2✔
350
                .by_ref()
2✔
351
                .take(parallel_requests)
2✔
352
                .map(|op| {
2✔
NEW
UNCOV
353
                    let client = self.clone();
×
NEW
UNCOV
354
                    std::thread::spawn(move || {
×
NEW
UNCOV
355
                        client
×
NEW
UNCOV
356
                            .get_output_status(&op.txid, op.vout as _)
×
NEW
357
                            .map_err(Box::new)
×
NEW
358
                    })
×
359
                })
2✔
360
                .collect::<Vec<JoinHandle<Result<Option<OutputStatus>, Error>>>>();
2✔
361

2✔
362
            if handles.is_empty() {
2✔
363
                break;
2✔
NEW
364
            }
×
365

NEW
366
            for handle in handles {
×
NEW
367
                if let Some(op_status) = handle.join().expect("thread must not panic")? {
×
NEW
368
                    let spend_txid = match op_status.txid {
×
NEW
369
                        Some(txid) => txid,
×
NEW
370
                        None => continue,
×
371
                    };
NEW
UNCOV
372
                    if tx_graph.get_tx(spend_txid).is_none() {
×
NEW
373
                        missing_txs.push(spend_txid);
×
NEW
UNCOV
374
                    }
×
NEW
UNCOV
375
                    if let Some(spend_status) = op_status.status {
×
NEW
UNCOV
376
                        if let Some(spend_anchor) = anchor_from_status(&spend_status) {
×
NEW
377
                            let _ = tx_graph.insert_anchor(spend_txid, spend_anchor);
×
NEW
UNCOV
378
                        }
×
NEW
379
                    }
×
NEW
380
                }
×
381
            }
382
        }
383

384
        let _ = tx_graph.apply_update(self.fetch_txs_with_txids(missing_txs, parallel_requests)?);
2✔
385
        Ok(tx_graph)
2✔
386
    }
2✔
387
}
388

389
/// Fetch latest blocks from Esplora in an atomic call.
390
///
391
/// We want to do this before fetching transactions and anchors as we cannot fetch latest blocks AND
392
/// transactions atomically, and the checkpoint tip is used to determine last-scanned block (for
393
/// block-based chain-sources). Therefore it's better to be conservative when setting the tip (use
394
/// an earlier tip rather than a later tip) otherwise the caller may accidentally skip blocks when
395
/// alternating between chain-sources.
396
fn fetch_latest_blocks(
28✔
397
    client: &esplora_client::BlockingClient,
28✔
398
) -> Result<BTreeMap<u32, BlockHash>, Error> {
28✔
399
    Ok(client
28✔
400
        .get_blocks(None)?
28✔
401
        .into_iter()
28✔
402
        .map(|b| (b.time.height, b.id))
280✔
403
        .collect())
28✔
404
}
28✔
405

406
/// Used instead of [`esplora_client::BlockingClient::get_block_hash`].
407
///
408
/// This first checks the previously fetched `latest_blocks` before fetching from Esplora again.
409
fn fetch_block(
66✔
410
    client: &esplora_client::BlockingClient,
66✔
411
    latest_blocks: &BTreeMap<u32, BlockHash>,
66✔
412
    height: u32,
66✔
413
) -> Result<Option<BlockHash>, Error> {
66✔
414
    if let Some(&hash) = latest_blocks.get(&height) {
66✔
415
        return Ok(Some(hash));
17✔
416
    }
49✔
417

49✔
418
    // We avoid fetching blocks higher than previously fetched `latest_blocks` as the local chain
49✔
419
    // tip is used to signal for the last-synced-up-to-height.
49✔
420
    let &tip_height = latest_blocks
49✔
421
        .keys()
49✔
422
        .last()
49✔
423
        .expect("must have atleast one entry");
49✔
424
    if height > tip_height {
49✔
425
        return Ok(None);
×
426
    }
49✔
427

49✔
428
    Ok(Some(client.get_block_hash(height)?))
49✔
429
}
66✔
430

431
/// Create the [`local_chain::Update`].
432
///
433
/// We want to have a corresponding checkpoint per anchor height. However, checkpoints fetched
434
/// should not surpass `latest_blocks`.
435
fn chain_update<A: Anchor>(
28✔
436
    client: &esplora_client::BlockingClient,
28✔
437
    latest_blocks: &BTreeMap<u32, BlockHash>,
28✔
438
    local_tip: &CheckPoint,
28✔
439
    anchors: &BTreeSet<(A, Txid)>,
28✔
440
) -> Result<CheckPoint, Error> {
28✔
441
    let mut point_of_agreement = None;
28✔
442
    let mut conflicts = vec![];
28✔
443
    for local_cp in local_tip.iter() {
37✔
444
        let remote_hash = match fetch_block(client, latest_blocks, local_cp.height())? {
37✔
445
            Some(hash) => hash,
37✔
446
            None => continue,
×
447
        };
448
        if remote_hash == local_cp.hash() {
37✔
449
            point_of_agreement = Some(local_cp.clone());
28✔
450
            break;
28✔
451
        } else {
9✔
452
            // it is not strictly necessary to include all the conflicted heights (we do need the
9✔
453
            // first one) but it seems prudent to make sure the updated chain's heights are a
9✔
454
            // superset of the existing chain after update.
9✔
455
            conflicts.push(BlockId {
9✔
456
                height: local_cp.height(),
9✔
457
                hash: remote_hash,
9✔
458
            });
9✔
459
        }
9✔
460
    }
461

462
    let mut tip = point_of_agreement.expect("remote esplora should have same genesis block");
28✔
463

28✔
464
    tip = tip
28✔
465
        .extend(conflicts.into_iter().rev())
28✔
466
        .expect("evicted are in order");
28✔
467

468
    for anchor in anchors {
67✔
469
        let height = anchor.0.anchor_block().height;
39✔
470
        if tip.get(height).is_none() {
39✔
471
            let hash = match fetch_block(client, latest_blocks, height)? {
29✔
472
                Some(hash) => hash,
29✔
UNCOV
473
                None => continue,
×
474
            };
475
            tip = tip.insert(BlockId { height, hash });
29✔
476
        }
10✔
477
    }
478

479
    // insert the most recent blocks at the tip to make sure we update the tip and make the update
480
    // robust.
481
    for (&height, &hash) in latest_blocks.iter() {
280✔
482
        tip = tip.insert(BlockId { height, hash });
280✔
483
    }
280✔
484

485
    Ok(tip)
28✔
486
}
28✔
487

488
#[cfg(test)]
489
mod test {
490
    use crate::blocking_ext::{chain_update, fetch_latest_blocks};
491
    use bdk_chain::bitcoin::hashes::Hash;
492
    use bdk_chain::bitcoin::Txid;
493
    use bdk_chain::local_chain::LocalChain;
494
    use bdk_chain::BlockId;
495
    use bdk_testenv::{anyhow, bitcoincore_rpc::RpcApi, TestEnv};
496
    use esplora_client::{BlockHash, Builder};
497
    use std::collections::{BTreeMap, BTreeSet};
498
    use std::time::Duration;
499

500
    macro_rules! h {
501
        ($index:literal) => {{
502
            bdk_chain::bitcoin::hashes::Hash::hash($index.as_bytes())
503
        }};
504
    }
505

506
    macro_rules! local_chain {
507
        [ $(($height:expr, $block_hash:expr)), * ] => {{
508
            #[allow(unused_mut)]
509
            bdk_chain::local_chain::LocalChain::from_blocks([$(($height, $block_hash).into()),*].into_iter().collect())
510
                .expect("chain must have genesis block")
511
        }};
512
    }
513

514
    /// Ensure that update does not remove heights (from original), and all anchor heights are included.
515
    #[test]
516
    pub fn test_finalize_chain_update() -> anyhow::Result<()> {
1✔
517
        struct TestCase<'a> {
1✔
518
            name: &'a str,
1✔
519
            /// Initial blockchain height to start the env with.
1✔
520
            initial_env_height: u32,
1✔
521
            /// Initial checkpoint heights to start with in the local chain.
1✔
522
            initial_cps: &'a [u32],
1✔
523
            /// The final blockchain height of the env.
1✔
524
            final_env_height: u32,
1✔
525
            /// The anchors to test with: `(height, txid)`. Only the height is provided as we can fetch
1✔
526
            /// the blockhash from the env.
1✔
527
            anchors: &'a [(u32, Txid)],
1✔
528
        }
1✔
529

1✔
530
        let test_cases = [
1✔
531
            TestCase {
1✔
532
                name: "chain_extends",
1✔
533
                initial_env_height: 60,
1✔
534
                initial_cps: &[59, 60],
1✔
535
                final_env_height: 90,
1✔
536
                anchors: &[],
1✔
537
            },
1✔
538
            TestCase {
1✔
539
                name: "introduce_older_heights",
1✔
540
                initial_env_height: 50,
1✔
541
                initial_cps: &[10, 15],
1✔
542
                final_env_height: 50,
1✔
543
                anchors: &[(11, h!("A")), (14, h!("B"))],
1✔
544
            },
1✔
545
            TestCase {
1✔
546
                name: "introduce_older_heights_after_chain_extends",
1✔
547
                initial_env_height: 50,
1✔
548
                initial_cps: &[10, 15],
1✔
549
                final_env_height: 100,
1✔
550
                anchors: &[(11, h!("A")), (14, h!("B"))],
1✔
551
            },
1✔
552
        ];
1✔
553

554
        for (i, t) in test_cases.into_iter().enumerate() {
3✔
555
            println!("[{}] running test case: {}", i, t.name);
3✔
556

557
            let env = TestEnv::new()?;
3✔
558
            let base_url = format!("http://{}", &env.electrsd.esplora_url.clone().unwrap());
3✔
559
            let client = Builder::new(base_url.as_str()).build_blocking();
3✔
560

561
            // set env to `initial_env_height`
562
            if let Some(to_mine) = t
3✔
563
                .initial_env_height
3✔
564
                .checked_sub(env.make_checkpoint_tip().height())
3✔
565
            {
566
                env.mine_blocks(to_mine as _, None)?;
3✔
UNCOV
567
            }
×
568
            while client.get_height()? < t.initial_env_height {
1,292✔
569
                std::thread::sleep(Duration::from_millis(10));
1,289✔
570
            }
1,289✔
571

572
            // craft initial `local_chain`
573
            let local_chain = {
3✔
574
                let (mut chain, _) = LocalChain::from_genesis_hash(env.genesis_hash()?);
3✔
575
                // force `chain_update_blocking` to add all checkpoints in `t.initial_cps`
576
                let anchors = t
3✔
577
                    .initial_cps
3✔
578
                    .iter()
3✔
579
                    .map(|&height| -> anyhow::Result<_> {
6✔
580
                        Ok((
6✔
581
                            BlockId {
6✔
582
                                height,
6✔
583
                                hash: env.bitcoind.client.get_block_hash(height as _)?,
6✔
584
                            },
585
                            Txid::all_zeros(),
6✔
586
                        ))
587
                    })
6✔
588
                    .collect::<anyhow::Result<BTreeSet<_>>>()?;
3✔
589
                let update = chain_update(
3✔
590
                    &client,
3✔
591
                    &fetch_latest_blocks(&client)?,
3✔
592
                    &chain.tip(),
3✔
593
                    &anchors,
3✔
UNCOV
594
                )?;
×
595
                chain.apply_update(update)?;
3✔
596
                chain
3✔
597
            };
3✔
598
            println!("local chain height: {}", local_chain.tip().height());
3✔
599

600
            // extend env chain
601
            if let Some(to_mine) = t
3✔
602
                .final_env_height
3✔
603
                .checked_sub(env.make_checkpoint_tip().height())
3✔
604
            {
605
                env.mine_blocks(to_mine as _, None)?;
3✔
606
            }
×
607
            while client.get_height()? < t.final_env_height {
956✔
608
                std::thread::sleep(Duration::from_millis(10));
953✔
609
            }
953✔
610

611
            // craft update
612
            let update = {
3✔
613
                let anchors = t
3✔
614
                    .anchors
3✔
615
                    .iter()
3✔
616
                    .map(|&(height, txid)| -> anyhow::Result<_> {
4✔
617
                        Ok((
4✔
618
                            BlockId {
4✔
619
                                height,
4✔
620
                                hash: env.bitcoind.client.get_block_hash(height as _)?,
4✔
621
                            },
622
                            txid,
4✔
623
                        ))
624
                    })
4✔
625
                    .collect::<anyhow::Result<_>>()?;
3✔
626
                chain_update(
627
                    &client,
3✔
628
                    &fetch_latest_blocks(&client)?,
3✔
629
                    &local_chain.tip(),
3✔
630
                    &anchors,
3✔
UNCOV
631
                )?
×
632
            };
633

634
            // apply update
635
            let mut updated_local_chain = local_chain.clone();
3✔
636
            updated_local_chain.apply_update(update)?;
3✔
637
            println!(
3✔
638
                "updated local chain height: {}",
3✔
639
                updated_local_chain.tip().height()
3✔
640
            );
3✔
641

3✔
642
            assert!(
3✔
643
                {
3✔
644
                    let initial_heights = local_chain
3✔
645
                        .iter_checkpoints()
3✔
646
                        .map(|cp| cp.height())
37✔
647
                        .collect::<BTreeSet<_>>();
3✔
648
                    let updated_heights = updated_local_chain
3✔
649
                        .iter_checkpoints()
3✔
650
                        .map(|cp| cp.height())
61✔
651
                        .collect::<BTreeSet<_>>();
3✔
652
                    updated_heights.is_superset(&initial_heights)
3✔
653
                },
UNCOV
654
                "heights from the initial chain must all be in the updated chain",
×
655
            );
656

657
            assert!(
3✔
658
                {
3✔
659
                    let exp_anchor_heights = t
3✔
660
                        .anchors
3✔
661
                        .iter()
3✔
662
                        .map(|(h, _)| *h)
4✔
663
                        .chain(t.initial_cps.iter().copied())
3✔
664
                        .collect::<BTreeSet<_>>();
3✔
665
                    let anchor_heights = updated_local_chain
3✔
666
                        .iter_checkpoints()
3✔
667
                        .map(|cp| cp.height())
61✔
668
                        .collect::<BTreeSet<_>>();
3✔
669
                    anchor_heights.is_superset(&exp_anchor_heights)
3✔
670
                },
UNCOV
671
                "anchor heights must all be in updated chain",
×
672
            );
673
        }
674

675
        Ok(())
1✔
676
    }
1✔
677

678
    #[test]
679
    fn update_local_chain() -> anyhow::Result<()> {
1✔
680
        const TIP_HEIGHT: u32 = 50;
681

682
        let env = TestEnv::new()?;
1✔
683
        let blocks = {
1✔
684
            let bitcoind_client = &env.bitcoind.client;
1✔
685
            assert_eq!(bitcoind_client.get_block_count()?, 1);
1✔
686
            [
687
                (0, bitcoind_client.get_block_hash(0)?),
1✔
688
                (1, bitcoind_client.get_block_hash(1)?),
1✔
689
            ]
690
            .into_iter()
1✔
691
            .chain((2..).zip(env.mine_blocks((TIP_HEIGHT - 1) as usize, None)?))
1✔
692
            .collect::<BTreeMap<_, _>>()
1✔
693
        };
694
        // so new blocks can be seen by Electrs
695
        let env = env.reset_electrsd()?;
1✔
696
        let base_url = format!("http://{}", &env.electrsd.esplora_url.clone().unwrap());
1✔
697
        let client = Builder::new(base_url.as_str()).build_blocking();
1✔
698

1✔
699
        struct TestCase {
1✔
700
            name: &'static str,
1✔
701
            /// Original local chain to start off with.
1✔
702
            chain: LocalChain,
1✔
703
            /// Heights of floating anchors. [`chain_update_blocking`] will request for checkpoints
1✔
704
            /// of these heights.
1✔
705
            request_heights: &'static [u32],
1✔
706
            /// The expected local chain result (heights only).
1✔
707
            exp_update_heights: &'static [u32],
1✔
708
        }
1✔
709

1✔
710
        let test_cases = [
1✔
711
            TestCase {
1✔
712
                name: "request_later_blocks",
1✔
713
                chain: local_chain![(0, blocks[&0]), (21, blocks[&21])],
1✔
714
                request_heights: &[22, 25, 28],
1✔
715
                exp_update_heights: &[21, 22, 25, 28],
1✔
716
            },
1✔
717
            TestCase {
1✔
718
                name: "request_prev_blocks",
1✔
719
                chain: local_chain![(0, blocks[&0]), (1, blocks[&1]), (5, blocks[&5])],
1✔
720
                request_heights: &[4],
1✔
721
                exp_update_heights: &[4, 5],
1✔
722
            },
1✔
723
            TestCase {
1✔
724
                name: "request_prev_blocks_2",
1✔
725
                chain: local_chain![(0, blocks[&0]), (1, blocks[&1]), (10, blocks[&10])],
1✔
726
                request_heights: &[4, 6],
1✔
727
                exp_update_heights: &[4, 6, 10],
1✔
728
            },
1✔
729
            TestCase {
1✔
730
                name: "request_later_and_prev_blocks",
1✔
731
                chain: local_chain![(0, blocks[&0]), (7, blocks[&7]), (11, blocks[&11])],
1✔
732
                request_heights: &[8, 9, 15],
1✔
733
                exp_update_heights: &[8, 9, 11, 15],
1✔
734
            },
1✔
735
            TestCase {
1✔
736
                name: "request_tip_only",
1✔
737
                chain: local_chain![(0, blocks[&0]), (5, blocks[&5]), (49, blocks[&49])],
1✔
738
                request_heights: &[TIP_HEIGHT],
1✔
739
                exp_update_heights: &[49],
1✔
740
            },
1✔
741
            TestCase {
1✔
742
                name: "request_nothing",
1✔
743
                chain: local_chain![(0, blocks[&0]), (13, blocks[&13]), (23, blocks[&23])],
1✔
744
                request_heights: &[],
1✔
745
                exp_update_heights: &[23],
1✔
746
            },
1✔
747
            TestCase {
1✔
748
                name: "request_nothing_during_reorg",
1✔
749
                chain: local_chain![(0, blocks[&0]), (13, blocks[&13]), (23, h!("23"))],
1✔
750
                request_heights: &[],
1✔
751
                exp_update_heights: &[13, 23],
1✔
752
            },
1✔
753
            TestCase {
1✔
754
                name: "request_nothing_during_reorg_2",
1✔
755
                chain: local_chain![
1✔
756
                    (0, blocks[&0]),
1✔
757
                    (21, blocks[&21]),
1✔
758
                    (22, h!("22")),
1✔
759
                    (23, h!("23"))
1✔
760
                ],
1✔
761
                request_heights: &[],
1✔
762
                exp_update_heights: &[21, 22, 23],
1✔
763
            },
1✔
764
            TestCase {
1✔
765
                name: "request_prev_blocks_during_reorg",
1✔
766
                chain: local_chain![
1✔
767
                    (0, blocks[&0]),
1✔
768
                    (21, blocks[&21]),
1✔
769
                    (22, h!("22")),
1✔
770
                    (23, h!("23"))
1✔
771
                ],
1✔
772
                request_heights: &[17, 20],
1✔
773
                exp_update_heights: &[17, 20, 21, 22, 23],
1✔
774
            },
1✔
775
            TestCase {
1✔
776
                name: "request_later_blocks_during_reorg",
1✔
777
                chain: local_chain![
1✔
778
                    (0, blocks[&0]),
1✔
779
                    (9, blocks[&9]),
1✔
780
                    (22, h!("22")),
1✔
781
                    (23, h!("23"))
1✔
782
                ],
1✔
783
                request_heights: &[25, 27],
1✔
784
                exp_update_heights: &[9, 22, 23, 25, 27],
1✔
785
            },
1✔
786
            TestCase {
1✔
787
                name: "request_later_blocks_during_reorg_2",
1✔
788
                chain: local_chain![(0, blocks[&0]), (9, h!("9"))],
1✔
789
                request_heights: &[10],
1✔
790
                exp_update_heights: &[0, 9, 10],
1✔
791
            },
1✔
792
            TestCase {
1✔
793
                name: "request_later_and_prev_blocks_during_reorg",
1✔
794
                chain: local_chain![(0, blocks[&0]), (1, blocks[&1]), (9, h!("9"))],
1✔
795
                request_heights: &[8, 11],
1✔
796
                exp_update_heights: &[1, 8, 9, 11],
1✔
797
            },
1✔
798
        ];
1✔
799

800
        for (i, t) in test_cases.into_iter().enumerate() {
12✔
801
            println!("Case {}: {}", i, t.name);
12✔
802
            let mut chain = t.chain;
12✔
803

12✔
804
            let mock_anchors = t
12✔
805
                .request_heights
12✔
806
                .iter()
12✔
807
                .map(|&h| {
17✔
808
                    let anchor_blockhash: BlockHash = bdk_chain::bitcoin::hashes::Hash::hash(
17✔
809
                        &format!("hash_at_height_{}", h).into_bytes(),
17✔
810
                    );
17✔
811
                    let txid: Txid = bdk_chain::bitcoin::hashes::Hash::hash(
17✔
812
                        &format!("txid_at_height_{}", h).into_bytes(),
17✔
813
                    );
17✔
814
                    let anchor = BlockId {
17✔
815
                        height: h,
17✔
816
                        hash: anchor_blockhash,
17✔
817
                    };
17✔
818
                    (anchor, txid)
17✔
819
                })
17✔
820
                .collect::<BTreeSet<_>>();
12✔
821
            let chain_update = chain_update(
12✔
822
                &client,
12✔
823
                &fetch_latest_blocks(&client)?,
12✔
824
                &chain.tip(),
12✔
825
                &mock_anchors,
12✔
826
            )?;
×
827

828
            let update_blocks = chain_update
12✔
829
                .iter()
12✔
830
                .map(|cp| cp.block_id())
172✔
831
                .collect::<BTreeSet<_>>();
12✔
832

12✔
833
            let exp_update_blocks = t
12✔
834
                .exp_update_heights
12✔
835
                .iter()
12✔
836
                .map(|&height| {
37✔
837
                    let hash = blocks[&height];
37✔
838
                    BlockId { height, hash }
37✔
839
                })
37✔
840
                .chain(
12✔
841
                    // Electrs Esplora `get_block` call fetches 10 blocks which is included in the
12✔
842
                    // update
12✔
843
                    blocks
12✔
844
                        .range(TIP_HEIGHT - 9..)
12✔
845
                        .map(|(&height, &hash)| BlockId { height, hash }),
120✔
846
                )
12✔
847
                .collect::<BTreeSet<_>>();
12✔
848

12✔
849
            assert!(
12✔
850
                update_blocks.is_superset(&exp_update_blocks),
12✔
UNCOV
851
                "[{}:{}] unexpected update",
×
852
                i,
853
                t.name
854
            );
855

856
            let _ = chain
12✔
857
                .apply_update(chain_update)
12✔
858
                .unwrap_or_else(|err| panic!("[{}:{}] update failed to apply: {}", i, t.name, err));
12✔
859

860
            // all requested heights must exist in the final chain
861
            for height in t.request_heights {
29✔
862
                let exp_blockhash = blocks.get(height).expect("block must exist in bitcoind");
17✔
863
                assert_eq!(
17✔
864
                    chain.get(*height).map(|cp| cp.hash()),
17✔
865
                    Some(*exp_blockhash),
17✔
UNCOV
866
                    "[{}:{}] block {}:{} must exist in final chain",
×
867
                    i,
868
                    t.name,
869
                    height,
870
                    exp_blockhash
871
                );
872
            }
873
        }
874

875
        Ok(())
1✔
876
    }
1✔
877
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc