Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into refactor/tx-validator
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Nethermind/Nethermind.Optimism/InitializeBlockchainOptimism.cs
  • Loading branch information
LukaszRozmej committed Sep 9, 2024
2 parents ec0d5a2 + 268625b commit 0b5d838
Show file tree
Hide file tree
Showing 98 changed files with 641 additions and 629 deletions.
18 changes: 10 additions & 8 deletions src/Nethermind/Ethereum.Test.Base/BlockchainTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading;
using System.Threading.Tasks;
using Nethermind.Blockchain;
using Nethermind.Blockchain.BeaconBlockRoot;
using Nethermind.Blockchain.Blocks;
using Nethermind.Blockchain.Find;
using Nethermind.Blockchain.Receipts;
Expand Down Expand Up @@ -155,21 +156,22 @@ protected async Task<EthereumTestResult> RunTest(BlockchainTest test, Stopwatch?
codeInfoRepository,
_logManager);

TransactionProcessor transactionProcessor = new(
specProvider,
stateProvider,
virtualMachine,
codeInfoRepository,
_logManager);

IBlockProcessor blockProcessor = new BlockProcessor(
specProvider,
blockValidator,
rewardCalculator,
new BlockProcessor.BlockValidationTransactionsExecutor(
new TransactionProcessor(
specProvider,
stateProvider,
virtualMachine,
codeInfoRepository,
_logManager),
stateProvider),
new BlockProcessor.BlockValidationTransactionsExecutor(transactionProcessor, stateProvider),
stateProvider,
receiptStorage,
new BlockhashStore(specProvider, stateProvider),
new BeaconBlockRootHandler(transactionProcessor),
_logManager);

IBlockchainProcessor blockchainProcessor = new BlockchainProcessor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using FluentAssertions;
using Nethermind.Blockchain;
using Nethermind.Blockchain.BeaconBlockRoot;
using Nethermind.Blockchain.Receipts;
using Nethermind.Blockchain.Test.Validators;
using Nethermind.Consensus.AuRa;
Expand Down Expand Up @@ -157,11 +158,11 @@ void Process(AuRaBlockProcessor auRaBlockProcessor, int blockNumber, Hash256 sta
new BlockProcessor.BlockValidationTransactionsExecutor(transactionProcessor, stateProvider),
stateProvider,
NullReceiptStorage.Instance,
new BeaconBlockRootHandler(transactionProcessor),
LimboLogs.Instance,
Substitute.For<IBlockTree>(),
new WithdrawalProcessor(stateProvider, LimboLogs.Instance),
null,
txFilter,
txFilter: txFilter,
contractRewriter: contractRewriter);

return (processor, stateProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using FluentAssertions;
using Nethermind.Abi;
using Nethermind.Blockchain;
using Nethermind.Blockchain.BeaconBlockRoot;
using Nethermind.Consensus;
using Nethermind.Consensus.AuRa;
using Nethermind.Consensus.AuRa.Contracts;
Expand Down Expand Up @@ -96,6 +97,7 @@ protected override BlockProcessor CreateBlockProcessor()
new BlockProcessor.BlockValidationTransactionsExecutor(TxProcessor, State),
State,
ReceiptStorage,
new BeaconBlockRootHandler(TxProcessor),
LimboLogs.Instance,
BlockTree,
NullWithdrawalProcessor.Instance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected TestContractBlockchain()
SealEngineType = Nethermind.Core.SealEngineType.AuRa;
}

