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

bitcoindevkit / bdk / 10196126131

01 Aug 2024 09:56AM UTC coverage: 82.085% (+0.2%) from 81.88%
10196126131

Pull #1478

github

web-flow
Merge 08194b620 into 785371e0a
Pull Request #1478: Make `bdk_esplora` more modular

472 of 649 new or added lines in 6 files covered. (72.73%)

18 existing lines in 4 files now uncovered.

11125 of 13553 relevant lines covered (82.09%)

16017.42 hits per line

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

90.08
/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::{BlockHash, OutPoint, ScriptBuf, Txid},
8
    local_chain::CheckPoint,
9
    BlockId, ConfirmationBlockTime, TxGraph,
10
};
11
use bdk_chain::{Anchor, Indexed};
12
use esplora_client::{OutputStatus, Tx, TxStatus};
13

14
use crate::{insert_anchor_from_status, insert_prevouts};
15

16
/// [`esplora_client::Error`]
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, R: Into<FullScanRequest<K>>>(
33
        &self,
34
        request: R,
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<I: 'static, R: Into<SyncRequest<I>>>(
47
        &self,
48
        request: R,
49
        parallel_requests: usize,
50
    ) -> Result<SyncResult, Error>;
51
}
52

53
impl EsploraExt for esplora_client::BlockingClient {
54
    fn full_scan<K: Ord + Clone, R: Into<FullScanRequest<K>>>(
4✔
55
        &self,
4✔
56
        request: R,
4✔
57
        stop_gap: usize,
4✔
58
        parallel_requests: usize,
4✔
59
    ) -> Result<FullScanResult<K>, Error> {
4✔
60
        let mut request = request.into();
4✔
61

4✔
62
        let chain_tip = request.chain_tip();
4✔
63
        let latest_blocks = if chain_tip.is_some() {
4✔
64
            Some(fetch_latest_blocks(self)?)
4✔
65
        } else {
NEW
66
            None
×
67
        };
68

69
        let mut graph_update = TxGraph::default();
4✔
70
        let mut last_active_indices = BTreeMap::<K, u32>::new();
4✔
71
        for keychain in request.keychains() {
4✔
72
            let keychain_spks = request.iter_spks(keychain.clone());
4✔
73
            let (tx_graph, last_active_index) =
4✔
74
                fetch_txs_with_keychain_spks(self, keychain_spks, stop_gap, parallel_requests)?;
4✔
75
            let _ = graph_update.apply_update(tx_graph);
4✔
76
            if let Some(last_active_index) = last_active_index {
4✔
77
                last_active_indices.insert(keychain, last_active_index);
3✔
78
            }
3✔
79
        }
80

81
        let chain_update = match (chain_tip, latest_blocks) {
4✔
82
            (Some(chain_tip), Some(latest_blocks)) => Some(chain_update(
4✔
83
                self,
4✔
84
                &latest_blocks,
4✔
85
                &chain_tip,
4✔
86
                graph_update.all_anchors(),
4✔
87
            )?),
4✔
NEW
88
            _ => None,
×
89
        };
90

91
        Ok(FullScanResult {
4✔
92
            chain_update,
4✔
93
            graph_update,
4✔
94
            last_active_indices,
4✔
95
        })
4✔
96
    }
4✔
97

98
    fn sync<I: 'static, R: Into<SyncRequest<I>>>(
1✔
99
        &self,
1✔
100
        request: R,
1✔
101
        parallel_requests: usize,
1✔
102
    ) -> Result<SyncResult, Error> {
1✔
103
        let mut request = request.into();
1✔
104

1✔
105
        let chain_tip = request.chain_tip();
1✔
106
        let latest_blocks = if chain_tip.is_some() {
1✔
107
            Some(fetch_latest_blocks(self)?)
1✔
108
        } else {
NEW
109
            None
×
110
        };
111

112
        let mut graph_update = TxGraph::default();
1✔
113
        let _ = graph_update.apply_update(fetch_txs_with_spks(
1✔
114
            self,
1✔
115
            request.iter_spks(),
1✔
116
            parallel_requests,
1✔
117
        )?);
1✔
118
        let _ = graph_update.apply_update(fetch_txs_with_txids(
1✔
119
            self,
1✔
120
            request.iter_txids(),
1✔
121
            parallel_requests,
1✔
122
        )?);
1✔
123
        let _ = graph_update.apply_update(fetch_txs_with_outpoints(
1✔
124
            self,
1✔
125
            request.iter_outpoints(),
1✔
126
            parallel_requests,
1✔
127
        )?);
1✔
128

129
        let chain_update = match (chain_tip, latest_blocks) {
1✔
130
            (Some(chain_tip), Some(latest_blocks)) => Some(chain_update(
1✔
131
                self,
1✔
132
                &latest_blocks,
1✔
133
                &chain_tip,
1✔
134
                graph_update.all_anchors(),
1✔
135
            )?),
1✔
NEW
136
            _ => None,
×
137
        };
138

139
        Ok(SyncResult {
1✔
140
            chain_update,
1✔
141
            graph_update,
1✔
142
        })
1✔
143
    }
