Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/eip3860 reject invalid TXs from TxPool #5147

Merged
merged 5 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static IEnumerable Eip1559LegacyTransactionTestCases

ProperTransactionsSelectedTestCase balanceCheckWithTxValue = new()
{
Eip1559Enabled = true,
ReleaseSpec = London.Instance,
BaseFee = 5,
AccountStates = { { TestItem.AddressA, (300, 1) } },
Transactions =
Expand Down Expand Up @@ -111,7 +111,7 @@ public static IEnumerable Eip1559TestCases

ProperTransactionsSelectedTestCase balanceCheckWithTxValue = new()
{
Eip1559Enabled = true,
ReleaseSpec = London.Instance,
BaseFee = 5,
AccountStates = { { TestItem.AddressA, (400, 1) } },
Transactions =
Expand All @@ -129,7 +129,7 @@ public static IEnumerable Eip1559TestCases

ProperTransactionsSelectedTestCase balanceCheckWithGasPremium = new()
{
Eip1559Enabled = true,
ReleaseSpec = London.Instance,
BaseFee = 5,
AccountStates = { { TestItem.AddressA, (400, 1) } },
Transactions =
Expand Down Expand Up @@ -182,10 +182,7 @@ void SetAccountStates(IEnumerable<Address> missingAddresses)
IBlockTree blockTree = Substitute.For<IBlockTree>();
Block block = Build.A.Block.WithNumber(0).TestObject;
blockTree.Head.Returns(block);
IReleaseSpec spec = new ReleaseSpec()
{
IsEip1559Enabled = testCase.Eip1559Enabled
};
IReleaseSpec spec = testCase.ReleaseSpec;
specProvider.GetSpec(Arg.Any<long>(), Arg.Any<ulong?>()).Returns(spec);
specProvider.GetSpec(Arg.Any<BlockHeader>()).Returns(spec);
specProvider.GetSpec(Arg.Any<ForkActivation>()).Returns(spec);
Expand Down Expand Up @@ -233,7 +230,7 @@ public class ProperTransactionsSelectedTestCase
public List<Transaction> ExpectedSelectedTransactions { get; } = new();
public UInt256 MinGasPriceForMining { get; set; } = 1;

public bool Eip1559Enabled { get; set; }
public IReleaseSpec ReleaseSpec { get; set; }

public UInt256 BaseFee { get; set; }

Expand All @@ -250,13 +247,14 @@ public class ProperTransactionsSelectedTestCase
Build.A.Transaction.WithSenderAddress(TestItem.AddressA).WithNonce(2).WithValue(10)
.WithGasPrice(10).WithGasLimit(10).SignedAndResolved(TestItem.PrivateKeyA).TestObject
},
GasLimit = 10000000
GasLimit = 10000000,
ReleaseSpec = Berlin.Instance
};

public static ProperTransactionsSelectedTestCase Eip1559DefaultLegacyTransactions =>
new()
{
Eip1559Enabled = true,
ReleaseSpec = London.Instance,
BaseFee = 1.GWei(),
AccountStates = { { TestItem.AddressA, (1000, 1) } },
Transactions =
Expand All @@ -274,7 +272,7 @@ public class ProperTransactionsSelectedTestCase
public static ProperTransactionsSelectedTestCase Eip1559Default =>
new()
{
Eip1559Enabled = true,
ReleaseSpec = London.Instance,
BaseFee = 1.GWei(),
AccountStates = { { TestItem.AddressA, (1000, 1) } },
Transactions =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Producers;
using Nethermind.Core;
using Nethermind.Core.Extensions;
using Nethermind.Core.Specs;
using Nethermind.Core.Test.Builders;
using Nethermind.Db;
Expand Down Expand Up @@ -85,6 +86,7 @@ public static IEnumerable ProperTransactionsSelectedTestCases

ProperTransactionsSelectedTestCase complexCase = new()
{
ReleaseSpec = Berlin.Instance,
AccountStates =
{
{TestItem.AddressA, (1000, 1)},
Expand Down Expand Up @@ -134,7 +136,7 @@ public static IEnumerable ProperTransactionsSelectedTestCases

ProperTransactionsSelectedTestCase baseFeeBalanceCheck = new()
{
Eip1559Enabled = true,
ReleaseSpec = London.Instance,
BaseFee = 5,
AccountStates = { { TestItem.AddressA, (1000, 1) } },
Transactions =
Expand All @@ -154,7 +156,7 @@ public static IEnumerable ProperTransactionsSelectedTestCases

ProperTransactionsSelectedTestCase balanceBelowMaxFeeTimesGasLimit = new()
{
Eip1559Enabled = true,
ReleaseSpec = London.Instance,
BaseFee = 5,
AccountStates = { { TestItem.AddressA, (400, 1) } },
Transactions =
Expand All @@ -169,7 +171,7 @@ public static IEnumerable ProperTransactionsSelectedTestCases
ProperTransactionsSelectedTestCase balanceFailingWithMaxFeePerGasCheck =
new()
{
Eip1559Enabled = true,
ReleaseSpec = London.Instance,
BaseFee = 5,
AccountStates = { { TestItem.AddressA, (400, 1) } },
Transactions =
Expand All @@ -195,10 +197,7 @@ public void Proper_transactions_selected(ProperTransactionsSelectedTestCase test
IStorageProvider storageProvider = Substitute.For<IStorageProvider>();
ISpecProvider specProvider = Substitute.For<ISpecProvider>();

IReleaseSpec spec = new ReleaseSpec()
{
IsEip1559Enabled = testCase.Eip1559Enabled
};
IReleaseSpec spec = testCase.ReleaseSpec;
specProvider.GetSpec(Arg.Any<long>(), Arg.Any<ulong?>()).Returns(spec);
specProvider.GetSpec(Arg.Any<BlockHeader>()).Returns(spec);
specProvider.GetSpec(Arg.Any<ForkActivation>()).Returns(spec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Nethermind.Consensus.Validators;
using Nethermind.Core;
Expand Down Expand Up @@ -215,5 +216,29 @@ public bool MaxFeePerGas_is_required_to_be_greater_than_MaxPriorityFeePerGas(TxT
TxValidator txValidator = new(ChainId.Mainnet);
return txValidator.IsWellFormed(tx, London.Instance);
}

[TestCase(true, 1, false)]
[TestCase(false, 1, true)]
[TestCase(true, -1, true)]
[TestCase(false, -1, true)]
public void Transaction_with_init_code_above_max_value_is_rejected_when_eip3860Enabled(bool eip3860Enabled, int dataSizeAboveInitCode, bool expectedResult)
{
IReleaseSpec releaseSpec = eip3860Enabled ? Shanghai.Instance : GrayGlacier.Instance;
byte[] initCode = Enumerable.Repeat((byte)0x20, (int)releaseSpec.MaxInitCodeSize + dataSizeAboveInitCode).ToArray();
byte[] sigData = new byte[65];
sigData[31] = 1; // correct r
sigData[63] = 1; // correct s
sigData[64] = 27;
Signature signature = new(sigData);
Transaction tx = Build.A.Transaction
.WithSignature(signature)
.WithGasLimit(int.MaxValue)
.WithChainId(ChainId.Mainnet)
.To(null)
.WithData(initCode).TestObject;

TxValidator txValidator = new(1);
txValidator.IsWellFormed(tx, releaseSpec).Should().Be(expectedResult);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ public bool IsWellFormed(Transaction transaction, IReleaseSpec releaseSpec)
while for an init it will be empty */
ValidateSignature(transaction.Signature, releaseSpec) &&
ValidateChainId(transaction) &&
Validate1559GasFields(transaction, releaseSpec);
Validate1559GasFields(transaction, releaseSpec) &&
Validate3860Rules(transaction, releaseSpec);
}

private bool Validate3860Rules(Transaction transaction, IReleaseSpec releaseSpec)
{
bool aboveInitCode = transaction.IsContractCreation && releaseSpec.IsEip3860Enabled && (transaction.Data?.Length ?? 0) > releaseSpec.MaxInitCodeSize;
Copy link
Member

@LukaszRozmej LukaszRozmej Jan 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(transaction.Data?.Length ?? 0) this is in few places, can we introduce on Transaction object property:

DataLength => Data?.Length ?? 0 to avoid this check in multiple places? (There are 2 other in solution outside this PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applied changes

return !aboveInitCode;
}

private bool ValidateTxType(Transaction transaction, IReleaseSpec releaseSpec)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public TransactionBuilder<T> WithTo(Address? address)
return this;
}

public TransactionBuilder<T> To(Address address)
public TransactionBuilder<T> To(Address? address)
{
TestObjectInternal.To = address;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private void Execute(Transaction transaction, BlockHeader block, ITxTracer txTra
}
}

if (transaction.IsContractCreation && spec.IsEip3860Enabled && transaction.Data.Length > spec.MaxInitCodeSize)
if (transaction.IsContractCreation && spec.IsEip3860Enabled && (transaction.Data?.Length ?? 0) > spec.MaxInitCodeSize)
{
TraceLogInvalidTx(transaction, $"CREATE_TRANSACTION_SIZE_EXCEEDS_MAX_INIT_CODE_SIZE {transaction.Data.Length} > {spec.MaxInitCodeSize}");
QuickFail(transaction, block, txTracer, eip658NotEnabled, "eip-3860 - transaction size over max init code size");
Expand Down