|
1 | 1 | //! Contains the [`IndexedTxGraph`] and associated types. Refer to the |
2 | 2 | //! [`IndexedTxGraph`] documentation for more. |
3 | | -use core::fmt::Debug; |
| 3 | +use core::{ |
| 4 | + convert::Infallible, |
| 5 | + fmt::{self, Debug}, |
| 6 | + ops::RangeBounds, |
| 7 | +}; |
4 | 8 |
|
5 | 9 | use alloc::{sync::Arc, vec::Vec}; |
6 | | -use bitcoin::{Block, OutPoint, Transaction, TxOut, Txid}; |
| 10 | +use bitcoin::{Block, OutPoint, ScriptBuf, Transaction, TxOut, Txid}; |
7 | 11 |
|
8 | 12 | use crate::{ |
| 13 | + spk_txout::SpkTxOutIndex, |
9 | 14 | tx_graph::{self, TxGraph}, |
10 | | - Anchor, BlockId, Indexer, Merge, TxPosInBlock, |
| 15 | + Anchor, BlockId, ChainOracle, Indexer, Merge, TxPosInBlock, |
11 | 16 | }; |
12 | 17 |
|
13 | 18 | /// The [`IndexedTxGraph`] combines a [`TxGraph`] and an [`Indexer`] implementation. |
@@ -127,6 +132,19 @@ where |
127 | 132 | self.graph.insert_seen_at(txid, seen_at).into() |
128 | 133 | } |
129 | 134 |
|
| 135 | + /// Inserts the given `evicted_at` for `txid`. |
| 136 | + /// |
| 137 | + /// The `evicted_at` timestamp represents the last known time when the transaction was observed |
| 138 | + /// to be missing from the mempool. If `txid` was previously recorded with an earlier |
| 139 | + /// `evicted_at` value, it is updated only if the new value is greater. |
| 140 | + pub fn insert_evicted_at(&mut self, txid: Txid, evicted_at: u64) -> ChangeSet<A, I::ChangeSet> { |
| 141 | + let tx_graph = self.graph.insert_evicted_at(txid, evicted_at); |
| 142 | + ChangeSet { |
| 143 | + tx_graph, |
| 144 | + ..Default::default() |
| 145 | + } |
| 146 | + } |
| 147 | + |
130 | 148 | /// Batch insert transactions, filtering out those that are irrelevant. |
131 | 149 | /// |
132 | 150 | /// Relevancy is determined by the [`Indexer::is_tx_relevant`] implementation of `I`. Irrelevant |
@@ -301,6 +319,58 @@ where |
301 | 319 | } |
302 | 320 | } |
303 | 321 |
|
| 322 | +impl<A, X> IndexedTxGraph<A, X> |
| 323 | +where |
| 324 | + A: Anchor, |
| 325 | +{ |
| 326 | + /// List txids that are expected to exist under the given spks. |
| 327 | + /// |
| 328 | + /// This is used to fill [`SyncRequestBuilder::expected_spk_txids`](bdk_core::spk_client::SyncRequestBuilder::expected_spk_txids). |
| 329 | + /// |
| 330 | + /// The spk index range can be contrained with `range`. |
| 331 | + /// |
| 332 | + /// # Error |
| 333 | + /// |
| 334 | + /// If the [`ChainOracle`] implementation (`chain`) fails, an error will be returned with the |
| 335 | + /// returned item. |
| 336 | + /// |
| 337 | + /// If the [`ChainOracle`] is infallible, |
| 338 | + /// [`list_expected_spk_txids`](Self::list_expected_spk_txids) can be used instead. |
| 339 | + pub fn try_list_expected_spk_txids<'a, C, I>( |
| 340 | + &'a self, |
| 341 | + chain: &'a C, |
| 342 | + chain_tip: BlockId, |
| 343 | + spk_index_range: impl RangeBounds<I> + 'a, |
| 344 | + ) -> impl Iterator<Item = Result<(ScriptBuf, Txid), C::Error>> + 'a |
| 345 | + where |
| 346 | + C: ChainOracle, |
| 347 | + X: AsRef<SpkTxOutIndex<I>> + 'a, |
| 348 | + I: fmt::Debug + Clone + Ord + 'a, |
| 349 | + { |
| 350 | + self.graph |
| 351 | + .try_list_expected_spk_txids(chain, chain_tip, &self.index, spk_index_range) |
| 352 | + } |
| 353 | + |
| 354 | + /// List txids that are expected to exist under the given spks. |
| 355 | + /// |
| 356 | + /// This is the infallible version of |
| 357 | + /// [`try_list_expected_spk_txids`](Self::try_list_expected_spk_txids). |
| 358 | + pub fn list_expected_spk_txids<'a, C, I>( |
| 359 | + &'a self, |
| 360 | + chain: &'a C, |
| 361 | + chain_tip: BlockId, |
| 362 | + spk_index_range: impl RangeBounds<I> + 'a, |
| 363 | + ) -> impl Iterator<Item = (ScriptBuf, Txid)> + 'a |
| 364 | + where |
| 365 | + C: ChainOracle<Error = Infallible>, |
| 366 | + X: AsRef<SpkTxOutIndex<I>> + 'a, |
| 367 | + I: fmt::Debug + Clone + Ord + 'a, |
| 368 | + { |
| 369 | + self.try_list_expected_spk_txids(chain, chain_tip, spk_index_range) |
| 370 | + .map(|r| r.expect("infallible")) |
| 371 | + } |
| 372 | +} |
| 373 | + |
304 | 374 | impl<A, I> AsRef<TxGraph<A>> for IndexedTxGraph<A, I> { |
305 | 375 | fn as_ref(&self) -> &TxGraph<A> { |
306 | 376 | &self.graph |
|
0 commit comments