]> Untitled Git - bdk/commitdiff
feat(chain): add TxNode::is_evicted
authorNoah Joeris <noahjoeris@gmail.com>
Wed, 22 Jul 2026 19:22:54 +0000 (22:22 +0300)
committerNoah Joeris <noahjoeris@gmail.com>
Wed, 22 Jul 2026 19:22:54 +0000 (22:22 +0300)
crates/chain/src/tx_graph.rs
crates/chain/tests/test_tx_graph.rs

index 560217b3b65bbd64d75e30c13ce16126f2591fed..df0fb01d721f22eaed3d5778d3a8bbadfe3c131f 100644 (file)
@@ -225,6 +225,20 @@ pub struct TxNode<'a, T, A> {
     pub last_evicted: Option<u64>,
 }
 
+impl<T, A> TxNode<'_, T, A> {
+    /// Whether the transaction has been evicted from the mempool.
+    ///
+    /// Only mempool observation timestamps are considered; anchors have no effect. A transaction
+    /// is evicted when its last-evicted timestamp is at least as recent as its last-seen timestamp.
+    pub fn is_evicted(&self) -> bool {
+        match (self.last_seen, self.last_evicted) {
+            (_, None) => false,
+            (Some(last_seen), Some(last_evicted)) => last_evicted >= last_seen,
+            (None, Some(_)) => true,
+        }
+    }
+}
+
 impl<T, A> Deref for TxNode<'_, T, A> {
     type Target = T;
 
index 621bd6706775275da642234400d4c77683e5d4ed..24b7b947de44d92eb648863cb2a601362bb3e9fe 100644 (file)
@@ -1455,6 +1455,32 @@ fn tx_graph_update_conversion() {
     }
 }
 
+#[test]
+fn tx_node_is_evicted() {
+    let anchors = [block_id!(1, "A")].into();
+    let test_cases = [
+        (None, None, false),
+        (Some(10), None, false),
+        (None, Some(10), true),
+        (Some(10), Some(9), false),
+        (Some(10), Some(10), true),
+        (Some(10), Some(11), true),
+    ];
+
+    for (last_seen, last_evicted, expected) in test_cases {
+        let node = tx_graph::TxNode {
+            txid: hash!("tx"),
+            tx: (),
+            anchors: &anchors,
+            first_seen: None,
+            last_seen,
+            last_evicted,
+        };
+
+        assert_eq!(node.is_evicted(), expected);
+    }
+}
+
 #[test]
 fn test_seen_at_updates() {
     // Update both first_seen and last_seen