1✔
144
}
145

146
/// Fetch latest blocks from Esplora in an atomic call.
147
///
148
/// We want to do this before fetching transactions and anchors as we cannot fetch latest blocks AND
149
/// transactions atomically, and the checkpoint tip is used to determine last-scanned block (for
150
/// block-based chain-sources). Therefore it's better to be conservative when setting the tip (use
151
/// an earlier tip rather than a later tip) otherwise the caller may accidentally skip blocks when
152
/// alternating between chain-sources.
153
fn fetch_latest_blocks(
28✔
154
    client: &esplora_client::BlockingClient,
28✔
155
) -> Result<BTreeMap<u32, BlockHash>, Error> {
28✔
156
    Ok(client
28✔
157
        .get_blocks(None)?
28✔
158
        .into_iter()
28✔
159
        .map(|b| (b.time.height, b.id))
280✔
160
        .collect())
28✔
161
}
28✔
162

163
/// Used instead of [`esplora_client::BlockingClient::get_block_hash`].
164
///
165
/// This first checks the previously fetched `latest_blocks` before fetching from Esplora again.
166
fn fetch_block(
66✔
167
    client: &esplora_client::BlockingClient,
66✔
168
    latest_blocks: &BTreeMap<u32, BlockHash>,
66✔
169
    height: u32,
66✔
170
) -> Result<Option<BlockHash>, Error> {
66✔
171
    if let Some(&hash) = latest_blocks.get(&height) {
66✔
172
        return Ok(Some(hash));
17✔
173
    }
49✔
174

49✔
175
    // We avoid fetching blocks higher than previously fetched `latest_blocks` as the local chain
49✔
176
    // tip is used to signal for the last-synced-up-to-height.
49✔
177
    let &tip_height = latest_blocks
49✔
178
        .keys()
49✔
179
        .last()
49✔
180
        .expect("must have atleast one entry");
49✔
181
    if height > tip_height {
49✔
182
        return Ok(None);
×
183
    }
49✔
184

49✔
185
    Ok(Some(client.get_block_hash(height)?))
49✔
186
}
66✔
187

188
/// Create the [`local_chain::Update`].
189
///
190
/// We want to have a corresponding checkpoint per anchor height. However, checkpoints fetched
191
/// should not surpass `latest_blocks`.
192
fn chain_update<A: Anchor>(
23✔
193
    client: &esplora_client::BlockingClient,
23✔
194
    latest_blocks: &BTreeMap<u32, BlockHash>,
23✔
195
    local_tip: &CheckPoint,
23✔
196
    anchors: &BTreeSet<(A, Txid)>,
23✔
197
) -> Result<CheckPoint, Error> {
23✔
198
    let mut point_of_agreement = None;
23✔
199
    let mut conflicts = vec![];
23✔
200
    for local_cp in local_tip.iter() {
32✔
201
        let remote_hash = match fetch_block(client, latest_blocks, local_cp.height())? {
32✔
202
            Some(hash) => hash,
32✔
203
            None => continue,
×
204
        };
205
        if remote_hash == local_cp.hash() {
32✔
206
            point_of_agreement = Some(local_cp.clone());
23✔
207
            break;
23✔
208
        } else {
9✔
209
            // it is not strictly necessary to include all the conflicted heights (we do need the
9✔
210
            // first one) but it seems prudent to make sure the updated chain's heights are a
9✔
211
            // superset of the existing chain after update.
9✔
212
            conflicts.push(BlockId {
9✔
213
                height: local_cp.height(),
9✔
214
                hash: remote_hash,
9✔
215
            });
9✔
216
        }
9✔
217
    }
218

219
    let mut tip = point_of_agreement.expect("remote esplora should have same genesis block");
23✔
220

23✔
221
    tip = tip
23✔
222
        .extend(conflicts.into_iter().rev())
23✔
223
        .expect("evicted are in order");
23✔
224

225
    for anchor in anchors {
56✔
226
        let height = anchor.0.anchor_block().height;
33✔
227
        if tip.get(height).is_none() {
33✔
228
            let hash = match fetch_block(client, latest_blocks, height)? {
28✔
229
                Some(hash) => hash,
28✔
230
                None => continue,
×
231
            };
232
            tip = tip.insert(BlockId { height, hash });
28✔
233
        }
5✔
234
    }
235

236
    // insert the most recent blocks at the tip to make sure we update the tip and make the update
237
    // robust.
238
    for (&height, &hash) in latest_blocks.iter() {
230✔
239
        tip = tip.insert(BlockId { height, hash });
230✔
240
    }
230✔
241

242
    Ok(tip)
23✔
243
}
23✔
244

