Skip to content

Commit

Permalink
System tx experiment rel (#6526)
Browse files Browse the repository at this point in the history
Co-authored-by: Demuirgos <ayman.bouchareb@outlook.fr>
Co-authored-by: MarekM25 <marekm2504@gmail.com>
Co-authored-by: Kamil Chodoła <kamil@nethermind.io>
  • Loading branch information
4 people authored Jan 12, 2024
1 parent a8ccee7 commit b21823f
Show file tree
Hide file tree
Showing 63 changed files with 875 additions and 223 deletions.
14 changes: 8 additions & 6 deletions src/Nethermind/Ethereum.Test.Base/BlockchainTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,22 @@ protected async Task<EthereumTestResult> RunTest(BlockchainTest test, Stopwatch?
specProvider,
_logManager);

TransactionProcessor? txProcessor = new(
specProvider,
stateProvider,
virtualMachine,
_logManager);

IBlockProcessor blockProcessor = new BlockProcessor(
specProvider,
blockValidator,
rewardCalculator,
new BlockProcessor.BlockValidationTransactionsExecutor(
new TransactionProcessor(
specProvider,
stateProvider,
virtualMachine,
_logManager),
new BlockProcessor.BlockValidationTransactionsExecutor(txProcessor,
stateProvider),
stateProvider,
receiptStorage,
NullWitnessCollector.Instance,
txProcessor,
_logManager);

IBlockchainProcessor blockchainProcessor = new BlockchainProcessor(
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Ethereum.Test.Base/GeneralTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private static void InitializeTestState(GeneralStateTest test, WorldState stateP
}

stateProvider.CreateAccount(accountState.Key, accountState.Value.Balance);
stateProvider.InsertCode(accountState.Key, accountState.Value.Code, specProvider.GenesisSpec);
stateProvider.InsertCode(accountState.Key, accountState.Value.Code, specProvider.GenesisSpec, false);
stateProvider.SetNonce(accountState.Key, accountState.Value.Nonce);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ protected override BlockProcessor CreateBlockProcessor()
State,
ReceiptStorage,
NullWitnessCollector.Instance,
TxProcessor,
LogManager);

AbiParameterConverter.RegisterFactory(new AbiTypeFactory(new AbiTuple<UserOperationAbi>()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ void Process(AuRaBlockProcessor auRaBlockProcessor, int blockNumber, Hash256 sta
LimboLogs.Instance,
Substitute.For<IBlockTree>(),
new WithdrawalProcessor(stateProvider, LimboLogs.Instance),
transactionProcessor,
txFilter,
contractRewriter: contractRewriter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ protected override BlockProcessor CreateBlockProcessor()
LimboLogs.Instance,
BlockTree,
NullWithdrawalProcessor.Instance,
TxProcessor,
null,
GasLimitCalculator as AuRaContractGasLimitOverride);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ protected override BlockProcessor CreateBlockProcessor()
ReceiptStorage,
LimboLogs.Instance,
BlockTree,
NullWithdrawalProcessor.Instance);
NullWithdrawalProcessor.Instance,
TxProcessor);
}

protected override Task AddBlocksOnStart() => Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ protected override BlockProcessor CreateBlockProcessor()
LimboLogs.Instance,
BlockTree,
NullWithdrawalProcessor.Instance,
TxProcessor,
PermissionBasedTxFilter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void Prepared_block_contains_author_field()
stateProvider,
NullReceiptStorage.Instance,
NullWitnessCollector.Instance,
transactionProcessor,
LimboLogs.Instance);

BlockHeader header = Build.A.BlockHeader.WithAuthor(TestItem.AddressD).TestObject;
Expand Down Expand Up @@ -80,6 +81,7 @@ public void Can_store_a_witness()
stateProvider,
NullReceiptStorage.Instance,
witnessCollector,
transactionProcessor,
LimboLogs.Instance);

