|
29 | 29 | //! for r in Emitter::new(&client, 709_632, None) { |
30 | 30 | //! let update = r.expect("todo: deal with the error properly"); |
31 | 31 | //! |
32 | | -//! if update.is_block() { |
33 | | -//! let cp = update.checkpoint(); |
34 | | -//! println!("block {}:{}", cp.height(), cp.hash()); |
35 | | -//! } else { |
36 | | -//! println!("mempool!"); |
| 32 | +//! match update.checkpoint() { |
| 33 | +//! Some(cp) => println!("block {}:{}", cp.height(), cp.hash()), |
| 34 | +//! None => println!("mempool!"), |
37 | 35 | //! } |
38 | 36 | //! } |
39 | 37 | //! ``` |
|
46 | 44 | use bdk_chain::{ |
47 | 45 | bitcoin::{Block, Transaction}, |
48 | 46 | indexed_tx_graph::Indexer, |
49 | | - local_chain::CheckPoint, |
| 47 | + local_chain::{self, CheckPoint}, |
50 | 48 | Append, BlockId, ConfirmationHeightAnchor, ConfirmationTimeAnchor, TxGraph, |
51 | 49 | }; |
52 | 50 | pub use bitcoincore_rpc; |
@@ -77,13 +75,23 @@ impl EmittedUpdate { |
77 | 75 | } |
78 | 76 |
|
79 | 77 | /// Get the emission's checkpoint. |
80 | | - pub fn checkpoint(&self) -> CheckPoint { |
| 78 | + /// |
| 79 | + /// The emission will only have a checkpoint if it is the [`EmittedUpdate::Block`] variant. |
| 80 | + pub fn checkpoint(&self) -> Option<CheckPoint> { |
81 | 81 | match self { |
82 | | - EmittedUpdate::Block(e) => e.checkpoint(), |
83 | | - EmittedUpdate::Mempool(e) => e.checkpoint(), |
| 82 | + EmittedUpdate::Block(e) => Some(e.checkpoint()), |
| 83 | + EmittedUpdate::Mempool(_) => None, |
84 | 84 | } |
85 | 85 | } |
86 | 86 |
|
| 87 | + /// Convenience method to get [`local_chain::Update`]. |
| 88 | + pub fn chain_update(&self) -> Option<local_chain::Update> { |
| 89 | + Some(local_chain::Update { |
| 90 | + tip: self.checkpoint()?, |
| 91 | + introduce_older_blocks: false, |
| 92 | + }) |
| 93 | + } |
| 94 | + |
87 | 95 | /// Transforms the emitted update into a [`TxGraph`] update. |
88 | 96 | /// |
89 | 97 | /// The `tx_filter` parameter takes in a closure that filters out irrelevant transactions so |
@@ -155,18 +163,11 @@ impl EmittedBlock { |
155 | 163 | /// An emitted subset of mempool transactions. |
156 | 164 | #[derive(Debug, Clone)] |
157 | 165 | pub struct EmittedMempool { |
158 | | - /// The checkpoint of the last-seen tip. |
159 | | - pub cp: CheckPoint, |
160 | 166 | /// Subset of mempool transactions. |
161 | 167 | pub txs: Vec<(Transaction, u64)>, |
162 | 168 | } |
163 | 169 |
|
164 | 170 | impl EmittedMempool { |
165 | | - /// Get the emission's checkpoint. |
166 | | - pub fn checkpoint(&self) -> CheckPoint { |
167 | | - self.cp.clone() |
168 | | - } |
169 | | - |
170 | 171 | /// Transforms the emitted mempool into a [`TxGraph`] update. |
171 | 172 | /// |
172 | 173 | /// The `tx_filter` parameter takes in a closure that filters out irrelevant transactions so |
@@ -295,15 +296,7 @@ impl<'c, C: RpcApi> Emitter<'c, C> { |
295 | 296 | }, |
296 | 297 | ) |
297 | 298 | .collect::<Result<Vec<_>, _>>()?; |
298 | | - let cp = match &self.last_cp { |
299 | | - Some(cp) => cp.clone(), |
300 | | - None => { |
301 | | - let hash = self.client.get_best_block_hash()?; |
302 | | - let height = self.client.get_block_info(&hash)?.height as u32; |
303 | | - CheckPoint::new(BlockId { height, hash }) |
304 | | - } |
305 | | - }; |
306 | | - Ok(EmittedMempool { cp, txs }) |
| 299 | + Ok(EmittedMempool { txs }) |
307 | 300 | } |
308 | 301 |
|
309 | 302 | /// Emits the next block (if any). |
|
0 commit comments