245
fn fetch_txs_with_keychain_spks<I: Iterator<Item = Indexed<ScriptBuf>>>(
5✔
246
    client: &esplora_client::BlockingClient,
5✔
247
    mut keychain_spks: I,
5✔
248
    stop_gap: usize,
5✔
249
    parallel_requests: usize,
5✔
250
) -> Result<(TxGraph<ConfirmationBlockTime>, Option<u32>), Error> {
5✔
251
    type TxsOfSpkIndex = (u32, Vec<esplora_client::Tx>);
5✔
252

5✔
253
    let mut tx_graph = TxGraph::default();
5✔
254
    let mut last_index = Option::<u32>::None;
5✔
255
    let mut last_active_index = Option::<u32>::None;
5✔
256

257
    loop {
34✔
258
        let handles = keychain_spks
34✔
259
            .by_ref()
34✔
260
            .take(parallel_requests)
34✔
261
            .map(|(spk_index, spk)| {
34✔
262
                std::thread::spawn({
32✔
263
                    let client = client.clone();
32✔
264
                    move || -> Result<TxsOfSpkIndex, Error> {
32✔
265
                        let mut last_seen = None;
32✔
266
                        let mut spk_txs = Vec::new();
32✔
267
                        loop {
268
                            let txs = client.scripthash_txs(&spk, last_seen)?;
32✔
269
                            let tx_count = txs.len();
32✔
270
                            last_seen = txs.last().map(|tx| tx.txid);
32✔
271
                            spk_txs.extend(txs);
32✔
272
                            if tx_count < 25 {
32✔
273
                                break Ok((spk_index, spk_txs));
32✔
NEW
274
                            }
×
275
                        }
276
                    }
32✔
277
                })
32✔
278
            })
34✔
279
            .collect::<Vec<JoinHandle<Result<TxsOfSpkIndex, Error>>>>();
34✔
280

34✔
281
        if handles.is_empty() {
34✔
282
            break;
2✔
283
        }
32✔
284

285
        for handle in handles {
64✔
286
            let (index, txs) = handle.join().expect("thread must not panic")?;
32✔
287
            last_index = Some(index);
32✔
288
            if !txs.is_empty() {
32✔
289
                last_active_index = Some(index);
6✔
290
            }
26✔
291
            for tx in txs {
38✔
292
                let _ = tx_graph.insert_tx(tx.to_tx());
6✔
293
                insert_anchor_from_status(&mut tx_graph, tx.txid, tx.status);
6✔
294
                insert_prevouts(&mut tx_graph, tx.vin);
6✔
295
            }
6✔
296
        }
297

298
        let last_index = last_index.expect("Must be set since handles wasn't empty.");
32✔
299
        let gap_limit_reached = if let Some(i) = last_active_index {
32✔
300
            last_index >= i.saturating_add(stop_gap as u32)
20✔
301
        } else {
302
            last_index + 1 >= stop_gap as u32
12✔
303
        };
304
        if gap_limit_reached {
32✔
305
            break;
3✔
306
        }
29✔
307
    }
308

309
    Ok((tx_graph, last_active_index))
5✔
310
}
5✔
311

312
/// Fetch transactions and associated [`ConfirmationBlockTime`]s by scanning `spks`
313
/// against Esplora.
314
///
315
/// Unlike with [`EsploraExt::fetch_txs_with_keychain_spks`], `spks` must be *bounded* as all
316
/// contained scripts will be scanned. `parallel_requests` specifies the maximum number of HTTP
317
/// requests to make in parallel.
318
///
319
/// Refer to [crate-level docs](crate) for more.
320
fn fetch_txs_with_spks<I: IntoIterator<Item = ScriptBuf>>(
1✔
321
    client: &esplora_client::BlockingClient,
1✔
322
    spks: I,
1✔
323
    parallel_requests: usize,
1✔
324
) -> Result<TxGraph<ConfirmationBlockTime>, Error> {
1✔
325
    fetch_txs_with_keychain_spks(
1✔
326
        client,
1✔
327
        spks.into_iter().enumerate().map(|(i, spk)| (i as u32, spk)),
2✔
328
        usize::MAX,
1✔
329
        parallel_requests,
1✔
330
    )
1✔
331
    .map(|(tx_graph, _)| tx_graph)
1✔
332
}
1✔
333

