Skip to content

Commit

Permalink
fix bug where txs wouldn't get squeezed if coin state already existed (
Browse files Browse the repository at this point in the history
…#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 <XgreenX9999@gmail.com>

Co-authored-by: Green Baneling <XgreenX9999@gmail.com>
  • Loading branch information
Voxelot and xgreenx authored Nov 9, 2022
1 parent 595c40b commit 07b206a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
17 changes: 8 additions & 9 deletions fuel-txpool/src/containers/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, .. }
Expand Down
48 changes: 48 additions & 0 deletions fuel-txpool/src/txpool/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 07b206a

Please sign in to comment.