Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -6,6 +6,7 @@
using Nethermind.Consensus.Producers;
using Nethermind.Core;
using Nethermind.Core.Specs;
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Logging;

namespace Nethermind.AccountAbstraction.Executor
Expand All @@ -25,10 +26,10 @@ public AABlockProducerTransactionsExecutorFactory(ISpecProvider specProvider, IL
_entryPointAddresses = entryPointAddresses;
}

public IBlockProcessor.IBlockTransactionsExecutor Create(ReadOnlyTxProcessingEnv readOnlyTxProcessingEnv)
public IBlockProcessor.IBlockTransactionsExecutor Create(IReadOnlyTxProcessingScope readOnlyTxProcessingEnv)
=> new AABlockProducerTransactionsExecutor(
readOnlyTxProcessingEnv.TransactionProcessor,
readOnlyTxProcessingEnv.StateProvider,
readOnlyTxProcessingEnv.WorldState,
_specProvider,
_logManager,
_signer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using Nethermind.Abi;
using Nethermind.AccountAbstraction.Data;
Expand Down Expand Up @@ -86,14 +87,14 @@ public ResultWrapper<Hash256> Simulate(UserOperation userOperation,
}

IEip1559Spec specFor1559 = _specProvider.GetSpecFor1559(parent.Number + 1);
ReadOnlyTxProcessingEnv txProcessingEnv = _readOnlyTxProcessingEnvFactory.Create();
ITransactionProcessor transactionProcessor = txProcessingEnv.Build(_stateProvider.StateRoot);
IReadOnlyTxProcessorSource processorSource = _readOnlyTxProcessingEnvFactory.Create();
using IReadOnlyTxProcessingScope scope = processorSource.Build(_stateProvider.StateRoot);

// wrap userOp into a tx calling the simulateWallet function off-chain from zero-address (look at EntryPoint.sol for more context)
Transaction simulateValidationTransaction =
BuildSimulateValidationTransaction(userOperation, parent, specFor1559);

UserOperationSimulationResult simulationResult = SimulateValidation(simulateValidationTransaction, userOperation, parent, transactionProcessor);
UserOperationSimulationResult simulationResult = SimulateValidation(simulateValidationTransaction, userOperation, parent, scope.TransactionProcessor);

if (!simulationResult.Success)
return ResultWrapper<Hash256>.Fail(simulationResult.Error ?? "unknown simulation failure");
Expand Down Expand Up @@ -187,19 +188,19 @@ private Transaction BuildSimulateValidationTransaction(
[Todo("Refactor once BlockchainBridge is separated")]
public CallOutput EstimateGas(BlockHeader header, Transaction tx, CancellationToken cancellationToken)
{
ReadOnlyTxProcessingEnv txProcessingEnv = _readOnlyTxProcessingEnvFactory.Create();
using IReadOnlyTransactionProcessor transactionProcessor = txProcessingEnv.Build(header.StateRoot!);
IReadOnlyTxProcessorSource txProcessorSource = _readOnlyTxProcessingEnvFactory.Create();
using IReadOnlyTxProcessingScope scope = txProcessorSource.Build(header.StateRoot!);

EstimateGasTracer estimateGasTracer = new();
(bool Success, string Error) tryCallResult = TryCallAndRestore(
transactionProcessor,
scope.TransactionProcessor,
header,
Math.Max(header.Timestamp + 1, _timestamper.UnixTime.Seconds),
tx,
true,
estimateGasTracer.WithCancellation(cancellationToken));

GasEstimator gasEstimator = new(transactionProcessor, _stateProvider, _specProvider, _blocksConfig);
GasEstimator gasEstimator = new(scope.TransactionProcessor, _stateProvider, _specProvider, _blocksConfig);
long estimate = gasEstimator.Estimate(tx, header, estimateGasTracer, GasEstimator.DefaultErrorMargin, cancellationToken);

return new CallOutput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Nethermind.Abi;
using Nethermind.Consensus;
using Nethermind.Consensus.AuRa.Contracts;
using Nethermind.Consensus.Processing;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Test;
Expand All @@ -26,24 +27,21 @@ public class ValidatorContractTests
{
private Block _block;
private readonly Address _contractAddress = Address.FromNumber(long.MaxValue);
private IReadOnlyTransactionProcessor _transactionProcessor;
private ITransactionProcessor _transactionProcessor;
private IReadOnlyTxProcessorSource _readOnlyTxProcessorSource;
private IWorldState _stateProvider;

[SetUp]
public void SetUp()
{
_block = new Block(Build.A.BlockHeader.TestObject, new BlockBody());
_transactionProcessor = Substitute.For<IReadOnlyTransactionProcessor>();
_readOnlyTxProcessorSource = Substitute.For<IReadOnlyTxProcessorSource>();
_readOnlyTxProcessorSource.Build(TestItem.KeccakA).Returns(_transactionProcessor);
_transactionProcessor = Substitute.For<ITransactionProcessor>();
_stateProvider = Substitute.For<IWorldState>();
_stateProvider.StateRoot.Returns(TestItem.KeccakA);
_readOnlyTxProcessorSource = Substitute.For<IReadOnlyTxProcessorSource>();
_readOnlyTxProcessorSource.Build(TestItem.KeccakA).Returns(new ReadOnlyTxProcessingScope(_transactionProcessor, _stateProvider, Keccak.EmptyTreeHash));
}

[TearDown]
public void TearDown() => _transactionProcessor?.Dispose();

[Test]
public void constructor_throws_ArgumentNullException_on_null_contractAddress()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using Nethermind.Evm;
using Nethermind.Core.Specs;
using System.Text.Json;
using Nethermind.Consensus.Processing;

namespace Nethermind.AuRa.Test.Validators;

Expand All @@ -43,7 +44,7 @@ public class ContractBasedValidatorTests
private AuRaParameters.Validator _validator;
private Block _block;
private BlockHeader _parentHeader;
private IReadOnlyTransactionProcessor _transactionProcessor;
private ITransactionProcessor _transactionProcessor;
private IAuRaBlockFinalizationManager _blockFinalizationManager;
private static readonly Address _contractAddress = Address.FromNumber(1000);
private (Address Sender, byte[] TransactionData) _getValidatorsData = (Address.Zero, new byte[] { 0, 1, 2 });
Expand Down Expand Up @@ -74,11 +75,12 @@ public void SetUp()
};
_block = new Block(Build.A.BlockHeader.WithNumber(1).WithAura(1, Array.Empty<byte>()).TestObject, new BlockBody());

_transactionProcessor = Substitute.For<IReadOnlyTransactionProcessor>();
_transactionProcessor.IsContractDeployed(_contractAddress).Returns(true);
_readOnlyTxProcessorSource = Substitute.For<IReadOnlyTxProcessorSource>();
_readOnlyTxProcessorSource.Build(Arg.Any<Hash256>()).Returns(_transactionProcessor);
_transactionProcessor = Substitute.For<ITransactionProcessor>();
_stateProvider.StateRoot.Returns(TestItem.KeccakA);
_stateProvider.IsContract(_contractAddress).Returns(true);

_readOnlyTxProcessorSource = Substitute.For<IReadOnlyTxProcessorSource>();
_readOnlyTxProcessorSource.Build(Arg.Any<Hash256>()).Returns(new ReadOnlyTxProcessingScope(_transactionProcessor, _stateProvider, Keccak.EmptyTreeHash));
_blockTree.Head.Returns(_block);

_abiEncoder
Expand All @@ -96,7 +98,6 @@ public void SetUp()
public void TearDown()
{
_blockFinalizationManager?.Dispose();
_transactionProcessor?.Dispose();
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected ConstantContractBase(Contract contract)
protected Transaction GenerateTransaction(CallInfo callInfo) =>
_contract.GenerateTransaction<SystemTransaction>(callInfo.ContractAddress, callInfo.FunctionName, callInfo.Sender, DefaultConstantContractGasLimit, callInfo.ParentHeader, callInfo.Arguments);

protected byte[] CallCore(CallInfo callInfo, IReadOnlyTransactionProcessor readOnlyTransactionProcessor, Transaction transaction) =>
protected byte[] CallCore(CallInfo callInfo, ITransactionProcessor readOnlyTransactionProcessor, Transaction transaction) =>
_contract.CallCore(readOnlyTransactionProcessor, callInfo.ParentHeader, callInfo.FunctionName, transaction, true);

protected object[] DecodeReturnData(string functionName, byte[] data) => _contract.DecodeReturnData(functionName, data);
Expand All @@ -85,17 +85,17 @@ public override object[] Call(CallInfo callInfo)

lock (_readOnlyTxProcessorSource)
{
using var readOnlyTransactionProcessor = _readOnlyTxProcessorSource.Build(GetState(callInfo.ParentHeader));
return CallRaw(callInfo, readOnlyTransactionProcessor);
using var scope = _readOnlyTxProcessorSource.Build(GetState(callInfo.ParentHeader));
return CallRaw(callInfo, scope);
}
}

protected virtual object[] CallRaw(CallInfo callInfo, IReadOnlyTransactionProcessor readOnlyTransactionProcessor)
protected virtual object[] CallRaw(CallInfo callInfo, IReadOnlyTxProcessingScope scope)
{
var transaction = GenerateTransaction(callInfo);
if (_contract.ContractAddress is not null && readOnlyTransactionProcessor.IsContractDeployed(_contract.ContractAddress))
if (_contract.ContractAddress is not null && scope.WorldState.IsContract(_contract.ContractAddress))
{
var result = CallCore(callInfo, readOnlyTransactionProcessor, transaction);
var result = CallCore(callInfo, scope.TransactionProcessor, transaction);
return callInfo.Result = _contract.DecodeReturnData(callInfo.FunctionName, result);
}
else if (callInfo.MissingContractResult is not null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ public PermissionConstantContract(Contract contract, IReadOnlyTxProcessorSource
{
}

protected override object[] CallRaw(CallInfo callInfo, IReadOnlyTransactionProcessor readOnlyTransactionProcessor)
protected override object[] CallRaw(CallInfo callInfo, IReadOnlyTxProcessingScope scope)
{
if (callInfo is PermissionCallInfo transactionPermissionCallInfo)
{
transactionPermissionCallInfo.ToIsContract = readOnlyTransactionProcessor.IsContractDeployed(transactionPermissionCallInfo.To);
transactionPermissionCallInfo.ToIsContract = scope.WorldState.IsContract(transactionPermissionCallInfo.To);
}

return base.CallRaw(callInfo, readOnlyTransactionProcessor);
return base.CallRaw(callInfo, scope);
}

public class PermissionCallInfo : CallInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Nethermind.Consensus.Producers;
using Nethermind.Consensus.Transactions;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Crypto;
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Init.Steps;
Expand Down Expand Up @@ -102,7 +103,7 @@ public IBlockProducer BuildProducer(ITxSource? additionalTxSource = null)
return blockProducer;
}

private BlockProcessor CreateBlockProcessor(ReadOnlyTxProcessingEnv changeableTxProcessingEnv)
private BlockProcessor CreateBlockProcessor(IReadOnlyTxProcessingScope changeableTxProcessingEnv)
{
if (_api.RewardCalculatorSource is null) throw new StepDependencyException(nameof(_api.RewardCalculatorSource));
if (_api.ValidatorStore is null) throw new StepDependencyException(nameof(_api.ValidatorStore));
Expand All @@ -119,9 +120,9 @@ private BlockProcessor CreateBlockProcessor(ReadOnlyTxProcessingEnv changeableTx
new LocalTxFilter(_api.EngineSigner));

_validator = new AuRaValidatorFactory(_api.AbiEncoder,
changeableTxProcessingEnv.StateProvider,
changeableTxProcessingEnv.WorldState,
changeableTxProcessingEnv.TransactionProcessor,
changeableTxProcessingEnv.BlockTree,
_api.BlockTree,
_api.CreateReadOnlyTransactionProcessorSource(),
_api.ReceiptStorage,
_api.ValidatorStore,
Expand Down Expand Up @@ -151,18 +152,18 @@ private BlockProcessor CreateBlockProcessor(ReadOnlyTxProcessingEnv changeableTx
_api.BlockValidator,
_api.RewardCalculatorSource.Get(changeableTxProcessingEnv.TransactionProcessor),
_api.BlockProducerEnvFactory.TransactionsExecutorFactory.Create(changeableTxProcessingEnv),
changeableTxProcessingEnv.StateProvider,
changeableTxProcessingEnv.WorldState,
_api.ReceiptStorage,
_api.LogManager,
changeableTxProcessingEnv.BlockTree,
_api.BlockTree,
NullWithdrawalProcessor.Instance,
_validator,
auRaTxFilter,
CreateGasLimitCalculator(_api) as AuRaContractGasLimitOverride,
contractRewriter);
}

internal TxPoolTxSource CreateTxPoolTxSource(ReadOnlyTxProcessingEnv processingEnv)
internal TxPoolTxSource CreateTxPoolTxSource()
{
_txPriorityContract = TxAuRaFilterBuilders.CreateTxPrioritySources(_api);
_localDataSource = _api.TxPriorityContractLocalDataSource;
Expand Down Expand Up @@ -203,7 +204,7 @@ internal TxPoolTxSource CreateTxPoolTxSource(ReadOnlyTxProcessingEnv processingE

return new TxPriorityTxSource(
_api.TxPool,
processingEnv.StateReader,
_api.StateReader,
_api.LogManager,
txFilterPipeline,
whitelistContractDataStore,
Expand All @@ -225,36 +226,37 @@ BlockProducerEnv Create()
ReadOnlyBlockTree readOnlyBlockTree = _api.BlockTree.AsReadOnly();

ReadOnlyTxProcessingEnv txProcessingEnv = _api.CreateReadOnlyTransactionProcessorSource();
BlockProcessor blockProcessor = CreateBlockProcessor(txProcessingEnv);
IReadOnlyTxProcessingScope scope = txProcessingEnv.Build(Keccak.EmptyTreeHash);
BlockProcessor blockProcessor = CreateBlockProcessor(scope);

IBlockchainProcessor blockchainProcessor =
new BlockchainProcessor(
readOnlyBlockTree,
blockProcessor,
_api.BlockPreprocessor,
txProcessingEnv.StateReader,
_api.StateReader,
_api.LogManager,
BlockchainProcessor.Options.NoReceipts);

OneTimeChainProcessor chainProcessor = new(
txProcessingEnv.StateProvider,
scope.WorldState,
blockchainProcessor);

return new BlockProducerEnv()
{
BlockTree = readOnlyBlockTree,
ChainProcessor = chainProcessor,
ReadOnlyStateProvider = txProcessingEnv.StateProvider,
TxSource = CreateTxSourceForProducer(txProcessingEnv, additionalTxSource),
ReadOnlyStateProvider = scope.WorldState,
TxSource = CreateTxSourceForProducer(additionalTxSource),
ReadOnlyTxProcessingEnv = _api.CreateReadOnlyTransactionProcessorSource(),
};
}

return _blockProducerContext ??= Create();
}

private ITxSource CreateStandardTxSourceForProducer(ReadOnlyTxProcessingEnv processingEnv) =>
CreateTxPoolTxSource(processingEnv);
private ITxSource CreateStandardTxSourceForProducer() =>
CreateTxPoolTxSource();

private TxPoolTxSource CreateStandardTxPoolTxSource()
{
Expand All @@ -268,7 +270,7 @@ private TxPoolTxSource CreateStandardTxPoolTxSource()

private ITxFilter CreateAuraTxFilterForProducer() => TxAuRaFilterBuilders.CreateAuRaTxFilterForProducer(_api, _minGasPricesContractDataStore);

private ITxSource CreateTxSourceForProducer(ReadOnlyTxProcessingEnv processingEnv, ITxSource? additionalTxSource)
private ITxSource CreateTxSourceForProducer(ITxSource? additionalTxSource)
{
bool CheckAddPosdaoTransactions(IList<ITxSource> list, long auRaPosdaoTransition)
{
Expand Down Expand Up @@ -320,7 +322,7 @@ IList<IRandomContract> GetRandomContracts(
if (_api.BlockTree is null) throw new StepDependencyException(nameof(_api.BlockTree));
if (_api.EngineSigner is null) throw new StepDependencyException(nameof(_api.EngineSigner));

IList<ITxSource> txSources = new List<ITxSource> { CreateStandardTxSourceForProducer(processingEnv) };
IList<ITxSource> txSources = new List<ITxSource> { CreateStandardTxSourceForProducer() };
bool needSigner = false;

if (additionalTxSource is not null)
Expand All @@ -335,7 +337,7 @@ IList<IRandomContract> GetRandomContracts(
if (needSigner)
{
TxSealer transactionSealer = new TxSealer(_api.EngineSigner, _api.Timestamper);
txSource = new GeneratedTxSource(txSource, transactionSealer, processingEnv.StateReader, _api.LogManager);
txSource = new GeneratedTxSource(txSource, transactionSealer, _api.StateReader, _api.LogManager);
}

ITxFilter? txPermissionFilter = TxAuRaFilterBuilders.CreateTxPermissionFilter(_api);
Expand Down
16 changes: 10 additions & 6 deletions src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
using Nethermind.Consensus.Transactions;
using Nethermind.Consensus.Withdrawals;
using Nethermind.Core.Attributes;
using Nethermind.Core.Crypto;
using Nethermind.Db;
using Nethermind.Evm.TransactionProcessing;
using Nethermind.JsonRpc.Modules;
using Nethermind.State;

Expand Down Expand Up @@ -100,16 +102,18 @@ public IBlockProducer InitBlockProducer(ITxSource? additionalTxSource = null)
getFromApi.SpecProvider,
getFromApi.LogManager);

IReadOnlyTxProcessingScope scope = producerEnv.Build(Keccak.EmptyTreeHash);

BlockProcessor producerProcessor = new(
getFromApi!.SpecProvider,
getFromApi!.BlockValidator,
NoBlockRewards.Instance,
getFromApi.BlockProducerEnvFactory.TransactionsExecutorFactory.Create(producerEnv),
producerEnv.StateProvider,
getFromApi.BlockProducerEnvFactory.TransactionsExecutorFactory.Create(scope),
scope.WorldState,
NullReceiptStorage.Instance,
new BlockhashStore(getFromApi.BlockTree, getFromApi.SpecProvider, producerEnv.StateProvider),
new BlockhashStore(getFromApi.BlockTree, getFromApi.SpecProvider, scope.WorldState),
getFromApi.LogManager,
new BlockProductionWithdrawalProcessor(new WithdrawalProcessor(producerEnv.StateProvider, getFromApi.LogManager)));
new BlockProductionWithdrawalProcessor(new WithdrawalProcessor(scope.WorldState, getFromApi.LogManager)));

IBlockchainProcessor producerChainProcessor = new BlockchainProcessor(
readOnlyBlockTree,
Expand All @@ -120,7 +124,7 @@ public IBlockProducer InitBlockProducer(ITxSource? additionalTxSource = null)
BlockchainProcessor.Options.NoReceipts);

OneTimeChainProcessor chainProcessor = new(
producerEnv.StateProvider,
scope.WorldState,
producerChainProcessor);

ITxFilterPipeline txFilterPipeline =
Expand All @@ -141,7 +145,7 @@ public IBlockProducer InitBlockProducer(ITxSource? additionalTxSource = null)
CliqueBlockProducer blockProducer = new(
additionalTxSource.Then(txPoolTxSource),
chainProcessor,
producerEnv.StateProvider,
scope.WorldState,
getFromApi.Timestamper,
getFromApi.CryptoRandom,
_snapshotManager!,
Expand Down
Loading