public static async Task<TTest> ForTest<TTest, TTestClass>(string testSuffix = null) where TTest : TestContractBlockchain, new()
public static async Task<TTest> ForTest<TTest, TTestClass>(string? testSuffix = null) where TTest : TestContractBlockchain, new()
{
(ChainSpec ChainSpec, ISpecProvider SpecProvider) GetSpecProvider()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ protected override async Task AddBlocksOnStart()
EthereumEcdsa ecdsa = new(ChainSpec.ChainId);

await AddBlock(
SignTransactions(ecdsa, TestItem.PrivateKeyA, 0,
SignTransactions(ecdsa, TestItem.PrivateKeyA, 1,
TxPriorityContract.SetPriority(TestItem.AddressA, FnSignature2, UInt256.One),
TxPriorityContract.SetPriority(TestItem.AddressB, FnSignature, 10),
TxPriorityContract.SetPriority(TestItem.AddressB, FnSignature2, 4),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ public void order_is_correct(Func<IEnumerable<Transaction>, IEnumerable<Transact
blockTree.Head.Returns(block);
ISpecProvider specProvider = Substitute.For<ISpecProvider>();
var spec = new ReleaseSpec() { IsEip1559Enabled = false };
specProvider.GetSpec(Arg.Any<BlockHeader>()).Returns(spec);
specProvider.GetSpec(Arg.Any<ForkActivation>()).Returns(spec);
TransactionComparerProvider transactionComparerProvider = new(specProvider, blockTree);
IComparer<Transaction> defaultComparer = transactionComparerProvider.GetDefaultComparer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Nethermind.Abi;
using Nethermind.AuRa.Test.Contract;
using Nethermind.Blockchain;
using Nethermind.Blockchain.BeaconBlockRoot;
using Nethermind.Consensus.AuRa;
using Nethermind.Consensus.AuRa.Contracts;
using Nethermind.Consensus.AuRa.Transactions;
Expand Down Expand Up @@ -154,10 +155,10 @@ protected override BlockProcessor CreateBlockProcessor()
new BlockProcessor.BlockValidationTransactionsExecutor(TxProcessor, State),
State,
ReceiptStorage,
new BeaconBlockRootHandler(TxProcessor),
LimboLogs.Instance,
BlockTree,
NullWithdrawalProcessor.Instance,
null,
preWarmer: CreateBlockCachePreWarmer());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Nethermind.Abi;
using Nethermind.AuRa.Test.Contract;
using Nethermind.Blockchain;
using Nethermind.Blockchain.BeaconBlockRoot;
using Nethermind.Consensus.AuRa;
using Nethermind.Consensus.AuRa.Contracts;
using Nethermind.Consensus.AuRa.Transactions;
Expand Down Expand Up @@ -295,14 +296,12 @@ protected override BlockProcessor CreateBlockProcessor()
new BlockProcessor.BlockValidationTransactionsExecutor(TxProcessor, State),
State,
ReceiptStorage,
new BeaconBlockRootHandler(TxProcessor),
LimboLogs.Instance,
BlockTree,
NullWithdrawalProcessor.Instance,
null,
PermissionBasedTxFilter,
null,
null,
CreateBlockCachePreWarmer());
txFilter: PermissionBasedTxFilter,
preWarmer: CreateBlockCachePreWarmer());
}

protected override async Task AddBlocksOnStart()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.Threading.Tasks;
using System.Threading;
using FluentAssertions;
using Nethermind.Blockchain.BeaconBlockRoot;
using Nethermind.Blockchain.Blocks;
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Rewards;
Expand Down Expand Up @@ -50,6 +51,7 @@ public void Prepared_block_contains_author_field()
stateProvider,
NullReceiptStorage.Instance,
Substitute.For<IBlockhashStore>(),
new BeaconBlockRootHandler(transactionProcessor),
LimboLogs.Instance);

BlockHeader header = Build.A.BlockHeader.WithAuthor(TestItem.AddressD).TestObject;
Expand Down Expand Up @@ -79,6 +81,7 @@ public void Recovers_state_on_cancel()
stateProvider,
NullReceiptStorage.Instance,
Substitute.For<IBlockhashStore>(),
new BeaconBlockRootHandler(transactionProcessor),
LimboLogs.Instance);

