|
1 | 1 | use std::io::{self, Write};
|
2 | 2 |
|
3 | 3 | use bdk_chain::{
|
4 |
| - bitcoin::Network, |
| 4 | + bitcoin::{Network,Txid, BlockHash}, |
5 | 5 | collections::BTreeSet,
|
6 | 6 | indexed_tx_graph,
|
| 7 | + TxGraph, |
7 | 8 | spk_client::{FullScanRequest, SyncRequest},
|
8 | 9 | CanonicalizationParams, ConfirmationBlockTime, Merge,
|
9 | 10 | };
|
@@ -94,6 +95,28 @@ pub struct ScanOptions {
|
94 | 95 | pub batch_size: usize,
|
95 | 96 | }
|
96 | 97 |
|
| 98 | +/// Extension trait to expose anchors in a public-friendly way |
| 99 | +trait TxGraphExt { |
| 100 | + fn iter_anchors_ext( |
| 101 | + &self, |
| 102 | + ) -> Box<dyn Iterator<Item = ((Txid, BlockHash), ConfirmationBlockTime)> + '_>; |
| 103 | +} |
| 104 | + |
| 105 | +impl TxGraphExt for TxGraph<ConfirmationBlockTime> { |
| 106 | + fn iter_anchors_ext( |
| 107 | + &self, |
| 108 | + ) -> Box<dyn Iterator<Item = ((Txid, BlockHash), ConfirmationBlockTime)> + '_> { |
| 109 | + Box::new( |
| 110 | + self.full_txs().flat_map(move |tx_node| { |
| 111 | + tx_node.anchors.iter().map(move |anchor| { |
| 112 | + let block_id = anchor.block_id; |
| 113 | + ((tx_node.txid, block_id.hash), anchor.clone()) |
| 114 | + }) |
| 115 | + }), |
| 116 | + ) |
| 117 | + } |
| 118 | +} |
| 119 | + |
97 | 120 | fn main() -> anyhow::Result<()> {
|
98 | 121 | let example_cli::Init {
|
99 | 122 | args,
|
@@ -125,6 +148,13 @@ fn main() -> anyhow::Result<()> {
|
125 | 148 | };
|
126 | 149 |
|
127 | 150 | let client = BdkElectrumClient::new(electrum_cmd.electrum_args().client(network)?);
|
| 151 | + |
| 152 | + // Lock the graph so we can safely borrow data from it, |
| 153 | + // and extract all anchor data in a public-friendly format using the extension trait. |
| 154 | + // This avoids re-fetching known anchor confirmations from Electrum. |
| 155 | + let g = graph.lock().unwrap(); |
| 156 | + let anchors = g.graph().iter_anchors_ext(); |
| 157 | + client.populate_anchor_cache(anchors); |
128 | 158 |
|
129 | 159 | // Tell the electrum client about the txs we've already got locally so it doesn't re-download
|
130 | 160 | // them
|
|
0 commit comments