334
/// Fetch transactions and associated [`ConfirmationBlockTime`]s by scanning `txids`
335
/// against Esplora.
336
///
337
/// `parallel_requests` specifies the maximum number of HTTP requests to make in parallel.
338
///
339
/// Refer to [crate-level docs](crate) for more.
340
fn fetch_txs_with_txids<I: IntoIterator<Item = Txid>>(
3✔
341
    client: &esplora_client::BlockingClient,
3✔
342
    txids: I,
3✔
343
    parallel_requests: usize,
3✔
344
) -> Result<TxGraph<ConfirmationBlockTime>, Error> {
3✔
345
    enum EsploraResp {
3✔
346
        TxStatus(TxStatus),
3✔
347
        Tx(Option<Tx>),
3✔
348
    }
3✔
349

3✔
350
    let mut tx_graph = TxGraph::default();
3✔
351
    let mut txids = txids.into_iter();
3✔
352
    loop {
3✔
353
        let handles = txids
3✔
354
            .by_ref()
3✔
355
            .take(parallel_requests)
3✔
356
            .map(|txid| {
3✔
NEW
357
                let client = client.clone();
×
NEW
358
                let tx_already_exists = tx_graph.get_tx(txid).is_some();
×
NEW
359
                std::thread::spawn(move || {
×
NEW
360
                    if tx_already_exists {
×
361
                        client
×
362
                            .get_tx_status(&txid)
×
363
                            .map_err(Box::new)
×
NEW
364
                            .map(|s| (txid, EsploraResp::TxStatus(s)))
×
365
                    } else {
NEW
366
                        client
×
NEW
367
                            .get_tx_info(&txid)
×
NEW
368
                            .map_err(Box::new)
×
NEW
369
                            .map(|t| (txid, EsploraResp::Tx(t)))
×
370
                    }
371
                })
×
372
            })
3✔
373
            .collect::<Vec<JoinHandle<Result<(Txid, EsploraResp), Error>>>>();
3✔
374

3✔
375
        if handles.is_empty() {
3✔
376
            break;
3✔
377
        }
×
378

379
        for handle in handles {
×
NEW
380
            let (txid, resp) = handle.join().expect("thread must not panic")?;
×
NEW
381
            match resp {
×
NEW
382
                EsploraResp::TxStatus(status) => {
×
NEW
383
                    insert_anchor_from_status(&mut tx_graph, txid, status);
×
NEW
384
                }
×
NEW
385
                EsploraResp::Tx(Some(tx_info)) => {
×
NEW
386
                    let _ = tx_graph.insert_tx(tx_info.to_tx());
×
NEW
387
                    insert_anchor_from_status(&mut tx_graph, txid, tx_info.status);
×
NEW
388
                    insert_prevouts(&mut tx_graph, tx_info.vin);
×
NEW
389
                }
×
NEW
390
                _ => continue,
×
391
            }
392
        }
393
    }
394
    Ok(tx_graph)
3✔
395
}
3✔
396

397
/// Fetch transactions and [`ConfirmationBlockTime`]s that contain and spend the provided
398
/// `outpoints`.
399
///
400
/// `parallel_requests` specifies the maximum number of HTTP requests to make in parallel.
401
///
402
/// Refer to [crate-level docs](crate) for more.
403
fn fetch_txs_with_outpoints<I: IntoIterator<Item = OutPoint>>(
1✔
404
    client: &esplora_client::BlockingClient,
1✔
405
    outpoints: I,
1✔
406
    parallel_requests: usize,
1✔
407
) -> Result<TxGraph<ConfirmationBlockTime>, Error> {
1✔
408
    let outpoints = outpoints.into_iter().collect::<Vec<_>>();
1✔
409

410
    // make sure txs exists in graph and tx statuses are updated
411
    // TODO: We should maintain a tx cache (like we do with Electrum).
412
    let mut tx_graph = fetch_txs_with_txids(
1✔
413
        client,
1✔
414
        outpoints.iter().map(|op| op.txid),
1✔
415
        parallel_requests,
1✔
416
    )?;
1✔
417

418
    // get outpoint spend-statuses
419
    let mut outpoints = outpoints.into_iter();
1✔
420
    let mut missing_txs = Vec::<Txid>::with_capacity(outpoints.len());
1✔
421
    loop {
1✔
422
        let handles = outpoints
1✔
423
            .by_ref()
1✔
424
            .take(parallel_requests)
1✔
425
            .map(|op| {
1✔
NEW
426
                let client = client.clone();
×
NEW
427
                std::thread::spawn(move || {
×
NEW
428
                    client
×
NEW
429
                        .get_output_status(&op.txid, op.vout as _)
×
NEW
430
                        .map_err(Box::new)
×
NEW
431
                })
×
432
            })
1✔
433
            .collect::<Vec<JoinHandle<Result<Option<OutputStatus>, Error>>>>();
1✔
434

1✔
435
        if handles.is_empty() {
1✔
436
            break;
1✔
UNCOV
437
        }
×
438

NEW
439
        for handle in handles {
×
NEW
440
            if let Some(op_status) = handle.join().expect("thread must not panic")? {
×
NEW
441
                let spend_txid = match op_status.txid {
×
NEW
442
                    Some(txid) => txid,
×
NEW
443
                    None => continue,
×
444
                };
NEW
445
                if tx_graph.get_tx(spend_txid).is_none() {
×
NEW
446
                    missing_txs.push(spend_txid);
×
NEW
447
                }
×
NEW
448
                if let Some(spend_status) = op_status.status {
×
NEW
449
                    insert_anchor_from_status(&mut tx_graph, spend_txid, spend_status);
×
450
                }
×
451
            }
×
452
        }
453
    }
454

455
    let _ = tx_graph.apply_update(fetch_txs_with_txids(
1✔
456
        client,
1✔
457
        missing_txs,
1✔
458
        parallel_requests,
1✔
459
    )?);
1✔
460
    Ok(tx_graph)
1✔
461
}
1✔
462