BlockHeader header = Build.A.BlockHeader.WithAuthor(TestItem.AddressD).TestObject;
Expand Down Expand Up @@ -109,6 +111,7 @@ public void Recovers_state_on_cancel()
stateProvider,
NullReceiptStorage.Instance,
NullWitnessCollector.Instance,
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 @@ -73,6 +73,7 @@ public void Test()
stateProvider,
NullReceiptStorage.Instance,
NullWitnessCollector.Instance,
txProcessor,
LimboLogs.Instance);
BlockchainProcessor blockchainProcessor = new(
blockTree,
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 @@ -75,6 +75,7 @@ public void Setup()
stateProvider,
NullReceiptStorage.Instance,
new WitnessCollector(memDbProvider.StateDb, LimboLogs.Instance),
transactionProcessor,
LimboLogs.Instance);
_blockchainProcessor = new BlockchainProcessor(
_blockTree,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,57 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

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

namespace Nethermind.Consensus.BeaconBlockRoot;
namespace Nethermind.Blockchain.BeaconBlockRoot;
public class BeaconBlockRootHandler : IBeaconBlockRootHandler
{
public static UInt256 HISTORICAL_ROOTS_LENGTH = 8191;
private static readonly Address DefaultPbbrContractAddress = new Address("0x000F3df6D732807Ef1319fB7B8bB8522d0Beac02");

public void ApplyContractStateChanges(Block block, IReleaseSpec spec, IWorldState stateProvider)
private readonly ITransactionProcessor _processor;
private static Address Default4788Address = new Address("0x000f3df6d732807ef1319fb7b8bb8522d0beac02");
private readonly ILogger _logger;
private const long GasLimit = 30_000_000L;
public BeaconBlockRootHandler(
ITransactionProcessor processor,
ILogManager logManager)
{
_processor = processor;
_logger = logManager.GetClassLogger();
}
public void ExecuteSystemCall(Block block, IReleaseSpec spec)
{
BlockHeader? header = block.Header;
if (!spec.IsBeaconBlockRootAvailable ||
block.IsGenesis ||
block.Header.ParentBeaconBlockRoot is null) return;
Address? eip4788Account = spec.Eip4788ContractAddress ?? DefaultPbbrContractAddress;
if (!stateProvider.AccountExists(eip4788Account))
return;

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

UInt256.Mod(timestamp, HISTORICAL_ROOTS_LENGTH, out UInt256 timestampReduced);
UInt256 rootIndex = timestampReduced + HISTORICAL_ROOTS_LENGTH;
header.IsGenesis ||
header.ParentBeaconBlockRoot is null) return;

StorageCell tsStorageCell = new(eip4788Account, timestampReduced);
StorageCell brStorageCell = new(eip4788Account, rootIndex);
Transaction? transaction = new()
{
Value = UInt256.Zero,
Data = header.ParentBeaconBlockRoot.Bytes.ToArray(),
To = spec.Eip4788ContractAddress ?? Default4788Address,
SenderAddress = Address.SystemUser,
GasLimit = GasLimit,
GasPrice = UInt256.Zero,
};
transaction.Hash = transaction.CalculateHash();

stateProvider.Set(tsStorageCell, Bytes.WithoutLeadingZeros(timestamp.ToBigEndian()).ToArray());
stateProvider.Set(brStorageCell, Bytes.WithoutLeadingZeros(parentBeaconBlockRoot.Bytes).ToArray());
try
{
_processor.Execute(transaction, header, NullTxTracer.Instance);
}
catch (Exception e)
{
if (_logger.IsError) _logger.Error("Error during calling BeaconBlockRoot contract", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

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

namespace Nethermind.Consensus.BeaconBlockRoot;
namespace Nethermind.Blockchain.BeaconBlockRoot;
public interface IBeaconBlockRootHandler
{
void ApplyContractStateChanges(Block block, IReleaseSpec spec, IWorldState state);
void ExecuteSystemCall(Block block, IReleaseSpec spec);
}
1 change: 0 additions & 1 deletion src/Nethermind/Nethermind.Blockchain/Contracts/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ protected byte[] CallCore(ITransactionProcessor transactionProcessor, BlockHeade
return tracer.ReturnValue;
}
}

protected object[] DecodeReturnData(string functionName, byte[] data)
{
AbiEncodingInfo abiEncodingInfo = AbiDefinition.GetFunction(functionName).GetReturnInfo();
Expand Down
3 changes: 0 additions & 3 deletions src/Nethermind/Nethermind.Blockchain/GenesisLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Specs.ChainSpecStyle;
using Nethermind.State;
using Nethermind.Consensus.BeaconBlockRoot;

namespace Nethermind.Blockchain
{
Expand All @@ -24,7 +23,6 @@ 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 @@ -36,7 +34,6 @@ 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 @@ -139,6 +139,7 @@ public On CreateNode(PrivateKey privateKey, bool withGenesisAlreadyProcessed = f
stateProvider,
NullReceiptStorage.Instance,
NullWitnessCollector.Instance,
transactionProcessor,
nodeLogManager);

BlockchainProcessor processor = new(blockTree, blockProcessor, new AuthorRecoveryStep(snapshotManager), stateReader, nodeLogManager, BlockchainProcessor.Options.NoReceipts);
Expand All @@ -158,6 +159,7 @@ public On CreateNode(PrivateKey privateKey, bool withGenesisAlreadyProcessed = f
minerStateProvider,
NullReceiptStorage.Instance,
NullWitnessCollector.Instance,
minerTransactionProcessor,
nodeLogManager);

BlockchainProcessor minerProcessor = new(blockTree, minerBlockProcessor, new AuthorRecoveryStep(snapshotManager), stateReader, nodeLogManager, BlockchainProcessor.Options.NoReceipts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Nethermind.Core.Specs;
using Nethermind.Crypto;
using Nethermind.Evm.Tracing;
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Logging;
using Nethermind.State;
using Nethermind.TxPool;
Expand All @@ -41,6 +42,7 @@ public AuRaBlockProcessor(
ILogManager logManager,
IBlockTree blockTree,
IWithdrawalProcessor withdrawalProcessor,
ITransactionProcessor transactionProcessor,
ITxFilter? txFilter = null,
AuRaContractGasLimitOverride? gasLimitOverride = null,
ContractRewriter? contractRewriter = null)
Expand All @@ -52,6 +54,7 @@ public AuRaBlockProcessor(
stateProvider,
receiptStorage,
NullWitnessCollector.Instance,
transactionProcessor,
logManager,
withdrawalProcessor)
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ protected virtual BlockProcessor NewBlockProcessor(AuRaNethermindApi api, ITxFil
_api.LogManager,
_api.BlockTree,
NullWithdrawalProcessor.Instance,
_api.TransactionProcessor,
txFilter,
GetGasLimitCalculator(),
contractRewriter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ private BlockProcessor CreateBlockProcessor(ReadOnlyTxProcessingEnv changeableTx
_api.LogManager,
changeableTxProcessingEnv.BlockTree,
NullWithdrawalProcessor.Instance,
_api.TransactionProcessor,
auRaTxFilter,
CreateGasLimitCalculator(constantContractTxProcessingEnv) as AuRaContractGasLimitOverride,
contractRewriter)
Expand Down
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public Task<IBlockProducer> InitBlockProducer(IBlockProductionTrigger? blockProd
producerEnv.StateProvider,
NullReceiptStorage.Instance,
NullWitnessCollector.Instance,
getFromApi.TransactionProcessor,
getFromApi.LogManager,
new BlockProductionWithdrawalProcessor(new WithdrawalProcessor(producerEnv.StateProvider, getFromApi.LogManager)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public Task<IBlockProducer> InitBlockProducer(IBlockProductionTrigger? blockProd
producerEnv.StateProvider,
NullReceiptStorage.Instance,
NullWitnessCollector.Instance,
getFromApi.TransactionProcessor,
getFromApi.LogManager);

IBlockchainProcessor producerChainProcessor = new BlockchainProcessor(
Expand Down
14 changes: 8 additions & 6 deletions src/Nethermind/Nethermind.Consensus/Processing/BlockProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
using System.Collections.Generic;
using System.Numerics;
using Nethermind.Blockchain;
using Nethermind.Blockchain.BeaconBlockRoot;
using Nethermind.Blockchain.Receipts;
using Nethermind.Consensus.BeaconBlockRoot;
using Nethermind.Consensus.Rewards;
using Nethermind.Consensus.Validators;
using Nethermind.Consensus.Withdrawals;
Expand All @@ -16,6 +16,7 @@
using Nethermind.Crypto;
using Nethermind.Evm;
using Nethermind.Evm.Tracing;
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Int256;
using Nethermind.Logging;
using Nethermind.Specs.Forks;
Expand Down Expand Up @@ -53,8 +54,10 @@ public BlockProcessor(
IWorldState? stateProvider,
IReceiptStorage? receiptStorage,
IWitnessCollector? witnessCollector,
ITransactionProcessor transactionProcessor,
ILogManager? logManager,
IWithdrawalProcessor? withdrawalProcessor = null)
IWithdrawalProcessor? withdrawalProcessor = null,
IBeaconBlockRootHandler? beaconBlockRootHandler = null)
{
_logger = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager));
_specProvider = specProvider ?? throw new ArgumentNullException(nameof(specProvider));
Expand All @@ -65,8 +68,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();

_beaconBlockRootHandler = beaconBlockRootHandler ?? new BeaconBlockRootHandler(transactionProcessor, logManager);
ReceiptsTracer = new BlockReceiptsTracer();
}

Expand Down Expand Up @@ -110,6 +112,7 @@ the previous head state.*/

// be cautious here as AuRa depends on processing
PreCommitBlock(newBranchStateRoot, suggestedBlocks[i].Number);

if (notReadOnly)
{
_witnessCollector.Persist(processedBlock.Hash!);
Expand Down Expand Up @@ -229,8 +232,7 @@ protected virtual TxReceipt[] ProcessBlock(
ReceiptsTracer.SetOtherTracer(blockTracer);
ReceiptsTracer.StartNewBlockTrace(block);

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

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

Expand Down
Loading

0 comments on commit b21823f

Please sign in to comment.