BlockHeader header = Build.A.BlockHeader.WithNumber(1).WithAuthor(TestItem.AddressD).TestObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public void SetUp()
private void SetUp(bool allowReceiptIterator)
{
var specProvider = Substitute.For<ISpecProvider>();
specProvider.GetSpec(Arg.Any<BlockHeader>()).IsEip155Enabled.Returns(true);
specProvider.GetSpec(Arg.Any<ForkActivation>()).IsEip155Enabled.Returns(true);
_receiptStorage = new InMemoryReceiptStorage(allowReceiptIterator);
_rawBlockTree = Build.A.BlockTree()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ private Block GetGenesisBlock(string chainspecPath)
TrieStore trieStore = new(stateDb, LimboLogs.Instance);
IWorldState stateProvider = new WorldState(trieStore, codeDb, LimboLogs.Instance);
ISpecProvider specProvider = Substitute.For<ISpecProvider>();
specProvider.GetSpec(Arg.Any<BlockHeader>()).Returns(Berlin.Instance);
specProvider.GetSpec(Arg.Any<ForkActivation>()).Returns(Berlin.Instance);
ITransactionProcessor transactionProcessor = Substitute.For<ITransactionProcessor>();
GenesisLoader genesisLoader = new(chainSpec, specProvider, stateProvider, transactionProcessor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Threading;
using FluentAssertions;
using Nethermind.Blockchain.BeaconBlockRoot;
using Nethermind.Blockchain.Blocks;
using Nethermind.Blockchain.Receipts;
using Nethermind.Config;
Expand Down Expand Up @@ -78,6 +79,7 @@ public void Test()
stateProvider,
NullReceiptStorage.Instance,
new BlockhashStore(specProvider, stateProvider),
new BeaconBlockRootHandler(txProcessor),
LimboLogs.Instance);
BlockchainProcessor blockchainProcessor = new(
blockTree,
Expand Down
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Blockchain.Test/ReorgTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using FluentAssertions;
using Nethermind.Blockchain.BeaconBlockRoot;
using Nethermind.Blockchain.Blocks;
using Nethermind.Blockchain.Receipts;
using Nethermind.Consensus.Comparers;
Expand Down Expand Up @@ -78,6 +79,7 @@ public void Setup()
stateProvider,
NullReceiptStorage.Instance,
new BlockhashStore(MainnetSpecProvider.Instance, stateProvider),
new BeaconBlockRootHandler(transactionProcessor),
LimboLogs.Instance);
_blockchainProcessor = new BlockchainProcessor(
_blockTree,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,8 @@ public TestingContext(bool isEip1559Enabled = false, long eip1559TransitionBlock
ReleaseSpec releaseSpec = new();
ReleaseSpec eip1559ReleaseSpec = new() { IsEip1559Enabled = isEip1559Enabled, Eip1559TransitionBlock = eip1559TransitionBlock };
ISpecProvider specProvider = Substitute.For<ISpecProvider>();
specProvider.GetSpecFor1559(Arg.Is<long>(x => x >= eip1559TransitionBlock)).Returns(eip1559ReleaseSpec);
specProvider.GetSpecFor1559(Arg.Is<long>(x => x < eip1559TransitionBlock)).Returns(releaseSpec);
specProvider.GetSpec(Arg.Is<BlockHeader>(x => x.Number >= eip1559TransitionBlock)).Returns(eip1559ReleaseSpec);
specProvider.GetSpec(Arg.Is<BlockHeader>(x => x.Number < eip1559TransitionBlock)).Returns(releaseSpec);
specProvider.GetSpec(Arg.Is<ForkActivation>(x => x.BlockNumber >= eip1559TransitionBlock)).Returns(eip1559ReleaseSpec);
specProvider.GetSpec(Arg.Is<ForkActivation>(x => x.BlockNumber < eip1559TransitionBlock)).Returns(releaseSpec);
_blockTree = Substitute.For<IBlockTree>();
UpdateBlockTreeHead();
_transactionComparerProvider =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,6 @@ void SetAccountStates(IEnumerable<Address> missingAddresses)
Block block = Build.A.Block.WithNumber(0).TestObject;
blockTree.Head.Returns(block);
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);
TransactionComparerProvider transactionComparerProvider =
new(specProvider, blockTree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,6 @@ public void Proper_transactions_selected(TransactionSelectorTests.ProperTransact
ISpecProvider specProvider = Substitute.For<ISpecProvider>();

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);

ITransactionProcessor transactionProcessor = Substitute.For<ITransactionProcessor>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Core.Specs;
using System;
using Nethermind.Core;
using Nethermind.Core.Eip2930;
using Nethermind.Core.Specs;
using Nethermind.Crypto;
using Nethermind.Evm.Tracing;
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Int256;
using Nethermind.State;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;

namespace Nethermind.Consensus.BeaconBlockRoot;

public class BeaconBlockRootHandler : IBeaconBlockRootHandler
namespace Nethermind.Blockchain.BeaconBlockRoot;
public class BeaconBlockRootHandler(ITransactionProcessor processor) : IBeaconBlockRootHandler
{
public void ApplyContractStateChanges(Block block, IReleaseSpec spec, IWorldState stateProvider)
{
if (!spec.IsBeaconBlockRootAvailable ||
block.IsGenesis ||
block.Header.ParentBeaconBlockRoot is null)
return;

Address eip4788Account = spec.Eip4788ContractAddress ?? Eip4788Constants.BeaconRootsAddress;
private const long GasLimit = 30_000_000L;

if (!stateProvider.AccountExists(eip4788Account))
return;

UInt256 timestamp = (UInt256)block.Timestamp;
Hash256 parentBeaconBlockRoot = block.ParentBeaconBlockRoot;

UInt256.Mod(timestamp, Eip4788Constants.HistoryBufferLength, out UInt256 timestampReduced);
UInt256 rootIndex = timestampReduced + Eip4788Constants.HistoryBufferLength;

StorageCell tsStorageCell = new(eip4788Account, timestampReduced);
StorageCell brStorageCell = new(eip4788Account, rootIndex);

stateProvider.Set(tsStorageCell, Bytes.WithoutLeadingZeros(timestamp.ToBigEndian()).ToArray());
stateProvider.Set(brStorageCell, Bytes.WithoutLeadingZeros(parentBeaconBlockRoot.Bytes).ToArray());
public void StoreBeaconRoot(Block block, IReleaseSpec spec)
{
BlockHeader? header = block.Header;
var canInsertBeaconRoot = spec.IsBeaconBlockRootAvailable
&& !header.IsGenesis
&& header.ParentBeaconBlockRoot is not null;

if (canInsertBeaconRoot)
{
Address beaconRootsAddress = spec.Eip4788ContractAddress ?? Eip4788Constants.BeaconRootsAddress;
Transaction transaction = new()
{
Value = UInt256.Zero,
Data = header.ParentBeaconBlockRoot.Bytes.ToArray(),
To = beaconRootsAddress,
SenderAddress = Address.SystemUser,
GasLimit = GasLimit,
GasPrice = UInt256.Zero,
AccessList = new AccessList.Builder().AddAddress(beaconRootsAddress).Build()
};

transaction.Hash = transaction.CalculateHash();

processor.Execute(transaction, header, NullTxTracer.Instance);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Core.Specs;
using Nethermind.Core;
using Nethermind.State;
using Nethermind.Core.Specs;

namespace Nethermind.Consensus.BeaconBlockRoot;
namespace Nethermind.Blockchain.BeaconBlockRoot;
public interface IBeaconBlockRootHandler
{
void ApplyContractStateChanges(Block block, IReleaseSpec spec, IWorldState state);
void StoreBeaconRoot(Block block, IReleaseSpec spec);
}
29 changes: 9 additions & 20 deletions src/Nethermind/Nethermind.Blockchain/GenesisLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,19 @@
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Specs.ChainSpecStyle;
using Nethermind.State;
using Nethermind.Consensus.BeaconBlockRoot;

namespace Nethermind.Blockchain
{
public class GenesisLoader
public class GenesisLoader(
ChainSpec chainSpec,
ISpecProvider specProvider,
IWorldState stateProvider,
ITransactionProcessor transactionProcessor)
{
private readonly ChainSpec _chainSpec;
private readonly ISpecProvider _specProvider;
private readonly IWorldState _stateProvider;
private readonly ITransactionProcessor _transactionProcessor;
private readonly BeaconBlockRootHandler _beaconBlockRootHandler;

public GenesisLoader(
ChainSpec chainSpec,
ISpecProvider specProvider,
IWorldState stateProvider,
ITransactionProcessor transactionProcessor)
{
_chainSpec = chainSpec ?? throw new ArgumentNullException(nameof(chainSpec));
_specProvider = specProvider ?? throw new ArgumentNullException(nameof(specProvider));
_stateProvider = stateProvider ?? throw new ArgumentNullException(nameof(stateProvider));
_transactionProcessor = transactionProcessor ?? throw new ArgumentNullException(nameof(transactionProcessor));
_beaconBlockRootHandler = new BeaconBlockRootHandler();
}
private readonly ChainSpec _chainSpec = chainSpec ?? throw new ArgumentNullException(nameof(chainSpec));
private readonly ISpecProvider _specProvider = specProvider ?? throw new ArgumentNullException(nameof(specProvider));
private readonly IWorldState _stateProvider = stateProvider ?? throw new ArgumentNullException(nameof(stateProvider));
private readonly ITransactionProcessor _transactionProcessor = transactionProcessor ?? throw new ArgumentNullException(nameof(transactionProcessor));

public Block Load()
{
Expand Down
Loading

0 comments on commit 0b5d838

Please sign in to comment.