463
#[cfg(test)]
464
mod test {
465
    use crate::blocking_ext::{chain_update, fetch_latest_blocks};
466
    use bdk_chain::bitcoin::hashes::Hash;
467
    use bdk_chain::bitcoin::Txid;
468
    use bdk_chain::local_chain::LocalChain;
469
    use bdk_chain::BlockId;
470
    use bdk_testenv::{anyhow, bitcoincore_rpc::RpcApi, TestEnv};
471
    use esplora_client::{BlockHash, Builder};
472
    use std::collections::{BTreeMap, BTreeSet};
473
    use std::time::Duration;
474

475
    macro_rules! h {
476
        ($index:literal) => {{
477
            bdk_chain::bitcoin::hashes::Hash::hash($index.as_bytes())
478
        }};
479
    }
480

481
    macro_rules! local_chain {
482
        [ $(($height:expr, $block_hash:expr)), * ] => {{
483
            #[allow(unused_mut)]
484
            bdk_chain::local_chain::LocalChain::from_blocks([$(($height, $block_hash).into()),*].into_iter().collect())
485
                .expect("chain must have genesis block")
486
        }};
487
    }
488

489
    /// Ensure that update does not remove heights (from original), and all anchor heights are included.
490
    #[test]
491
    pub fn test_finalize_chain_update() -> anyhow::Result<()> {
1✔
492
        struct TestCase<'a> {
1✔
493
            name: &'a str,
1✔
494
            /// Initial blockchain height to start the env with.
1✔
495
            initial_env_height: u32,
1✔
496
            /// Initial checkpoint heights to start with in the local chain.
1✔
497
            initial_cps: &'a [u32],
1✔
498
            /// The final blockchain height of the env.
1✔
499
            final_env_height: u32,
1✔
500
            /// The anchors to test with: `(height, txid)`. Only the height is provided as we can fetch
1✔
501
            /// the blockhash from the env.
1✔
502
            anchors: &'a [(u32, Txid)],
1✔
503
        }
1✔
504

1✔
505
        let test_cases = [
1✔
506
            TestCase {
1✔
507
                name: "chain_extends",
1✔
508
                initial_env_height: 60,
1✔
509
                initial_cps: &[59, 60],
1✔
510
                final_env_height: 90,
1✔
511
                anchors: &[],
1✔
512
            },
1✔
513
            TestCase {
1✔
514
                name: "introduce_older_heights",
1✔
515
                initial_env_height: 50,
1✔
516
                initial_cps: &[10, 15],
1✔
517
                final_env_height: 50,
1✔
518
                anchors: &[(11, h!("A")), (14, h!("B"))],
1✔
519
            },
1✔
520
            TestCase {
1✔
521
                name: "introduce_older_heights_after_chain_extends",
1✔
522
                initial_env_height: 50,
1✔
523
                initial_cps: &[10, 15],
1✔
524
                final_env_height: 100,
1✔
525
                anchors: &[(11, h!("A")), (14, h!("B"))],
1✔
526
            },
1✔
527
        ];
1✔
528

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

532
            let env = TestEnv::new()?;
3✔
533
            let base_url = format!("http://{}", &env.electrsd.esplora_url.clone().unwrap());
3✔
534
            let client = Builder::new(base_url.as_str()).build_blocking();
3✔
535

536
            // set env to `initial_env_height`
537
            if let Some(to_mine) = t
3✔
538
                .initial_env_height
3✔
539
                .checked_sub(env.make_checkpoint_tip().height())
3✔
540
            {
541
                env.mine_blocks(to_mine as _, None)?;
3✔
542
            }
×
543
            while client.get_height()? < t.initial_env_height {
1,293✔
544
                std::thread::sleep(Duration::from_millis(10));
1,290✔
545
            }
1,290✔
546

547
            // craft initial `local_chain`
548
            let local_chain = {
3✔
549
                let (mut chain, _) = LocalChain::from_genesis_hash(env.genesis_hash()?);
3✔
550
                // force `chain_update_blocking` to add all checkpoints in `t.initial_cps`
551
                let anchors = t
3✔
552
                    .initial_cps
3✔
553
                    .iter()
3✔
554
                    .map(|&height| -> anyhow::Result<_> {
6✔
555
                        Ok((
6✔
556
                            BlockId {
6✔
557
                                height,
6✔
558
                                hash: env.bitcoind.client.get_block_hash(height as _)?,
6✔
559
                            },
560
                            Txid::all_zeros(),
6✔
561
                        ))
562
                    })
6✔
563
                    .collect::<anyhow::Result<BTreeSet<_>>>()?;
3✔
564
                let update = chain_update(
3✔
565
                    &client,
3✔
566
                    &fetch_latest_blocks(&client)?,
3✔
567
                    &chain.tip(),
3✔
568
                    &anchors,
3✔
569
                )?;
×
570
                chain.apply_update(update)?;
3✔
571
                chain
3✔
572
            };
3✔
573
            println!("local chain height: {}", local_chain.tip().height());
3✔
574

575
            // extend env chain
576
            if let Some(to_mine) = t
3✔
577
                .final_env_height
3✔
578
                .checked_sub(env.make_checkpoint_tip().height())
3✔
579
            {
580
                env.mine_blocks(to_mine as _, None)?;
3✔
581
            }
×
582
            while client.get_height()? < t.final_env_height {
948✔
583
                std::thread::sleep(Duration::from_millis(10));
945✔
584
            }
945✔
585

586
            // craft update
587
            let update = {
3✔
588
                let anchors = t
3✔
589
                    .anchors
3✔
590
                    .iter()
3✔
591
                    .map(|&(height, txid)| -> anyhow::Result<_> {
4✔
592
                        Ok((
4✔
593
                            BlockId {
4✔
594
                                height,
4✔
595
                                hash: env.bitcoind.client.get_block_hash(height as _)?,
4✔
596
                            },
597
                            txid,
4✔
598
                        ))
599
                    })
4✔
600
                    .collect::<anyhow::Result<_>>()?;
3✔
601
                chain_update(
602
                    &client,
3✔
603
                    &fetch_latest_blocks(&client)?,
3✔
604
                    &local_chain.tip(),
3✔
605
                    &anchors,
3✔
606
                )?
×
607
            };
608

609
            // apply update
610
            let mut updated_local_chain = local_chain.clone();
3✔
611
            updated_local_chain.apply_update(update)?;
3✔
612
            println!(
3✔
613
                "updated local chain height: {}",
3✔
614
                updated_local_chain.tip().height()
3✔
615
            );
3✔
616

3✔
617
            assert!(
3✔
618
                {
3✔
619
                    let initial_heights = local_chain
3✔
620
                        .iter_checkpoints()
3✔
621
                        .map(|cp| cp.height())
37✔
622
                        .collect::<BTreeSet<_>>();
3✔
623
                    let updated_heights = updated_local_chain
3✔
624
                        .iter_checkpoints()
3✔
625
                        .map(|cp| cp.height())
61✔
626
                        .collect::<BTreeSet<_>>();
3✔
627
                    updated_heights.is_superset(&initial_heights)
3✔
628
                },
629
                "heights from the initial chain must all be in the updated chain",
×
630
            );
631

632
            assert!(
3✔
633
                {
3✔
634
                    let exp_anchor_heights = t
3✔
635
                        .anchors
3✔
636
                        .iter()
3✔
637
                        .map(|(h, _)| *h)
4✔
638
                        .chain(t.initial_cps.iter().copied())
3✔
639
                        .collect::<BTreeSet<_>>();
3✔
640
                    let anchor_heights = updated_local_chain
3✔
641
                        .iter_checkpoints()
3✔
642
                        .map(|cp| cp.height())
61✔
643
                        .collect::<BTreeSet<_>>();
3✔
644
                    anchor_heights.is_superset(&exp_anchor_heights)
3✔
645
                },
646
                "anchor heights must all be in updated chain",
×
647
            );
648
        }
