Skip to content

Commit d11f6ef

Browse files
committed
feat(graph): add convenience function for inserting relevant evicted_ats
1 parent 28ef7c9 commit d11f6ef

File tree

5 files changed

+48
-10
lines changed

5 files changed

+48
-10
lines changed

crates/bitcoind_rpc/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ pub struct MempoolEvent {
236236
pub latest_update_time: u64,
237237
}
238238

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+
239247
/// A newly emitted block from [`Emitter`].
240248
#[derive(Debug)]
241249
pub struct BlockEvent<B> {

crates/bitcoind_rpc/tests/test_emitter.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -847,11 +847,7 @@ fn test_expect_tx_evicted() -> anyhow::Result<()> {
847847
assert!(mempool_event.evicted_txids.contains(&txid_1));
848848

849849
// 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());
855851

856852
let canonical_txids = graph
857853
.graph()

crates/chain/src/indexed_tx_graph.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,22 @@ where
145145
}
146146
}
147147

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+
148164
/// Batch insert transactions, filtering out those that are irrelevant.
149165
///
150166
/// Relevancy is determined by the [`Indexer::is_tx_relevant`] implementation of `I`. Irrelevant

crates/chain/src/tx_graph.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,26 @@ impl<A: Anchor> TxGraph<A> {
835835
changeset
836836
}
837837

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+
838858
/// Extends this graph with the given `update`.
839859
///
840860
/// The returned [`ChangeSet`] is the set difference between `update` and `self` (transactions that

examples/example_bitcoind_rpc_polling/src/main.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,9 @@ fn main() -> anyhow::Result<()> {
289289
Emission::Mempool(mempool_txs) => {
290290
let mut graph_changeset =
291291
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+
);
297295
(local_chain::ChangeSet::default(), graph_changeset)
298296
}
299297
Emission::Tip(h) => {

0 commit comments

Comments
 (0)