Skip to content

Commit

Permalink
Cancun fixes + EIP 4788 (#6009)
Browse files Browse the repository at this point in the history
* - Added Eip4788 to ReleaseSpecs
- Added Eip4788 wiring to ChainSpecs

* - Added passing state to Precompiles
- Added Eip4788 stateful precompile

* - Adds consensus logic of Eip4788

* - Tests update

* - Minor fixes, Missing migration code

* - Test improvements

* - Test progress, Cleanup

* - More tests fixes

* - Whitespace fixes

* - Attempt fix failing JsonRpc.Tests

* - Fix whistespace check failure

* - move setup precompile 4788 to instance method
- make Instance field in Precompile static generic to match class type

* - fix some build issue

* - fixed failing tests

* - Apply changes

* - Fix failing tests

* - ws fixes

* - Introduce IBeaconBlockRootHandler

* - Removed unnecessary "null" default value

* - Fixed some naming inconsistensies

* - Fix ws problems

* - Fix default behavior of beaconBlockRootHandler argument in BlockProcessor.ctor

* - fix

* - Fix ws

* small improvements

* Add EngineApiVersions

* - refactor PayloadAttributes.Validate

* - ws fix

* Init beacon root precompile state for genesis (#5982)

* - Applied suggested changes

* Fix merge

* Fix name, whitespaces

* Fix expected code

* Remove balance set

* genesis.json beacon root.

* jsonrpc: fix parentBeaconBlockRoot naming, add isEip4788.

* Catch args issue

* - ignore 0x0b from precompile checks

* Ignore for genesis

* Don't count RLP wrapper, when calc tx size for pooled txs msg

* Revert "Trie recovery (#5861)"

This reverts commit a099f0f.

* Use main EIP to detect fork

* Adding some workarounds

* Add system acc

* Another workaround

* What if zero bloc

* Another idea

* Another idea

* +

* +

* Another attempt

* 1) What

* No genesis

* 2) H

* Revert "Revert "Trie recovery (#5861)""

This reverts commit 1425d3c.

* Adhere a formal rule of encoding

* Decline payloads with v3 fields

* 2) The

* Fix 2) The

* clarification comment

* fix typo in file name

* Add RLP testing scenarios

* FIx tracer type

* - 4788 cleanup

* - Make Eip4788 settabe from chainspec

* Fix merge

* - other tests fixes

* Working on review comments

* fix build

* remove BeaconBlock from precompile list

* - moved ParentBeaconBlockRoot type from byte[] to Keccak
- removed commented precompile line from VirtualMachine

* - fixed some tests
- removed deprectated tests

* - more cleanup

* - adjusted expected size in blob tx size tests

* - ws fix

* Default addr

* more cosmetic stuff

* Remove worldState parameter

* Revert "more cosmetic stuff"

This reverts commit 872bce8.

---------

Co-authored-by: Demuirgos <[email protected]>
Co-authored-by: lukasz.rozmej <[email protected]>
Co-authored-by: Alexey <[email protected]>
Co-authored-by: spencer-tb <[email protected]>
Co-authored-by: smartprogrammer <[email protected]>
  • Loading branch information
6 people authored Aug 18, 2023
1 parent 84bbb25 commit 055c64b
Show file tree
Hide file tree
Showing 96 changed files with 625 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
using Nethermind.State;
using NSubstitute;
using Nethermind.Config;
using Nethermind.Consensus.BeaconBlockRoot;

namespace Nethermind.AccountAbstraction.Test
{
Expand Down Expand Up @@ -252,7 +253,7 @@ protected override BlockProcessor CreateBlockProcessor()
}

