From 07b206aef66e98765b5ce3b34d10f83c8ab87eb8 Mon Sep 17 00:00:00 2001 From: Brandon Kite Date: Tue, 8 Nov 2022 20:40:00 -0800 Subject: [PATCH] fix bug where txs wouldn't get squeezed if coin state already existed (#767) * fix bug where txs wouldn't get squeezed if coin state already existed * Update fuel-txpool/src/containers/dependency.rs Co-authored-by: Green Baneling Co-authored-by: Green Baneling --- fuel-txpool/src/containers/dependency.rs | 17 ++++----- fuel-txpool/src/txpool/tests.rs | 48 ++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/fuel-txpool/src/containers/dependency.rs b/fuel-txpool/src/containers/dependency.rs index 761f0cbc569..faabceca1e9 100644 --- a/fuel-txpool/src/containers/dependency.rs +++ b/fuel-txpool/src/containers/dependency.rs @@ -392,17 +392,16 @@ impl Dependency { Self::check_if_coin_input_can_spend_db_coin(&coin, input)?; } - max_depth = core::cmp::max(1, max_depth); - db_coins.insert( - *utxo_id, - CoinState { - is_spend_by: Some(tx.id() as TxId), - depth: 0, - }, - ); } - + // mark this coin as spent by the current tx + db_coins.insert( + *utxo_id, + CoinState { + is_spend_by: Some(tx.id() as TxId), + depth: max_depth - 1, + }, + ); // yey we got our coin } Input::MessagePredicate { message_id, .. } diff --git a/fuel-txpool/src/txpool/tests.rs b/fuel-txpool/src/txpool/tests.rs index 7f771bc8843..0c4ff665b2f 100644 --- a/fuel-txpool/src/txpool/tests.rs +++ b/fuel-txpool/src/txpool/tests.rs @@ -525,6 +525,54 @@ async fn more_priced_tx3_removes_tx1_and_dependent_tx2() { assert_eq!(vec.removed[1].id(), tx2.id(), "Tx2 id should be removed"); } +#[tokio::test] +async fn more_priced_tx2_removes_tx1_and_more_priced_tx3_removes_tx2() { + let mut rng = StdRng::seed_from_u64(0); + let mut txpool = TxPool::new(Default::default()); + let db = MockDb::default(); + + let (_, gas_coin) = setup_coin(&mut rng, Some(&db)); + + let tx1 = Arc::new( + TransactionBuilder::script(vec![], vec![]) + .gas_price(10) + .add_input(gas_coin.clone()) + .finalize_as_transaction(), + ); + let tx2 = Arc::new( + TransactionBuilder::script(vec![], vec![]) + .gas_price(11) + .add_input(gas_coin.clone()) + .finalize_as_transaction(), + ); + let tx3 = Arc::new( + TransactionBuilder::script(vec![], vec![]) + .gas_price(12) + .add_input(gas_coin) + .finalize_as_transaction(), + ); + + txpool + .insert_inner(tx1.clone(), &db) + .await + .expect("Tx1 should be OK, got Err"); + let squeezed = txpool + .insert_inner(tx2.clone(), &db) + .await + .expect("Tx2 should be OK, got Err"); + assert_eq!(squeezed.removed.len(), 1); + let squeezed = txpool + .insert_inner(tx3.clone(), &db) + .await + .expect("Tx3 should be OK, got Err"); + assert_eq!( + squeezed.removed.len(), + 1, + "Tx2 should be removed:{:?}", + squeezed + ); +} + #[tokio::test] async fn tx_limit_hit() { let mut rng = StdRng::seed_from_u64(0);