From 5c65aca914b2f12a6275d2a0e36e94908a4465a3 Mon Sep 17 00:00:00 2001 From: Wei Chen Date: Fri, 1 Sep 2023 17:58:47 +0800 Subject: [PATCH] chain(fix): conflict resolution for txs with same last_seen --- crates/chain/src/tx_graph.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/crates/chain/src/tx_graph.rs b/crates/chain/src/tx_graph.rs index 7f58f2031f..7733ff7c7f 100644 --- a/crates/chain/src/tx_graph.rs +++ b/crates/chain/src/tx_graph.rs @@ -693,6 +693,25 @@ impl TxGraph { if conflicting_tx.last_seen_unconfirmed > *last_seen { return Ok(None); } + if conflicting_tx.last_seen_unconfirmed == *last_seen { + // Check if conflicting tx has higher absolute fee and fee rate + if let Ok(fee) = self.calculate_fee(tx) { + if let Ok(conflicting_fee) = self.calculate_fee(&conflicting_tx) { + let fee_rate = fee as f32 / tx.weight().to_vbytes_ceil() as f32; + let conflicting_fee_rate = conflicting_fee as f32 + / conflicting_tx.weight().to_vbytes_ceil() as f32; + + if conflicting_fee > fee && conflicting_fee_rate > fee_rate { + return Ok(None); + } + } + } + // If fee rates cannot be distinguished, then conflicting tx has priority if txid of + // conflicting tx > txid of original tx + else if conflicting_tx.txid() > tx.txid() { + return Ok(None); + } + } } Ok(Some(ChainPosition::Unconfirmed(*last_seen)))