protected override async Task<TestBlockchain> Build(ISpecProvider? specProvider = null,
UInt256? initialValues = null)
UInt256? initialValues = null, bool addBlockOnStart = true)
{
TestBlockchain chain = await base.Build(specProvider, initialValues);
IList<Address> entryPointContractAddresses = new List<Address>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections.Generic;
using FluentAssertions;
using Nethermind.Abi;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using Nethermind.Core.Extensions;
using Nethermind.Core.Test.Builders;
using Nethermind.Db;
using Nethermind.Evm;
using Nethermind.Evm.Tracing;
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Int256;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Nethermind.Abi;
using Nethermind.Consensus;
using Nethermind.Consensus.AuRa;
using Nethermind.Consensus.AuRa.BeaconBlockRoot;
using Nethermind.Consensus.AuRa.Contracts;
using Nethermind.Consensus.AuRa.Withdrawals;
using Nethermind.Consensus.Processing;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ protected override ILocalDataSource<IEnumerable<Address>> GetWhitelistLocalDataS
protected override ILocalDataSource<IEnumerable<TxPriorityContract.Destination>> GetMinGasPricesLocalDataStore() =>
LocalDataSource.GetMinGasPricesLocalDataSource();

protected override Task<TestBlockchain> Build(ISpecProvider specProvider = null, UInt256? initialValues = null)
protected override Task<TestBlockchain> Build(ISpecProvider specProvider = null, UInt256? initialValues = null, bool addBlockOnStart = true)
{
TempFile = TempPath.GetTempFile();
LocalDataSource = new TxPriorityContract.LocalDataSource(TempFile.Path, new EthereumJsonSerializer(), new FileSystem(), LimboLogs.Instance, Interval);
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.Consensus.AuRa;
using Nethermind.Consensus.AuRa.BeaconBlockRoot;
using Nethermind.Consensus.AuRa.Contracts;
using Nethermind.Consensus.AuRa.Transactions;
using Nethermind.Consensus.AuRa.Withdrawals;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using Nethermind.TxPool;
using NSubstitute;
using NUnit.Framework;
using Nethermind.Consensus.AuRa.BeaconBlockRoot;

namespace Nethermind.AuRa.Test.Transactions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Nethermind.Specs;
using Nethermind.Core.Test.Builders;
using Nethermind.Db;
using Nethermind.Evm;
using Nethermind.Evm.Tracing;
using Nethermind.Logging;
using Nethermind.Specs.Forks;
Expand All @@ -26,7 +25,6 @@
using FluentAssertions;
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Rewards;
using Nethermind.Consensus.Withdrawals;
using Nethermind.Core.Test.Blockchain;
using Nethermind.Evm.TransactionProcessing;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public PruningTestBlockchain()
TempDirectory = TempPath.GetTempDirectory();
}

protected override async Task<TestBlockchain> Build(ISpecProvider? specProvider = null, UInt256? initialValues = null)
protected override async Task<TestBlockchain> Build(ISpecProvider? specProvider = null, UInt256? initialValues = null, bool addBlockOnStart = true)
{
TestBlockchain chain = await base.Build(specProvider, initialValues);
PruningDb = (IFullPruningDb)DbProvider.StateDb;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using FluentAssertions;
using Nethermind.Blockchain.Receipts;
using Nethermind.Config;
using Nethermind.Consensus.BeaconBlockRoot;
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Producers;
using Nethermind.Consensus.Rewards;
Expand Down
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Blockchain.Test/ReorgTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Nethermind.Blockchain.Receipts;
using Nethermind.Blockchain.Spec;
using Nethermind.Blockchain.Synchronization;
using Nethermind.Consensus.BeaconBlockRoot;
using Nethermind.Consensus.Comparers;
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Rewards;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Core.Specs;
using Nethermind.Core;
using Nethermind.Int256;
using Nethermind.State;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;

namespace Nethermind.Consensus.BeaconBlockRoot;
public class BeaconBlockRootHandler : IBeaconBlockRootHandler
{
public static UInt256 HISTORICAL_ROOTS_LENGTH = 98304;
private static readonly Address DefaultPbbrContractAddress = Address.FromNumber(0x0b);

public void ApplyContractStateChanges(Block block, IReleaseSpec spec, IWorldState stateProvider)
{
if (!spec.IsBeaconBlockRootAvailable ||
block.IsGenesis ||
block.Header.ParentBeaconBlockRoot is null) return;

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

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

StorageCell tsStorageCell = new(spec.Eip4788ContractAddress ?? DefaultPbbrContractAddress, timestampReduced);
StorageCell brStorageCell = new(spec.Eip4788ContractAddress ?? DefaultPbbrContractAddress, rootIndex);

stateProvider.Set(tsStorageCell, Bytes.WithoutLeadingZeros(timestamp.ToBigEndian()).ToArray());
stateProvider.Set(brStorageCell, Bytes.WithoutLeadingZeros(parentBeaconBlockRoot.Bytes).ToArray());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nethermind.Core.Specs;
using Nethermind.Core;
using Nethermind.State;

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

namespace Nethermind.Blockchain
{
Expand All @@ -24,6 +25,7 @@ public class GenesisLoader
private readonly ISpecProvider _specProvider;
private readonly IWorldState _stateProvider;
private readonly ITransactionProcessor _transactionProcessor;
private readonly BeaconBlockRootHandler _beaconBlockRootHandler;

public GenesisLoader(
ChainSpec chainSpec,
Expand All @@ -35,6 +37,7 @@ public GenesisLoader(
_specProvider = specProvider ?? throw new ArgumentNullException(nameof(specProvider));
_stateProvider = stateProvider ?? throw new ArgumentNullException(nameof(stateProvider));
_transactionProcessor = transactionProcessor ?? throw new ArgumentNullException(nameof(transactionProcessor));
_beaconBlockRootHandler = new BeaconBlockRootHandler();
}

public Block Load()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
using Nethermind.TxPool;
using NUnit.Framework;
using Nethermind.Config;
using Nethermind.Consensus.BeaconBlockRoot;

namespace Nethermind.Clique.Test
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Nethermind.Blockchain.Find;
using Nethermind.Blockchain.Receipts;
using Nethermind.Consensus.AuRa.Validators;
using Nethermind.Consensus.BeaconBlockRoot;
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Rewards;
using Nethermind.Consensus.Transactions;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nethermind.Consensus.BeaconBlockRoot;
using Nethermind.Core;
using Nethermind.Core.Specs;
using Nethermind.State;

namespace Nethermind.Consensus.AuRa.BeaconBlockRoot;
internal class NullBeaconBlockRootHandler : IBeaconBlockRootHandler
{
public void ApplyContractStateChanges(Block block, IReleaseSpec spec, IWorldState state)
{
}

public static IBeaconBlockRootHandler Instance { get; } = new NullBeaconBlockRootHandler();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Nethermind.Blockchain.Data;
using Nethermind.Blockchain.Services;
using Nethermind.Config;
using Nethermind.Consensus.AuRa.BeaconBlockRoot;
using Nethermind.Consensus.AuRa.Config;
using Nethermind.Consensus.AuRa.Contracts;
using Nethermind.Consensus.AuRa.Contracts.DataStore;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Nethermind.Blockchain;
using Nethermind.Blockchain.Data;
using Nethermind.Config;
using Nethermind.Consensus.AuRa.BeaconBlockRoot;
using Nethermind.Consensus.AuRa.Config;
using Nethermind.Consensus.AuRa.Contracts;
using Nethermind.Consensus.AuRa.Contracts.DataStore;
Expand Down
11 changes: 11 additions & 0 deletions src/Nethermind/Nethermind.Consensus/EngineApiVersions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

namespace Nethermind.Consensus;

public static class EngineApiVersions
{
public const int Paris = 1;
public const int Shanghai = 2;
public const int Cancun = 3;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Numerics;
using Nethermind.Blockchain;
using Nethermind.Blockchain.Receipts;
using Nethermind.Consensus.BeaconBlockRoot;
using Nethermind.Consensus.Rewards;
using Nethermind.Consensus.Validators;
using Nethermind.Consensus.Withdrawals;
Expand All @@ -32,6 +33,7 @@ public partial class BlockProcessor : IBlockProcessor
private readonly IReceiptStorage _receiptStorage;
private readonly IWitnessCollector _witnessCollector;
private readonly IWithdrawalProcessor _withdrawalProcessor;
private readonly IBeaconBlockRootHandler _beaconBlockRootHandler;
private readonly IBlockValidator _blockValidator;
private readonly IRewardCalculator _rewardCalculator;
private readonly IBlockProcessor.IBlockTransactionsExecutor _blockTransactionsExecutor;
Expand Down Expand Up @@ -64,7 +66,7 @@ public BlockProcessor(
_withdrawalProcessor = withdrawalProcessor ?? new WithdrawalProcessor(stateProvider, logManager);
_rewardCalculator = rewardCalculator ?? throw new ArgumentNullException(nameof(rewardCalculator));
_blockTransactionsExecutor = blockTransactionsExecutor ?? throw new ArgumentNullException(nameof(blockTransactionsExecutor));

_beaconBlockRootHandler = new BeaconBlockRootHandler();

_receiptsTracer = new BlockReceiptsTracer();
}
Expand Down Expand Up @@ -225,6 +227,9 @@ protected virtual TxReceipt[] ProcessBlock(
_receiptsTracer.SetOtherTracer(blockTracer);
_receiptsTracer.StartNewBlockTrace(block);

_beaconBlockRootHandler.ApplyContractStateChanges(block, spec, _stateProvider);
_stateProvider.Commit(spec);

TxReceipt[] receipts = _blockTransactionsExecutor.ProcessTransactions(block, options, _receiptsTracer, spec);

if (spec.IsEip4844Enabled)
Expand Down Expand Up @@ -283,6 +288,7 @@ private Block PrepareBlockForProcessing(Block suggestedBlock)
BaseFeePerGas = bh.BaseFeePerGas,
WithdrawalsRoot = bh.WithdrawalsRoot,
IsPostMerge = bh.IsPostMerge,
ParentBeaconBlockRoot = bh.ParentBeaconBlockRoot,
};

return suggestedBlock.CreateCopy(headerForProcessing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using Nethermind.Blockchain.Receipts;
using Nethermind.Consensus.BeaconBlockRoot;
using Nethermind.Consensus.Rewards;
using Nethermind.Consensus.Validators;
using Nethermind.Consensus.Withdrawals;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ protected virtual BlockHeader PrepareBlockHeader(BlockHeader parent,
{
Author = blockAuthor,
MixHash = payloadAttributes?.PrevRandao,
ParentBeaconBlockRoot = payloadAttributes?.ParentBeaconBlockRoot
};

UInt256 difficulty = _difficultyCalculator.Calculate(header, parent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Nethermind.Blockchain;
using Nethermind.Blockchain.Receipts;
using Nethermind.Config;
using Nethermind.Consensus.BeaconBlockRoot;
using Nethermind.Consensus.Comparers;
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Rewards;
Expand Down
Loading

0 comments on commit 055c64b

Please sign in to comment.