Skip to content

Commit

Permalink
1559 balance assert fix (#3238)
Browse files Browse the repository at this point in the history
* fix balance assert for 1559

* fix 2nd check

* add tests
  • Loading branch information
LukaszRozmej authored Jul 21, 2021
1 parent 8b0458f commit 5f4ee51
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Nethermind/Ethereum.VM.Test/VMTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ protected void RunTest(VirtualMachineTest test)
}
}

EvmState state = new EvmState((long)test.Execution.Gas, environment, ExecutionType.Transaction, true, false);
EvmState state = new EvmState((long)test.Execution.Gas, environment, ExecutionType.Transaction, true, 0, 0, false);

_storageProvider.Commit();
_stateProvider.Commit(Olympic.Instance);
Expand Down
42 changes: 37 additions & 5 deletions src/Nethermind/Nethermind.Evm.Test/TransactionProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using FluentAssertions;
using Nethermind.Core;
using Nethermind.Core.Attributes;
using Nethermind.Core.Eip2930;
using Nethermind.Core.Extensions;
using Nethermind.Core.Specs;
using Nethermind.Specs;
Expand Down Expand Up @@ -183,19 +184,50 @@ public void Can_handle_quick_fail_on_invalid_nonce(bool withStateDiff, bool with
[TestCase(true, false)]
[TestCase(false, true)]
[TestCase(false, false)]
public void Can_handle_quick_fail_on_not_enough_balance(bool withStateDiff, bool withTrace)
public void Can_handle_quick_fail_on_not_enough_balance_on_intrinsic_gas(bool withStateDiff, bool withTrace)
{
Transaction tx = Build.A.Transaction.SignedAndResolved(_ethereumEcdsa, TestItem.PrivateKeyA, _isEip155Enabled).WithGasLimit(100000).TestObject;
tx.Value = 2.Ether();
AccessListBuilder accessListBuilder = new();
foreach (Address address in TestItem.Addresses)
{
accessListBuilder.AddAddress(address);
}

Transaction tx = Build.A.Transaction
.WithGasLimit(GasCostOf.Transaction * 2)
.WithAccessList(accessListBuilder.ToAccessList())
.SignedAndResolved(_ethereumEcdsa, TestItem.PrivateKeyA, _isEip155Enabled)
.TestObject;

Block block = Build.A.Block.WithNumber(1).WithTransactions(tx).TestObject;
tx.Value = 1.Ether() - 3 * GasCostOf.Transaction;

Block block = Build.A.Block.WithNumber(MainnetSpecProvider.BerlinBlockNumber).WithTransactions(tx).TestObject;

BlockReceiptsTracer tracer = BuildTracer(block, tx, withStateDiff, withTrace);
Execute(tracer, tx, block);

Assert.AreEqual(StatusCode.Failure, tracer.TxReceipts[0].StatusCode);
}

[TestCase(true, true)]
[TestCase(true, false)]
[TestCase(false, true)]
[TestCase(false, false)]
public void Can_handle_quick_fail_on_not_enough_balance_on_reserved_gas_payment(bool withStateDiff, bool withTrace)
{
Transaction tx = Build.A.Transaction
.WithGasLimit(GasCostOf.Transaction * 2)
.SignedAndResolved(_ethereumEcdsa, TestItem.PrivateKeyA, _isEip155Enabled)
.TestObject;

tx.Value = 1.Ether() - GasCostOf.Transaction;

Block block = Build.A.Block.WithNumber(MainnetSpecProvider.BerlinBlockNumber).WithTransactions(tx).TestObject;

BlockReceiptsTracer tracer = BuildTracer(block, tx, withStateDiff, withTrace);
Execute(tracer, tx, block);

Assert.AreEqual(StatusCode.Failure, tracer.TxReceipts[0].StatusCode);
}

[TestCase(true, true)]
[TestCase(true, false)]
Expand All @@ -209,7 +241,7 @@ public void Can_handle_quick_fail_when_balance_is_lower_than_fee_cap_times_gas(b
.WithType(TxType.EIP1559)
.WithGasLimit(100000).TestObject;

Block block = Build.A.Block.WithNumber(1).WithTransactions(tx).TestObject;
Block block = Build.A.Block.WithNumber(MainnetSpecProvider.LondonBlockNumber).WithTransactions(tx).TestObject;

BlockReceiptsTracer tracer = BuildTracer(block, tx, withStateDiff, withTrace);
Execute(tracer, tx, block);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,14 @@ private void Execute(Transaction transaction, BlockHeader block, ITxTracer txTra
if (notSystemTransaction)
{
UInt256 senderBalance = _stateProvider.GetBalance(caller);
if (!restore && ((ulong) intrinsicGas * gasPrice + value > senderBalance || senderReservedGasPayment > senderBalance))
if (!restore && ((ulong) intrinsicGas * gasPrice + value > senderBalance || senderReservedGasPayment + value > senderBalance))
{
TraceLogInvalidTx(transaction, $"INSUFFICIENT_SENDER_BALANCE: ({caller})_BALANCE = {senderBalance}");
QuickFail(transaction, block, txTracer, eip658NotEnabled, "insufficient sender balance");
return;
}

if (!restore && transaction.IsEip1559 && !transaction.IsServiceTransaction && senderBalance < (UInt256)transaction.GasLimit * transaction.MaxFeePerGas)
if (!restore && spec.IsEip1559Enabled && !transaction.IsServiceTransaction && senderBalance < (UInt256)transaction.GasLimit * transaction.MaxFeePerGas + value)
{
TraceLogInvalidTx(transaction, $"INSUFFICIENT_MAX_FEE_PER_GAS_FOR_SENDER_BALANCE: ({caller})_BALANCE = {senderBalance}, MAX_FEE_PER_GAS: {transaction.MaxFeePerGas}");
QuickFail(transaction, block, txTracer, eip658NotEnabled, "insufficient MaxFeePerGas for sender balance");
Expand Down

0 comments on commit 5f4ee51

Please sign in to comment.