File tree Expand file tree Collapse file tree 5 files changed +48
-10
lines changed
examples/example_bitcoind_rpc_polling/src Expand file tree Collapse file tree 5 files changed +48
-10
lines changed Original file line number Diff line number Diff line change @@ -236,6 +236,14 @@ pub struct MempoolEvent {
236
236
pub latest_update_time : u64 ,
237
237
}
238
238
239
+ impl MempoolEvent {
240
+ /// Returns an iterator of `(txid, evicted_at)` pairs for all evicted transactions.
241
+ pub fn evicted_ats ( & self ) -> impl ExactSizeIterator < Item = ( Txid , u64 ) > + ' _ {
242
+ let time = self . latest_update_time ;
243
+ self . evicted_txids . iter ( ) . map ( move |& txid| ( txid, time) )
244
+ }
245
+ }
246
+
239
247
/// A newly emitted block from [`Emitter`].
240
248
#[ derive( Debug ) ]
241
249
pub struct BlockEvent < B > {
Original file line number Diff line number Diff line change @@ -847,11 +847,7 @@ fn test_expect_tx_evicted() -> anyhow::Result<()> {
847
847
assert ! ( mempool_event. evicted_txids. contains( & txid_1) ) ;
848
848
849
849
// Update graph with evicted tx.
850
- for txid in mempool_event. evicted_txids {
851
- if graph. graph ( ) . get_tx_node ( txid) . is_some ( ) {
852
- let _ = graph. insert_evicted_at ( txid, mempool_event. latest_update_time ) ;
853
- }
854
- }
850
+ let _ = graph. batch_insert_relevant_evicted_at ( mempool_event. evicted_ats ( ) ) ;
855
851
856
852
let canonical_txids = graph
857
853
. graph ( )
Original file line number Diff line number Diff line change @@ -145,6 +145,22 @@ where
145
145
}
146
146
}
147
147
148
+ /// Batch inserts `(txid, evicted_at)` pairs for `txid`s that the graph is tracking.
149
+ ///
150
+ /// The `evicted_at` timestamp represents the last known time when the transaction was observed
151
+ /// to be missing from the mempool. If `txid` was previously recorded with an earlier
152
+ /// `evicted_at` value, it is updated only if the new value is greater.
153
+ pub fn batch_insert_relevant_evicted_at (
154
+ & mut self ,
155
+ evicted_ats : impl IntoIterator < Item = ( Txid , u64 ) > ,
156
+ ) -> ChangeSet < A , I :: ChangeSet > {
157
+ let tx_graph = self . graph . batch_insert_relevant_evicted_at ( evicted_ats) ;
158
+ ChangeSet {
159
+ tx_graph,
160
+ ..Default :: default ( )
161
+ }
162
+ }
163
+
148
164
/// Batch insert transactions, filtering out those that are irrelevant.
149
165
///
150
166
/// Relevancy is determined by the [`Indexer::is_tx_relevant`] implementation of `I`. Irrelevant
Original file line number Diff line number Diff line change @@ -835,6 +835,26 @@ impl<A: Anchor> TxGraph<A> {
835
835
changeset
836
836
}
837
837
838
+ /// Batch inserts `(txid, evicted_at)` pairs into [`TxGraph`] for `txid`s that the graph is
839
+ /// tracking.
840
+ ///
841
+ /// The `evicted_at` timestamp represents the last known time when the transaction was observed
842
+ /// to be missing from the mempool. If `txid` was previously recorded with an earlier
843
+ /// `evicted_at` value, it is updated only if the new value is greater.
844
+ pub fn batch_insert_relevant_evicted_at (
845
+ & mut self ,
846
+ evicted_ats : impl IntoIterator < Item = ( Txid , u64 ) > ,
847
+ ) -> ChangeSet < A > {
848
+ let mut changeset = ChangeSet :: default ( ) ;
849
+ for ( txid, evicted_at) in evicted_ats {
850
+ // Only record evictions for transactions the graph is tracking.
851
+ if self . txs . contains_key ( & txid) {
852
+ changeset. merge ( self . insert_evicted_at ( txid, evicted_at) ) ;
853
+ }
854
+ }
855
+ changeset
856
+ }
857
+
838
858
/// Extends this graph with the given `update`.
839
859
///
840
860
/// The returned [`ChangeSet`] is the set difference between `update` and `self` (transactions that
Original file line number Diff line number Diff line change @@ -289,11 +289,9 @@ fn main() -> anyhow::Result<()> {
289
289
Emission :: Mempool ( mempool_txs) => {
290
290
let mut graph_changeset =
291
291
graph. batch_insert_relevant_unconfirmed ( mempool_txs. new_txs . clone ( ) ) ;
292
- for txid in mempool_txs. evicted_txids {
293
- graph_changeset. merge (
294
- graph. insert_evicted_at ( txid, mempool_txs. latest_update_time ) ,
295
- ) ;
296
- }
292
+ graph_changeset. merge (
293
+ graph. batch_insert_relevant_evicted_at ( mempool_txs. evicted_ats ( ) ) ,
294
+ ) ;
297
295
( local_chain:: ChangeSet :: default ( ) , graph_changeset)
298
296
}
299
297
Emission :: Tip ( h) => {
You can’t perform that action at this time.
0 commit comments