From: Noah Joeris Date: Wed, 22 Jul 2026 19:22:54 +0000 (+0300) Subject: feat(chain): add TxNode::is_evicted X-Git-Url: http://internal-gitweb-vhost/blockdata/script/encode/-script/ui-kit/value/struct.EncoderStringWriter.html?a=commitdiff_plain;h=9e404720bbc0ba462a661141c01325815aa6c180;p=bdk feat(chain): add TxNode::is_evicted --- diff --git a/crates/chain/src/tx_graph.rs b/crates/chain/src/tx_graph.rs index 560217b3..df0fb01d 100644 --- a/crates/chain/src/tx_graph.rs +++ b/crates/chain/src/tx_graph.rs @@ -225,6 +225,20 @@ pub struct TxNode<'a, T, A> { pub last_evicted: Option, } +impl 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 Deref for TxNode<'_, T, A> { type Target = T; diff --git a/crates/chain/tests/test_tx_graph.rs b/crates/chain/tests/test_tx_graph.rs index 621bd670..24b7b947 100644 --- a/crates/chain/tests/test_tx_graph.rs +++ b/crates/chain/tests/test_tx_graph.rs @@ -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