Skip to content

Commit 977b4a6

Browse files
committed
feat(graph): add convenience function for inserting relevant evicted_ats
1 parent 6e1178c commit 977b4a6

5 files changed

Lines changed: 48 additions & 10 deletions

File tree

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
@@ -843,11 +843,7 @@ fn test_expect_tx_evicted() -> anyhow::Result<()> {
843843
assert!(mempool_event.evicted_txids.contains(&txid_1));
844844

845845
// Update graph with evicted tx.
846-
for txid in mempool_event.evicted_txids {
847-
if graph.graph().get_tx_node(txid).is_some() {
848-
let _ = graph.insert_evicted_at(txid, mempool_event.latest_update_time);
849-
}
850-
}
846+
let _ = graph.batch_insert_relevant_evicted_at(mempool_event.evicted_ats());
851847

852848
let canonical_txids = graph
853849
.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
@@ -777,6 +777,26 @@ impl<A: Anchor> TxGraph<A> {
777777
changeset
778778
}
779779

780+
/// Batch inserts `(txid, evicted_at)` pairs into [`TxGraph`] for `txid`s that the graph is
781+
/// tracking.
782+
///
783+
/// The `evicted_at` timestamp represents the last known time when the transaction was observed
784+
/// to be missing from the mempool. If `txid` was previously recorded with an earlier
785+
/// `evicted_at` value, it is updated only if the new value is greater.
786+
pub fn batch_insert_relevant_evicted_at(
787+
&mut self,
788+
evicted_ats: impl IntoIterator<Item = (Txid, u64)>,
789+
) -> ChangeSet<A> {
790+
let mut changeset = ChangeSet::default();
791+
for (txid, evicted_at) in evicted_ats {
792+
// Only record evictions for transactions the graph is tracking.
793+
if self.txs.contains_key(&txid) {
794+
changeset.merge(self.insert_evicted_at(txid, evicted_at));
795+
}
796+
}
797+
changeset
798+
}
799+
780800
/// Extends this graph with the given `update`.
781801
///
782802
/// 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
@@ -288,11 +288,9 @@ fn main() -> anyhow::Result<()> {
288288
Emission::Mempool(mempool_txs) => {
289289
let mut graph_changeset =
290290
graph.batch_insert_relevant_unconfirmed(mempool_txs.new_txs.clone());
291-
for txid in mempool_txs.evicted_txids {
292-
graph_changeset.merge(
293-
graph.insert_evicted_at(txid, mempool_txs.latest_update_time),
294-
);
295-
}
291+
graph_changeset.merge(
292+
graph.batch_insert_relevant_evicted_at(mempool_txs.evicted_ats()),
293+
);
296294
(local_chain::ChangeSet::default(), graph_changeset)
297295
}
298296
Emission::Tip(h) => {

0 commit comments

Comments
 (0)