649

650
        Ok(())
1✔
651
    }
1✔
652

653
    #[test]
654
    fn update_local_chain() -> anyhow::Result<()> {
1✔
655
        const TIP_HEIGHT: u32 = 50;
656

657
        let env = TestEnv::new()?;
1✔
658
        let blocks = {
1✔
659
            let bitcoind_client = &env.bitcoind.client;
1✔
660
            assert_eq!(bitcoind_client.get_block_count()?, 1);
1✔
661
            [
662
                (0, bitcoind_client.get_block_hash(0)?),
1✔
663
                (1, bitcoind_client.get_block_hash(1)?),
1✔
664
            ]
665
            .into_iter()
1✔
666
            .chain((2..).zip(env.mine_blocks((TIP_HEIGHT - 1) as usize, None)?))
1✔
667
            .collect::<BTreeMap<_, _>>()
1✔
668
        };
669
        // so new blocks can be seen by Electrs
670
        let env = env.reset_electrsd()?;
1✔
671
        let base_url = format!("http://{}", &env.electrsd.esplora_url.clone().unwrap());
1✔
672
        let client = Builder::new(base_url.as_str()).build_blocking();
1✔
673

1✔
674
        struct TestCase {
1✔
675
            name: &'static str,
1✔
676
            /// Original local chain to start off with.
1✔
677
            chain: LocalChain,
1✔
678
            /// Heights of floating anchors. [`chain_update_blocking`] will request for checkpoints
1✔
679
            /// of these heights.
1✔
680
            request_heights: &'static [u32],
1✔
681
            /// The expected local chain result (heights only).
1✔
682
            exp_update_heights: &'static [u32],
1✔
683
        }
1✔
684

1✔
685
        let test_cases = [
1✔
686
            TestCase {
1✔
687
                name: "request_later_blocks",
1✔
688
                chain: local_chain![(0, blocks[&0]), (21, blocks[&21])],
1✔
689
                request_heights: &[22, 25, 28],
1✔
690
                exp_update_heights: &[21, 22, 25, 28],
1✔
691
            },
1✔
692
            TestCase {
1✔
693
                name: "request_prev_blocks",
1✔
694
                chain: local_chain![(0, blocks[&0]), (1, blocks[&1]), (5, blocks[&5])],
1✔
695
                request_heights: &[4],
1✔
696
                exp_update_heights: &[4, 5],
1✔
697
            },
1✔
698
            TestCase {
1✔
699
                name: "request_prev_blocks_2",
1✔
700
                chain: local_chain![(0, blocks[&0]), (1, blocks[&1]), (10, blocks[&10])],
1✔
701
                request_heights: &[4, 6],
1✔
702
                exp_update_heights: &[4, 6, 10],
1✔
703
            },
1✔
704
            TestCase {
1✔
705
                name: "request_later_and_prev_blocks",
1✔
706
                chain: local_chain![(0, blocks[&0]), (7, blocks[&7]), (11, blocks[&11])],
1✔
707
                request_heights: &[8, 9, 15],
1✔
708
                exp_update_heights: &[8, 9, 11, 15],
1✔
709
            },
1✔
710
            TestCase {
1✔
711
                name: "request_tip_only",
1✔
712
                chain: local_chain![(0, blocks[&0]), (5, blocks[&5]), (49, blocks[&49])],
1✔
713
                request_heights: &[TIP_HEIGHT],
1✔
714
                exp_update_heights: &[49],
1✔
715
            },
1✔
716
            TestCase {
1✔
717
                name: "request_nothing",
1✔
718
                chain: local_chain![(0, blocks[&0]), (13, blocks[&13]), (23, blocks[&23])],
1✔
719
                request_heights: &[],
1✔
720
                exp_update_heights: &[23],
1✔
721
            },
1✔
722
            TestCase {
1✔
723
                name: "request_nothing_during_reorg",
1✔
724
                chain: local_chain![(0, blocks[&0]), (13, blocks[&13]), (23, h!("23"))],
1✔
725
                request_heights: &[],
1✔
726
                exp_update_heights: &[13, 23],
1✔
727
            },
1✔
728
            TestCase {
1✔
729
                name: "request_nothing_during_reorg_2",
1✔
730
                chain: local_chain![
1✔
731
                    (0, blocks[&0]),
1✔
732
                    (21, blocks[&21]),
1✔
733
                    (22, h!("22")),
1✔
734
                    (23, h!("23"))
1✔
735
                ],
1✔
736
                request_heights: &[],
1✔
737
                exp_update_heights: &[21, 22, 23],
1✔
738
            },
1✔
739
            TestCase {
1✔
740
                name: "request_prev_blocks_during_reorg",
1✔
741
                chain: local_chain![
1✔
742
                    (0, blocks[&0]),
1✔
743
                    (21, blocks[&21]),
1✔
744
                    (22, h!("22")),
1✔
745
                    (23, h!("23"))
1✔
746
                ],
1✔
747
                request_heights: &[17, 20],
1✔
748
                exp_update_heights: &[17, 20, 21, 22, 23],
1✔
749
            },
1✔
750
            TestCase {
1✔
751
                name: "request_later_blocks_during_reorg",
1✔
752
                chain: local_chain![
1✔
753
                    (0, blocks[&0]),
1✔
754
                    (9, blocks[&9]),
1✔
755
                    (22, h!("22")),
1✔
756
                    (23, h!("23"))
1✔
757
                ],
1✔
758
                request_heights: &[25, 27],
1✔
759
                exp_update_heights: &[9, 22, 23, 25, 27],
1✔
760
            },
1✔
761
            TestCase {
1✔
762
                name: "request_later_blocks_during_reorg_2",
1✔
763
                chain: local_chain![(0, blocks[&0]), (9, h!("9"))],
1✔
764
                request_heights: &[10],
1✔
765
                exp_update_heights: &[0, 9, 10],
1✔
766
            },
1✔
767
            TestCase {
1✔
768
                name: "request_later_and_prev_blocks_during_reorg",
1✔
769
                chain: local_chain![(0, blocks[&0]), (1, blocks[&1]), (9, h!("9"))],
1✔
770
                request_heights: &[8, 11],
1✔
771
                exp_update_heights: &[1, 8, 9, 11],
1✔
772
            },
1✔
773
        ];
1✔
774

775
        for (i, t) in test_cases.into_iter().enumerate() {
12✔
776
            println!("Case {}: {}", i, t.name);
12✔
777
            let mut chain = t.chain;
12✔
778

12✔
779
            let mock_anchors = t
12✔
780
                .request_heights
12✔
781
                .iter()
12✔
782
                .map(|&h| {
17✔
783
                    let anchor_blockhash: BlockHash = bdk_chain::bitcoin::hashes::Hash::hash(
17✔
784
                        &format!("hash_at_height_{}", h).into_bytes(),
17✔
785
                    );
17✔
786
                    let txid: Txid = bdk_chain::bitcoin::hashes::Hash::hash(
17✔
787
                        &format!("txid_at_height_{}", h).into_bytes(),
17✔
788
                    );
17✔
789
                    let anchor = BlockId {
17✔
790
                        height: h,
17✔
791
                        hash: anchor_blockhash,
17✔
792
                    };
17✔
793
                    (anchor, txid)
17✔
794
                })
17✔
795
                .collect::<BTreeSet<_>>();
12✔
796
            let chain_update = chain_update(
12✔
797
                &client,
12✔
798
                &fetch_latest_blocks(&client)?,
12✔
799
                &chain.tip(),
12✔
800
                &mock_anchors,
12✔
801
            )?;
×
802

803
            let update_blocks = chain_update
12✔
804
                .iter()
12✔
805
                .map(|cp| cp.block_id())
172✔
806
                .collect::<BTreeSet<_>>();
12✔
807

12✔
808
            let exp_update_blocks = t
12✔
809
                .exp_update_heights
12✔
810
                .iter()
12✔
811
                .map(|&height| {
37✔
812
                    let hash = blocks[&height];
37✔
813
                    BlockId { height, hash }
37✔
814
                })
37✔
815
                .chain(
12✔
816
                    // Electrs Esplora `get_block` call fetches 10 blocks which is included in the
12✔
817
                    // update
12✔
818
                    blocks
12✔
819
                        .range(TIP_HEIGHT - 9..)
12✔
820
                        .map(|(&height, &hash)| BlockId { height, hash }),
120✔
821
                )
12✔
822
                .collect::<BTreeSet<_>>();
12✔
823

12✔
824
            assert!(
12✔
825
                update_blocks.is_superset(&exp_update_blocks),
12✔
826
                "[{}:{}] unexpected update",
×
827
                i,
828
                t.name
829
            );
830

831
            let _ = chain
12✔
832
                .apply_update(chain_update)
12✔
833
                .unwrap_or_else(|err| panic!("[{}:{}] update failed to apply: {}", i, t.name, err));
12✔
834

835
            // all requested heights must exist in the final chain
836
            for height in t.request_heights {
29✔
837
                let exp_blockhash = blocks.get(height).expect("block must exist in bitcoind");
17✔
838
                assert_eq!(
17✔
839
                    chain.get(*height).map(|cp| cp.hash()),
17✔
840
                    Some(*exp_blockhash),
17✔
841
                    "[{}:{}] block {}:{} must exist in final chain",
×
842
                    i,
843
                    t.name,
844
                    height,
845
                    exp_blockhash
846
                );
847
            }
848
        }
849

850
        Ok(())
1✔
851
    }
1✔
852
}
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