Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marcindsobczak committed Jul 14, 2023
1 parent 63523ed commit ad68701
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
15 changes: 10 additions & 5 deletions src/Nethermind/Nethermind.TxPool.Test/TxPoolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,7 @@ public void should_not_count_txs_with_stale_nonces_when_calculating_cumulative_c
}

[Test]
public void
should_add_tx_if_cost_of_executing_all_txs_in_bucket_exceeds_balance_but_these_with_lower_nonces_doesnt()
public void should_add_tx_if_cost_of_executing_all_txs_in_bucket_exceeds_balance_but_these_with_lower_nonces_doesnt()
{
const int gasPrice = 10;
const int value = 1;
Expand All @@ -606,9 +605,15 @@ public void
}
}

_txPool.GetPendingTransactions().Length.Should().Be(8);
_txPool.SubmitTx(transactions[7], TxHandlingOptions.PersistentBroadcast);
_txPool.GetPendingTransactions().Length.Should().Be(9);
_txPool.GetPendingTransactions().Length.Should().Be(8); // nonces 0-6 and 8
_txPool.GetPendingTransactions().Last().Nonce.Should().Be(8);

_txPool.SubmitTx(transactions[8], TxHandlingOptions.PersistentBroadcast).Should().Be(AcceptTxResult.AlreadyKnown);
_txPool.SubmitTx(transactions[7], TxHandlingOptions.PersistentBroadcast).Should().Be(AcceptTxResult.Accepted);

_txPool.GetPendingTransactions().Length.Should().Be(8); // nonces 0-7 - 8 was removed because of not enough balance
_txPool.GetPendingTransactions().Last().Nonce.Should().Be(7);
_txPool.GetPendingTransactions().Should().BeEquivalentTo(transactions.SkipLast(2));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,16 @@ public AcceptTxResult Accept(Transaction tx, TxFilteringState state, TxHandlingO

if (otherTx.Nonce < tx.Nonce)
{
overflow |= UInt256.MultiplyOverflow(otherTx.MaxFeePerGas, (UInt256)otherTx.GasLimit, out UInt256 maxTxCost);
overflow |= UInt256.AddOverflow(cumulativeCost, maxTxCost, out cumulativeCost);
overflow |= UInt256.AddOverflow(cumulativeCost, otherTx.Value, out cumulativeCost);
overflow |= otherTx.IsOverflowWhenAddingTxCostToCumulative(cumulativeCost, out cumulativeCost);
}
else
{
break;
}
}

overflow |= UInt256.MultiplyOverflow(tx.MaxFeePerGas, (UInt256)tx.GasLimit, out UInt256 cost);
overflow |= UInt256.AddOverflow(cost, tx.Value, out cost);
overflow |= UInt256.AddOverflow(cost, cumulativeCost, out cumulativeCost);
overflow |= tx.IsOverflowWhenAddingTxCostToCumulative(cumulativeCost, out cumulativeCost);

if (overflow)
{
if (_logger.IsTrace)
Expand Down
14 changes: 14 additions & 0 deletions src/Nethermind/Nethermind.TxPool/TransactionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,19 @@ internal static UInt256 CalculateAffordableGasPrice(this Transaction tx, bool ei

return balance <= tx.Value ? default : tx.GasPrice;
}

internal static bool CheckForNotEnoughBalance(this Transaction tx, UInt256 currentCost, UInt256 balance, out UInt256 cumulativeCost)
=> tx.IsOverflowWhenAddingTxCostToCumulative(currentCost, out cumulativeCost) || balance < cumulativeCost;

internal static bool IsOverflowWhenAddingTxCostToCumulative(this Transaction tx, UInt256 currentCost, out UInt256 cumulativeCost)
{
bool overflow = false;

overflow |= UInt256.MultiplyOverflow(tx.MaxFeePerGas, (UInt256)tx.GasLimit, out UInt256 maxTxCost);
overflow |= UInt256.AddOverflow(currentCost, maxTxCost, out cumulativeCost);
overflow |= UInt256.AddOverflow(cumulativeCost, tx.Value, out cumulativeCost);

return overflow;
}
}
}
8 changes: 8 additions & 0 deletions src/Nethermind/Nethermind.TxPool/TxPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ private AcceptTxResult AddCore(Transaction tx, TxFilteringState state, bool isPe
{
UInt256? previousTxBottleneck = null;
int i = 0;
UInt256 cumulativeCost = 0;

foreach (Transaction tx in transactions)
{
Expand All @@ -434,6 +435,13 @@ private AcceptTxResult AddCore(Transaction tx, TxFilteringState state, bool isPe
UInt256 effectiveGasPrice =
tx.CalculateEffectiveGasPrice(_specProvider.GetCurrentHeadSpec().IsEip1559Enabled,
_headInfo.CurrentBaseFee);

if (tx.CheckForNotEnoughBalance(cumulativeCost, balance, out cumulativeCost))
{
// balance too low, remove tx from the pool
_broadcaster.StopBroadcast(tx.Hash!);
yield return (tx, null);
}
gasBottleneck = UInt256.Min(effectiveGasPrice, previousTxBottleneck ?? 0);
}

Expand Down

0 comments on commit ad68701

Please sign in to comment.