From 489c06c6394e1e50ca22ca77945af51cb975fafa Mon Sep 17 00:00:00 2001 From: Wei Chen Date: Thu, 12 Oct 2023 16:34:27 +0800 Subject: [PATCH] chain(test): updated test scenario for txs with same last_seen The tx conflict `Scenario` test for unconfirmed txs with the same last_seen has been amended for its corresponding conflict resolution bug fix. --- crates/chain/src/tx_graph.rs | 28 +++++++++---------- crates/chain/tests/test_tx_graph_conflicts.rs | 10 +++---- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/crates/chain/src/tx_graph.rs b/crates/chain/src/tx_graph.rs index 0a0ca7045e..e37fb1291f 100644 --- a/crates/chain/src/tx_graph.rs +++ b/crates/chain/src/tx_graph.rs @@ -789,22 +789,22 @@ impl TxGraph { if conflicting_tx.last_seen_unconfirmed > tx_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), Ok(conflicting_fee)) = - (self.calculate_fee(tx), 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 { + if conflicting_tx.last_seen_unconfirmed == *last_seen { + // Check if conflicting tx has higher absolute fee and fee rate + if let (Ok(fee), Ok(conflicting_fee)) = + (self.calculate_fee(tx), 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); + } + } else if conflicting_tx.txid() > tx.txid() { + // If fee rates cannot be distinguished, then conflicting tx has priority if + // txid of conflicting tx > txid of original tx return Ok(None); } - } else if conflicting_tx.txid() > tx.txid() { - // If fee rates cannot be distinguished, then conflicting tx has priority if - // txid of conflicting tx > txid of original tx - return Ok(None); } } } diff --git a/crates/chain/tests/test_tx_graph_conflicts.rs b/crates/chain/tests/test_tx_graph_conflicts.rs index 1794d8452f..9669b3aed2 100644 --- a/crates/chain/tests/test_tx_graph_conflicts.rs +++ b/crates/chain/tests/test_tx_graph_conflicts.rs @@ -70,14 +70,12 @@ fn test_tx_conflict_handling() { ..Default::default() }, ], - // correct output if filtered by fee rate: tx1, tx_conflict_1 - exp_chain_txs: HashSet::from(["tx1", "tx_conflict_1", "tx_conflict_2"]), - exp_chain_txouts: HashSet::from([("tx1", 0), ("tx_conflict_1", 0), ("tx_conflict_2", 0)]), - // correct output if filtered by fee rate: tx_conflict_1 - exp_unspents: HashSet::from([("tx_conflict_1", 0), ("tx_conflict_2", 0)]), + exp_chain_txs: HashSet::from(["tx1", "tx_conflict_1"]), + exp_chain_txouts: HashSet::from([("tx1", 0), ("tx_conflict_1", 0)]), + exp_unspents: HashSet::from([("tx_conflict_1", 0)]), exp_balance: Balance { immature: 0, - trusted_pending: 50000, // correct output if filtered by fee rate: 20000 + trusted_pending: 20000, untrusted_pending: 0, confirmed: 0, },