Skip to content

Commit ec5262a

Browse files
committed
feat(graph): add convenience function for inserting relevant evicted_ats
1 parent 8074550 commit ec5262a

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
@@ -235,6 +235,14 @@ pub struct MempoolEvent {
235235
pub latest_update_time: u64,
236236
}
237237

238+
impl MempoolEvent {
239+
/// Returns an iterator of `(txid, evicted_at)` pairs for all evicted transactions.
240+
pub fn evicted_ats(&self) -> impl ExactSizeIterator<Item = (Txid, u64)> + '_ {
241+
let time = self.latest_update_time;
242+
self.evicted_txids.iter().map(move |&txid| (txid, time))
243+
}
244+
}
245+
238246
/// A newly emitted block from [`Emitter`].
239247
#[derive(Debug)]
240248
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
@@ -835,11 +835,7 @@ fn test_expect_tx_evicted() -> anyhow::Result<()> {
835835
assert!(mempool_event.evicted_txids.contains(&txid_1));
836836

837837
// Update graph with evicted tx.
838-
for txid in mempool_event.evicted_txids {
839-
if graph.graph().get_tx_node(txid).is_some() {
840-
let _ = graph.insert_evicted_at(txid, mempool_event.latest_update_time);
841-
}
842-
}
838+
let _ = graph.batch_insert_relevant_evicted_at(mempool_event.evicted_ats());
843839

844840
let canonical_txids = graph
845841
.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)