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

Fix BeaconBlockRootHandler #7604

Merged
merged 7 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
@@ -0,0 +1,156 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Blockchain.BeaconBlockRoot;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Eip2930;
using Nethermind.Core.Test.Builders;
using Nethermind.Crypto;
using Nethermind.Evm;
using Nethermind.Evm.Tracing;
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Int256;
using Nethermind.Specs.Forks;
using Nethermind.State;
using NSubstitute;
using NUnit.Framework;

namespace Nethermind.Blockchain.Test;

public class BeaconBlockRootHandlerTests
{
private BeaconBlockRootHandler _beaconBlockRootHandler;
private ITransactionProcessor _transactionProcessor;
private IWorldState _worldState;

[SetUp]
public void Setup()
{
_worldState = Substitute.For<IWorldState>();
_transactionProcessor = Substitute.For<ITransactionProcessor>();
_beaconBlockRootHandler = new BeaconBlockRootHandler(_transactionProcessor);
}

[Test]
public void Test_BeaconRootsAccessList_IsBeaconBlockRootAvailableFalse()
{
BlockHeader header = Build.A.BlockHeader.WithNumber(1).WithParentBeaconBlockRoot(Hash256.Zero).TestObject;
Block block = Build.A.Block.WithHeader(header).TestObject;
_worldState.AccountExists(Arg.Any<Address>()).Returns(true);

(Address? toAddress, AccessList? accessList) result = _beaconBlockRootHandler
.BeaconRootsAccessList(block, Shanghai.Instance, _worldState);

Assert.That(result.accessList, Is.Null);
Assert.That(result.toAddress, Is.Null);
}

[Test]
public void Test_BeaconRootsAccessList_HeaderIsGenesis()
{
BlockHeader header = Build.A.BlockHeader.WithParentBeaconBlockRoot(Hash256.Zero).TestObject;
Block block = Build.A.Block.WithHeader(header).TestObject;
_worldState.AccountExists(Arg.Any<Address>()).Returns(true);

(Address? toAddress, AccessList? accessList) result = _beaconBlockRootHandler
.BeaconRootsAccessList(block, Cancun.Instance, _worldState);

Assert.That(result.accessList, Is.Null);
Assert.That(result.toAddress, Is.Null);
}

[Test]
public void Test_BeaconRootsAccessList_ParentBeaconBlockRootIsNull()
{
BlockHeader header = Build.A.BlockHeader.WithNumber(1).TestObject;
Block block = Build.A.Block.WithHeader(header).TestObject;
_worldState.AccountExists(Arg.Any<Address>()).Returns(true);

(Address? toAddress, AccessList? accessList) result = _beaconBlockRootHandler
.BeaconRootsAccessList(block, Cancun.Instance, _worldState);

Assert.That(result.accessList, Is.Null);
Assert.That(result.toAddress, Is.Null);
}

[Test]
public void Test_BeaconRootsAccessList_canInsertBeaconRootIsTrue_AccountNotExist()
{
BlockHeader header = Build.A.BlockHeader.WithNumber(1).WithParentBeaconBlockRoot(Hash256.Zero).TestObject;
Block block = Build.A.Block.WithHeader(header).TestObject;
_worldState.AccountExists(Arg.Any<Address>()).Returns(false);

(Address? toAddress, AccessList? accessList) result = _beaconBlockRootHandler
.BeaconRootsAccessList(block, Cancun.Instance, _worldState);

Assert.That(result.accessList, Is.Null);
Assert.That(result.toAddress, Is.Null);
}

[Test]
public void Test_BeaconRootsAccessList_canInsertBeaconRootIsTrue_AccountExists()
{
BlockHeader header = Build.A.BlockHeader.WithNumber(1).WithParentBeaconBlockRoot(Hash256.Zero).TestObject;
Block block = Build.A.Block.WithHeader(header).TestObject;
_worldState.AccountExists(Arg.Any<Address>()).Returns(true);

(Address? toAddress, AccessList? accessList) result = _beaconBlockRootHandler
.BeaconRootsAccessList(block, Cancun.Instance, _worldState);

Assert.That(result.accessList, Is.Not.Null);
Assert.That(result.accessList.Count.AddressesCount, Is.EqualTo(1));
Assert.That(result.accessList.Count.StorageKeysCount, Is.EqualTo(1));
}

[Test]
public void Test_BeaconRootsAccessList_canInsertBeaconRootIsTrue_AccountExists_IncludeStorageCellsIsFalse()
{
BlockHeader header = Build.A.BlockHeader.WithNumber(1).WithParentBeaconBlockRoot(Hash256.Zero).TestObject;
Block block = Build.A.Block.WithHeader(header).TestObject;
_worldState.AccountExists(Arg.Any<Address>()).Returns(true);

(Address? toAddress, AccessList? accessList) result = _beaconBlockRootHandler
.BeaconRootsAccessList(block, Cancun.Instance, _worldState, false);

Assert.That(result.accessList, Is.Not.Null);
Assert.That(result.accessList.Count.AddressesCount, Is.EqualTo(1));
Assert.That(result.accessList.Count.StorageKeysCount, Is.EqualTo(0));
}

[Test]
public void Test_StoreBeaconRoot_AccessListIsNull()
{
BlockHeader header = Build.A.BlockHeader.TestObject;
Block block = Build.A.Block.WithHeader(header).TestObject;

_beaconBlockRootHandler.StoreBeaconRoot(block, Cancun.Instance, _worldState);

_transactionProcessor.DidNotReceive().Execute(Arg.Any<Transaction>(), Arg.Any<BlockExecutionContext>(), Arg.Any<ITxTracer>());
}

[Test]
public void Test_StoreBeaconRoot_AccessListNotNull()
{
BlockHeader header = Build.A.BlockHeader.WithNumber(1).WithParentBeaconBlockRoot(Hash256.Zero).TestObject;
Block block = Build.A.Block.WithHeader(header).TestObject;
_worldState.AccountExists(Arg.Any<Address>()).Returns(true);

_beaconBlockRootHandler.StoreBeaconRoot(block, Cancun.Instance, _worldState);

Transaction transaction = new()
{
Value = UInt256.Zero,
Data = header.ParentBeaconBlockRoot!.Bytes.ToArray(),
To = Eip4788Constants.BeaconRootsAddress,
SenderAddress = Address.SystemUser,
GasLimit = 30_000_000L,
GasPrice = UInt256.Zero,
AccessList = new AccessList.Builder().AddAddress(Eip4788Constants.BeaconRootsAddress).Build()
};

transaction.Hash = transaction.CalculateHash();
_transactionProcessor.Received().Execute(Arg.Is<Transaction>(t =>
t.Hash == transaction.Hash), header, NullTxTracer.Instance);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

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;

namespace Nethermind.Blockchain.BeaconBlockRoot;
public class BeaconBlockRootHandler(ITransactionProcessor processor) : IBeaconBlockRootHandler
{
private const long GasLimit = 30_000_000L;

public (Address? toAddress, AccessList? accessList) BeaconRootsAccessList(Block block, IReleaseSpec spec, bool includeStorageCells = true)
public (Address? toAddress, AccessList? accessList) BeaconRootsAccessList(Block block, IReleaseSpec spec, IWorldState stateProvider, bool includeStorageCells = true)
Copy link
Contributor

Choose a reason for hiding this comment

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

can you pass StateProvider in constructor?

{
BlockHeader? header = block.Header;
bool canInsertBeaconRoot = spec.IsBeaconBlockRootAvailable
Expand All @@ -26,7 +26,7 @@ public class BeaconBlockRootHandler(ITransactionProcessor processor) : IBeaconBl
spec.Eip4788ContractAddress ?? Eip4788Constants.BeaconRootsAddress :
null;

if (eip4788ContractAddress is null)
if (eip4788ContractAddress is null || !stateProvider.AccountExists(eip4788ContractAddress))
{
return (null, null);
}
Expand All @@ -42,9 +42,9 @@ public class BeaconBlockRootHandler(ITransactionProcessor processor) : IBeaconBl
return (eip4788ContractAddress, builder.Build());
}

public void StoreBeaconRoot(Block block, IReleaseSpec spec)
public void StoreBeaconRoot(Block block, IReleaseSpec spec, IWorldState stateProvider)
{
(Address? toAddress, AccessList? accessList) = BeaconRootsAccessList(block, spec, includeStorageCells: false);
(Address? toAddress, AccessList? accessList) = BeaconRootsAccessList(block, spec, stateProvider, includeStorageCells: false);

if (toAddress is not null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
using Nethermind.Core;
using Nethermind.Core.Eip2930;
using Nethermind.Core.Specs;
using Nethermind.State;

namespace Nethermind.Blockchain.BeaconBlockRoot;
public interface IBeaconBlockRootHandler
{
(Address? toAddress, AccessList? accessList) BeaconRootsAccessList(Block block, IReleaseSpec spec, bool includeStorageCells = true);
void StoreBeaconRoot(Block block, IReleaseSpec spec);
(Address? toAddress, AccessList? accessList) BeaconRootsAccessList(Block block, IReleaseSpec spec, IWorldState stateProvider, bool includeStorageCells = true);
void StoreBeaconRoot(Block block, IReleaseSpec spec, IWorldState stateProvider);
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ the previous head state.*/
if (!skipPrewarming)
{
using CancellationTokenSource cancellationTokenSource = new();
(_, AccessList? accessList) = _beaconBlockRootHandler.BeaconRootsAccessList(suggestedBlock, _specProvider.GetSpec(suggestedBlock.Header));
(_, AccessList? accessList) = _beaconBlockRootHandler.BeaconRootsAccessList(suggestedBlock, _specProvider.GetSpec(suggestedBlock.Header), _stateProvider);
preWarmTask = preWarmer.PreWarmCaches(suggestedBlock, preBlockStateRoot, accessList, cancellationTokenSource.Token);
(processedBlock, receipts) = ProcessOne(suggestedBlock, options, blockTracer);
// Block is processed, we can cancel the prewarm task
Expand Down Expand Up @@ -345,7 +345,7 @@ private void StoreBeaconRoot(Block block, IReleaseSpec spec)
{
try
{
_beaconBlockRootHandler.StoreBeaconRoot(block, spec);
_beaconBlockRootHandler.StoreBeaconRoot(block, spec, _stateProvider);
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private static ExecutionPayloadV4 CreateBlockRequestV4(MergeTestBlockchain chain
blockRequestV4.TryGetBlock(out Block? block);

var beaconBlockRootHandler = new BeaconBlockRootHandler(chain.TxProcessor);
beaconBlockRootHandler.StoreBeaconRoot(block!, chain.SpecProvider.GetSpec(block!.Header));
beaconBlockRootHandler.StoreBeaconRoot(block!, chain.SpecProvider.GetSpec(block!.Header), chain.WorldStateManager.GlobalWorldState);
Snapshot before = chain.State.TakeSnapshot();
var blockHashStore = new BlockhashStore(chain.SpecProvider, chain.State);
blockHashStore.ApplyBlockhashStateChanges(block!.Header);
Expand Down
Loading