From 75a852a8de4bed7de0d56380dcd63e8186a8e475 Mon Sep 17 00:00:00 2001 From: Amirul Ashraf Date: Wed, 8 Nov 2023 14:23:53 +0800 Subject: [PATCH 01/17] Separate statedb initialization from blockchain initialization --- .../Nethermind.Init/InitializeStateDb.cs | 240 ++++++++++++++++++ .../Steps/InitializeBlockchain.cs | 198 +-------------- 2 files changed, 246 insertions(+), 192 deletions(-) create mode 100644 src/Nethermind/Nethermind.Init/InitializeStateDb.cs diff --git a/src/Nethermind/Nethermind.Init/InitializeStateDb.cs b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs new file mode 100644 index 00000000000..2345f31541f --- /dev/null +++ b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs @@ -0,0 +1,240 @@ +// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited +// SPDX-License-Identifier: LGPL-3.0-only + +using System; +using System.IO.Abstractions; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Nethermind.Api; +using Nethermind.Blockchain; +using Nethermind.Blockchain.FullPruning; +using Nethermind.Blockchain.Synchronization; +using Nethermind.Config; +using Nethermind.Consensus; +using Nethermind.Consensus.Comparers; +using Nethermind.Core; +using Nethermind.Core.Crypto; +using Nethermind.Core.Extensions; +using Nethermind.Db; +using Nethermind.Db.FullPruning; +using Nethermind.Init.Steps; +using Nethermind.JsonRpc.Converters; +using Nethermind.JsonRpc.Modules.DebugModule; +using Nethermind.JsonRpc.Modules.Trace; +using Nethermind.Logging; +using Nethermind.Serialization.Json; +using Nethermind.State; +using Nethermind.State.Witnesses; +using Nethermind.Synchronization.Trie; +using Nethermind.Synchronization.Witness; +using Nethermind.Trie; +using Nethermind.Trie.Pruning; + +namespace Nethermind.Init; + +[RunnerStepDependencies(typeof(InitializePlugins), typeof(InitializeBlockTree), typeof(SetupKeyStore))] +public class InitializeStateDb: IStep +{ + private readonly INethermindApi _api; + private ILogger? _logger; + + public InitializeStateDb(INethermindApi api) + { + _api = api; + } + + public Task Execute(CancellationToken cancellationToken) + { + InitBlockTraceDumper(); + + (IApiWithStores getApi, IApiWithBlockchain setApi) = _api.ForBlockchain; + + if (getApi.ChainSpec is null) throw new StepDependencyException(nameof(getApi.ChainSpec)); + if (getApi.DbProvider is null) throw new StepDependencyException(nameof(getApi.DbProvider)); + if (getApi.SpecProvider is null) throw new StepDependencyException(nameof(getApi.SpecProvider)); + if (getApi.BlockTree is null) throw new StepDependencyException(nameof(getApi.BlockTree)); + + _logger = getApi.LogManager.GetClassLogger(); + ISyncConfig syncConfig = getApi.Config(); + IPruningConfig pruningConfig = getApi.Config(); + IInitConfig initConfig = getApi.Config(); + + if (syncConfig.DownloadReceiptsInFastSync && !syncConfig.DownloadBodiesInFastSync) + { + if (_logger.IsWarn) _logger.Warn($"{nameof(syncConfig.DownloadReceiptsInFastSync)} is selected but {nameof(syncConfig.DownloadBodiesInFastSync)} - enabling bodies to support receipts download."); + syncConfig.DownloadBodiesInFastSync = true; + } + + IWitnessCollector witnessCollector; + if (syncConfig.WitnessProtocolEnabled) + { + WitnessCollector witnessCollectorImpl = new(getApi.DbProvider.WitnessDb, _api.LogManager); + witnessCollector = setApi.WitnessCollector = witnessCollectorImpl; + setApi.WitnessRepository = witnessCollectorImpl.WithPruning(getApi.BlockTree!, getApi.LogManager); + } + else + { + witnessCollector = setApi.WitnessCollector = NullWitnessCollector.Instance; + setApi.WitnessRepository = NullWitnessCollector.Instance; + } + + CachingStore cachedStateDb = getApi.DbProvider.StateDb + .Cached(Trie.MemoryAllowance.TrieNodeCacheCount); + setApi.MainStateDbWithCache = cachedStateDb; + IKeyValueStore codeDb = getApi.DbProvider.CodeDb + .WitnessedBy(witnessCollector); + + IKeyValueStoreWithBatching stateWitnessedBy = setApi.MainStateDbWithCache.WitnessedBy(witnessCollector); + IPersistenceStrategy persistenceStrategy; + IPruningStrategy pruningStrategy; + if (pruningConfig.Mode.IsMemory()) + { + persistenceStrategy = Persist.IfBlockOlderThan(pruningConfig.PersistenceInterval); // TODO: this should be based on time + if (pruningConfig.Mode.IsFull()) + { + PruningTriggerPersistenceStrategy triggerPersistenceStrategy = new((IFullPruningDb)getApi.DbProvider!.StateDb, getApi.BlockTree!, getApi.LogManager); + getApi.DisposeStack.Push(triggerPersistenceStrategy); + persistenceStrategy = persistenceStrategy.Or(triggerPersistenceStrategy); + } + + pruningStrategy = Prune.WhenCacheReaches(pruningConfig.CacheMb.MB()); // TODO: memory hint should define this + } + else + { + pruningStrategy = No.Pruning; + persistenceStrategy = Persist.EveryBlock; + } + + TrieStore trieStore = syncConfig.TrieHealing + ? new HealingTrieStore( + stateWitnessedBy, + pruningStrategy, + persistenceStrategy, + getApi.LogManager) + : new TrieStore( + stateWitnessedBy, + pruningStrategy, + persistenceStrategy, + getApi.LogManager); + setApi.TrieStore = trieStore; + + IWorldState worldState = setApi.WorldState = syncConfig.TrieHealing + ? new HealingWorldState( + trieStore, + codeDb, + getApi.LogManager) + : new WorldState( + trieStore, + codeDb, + getApi.LogManager); + + if (pruningConfig.Mode.IsFull()) + { + IFullPruningDb fullPruningDb = (IFullPruningDb)getApi.DbProvider!.StateDb; + fullPruningDb.PruningStarted += (_, args) => + { + cachedStateDb.PersistCache(args.Context); + trieStore.PersistCache(args.Context, args.Context.CancellationTokenSource.Token); + }; + } + + TrieStoreBoundaryWatcher trieStoreBoundaryWatcher = new(trieStore, _api.BlockTree!, _api.LogManager); + getApi.DisposeStack.Push(trieStoreBoundaryWatcher); + getApi.DisposeStack.Push(trieStore); + + ITrieStore readOnlyTrieStore = setApi.ReadOnlyTrieStore = trieStore.AsReadOnly(cachedStateDb); + + ReadOnlyDbProvider readOnly = new(getApi.DbProvider, false); + + IStateReader stateReader = setApi.StateReader = new StateReader(readOnlyTrieStore, readOnly.GetDb(DbNames.Code), getApi.LogManager); + + setApi.TransactionComparerProvider = new TransactionComparerProvider(getApi.SpecProvider!, getApi.BlockTree.AsReadOnly()); + setApi.ChainHeadStateProvider = new ChainHeadReadOnlyStateProvider(getApi.BlockTree, stateReader); + + worldState.StateRoot = getApi.BlockTree!.Head?.StateRoot ?? Keccak.EmptyTreeHash; + + if (_api.Config().DiagnosticMode == DiagnosticMode.VerifyTrie) + { + Task.Run(() => + { + try + { + _logger!.Info("Collecting trie stats and verifying that no nodes are missing..."); + TrieStore noPruningStore = new(stateWitnessedBy, No.Pruning, Persist.EveryBlock, getApi.LogManager); + IWorldState diagStateProvider = new WorldState(noPruningStore, codeDb, getApi.LogManager) + { + StateRoot = getApi.BlockTree!.Head?.StateRoot ?? Keccak.EmptyTreeHash + }; + TrieStats stats = diagStateProvider.CollectStats(getApi.DbProvider.CodeDb, _api.LogManager); + _logger.Info($"Starting from {getApi.BlockTree.Head?.Number} {getApi.BlockTree.Head?.StateRoot}{Environment.NewLine}" + stats); + } + catch (Exception ex) + { + _logger!.Error(ex.ToString()); + } + }); + } + + // Init state if we need system calls before actual processing starts + if (getApi.BlockTree!.Head?.StateRoot is not null) + { + worldState.StateRoot = getApi.BlockTree.Head.StateRoot; + } + + InitializeFullPruning(pruningConfig, initConfig, _api, stateReader); + + return Task.CompletedTask; + } + + private static void InitBlockTraceDumper() + { + BlockTraceDumper.Converters.AddRange(EthereumJsonSerializer.CommonConverters); + BlockTraceDumper.Converters.AddRange(DebugModuleFactory.Converters); + BlockTraceDumper.Converters.AddRange(TraceModuleFactory.Converters); + BlockTraceDumper.Converters.Add(new TxReceiptConverter()); + } + + private static void InitializeFullPruning( + IPruningConfig pruningConfig, + IInitConfig initConfig, + INethermindApi api, + IStateReader stateReader) + { + IPruningTrigger? CreateAutomaticTrigger(string dbPath) + { + long threshold = pruningConfig.FullPruningThresholdMb.MB(); + + switch (pruningConfig.FullPruningTrigger) + { + case FullPruningTrigger.StateDbSize: + return new PathSizePruningTrigger(dbPath, threshold, api.TimerFactory, api.FileSystem); + case FullPruningTrigger.VolumeFreeSpace: + return new DiskFreeSpacePruningTrigger(dbPath, threshold, api.TimerFactory, api.FileSystem); + default: + return null; + } + } + + if (pruningConfig.Mode.IsFull()) + { + IDb stateDb = api.DbProvider!.StateDb; + if (stateDb is IFullPruningDb fullPruningDb) + { + string pruningDbPath = fullPruningDb.GetPath(initConfig.BaseDbPath); + IPruningTrigger? pruningTrigger = CreateAutomaticTrigger(pruningDbPath); + if (pruningTrigger is not null) + { + api.PruningTrigger.Add(pruningTrigger); + } + + IDriveInfo? drive = api.FileSystem.GetDriveInfos(pruningDbPath).FirstOrDefault(); + FullPruner pruner = new(fullPruningDb, api.PruningTrigger, pruningConfig, api.BlockTree!, + stateReader, api.ProcessExit!, ChainSizes.CreateChainSizeInfo(api.ChainSpec.ChainId), + drive, api.LogManager); + api.DisposeStack.Push(pruner); + } + } + } + +} diff --git a/src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs b/src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs index 1aacf5203e6..69c159b307f 100644 --- a/src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs +++ b/src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs @@ -44,11 +44,10 @@ namespace Nethermind.Init.Steps { - [RunnerStepDependencies(typeof(InitializePlugins), typeof(InitializeBlockTree), typeof(SetupKeyStore))] + [RunnerStepDependencies(typeof(InitializeStateDb), typeof(InitializePlugins), typeof(InitializeBlockTree), typeof(SetupKeyStore))] public class InitializeBlockchain : IStep { private readonly INethermindApi _api; - private ILogger? _logger; // ReSharper disable once MemberCanBeProtected.Global public InitializeBlockchain(INethermindApi api) @@ -64,146 +63,13 @@ public async Task Execute(CancellationToken _) [Todo(Improve.Refactor, "Use chain spec for all chain configuration")] protected virtual Task InitBlockchain() { - InitBlockTraceDumper(); - (IApiWithStores getApi, IApiWithBlockchain setApi) = _api.ForBlockchain; + setApi.TxValidator = new TxValidator(_api.SpecProvider!.ChainId); - if (getApi.ChainSpec is null) throw new StepDependencyException(nameof(getApi.ChainSpec)); - if (getApi.DbProvider is null) throw new StepDependencyException(nameof(getApi.DbProvider)); - if (getApi.SpecProvider is null) throw new StepDependencyException(nameof(getApi.SpecProvider)); - if (getApi.BlockTree is null) throw new StepDependencyException(nameof(getApi.BlockTree)); - - _logger = getApi.LogManager.GetClassLogger(); IInitConfig initConfig = getApi.Config(); - ISyncConfig syncConfig = getApi.Config(); - IPruningConfig pruningConfig = getApi.Config(); IBlocksConfig blocksConfig = getApi.Config(); - IMiningConfig miningConfig = getApi.Config(); - - if (syncConfig.DownloadReceiptsInFastSync && !syncConfig.DownloadBodiesInFastSync) - { - if (_logger.IsWarn) _logger.Warn($"{nameof(syncConfig.DownloadReceiptsInFastSync)} is selected but {nameof(syncConfig.DownloadBodiesInFastSync)} - enabling bodies to support receipts download."); - syncConfig.DownloadBodiesInFastSync = true; - } - - IWitnessCollector witnessCollector; - if (syncConfig.WitnessProtocolEnabled) - { - WitnessCollector witnessCollectorImpl = new(getApi.DbProvider.WitnessDb, _api.LogManager); - witnessCollector = setApi.WitnessCollector = witnessCollectorImpl; - setApi.WitnessRepository = witnessCollectorImpl.WithPruning(getApi.BlockTree!, getApi.LogManager); - } - else - { - witnessCollector = setApi.WitnessCollector = NullWitnessCollector.Instance; - setApi.WitnessRepository = NullWitnessCollector.Instance; - } - - CachingStore cachedStateDb = getApi.DbProvider.StateDb - .Cached(Trie.MemoryAllowance.TrieNodeCacheCount); - setApi.MainStateDbWithCache = cachedStateDb; - IKeyValueStore codeDb = getApi.DbProvider.CodeDb - .WitnessedBy(witnessCollector); - - IKeyValueStoreWithBatching stateWitnessedBy = setApi.MainStateDbWithCache.WitnessedBy(witnessCollector); - IPersistenceStrategy persistenceStrategy; - IPruningStrategy pruningStrategy; - if (pruningConfig.Mode.IsMemory()) - { - persistenceStrategy = Persist.IfBlockOlderThan(pruningConfig.PersistenceInterval); // TODO: this should be based on time - if (pruningConfig.Mode.IsFull()) - { - PruningTriggerPersistenceStrategy triggerPersistenceStrategy = new((IFullPruningDb)getApi.DbProvider!.StateDb, getApi.BlockTree!, getApi.LogManager); - getApi.DisposeStack.Push(triggerPersistenceStrategy); - persistenceStrategy = persistenceStrategy.Or(triggerPersistenceStrategy); - } - - pruningStrategy = Prune.WhenCacheReaches(pruningConfig.CacheMb.MB()); // TODO: memory hint should define this - } - else - { - pruningStrategy = No.Pruning; - persistenceStrategy = Persist.EveryBlock; - } - - TrieStore trieStore = syncConfig.TrieHealing - ? new HealingTrieStore( - stateWitnessedBy, - pruningStrategy, - persistenceStrategy, - getApi.LogManager) - : new TrieStore( - stateWitnessedBy, - pruningStrategy, - persistenceStrategy, - getApi.LogManager); - setApi.TrieStore = trieStore; - - IWorldState worldState = setApi.WorldState = syncConfig.TrieHealing - ? new HealingWorldState( - trieStore, - codeDb, - getApi.LogManager) - : new WorldState( - trieStore, - codeDb, - getApi.LogManager); - - if (pruningConfig.Mode.IsFull()) - { - IFullPruningDb fullPruningDb = (IFullPruningDb)getApi.DbProvider!.StateDb; - fullPruningDb.PruningStarted += (_, args) => - { - cachedStateDb.PersistCache(args.Context); - trieStore.PersistCache(args.Context, args.Context.CancellationTokenSource.Token); - }; - } - - TrieStoreBoundaryWatcher trieStoreBoundaryWatcher = new(trieStore, _api.BlockTree!, _api.LogManager); - getApi.DisposeStack.Push(trieStoreBoundaryWatcher); - getApi.DisposeStack.Push(trieStore); - - ITrieStore readOnlyTrieStore = setApi.ReadOnlyTrieStore = trieStore.AsReadOnly(cachedStateDb); - - ReadOnlyDbProvider readOnly = new(getApi.DbProvider, false); - - IStateReader stateReader = setApi.StateReader = new StateReader(readOnlyTrieStore, readOnly.GetDb(DbNames.Code), getApi.LogManager); - - setApi.TransactionComparerProvider = new TransactionComparerProvider(getApi.SpecProvider!, getApi.BlockTree.AsReadOnly()); - setApi.ChainHeadStateProvider = new ChainHeadReadOnlyStateProvider(getApi.BlockTree, stateReader); - - worldState.StateRoot = getApi.BlockTree!.Head?.StateRoot ?? Keccak.EmptyTreeHash; - - if (_api.Config().DiagnosticMode == DiagnosticMode.VerifyTrie) - { - Task.Run(() => - { - try - { - _logger!.Info("Collecting trie stats and verifying that no nodes are missing..."); - TrieStore noPruningStore = new(stateWitnessedBy, No.Pruning, Persist.EveryBlock, getApi.LogManager); - IWorldState diagStateProvider = new WorldState(noPruningStore, codeDb, getApi.LogManager) - { - StateRoot = getApi.BlockTree!.Head?.StateRoot ?? Keccak.EmptyTreeHash - }; - TrieStats stats = diagStateProvider.CollectStats(getApi.DbProvider.CodeDb, _api.LogManager); - _logger.Info($"Starting from {getApi.BlockTree.Head?.Number} {getApi.BlockTree.Head?.StateRoot}{Environment.NewLine}" + stats); - } - catch (Exception ex) - { - _logger!.Error(ex.ToString()); - } - }); - } - - // Init state if we need system calls before actual processing starts - if (getApi.BlockTree!.Head?.StateRoot is not null) - { - worldState.StateRoot = getApi.BlockTree.Head.StateRoot; - } - - setApi.TxValidator = new TxValidator(_api.SpecProvider!.ChainId); + IStateReader stateReader = setApi.StateReader!; ITxPool txPool = _api.TxPool = CreateTxPool(); ReceiptCanonicalityMonitor receiptCanonicalityMonitor = new(getApi.BlockTree, getApi.ReceiptStorage, _api.LogManager); @@ -223,10 +89,10 @@ protected virtual Task InitBlockchain() setApi.BlockValidator = CreateBlockValidator(); IChainHeadInfoProvider chainHeadInfoProvider = - new ChainHeadInfoProvider(getApi.SpecProvider, getApi.BlockTree, stateReader); + new ChainHeadInfoProvider(getApi.SpecProvider!, getApi.BlockTree!, stateReader); // TODO: can take the tx sender from plugin here maybe - ITxSigner txSigner = new WalletTxSigner(getApi.Wallet, getApi.SpecProvider.ChainId); + ITxSigner txSigner = new WalletTxSigner(getApi.Wallet, getApi.SpecProvider!.ChainId); TxSealer nonceReservingTxSealer = new(txSigner, getApi.Timestamper); INonceManager nonceManager = new NonceManager(chainHeadInfoProvider.AccountStateProvider); @@ -234,7 +100,7 @@ protected virtual Task InitBlockchain() setApi.TxSender = new TxPoolSender(txPool, nonceReservingTxSealer, nonceManager, getApi.EthereumEcdsa!); setApi.TxPoolInfoProvider = new TxPoolInfoProvider(chainHeadInfoProvider.AccountStateProvider, txPool); - setApi.GasPriceOracle = new GasPriceOracle(getApi.BlockTree, getApi.SpecProvider, _api.LogManager, blocksConfig.MinGasPrice); + setApi.GasPriceOracle = new GasPriceOracle(getApi.BlockTree!, getApi.SpecProvider, _api.LogManager, blocksConfig.MinGasPrice); IBlockProcessor mainBlockProcessor = setApi.MainBlockProcessor = CreateBlockProcessor(); BlockchainProcessor blockchainProcessor = new( @@ -260,8 +126,6 @@ protected virtual Task InitBlockchain() setApi.HealthHintService = CreateHealthHintService(); setApi.BlockProductionPolicy = CreateBlockProductionPolicy(); - InitializeFullPruning(pruningConfig, initConfig, _api, stateReader); - return Task.CompletedTask; } @@ -312,56 +176,6 @@ protected virtual VirtualMachine CreateVirtualMachine() _api.LogManager); } - private static void InitializeFullPruning( - IPruningConfig pruningConfig, - IInitConfig initConfig, - INethermindApi api, - IStateReader stateReader) - { - IPruningTrigger? CreateAutomaticTrigger(string dbPath) - { - long threshold = pruningConfig.FullPruningThresholdMb.MB(); - - switch (pruningConfig.FullPruningTrigger) - { - case FullPruningTrigger.StateDbSize: - return new PathSizePruningTrigger(dbPath, threshold, api.TimerFactory, api.FileSystem); - case FullPruningTrigger.VolumeFreeSpace: - return new DiskFreeSpacePruningTrigger(dbPath, threshold, api.TimerFactory, api.FileSystem); - default: - return null; - } - } - - if (pruningConfig.Mode.IsFull()) - { - IDb stateDb = api.DbProvider!.StateDb; - if (stateDb is IFullPruningDb fullPruningDb) - { - string pruningDbPath = fullPruningDb.GetPath(initConfig.BaseDbPath); - IPruningTrigger? pruningTrigger = CreateAutomaticTrigger(pruningDbPath); - if (pruningTrigger is not null) - { - api.PruningTrigger.Add(pruningTrigger); - } - - IDriveInfo? drive = api.FileSystem.GetDriveInfos(pruningDbPath).FirstOrDefault(); - FullPruner pruner = new(fullPruningDb, api.PruningTrigger, pruningConfig, api.BlockTree!, - stateReader, api.ProcessExit!, ChainSizes.CreateChainSizeInfo(api.ChainSpec.ChainId), - drive, api.LogManager); - api.DisposeStack.Push(pruner); - } - } - } - - private static void InitBlockTraceDumper() - { - BlockTraceDumper.Converters.AddRange(EthereumJsonSerializer.CommonConverters); - BlockTraceDumper.Converters.AddRange(DebugModuleFactory.Converters); - BlockTraceDumper.Converters.AddRange(TraceModuleFactory.Converters); - BlockTraceDumper.Converters.Add(new TxReceiptConverter()); - } - protected virtual IHealthHintService CreateHealthHintService() => new HealthHintService(_api.ChainSpec!); From 77761381b112ff9cd5b02d470d8897c5fe24d36f Mon Sep 17 00:00:00 2001 From: Amirul Ashraf Date: Wed, 8 Nov 2023 14:43:20 +0800 Subject: [PATCH 02/17] Minor cleanup --- .../Nethermind.Api/IApiWithBlockchain.cs | 1 - .../InitializeBlockchainAuRa.cs | 12 ++++++++---- .../InitializationSteps/LoadGenesisBlockAuRa.cs | 15 +++++++-------- .../Nethermind.Init/InitializeStateDb.cs | 3 +-- .../Steps/InitializeBlockchain.cs | 7 ++++--- .../Nethermind.Init/Steps/LoadGenesisBlock.cs | 16 ++++++++-------- .../InitializeBlockchainAuRaMerge.cs | 12 ++++++++---- .../Nethermind.Merge.Plugin/MergePlugin.cs | 1 - .../InitializeBlockchainOptimism.cs | 1 - 9 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs b/src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs index 5bbf5f7886d..fdcd5ad732b 100644 --- a/src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs +++ b/src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs @@ -59,7 +59,6 @@ public interface IApiWithBlockchain : IApiWithStores, IBlockchainBridgeFactory /// DO NOT USE OUTSIDE OF PROCESSING BLOCK CONTEXT! /// IWorldState? WorldState { get; set; } - IKeyValueStoreWithBatching? MainStateDbWithCache { get; set; } IReadOnlyStateProvider? ChainHeadStateProvider { get; set; } IStateReader? StateReader { get; set; } ITransactionProcessor? TransactionProcessor { get; set; } diff --git a/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/InitializeBlockchainAuRa.cs b/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/InitializeBlockchainAuRa.cs index f23c2acfce7..e954ac23906 100644 --- a/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/InitializeBlockchainAuRa.cs +++ b/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/InitializeBlockchainAuRa.cs @@ -25,6 +25,7 @@ using Nethermind.Evm.TransactionProcessing; using Nethermind.Init.Steps; using Nethermind.Logging; +using Nethermind.State; using Nethermind.TxPool; using Nethermind.TxPool.Comparison; @@ -53,7 +54,6 @@ protected override BlockProcessor CreateBlockProcessor() if (_api.RewardCalculatorSource is null) throw new StepDependencyException(nameof(_api.RewardCalculatorSource)); if (_api.TransactionProcessor is null) throw new StepDependencyException(nameof(_api.TransactionProcessor)); if (_api.DbProvider is null) throw new StepDependencyException(nameof(_api.DbProvider)); - if (_api.WorldState is null) throw new StepDependencyException(nameof(_api.WorldState)); if (_api.TxPool is null) throw new StepDependencyException(nameof(_api.TxPool)); if (_api.ReceiptStorage is null) throw new StepDependencyException(nameof(_api.ReceiptStorage)); if (_api.BlockTree is null) throw new StepDependencyException(nameof(_api.BlockTree)); @@ -87,12 +87,14 @@ protected override BlockProcessor CreateBlockProcessor() protected virtual BlockProcessor NewBlockProcessor(AuRaNethermindApi api, ITxFilter txFilter, ContractRewriter contractRewriter) { + IWorldState worldState = _api.WorldState!; + return new AuRaBlockProcessor( _api.SpecProvider, _api.BlockValidator, _api.RewardCalculatorSource.Get(_api.TransactionProcessor), - new BlockProcessor.BlockValidationTransactionsExecutor(_api.TransactionProcessor, _api.WorldState), - _api.WorldState, + new BlockProcessor.BlockValidationTransactionsExecutor(_api.TransactionProcessor, worldState), + worldState, _api.ReceiptStorage, _api.LogManager, _api.BlockTree, @@ -129,8 +131,10 @@ private IAuRaValidator CreateAuRaValidator(IBlockProcessor processor, IReadOnlyT _api.LogManager, chainSpecAuRa.TwoThirdsMajorityTransition); + IWorldState worldState = _api.WorldState!; + IAuRaValidator validator = new AuRaValidatorFactory(_api.AbiEncoder, - _api.WorldState, + worldState, _api.TransactionProcessor, _api.BlockTree, readOnlyTxProcessorSource, diff --git a/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/LoadGenesisBlockAuRa.cs b/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/LoadGenesisBlockAuRa.cs index 06890d4fefc..4a27419705b 100644 --- a/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/LoadGenesisBlockAuRa.cs +++ b/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/LoadGenesisBlockAuRa.cs @@ -6,6 +6,7 @@ using Nethermind.Init.Steps; using Nethermind.Int256; using Nethermind.Specs.Forks; +using Nethermind.State; namespace Nethermind.Consensus.AuRa.InitializationSteps { @@ -18,23 +19,21 @@ public LoadGenesisBlockAuRa(AuRaNethermindApi api) : base(api) _api = api; } - protected override void Load() + protected override void Load(IWorldState worldState) { - CreateSystemAccounts(); - base.Load(); + CreateSystemAccounts(worldState); + base.Load(worldState); } - private void CreateSystemAccounts() + private void CreateSystemAccounts(IWorldState worldState) { if (_api.ChainSpec is null) throw new StepDependencyException(nameof(_api.ChainSpec)); bool hasConstructorAllocation = _api.ChainSpec.Allocations.Values.Any(a => a.Constructor is not null); if (hasConstructorAllocation) { - if (_api.WorldState is null) throw new StepDependencyException(nameof(_api.WorldState)); - - _api.WorldState.CreateAccount(Address.Zero, UInt256.Zero); - _api.WorldState.Commit(Homestead.Instance); + worldState.CreateAccount(Address.Zero, UInt256.Zero); + worldState.Commit(Homestead.Instance); } } } diff --git a/src/Nethermind/Nethermind.Init/InitializeStateDb.cs b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs index 2345f31541f..ecdad6d567f 100644 --- a/src/Nethermind/Nethermind.Init/InitializeStateDb.cs +++ b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs @@ -81,11 +81,10 @@ public Task Execute(CancellationToken cancellationToken) CachingStore cachedStateDb = getApi.DbProvider.StateDb .Cached(Trie.MemoryAllowance.TrieNodeCacheCount); - setApi.MainStateDbWithCache = cachedStateDb; IKeyValueStore codeDb = getApi.DbProvider.CodeDb .WitnessedBy(witnessCollector); - IKeyValueStoreWithBatching stateWitnessedBy = setApi.MainStateDbWithCache.WitnessedBy(witnessCollector); + IKeyValueStoreWithBatching stateWitnessedBy = cachedStateDb.WitnessedBy(witnessCollector); IPersistenceStrategy persistenceStrategy; IPruningStrategy pruningStrategy; if (pruningConfig.Mode.IsMemory()) diff --git a/src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs b/src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs index 69c159b307f..9a0a5684da1 100644 --- a/src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs +++ b/src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs @@ -150,7 +150,6 @@ protected virtual IUnclesValidator CreateUnclesValidator() protected virtual ITransactionProcessor CreateTransactionProcessor() { if (_api.SpecProvider is null) throw new StepDependencyException(nameof(_api.SpecProvider)); - if (_api.WorldState is null) throw new StepDependencyException(nameof(_api.WorldState)); VirtualMachine virtualMachine = CreateVirtualMachine(); @@ -208,12 +207,14 @@ protected virtual BlockProcessor CreateBlockProcessor() if (_api.RewardCalculatorSource is null) throw new StepDependencyException(nameof(_api.RewardCalculatorSource)); if (_api.TransactionProcessor is null) throw new StepDependencyException(nameof(_api.TransactionProcessor)); + IWorldState worldState = _api.WorldState!; + return new BlockProcessor( _api.SpecProvider, _api.BlockValidator, _api.RewardCalculatorSource.Get(_api.TransactionProcessor!), - new BlockProcessor.BlockValidationTransactionsExecutor(_api.TransactionProcessor, _api.WorldState!), - _api.WorldState, + new BlockProcessor.BlockValidationTransactionsExecutor(_api.TransactionProcessor, worldState), + worldState, _api.ReceiptStorage, _api.WitnessCollector, _api.LogManager); diff --git a/src/Nethermind/Nethermind.Init/Steps/LoadGenesisBlock.cs b/src/Nethermind/Nethermind.Init/Steps/LoadGenesisBlock.cs index 7752c47a248..a87d9f01663 100644 --- a/src/Nethermind/Nethermind.Init/Steps/LoadGenesisBlock.cs +++ b/src/Nethermind/Nethermind.Init/Steps/LoadGenesisBlock.cs @@ -38,13 +38,15 @@ public async Task Execute(CancellationToken _) throw new StepDependencyException(); } + IWorldState worldState = _api.WorldState!; + // if we already have a database with blocks then we do not need to load genesis from spec if (_api.BlockTree.Genesis is null) { - Load(); + Load(worldState); } - ValidateGenesisHash(expectedGenesisHash); + ValidateGenesisHash(expectedGenesisHash, worldState); if (!_initConfig.ProcessingEnabled) { @@ -53,11 +55,10 @@ public async Task Execute(CancellationToken _) } } - protected virtual void Load() + protected virtual void Load(IWorldState worldState) { if (_api.ChainSpec is null) throw new StepDependencyException(nameof(_api.ChainSpec)); if (_api.BlockTree is null) throw new StepDependencyException(nameof(_api.BlockTree)); - if (_api.WorldState is null) throw new StepDependencyException(nameof(_api.WorldState)); if (_api.SpecProvider is null) throw new StepDependencyException(nameof(_api.SpecProvider)); if (_api.DbProvider is null) throw new StepDependencyException(nameof(_api.DbProvider)); if (_api.TransactionProcessor is null) throw new StepDependencyException(nameof(_api.TransactionProcessor)); @@ -65,7 +66,7 @@ protected virtual void Load() Block genesis = new GenesisLoader( _api.ChainSpec, _api.SpecProvider, - _api.WorldState, + worldState, _api.TransactionProcessor) .Load(); @@ -92,15 +93,14 @@ void GenesisProcessed(object? sender, BlockEventArgs args) /// If is null then it means that we do not care about the genesis hash (e.g. in some quick testing of private chains)/> /// /// - private void ValidateGenesisHash(Hash256? expectedGenesisHash) + private void ValidateGenesisHash(Hash256? expectedGenesisHash, IWorldState worldState) { - if (_api.WorldState is null) throw new StepDependencyException(nameof(_api.WorldState)); if (_api.BlockTree is null) throw new StepDependencyException(nameof(_api.BlockTree)); BlockHeader genesis = _api.BlockTree.Genesis ?? throw new NullReferenceException("Genesis block is null"); if (expectedGenesisHash is not null && genesis.Hash != expectedGenesisHash) { - if (_logger.IsWarn) _logger.Warn(_api.WorldState.DumpState()); + if (_logger.IsWarn) _logger.Warn(worldState.DumpState()); if (_logger.IsWarn) _logger.Warn(genesis.ToString(BlockHeader.Format.Full)); if (_logger.IsError) _logger.Error($"Unexpected genesis hash, expected {expectedGenesisHash}, but was {genesis.Hash}"); } diff --git a/src/Nethermind/Nethermind.Merge.AuRa/InitializationSteps/InitializeBlockchainAuRaMerge.cs b/src/Nethermind/Nethermind.Merge.AuRa/InitializationSteps/InitializeBlockchainAuRaMerge.cs index 7bdf6ee9311..cfecb0722b6 100644 --- a/src/Nethermind/Nethermind.Merge.AuRa/InitializationSteps/InitializeBlockchainAuRaMerge.cs +++ b/src/Nethermind/Nethermind.Merge.AuRa/InitializationSteps/InitializeBlockchainAuRaMerge.cs @@ -5,8 +5,10 @@ using Nethermind.Consensus.AuRa.InitializationSteps; using Nethermind.Consensus.Processing; using Nethermind.Consensus.Transactions; +using Nethermind.Evm.TransactionProcessing; using Nethermind.Init.Steps; using Nethermind.Merge.AuRa.Withdrawals; +using Nethermind.State; namespace Nethermind.Merge.AuRa.InitializationSteps { @@ -21,19 +23,21 @@ public InitializeBlockchainAuRaMerge(AuRaNethermindApi api) : base(api) protected override BlockProcessor NewBlockProcessor(AuRaNethermindApi api, ITxFilter txFilter, ContractRewriter contractRewriter) { - var withdrawalContractFactory = new WithdrawalContractFactory(_api.ChainSpec!.AuRa, _api.AbiEncoder); + WithdrawalContractFactory withdrawalContractFactory = new WithdrawalContractFactory(_api.ChainSpec!.AuRa, _api.AbiEncoder); + IWorldState worldState = _api.WorldState!; + ITransactionProcessor transactionProcessor = _api.TransactionProcessor!; return new AuRaMergeBlockProcessor( _api.SpecProvider!, _api.BlockValidator!, _api.RewardCalculatorSource!.Get(_api.TransactionProcessor!), - new BlockProcessor.BlockValidationTransactionsExecutor(_api.TransactionProcessor!, _api.WorldState!), - _api.WorldState!, + new BlockProcessor.BlockValidationTransactionsExecutor(transactionProcessor!, worldState), + worldState, _api.ReceiptStorage!, _api.LogManager, _api.BlockTree!, new AuraWithdrawalProcessor( - withdrawalContractFactory.Create(_api.TransactionProcessor!), _api.LogManager), + withdrawalContractFactory.Create(transactionProcessor!), _api.LogManager), txFilter, GetGasLimitCalculator(), contractRewriter diff --git a/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs b/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs index 3af26b08cef..186a61c71f2 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs @@ -257,7 +257,6 @@ public Task InitRpcModules() { if (_api.BlockTree is null) throw new ArgumentNullException(nameof(_api.BlockTree)); if (_api.BlockchainProcessor is null) throw new ArgumentNullException(nameof(_api.BlockchainProcessor)); - if (_api.WorldState is null) throw new ArgumentNullException(nameof(_api.WorldState)); if (_api.HeaderValidator is null) throw new ArgumentNullException(nameof(_api.HeaderValidator)); if (_api.EthSyncingInfo is null) throw new ArgumentNullException(nameof(_api.EthSyncingInfo)); if (_api.Sealer is null) throw new ArgumentNullException(nameof(_api.Sealer)); diff --git a/src/Nethermind/Nethermind.Optimism/InitializeBlockchainOptimism.cs b/src/Nethermind/Nethermind.Optimism/InitializeBlockchainOptimism.cs index fbf648b45aa..6920765a0dc 100644 --- a/src/Nethermind/Nethermind.Optimism/InitializeBlockchainOptimism.cs +++ b/src/Nethermind/Nethermind.Optimism/InitializeBlockchainOptimism.cs @@ -40,7 +40,6 @@ protected override Task InitBlockchain() protected override ITransactionProcessor CreateTransactionProcessor() { if (_api.SpecProvider is null) throw new StepDependencyException(nameof(_api.SpecProvider)); - if (_api.WorldState is null) throw new StepDependencyException(nameof(_api.WorldState)); if (_api.SpecHelper is null) throw new StepDependencyException(nameof(_api.SpecHelper)); if (_api.L1CostHelper is null) throw new StepDependencyException(nameof(_api.L1CostHelper)); From 5a6c6a3d640c313b91df96d860160574aff94eb4 Mon Sep 17 00:00:00 2001 From: Amirul Ashraf Date: Wed, 8 Nov 2023 15:09:41 +0800 Subject: [PATCH 03/17] World state factory --- .../Nethermind.Analytics/AnalyticsPlugin.cs | 2 +- .../Nethermind.Api/IApiWithBlockchain.cs | 2 +- .../Nethermind.Api/NethermindApi.cs | 1 + .../CliquePlugin.cs | 2 +- .../NethDevPlugin.cs | 2 +- .../Nethermind.Init/InitializeStateDb.cs | 27 ++++++-------- .../Steps/InitializeBlockchain.cs | 5 ++- .../Steps/RegisterRpcModules.cs | 4 +- .../Nethermind.Merge.Plugin/MergePlugin.cs | 4 +- src/Nethermind/Nethermind.Mev/MevPlugin.cs | 2 +- .../Nethermind.State/IWorldStateFactory.cs | 10 +++++ .../ReadOnlyWorldStateFactory.cs | 37 +++++++++++++++++++ .../Nethermind.State/StateReader.cs | 4 +- .../Nethermind.State/WorldStateFactory.cs | 36 ++++++++++++++++++ 14 files changed, 110 insertions(+), 28 deletions(-) create mode 100644 src/Nethermind/Nethermind.State/IWorldStateFactory.cs create mode 100644 src/Nethermind/Nethermind.State/ReadOnlyWorldStateFactory.cs create mode 100644 src/Nethermind/Nethermind.State/WorldStateFactory.cs diff --git a/src/Nethermind/Nethermind.Analytics/AnalyticsPlugin.cs b/src/Nethermind/Nethermind.Analytics/AnalyticsPlugin.cs index 503ce7ffc4c..a3447dff5f9 100644 --- a/src/Nethermind/Nethermind.Analytics/AnalyticsPlugin.cs +++ b/src/Nethermind/Nethermind.Analytics/AnalyticsPlugin.cs @@ -91,7 +91,7 @@ public Task InitRpcModules() { var (getFromAPi, _) = _api.ForRpc; AnalyticsRpcModule analyticsRpcModule = new( - getFromAPi.BlockTree, getFromAPi.StateReader, getFromAPi.LogManager); + getFromAPi.BlockTree, getFromAPi.ReadOnlyWorldStateFactory!.CreateStateReader(), getFromAPi.LogManager); getFromAPi.RpcModuleProvider.Register(new SingletonModulePool(analyticsRpcModule)); return Task.CompletedTask; } diff --git a/src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs b/src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs index fdcd5ad732b..6ae50ce9143 100644 --- a/src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs +++ b/src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs @@ -60,7 +60,7 @@ public interface IApiWithBlockchain : IApiWithStores, IBlockchainBridgeFactory /// IWorldState? WorldState { get; set; } IReadOnlyStateProvider? ChainHeadStateProvider { get; set; } - IStateReader? StateReader { get; set; } + IWorldStateFactory? ReadOnlyWorldStateFactory { get; set; } ITransactionProcessor? TransactionProcessor { get; set; } ITrieStore? TrieStore { get; set; } ITxSender? TxSender { get; set; } diff --git a/src/Nethermind/Nethermind.Api/NethermindApi.cs b/src/Nethermind/Nethermind.Api/NethermindApi.cs index d83305c6cb8..ea8f55f9a83 100644 --- a/src/Nethermind/Nethermind.Api/NethermindApi.cs +++ b/src/Nethermind/Nethermind.Api/NethermindApi.cs @@ -191,6 +191,7 @@ public ISealEngine SealEngine public ISyncServer? SyncServer { get; set; } public IWorldState? WorldState { get; set; } public IReadOnlyStateProvider? ChainHeadStateProvider { get; set; } + public IWorldStateFactory? ReadOnlyWorldStateFactory { get; set; } public IStateReader? StateReader { get; set; } public IStaticNodesManager? StaticNodesManager { get; set; } public ITimestamper Timestamper { get; } = Core.Timestamper.Default; diff --git a/src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs b/src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs index 4180bd2a69a..cf2fd70c78f 100644 --- a/src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs +++ b/src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs @@ -116,7 +116,7 @@ public Task InitBlockProducer(IBlockProductionTrigger? blockProd readOnlyBlockTree, producerProcessor, getFromApi.BlockPreprocessor, - getFromApi.StateReader, + getFromApi.ReadOnlyWorldStateFactory!.CreateStateReader(), getFromApi.LogManager, BlockchainProcessor.Options.NoReceipts); diff --git a/src/Nethermind/Nethermind.Consensus.Ethash/NethDevPlugin.cs b/src/Nethermind/Nethermind.Consensus.Ethash/NethDevPlugin.cs index 60fa34be699..ca37ef3f571 100644 --- a/src/Nethermind/Nethermind.Consensus.Ethash/NethDevPlugin.cs +++ b/src/Nethermind/Nethermind.Consensus.Ethash/NethDevPlugin.cs @@ -86,7 +86,7 @@ public Task InitBlockProducer(IBlockProductionTrigger? blockProd readOnlyBlockTree, producerProcessor, getFromApi.BlockPreprocessor, - getFromApi.StateReader, + getFromApi.ReadOnlyWorldStateFactory!.CreateStateReader(), getFromApi.LogManager, BlockchainProcessor.Options.NoReceipts); diff --git a/src/Nethermind/Nethermind.Init/InitializeStateDb.cs b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs index ecdad6d567f..dd42984fc58 100644 --- a/src/Nethermind/Nethermind.Init/InitializeStateDb.cs +++ b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs @@ -10,9 +10,6 @@ using Nethermind.Blockchain; using Nethermind.Blockchain.FullPruning; using Nethermind.Blockchain.Synchronization; -using Nethermind.Config; -using Nethermind.Consensus; -using Nethermind.Consensus.Comparers; using Nethermind.Core; using Nethermind.Core.Crypto; using Nethermind.Core.Extensions; @@ -128,6 +125,8 @@ public Task Execute(CancellationToken cancellationToken) codeDb, getApi.LogManager); + WorldStateFactory stateFactory = new WorldStateFactory(trieStore, codeDb, getApi.LogManager); + if (pruningConfig.Mode.IsFull()) { IFullPruningDb fullPruningDb = (IFullPruningDb)getApi.DbProvider!.StateDb; @@ -138,18 +137,20 @@ public Task Execute(CancellationToken cancellationToken) }; } + // TODO: Don't forget this TrieStoreBoundaryWatcher trieStoreBoundaryWatcher = new(trieStore, _api.BlockTree!, _api.LogManager); getApi.DisposeStack.Push(trieStoreBoundaryWatcher); getApi.DisposeStack.Push(trieStore); - ITrieStore readOnlyTrieStore = setApi.ReadOnlyTrieStore = trieStore.AsReadOnly(cachedStateDb); - + IReadOnlyTrieStore readOnlyTrieStore = setApi.ReadOnlyTrieStore = trieStore.AsReadOnly(cachedStateDb); ReadOnlyDbProvider readOnly = new(getApi.DbProvider, false); - IStateReader stateReader = setApi.StateReader = new StateReader(readOnlyTrieStore, readOnly.GetDb(DbNames.Code), getApi.LogManager); + IWorldStateFactory readOnlyStateFactory = setApi.ReadOnlyWorldStateFactory = new ReadOnlyWorldStateFactory( + readOnlyTrieStore, + readOnly.GetDb(DbNames.Code), + getApi.LogManager); - setApi.TransactionComparerProvider = new TransactionComparerProvider(getApi.SpecProvider!, getApi.BlockTree.AsReadOnly()); - setApi.ChainHeadStateProvider = new ChainHeadReadOnlyStateProvider(getApi.BlockTree, stateReader); + setApi.ChainHeadStateProvider = new ChainHeadReadOnlyStateProvider(getApi.BlockTree, readOnlyStateFactory.CreateStateReader()); worldState.StateRoot = getApi.BlockTree!.Head?.StateRoot ?? Keccak.EmptyTreeHash; @@ -160,11 +161,8 @@ public Task Execute(CancellationToken cancellationToken) try { _logger!.Info("Collecting trie stats and verifying that no nodes are missing..."); - TrieStore noPruningStore = new(stateWitnessedBy, No.Pruning, Persist.EveryBlock, getApi.LogManager); - IWorldState diagStateProvider = new WorldState(noPruningStore, codeDb, getApi.LogManager) - { - StateRoot = getApi.BlockTree!.Head?.StateRoot ?? Keccak.EmptyTreeHash - }; + IWorldState diagStateProvider = readOnlyStateFactory.CreateWorldState(); + diagStateProvider.StateRoot = getApi.BlockTree!.Head?.StateRoot ?? Keccak.EmptyTreeHash; TrieStats stats = diagStateProvider.CollectStats(getApi.DbProvider.CodeDb, _api.LogManager); _logger.Info($"Starting from {getApi.BlockTree.Head?.Number} {getApi.BlockTree.Head?.StateRoot}{Environment.NewLine}" + stats); } @@ -181,7 +179,7 @@ public Task Execute(CancellationToken cancellationToken) worldState.StateRoot = getApi.BlockTree.Head.StateRoot; } - InitializeFullPruning(pruningConfig, initConfig, _api, stateReader); + InitializeFullPruning(pruningConfig, initConfig, _api, readOnlyStateFactory.CreateStateReader()); return Task.CompletedTask; } @@ -235,5 +233,4 @@ private static void InitializeFullPruning( } } } - } diff --git a/src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs b/src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs index 9a0a5684da1..5213ff5b2f1 100644 --- a/src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs +++ b/src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs @@ -64,12 +64,13 @@ public async Task Execute(CancellationToken _) protected virtual Task InitBlockchain() { (IApiWithStores getApi, IApiWithBlockchain setApi) = _api.ForBlockchain; + setApi.TransactionComparerProvider = new TransactionComparerProvider(getApi.SpecProvider!, getApi.BlockTree!.AsReadOnly()); setApi.TxValidator = new TxValidator(_api.SpecProvider!.ChainId); IInitConfig initConfig = getApi.Config(); IBlocksConfig blocksConfig = getApi.Config(); - IStateReader stateReader = setApi.StateReader!; + IStateReader stateReader = setApi.ReadOnlyWorldStateFactory!.CreateStateReader(); ITxPool txPool = _api.TxPool = CreateTxPool(); ReceiptCanonicalityMonitor receiptCanonicalityMonitor = new(getApi.BlockTree, getApi.ReceiptStorage, _api.LogManager); @@ -184,7 +185,7 @@ protected virtual IBlockProductionPolicy CreateBlockProductionPolicy() => protected virtual TxPool.TxPool CreateTxPool() => new(_api.EthereumEcdsa!, _api.BlobTxStorage ?? NullBlobTxStorage.Instance, - new ChainHeadInfoProvider(_api.SpecProvider!, _api.BlockTree!, _api.StateReader!), + new ChainHeadInfoProvider(_api.SpecProvider!, _api.BlockTree!, _api.ReadOnlyWorldStateFactory!.CreateStateReader()!), _api.Config(), _api.TxValidator!, _api.LogManager, diff --git a/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs b/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs index 0eb289dd245..7fb03ba4f26 100644 --- a/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs +++ b/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs @@ -60,7 +60,7 @@ public virtual async Task Execute(CancellationToken cancellationToken) if (_api.SpecProvider is null) throw new StepDependencyException(nameof(_api.SpecProvider)); if (_api.SyncModeSelector is null) throw new StepDependencyException(nameof(_api.SyncModeSelector)); if (_api.TxSender is null) throw new StepDependencyException(nameof(_api.TxSender)); - if (_api.StateReader is null) throw new StepDependencyException(nameof(_api.StateReader)); + if (_api.ReadOnlyWorldStateFactory is null) throw new StepDependencyException(nameof(_api.ReadOnlyWorldStateFactory)); if (_api.PeerManager is null) throw new StepDependencyException(nameof(_api.PeerManager)); if (jsonRpcConfig.Enabled) @@ -98,7 +98,7 @@ public virtual async Task Execute(CancellationToken cancellationToken) _api.BlockTree, rpcConfig, _api.LogManager, - _api.StateReader, + _api.ReadOnlyWorldStateFactory.CreateStateReader(), _api, _api.SpecProvider, _api.ReceiptStorage, diff --git a/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs b/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs index 186a61c71f2..05d985fece1 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs @@ -263,7 +263,7 @@ public Task InitRpcModules() if (_api.BlockValidator is null) throw new ArgumentNullException(nameof(_api.BlockValidator)); if (_api.BlockProcessingQueue is null) throw new ArgumentNullException(nameof(_api.BlockProcessingQueue)); if (_api.SpecProvider is null) throw new ArgumentNullException(nameof(_api.SpecProvider)); - if (_api.StateReader is null) throw new ArgumentNullException(nameof(_api.StateReader)); + if (_api.ReadOnlyWorldStateFactory is null) throw new ArgumentNullException(nameof(_api.ReadOnlyWorldStateFactory)); if (_beaconPivot is null) throw new ArgumentNullException(nameof(_beaconPivot)); if (_beaconSync is null) throw new ArgumentNullException(nameof(_beaconSync)); if (_blockProductionTrigger is null) throw new ArgumentNullException(nameof(_blockProductionTrigger)); @@ -289,7 +289,7 @@ public Task InitRpcModules() { DefaultHttpClient httpClient = new(new HttpClient(), _api.EthereumJsonSerializer, _api.LogManager, retryDelayMilliseconds: 100); IBoostRelay boostRelay = new BoostRelay(httpClient, _mergeConfig.BuilderRelayUrl); - BoostBlockImprovementContextFactory boostBlockImprovementContextFactory = new(_blockProductionTrigger, TimeSpan.FromSeconds(_blocksConfig.SecondsPerSlot), boostRelay, _api.StateReader); + BoostBlockImprovementContextFactory boostBlockImprovementContextFactory = new(_blockProductionTrigger, TimeSpan.FromSeconds(_blocksConfig.SecondsPerSlot), boostRelay, _api.ReadOnlyWorldStateFactory.CreateStateReader()); improvementContextFactory = boostBlockImprovementContextFactory; } diff --git a/src/Nethermind/Nethermind.Mev/MevPlugin.cs b/src/Nethermind/Nethermind.Mev/MevPlugin.cs index 96aaf787dd3..06da91a3bcb 100644 --- a/src/Nethermind/Nethermind.Mev/MevPlugin.cs +++ b/src/Nethermind/Nethermind.Mev/MevPlugin.cs @@ -115,7 +115,7 @@ public Task InitRpcModules() MevModuleFactory mevModuleFactory = new(rpcConfig, BundlePool, getFromApi.BlockTree!, - getFromApi.StateReader!, + getFromApi.ReadOnlyWorldStateFactory!.CreateStateReader(), TracerFactory, getFromApi.SpecProvider!, getFromApi.EngineSigner); diff --git a/src/Nethermind/Nethermind.State/IWorldStateFactory.cs b/src/Nethermind/Nethermind.State/IWorldStateFactory.cs new file mode 100644 index 00000000000..7657c790488 --- /dev/null +++ b/src/Nethermind/Nethermind.State/IWorldStateFactory.cs @@ -0,0 +1,10 @@ +// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited +// SPDX-License-Identifier: LGPL-3.0-only + +namespace Nethermind.State; + +public interface IWorldStateFactory +{ + IWorldState CreateWorldState(); + IStateReader CreateStateReader(); +} diff --git a/src/Nethermind/Nethermind.State/ReadOnlyWorldStateFactory.cs b/src/Nethermind/Nethermind.State/ReadOnlyWorldStateFactory.cs new file mode 100644 index 00000000000..f075635add1 --- /dev/null +++ b/src/Nethermind/Nethermind.State/ReadOnlyWorldStateFactory.cs @@ -0,0 +1,37 @@ +// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited +// SPDX-License-Identifier: LGPL-3.0-only + +using Nethermind.Core; +using Nethermind.Db; +using Nethermind.Logging; +using Nethermind.Trie.Pruning; + +namespace Nethermind.State; + +public class ReadOnlyWorldStateFactory: IWorldStateFactory +{ + private IReadOnlyTrieStore? _readOnlyTrieStore; + private IKeyValueStore _codeDb; + private ILogManager _logManager; + + public ReadOnlyWorldStateFactory( + IReadOnlyTrieStore? readOnlyTrieStore, + IKeyValueStore codeDb, + ILogManager logManager + ) + { + _readOnlyTrieStore = readOnlyTrieStore; + _codeDb = codeDb; + _logManager = logManager; + } + + public IWorldState CreateWorldState() + { + return new WorldState(_readOnlyTrieStore, _codeDb, _logManager); + } + + public IStateReader CreateStateReader() + { + return new StateReader(_readOnlyTrieStore, _codeDb, _logManager); + } +} diff --git a/src/Nethermind/Nethermind.State/StateReader.cs b/src/Nethermind/Nethermind.State/StateReader.cs index 1fb7463f0c9..c6544eb0bef 100644 --- a/src/Nethermind/Nethermind.State/StateReader.cs +++ b/src/Nethermind/Nethermind.State/StateReader.cs @@ -15,12 +15,12 @@ namespace Nethermind.State { public class StateReader : IStateReader { - private readonly IDb _codeDb; + private readonly IKeyValueStore _codeDb; private readonly ILogger _logger; private readonly StateTree _state; private readonly StorageTree _storage; - public StateReader(ITrieStore? trieStore, IDb? codeDb, ILogManager? logManager) + public StateReader(ITrieStore? trieStore, IKeyValueStore? codeDb, ILogManager? logManager) { _logger = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager)); _codeDb = codeDb ?? throw new ArgumentNullException(nameof(codeDb)); diff --git a/src/Nethermind/Nethermind.State/WorldStateFactory.cs b/src/Nethermind/Nethermind.State/WorldStateFactory.cs new file mode 100644 index 00000000000..73200af4a3e --- /dev/null +++ b/src/Nethermind/Nethermind.State/WorldStateFactory.cs @@ -0,0 +1,36 @@ +// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited +// SPDX-License-Identifier: LGPL-3.0-only + +using Nethermind.Core; +using Nethermind.Logging; +using Nethermind.Trie.Pruning; + +namespace Nethermind.State; + +public class WorldStateFactory: IWorldStateFactory +{ + private ITrieStore _trieStore; + private IKeyValueStore _codeDb; + private ILogManager _logManager; + + public WorldStateFactory( + TrieStore trieStore, + IKeyValueStore codeDb, + ILogManager logManager + ) + { + _trieStore = trieStore; + _codeDb = codeDb; + _logManager = logManager; + } + + public IWorldState CreateWorldState() + { + return new WorldState(_trieStore, _codeDb, _logManager); + } + + public IStateReader CreateStateReader() + { + return new StateReader(_trieStore, _codeDb, _logManager); + } +} From 5d45838127465e9e40c064e31ce07af43a771ee6 Mon Sep 17 00:00:00 2001 From: Amirul Ashraf Date: Wed, 8 Nov 2023 18:24:07 +0800 Subject: [PATCH 04/17] Don't take db provider or trie store --- ...sts.TestAccountAbstractionRpcBlockchain.cs | 5 ++-- .../AccountAbstractionPlugin.cs | 3 +-- .../Nethermind.Api/NethermindApi.cs | 6 +---- .../AuRaContractGasLimitOverrideTests.cs | 3 +-- .../Contract/TxPriorityContractTests.cs | 2 +- .../Transactions/TxCertifierFilterTests.cs | 5 ++-- .../Transactions/TxPermissionFilterTest.cs | 5 ++-- .../InitializeBlockchainAuRa.cs | 2 +- .../StartBlockProducerAuRa.cs | 13 +++++----- .../CliquePlugin.cs | 6 ++--- .../NethDevPlugin.cs | 5 +--- .../Processing/OneTimeProcessor.cs | 10 ++++---- .../Processing/ReadOnlyChainProcessingEnv.cs | 4 ++-- .../Processing/ReadOnlyTxProcessingEnv.cs | 22 +++++++---------- .../ReadOnlyTxProcessingEnvFactory.cs | 17 ++++++------- .../Producers/BlockProducerEnvFactory.cs | 19 ++++++--------- .../Blockchain/TestBlockchain.cs | 5 ++-- .../BlockchainBridgeTests.cs | 19 +++++++++++---- .../Nethermind.Init/InitializeStateDb.cs | 3 +-- .../Steps/InitializeBlockProducer.cs | 4 ++-- .../Steps/RegisterRpcModules.cs | 6 ++--- .../Modules/Proof/ProofRpcModuleTests.cs | 10 ++++---- .../Modules/TestRpcBlockchain.cs | 3 +-- .../Modules/TraceRpcModuleTests.cs | 5 ++-- .../Modules/DebugModule/DebugModuleFactory.cs | 9 ++++--- .../Modules/Proof/ProofModuleFactory.cs | 14 +++++------ .../Modules/Trace/TraceModuleFactory.cs | 14 +++++------ .../AuRaMergeEngineModuleTests.cs | 4 ++-- .../AuRaMergeBlockProducerEnvFactory.cs | 11 ++++----- .../Nethermind.Merge.AuRa/AuRaMergePlugin.cs | 3 +-- .../EngineModuleTests.Setup.cs | 3 +-- .../MergePluginTests.cs | 3 +-- .../MevRpcModuleTests.TestMevRpcBlockchain.cs | 6 ++--- .../Nethermind.Mev/Execution/TracerFactory.cs | 14 +++++------ src/Nethermind/Nethermind.Mev/MevPlugin.cs | 3 +-- .../InitializeBlockProducerOptimism.cs | 4 ++-- .../OptimismBlockProducerEnvFactory.cs | 12 +++++----- .../Ethereum/ContextWithMocks.cs | 10 ++++++-- .../Nethermind.State/IWorldStateFactory.cs | 3 +++ .../ReadOnlyWorldStateFactory.cs | 24 +++++++++++++++---- .../Nethermind.State/WorldStateFactory.cs | 6 +++++ 41 files changed, 163 insertions(+), 162 deletions(-) diff --git a/src/Nethermind/Nethermind.AccountAbstraction.Test/AccountAbstractionRpcModuleTests.TestAccountAbstractionRpcBlockchain.cs b/src/Nethermind/Nethermind.AccountAbstraction.Test/AccountAbstractionRpcModuleTests.TestAccountAbstractionRpcBlockchain.cs index f14e0ae0e5c..56279c499be 100644 --- a/src/Nethermind/Nethermind.AccountAbstraction.Test/AccountAbstractionRpcModuleTests.TestAccountAbstractionRpcBlockchain.cs +++ b/src/Nethermind/Nethermind.AccountAbstraction.Test/AccountAbstractionRpcModuleTests.TestAccountAbstractionRpcBlockchain.cs @@ -99,9 +99,8 @@ protected override IBlockProducer CreateTestBlockProducer(TxPoolTxSource txPoolT SpecProvider.UpdateMergeTransitionInfo(1, 0); BlockProducerEnvFactory blockProducerEnvFactory = new BlockProducerEnvFactory( - DbProvider, + ReadOnlyWorldStateFactory, BlockTree, - ReadOnlyTrieStore, SpecProvider, BlockValidator, NoBlockRewards.Instance, @@ -212,7 +211,7 @@ protected override BlockProcessor CreateBlockProcessor() UserOperationSimulator[entryPoint] = new( UserOperationTxBuilder[entryPoint], ReadOnlyState, - new ReadOnlyTxProcessingEnvFactory(DbProvider, ReadOnlyTrieStore, BlockTree, SpecProvider, LogManager), + new ReadOnlyTxProcessingEnvFactory(ReadOnlyWorldStateFactory, BlockTree, SpecProvider, LogManager), EntryPointContractAbi, entryPoint!, WhitelistedPayamsters, diff --git a/src/Nethermind/Nethermind.AccountAbstraction/AccountAbstractionPlugin.cs b/src/Nethermind/Nethermind.AccountAbstraction/AccountAbstractionPlugin.cs index df23fac7e6b..d95c3308e80 100644 --- a/src/Nethermind/Nethermind.AccountAbstraction/AccountAbstractionPlugin.cs +++ b/src/Nethermind/Nethermind.AccountAbstraction/AccountAbstractionPlugin.cs @@ -121,8 +121,7 @@ private UserOperationSimulator UserOperationSimulator(Address entryPoint) var (getFromApi, _) = _nethermindApi!.ForProducer; ReadOnlyTxProcessingEnvFactory readOnlyTxProcessingEnvFactory = new( - getFromApi.DbProvider, - getFromApi.ReadOnlyTrieStore, + getFromApi.ReadOnlyWorldStateFactory!, getFromApi.BlockTree, getFromApi.SpecProvider, getFromApi.LogManager); diff --git a/src/Nethermind/Nethermind.Api/NethermindApi.cs b/src/Nethermind/Nethermind.Api/NethermindApi.cs index ea8f55f9a83..c25df50a58e 100644 --- a/src/Nethermind/Nethermind.Api/NethermindApi.cs +++ b/src/Nethermind/Nethermind.Api/NethermindApi.cs @@ -70,17 +70,13 @@ public NethermindApi(IConfigProvider configProvider, IJsonSerializer jsonSeriali DisposeStack.Push(CryptoRandom); } - private IReadOnlyDbProvider? _readOnlyDbProvider; - public IBlockchainBridge CreateBlockchainBridge() { ReadOnlyBlockTree readOnlyTree = BlockTree!.AsReadOnly(); - LazyInitializer.EnsureInitialized(ref _readOnlyDbProvider, () => new ReadOnlyDbProvider(DbProvider, false)); // TODO: reuse the same trie cache here ReadOnlyTxProcessingEnv readOnlyTxProcessingEnv = new( - _readOnlyDbProvider, - ReadOnlyTrieStore, + ReadOnlyWorldStateFactory!, readOnlyTree, SpecProvider, LogManager); diff --git a/src/Nethermind/Nethermind.AuRa.Test/Contract/AuRaContractGasLimitOverrideTests.cs b/src/Nethermind/Nethermind.AuRa.Test/Contract/AuRaContractGasLimitOverrideTests.cs index c5f1a04d732..708ef254478 100644 --- a/src/Nethermind/Nethermind.AuRa.Test/Contract/AuRaContractGasLimitOverrideTests.cs +++ b/src/Nethermind/Nethermind.AuRa.Test/Contract/AuRaContractGasLimitOverrideTests.cs @@ -83,8 +83,7 @@ protected override BlockProcessor CreateBlockProcessor() KeyValuePair blockGasLimitContractTransition = ChainSpec.AuRa.BlockGasLimitContractTransitions.First(); BlockGasLimitContract gasLimitContract = new(AbiEncoder.Instance, blockGasLimitContractTransition.Value, blockGasLimitContractTransition.Key, new ReadOnlyTxProcessingEnv( - DbProvider, - new TrieStore(DbProvider.StateDb, LimboLogs.Instance).AsReadOnly(), + ReadOnlyWorldStateFactory, BlockTree, SpecProvider, LimboLogs.Instance)); GasLimitOverrideCache = new AuRaContractGasLimitOverride.Cache(); diff --git a/src/Nethermind/Nethermind.AuRa.Test/Contract/TxPriorityContractTests.cs b/src/Nethermind/Nethermind.AuRa.Test/Contract/TxPriorityContractTests.cs index 3e0fc34aa08..78e24ed4cb3 100644 --- a/src/Nethermind/Nethermind.AuRa.Test/Contract/TxPriorityContractTests.cs +++ b/src/Nethermind/Nethermind.AuRa.Test/Contract/TxPriorityContractTests.cs @@ -254,7 +254,7 @@ protected override TxPoolTxSource CreateTxPoolTxSource() TxPoolTxSource txPoolTxSource = base.CreateTxPoolTxSource(); TxPriorityContract = new TxPriorityContract(AbiEncoder.Instance, TestItem.AddressA, - new ReadOnlyTxProcessingEnv(DbProvider, TrieStore.AsReadOnly(), BlockTree, SpecProvider, LimboLogs.Instance)); + new ReadOnlyTxProcessingEnv(ReadOnlyWorldStateFactory, BlockTree, SpecProvider, LimboLogs.Instance)); Priorities = new DictionaryContractDataStore( new TxPriorityContract.DestinationSortedListContractDataStoreCollection(), diff --git a/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxCertifierFilterTests.cs b/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxCertifierFilterTests.cs index 887961a3c6d..4d907fd786c 100644 --- a/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxCertifierFilterTests.cs +++ b/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxCertifierFilterTests.cs @@ -137,9 +137,8 @@ protected override BlockProcessor CreateBlockProcessor() { AbiEncoder abiEncoder = AbiEncoder.Instance; ReadOnlyTransactionProcessorSource = new ReadOnlyTxProcessingEnv( - DbProvider, - new TrieStore(DbProvider.StateDb, LimboLogs.Instance).AsReadOnly(), - BlockTree, SpecProvider, + ReadOnlyWorldStateFactory, + BlockTree, SpecProvider, LimboLogs.Instance); RegisterContract = new RegisterContract(abiEncoder, ChainSpec.Parameters.Registrar, ReadOnlyTransactionProcessorSource); CertifierContract = new CertifierContract( diff --git a/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxPermissionFilterTest.cs b/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxPermissionFilterTest.cs index 27a9fd4e8c2..cea9fcce901 100644 --- a/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxPermissionFilterTest.cs +++ b/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxPermissionFilterTest.cs @@ -276,14 +276,13 @@ protected override BlockProcessor CreateBlockProcessor() IReadOnlyTrieStore trieStore = new TrieStore(DbProvider.StateDb, LimboLogs.Instance).AsReadOnly(); IReadOnlyTxProcessorSource txProcessorSource = new ReadOnlyTxProcessingEnv( - DbProvider, - trieStore, + ReadOnlyWorldStateFactory, BlockTree, SpecProvider, LimboLogs.Instance); VersionedTransactionPermissionContract transactionPermissionContract = new(AbiEncoder.Instance, _contractAddress, 1, - new ReadOnlyTxProcessingEnv(DbProvider, trieStore, BlockTree, SpecProvider, LimboLogs.Instance), TransactionPermissionContractVersions, LimboLogs.Instance, SpecProvider); + new ReadOnlyTxProcessingEnv(ReadOnlyWorldStateFactory, BlockTree, SpecProvider, LimboLogs.Instance), TransactionPermissionContractVersions, LimboLogs.Instance, SpecProvider); TxPermissionFilterCache = new PermissionBasedTxFilter.Cache(); PermissionBasedTxFilter = new PermissionBasedTxFilter(transactionPermissionContract, TxPermissionFilterCache, LimboLogs.Instance); diff --git a/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/InitializeBlockchainAuRa.cs b/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/InitializeBlockchainAuRa.cs index e954ac23906..38dfaf43cb0 100644 --- a/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/InitializeBlockchainAuRa.cs +++ b/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/InitializeBlockchainAuRa.cs @@ -106,7 +106,7 @@ protected virtual BlockProcessor NewBlockProcessor(AuRaNethermindApi api, ITxFil } protected ReadOnlyTxProcessingEnv CreateReadOnlyTransactionProcessorSource() => - new ReadOnlyTxProcessingEnv(_api.DbProvider, _api.ReadOnlyTrieStore, _api.BlockTree, _api.SpecProvider, _api.LogManager); + new ReadOnlyTxProcessingEnv(_api.ReadOnlyWorldStateFactory!, _api.BlockTree, _api.SpecProvider, _api.LogManager); protected override IHealthHintService CreateHealthHintService() => new AuraHealthHintService(_auRaStepCalculator, _api.ValidatorStore); diff --git a/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/StartBlockProducerAuRa.cs b/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/StartBlockProducerAuRa.cs index c8bd56c76e6..a3b909d5403 100644 --- a/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/StartBlockProducerAuRa.cs +++ b/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/StartBlockProducerAuRa.cs @@ -171,7 +171,7 @@ internal TxPoolTxSource CreateTxPoolTxSource(ReadOnlyTxProcessingEnv processingE { // We need special one for TxPriority as its following Head separately with events and we want rules from Head, not produced block IReadOnlyTxProcessorSource readOnlyTxProcessorSourceForTxPriority = - new ReadOnlyTxProcessingEnv(_api.DbProvider, _api.ReadOnlyTrieStore, _api.BlockTree, _api.SpecProvider, _api.LogManager); + new ReadOnlyTxProcessingEnv(_api.ReadOnlyWorldStateFactory!, _api.BlockTree, _api.SpecProvider, _api.LogManager); (_txPriorityContract, _localDataSource) = TxAuRaFilterBuilders.CreateTxPrioritySources(_auraConfig, _api, readOnlyTxProcessorSourceForTxPriority); @@ -228,18 +228,17 @@ internal TxPoolTxSource CreateTxPoolTxSource(ReadOnlyTxProcessingEnv processingE // TODO: Use BlockProducerEnvFactory private BlockProducerEnv GetProducerChain(ITxSource? additionalTxSource) { - ReadOnlyTxProcessingEnv CreateReadonlyTxProcessingEnv(ReadOnlyDbProvider dbProvider, ReadOnlyBlockTree blockTree) + ReadOnlyTxProcessingEnv CreateReadonlyTxProcessingEnv(ReadOnlyBlockTree blockTree) { - return new(dbProvider, _api.ReadOnlyTrieStore, blockTree, _api.SpecProvider, _api.LogManager); + return new(_api.ReadOnlyWorldStateFactory!, blockTree, _api.SpecProvider, _api.LogManager); } BlockProducerEnv Create() { - ReadOnlyDbProvider dbProvider = _api.DbProvider.AsReadOnly(false); ReadOnlyBlockTree readOnlyBlockTree = _api.BlockTree.AsReadOnly(); - ReadOnlyTxProcessingEnv txProcessingEnv = CreateReadonlyTxProcessingEnv(dbProvider, readOnlyBlockTree); - ReadOnlyTxProcessingEnv constantContractsProcessingEnv = CreateReadonlyTxProcessingEnv(dbProvider, readOnlyBlockTree); + ReadOnlyTxProcessingEnv txProcessingEnv = CreateReadonlyTxProcessingEnv(readOnlyBlockTree); + ReadOnlyTxProcessingEnv constantContractsProcessingEnv = CreateReadonlyTxProcessingEnv(readOnlyBlockTree); BlockProcessor blockProcessor = CreateBlockProcessor(txProcessingEnv, constantContractsProcessingEnv); IBlockchainProcessor blockchainProcessor = @@ -252,7 +251,7 @@ BlockProducerEnv Create() BlockchainProcessor.Options.NoReceipts); OneTimeChainProcessor chainProcessor = new( - dbProvider, + txProcessingEnv.ResetDb, blockchainProcessor); return new BlockProducerEnv() diff --git a/src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs b/src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs index cf2fd70c78f..9470a73ddfb 100644 --- a/src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs +++ b/src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs @@ -90,13 +90,11 @@ public Task InitBlockProducer(IBlockProductionTrigger? blockProd _snapshotManager!, getFromApi.LogManager); - ReadOnlyDbProvider readOnlyDbProvider = getFromApi.DbProvider!.AsReadOnly(false); ReadOnlyBlockTree readOnlyBlockTree = getFromApi.BlockTree!.AsReadOnly(); ITransactionComparerProvider transactionComparerProvider = getFromApi.TransactionComparerProvider; ReadOnlyTxProcessingEnv producerEnv = new( - readOnlyDbProvider, - getFromApi.ReadOnlyTrieStore, + _nethermindApi.ReadOnlyWorldStateFactory!, readOnlyBlockTree, getFromApi.SpecProvider, getFromApi.LogManager); @@ -121,7 +119,7 @@ public Task InitBlockProducer(IBlockProductionTrigger? blockProd BlockchainProcessor.Options.NoReceipts); OneTimeChainProcessor chainProcessor = new( - readOnlyDbProvider, + producerEnv.ResetDb, producerChainProcessor); ITxFilterPipeline txFilterPipeline = diff --git a/src/Nethermind/Nethermind.Consensus.Ethash/NethDevPlugin.cs b/src/Nethermind/Nethermind.Consensus.Ethash/NethDevPlugin.cs index ca37ef3f571..bdea51027bf 100644 --- a/src/Nethermind/Nethermind.Consensus.Ethash/NethDevPlugin.cs +++ b/src/Nethermind/Nethermind.Consensus.Ethash/NethDevPlugin.cs @@ -45,7 +45,6 @@ public Task InitBlockProducer(IBlockProductionTrigger? blockProd var (getFromApi, _) = _nethermindApi!.ForProducer; - ReadOnlyDbProvider readOnlyDbProvider = getFromApi.DbProvider.AsReadOnly(false); ReadOnlyBlockTree readOnlyBlockTree = getFromApi.BlockTree.AsReadOnly(); ITxFilterPipeline txFilterPipeline = new TxFilterPipelineBuilder(_nethermindApi.LogManager) @@ -64,10 +63,8 @@ public Task InitBlockProducer(IBlockProductionTrigger? blockProd ILogger logger = getFromApi.LogManager.GetClassLogger(); if (logger.IsInfo) logger.Info("Starting Neth Dev block producer & sealer"); - ReadOnlyTxProcessingEnv producerEnv = new( - readOnlyDbProvider, - getFromApi.ReadOnlyTrieStore, + _nethermindApi.ReadOnlyWorldStateFactory!, readOnlyBlockTree, getFromApi.SpecProvider, getFromApi.LogManager); diff --git a/src/Nethermind/Nethermind.Consensus/Processing/OneTimeProcessor.cs b/src/Nethermind/Nethermind.Consensus/Processing/OneTimeProcessor.cs index dba8eecce74..0b84e72f298 100644 --- a/src/Nethermind/Nethermind.Consensus/Processing/OneTimeProcessor.cs +++ b/src/Nethermind/Nethermind.Consensus/Processing/OneTimeProcessor.cs @@ -6,6 +6,7 @@ using Nethermind.Core; using Nethermind.Db; using Nethermind.Evm.Tracing; +using Nethermind.State; namespace Nethermind.Consensus.Processing { @@ -14,13 +15,13 @@ public class OneTimeChainProcessor : IBlockchainProcessor public ITracerBag Tracers => _processor.Tracers; private readonly IBlockchainProcessor _processor; - private readonly IReadOnlyDbProvider _readOnlyDbProvider; + private readonly Action _clearTemporaryState; private object _lock = new(); - public OneTimeChainProcessor(IReadOnlyDbProvider readOnlyDbProvider, IBlockchainProcessor processor) + public OneTimeChainProcessor(Action clearTemporaryState, IBlockchainProcessor processor) { - _readOnlyDbProvider = readOnlyDbProvider ?? throw new ArgumentNullException(nameof(readOnlyDbProvider)); + _clearTemporaryState = clearTemporaryState ?? throw new ArgumentNullException(nameof(clearTemporaryState)); _processor = processor ?? throw new ArgumentNullException(nameof(processor)); } @@ -45,7 +46,7 @@ public Task StopAsync(bool processRemainingBlocks = false) } finally { - _readOnlyDbProvider.ClearTempChanges(); + _clearTemporaryState.Invoke(); } return result; @@ -66,7 +67,6 @@ public bool IsProcessingBlocks(ulong? maxProcessingInterval) public void Dispose() { _processor?.Dispose(); - _readOnlyDbProvider?.Dispose(); } } } diff --git a/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyChainProcessingEnv.cs b/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyChainProcessingEnv.cs index 65baaa7ea3e..53a3cf864ed 100644 --- a/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyChainProcessingEnv.cs +++ b/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyChainProcessingEnv.cs @@ -31,7 +31,7 @@ public ReadOnlyChainProcessingEnv( IBlockPreprocessorStep recoveryStep, IRewardCalculator rewardCalculator, IReceiptStorage receiptStorage, - IReadOnlyDbProvider dbProvider, + Action resetAction, ISpecProvider specProvider, ILogManager logManager, IBlockProcessor.IBlockTransactionsExecutor? blockTransactionsExecutor = null) @@ -53,7 +53,7 @@ public ReadOnlyChainProcessingEnv( _blockProcessingQueue = new BlockchainProcessor(_txEnv.BlockTree, BlockProcessor, recoveryStep, _txEnv.StateReader, logManager, BlockchainProcessor.Options.NoReceipts); BlockProcessingQueue = _blockProcessingQueue; - ChainProcessor = new OneTimeChainProcessor(dbProvider, _blockProcessingQueue); + ChainProcessor = new OneTimeChainProcessor(resetAction, _blockProcessingQueue); } public void Dispose() diff --git a/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyTxProcessingEnv.cs b/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyTxProcessingEnv.cs index 8cadba155b6..a60afdb8d5f 100644 --- a/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyTxProcessingEnv.cs +++ b/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyTxProcessingEnv.cs @@ -5,12 +5,10 @@ using Nethermind.Blockchain; using Nethermind.Core.Crypto; using Nethermind.Core.Specs; -using Nethermind.Db; using Nethermind.Evm; using Nethermind.Evm.TransactionProcessing; using Nethermind.Logging; using Nethermind.State; -using Nethermind.Trie.Pruning; // ReSharper disable MemberCanBePrivate.Global // ReSharper disable UnusedAutoPropertyAccessor.Global @@ -22,34 +20,32 @@ public class ReadOnlyTxProcessingEnv : IReadOnlyTxProcessorSource public IWorldState StateProvider { get; } public ITransactionProcessor TransactionProcessor { get; set; } public IBlockTree BlockTree { get; } - public IReadOnlyDbProvider DbProvider { get; } public IBlockhashProvider BlockhashProvider { get; } public IVirtualMachine Machine { get; } + public Action ResetDb { get; } public ReadOnlyTxProcessingEnv( - IDbProvider? dbProvider, - IReadOnlyTrieStore? trieStore, + IWorldStateFactory worldStateFactory, IBlockTree? blockTree, ISpecProvider? specProvider, ILogManager? logManager) - : this(dbProvider?.AsReadOnly(false), trieStore, blockTree?.AsReadOnly(), specProvider, logManager) + : this(worldStateFactory, blockTree?.AsReadOnly(), specProvider, logManager) { } public ReadOnlyTxProcessingEnv( - IReadOnlyDbProvider? readOnlyDbProvider, - IReadOnlyTrieStore? readOnlyTrieStore, + IWorldStateFactory worldStateFactory, IReadOnlyBlockTree? readOnlyBlockTree, ISpecProvider? specProvider, ILogManager? logManager) { if (specProvider is null) throw new ArgumentNullException(nameof(specProvider)); + if (worldStateFactory is null) throw new ArgumentNullException(nameof(worldStateFactory)); - DbProvider = readOnlyDbProvider ?? throw new ArgumentNullException(nameof(readOnlyDbProvider)); - ReadOnlyDb codeDb = readOnlyDbProvider.CodeDb.AsReadOnly(true); - - StateReader = new StateReader(readOnlyTrieStore, codeDb, logManager); - StateProvider = new WorldState(readOnlyTrieStore, codeDb, logManager); + (IWorldState worldState, IStateReader stateReader, Action reset) = worldStateFactory.CreateResettableWorldState(); + StateReader = stateReader; + StateProvider = worldState; + ResetDb = reset; BlockTree = readOnlyBlockTree ?? throw new ArgumentNullException(nameof(readOnlyBlockTree)); BlockhashProvider = new BlockhashProvider(BlockTree, logManager); diff --git a/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyTxProcessingEnvFactory.cs b/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyTxProcessingEnvFactory.cs index 0f5612dd38d..b0ceb7901e7 100644 --- a/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyTxProcessingEnvFactory.cs +++ b/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyTxProcessingEnvFactory.cs @@ -5,41 +5,38 @@ using Nethermind.Core.Specs; using Nethermind.Db; using Nethermind.Logging; +using Nethermind.State; using Nethermind.Trie.Pruning; namespace Nethermind.Consensus.Processing; public class ReadOnlyTxProcessingEnvFactory { - private readonly IReadOnlyDbProvider? _readOnlyDbProvider; - private readonly IReadOnlyTrieStore? _readOnlyTrieStore; + private readonly IWorldStateFactory _worldStateFactory; private readonly IReadOnlyBlockTree? _readOnlyBlockTree; private readonly ISpecProvider? _specProvider; private readonly ILogManager? _logManager; public ReadOnlyTxProcessingEnvFactory( - IDbProvider? dbProvider, - IReadOnlyTrieStore? trieStore, + IWorldStateFactory worldStateFactory, IBlockTree? blockTree, ISpecProvider? specProvider, ILogManager? logManager) - : this(dbProvider?.AsReadOnly(false), trieStore, blockTree?.AsReadOnly(), specProvider, logManager) + : this(worldStateFactory, blockTree?.AsReadOnly(), specProvider, logManager) { } public ReadOnlyTxProcessingEnvFactory( - IReadOnlyDbProvider? readOnlyDbProvider, - IReadOnlyTrieStore? readOnlyTrieStore, + IWorldStateFactory worldStateFactory, IReadOnlyBlockTree? readOnlyBlockTree, ISpecProvider? specProvider, ILogManager? logManager) { - _readOnlyDbProvider = readOnlyDbProvider; - _readOnlyTrieStore = readOnlyTrieStore; + _worldStateFactory = worldStateFactory; _readOnlyBlockTree = readOnlyBlockTree; _specProvider = specProvider; _logManager = logManager; } - public ReadOnlyTxProcessingEnv Create() => new(_readOnlyDbProvider, _readOnlyTrieStore, _readOnlyBlockTree, _specProvider, _logManager); + public ReadOnlyTxProcessingEnv Create() => new(_worldStateFactory, _readOnlyBlockTree, _specProvider, _logManager); } diff --git a/src/Nethermind/Nethermind.Consensus/Producers/BlockProducerEnvFactory.cs b/src/Nethermind/Nethermind.Consensus/Producers/BlockProducerEnvFactory.cs index 3d88b53ba6a..421dee82481 100644 --- a/src/Nethermind/Nethermind.Consensus/Producers/BlockProducerEnvFactory.cs +++ b/src/Nethermind/Nethermind.Consensus/Producers/BlockProducerEnvFactory.cs @@ -14,16 +14,14 @@ using Nethermind.Db; using Nethermind.Logging; using Nethermind.State; -using Nethermind.Trie.Pruning; using Nethermind.TxPool; namespace Nethermind.Consensus.Producers { public class BlockProducerEnvFactory : IBlockProducerEnvFactory { - protected readonly IDbProvider _dbProvider; + protected readonly IWorldStateFactory _worldStateFactory; protected readonly IBlockTree _blockTree; - protected readonly IReadOnlyTrieStore _readOnlyTrieStore; protected readonly ISpecProvider _specProvider; protected readonly IBlockValidator _blockValidator; protected readonly IRewardCalculatorSource _rewardCalculatorSource; @@ -37,9 +35,8 @@ public class BlockProducerEnvFactory : IBlockProducerEnvFactory public IBlockTransactionsExecutorFactory TransactionsExecutorFactory { get; set; } public BlockProducerEnvFactory( - IDbProvider dbProvider, + IWorldStateFactory worldStateFactory, IBlockTree blockTree, - IReadOnlyTrieStore readOnlyTrieStore, ISpecProvider specProvider, IBlockValidator blockValidator, IRewardCalculatorSource rewardCalculatorSource, @@ -50,9 +47,8 @@ public BlockProducerEnvFactory( IBlocksConfig blocksConfig, ILogManager logManager) { - _dbProvider = dbProvider; + _worldStateFactory = worldStateFactory; _blockTree = blockTree; - _readOnlyTrieStore = readOnlyTrieStore; _specProvider = specProvider; _blockValidator = blockValidator; _rewardCalculatorSource = rewardCalculatorSource; @@ -68,11 +64,10 @@ public BlockProducerEnvFactory( public virtual BlockProducerEnv Create(ITxSource? additionalTxSource = null) { - ReadOnlyDbProvider readOnlyDbProvider = _dbProvider.AsReadOnly(false); ReadOnlyBlockTree readOnlyBlockTree = _blockTree.AsReadOnly(); ReadOnlyTxProcessingEnv txProcessingEnv = - CreateReadonlyTxProcessingEnv(readOnlyDbProvider, readOnlyBlockTree); + CreateReadonlyTxProcessingEnv(_worldStateFactory, readOnlyBlockTree); BlockProcessor blockProcessor = CreateBlockProcessor(txProcessingEnv, @@ -93,7 +88,7 @@ public virtual BlockProducerEnv Create(ITxSource? additionalTxSource = null) BlockchainProcessor.Options.NoReceipts); OneTimeChainProcessor chainProcessor = new( - readOnlyDbProvider, + txProcessingEnv.ResetDb, blockchainProcessor); return new BlockProducerEnv @@ -106,8 +101,8 @@ public virtual BlockProducerEnv Create(ITxSource? additionalTxSource = null) }; } - protected virtual ReadOnlyTxProcessingEnv CreateReadonlyTxProcessingEnv(ReadOnlyDbProvider readOnlyDbProvider, ReadOnlyBlockTree readOnlyBlockTree) => - new(readOnlyDbProvider, _readOnlyTrieStore, readOnlyBlockTree, _specProvider, _logManager); + protected virtual ReadOnlyTxProcessingEnv CreateReadonlyTxProcessingEnv(IWorldStateFactory worldStateFactory, ReadOnlyBlockTree readOnlyBlockTree) => + new(worldStateFactory, readOnlyBlockTree, _specProvider, _logManager); protected virtual ITxSource CreateTxSourceForProducer( ITxSource? additionalTxSource, diff --git a/src/Nethermind/Nethermind.Core.Test/Blockchain/TestBlockchain.cs b/src/Nethermind/Nethermind.Core.Test/Blockchain/TestBlockchain.cs index 829c74fab84..270e4709b09 100644 --- a/src/Nethermind/Nethermind.Core.Test/Blockchain/TestBlockchain.cs +++ b/src/Nethermind/Nethermind.Core.Test/Blockchain/TestBlockchain.cs @@ -49,6 +49,7 @@ public class TestBlockchain : IDisposable public IReceiptStorage ReceiptStorage { get; set; } = null!; public ITxPool TxPool { get; set; } = null!; public IDb CodeDb => DbProvider.CodeDb; + public IWorldStateFactory ReadOnlyWorldStateFactory = null!; public IBlockProcessor BlockProcessor { get; set; } = null!; public IBeaconBlockRootHandler BeaconBlockRootHandler { get; set; } = null!; public IBlockchainProcessor BlockchainProcessor { get; set; } = null!; @@ -139,6 +140,7 @@ protected virtual async Task Build(ISpecProvider? specProvider = State.CommitTree(0); ReadOnlyTrieStore = TrieStore.AsReadOnly(StateDb); + ReadOnlyWorldStateFactory = new ReadOnlyWorldStateFactory(DbProvider, ReadOnlyTrieStore, LimboLogs.Instance); StateReader = new StateReader(ReadOnlyTrieStore, CodeDb, LogManager); BlockTree = Builders.Build.A.BlockTree() @@ -247,9 +249,8 @@ protected virtual IBlockProducer CreateTestBlockProducer(TxPoolTxSource txPoolTx BlocksConfig blocksConfig = new(); BlockProducerEnvFactory blockProducerEnvFactory = new( - DbProvider, + ReadOnlyWorldStateFactory, BlockTree, - ReadOnlyTrieStore, SpecProvider, BlockValidator, NoBlockRewards.Instance, diff --git a/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs b/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs index 5726f173f9c..b40c63af2b3 100644 --- a/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs +++ b/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs @@ -29,6 +29,7 @@ using NUnit.Framework; using Nethermind.Config; using Nethermind.Evm; +using Nethermind.State; namespace Nethermind.Facade.Test { @@ -60,9 +61,14 @@ public async Task SetUp() _ethereumEcdsa = Substitute.For(); _specProvider = MainnetSpecProvider.Instance; + ReadOnlyDbProvider dbProvider = new ReadOnlyDbProvider(_dbProvider, false); + IReadOnlyTrieStore trieStore = new TrieStore(_dbProvider.StateDb, LimboLogs.Instance).AsReadOnly(); + + IWorldStateFactory readOnlyWorldStateFactory = + new ReadOnlyWorldStateFactory(dbProvider, trieStore, LimboLogs.Instance); + ReadOnlyTxProcessingEnv processingEnv = new( - new ReadOnlyDbProvider(_dbProvider, false), - new TrieStore(_dbProvider.StateDb, LimboLogs.Instance).AsReadOnly(), + readOnlyWorldStateFactory, new ReadOnlyBlockTree(_blockTree), _specProvider, LimboLogs.Instance); @@ -204,9 +210,14 @@ public void Call_uses_valid_beneficiary() [TestCase(0)] public void Bridge_head_is_correct(long headNumber) { + ReadOnlyDbProvider dbProvider = new ReadOnlyDbProvider(_dbProvider, false); + IReadOnlyTrieStore trieStore = new TrieStore(_dbProvider.StateDb, LimboLogs.Instance).AsReadOnly(); + + IWorldStateFactory readOnlyWorldStateFactory = + new ReadOnlyWorldStateFactory(dbProvider, trieStore, LimboLogs.Instance); + ReadOnlyTxProcessingEnv processingEnv = new( - new ReadOnlyDbProvider(_dbProvider, false), - new TrieStore(_dbProvider.StateDb, LimboLogs.Instance).AsReadOnly(), + readOnlyWorldStateFactory, new ReadOnlyBlockTree(_blockTree), _specProvider, LimboLogs.Instance); diff --git a/src/Nethermind/Nethermind.Init/InitializeStateDb.cs b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs index dd42984fc58..a615a425bee 100644 --- a/src/Nethermind/Nethermind.Init/InitializeStateDb.cs +++ b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs @@ -143,11 +143,10 @@ public Task Execute(CancellationToken cancellationToken) getApi.DisposeStack.Push(trieStore); IReadOnlyTrieStore readOnlyTrieStore = setApi.ReadOnlyTrieStore = trieStore.AsReadOnly(cachedStateDb); - ReadOnlyDbProvider readOnly = new(getApi.DbProvider, false); IWorldStateFactory readOnlyStateFactory = setApi.ReadOnlyWorldStateFactory = new ReadOnlyWorldStateFactory( + getApi.DbProvider, readOnlyTrieStore, - readOnly.GetDb(DbNames.Code), getApi.LogManager); setApi.ChainHeadStateProvider = new ChainHeadReadOnlyStateProvider(getApi.BlockTree, readOnlyStateFactory.CreateStateReader()); diff --git a/src/Nethermind/Nethermind.Init/Steps/InitializeBlockProducer.cs b/src/Nethermind/Nethermind.Init/Steps/InitializeBlockProducer.cs index badde6e2f42..0a9b7b2b110 100644 --- a/src/Nethermind/Nethermind.Init/Steps/InitializeBlockProducer.cs +++ b/src/Nethermind/Nethermind.Init/Steps/InitializeBlockProducer.cs @@ -32,9 +32,9 @@ public async Task Execute(CancellationToken _) protected virtual async Task BuildProducer() { - _api.BlockProducerEnvFactory = new BlockProducerEnvFactory(_api.DbProvider!, + _api.BlockProducerEnvFactory = new BlockProducerEnvFactory( + _api.ReadOnlyWorldStateFactory!, _api.BlockTree!, - _api.ReadOnlyTrieStore!, _api.SpecProvider!, _api.BlockValidator!, _api.RewardCalculatorSource!, diff --git a/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs b/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs index 7fb03ba4f26..164de2c0d19 100644 --- a/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs +++ b/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs @@ -116,10 +116,11 @@ public virtual async Task Execute(CancellationToken cancellationToken) if (_api.PeerPool is null) throw new StepDependencyException(nameof(_api.PeerPool)); if (_api.WitnessRepository is null) throw new StepDependencyException(nameof(_api.WitnessRepository)); - ProofModuleFactory proofModuleFactory = new(_api.DbProvider, _api.BlockTree, _api.ReadOnlyTrieStore, _api.BlockPreprocessor, _api.ReceiptFinder, _api.SpecProvider, _api.LogManager); + ProofModuleFactory proofModuleFactory = new(_api.ReadOnlyWorldStateFactory, _api.BlockTree, _api.BlockPreprocessor, _api.ReceiptFinder, _api.SpecProvider, _api.LogManager); rpcModuleProvider.RegisterBounded(proofModuleFactory, 2, rpcConfig.Timeout); DebugModuleFactory debugModuleFactory = new( + _api.ReadOnlyWorldStateFactory, _api.DbProvider, _api.BlockTree, rpcConfig, @@ -137,9 +138,8 @@ public virtual async Task Execute(CancellationToken cancellationToken) rpcModuleProvider.RegisterBoundedByCpuCount(debugModuleFactory, rpcConfig.Timeout); TraceModuleFactory traceModuleFactory = new( - _api.DbProvider, + _api.ReadOnlyWorldStateFactory, _api.BlockTree, - _api.ReadOnlyTrieStore, rpcConfig, _api.BlockPreprocessor, _api.RewardCalculatorSource, diff --git a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Proof/ProofRpcModuleTests.cs b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Proof/ProofRpcModuleTests.cs index 2c791359a4d..3ed217af592 100644 --- a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Proof/ProofRpcModuleTests.cs +++ b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Proof/ProofRpcModuleTests.cs @@ -44,6 +44,7 @@ public class ProofRpcModuleTests private IBlockTree _blockTree = null!; private IDbProvider _dbProvider = null!; private TestSpecProvider _specProvider = null!; + private ReadOnlyWorldStateFactory _readOnlyWorldStateFactory = null!; public ProofRpcModuleTests(bool createSystemAccount, bool useNonZeroGasPrice) { @@ -59,10 +60,12 @@ public async Task Setup() _blockTree = Build.A.BlockTree(_specProvider).WithTransactions(receiptStorage).OfChainLength(10).TestObject; _dbProvider = await TestMemDbProvider.InitAsync(); + ITrieStore trieStore = new TrieStore(_dbProvider.StateDb, LimboLogs.Instance); + _readOnlyWorldStateFactory = new ReadOnlyWorldStateFactory(_dbProvider, trieStore.AsReadOnly(), LimboLogs.Instance); + ProofModuleFactory moduleFactory = new( - _dbProvider, + _readOnlyWorldStateFactory, _blockTree, - new TrieStore(_dbProvider.StateDb, LimboLogs.Instance).AsReadOnly(), new CompositeBlockPreprocessorStep(new RecoverSignatures(new EthereumEcdsa(TestBlockchainIds.ChainId, LimboLogs.Instance), NullTxPool.Instance, _specProvider, LimboLogs.Instance)), receiptStorage, _specProvider, @@ -205,9 +208,8 @@ public async Task Get_receipt_when_block_has_few_receipts(bool withHeader, strin _receiptFinder.FindBlockHash(Arg.Any()).Returns(_blockTree.FindBlock(1)!.Hash); ProofModuleFactory moduleFactory = new ProofModuleFactory( - _dbProvider, + _readOnlyWorldStateFactory, _blockTree, - new TrieStore(_dbProvider.StateDb, LimboLogs.Instance).AsReadOnly(), new CompositeBlockPreprocessorStep(new RecoverSignatures(new EthereumEcdsa(TestBlockchainIds.ChainId, LimboLogs.Instance), NullTxPool.Instance, _specProvider, LimboLogs.Instance)), _receiptFinder, _specProvider, diff --git a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TestRpcBlockchain.cs b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TestRpcBlockchain.cs index 7bbe956ae36..52ee9ba91d5 100644 --- a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TestRpcBlockchain.cs +++ b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TestRpcBlockchain.cs @@ -123,8 +123,7 @@ protected override async Task Build(ISpecProvider? specProvider IFilterManager filterManager = new FilterManager(filterStore, BlockProcessor, TxPool, LimboLogs.Instance); ReadOnlyTxProcessingEnv processingEnv = new( - new ReadOnlyDbProvider(DbProvider, false), - new TrieStore(DbProvider.StateDb, LimboLogs.Instance).AsReadOnly(), + ReadOnlyWorldStateFactory, new ReadOnlyBlockTree(BlockTree), SpecProvider, LimboLogs.Instance); diff --git a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TraceRpcModuleTests.cs b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TraceRpcModuleTests.cs index d7ccdf1b973..19de500d095 100644 --- a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TraceRpcModuleTests.cs +++ b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TraceRpcModuleTests.cs @@ -46,12 +46,11 @@ public async Task Build(ISpecProvider? specProvider = null, bool isAura = false) await Blockchain.AddFunds(TestItem.AddressA, 1000.Ether()); await Blockchain.AddFunds(TestItem.AddressB, 1000.Ether()); await Blockchain.AddFunds(TestItem.AddressC, 1000.Ether()); - ReadOnlyDbProvider? dbProvider = Blockchain.DbProvider.AsReadOnly(false); ReceiptsRecovery receiptsRecovery = new(Blockchain.EthereumEcdsa, Blockchain.SpecProvider); IReceiptFinder receiptFinder = new FullInfoReceiptFinder(Blockchain.ReceiptStorage, receiptsRecovery, Blockchain.BlockFinder); ReadOnlyTxProcessingEnv txProcessingEnv = - new(dbProvider, Blockchain.ReadOnlyTrieStore, Blockchain.BlockTree.AsReadOnly(), Blockchain.SpecProvider, Blockchain.LogManager); + new(Blockchain.ReadOnlyWorldStateFactory, Blockchain.BlockTree.AsReadOnly(), Blockchain.SpecProvider, Blockchain.LogManager); RewardCalculator rewardCalculatorSource = new(Blockchain.SpecProvider); IRewardCalculator rewardCalculator = rewardCalculatorSource.Get(txProcessingEnv.TransactionProcessor); @@ -65,7 +64,7 @@ public async Task Build(ISpecProvider? specProvider = null, bool isAura = false) Blockchain.BlockPreprocessorStep, rewardCalculator, Blockchain.ReceiptStorage, - dbProvider, + txProcessingEnv.ResetDb, Blockchain.SpecProvider, Blockchain.LogManager, transactionsExecutor); diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/DebugModule/DebugModuleFactory.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/DebugModule/DebugModuleFactory.cs index f9385b8d86c..3d8166d0d04 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/DebugModule/DebugModuleFactory.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/DebugModule/DebugModuleFactory.cs @@ -16,6 +16,7 @@ using Nethermind.Evm.Tracing.GethStyle.JavaScript; using Nethermind.Evm.TransactionProcessing; using Nethermind.Logging; +using Nethermind.State; using Nethermind.Synchronization.ParallelSync; using Nethermind.Trie.Pruning; using Newtonsoft.Json; @@ -24,6 +25,7 @@ namespace Nethermind.JsonRpc.Modules.DebugModule; public class DebugModuleFactory : ModuleFactoryBase { + private readonly IWorldStateFactory _worldStateFactory; private readonly IJsonRpcConfig _jsonRpcConfig; private readonly IBlockValidator _blockValidator; private readonly IRewardCalculatorSource _rewardCalculatorSource; @@ -41,6 +43,7 @@ public class DebugModuleFactory : ModuleFactoryBase private ILogger _logger; public DebugModuleFactory( + IWorldStateFactory worldStateFactory, IDbProvider dbProvider, IBlockTree blockTree, IJsonRpcConfig jsonRpcConfig, @@ -56,6 +59,7 @@ public DebugModuleFactory( IFileSystem fileSystem, ILogManager logManager) { + _worldStateFactory = worldStateFactory; _dbProvider = dbProvider.AsReadOnly(false); _blockTree = blockTree.AsReadOnly(); _jsonRpcConfig = jsonRpcConfig ?? throw new ArgumentNullException(nameof(jsonRpcConfig)); @@ -76,8 +80,7 @@ public DebugModuleFactory( public override IDebugRpcModule Create() { ReadOnlyTxProcessingEnv txEnv = new( - _dbProvider, - _trieStore, + _worldStateFactory, _blockTree, _specProvider, _logManager); @@ -90,7 +93,7 @@ public override IDebugRpcModule Create() _recoveryStep, _rewardCalculatorSource.Get(txEnv.TransactionProcessor), _receiptStorage, - _dbProvider, + txEnv.ResetDb, _specProvider, _logManager, transactionsExecutor); diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofModuleFactory.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofModuleFactory.cs index 39693f5b682..1516084636a 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofModuleFactory.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofModuleFactory.cs @@ -13,6 +13,7 @@ using Nethermind.Db; using Nethermind.JsonRpc.Data; using Nethermind.Logging; +using Nethermind.State; using Nethermind.Trie.Pruning; using Newtonsoft.Json; @@ -25,34 +26,31 @@ public class ProofModuleFactory : ModuleFactoryBase private readonly ISpecProvider _specProvider; private readonly ILogManager _logManager; private readonly IReadOnlyBlockTree _blockTree; - private readonly ReadOnlyDbProvider _dbProvider; - private readonly IReadOnlyTrieStore _trieStore; + private readonly IWorldStateFactory _worldStateFactory; public ProofModuleFactory( - IDbProvider dbProvider, + IWorldStateFactory worldStateFactory, IBlockTree blockTree, - IReadOnlyTrieStore trieStore, IBlockPreprocessorStep recoveryStep, IReceiptFinder receiptFinder, ISpecProvider specProvider, ILogManager logManager) { + _worldStateFactory = worldStateFactory ?? throw new ArgumentNullException(nameof(worldStateFactory)); _logManager = logManager ?? throw new ArgumentNullException(nameof(logManager)); _recoveryStep = recoveryStep ?? throw new ArgumentNullException(nameof(recoveryStep)); _receiptFinder = receiptFinder ?? throw new ArgumentNullException(nameof(receiptFinder)); _specProvider = specProvider ?? throw new ArgumentNullException(nameof(specProvider)); - _dbProvider = dbProvider.AsReadOnly(false); _blockTree = blockTree.AsReadOnly(); - _trieStore = trieStore; } public override IProofRpcModule Create() { ReadOnlyTxProcessingEnv txProcessingEnv = new( - _dbProvider, _trieStore, _blockTree, _specProvider, _logManager); + _worldStateFactory, _blockTree, _specProvider, _logManager); ReadOnlyChainProcessingEnv chainProcessingEnv = new( - txProcessingEnv, Always.Valid, _recoveryStep, NoBlockRewards.Instance, new InMemoryReceiptStorage(), _dbProvider, _specProvider, _logManager); + txProcessingEnv, Always.Valid, _recoveryStep, NoBlockRewards.Instance, new InMemoryReceiptStorage(), txProcessingEnv.ResetDb, _specProvider, _logManager); Tracer tracer = new( txProcessingEnv.StateProvider, diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Trace/TraceModuleFactory.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Trace/TraceModuleFactory.cs index 5c6bbc599a7..74ab5a8a379 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Trace/TraceModuleFactory.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Trace/TraceModuleFactory.cs @@ -13,6 +13,7 @@ using Nethermind.Core.Specs; using Nethermind.Db; using Nethermind.Logging; +using Nethermind.State; using Nethermind.Trie.Pruning; using Newtonsoft.Json; @@ -20,9 +21,8 @@ namespace Nethermind.JsonRpc.Modules.Trace { public class TraceModuleFactory : ModuleFactoryBase { - private readonly ReadOnlyDbProvider _dbProvider; + private readonly IWorldStateFactory _worldStateFactory; private readonly IReadOnlyBlockTree _blockTree; - private readonly IReadOnlyTrieStore _trieNodeResolver; private readonly IJsonRpcConfig _jsonRpcConfig; private readonly IReceiptStorage _receiptStorage; private readonly ISpecProvider _specProvider; @@ -32,9 +32,8 @@ public class TraceModuleFactory : ModuleFactoryBase private readonly IPoSSwitcher _poSSwitcher; public TraceModuleFactory( - IDbProvider dbProvider, + IWorldStateFactory worldStateFactory, IBlockTree blockTree, - IReadOnlyTrieStore trieNodeResolver, IJsonRpcConfig jsonRpcConfig, IBlockPreprocessorStep recoveryStep, IRewardCalculatorSource rewardCalculatorSource, @@ -43,9 +42,8 @@ public TraceModuleFactory( IPoSSwitcher poSSwitcher, ILogManager logManager) { - _dbProvider = dbProvider.AsReadOnly(false); + _worldStateFactory = worldStateFactory; _blockTree = blockTree.AsReadOnly(); - _trieNodeResolver = trieNodeResolver; _jsonRpcConfig = jsonRpcConfig ?? throw new ArgumentNullException(nameof(jsonRpcConfig)); _recoveryStep = recoveryStep ?? throw new ArgumentNullException(nameof(recoveryStep)); _rewardCalculatorSource = rewardCalculatorSource ?? throw new ArgumentNullException(nameof(rewardCalculatorSource)); @@ -59,7 +57,7 @@ public TraceModuleFactory( public override ITraceRpcModule Create() { ReadOnlyTxProcessingEnv txProcessingEnv = - new(_dbProvider, _trieNodeResolver, _blockTree, _specProvider, _logManager); + new(_worldStateFactory, _blockTree, _specProvider, _logManager); IRewardCalculator rewardCalculator = new MergeRpcRewardCalculator(_rewardCalculatorSource.Get(txProcessingEnv.TransactionProcessor), @@ -75,7 +73,7 @@ public override ITraceRpcModule Create() _recoveryStep, rewardCalculator, _receiptStorage, - _dbProvider, + txProcessingEnv.ResetDb, _specProvider, _logManager, transactionsExecutor); diff --git a/src/Nethermind/Nethermind.Merge.AuRa.Test/AuRaMergeEngineModuleTests.cs b/src/Nethermind/Nethermind.Merge.AuRa.Test/AuRaMergeEngineModuleTests.cs index d0a268e94c5..87f563977d5 100644 --- a/src/Nethermind/Nethermind.Merge.AuRa.Test/AuRaMergeEngineModuleTests.cs +++ b/src/Nethermind/Nethermind.Merge.AuRa.Test/AuRaMergeEngineModuleTests.cs @@ -111,6 +111,7 @@ protected override IBlockProcessor CreateBlockProcessor() BlockTree = BlockTree, DbProvider = DbProvider, ReadOnlyTrieStore = ReadOnlyTrieStore, + ReadOnlyWorldStateFactory = ReadOnlyWorldStateFactory, SpecProvider = SpecProvider, TransactionComparerProvider = TransactionComparerProvider, TxPool = TxPool @@ -157,9 +158,8 @@ protected override IBlockProducer CreateTestBlockProducer(TxPoolTxSource txPoolT _api!, new AuRaConfig(), new DisposableStack(), - DbProvider, + ReadOnlyWorldStateFactory, BlockTree, - ReadOnlyTrieStore, SpecProvider, BlockValidator, NoBlockRewards.Instance, diff --git a/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergeBlockProducerEnvFactory.cs b/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergeBlockProducerEnvFactory.cs index b8e311bd306..c2ac61d8ec5 100644 --- a/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergeBlockProducerEnvFactory.cs +++ b/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergeBlockProducerEnvFactory.cs @@ -13,10 +13,9 @@ using Nethermind.Consensus.Validators; using Nethermind.Core; using Nethermind.Core.Specs; -using Nethermind.Db; using Nethermind.Logging; using Nethermind.Merge.AuRa.Withdrawals; -using Nethermind.Trie.Pruning; +using Nethermind.State; using Nethermind.TxPool; namespace Nethermind.Merge.AuRa; @@ -31,9 +30,8 @@ public AuRaMergeBlockProducerEnvFactory( AuRaNethermindApi auraApi, IAuraConfig auraConfig, DisposableStack disposeStack, - IDbProvider dbProvider, + IWorldStateFactory worldStateFactory, IBlockTree blockTree, - IReadOnlyTrieStore readOnlyTrieStore, ISpecProvider specProvider, IBlockValidator blockValidator, IRewardCalculatorSource rewardCalculatorSource, @@ -43,9 +41,8 @@ public AuRaMergeBlockProducerEnvFactory( ITransactionComparerProvider transactionComparerProvider, IBlocksConfig blocksConfig, ILogManager logManager) : base( - dbProvider, + worldStateFactory, blockTree, - readOnlyTrieStore, specProvider, blockValidator, rewardCalculatorSource, @@ -97,7 +94,7 @@ protected override TxPoolTxSource CreateTxPoolTxSource( ILogManager logManager) { ReadOnlyTxProcessingEnv constantContractsProcessingEnv = CreateReadonlyTxProcessingEnv( - _dbProvider.AsReadOnly(false), + _worldStateFactory, _blockTree.AsReadOnly()); return new StartBlockProducerAuRa(_auraApi) diff --git a/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergePlugin.cs b/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergePlugin.cs index 0c7f3c666b2..1d77df3ee7f 100644 --- a/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergePlugin.cs +++ b/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergePlugin.cs @@ -50,9 +50,8 @@ public override Task InitBlockProducer(IConsensusPlugin consensu (AuRaNethermindApi)_api, _api.Config(), _api.DisposeStack, - _api.DbProvider!, + _api.ReadOnlyWorldStateFactory!, _api.BlockTree!, - _api.ReadOnlyTrieStore!, _api.SpecProvider!, _api.BlockValidator!, _api.RewardCalculatorSource!, diff --git a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Setup.cs b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Setup.cs index 27147005e13..94cc041a043 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Setup.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Setup.cs @@ -188,9 +188,8 @@ protected override IBlockProducer CreateTestBlockProducer(TxPoolTxSource txPoolT targetAdjustedGasLimitCalculator); BlockProducerEnvFactory blockProducerEnvFactory = new( - DbProvider, + ReadOnlyWorldStateFactory!, BlockTree, - ReadOnlyTrieStore, SpecProvider, BlockValidator, NoBlockRewards.Instance, diff --git a/src/Nethermind/Nethermind.Merge.Plugin.Test/MergePluginTests.cs b/src/Nethermind/Nethermind.Merge.Plugin.Test/MergePluginTests.cs index 98e6cf1254e..ead03deac39 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin.Test/MergePluginTests.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin.Test/MergePluginTests.cs @@ -45,9 +45,8 @@ public void Setup() _context.BlockProcessingQueue?.IsEmpty.Returns(true); _context.MemDbFactory = new MemDbFactory(); _context.BlockProducerEnvFactory = new BlockProducerEnvFactory( - _context.DbProvider!, + _context.ReadOnlyWorldStateFactory!, _context.BlockTree!, - _context.ReadOnlyTrieStore!, _context.SpecProvider!, _context.BlockValidator!, _context.RewardCalculatorSource!, diff --git a/src/Nethermind/Nethermind.Mev.Test/MevRpcModuleTests.TestMevRpcBlockchain.cs b/src/Nethermind/Nethermind.Mev.Test/MevRpcModuleTests.TestMevRpcBlockchain.cs index 9198ecf4c71..223c50fe77c 100644 --- a/src/Nethermind/Nethermind.Mev.Test/MevRpcModuleTests.TestMevRpcBlockchain.cs +++ b/src/Nethermind/Nethermind.Mev.Test/MevRpcModuleTests.TestMevRpcBlockchain.cs @@ -92,9 +92,8 @@ protected override IBlockProducer CreateTestBlockProducer(TxPoolTxSource txPoolT SpecProvider.UpdateMergeTransitionInfo(1, 0); BlockProducerEnvFactory blockProducerEnvFactory = new( - DbProvider, + ReadOnlyWorldStateFactory, BlockTree, - ReadOnlyTrieStore, SpecProvider, BlockValidator, NoBlockRewards.Instance, @@ -205,9 +204,8 @@ protected override BlockProcessor CreateBlockProcessor() LogManager); _tracerFactory = new TracerFactory( - DbProvider, BlockTree, - ReadOnlyTrieStore, + ReadOnlyWorldStateFactory, BlockPreprocessorStep, SpecProvider, LogManager, diff --git a/src/Nethermind/Nethermind.Mev/Execution/TracerFactory.cs b/src/Nethermind/Nethermind.Mev/Execution/TracerFactory.cs index d09f9f9955c..2e52dc83ae9 100644 --- a/src/Nethermind/Nethermind.Mev/Execution/TracerFactory.cs +++ b/src/Nethermind/Nethermind.Mev/Execution/TracerFactory.cs @@ -11,6 +11,7 @@ using Nethermind.Core.Specs; using Nethermind.Db; using Nethermind.Logging; +using Nethermind.State; using Nethermind.Trie.Pruning; namespace Nethermind.Mev.Execution @@ -22,13 +23,11 @@ public class TracerFactory : ITracerFactory private readonly ILogManager _logManager; private readonly ProcessingOptions _processingOptions; private readonly IReadOnlyBlockTree _blockTree; - private readonly ReadOnlyDbProvider _dbProvider; - private readonly IReadOnlyTrieStore _trieStore; + private readonly IWorldStateFactory _worldStateFactory; public TracerFactory( - IDbProvider dbProvider, IBlockTree blockTree, - IReadOnlyTrieStore trieStore, + IWorldStateFactory worldStateFactory, IBlockPreprocessorStep recoveryStep, ISpecProvider specProvider, ILogManager logManager, @@ -38,18 +37,17 @@ public TracerFactory( _processingOptions = processingOptions; _recoveryStep = recoveryStep ?? throw new ArgumentNullException(nameof(recoveryStep)); _specProvider = specProvider ?? throw new ArgumentNullException(nameof(specProvider)); - _dbProvider = dbProvider.AsReadOnly(false); + _worldStateFactory = worldStateFactory ?? throw new ArgumentNullException(nameof(worldStateFactory)); _blockTree = blockTree.AsReadOnly(); - _trieStore = trieStore; } public ITracer Create() { ReadOnlyTxProcessingEnv txProcessingEnv = new( - _dbProvider, _trieStore, _blockTree, _specProvider, _logManager); + _worldStateFactory, _blockTree, _specProvider, _logManager); ReadOnlyChainProcessingEnv chainProcessingEnv = new( - txProcessingEnv, Always.Valid, _recoveryStep, NoBlockRewards.Instance, new InMemoryReceiptStorage(), _dbProvider, _specProvider, _logManager); + txProcessingEnv, Always.Valid, _recoveryStep, NoBlockRewards.Instance, new InMemoryReceiptStorage(), txProcessingEnv.ResetDb, _specProvider, _logManager); return CreateTracer(txProcessingEnv, chainProcessingEnv); } diff --git a/src/Nethermind/Nethermind.Mev/MevPlugin.cs b/src/Nethermind/Nethermind.Mev/MevPlugin.cs index 06da91a3bcb..53255613c95 100644 --- a/src/Nethermind/Nethermind.Mev/MevPlugin.cs +++ b/src/Nethermind/Nethermind.Mev/MevPlugin.cs @@ -90,9 +90,8 @@ private ITracerFactory TracerFactory var (getFromApi, _) = _nethermindApi!.ForProducer; _tracerFactory = new TracerFactory( - getFromApi.DbProvider!, getFromApi.BlockTree!, - getFromApi.ReadOnlyTrieStore!, + getFromApi.ReadOnlyWorldStateFactory!, getFromApi.BlockPreprocessor!, getFromApi.SpecProvider!, getFromApi.LogManager!, diff --git a/src/Nethermind/Nethermind.Optimism/InitializeBlockProducerOptimism.cs b/src/Nethermind/Nethermind.Optimism/InitializeBlockProducerOptimism.cs index e7b01097df8..9ee3f0aacb3 100644 --- a/src/Nethermind/Nethermind.Optimism/InitializeBlockProducerOptimism.cs +++ b/src/Nethermind/Nethermind.Optimism/InitializeBlockProducerOptimism.cs @@ -32,12 +32,12 @@ protected override Task BuildProducer() if (_api.BlockValidator is null) throw new StepDependencyException(nameof(_api.BlockValidator)); if (_api.SpecHelper is null) throw new StepDependencyException(nameof(_api.SpecHelper)); if (_api.L1CostHelper is null) throw new StepDependencyException(nameof(_api.L1CostHelper)); + if (_api.ReadOnlyWorldStateFactory is null) throw new StepDependencyException(nameof(_api.ReadOnlyWorldStateFactory)); _api.BlockProducerEnvFactory = new OptimismBlockProducerEnvFactory( + _api.ReadOnlyWorldStateFactory, _api.ChainSpec, - _api.DbProvider, _api.BlockTree, - _api.ReadOnlyTrieStore, _api.SpecProvider, _api.BlockValidator, _api.RewardCalculatorSource, diff --git a/src/Nethermind/Nethermind.Optimism/OptimismBlockProducerEnvFactory.cs b/src/Nethermind/Nethermind.Optimism/OptimismBlockProducerEnvFactory.cs index 3ae5600db5b..dd71c6a475a 100644 --- a/src/Nethermind/Nethermind.Optimism/OptimismBlockProducerEnvFactory.cs +++ b/src/Nethermind/Nethermind.Optimism/OptimismBlockProducerEnvFactory.cs @@ -16,6 +16,7 @@ using Nethermind.Evm.TransactionProcessing; using Nethermind.Logging; using Nethermind.Specs.ChainSpecStyle; +using Nethermind.State; using Nethermind.Trie.Pruning; using Nethermind.TxPool; @@ -28,10 +29,9 @@ public class OptimismBlockProducerEnvFactory : BlockProducerEnvFactory private readonly OPL1CostHelper _l1CostHelper; public OptimismBlockProducerEnvFactory( + IWorldStateFactory worldStateFactory, ChainSpec chainSpec, - IDbProvider dbProvider, IBlockTree blockTree, - IReadOnlyTrieStore readOnlyTrieStore, ISpecProvider specProvider, IBlockValidator blockValidator, IRewardCalculatorSource rewardCalculatorSource, @@ -42,8 +42,8 @@ public OptimismBlockProducerEnvFactory( IBlocksConfig blocksConfig, OPSpecHelper specHelper, OPL1CostHelper l1CostHelper, - ILogManager logManager) : base(dbProvider, - blockTree, readOnlyTrieStore, specProvider, blockValidator, + ILogManager logManager) : base(worldStateFactory, + blockTree, specProvider, blockValidator, rewardCalculatorSource, receiptStorage, blockPreprocessorStep, txPool, transactionComparerProvider, blocksConfig, logManager) { @@ -53,10 +53,10 @@ public OptimismBlockProducerEnvFactory( TransactionsExecutorFactory = new OptimismTransactionsExecutorFactory(specProvider, logManager); } - protected override ReadOnlyTxProcessingEnv CreateReadonlyTxProcessingEnv(ReadOnlyDbProvider readOnlyDbProvider, + protected override ReadOnlyTxProcessingEnv CreateReadonlyTxProcessingEnv(IWorldStateFactory worldStateFactory, ReadOnlyBlockTree readOnlyBlockTree) { - ReadOnlyTxProcessingEnv result = new(readOnlyDbProvider, _readOnlyTrieStore, + ReadOnlyTxProcessingEnv result = new(worldStateFactory, readOnlyBlockTree, _specProvider, _logManager); result.TransactionProcessor = new OptimismTransactionProcessor(_specProvider, result.StateProvider, result.Machine, _logManager, _l1CostHelper, _specHelper); diff --git a/src/Nethermind/Nethermind.Runner.Test/Ethereum/ContextWithMocks.cs b/src/Nethermind/Nethermind.Runner.Test/Ethereum/ContextWithMocks.cs index a4e48cec847..b037d9e9fc0 100644 --- a/src/Nethermind/Nethermind.Runner.Test/Ethereum/ContextWithMocks.cs +++ b/src/Nethermind/Nethermind.Runner.Test/Ethereum/ContextWithMocks.cs @@ -49,8 +49,10 @@ namespace Nethermind.Runner.Test.Ethereum { public static class Build { - public static NethermindApi ContextWithMocks() => - new(Substitute.For(), Substitute.For(), LimboLogs.Instance, new ChainSpec()) + public static NethermindApi ContextWithMocks() + { + var api = new NethermindApi(Substitute.For(), Substitute.For(), LimboLogs.Instance, + new ChainSpec()) { Enode = Substitute.For(), TxPool = Substitute.For(), @@ -118,5 +120,9 @@ public static NethermindApi ContextWithMocks() => ReceiptMonitor = Substitute.For(), WitnessRepository = Substitute.For() }; + + api.ReadOnlyWorldStateFactory = new ReadOnlyWorldStateFactory(api.DbProvider, api.ReadOnlyTrieStore, LimboLogs.Instance); + return api; + } } } diff --git a/src/Nethermind/Nethermind.State/IWorldStateFactory.cs b/src/Nethermind/Nethermind.State/IWorldStateFactory.cs index 7657c790488..b8029f8a818 100644 --- a/src/Nethermind/Nethermind.State/IWorldStateFactory.cs +++ b/src/Nethermind/Nethermind.State/IWorldStateFactory.cs @@ -1,10 +1,13 @@ // SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited // SPDX-License-Identifier: LGPL-3.0-only +using System; + namespace Nethermind.State; public interface IWorldStateFactory { IWorldState CreateWorldState(); IStateReader CreateStateReader(); + (IWorldState, IStateReader, Action) CreateResettableWorldState(); } diff --git a/src/Nethermind/Nethermind.State/ReadOnlyWorldStateFactory.cs b/src/Nethermind/Nethermind.State/ReadOnlyWorldStateFactory.cs index f075635add1..81e75e2e53c 100644 --- a/src/Nethermind/Nethermind.State/ReadOnlyWorldStateFactory.cs +++ b/src/Nethermind/Nethermind.State/ReadOnlyWorldStateFactory.cs @@ -1,6 +1,7 @@ // SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited // SPDX-License-Identifier: LGPL-3.0-only +using System; using Nethermind.Core; using Nethermind.Db; using Nethermind.Logging; @@ -11,27 +12,40 @@ namespace Nethermind.State; public class ReadOnlyWorldStateFactory: IWorldStateFactory { private IReadOnlyTrieStore? _readOnlyTrieStore; - private IKeyValueStore _codeDb; private ILogManager _logManager; + private readonly IDbProvider _dbProvider; public ReadOnlyWorldStateFactory( + IDbProvider dbProvider, IReadOnlyTrieStore? readOnlyTrieStore, - IKeyValueStore codeDb, ILogManager logManager ) { _readOnlyTrieStore = readOnlyTrieStore; - _codeDb = codeDb; + _dbProvider = dbProvider; _logManager = logManager; } public IWorldState CreateWorldState() { - return new WorldState(_readOnlyTrieStore, _codeDb, _logManager); + IKeyValueStore codeDb = _dbProvider.AsReadOnly(false).GetDb(DbNames.Code); + return new WorldState(_readOnlyTrieStore, codeDb, _logManager); } public IStateReader CreateStateReader() { - return new StateReader(_readOnlyTrieStore, _codeDb, _logManager); + IKeyValueStore codeDb = _dbProvider.AsReadOnly(false).GetDb(DbNames.Code); + return new StateReader(_readOnlyTrieStore, codeDb, _logManager); + } + + public (IWorldState, IStateReader, Action) CreateResettableWorldState() + { + ReadOnlyDbProvider readOnlyDbProvider = _dbProvider.AsReadOnly(false); + IKeyValueStore codeDb = readOnlyDbProvider.GetDb(DbNames.Code).AsReadOnly(true); + return ( + new WorldState(_readOnlyTrieStore, codeDb, _logManager), + new StateReader(_readOnlyTrieStore, codeDb, _logManager), + readOnlyDbProvider.ClearTempChanges + ); } } diff --git a/src/Nethermind/Nethermind.State/WorldStateFactory.cs b/src/Nethermind/Nethermind.State/WorldStateFactory.cs index 73200af4a3e..b865f74722e 100644 --- a/src/Nethermind/Nethermind.State/WorldStateFactory.cs +++ b/src/Nethermind/Nethermind.State/WorldStateFactory.cs @@ -1,6 +1,7 @@ // SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited // SPDX-License-Identifier: LGPL-3.0-only +using System; using Nethermind.Core; using Nethermind.Logging; using Nethermind.Trie.Pruning; @@ -33,4 +34,9 @@ public IStateReader CreateStateReader() { return new StateReader(_trieStore, _codeDb, _logManager); } + + public (IWorldState, IStateReader, Action) CreateResettableWorldState() + { + throw new InvalidOperationException("Unsupported action"); + } } From 1881e2468583e17254e6c49f8f652dc0f7535e9d Mon Sep 17 00:00:00 2001 From: Amirul Ashraf Date: Wed, 8 Nov 2023 18:56:53 +0800 Subject: [PATCH 05/17] Use state reader instead of trie store for full state finder --- .../Steps/InitializeNetwork.cs | 1 + .../EngineModuleTests.Synchronization.cs | 2 +- .../Nethermind.Merge.Plugin/MergePlugin.cs | 3 +- .../Synchronization/MergeSynchronizer.cs | 3 ++ .../Nethermind.Optimism/OptimismPlugin.cs | 3 +- .../OldStyleFullSynchronizerTests.cs | 5 ++ .../SyncProgressResolverTests.cs | 47 ++++++++++--------- .../SyncThreadTests.cs | 1 + .../SynchronizerTests.cs | 7 ++- .../ParallelSync/FullStateFinder.cs | 20 ++------ .../Synchronizer.cs | 7 ++- 11 files changed, 56 insertions(+), 43 deletions(-) diff --git a/src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs b/src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs index 7b0e7e7a60c..1955d9e102d 100644 --- a/src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs +++ b/src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs @@ -155,6 +155,7 @@ private async Task Initialize(CancellationToken cancellationToken) _api.ReadOnlyTrieStore!, _api.BetterPeerStrategy, _api.ChainSpec, + _api.ReadOnlyWorldStateFactory!.CreateStateReader(), _api.LogManager); } diff --git a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Synchronization.cs b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Synchronization.cs index 1f05dbed6f1..850e2e6ae14 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Synchronization.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Synchronization.cs @@ -934,7 +934,7 @@ private MultiSyncModeSelector CreateMultiSyncModeSelector(MergeTestBlockchain ch SyncProgressResolver syncProgressResolver = new( chain.BlockTree, - new FullStateFinder(chain.BlockTree, chain.DbProvider.StateDb, chain.TrieStore), + new FullStateFinder(chain.BlockTree, chain.ReadOnlyWorldStateFactory.CreateStateReader()), new SyncConfig(), Substitute.For>(), Substitute.For>(), diff --git a/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs b/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs index 05d985fece1..db441fad3b7 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs @@ -415,7 +415,7 @@ public Task InitSynchronization() _api.SealValidator!, _syncConfig, _api.BetterPeerStrategy!, - new FullStateFinder(_api.BlockTree, _api.DbProvider.StateDb, _api.TrieStore!.AsReadOnly()), + new FullStateFinder(_api.BlockTree, _api.ReadOnlyWorldStateFactory!.CreateStateReader()), _api.LogManager); MergeSynchronizer synchronizer = new MergeSynchronizer( @@ -436,6 +436,7 @@ public Task InitSynchronization() _api.BetterPeerStrategy, _api.ChainSpec, _beaconSync, + _api.ReadOnlyWorldStateFactory.CreateStateReader(), _api.LogManager ); _api.Synchronizer = synchronizer; diff --git a/src/Nethermind/Nethermind.Merge.Plugin/Synchronization/MergeSynchronizer.cs b/src/Nethermind/Nethermind.Merge.Plugin/Synchronization/MergeSynchronizer.cs index ea6142ce3ba..6c01dae30a0 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin/Synchronization/MergeSynchronizer.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin/Synchronization/MergeSynchronizer.cs @@ -11,6 +11,7 @@ using Nethermind.Logging; using Nethermind.Merge.Plugin.InvalidChainTracker; using Nethermind.Specs.ChainSpecStyle; +using Nethermind.State; using Nethermind.Stats; using Nethermind.Synchronization; using Nethermind.Synchronization.Blocks; @@ -55,6 +56,7 @@ public MergeSynchronizer( IBetterPeerStrategy betterPeerStrategy, ChainSpec chainSpec, IBeaconSyncStrategy beaconSync, + IStateReader stateReader, ILogManager logManager) : base( dbProvider, @@ -70,6 +72,7 @@ public MergeSynchronizer( readOnlyTrieStore, betterPeerStrategy, chainSpec, + stateReader, logManager) { _invalidChainTracker = invalidChainTracker; diff --git a/src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs b/src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs index a8acd8974d5..15f75e13c18 100644 --- a/src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs +++ b/src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs @@ -148,7 +148,7 @@ public Task InitSynchronization() _api.SealValidator!, _syncConfig, _api.BetterPeerStrategy!, - new FullStateFinder(_api.BlockTree, _api.DbProvider.StateDb, _api.TrieStore!.AsReadOnly()), + new FullStateFinder(_api.BlockTree, _api.ReadOnlyWorldStateFactory!.CreateStateReader()), _api.LogManager); _api.Synchronizer = new MergeSynchronizer( @@ -169,6 +169,7 @@ public Task InitSynchronization() _api.BetterPeerStrategy, _api.ChainSpec, _beaconSync, + _api.ReadOnlyWorldStateFactory.CreateStateReader(), _api.LogManager ); diff --git a/src/Nethermind/Nethermind.Synchronization.Test/OldStyleFullSynchronizerTests.cs b/src/Nethermind/Nethermind.Synchronization.Test/OldStyleFullSynchronizerTests.cs index 04bacae7669..eeda9bd0741 100644 --- a/src/Nethermind/Nethermind.Synchronization.Test/OldStyleFullSynchronizerTests.cs +++ b/src/Nethermind/Nethermind.Synchronization.Test/OldStyleFullSynchronizerTests.cs @@ -20,6 +20,7 @@ using Nethermind.Logging; using Nethermind.Specs; using Nethermind.Specs.ChainSpecStyle; +using Nethermind.State; using Nethermind.State.Witnesses; using Nethermind.Stats; using Nethermind.Synchronization.Blocks; @@ -65,6 +66,9 @@ public async Task Setup() Always.Valid, new TotalDifficultyBetterPeerStrategy(LimboLogs.Instance), LimboLogs.Instance); + + IStateReader stateReader = new StateReader(trieStore, _codeDb, LimboLogs.Instance); + _synchronizer = new Synchronizer( dbProvider, MainnetSpecProvider.Instance, @@ -79,6 +83,7 @@ public async Task Setup() trieStore.AsReadOnly(), bestPeerStrategy, new ChainSpec(), + stateReader, LimboLogs.Instance); _syncServer = new SyncServer( trieStore.AsKeyValueStore(), diff --git a/src/Nethermind/Nethermind.Synchronization.Test/SyncProgressResolverTests.cs b/src/Nethermind/Nethermind.Synchronization.Test/SyncProgressResolverTests.cs index 3fa95df3df1..2a82088423e 100644 --- a/src/Nethermind/Nethermind.Synchronization.Test/SyncProgressResolverTests.cs +++ b/src/Nethermind/Nethermind.Synchronization.Test/SyncProgressResolverTests.cs @@ -8,6 +8,7 @@ using Nethermind.Core.Test.Builders; using Nethermind.Db; using Nethermind.Logging; +using Nethermind.State; using Nethermind.Synchronization.FastBlocks; using Nethermind.Synchronization.ParallelSync; using Nethermind.Synchronization.SnapSync; @@ -26,13 +27,13 @@ public class SyncProgressResolverTests public void Header_block_is_0_when_no_header_was_suggested() { IBlockTree blockTree = Substitute.For(); - IDb stateDb = new MemDb(); + IStateReader stateReader = Substitute.For(); SyncConfig syncConfig = new() { PivotNumber = "1", }; - SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateDb, NullTrieNodeResolver.Instance, false, syncConfig, LimboLogs.Instance); + SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateReader, false, syncConfig, LimboLogs.Instance); blockTree.BestSuggestedHeader.ReturnsNull(); Assert.That(syncProgressResolver.FindBestHeader(), Is.EqualTo(0)); } @@ -41,13 +42,13 @@ public void Header_block_is_0_when_no_header_was_suggested() public void Best_block_is_0_when_no_block_was_suggested() { IBlockTree blockTree = Substitute.For(); - IDb stateDb = new MemDb(); + IStateReader stateReader = Substitute.For(); SyncConfig syncConfig = new() { PivotNumber = "1", }; - SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateDb, NullTrieNodeResolver.Instance, false, syncConfig, LimboLogs.Instance); + SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateReader, false, syncConfig, LimboLogs.Instance); blockTree.BestSuggestedBody.ReturnsNull(); Assert.That(syncProgressResolver.FindBestFullBlock(), Is.EqualTo(0)); } @@ -56,17 +57,17 @@ public void Best_block_is_0_when_no_block_was_suggested() public void Best_state_is_head_when_there_are_no_suggested_blocks() { IBlockTree blockTree = Substitute.For(); - IDb stateDb = new MemDb(); + IStateReader stateReader = Substitute.For(); SyncConfig syncConfig = new() { PivotNumber = "1", }; - SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateDb, NullTrieNodeResolver.Instance, false, syncConfig, LimboLogs.Instance); + SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateReader, false, syncConfig, LimboLogs.Instance); Block head = Build.A.Block.WithHeader(Build.A.BlockHeader.WithNumber(5).WithStateRoot(TestItem.KeccakA).TestObject).TestObject; blockTree.Head.Returns(head); blockTree.BestSuggestedHeader.Returns(head.Header); - stateDb[head.StateRoot!.Bytes] = new byte[] { 1 }; + stateReader.HasStateForRoot(head.StateRoot!).Returns(true); Assert.That(syncProgressResolver.FindBestFullState(), Is.EqualTo(head.Number)); } @@ -74,21 +75,21 @@ public void Best_state_is_head_when_there_are_no_suggested_blocks() public void Best_state_is_suggested_if_there_is_suggested_block_with_state() { IBlockTree blockTree = Substitute.For(); - IDb stateDb = new MemDb(); + IStateReader stateReader = Substitute.For(); SyncConfig syncConfig = new() { PivotNumber = "1", }; - SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateDb, NullTrieNodeResolver.Instance, false, syncConfig, LimboLogs.Instance); + SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateReader, false, syncConfig, LimboLogs.Instance); Block head = Build.A.Block.WithHeader(Build.A.BlockHeader.WithNumber(5).WithStateRoot(TestItem.KeccakA).TestObject).TestObject; BlockHeader suggested = Build.A.BlockHeader.WithNumber(6).WithStateRoot(TestItem.KeccakB).TestObject; blockTree.Head.Returns(head); blockTree.BestSuggestedHeader.Returns(suggested); blockTree.FindHeader(Arg.Any(), BlockTreeLookupOptions.TotalDifficultyNotNeeded).Returns(head.Header); - stateDb[head.StateRoot!.Bytes] = new byte[] { 1 }; - stateDb[suggested.StateRoot!.Bytes] = new byte[] { 1 }; + stateReader.HasStateForRoot(head.StateRoot!).Returns(true); + stateReader.HasStateForRoot(suggested.StateRoot!).Returns(true); Assert.That(syncProgressResolver.FindBestFullState(), Is.EqualTo(suggested.Number)); } @@ -96,20 +97,20 @@ public void Best_state_is_suggested_if_there_is_suggested_block_with_state() public void Best_state_is_head_if_there_is_suggested_block_without_state() { IBlockTree blockTree = Substitute.For(); - IDb stateDb = new MemDb(); + IStateReader stateReader = Substitute.For(); SyncConfig syncConfig = new() { PivotNumber = "1", }; - SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateDb, NullTrieNodeResolver.Instance, false, syncConfig, LimboLogs.Instance); + SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateReader, false, syncConfig, LimboLogs.Instance); Block head = Build.A.Block.WithHeader(Build.A.BlockHeader.WithNumber(5).WithStateRoot(TestItem.KeccakA).TestObject).TestObject; BlockHeader suggested = Build.A.BlockHeader.WithNumber(6).WithStateRoot(TestItem.KeccakB).TestObject; blockTree.Head.Returns(head); blockTree.BestSuggestedHeader.Returns(suggested); blockTree.FindHeader(Arg.Any(), BlockTreeLookupOptions.TotalDifficultyNotNeeded).Returns(head.Header); - stateDb[head.StateRoot!.Bytes] = new byte[] { 1 }; - stateDb[suggested.StateRoot!.Bytes] = null; + stateReader.HasStateForRoot(head.StateRoot!).Returns(true); + stateReader.HasStateForRoot(suggested.StateRoot!).Returns(false); Assert.That(syncProgressResolver.FindBestFullState(), Is.EqualTo(head.Number)); } @@ -117,14 +118,14 @@ public void Best_state_is_head_if_there_is_suggested_block_without_state() public void Is_fast_block_finished_returns_true_when_no_fast_block_sync_is_used() { IBlockTree blockTree = Substitute.For(); - IDb stateDb = new MemDb(); + IStateReader stateReader = Substitute.For(); SyncConfig syncConfig = new() { FastBlocks = false, PivotNumber = "1", }; - SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateDb, NullTrieNodeResolver.Instance, false, syncConfig, LimboLogs.Instance); + SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateReader, false, syncConfig, LimboLogs.Instance); Assert.True(syncProgressResolver.IsFastBlocksHeadersFinished()); Assert.True(syncProgressResolver.IsFastBlocksBodiesFinished()); Assert.True(syncProgressResolver.IsFastBlocksReceiptsFinished()); @@ -134,7 +135,7 @@ public void Is_fast_block_finished_returns_true_when_no_fast_block_sync_is_used( public void Is_fast_block_bodies_finished_returns_false_when_blocks_not_downloaded() { IBlockTree blockTree = Substitute.For(); - IDb stateDb = new MemDb(); + IStateReader stateReader = Substitute.For(); SyncConfig syncConfig = new() { FastBlocks = true, @@ -147,7 +148,7 @@ public void Is_fast_block_bodies_finished_returns_false_when_blocks_not_download blockTree.LowestInsertedHeader.Returns(Build.A.BlockHeader.WithNumber(1).WithStateRoot(TestItem.KeccakA).TestObject); blockTree.LowestInsertedBodyNumber.Returns(2); - SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateDb, NullTrieNodeResolver.Instance, false, syncConfig, LimboLogs.Instance); + SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateReader, false, syncConfig, LimboLogs.Instance); Assert.False(syncProgressResolver.IsFastBlocksBodiesFinished()); } @@ -155,7 +156,7 @@ public void Is_fast_block_bodies_finished_returns_false_when_blocks_not_download public void Is_fast_block_receipts_finished_returns_true_when_receipts_not_downloaded_and_we_do_not_want_to_download_receipts() { IBlockTree blockTree = Substitute.For(); - IDb stateDb = new MemDb(); + IStateReader stateReader = Substitute.For(); SyncConfig syncConfig = new() { FastBlocks = true, @@ -167,19 +168,19 @@ public void Is_fast_block_receipts_finished_returns_true_when_receipts_not_downl blockTree.LowestInsertedHeader.Returns(Build.A.BlockHeader.WithNumber(1).WithStateRoot(TestItem.KeccakA).TestObject); blockTree.LowestInsertedBodyNumber.Returns(1); - SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateDb, NullTrieNodeResolver.Instance, true, syncConfig, LimboLogs.Instance); + SyncProgressResolver syncProgressResolver = CreateProgressResolver(blockTree, stateReader, true, syncConfig, LimboLogs.Instance); Assert.True(syncProgressResolver.IsFastBlocksReceiptsFinished()); } - private SyncProgressResolver CreateProgressResolver(IBlockTree blockTree, IDb stateDb, NullTrieNodeResolver nodeResolver, bool isReceiptFinished, SyncConfig syncConfig, LimboLogs limboLogs) + private SyncProgressResolver CreateProgressResolver(IBlockTree blockTree, IStateReader stateReader, bool isReceiptFinished, SyncConfig syncConfig, LimboLogs limboLogs) { ISyncFeed receiptFeed = Substitute.For>(); receiptFeed.IsFinished.Returns(isReceiptFinished); return new SyncProgressResolver( blockTree, - new FullStateFinder(blockTree, stateDb, nodeResolver), + new FullStateFinder(blockTree, stateReader), syncConfig, Substitute.For>(), Substitute.For>(), diff --git a/src/Nethermind/Nethermind.Synchronization.Test/SyncThreadTests.cs b/src/Nethermind/Nethermind.Synchronization.Test/SyncThreadTests.cs index f14ef9cd2d5..aac4193324f 100644 --- a/src/Nethermind/Nethermind.Synchronization.Test/SyncThreadTests.cs +++ b/src/Nethermind/Nethermind.Synchronization.Test/SyncThreadTests.cs @@ -363,6 +363,7 @@ private SyncTestContext CreateSyncManager(int index) trieStore.AsReadOnly(), bestPeerStrategy, new ChainSpec(), + stateReader, logManager); ISyncModeSelector selector = synchronizer.SyncModeSelector; diff --git a/src/Nethermind/Nethermind.Synchronization.Test/SynchronizerTests.cs b/src/Nethermind/Nethermind.Synchronization.Test/SynchronizerTests.cs index f739e907743..6807ea4cc0e 100644 --- a/src/Nethermind/Nethermind.Synchronization.Test/SynchronizerTests.cs +++ b/src/Nethermind/Nethermind.Synchronization.Test/SynchronizerTests.cs @@ -32,6 +32,7 @@ using Nethermind.Merge.Plugin.Synchronization; using Nethermind.Merge.Plugin.Test; using Nethermind.Specs.ChainSpecStyle; +using Nethermind.State; using Nethermind.State.Witnesses; using Nethermind.Synchronization.Blocks; using Nethermind.Synchronization.ParallelSync; @@ -332,7 +333,9 @@ ISyncConfig GetSyncConfig() => ? new MergeBetterPeerStrategy(totalDifficultyBetterPeerStrategy, poSSwitcher, beaconPivot, LimboLogs.Instance) : totalDifficultyBetterPeerStrategy; - FullStateFinder fullStateFinder = new FullStateFinder(BlockTree, stateDb, trieStore); + StateReader reader = new StateReader(trieStore, codeDb, LimboLogs.Instance); + + FullStateFinder fullStateFinder = new FullStateFinder(BlockTree, reader); SyncPeerPool = new SyncPeerPool(BlockTree, stats, bestPeerStrategy, _logManager, 25); Pivot pivot = new(syncConfig); @@ -369,6 +372,7 @@ ISyncConfig GetSyncConfig() => bestPeerStrategy, new ChainSpec(), No.BeaconSync, + reader, _logManager); } else @@ -394,6 +398,7 @@ ISyncConfig GetSyncConfig() => trieStore.AsReadOnly(), bestPeerStrategy, new ChainSpec(), + reader, _logManager); } diff --git a/src/Nethermind/Nethermind.Synchronization/ParallelSync/FullStateFinder.cs b/src/Nethermind/Nethermind.Synchronization/ParallelSync/FullStateFinder.cs index b885071f9f9..4b89d37cdad 100644 --- a/src/Nethermind/Nethermind.Synchronization/ParallelSync/FullStateFinder.cs +++ b/src/Nethermind/Nethermind.Synchronization/ParallelSync/FullStateFinder.cs @@ -7,6 +7,7 @@ using Nethermind.Core.Crypto; using Nethermind.Db; using Nethermind.Logging; +using Nethermind.State; using Nethermind.Trie; using Nethermind.Trie.Pruning; @@ -18,18 +19,15 @@ public class FullStateFinder : IFullStateFinder // then we will never have to look 128 back again // note that we will be doing that every second or so private const int MaxLookupBack = 128; - private readonly IDb _stateDb; - private readonly ITrieNodeResolver _trieNodeResolver; + private readonly IStateReader _stateReader; private readonly IBlockTree _blockTree; public FullStateFinder( IBlockTree blockTree, - IDb stateDb, - ITrieNodeResolver trieNodeResolver) + IStateReader stateReader) { _blockTree = blockTree ?? throw new ArgumentNullException(nameof(blockTree)); - _stateDb = stateDb ?? throw new ArgumentNullException(nameof(stateDb)); - _trieNodeResolver = trieNodeResolver ?? throw new ArgumentNullException(nameof(trieNodeResolver)); + _stateReader = stateReader ?? throw new ArgumentNullException(nameof(stateReader)); } private bool IsFullySynced(Hash256 stateRoot) @@ -39,15 +37,7 @@ private bool IsFullySynced(Hash256 stateRoot) return true; } - TrieNode trieNode = _trieNodeResolver.FindCachedOrUnknown(stateRoot); - bool stateRootIsInMemory = trieNode.NodeType != NodeType.Unknown; - // We check whether one of below happened: - // 1) the block has been processed but not yet persisted (pruning) OR - // 2) the block has been persisted and removed from cache already OR - // 3) the full block state has been synced in the state nodes sync (fast sync) - // In 2) and 3) the state root will be saved in the database. - // In fast sync we never save the state root unless all the descendant nodes have been stored in the DB. - return stateRootIsInMemory || _stateDb.Get(stateRoot) is not null; + return _stateReader.HasStateForRoot(stateRoot); } public long FindBestFullState() diff --git a/src/Nethermind/Nethermind.Synchronization/Synchronizer.cs b/src/Nethermind/Nethermind.Synchronization/Synchronizer.cs index 49fd21feec7..b5b3d951a0c 100644 --- a/src/Nethermind/Nethermind.Synchronization/Synchronizer.cs +++ b/src/Nethermind/Nethermind.Synchronization/Synchronizer.cs @@ -14,6 +14,7 @@ using Nethermind.Db; using Nethermind.Logging; using Nethermind.Specs.ChainSpecStyle; +using Nethermind.State; using Nethermind.Stats; using Nethermind.Stats.Model; using Nethermind.Synchronization.Blocks; @@ -76,7 +77,7 @@ public class Synchronizer : ISynchronizer private ISyncProgressResolver? _syncProgressResolver; public ISyncProgressResolver SyncProgressResolver => _syncProgressResolver ??= new SyncProgressResolver( _blockTree, - new FullStateFinder(_blockTree, _dbProvider.StateDb, _readOnlyTrieStore), + new FullStateFinder(_blockTree, _stateReader), _syncConfig, HeadersSyncFeed, BodiesSyncFeed, @@ -88,6 +89,8 @@ public class Synchronizer : ISynchronizer protected ISyncModeSelector? _syncModeSelector; + private readonly IStateReader _stateReader; + public virtual ISyncModeSelector SyncModeSelector => _syncModeSelector ??= new MultiSyncModeSelector( SyncProgressResolver, _syncPeerPool!, @@ -111,6 +114,7 @@ public Synchronizer( IReadOnlyTrieStore readOnlyTrieStore, IBetterPeerStrategy betterPeerStrategy, ChainSpec chainSpec, + IStateReader stateReader, ILogManager logManager) { _dbProvider = dbProvider ?? throw new ArgumentNullException(nameof(dbProvider)); @@ -128,6 +132,7 @@ public Synchronizer( _betterPeerStrategy = betterPeerStrategy ?? throw new ArgumentNullException(nameof(betterPeerStrategy)); _chainSpec = chainSpec ?? throw new ArgumentNullException(nameof(chainSpec)); _readOnlyTrieStore = readOnlyTrieStore ?? throw new ArgumentNullException(nameof(readOnlyTrieStore)); + _stateReader = stateReader ?? throw new ArgumentNullException(nameof(readOnlyTrieStore)); _syncReport = new SyncReport(_syncPeerPool!, nodeStatsManager!, _syncConfig, _pivot, logManager); From 27cbf4c2e45c664831ef5bb79f9312299d9c1a78 Mon Sep 17 00:00:00 2001 From: Amirul Ashraf Date: Wed, 8 Nov 2023 19:09:18 +0800 Subject: [PATCH 06/17] Unify and use property --- ...sts.TestAccountAbstractionRpcBlockchain.cs | 4 +- .../AccountAbstractionPlugin.cs | 2 +- .../Nethermind.Analytics/AnalyticsPlugin.cs | 2 +- .../Nethermind.Api/IApiWithBlockchain.cs | 2 +- .../Nethermind.Api/NethermindApi.cs | 4 +- .../AuRaContractGasLimitOverrideTests.cs | 2 +- .../Contract/TxPriorityContractTests.cs | 2 +- .../Transactions/TxCertifierFilterTests.cs | 2 +- .../Transactions/TxPermissionFilterTest.cs | 4 +- .../InitializeBlockchainAuRa.cs | 2 +- .../StartBlockProducerAuRa.cs | 4 +- .../CliquePlugin.cs | 4 +- .../NethDevPlugin.cs | 4 +- .../Processing/ReadOnlyTxProcessingEnv.cs | 10 ++--- .../ReadOnlyTxProcessingEnvFactory.cs | 12 +++--- .../Producers/BlockProducerEnvFactory.cs | 12 +++--- .../Blockchain/TestBlockchain.cs | 6 +-- .../BlockchainBridgeTests.cs | 12 +++--- .../Nethermind.Init/InitializeStateDb.cs | 11 +++-- .../Steps/InitializeBlockProducer.cs | 2 +- .../Steps/InitializeBlockchain.cs | 4 +- .../Steps/InitializeNetwork.cs | 2 +- .../Steps/RegisterRpcModules.cs | 10 ++--- .../Modules/Proof/ProofRpcModuleTests.cs | 8 ++-- .../Modules/TestRpcBlockchain.cs | 2 +- .../Modules/TraceRpcModuleTests.cs | 2 +- .../Modules/DebugModule/DebugModuleFactory.cs | 8 ++-- .../Modules/Proof/ProofModuleFactory.cs | 8 ++-- .../Modules/Trace/TraceModuleFactory.cs | 8 ++-- .../AuRaMergeEngineModuleTests.cs | 4 +- .../AuRaMergeBlockProducerEnvFactory.cs | 6 +-- .../Nethermind.Merge.AuRa/AuRaMergePlugin.cs | 2 +- .../EngineModuleTests.Setup.cs | 2 +- .../EngineModuleTests.Synchronization.cs | 2 +- .../MergePluginTests.cs | 2 +- .../Nethermind.Merge.Plugin/MergePlugin.cs | 8 ++-- .../MevRpcModuleTests.TestMevRpcBlockchain.cs | 4 +- .../Nethermind.Mev/Execution/TracerFactory.cs | 8 ++-- src/Nethermind/Nethermind.Mev/MevPlugin.cs | 4 +- .../InitializeBlockProducerOptimism.cs | 4 +- .../OptimismBlockProducerEnvFactory.cs | 8 ++-- .../Nethermind.Optimism/OptimismPlugin.cs | 4 +- .../Ethereum/ContextWithMocks.cs | 2 +- ...dStateFactory.cs => IWorldStateManager.cs} | 6 +-- ...actory.cs => ReadOnlyWorldStateManager.cs} | 17 +++----- .../Nethermind.State/WorldStateFactory.cs | 42 ------------------- .../Nethermind.State/WorldStateManager.cs | 27 ++++++++++++ 47 files changed, 143 insertions(+), 164 deletions(-) rename src/Nethermind/Nethermind.State/{IWorldStateFactory.cs => IWorldStateManager.cs} (64%) rename src/Nethermind/Nethermind.State/{ReadOnlyWorldStateFactory.cs => ReadOnlyWorldStateManager.cs} (73%) delete mode 100644 src/Nethermind/Nethermind.State/WorldStateFactory.cs create mode 100644 src/Nethermind/Nethermind.State/WorldStateManager.cs diff --git a/src/Nethermind/Nethermind.AccountAbstraction.Test/AccountAbstractionRpcModuleTests.TestAccountAbstractionRpcBlockchain.cs b/src/Nethermind/Nethermind.AccountAbstraction.Test/AccountAbstractionRpcModuleTests.TestAccountAbstractionRpcBlockchain.cs index 56279c499be..f976fe49fd3 100644 --- a/src/Nethermind/Nethermind.AccountAbstraction.Test/AccountAbstractionRpcModuleTests.TestAccountAbstractionRpcBlockchain.cs +++ b/src/Nethermind/Nethermind.AccountAbstraction.Test/AccountAbstractionRpcModuleTests.TestAccountAbstractionRpcBlockchain.cs @@ -99,7 +99,7 @@ protected override IBlockProducer CreateTestBlockProducer(TxPoolTxSource txPoolT SpecProvider.UpdateMergeTransitionInfo(1, 0); BlockProducerEnvFactory blockProducerEnvFactory = new BlockProducerEnvFactory( - ReadOnlyWorldStateFactory, + _readOnlyWorldStateManager, BlockTree, SpecProvider, BlockValidator, @@ -211,7 +211,7 @@ protected override BlockProcessor CreateBlockProcessor() UserOperationSimulator[entryPoint] = new( UserOperationTxBuilder[entryPoint], ReadOnlyState, - new ReadOnlyTxProcessingEnvFactory(ReadOnlyWorldStateFactory, BlockTree, SpecProvider, LogManager), + new ReadOnlyTxProcessingEnvFactory(_readOnlyWorldStateManager, BlockTree, SpecProvider, LogManager), EntryPointContractAbi, entryPoint!, WhitelistedPayamsters, diff --git a/src/Nethermind/Nethermind.AccountAbstraction/AccountAbstractionPlugin.cs b/src/Nethermind/Nethermind.AccountAbstraction/AccountAbstractionPlugin.cs index d95c3308e80..6d7f1145b67 100644 --- a/src/Nethermind/Nethermind.AccountAbstraction/AccountAbstractionPlugin.cs +++ b/src/Nethermind/Nethermind.AccountAbstraction/AccountAbstractionPlugin.cs @@ -121,7 +121,7 @@ private UserOperationSimulator UserOperationSimulator(Address entryPoint) var (getFromApi, _) = _nethermindApi!.ForProducer; ReadOnlyTxProcessingEnvFactory readOnlyTxProcessingEnvFactory = new( - getFromApi.ReadOnlyWorldStateFactory!, + getFromApi.WorldStateManager!, getFromApi.BlockTree, getFromApi.SpecProvider, getFromApi.LogManager); diff --git a/src/Nethermind/Nethermind.Analytics/AnalyticsPlugin.cs b/src/Nethermind/Nethermind.Analytics/AnalyticsPlugin.cs index a3447dff5f9..d483df6c7fc 100644 --- a/src/Nethermind/Nethermind.Analytics/AnalyticsPlugin.cs +++ b/src/Nethermind/Nethermind.Analytics/AnalyticsPlugin.cs @@ -91,7 +91,7 @@ public Task InitRpcModules() { var (getFromAPi, _) = _api.ForRpc; AnalyticsRpcModule analyticsRpcModule = new( - getFromAPi.BlockTree, getFromAPi.ReadOnlyWorldStateFactory!.CreateStateReader(), getFromAPi.LogManager); + getFromAPi.BlockTree, getFromAPi.WorldStateManager!.GlobalStateReader, getFromAPi.LogManager); getFromAPi.RpcModuleProvider.Register(new SingletonModulePool(analyticsRpcModule)); return Task.CompletedTask; } diff --git a/src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs b/src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs index 6ae50ce9143..a8b108393c5 100644 --- a/src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs +++ b/src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs @@ -60,7 +60,7 @@ public interface IApiWithBlockchain : IApiWithStores, IBlockchainBridgeFactory /// IWorldState? WorldState { get; set; } IReadOnlyStateProvider? ChainHeadStateProvider { get; set; } - IWorldStateFactory? ReadOnlyWorldStateFactory { get; set; } + IWorldStateManager? WorldStateManager { get; set; } ITransactionProcessor? TransactionProcessor { get; set; } ITrieStore? TrieStore { get; set; } ITxSender? TxSender { get; set; } diff --git a/src/Nethermind/Nethermind.Api/NethermindApi.cs b/src/Nethermind/Nethermind.Api/NethermindApi.cs index c25df50a58e..18ce2c7d9a5 100644 --- a/src/Nethermind/Nethermind.Api/NethermindApi.cs +++ b/src/Nethermind/Nethermind.Api/NethermindApi.cs @@ -76,7 +76,7 @@ public IBlockchainBridge CreateBlockchainBridge() // TODO: reuse the same trie cache here ReadOnlyTxProcessingEnv readOnlyTxProcessingEnv = new( - ReadOnlyWorldStateFactory!, + WorldStateManager!, readOnlyTree, SpecProvider, LogManager); @@ -187,7 +187,7 @@ public ISealEngine SealEngine public ISyncServer? SyncServer { get; set; } public IWorldState? WorldState { get; set; } public IReadOnlyStateProvider? ChainHeadStateProvider { get; set; } - public IWorldStateFactory? ReadOnlyWorldStateFactory { get; set; } + public IWorldStateManager? WorldStateManager { get; set; } public IStateReader? StateReader { get; set; } public IStaticNodesManager? StaticNodesManager { get; set; } public ITimestamper Timestamper { get; } = Core.Timestamper.Default; diff --git a/src/Nethermind/Nethermind.AuRa.Test/Contract/AuRaContractGasLimitOverrideTests.cs b/src/Nethermind/Nethermind.AuRa.Test/Contract/AuRaContractGasLimitOverrideTests.cs index 708ef254478..defd82ddb06 100644 --- a/src/Nethermind/Nethermind.AuRa.Test/Contract/AuRaContractGasLimitOverrideTests.cs +++ b/src/Nethermind/Nethermind.AuRa.Test/Contract/AuRaContractGasLimitOverrideTests.cs @@ -83,7 +83,7 @@ protected override BlockProcessor CreateBlockProcessor() KeyValuePair blockGasLimitContractTransition = ChainSpec.AuRa.BlockGasLimitContractTransitions.First(); BlockGasLimitContract gasLimitContract = new(AbiEncoder.Instance, blockGasLimitContractTransition.Value, blockGasLimitContractTransition.Key, new ReadOnlyTxProcessingEnv( - ReadOnlyWorldStateFactory, + _readOnlyWorldStateManager, BlockTree, SpecProvider, LimboLogs.Instance)); GasLimitOverrideCache = new AuRaContractGasLimitOverride.Cache(); diff --git a/src/Nethermind/Nethermind.AuRa.Test/Contract/TxPriorityContractTests.cs b/src/Nethermind/Nethermind.AuRa.Test/Contract/TxPriorityContractTests.cs index 78e24ed4cb3..416a338de0f 100644 --- a/src/Nethermind/Nethermind.AuRa.Test/Contract/TxPriorityContractTests.cs +++ b/src/Nethermind/Nethermind.AuRa.Test/Contract/TxPriorityContractTests.cs @@ -254,7 +254,7 @@ protected override TxPoolTxSource CreateTxPoolTxSource() TxPoolTxSource txPoolTxSource = base.CreateTxPoolTxSource(); TxPriorityContract = new TxPriorityContract(AbiEncoder.Instance, TestItem.AddressA, - new ReadOnlyTxProcessingEnv(ReadOnlyWorldStateFactory, BlockTree, SpecProvider, LimboLogs.Instance)); + new ReadOnlyTxProcessingEnv(_readOnlyWorldStateManager, BlockTree, SpecProvider, LimboLogs.Instance)); Priorities = new DictionaryContractDataStore( new TxPriorityContract.DestinationSortedListContractDataStoreCollection(), diff --git a/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxCertifierFilterTests.cs b/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxCertifierFilterTests.cs index 4d907fd786c..c9fbcbcb847 100644 --- a/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxCertifierFilterTests.cs +++ b/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxCertifierFilterTests.cs @@ -137,7 +137,7 @@ protected override BlockProcessor CreateBlockProcessor() { AbiEncoder abiEncoder = AbiEncoder.Instance; ReadOnlyTransactionProcessorSource = new ReadOnlyTxProcessingEnv( - ReadOnlyWorldStateFactory, + _readOnlyWorldStateManager, BlockTree, SpecProvider, LimboLogs.Instance); RegisterContract = new RegisterContract(abiEncoder, ChainSpec.Parameters.Registrar, ReadOnlyTransactionProcessorSource); diff --git a/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxPermissionFilterTest.cs b/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxPermissionFilterTest.cs index cea9fcce901..ca279aa5de5 100644 --- a/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxPermissionFilterTest.cs +++ b/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxPermissionFilterTest.cs @@ -276,13 +276,13 @@ protected override BlockProcessor CreateBlockProcessor() IReadOnlyTrieStore trieStore = new TrieStore(DbProvider.StateDb, LimboLogs.Instance).AsReadOnly(); IReadOnlyTxProcessorSource txProcessorSource = new ReadOnlyTxProcessingEnv( - ReadOnlyWorldStateFactory, + _readOnlyWorldStateManager, BlockTree, SpecProvider, LimboLogs.Instance); VersionedTransactionPermissionContract transactionPermissionContract = new(AbiEncoder.Instance, _contractAddress, 1, - new ReadOnlyTxProcessingEnv(ReadOnlyWorldStateFactory, BlockTree, SpecProvider, LimboLogs.Instance), TransactionPermissionContractVersions, LimboLogs.Instance, SpecProvider); + new ReadOnlyTxProcessingEnv(_readOnlyWorldStateManager, BlockTree, SpecProvider, LimboLogs.Instance), TransactionPermissionContractVersions, LimboLogs.Instance, SpecProvider); TxPermissionFilterCache = new PermissionBasedTxFilter.Cache(); PermissionBasedTxFilter = new PermissionBasedTxFilter(transactionPermissionContract, TxPermissionFilterCache, LimboLogs.Instance); diff --git a/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/InitializeBlockchainAuRa.cs b/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/InitializeBlockchainAuRa.cs index 38dfaf43cb0..a229f242635 100644 --- a/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/InitializeBlockchainAuRa.cs +++ b/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/InitializeBlockchainAuRa.cs @@ -106,7 +106,7 @@ protected virtual BlockProcessor NewBlockProcessor(AuRaNethermindApi api, ITxFil } protected ReadOnlyTxProcessingEnv CreateReadOnlyTransactionProcessorSource() => - new ReadOnlyTxProcessingEnv(_api.ReadOnlyWorldStateFactory!, _api.BlockTree, _api.SpecProvider, _api.LogManager); + new ReadOnlyTxProcessingEnv(_api.WorldStateManager!, _api.BlockTree, _api.SpecProvider, _api.LogManager); protected override IHealthHintService CreateHealthHintService() => new AuraHealthHintService(_auRaStepCalculator, _api.ValidatorStore); diff --git a/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/StartBlockProducerAuRa.cs b/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/StartBlockProducerAuRa.cs index a3b909d5403..559e6303235 100644 --- a/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/StartBlockProducerAuRa.cs +++ b/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/StartBlockProducerAuRa.cs @@ -171,7 +171,7 @@ internal TxPoolTxSource CreateTxPoolTxSource(ReadOnlyTxProcessingEnv processingE { // We need special one for TxPriority as its following Head separately with events and we want rules from Head, not produced block IReadOnlyTxProcessorSource readOnlyTxProcessorSourceForTxPriority = - new ReadOnlyTxProcessingEnv(_api.ReadOnlyWorldStateFactory!, _api.BlockTree, _api.SpecProvider, _api.LogManager); + new ReadOnlyTxProcessingEnv(_api.WorldStateManager!, _api.BlockTree, _api.SpecProvider, _api.LogManager); (_txPriorityContract, _localDataSource) = TxAuRaFilterBuilders.CreateTxPrioritySources(_auraConfig, _api, readOnlyTxProcessorSourceForTxPriority); @@ -230,7 +230,7 @@ private BlockProducerEnv GetProducerChain(ITxSource? additionalTxSource) { ReadOnlyTxProcessingEnv CreateReadonlyTxProcessingEnv(ReadOnlyBlockTree blockTree) { - return new(_api.ReadOnlyWorldStateFactory!, blockTree, _api.SpecProvider, _api.LogManager); + return new(_api.WorldStateManager!, blockTree, _api.SpecProvider, _api.LogManager); } BlockProducerEnv Create() diff --git a/src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs b/src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs index 9470a73ddfb..a97f6f3e762 100644 --- a/src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs +++ b/src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs @@ -94,7 +94,7 @@ public Task InitBlockProducer(IBlockProductionTrigger? blockProd ITransactionComparerProvider transactionComparerProvider = getFromApi.TransactionComparerProvider; ReadOnlyTxProcessingEnv producerEnv = new( - _nethermindApi.ReadOnlyWorldStateFactory!, + _nethermindApi.WorldStateManager!, readOnlyBlockTree, getFromApi.SpecProvider, getFromApi.LogManager); @@ -114,7 +114,7 @@ public Task InitBlockProducer(IBlockProductionTrigger? blockProd readOnlyBlockTree, producerProcessor, getFromApi.BlockPreprocessor, - getFromApi.ReadOnlyWorldStateFactory!.CreateStateReader(), + getFromApi.WorldStateManager!.GlobalStateReader, getFromApi.LogManager, BlockchainProcessor.Options.NoReceipts); diff --git a/src/Nethermind/Nethermind.Consensus.Ethash/NethDevPlugin.cs b/src/Nethermind/Nethermind.Consensus.Ethash/NethDevPlugin.cs index bdea51027bf..b5254e1ade3 100644 --- a/src/Nethermind/Nethermind.Consensus.Ethash/NethDevPlugin.cs +++ b/src/Nethermind/Nethermind.Consensus.Ethash/NethDevPlugin.cs @@ -64,7 +64,7 @@ public Task InitBlockProducer(IBlockProductionTrigger? blockProd if (logger.IsInfo) logger.Info("Starting Neth Dev block producer & sealer"); ReadOnlyTxProcessingEnv producerEnv = new( - _nethermindApi.ReadOnlyWorldStateFactory!, + _nethermindApi.WorldStateManager!, readOnlyBlockTree, getFromApi.SpecProvider, getFromApi.LogManager); @@ -83,7 +83,7 @@ public Task InitBlockProducer(IBlockProductionTrigger? blockProd readOnlyBlockTree, producerProcessor, getFromApi.BlockPreprocessor, - getFromApi.ReadOnlyWorldStateFactory!.CreateStateReader(), + getFromApi.WorldStateManager!.GlobalStateReader, getFromApi.LogManager, BlockchainProcessor.Options.NoReceipts); diff --git a/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyTxProcessingEnv.cs b/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyTxProcessingEnv.cs index a60afdb8d5f..df480143b76 100644 --- a/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyTxProcessingEnv.cs +++ b/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyTxProcessingEnv.cs @@ -25,24 +25,24 @@ public class ReadOnlyTxProcessingEnv : IReadOnlyTxProcessorSource public Action ResetDb { get; } public ReadOnlyTxProcessingEnv( - IWorldStateFactory worldStateFactory, + IWorldStateManager worldStateManager, IBlockTree? blockTree, ISpecProvider? specProvider, ILogManager? logManager) - : this(worldStateFactory, blockTree?.AsReadOnly(), specProvider, logManager) + : this(worldStateManager, blockTree?.AsReadOnly(), specProvider, logManager) { } public ReadOnlyTxProcessingEnv( - IWorldStateFactory worldStateFactory, + IWorldStateManager worldStateManager, IReadOnlyBlockTree? readOnlyBlockTree, ISpecProvider? specProvider, ILogManager? logManager) { if (specProvider is null) throw new ArgumentNullException(nameof(specProvider)); - if (worldStateFactory is null) throw new ArgumentNullException(nameof(worldStateFactory)); + if (worldStateManager is null) throw new ArgumentNullException(nameof(worldStateManager)); - (IWorldState worldState, IStateReader stateReader, Action reset) = worldStateFactory.CreateResettableWorldState(); + (IWorldState worldState, IStateReader stateReader, Action reset) = worldStateManager.CreateResettableWorldState(); StateReader = stateReader; StateProvider = worldState; ResetDb = reset; diff --git a/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyTxProcessingEnvFactory.cs b/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyTxProcessingEnvFactory.cs index b0ceb7901e7..1408e3c547e 100644 --- a/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyTxProcessingEnvFactory.cs +++ b/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyTxProcessingEnvFactory.cs @@ -12,31 +12,31 @@ namespace Nethermind.Consensus.Processing; public class ReadOnlyTxProcessingEnvFactory { - private readonly IWorldStateFactory _worldStateFactory; + private readonly IWorldStateManager _worldStateManager; private readonly IReadOnlyBlockTree? _readOnlyBlockTree; private readonly ISpecProvider? _specProvider; private readonly ILogManager? _logManager; public ReadOnlyTxProcessingEnvFactory( - IWorldStateFactory worldStateFactory, + IWorldStateManager worldStateManager, IBlockTree? blockTree, ISpecProvider? specProvider, ILogManager? logManager) - : this(worldStateFactory, blockTree?.AsReadOnly(), specProvider, logManager) + : this(worldStateManager, blockTree?.AsReadOnly(), specProvider, logManager) { } public ReadOnlyTxProcessingEnvFactory( - IWorldStateFactory worldStateFactory, + IWorldStateManager worldStateManager, IReadOnlyBlockTree? readOnlyBlockTree, ISpecProvider? specProvider, ILogManager? logManager) { - _worldStateFactory = worldStateFactory; + _worldStateManager = worldStateManager; _readOnlyBlockTree = readOnlyBlockTree; _specProvider = specProvider; _logManager = logManager; } - public ReadOnlyTxProcessingEnv Create() => new(_worldStateFactory, _readOnlyBlockTree, _specProvider, _logManager); + public ReadOnlyTxProcessingEnv Create() => new(_worldStateManager, _readOnlyBlockTree, _specProvider, _logManager); } diff --git a/src/Nethermind/Nethermind.Consensus/Producers/BlockProducerEnvFactory.cs b/src/Nethermind/Nethermind.Consensus/Producers/BlockProducerEnvFactory.cs index 421dee82481..2670664d742 100644 --- a/src/Nethermind/Nethermind.Consensus/Producers/BlockProducerEnvFactory.cs +++ b/src/Nethermind/Nethermind.Consensus/Producers/BlockProducerEnvFactory.cs @@ -20,7 +20,7 @@ namespace Nethermind.Consensus.Producers { public class BlockProducerEnvFactory : IBlockProducerEnvFactory { - protected readonly IWorldStateFactory _worldStateFactory; + protected readonly IWorldStateManager _worldStateManager; protected readonly IBlockTree _blockTree; protected readonly ISpecProvider _specProvider; protected readonly IBlockValidator _blockValidator; @@ -35,7 +35,7 @@ public class BlockProducerEnvFactory : IBlockProducerEnvFactory public IBlockTransactionsExecutorFactory TransactionsExecutorFactory { get; set; } public BlockProducerEnvFactory( - IWorldStateFactory worldStateFactory, + IWorldStateManager worldStateManager, IBlockTree blockTree, ISpecProvider specProvider, IBlockValidator blockValidator, @@ -47,7 +47,7 @@ public BlockProducerEnvFactory( IBlocksConfig blocksConfig, ILogManager logManager) { - _worldStateFactory = worldStateFactory; + _worldStateManager = worldStateManager; _blockTree = blockTree; _specProvider = specProvider; _blockValidator = blockValidator; @@ -67,7 +67,7 @@ public virtual BlockProducerEnv Create(ITxSource? additionalTxSource = null) ReadOnlyBlockTree readOnlyBlockTree = _blockTree.AsReadOnly(); ReadOnlyTxProcessingEnv txProcessingEnv = - CreateReadonlyTxProcessingEnv(_worldStateFactory, readOnlyBlockTree); + CreateReadonlyTxProcessingEnv(_worldStateManager, readOnlyBlockTree); BlockProcessor blockProcessor = CreateBlockProcessor(txProcessingEnv, @@ -101,8 +101,8 @@ public virtual BlockProducerEnv Create(ITxSource? additionalTxSource = null) }; } - protected virtual ReadOnlyTxProcessingEnv CreateReadonlyTxProcessingEnv(IWorldStateFactory worldStateFactory, ReadOnlyBlockTree readOnlyBlockTree) => - new(worldStateFactory, readOnlyBlockTree, _specProvider, _logManager); + protected virtual ReadOnlyTxProcessingEnv CreateReadonlyTxProcessingEnv(IWorldStateManager worldStateManager, ReadOnlyBlockTree readOnlyBlockTree) => + new(worldStateManager, readOnlyBlockTree, _specProvider, _logManager); protected virtual ITxSource CreateTxSourceForProducer( ITxSource? additionalTxSource, diff --git a/src/Nethermind/Nethermind.Core.Test/Blockchain/TestBlockchain.cs b/src/Nethermind/Nethermind.Core.Test/Blockchain/TestBlockchain.cs index 270e4709b09..103a6d48a3f 100644 --- a/src/Nethermind/Nethermind.Core.Test/Blockchain/TestBlockchain.cs +++ b/src/Nethermind/Nethermind.Core.Test/Blockchain/TestBlockchain.cs @@ -49,7 +49,7 @@ public class TestBlockchain : IDisposable public IReceiptStorage ReceiptStorage { get; set; } = null!; public ITxPool TxPool { get; set; } = null!; public IDb CodeDb => DbProvider.CodeDb; - public IWorldStateFactory ReadOnlyWorldStateFactory = null!; + public IWorldStateManager _readOnlyWorldStateManager = null!; public IBlockProcessor BlockProcessor { get; set; } = null!; public IBeaconBlockRootHandler BeaconBlockRootHandler { get; set; } = null!; public IBlockchainProcessor BlockchainProcessor { get; set; } = null!; @@ -140,7 +140,7 @@ protected virtual async Task Build(ISpecProvider? specProvider = State.CommitTree(0); ReadOnlyTrieStore = TrieStore.AsReadOnly(StateDb); - ReadOnlyWorldStateFactory = new ReadOnlyWorldStateFactory(DbProvider, ReadOnlyTrieStore, LimboLogs.Instance); + _readOnlyWorldStateManager = new ReadOnlyWorldStateManager(DbProvider, ReadOnlyTrieStore, LimboLogs.Instance); StateReader = new StateReader(ReadOnlyTrieStore, CodeDb, LogManager); BlockTree = Builders.Build.A.BlockTree() @@ -249,7 +249,7 @@ protected virtual IBlockProducer CreateTestBlockProducer(TxPoolTxSource txPoolTx BlocksConfig blocksConfig = new(); BlockProducerEnvFactory blockProducerEnvFactory = new( - ReadOnlyWorldStateFactory, + _readOnlyWorldStateManager, BlockTree, SpecProvider, BlockValidator, diff --git a/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs b/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs index b40c63af2b3..ec3713b966b 100644 --- a/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs +++ b/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs @@ -64,11 +64,11 @@ public async Task SetUp() ReadOnlyDbProvider dbProvider = new ReadOnlyDbProvider(_dbProvider, false); IReadOnlyTrieStore trieStore = new TrieStore(_dbProvider.StateDb, LimboLogs.Instance).AsReadOnly(); - IWorldStateFactory readOnlyWorldStateFactory = - new ReadOnlyWorldStateFactory(dbProvider, trieStore, LimboLogs.Instance); + IWorldStateManager readOnlyWorldStateManager = + new ReadOnlyWorldStateManager(dbProvider, trieStore, LimboLogs.Instance); ReadOnlyTxProcessingEnv processingEnv = new( - readOnlyWorldStateFactory, + readOnlyWorldStateManager, new ReadOnlyBlockTree(_blockTree), _specProvider, LimboLogs.Instance); @@ -213,11 +213,11 @@ public void Bridge_head_is_correct(long headNumber) ReadOnlyDbProvider dbProvider = new ReadOnlyDbProvider(_dbProvider, false); IReadOnlyTrieStore trieStore = new TrieStore(_dbProvider.StateDb, LimboLogs.Instance).AsReadOnly(); - IWorldStateFactory readOnlyWorldStateFactory = - new ReadOnlyWorldStateFactory(dbProvider, trieStore, LimboLogs.Instance); + IWorldStateManager readOnlyWorldStateManager = + new ReadOnlyWorldStateManager(dbProvider, trieStore, LimboLogs.Instance); ReadOnlyTxProcessingEnv processingEnv = new( - readOnlyWorldStateFactory, + readOnlyWorldStateManager, new ReadOnlyBlockTree(_blockTree), _specProvider, LimboLogs.Instance); diff --git a/src/Nethermind/Nethermind.Init/InitializeStateDb.cs b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs index a615a425bee..d409d75a854 100644 --- a/src/Nethermind/Nethermind.Init/InitializeStateDb.cs +++ b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs @@ -125,8 +125,6 @@ public Task Execute(CancellationToken cancellationToken) codeDb, getApi.LogManager); - WorldStateFactory stateFactory = new WorldStateFactory(trieStore, codeDb, getApi.LogManager); - if (pruningConfig.Mode.IsFull()) { IFullPruningDb fullPruningDb = (IFullPruningDb)getApi.DbProvider!.StateDb; @@ -144,12 +142,13 @@ public Task Execute(CancellationToken cancellationToken) IReadOnlyTrieStore readOnlyTrieStore = setApi.ReadOnlyTrieStore = trieStore.AsReadOnly(cachedStateDb); - IWorldStateFactory readOnlyStateFactory = setApi.ReadOnlyWorldStateFactory = new ReadOnlyWorldStateFactory( + IWorldStateManager readOnlyStateManager = setApi.WorldStateManager = new WorldStateManager( + worldState, getApi.DbProvider, readOnlyTrieStore, getApi.LogManager); - setApi.ChainHeadStateProvider = new ChainHeadReadOnlyStateProvider(getApi.BlockTree, readOnlyStateFactory.CreateStateReader()); + setApi.ChainHeadStateProvider = new ChainHeadReadOnlyStateProvider(getApi.BlockTree, readOnlyStateManager.GlobalStateReader); worldState.StateRoot = getApi.BlockTree!.Head?.StateRoot ?? Keccak.EmptyTreeHash; @@ -160,7 +159,7 @@ public Task Execute(CancellationToken cancellationToken) try { _logger!.Info("Collecting trie stats and verifying that no nodes are missing..."); - IWorldState diagStateProvider = readOnlyStateFactory.CreateWorldState(); + IWorldState diagStateProvider = readOnlyStateManager.GlobalWorldState; diagStateProvider.StateRoot = getApi.BlockTree!.Head?.StateRoot ?? Keccak.EmptyTreeHash; TrieStats stats = diagStateProvider.CollectStats(getApi.DbProvider.CodeDb, _api.LogManager); _logger.Info($"Starting from {getApi.BlockTree.Head?.Number} {getApi.BlockTree.Head?.StateRoot}{Environment.NewLine}" + stats); @@ -178,7 +177,7 @@ public Task Execute(CancellationToken cancellationToken) worldState.StateRoot = getApi.BlockTree.Head.StateRoot; } - InitializeFullPruning(pruningConfig, initConfig, _api, readOnlyStateFactory.CreateStateReader()); + InitializeFullPruning(pruningConfig, initConfig, _api, readOnlyStateManager.GlobalStateReader); return Task.CompletedTask; } diff --git a/src/Nethermind/Nethermind.Init/Steps/InitializeBlockProducer.cs b/src/Nethermind/Nethermind.Init/Steps/InitializeBlockProducer.cs index 0a9b7b2b110..0fa117e656e 100644 --- a/src/Nethermind/Nethermind.Init/Steps/InitializeBlockProducer.cs +++ b/src/Nethermind/Nethermind.Init/Steps/InitializeBlockProducer.cs @@ -33,7 +33,7 @@ public async Task Execute(CancellationToken _) protected virtual async Task BuildProducer() { _api.BlockProducerEnvFactory = new BlockProducerEnvFactory( - _api.ReadOnlyWorldStateFactory!, + _api.WorldStateManager!, _api.BlockTree!, _api.SpecProvider!, _api.BlockValidator!, diff --git a/src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs b/src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs index 5213ff5b2f1..fef3be37609 100644 --- a/src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs +++ b/src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs @@ -70,7 +70,7 @@ protected virtual Task InitBlockchain() IInitConfig initConfig = getApi.Config(); IBlocksConfig blocksConfig = getApi.Config(); - IStateReader stateReader = setApi.ReadOnlyWorldStateFactory!.CreateStateReader(); + IStateReader stateReader = setApi.WorldStateManager!.GlobalStateReader; ITxPool txPool = _api.TxPool = CreateTxPool(); ReceiptCanonicalityMonitor receiptCanonicalityMonitor = new(getApi.BlockTree, getApi.ReceiptStorage, _api.LogManager); @@ -185,7 +185,7 @@ protected virtual IBlockProductionPolicy CreateBlockProductionPolicy() => protected virtual TxPool.TxPool CreateTxPool() => new(_api.EthereumEcdsa!, _api.BlobTxStorage ?? NullBlobTxStorage.Instance, - new ChainHeadInfoProvider(_api.SpecProvider!, _api.BlockTree!, _api.ReadOnlyWorldStateFactory!.CreateStateReader()!), + new ChainHeadInfoProvider(_api.SpecProvider!, _api.BlockTree!, _api.WorldStateManager!.GlobalStateReader!), _api.Config(), _api.TxValidator!, _api.LogManager, diff --git a/src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs b/src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs index 1955d9e102d..fbc1d584feb 100644 --- a/src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs +++ b/src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs @@ -155,7 +155,7 @@ private async Task Initialize(CancellationToken cancellationToken) _api.ReadOnlyTrieStore!, _api.BetterPeerStrategy, _api.ChainSpec, - _api.ReadOnlyWorldStateFactory!.CreateStateReader(), + _api.WorldStateManager!.GlobalStateReader, _api.LogManager); } diff --git a/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs b/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs index 164de2c0d19..9c8a89b27c4 100644 --- a/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs +++ b/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs @@ -60,7 +60,7 @@ public virtual async Task Execute(CancellationToken cancellationToken) if (_api.SpecProvider is null) throw new StepDependencyException(nameof(_api.SpecProvider)); if (_api.SyncModeSelector is null) throw new StepDependencyException(nameof(_api.SyncModeSelector)); if (_api.TxSender is null) throw new StepDependencyException(nameof(_api.TxSender)); - if (_api.ReadOnlyWorldStateFactory is null) throw new StepDependencyException(nameof(_api.ReadOnlyWorldStateFactory)); + if (_api.WorldStateManager is null) throw new StepDependencyException(nameof(_api.WorldStateManager)); if (_api.PeerManager is null) throw new StepDependencyException(nameof(_api.PeerManager)); if (jsonRpcConfig.Enabled) @@ -98,7 +98,7 @@ public virtual async Task Execute(CancellationToken cancellationToken) _api.BlockTree, rpcConfig, _api.LogManager, - _api.ReadOnlyWorldStateFactory.CreateStateReader(), + _api.WorldStateManager.GlobalStateReader, _api, _api.SpecProvider, _api.ReceiptStorage, @@ -116,11 +116,11 @@ public virtual async Task Execute(CancellationToken cancellationToken) if (_api.PeerPool is null) throw new StepDependencyException(nameof(_api.PeerPool)); if (_api.WitnessRepository is null) throw new StepDependencyException(nameof(_api.WitnessRepository)); - ProofModuleFactory proofModuleFactory = new(_api.ReadOnlyWorldStateFactory, _api.BlockTree, _api.BlockPreprocessor, _api.ReceiptFinder, _api.SpecProvider, _api.LogManager); + ProofModuleFactory proofModuleFactory = new(_api.WorldStateManager, _api.BlockTree, _api.BlockPreprocessor, _api.ReceiptFinder, _api.SpecProvider, _api.LogManager); rpcModuleProvider.RegisterBounded(proofModuleFactory, 2, rpcConfig.Timeout); DebugModuleFactory debugModuleFactory = new( - _api.ReadOnlyWorldStateFactory, + _api.WorldStateManager, _api.DbProvider, _api.BlockTree, rpcConfig, @@ -138,7 +138,7 @@ public virtual async Task Execute(CancellationToken cancellationToken) rpcModuleProvider.RegisterBoundedByCpuCount(debugModuleFactory, rpcConfig.Timeout); TraceModuleFactory traceModuleFactory = new( - _api.ReadOnlyWorldStateFactory, + _api.WorldStateManager, _api.BlockTree, rpcConfig, _api.BlockPreprocessor, diff --git a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Proof/ProofRpcModuleTests.cs b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Proof/ProofRpcModuleTests.cs index 3ed217af592..aabb92d9c02 100644 --- a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Proof/ProofRpcModuleTests.cs +++ b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Proof/ProofRpcModuleTests.cs @@ -44,7 +44,7 @@ public class ProofRpcModuleTests private IBlockTree _blockTree = null!; private IDbProvider _dbProvider = null!; private TestSpecProvider _specProvider = null!; - private ReadOnlyWorldStateFactory _readOnlyWorldStateFactory = null!; + private ReadOnlyWorldStateManager _readOnlyWorldStateManager = null!; public ProofRpcModuleTests(bool createSystemAccount, bool useNonZeroGasPrice) { @@ -61,10 +61,10 @@ public async Task Setup() _dbProvider = await TestMemDbProvider.InitAsync(); ITrieStore trieStore = new TrieStore(_dbProvider.StateDb, LimboLogs.Instance); - _readOnlyWorldStateFactory = new ReadOnlyWorldStateFactory(_dbProvider, trieStore.AsReadOnly(), LimboLogs.Instance); + _readOnlyWorldStateManager = new ReadOnlyWorldStateManager(_dbProvider, trieStore.AsReadOnly(), LimboLogs.Instance); ProofModuleFactory moduleFactory = new( - _readOnlyWorldStateFactory, + _readOnlyWorldStateManager, _blockTree, new CompositeBlockPreprocessorStep(new RecoverSignatures(new EthereumEcdsa(TestBlockchainIds.ChainId, LimboLogs.Instance), NullTxPool.Instance, _specProvider, LimboLogs.Instance)), receiptStorage, @@ -208,7 +208,7 @@ public async Task Get_receipt_when_block_has_few_receipts(bool withHeader, strin _receiptFinder.FindBlockHash(Arg.Any()).Returns(_blockTree.FindBlock(1)!.Hash); ProofModuleFactory moduleFactory = new ProofModuleFactory( - _readOnlyWorldStateFactory, + _readOnlyWorldStateManager, _blockTree, new CompositeBlockPreprocessorStep(new RecoverSignatures(new EthereumEcdsa(TestBlockchainIds.ChainId, LimboLogs.Instance), NullTxPool.Instance, _specProvider, LimboLogs.Instance)), _receiptFinder, diff --git a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TestRpcBlockchain.cs b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TestRpcBlockchain.cs index 52ee9ba91d5..0a0c5c49e75 100644 --- a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TestRpcBlockchain.cs +++ b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TestRpcBlockchain.cs @@ -123,7 +123,7 @@ protected override async Task Build(ISpecProvider? specProvider IFilterManager filterManager = new FilterManager(filterStore, BlockProcessor, TxPool, LimboLogs.Instance); ReadOnlyTxProcessingEnv processingEnv = new( - ReadOnlyWorldStateFactory, + _readOnlyWorldStateManager, new ReadOnlyBlockTree(BlockTree), SpecProvider, LimboLogs.Instance); diff --git a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TraceRpcModuleTests.cs b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TraceRpcModuleTests.cs index 19de500d095..b4b7181b16e 100644 --- a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TraceRpcModuleTests.cs +++ b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TraceRpcModuleTests.cs @@ -50,7 +50,7 @@ public async Task Build(ISpecProvider? specProvider = null, bool isAura = false) new(Blockchain.EthereumEcdsa, Blockchain.SpecProvider); IReceiptFinder receiptFinder = new FullInfoReceiptFinder(Blockchain.ReceiptStorage, receiptsRecovery, Blockchain.BlockFinder); ReadOnlyTxProcessingEnv txProcessingEnv = - new(Blockchain.ReadOnlyWorldStateFactory, Blockchain.BlockTree.AsReadOnly(), Blockchain.SpecProvider, Blockchain.LogManager); + new(Blockchain._readOnlyWorldStateManager, Blockchain.BlockTree.AsReadOnly(), Blockchain.SpecProvider, Blockchain.LogManager); RewardCalculator rewardCalculatorSource = new(Blockchain.SpecProvider); IRewardCalculator rewardCalculator = rewardCalculatorSource.Get(txProcessingEnv.TransactionProcessor); diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/DebugModule/DebugModuleFactory.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/DebugModule/DebugModuleFactory.cs index 3d8166d0d04..87bad42c986 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/DebugModule/DebugModuleFactory.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/DebugModule/DebugModuleFactory.cs @@ -25,7 +25,7 @@ namespace Nethermind.JsonRpc.Modules.DebugModule; public class DebugModuleFactory : ModuleFactoryBase { - private readonly IWorldStateFactory _worldStateFactory; + private readonly IWorldStateManager _worldStateManager; private readonly IJsonRpcConfig _jsonRpcConfig; private readonly IBlockValidator _blockValidator; private readonly IRewardCalculatorSource _rewardCalculatorSource; @@ -43,7 +43,7 @@ public class DebugModuleFactory : ModuleFactoryBase private ILogger _logger; public DebugModuleFactory( - IWorldStateFactory worldStateFactory, + IWorldStateManager worldStateManager, IDbProvider dbProvider, IBlockTree blockTree, IJsonRpcConfig jsonRpcConfig, @@ -59,7 +59,7 @@ public DebugModuleFactory( IFileSystem fileSystem, ILogManager logManager) { - _worldStateFactory = worldStateFactory; + _worldStateManager = worldStateManager; _dbProvider = dbProvider.AsReadOnly(false); _blockTree = blockTree.AsReadOnly(); _jsonRpcConfig = jsonRpcConfig ?? throw new ArgumentNullException(nameof(jsonRpcConfig)); @@ -80,7 +80,7 @@ public DebugModuleFactory( public override IDebugRpcModule Create() { ReadOnlyTxProcessingEnv txEnv = new( - _worldStateFactory, + _worldStateManager, _blockTree, _specProvider, _logManager); diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofModuleFactory.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofModuleFactory.cs index 1516084636a..b7443b6e6ac 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofModuleFactory.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofModuleFactory.cs @@ -26,17 +26,17 @@ public class ProofModuleFactory : ModuleFactoryBase private readonly ISpecProvider _specProvider; private readonly ILogManager _logManager; private readonly IReadOnlyBlockTree _blockTree; - private readonly IWorldStateFactory _worldStateFactory; + private readonly IWorldStateManager _worldStateManager; public ProofModuleFactory( - IWorldStateFactory worldStateFactory, + IWorldStateManager worldStateManager, IBlockTree blockTree, IBlockPreprocessorStep recoveryStep, IReceiptFinder receiptFinder, ISpecProvider specProvider, ILogManager logManager) { - _worldStateFactory = worldStateFactory ?? throw new ArgumentNullException(nameof(worldStateFactory)); + _worldStateManager = worldStateManager ?? throw new ArgumentNullException(nameof(worldStateManager)); _logManager = logManager ?? throw new ArgumentNullException(nameof(logManager)); _recoveryStep = recoveryStep ?? throw new ArgumentNullException(nameof(recoveryStep)); _receiptFinder = receiptFinder ?? throw new ArgumentNullException(nameof(receiptFinder)); @@ -47,7 +47,7 @@ public ProofModuleFactory( public override IProofRpcModule Create() { ReadOnlyTxProcessingEnv txProcessingEnv = new( - _worldStateFactory, _blockTree, _specProvider, _logManager); + _worldStateManager, _blockTree, _specProvider, _logManager); ReadOnlyChainProcessingEnv chainProcessingEnv = new( txProcessingEnv, Always.Valid, _recoveryStep, NoBlockRewards.Instance, new InMemoryReceiptStorage(), txProcessingEnv.ResetDb, _specProvider, _logManager); diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Trace/TraceModuleFactory.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Trace/TraceModuleFactory.cs index 74ab5a8a379..bfc33407e14 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Trace/TraceModuleFactory.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Trace/TraceModuleFactory.cs @@ -21,7 +21,7 @@ namespace Nethermind.JsonRpc.Modules.Trace { public class TraceModuleFactory : ModuleFactoryBase { - private readonly IWorldStateFactory _worldStateFactory; + private readonly IWorldStateManager _worldStateManager; private readonly IReadOnlyBlockTree _blockTree; private readonly IJsonRpcConfig _jsonRpcConfig; private readonly IReceiptStorage _receiptStorage; @@ -32,7 +32,7 @@ public class TraceModuleFactory : ModuleFactoryBase private readonly IPoSSwitcher _poSSwitcher; public TraceModuleFactory( - IWorldStateFactory worldStateFactory, + IWorldStateManager worldStateManager, IBlockTree blockTree, IJsonRpcConfig jsonRpcConfig, IBlockPreprocessorStep recoveryStep, @@ -42,7 +42,7 @@ public TraceModuleFactory( IPoSSwitcher poSSwitcher, ILogManager logManager) { - _worldStateFactory = worldStateFactory; + _worldStateManager = worldStateManager; _blockTree = blockTree.AsReadOnly(); _jsonRpcConfig = jsonRpcConfig ?? throw new ArgumentNullException(nameof(jsonRpcConfig)); _recoveryStep = recoveryStep ?? throw new ArgumentNullException(nameof(recoveryStep)); @@ -57,7 +57,7 @@ public TraceModuleFactory( public override ITraceRpcModule Create() { ReadOnlyTxProcessingEnv txProcessingEnv = - new(_worldStateFactory, _blockTree, _specProvider, _logManager); + new(_worldStateManager, _blockTree, _specProvider, _logManager); IRewardCalculator rewardCalculator = new MergeRpcRewardCalculator(_rewardCalculatorSource.Get(txProcessingEnv.TransactionProcessor), diff --git a/src/Nethermind/Nethermind.Merge.AuRa.Test/AuRaMergeEngineModuleTests.cs b/src/Nethermind/Nethermind.Merge.AuRa.Test/AuRaMergeEngineModuleTests.cs index 87f563977d5..9b8c84d5841 100644 --- a/src/Nethermind/Nethermind.Merge.AuRa.Test/AuRaMergeEngineModuleTests.cs +++ b/src/Nethermind/Nethermind.Merge.AuRa.Test/AuRaMergeEngineModuleTests.cs @@ -111,7 +111,7 @@ protected override IBlockProcessor CreateBlockProcessor() BlockTree = BlockTree, DbProvider = DbProvider, ReadOnlyTrieStore = ReadOnlyTrieStore, - ReadOnlyWorldStateFactory = ReadOnlyWorldStateFactory, + WorldStateManager = _readOnlyWorldStateManager, SpecProvider = SpecProvider, TransactionComparerProvider = TransactionComparerProvider, TxPool = TxPool @@ -158,7 +158,7 @@ protected override IBlockProducer CreateTestBlockProducer(TxPoolTxSource txPoolT _api!, new AuRaConfig(), new DisposableStack(), - ReadOnlyWorldStateFactory, + _readOnlyWorldStateManager, BlockTree, SpecProvider, BlockValidator, diff --git a/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergeBlockProducerEnvFactory.cs b/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergeBlockProducerEnvFactory.cs index c2ac61d8ec5..163f28bf719 100644 --- a/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergeBlockProducerEnvFactory.cs +++ b/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergeBlockProducerEnvFactory.cs @@ -30,7 +30,7 @@ public AuRaMergeBlockProducerEnvFactory( AuRaNethermindApi auraApi, IAuraConfig auraConfig, DisposableStack disposeStack, - IWorldStateFactory worldStateFactory, + IWorldStateManager worldStateManager, IBlockTree blockTree, ISpecProvider specProvider, IBlockValidator blockValidator, @@ -41,7 +41,7 @@ public AuRaMergeBlockProducerEnvFactory( ITransactionComparerProvider transactionComparerProvider, IBlocksConfig blocksConfig, ILogManager logManager) : base( - worldStateFactory, + worldStateManager, blockTree, specProvider, blockValidator, @@ -94,7 +94,7 @@ protected override TxPoolTxSource CreateTxPoolTxSource( ILogManager logManager) { ReadOnlyTxProcessingEnv constantContractsProcessingEnv = CreateReadonlyTxProcessingEnv( - _worldStateFactory, + _worldStateManager, _blockTree.AsReadOnly()); return new StartBlockProducerAuRa(_auraApi) diff --git a/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergePlugin.cs b/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergePlugin.cs index 1d77df3ee7f..e38a2d21256 100644 --- a/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergePlugin.cs +++ b/src/Nethermind/Nethermind.Merge.AuRa/AuRaMergePlugin.cs @@ -50,7 +50,7 @@ public override Task InitBlockProducer(IConsensusPlugin consensu (AuRaNethermindApi)_api, _api.Config(), _api.DisposeStack, - _api.ReadOnlyWorldStateFactory!, + _api.WorldStateManager!, _api.BlockTree!, _api.SpecProvider!, _api.BlockValidator!, diff --git a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Setup.cs b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Setup.cs index 94cc041a043..6b7801bd78c 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Setup.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Setup.cs @@ -188,7 +188,7 @@ protected override IBlockProducer CreateTestBlockProducer(TxPoolTxSource txPoolT targetAdjustedGasLimitCalculator); BlockProducerEnvFactory blockProducerEnvFactory = new( - ReadOnlyWorldStateFactory!, + _readOnlyWorldStateManager!, BlockTree, SpecProvider, BlockValidator, diff --git a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Synchronization.cs b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Synchronization.cs index 850e2e6ae14..ab2c11c5a0a 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Synchronization.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Synchronization.cs @@ -934,7 +934,7 @@ private MultiSyncModeSelector CreateMultiSyncModeSelector(MergeTestBlockchain ch SyncProgressResolver syncProgressResolver = new( chain.BlockTree, - new FullStateFinder(chain.BlockTree, chain.ReadOnlyWorldStateFactory.CreateStateReader()), + new FullStateFinder(chain.BlockTree, chain._readOnlyWorldStateManager.GlobalStateReader), new SyncConfig(), Substitute.For>(), Substitute.For>(), diff --git a/src/Nethermind/Nethermind.Merge.Plugin.Test/MergePluginTests.cs b/src/Nethermind/Nethermind.Merge.Plugin.Test/MergePluginTests.cs index ead03deac39..33cc6895c78 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin.Test/MergePluginTests.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin.Test/MergePluginTests.cs @@ -45,7 +45,7 @@ public void Setup() _context.BlockProcessingQueue?.IsEmpty.Returns(true); _context.MemDbFactory = new MemDbFactory(); _context.BlockProducerEnvFactory = new BlockProducerEnvFactory( - _context.ReadOnlyWorldStateFactory!, + _context.WorldStateManager!, _context.BlockTree!, _context.SpecProvider!, _context.BlockValidator!, diff --git a/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs b/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs index db441fad3b7..c221bdc6b99 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs @@ -263,7 +263,7 @@ public Task InitRpcModules() if (_api.BlockValidator is null) throw new ArgumentNullException(nameof(_api.BlockValidator)); if (_api.BlockProcessingQueue is null) throw new ArgumentNullException(nameof(_api.BlockProcessingQueue)); if (_api.SpecProvider is null) throw new ArgumentNullException(nameof(_api.SpecProvider)); - if (_api.ReadOnlyWorldStateFactory is null) throw new ArgumentNullException(nameof(_api.ReadOnlyWorldStateFactory)); + if (_api.WorldStateManager is null) throw new ArgumentNullException(nameof(_api.WorldStateManager)); if (_beaconPivot is null) throw new ArgumentNullException(nameof(_beaconPivot)); if (_beaconSync is null) throw new ArgumentNullException(nameof(_beaconSync)); if (_blockProductionTrigger is null) throw new ArgumentNullException(nameof(_blockProductionTrigger)); @@ -289,7 +289,7 @@ public Task InitRpcModules() { DefaultHttpClient httpClient = new(new HttpClient(), _api.EthereumJsonSerializer, _api.LogManager, retryDelayMilliseconds: 100); IBoostRelay boostRelay = new BoostRelay(httpClient, _mergeConfig.BuilderRelayUrl); - BoostBlockImprovementContextFactory boostBlockImprovementContextFactory = new(_blockProductionTrigger, TimeSpan.FromSeconds(_blocksConfig.SecondsPerSlot), boostRelay, _api.ReadOnlyWorldStateFactory.CreateStateReader()); + BoostBlockImprovementContextFactory boostBlockImprovementContextFactory = new(_blockProductionTrigger, TimeSpan.FromSeconds(_blocksConfig.SecondsPerSlot), boostRelay, _api.WorldStateManager.GlobalStateReader); improvementContextFactory = boostBlockImprovementContextFactory; } @@ -415,7 +415,7 @@ public Task InitSynchronization() _api.SealValidator!, _syncConfig, _api.BetterPeerStrategy!, - new FullStateFinder(_api.BlockTree, _api.ReadOnlyWorldStateFactory!.CreateStateReader()), + new FullStateFinder(_api.BlockTree, _api.WorldStateManager!.GlobalStateReader), _api.LogManager); MergeSynchronizer synchronizer = new MergeSynchronizer( @@ -436,7 +436,7 @@ public Task InitSynchronization() _api.BetterPeerStrategy, _api.ChainSpec, _beaconSync, - _api.ReadOnlyWorldStateFactory.CreateStateReader(), + _api.WorldStateManager.GlobalStateReader, _api.LogManager ); _api.Synchronizer = synchronizer; diff --git a/src/Nethermind/Nethermind.Mev.Test/MevRpcModuleTests.TestMevRpcBlockchain.cs b/src/Nethermind/Nethermind.Mev.Test/MevRpcModuleTests.TestMevRpcBlockchain.cs index 223c50fe77c..039584b0846 100644 --- a/src/Nethermind/Nethermind.Mev.Test/MevRpcModuleTests.TestMevRpcBlockchain.cs +++ b/src/Nethermind/Nethermind.Mev.Test/MevRpcModuleTests.TestMevRpcBlockchain.cs @@ -92,7 +92,7 @@ protected override IBlockProducer CreateTestBlockProducer(TxPoolTxSource txPoolT SpecProvider.UpdateMergeTransitionInfo(1, 0); BlockProducerEnvFactory blockProducerEnvFactory = new( - ReadOnlyWorldStateFactory, + _readOnlyWorldStateManager, BlockTree, SpecProvider, BlockValidator, @@ -205,7 +205,7 @@ protected override BlockProcessor CreateBlockProcessor() _tracerFactory = new TracerFactory( BlockTree, - ReadOnlyWorldStateFactory, + _readOnlyWorldStateManager, BlockPreprocessorStep, SpecProvider, LogManager, diff --git a/src/Nethermind/Nethermind.Mev/Execution/TracerFactory.cs b/src/Nethermind/Nethermind.Mev/Execution/TracerFactory.cs index 2e52dc83ae9..a01b530956e 100644 --- a/src/Nethermind/Nethermind.Mev/Execution/TracerFactory.cs +++ b/src/Nethermind/Nethermind.Mev/Execution/TracerFactory.cs @@ -23,11 +23,11 @@ public class TracerFactory : ITracerFactory private readonly ILogManager _logManager; private readonly ProcessingOptions _processingOptions; private readonly IReadOnlyBlockTree _blockTree; - private readonly IWorldStateFactory _worldStateFactory; + private readonly IWorldStateManager _worldStateManager; public TracerFactory( IBlockTree blockTree, - IWorldStateFactory worldStateFactory, + IWorldStateManager worldStateManager, IBlockPreprocessorStep recoveryStep, ISpecProvider specProvider, ILogManager logManager, @@ -37,14 +37,14 @@ public TracerFactory( _processingOptions = processingOptions; _recoveryStep = recoveryStep ?? throw new ArgumentNullException(nameof(recoveryStep)); _specProvider = specProvider ?? throw new ArgumentNullException(nameof(specProvider)); - _worldStateFactory = worldStateFactory ?? throw new ArgumentNullException(nameof(worldStateFactory)); + _worldStateManager = worldStateManager ?? throw new ArgumentNullException(nameof(worldStateManager)); _blockTree = blockTree.AsReadOnly(); } public ITracer Create() { ReadOnlyTxProcessingEnv txProcessingEnv = new( - _worldStateFactory, _blockTree, _specProvider, _logManager); + _worldStateManager, _blockTree, _specProvider, _logManager); ReadOnlyChainProcessingEnv chainProcessingEnv = new( txProcessingEnv, Always.Valid, _recoveryStep, NoBlockRewards.Instance, new InMemoryReceiptStorage(), txProcessingEnv.ResetDb, _specProvider, _logManager); diff --git a/src/Nethermind/Nethermind.Mev/MevPlugin.cs b/src/Nethermind/Nethermind.Mev/MevPlugin.cs index 53255613c95..f1ec8a96d4b 100644 --- a/src/Nethermind/Nethermind.Mev/MevPlugin.cs +++ b/src/Nethermind/Nethermind.Mev/MevPlugin.cs @@ -91,7 +91,7 @@ private ITracerFactory TracerFactory _tracerFactory = new TracerFactory( getFromApi.BlockTree!, - getFromApi.ReadOnlyWorldStateFactory!, + getFromApi.WorldStateManager!, getFromApi.BlockPreprocessor!, getFromApi.SpecProvider!, getFromApi.LogManager!, @@ -114,7 +114,7 @@ public Task InitRpcModules() MevModuleFactory mevModuleFactory = new(rpcConfig, BundlePool, getFromApi.BlockTree!, - getFromApi.ReadOnlyWorldStateFactory!.CreateStateReader(), + getFromApi.WorldStateManager!.GlobalStateReader, TracerFactory, getFromApi.SpecProvider!, getFromApi.EngineSigner); diff --git a/src/Nethermind/Nethermind.Optimism/InitializeBlockProducerOptimism.cs b/src/Nethermind/Nethermind.Optimism/InitializeBlockProducerOptimism.cs index 9ee3f0aacb3..91719067bca 100644 --- a/src/Nethermind/Nethermind.Optimism/InitializeBlockProducerOptimism.cs +++ b/src/Nethermind/Nethermind.Optimism/InitializeBlockProducerOptimism.cs @@ -32,10 +32,10 @@ protected override Task BuildProducer() if (_api.BlockValidator is null) throw new StepDependencyException(nameof(_api.BlockValidator)); if (_api.SpecHelper is null) throw new StepDependencyException(nameof(_api.SpecHelper)); if (_api.L1CostHelper is null) throw new StepDependencyException(nameof(_api.L1CostHelper)); - if (_api.ReadOnlyWorldStateFactory is null) throw new StepDependencyException(nameof(_api.ReadOnlyWorldStateFactory)); + if (_api.WorldStateManager is null) throw new StepDependencyException(nameof(_api.WorldStateManager)); _api.BlockProducerEnvFactory = new OptimismBlockProducerEnvFactory( - _api.ReadOnlyWorldStateFactory, + _api.WorldStateManager, _api.ChainSpec, _api.BlockTree, _api.SpecProvider, diff --git a/src/Nethermind/Nethermind.Optimism/OptimismBlockProducerEnvFactory.cs b/src/Nethermind/Nethermind.Optimism/OptimismBlockProducerEnvFactory.cs index dd71c6a475a..7393e7fdfa8 100644 --- a/src/Nethermind/Nethermind.Optimism/OptimismBlockProducerEnvFactory.cs +++ b/src/Nethermind/Nethermind.Optimism/OptimismBlockProducerEnvFactory.cs @@ -29,7 +29,7 @@ public class OptimismBlockProducerEnvFactory : BlockProducerEnvFactory private readonly OPL1CostHelper _l1CostHelper; public OptimismBlockProducerEnvFactory( - IWorldStateFactory worldStateFactory, + IWorldStateManager worldStateManager, ChainSpec chainSpec, IBlockTree blockTree, ISpecProvider specProvider, @@ -42,7 +42,7 @@ public OptimismBlockProducerEnvFactory( IBlocksConfig blocksConfig, OPSpecHelper specHelper, OPL1CostHelper l1CostHelper, - ILogManager logManager) : base(worldStateFactory, + ILogManager logManager) : base(worldStateManager, blockTree, specProvider, blockValidator, rewardCalculatorSource, receiptStorage, blockPreprocessorStep, txPool, transactionComparerProvider, blocksConfig, logManager) @@ -53,10 +53,10 @@ public OptimismBlockProducerEnvFactory( TransactionsExecutorFactory = new OptimismTransactionsExecutorFactory(specProvider, logManager); } - protected override ReadOnlyTxProcessingEnv CreateReadonlyTxProcessingEnv(IWorldStateFactory worldStateFactory, + protected override ReadOnlyTxProcessingEnv CreateReadonlyTxProcessingEnv(IWorldStateManager worldStateManager, ReadOnlyBlockTree readOnlyBlockTree) { - ReadOnlyTxProcessingEnv result = new(worldStateFactory, + ReadOnlyTxProcessingEnv result = new(worldStateManager, readOnlyBlockTree, _specProvider, _logManager); result.TransactionProcessor = new OptimismTransactionProcessor(_specProvider, result.StateProvider, result.Machine, _logManager, _l1CostHelper, _specHelper); diff --git a/src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs b/src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs index 15f75e13c18..41149f856e3 100644 --- a/src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs +++ b/src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs @@ -148,7 +148,7 @@ public Task InitSynchronization() _api.SealValidator!, _syncConfig, _api.BetterPeerStrategy!, - new FullStateFinder(_api.BlockTree, _api.ReadOnlyWorldStateFactory!.CreateStateReader()), + new FullStateFinder(_api.BlockTree, _api.WorldStateManager!.GlobalStateReader), _api.LogManager); _api.Synchronizer = new MergeSynchronizer( @@ -169,7 +169,7 @@ public Task InitSynchronization() _api.BetterPeerStrategy, _api.ChainSpec, _beaconSync, - _api.ReadOnlyWorldStateFactory.CreateStateReader(), + _api.WorldStateManager.GlobalStateReader, _api.LogManager ); diff --git a/src/Nethermind/Nethermind.Runner.Test/Ethereum/ContextWithMocks.cs b/src/Nethermind/Nethermind.Runner.Test/Ethereum/ContextWithMocks.cs index b037d9e9fc0..ca10e2caa80 100644 --- a/src/Nethermind/Nethermind.Runner.Test/Ethereum/ContextWithMocks.cs +++ b/src/Nethermind/Nethermind.Runner.Test/Ethereum/ContextWithMocks.cs @@ -121,7 +121,7 @@ public static NethermindApi ContextWithMocks() WitnessRepository = Substitute.For() }; - api.ReadOnlyWorldStateFactory = new ReadOnlyWorldStateFactory(api.DbProvider, api.ReadOnlyTrieStore, LimboLogs.Instance); + api.WorldStateManager = new ReadOnlyWorldStateManager(api.DbProvider, api.ReadOnlyTrieStore, LimboLogs.Instance); return api; } } diff --git a/src/Nethermind/Nethermind.State/IWorldStateFactory.cs b/src/Nethermind/Nethermind.State/IWorldStateManager.cs similarity index 64% rename from src/Nethermind/Nethermind.State/IWorldStateFactory.cs rename to src/Nethermind/Nethermind.State/IWorldStateManager.cs index b8029f8a818..4b58bd7c854 100644 --- a/src/Nethermind/Nethermind.State/IWorldStateFactory.cs +++ b/src/Nethermind/Nethermind.State/IWorldStateManager.cs @@ -5,9 +5,9 @@ namespace Nethermind.State; -public interface IWorldStateFactory +public interface IWorldStateManager { - IWorldState CreateWorldState(); - IStateReader CreateStateReader(); + IWorldState GlobalWorldState { get; } + IStateReader GlobalStateReader { get; } (IWorldState, IStateReader, Action) CreateResettableWorldState(); } diff --git a/src/Nethermind/Nethermind.State/ReadOnlyWorldStateFactory.cs b/src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs similarity index 73% rename from src/Nethermind/Nethermind.State/ReadOnlyWorldStateFactory.cs rename to src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs index 81e75e2e53c..83547af004e 100644 --- a/src/Nethermind/Nethermind.State/ReadOnlyWorldStateFactory.cs +++ b/src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs @@ -9,13 +9,13 @@ namespace Nethermind.State; -public class ReadOnlyWorldStateFactory: IWorldStateFactory +public class ReadOnlyWorldStateManager: IWorldStateManager { private IReadOnlyTrieStore? _readOnlyTrieStore; private ILogManager _logManager; private readonly IDbProvider _dbProvider; - public ReadOnlyWorldStateFactory( + public ReadOnlyWorldStateManager( IDbProvider dbProvider, IReadOnlyTrieStore? readOnlyTrieStore, ILogManager logManager @@ -24,19 +24,14 @@ ILogManager logManager _readOnlyTrieStore = readOnlyTrieStore; _dbProvider = dbProvider; _logManager = logManager; - } - public IWorldState CreateWorldState() - { IKeyValueStore codeDb = _dbProvider.AsReadOnly(false).GetDb(DbNames.Code); - return new WorldState(_readOnlyTrieStore, codeDb, _logManager); + GlobalStateReader = new StateReader(_readOnlyTrieStore, codeDb, _logManager); } - public IStateReader CreateStateReader() - { - IKeyValueStore codeDb = _dbProvider.AsReadOnly(false).GetDb(DbNames.Code); - return new StateReader(_readOnlyTrieStore, codeDb, _logManager); - } + public virtual IWorldState GlobalWorldState => throw new InvalidOperationException("global world state not supported"); + + public IStateReader GlobalStateReader { get; } public (IWorldState, IStateReader, Action) CreateResettableWorldState() { diff --git a/src/Nethermind/Nethermind.State/WorldStateFactory.cs b/src/Nethermind/Nethermind.State/WorldStateFactory.cs deleted file mode 100644 index b865f74722e..00000000000 --- a/src/Nethermind/Nethermind.State/WorldStateFactory.cs +++ /dev/null @@ -1,42 +0,0 @@ -// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited -// SPDX-License-Identifier: LGPL-3.0-only - -using System; -using Nethermind.Core; -using Nethermind.Logging; -using Nethermind.Trie.Pruning; - -namespace Nethermind.State; - -public class WorldStateFactory: IWorldStateFactory -{ - private ITrieStore _trieStore; - private IKeyValueStore _codeDb; - private ILogManager _logManager; - - public WorldStateFactory( - TrieStore trieStore, - IKeyValueStore codeDb, - ILogManager logManager - ) - { - _trieStore = trieStore; - _codeDb = codeDb; - _logManager = logManager; - } - - public IWorldState CreateWorldState() - { - return new WorldState(_trieStore, _codeDb, _logManager); - } - - public IStateReader CreateStateReader() - { - return new StateReader(_trieStore, _codeDb, _logManager); - } - - public (IWorldState, IStateReader, Action) CreateResettableWorldState() - { - throw new InvalidOperationException("Unsupported action"); - } -} diff --git a/src/Nethermind/Nethermind.State/WorldStateManager.cs b/src/Nethermind/Nethermind.State/WorldStateManager.cs new file mode 100644 index 00000000000..ce401bca64b --- /dev/null +++ b/src/Nethermind/Nethermind.State/WorldStateManager.cs @@ -0,0 +1,27 @@ +// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited +// SPDX-License-Identifier: LGPL-3.0-only + +using System; +using Nethermind.Core; +using Nethermind.Db; +using Nethermind.Logging; +using Nethermind.Trie.Pruning; + +namespace Nethermind.State; + +public class WorldStateManager: ReadOnlyWorldStateManager +{ + private readonly IWorldState _worldState; + + public WorldStateManager( + IWorldState worldState, + IDbProvider dbProvider, + IReadOnlyTrieStore? readOnlyTrieStore, + ILogManager logManager + ) : base(dbProvider, readOnlyTrieStore, logManager) + { + _worldState = worldState; + } + + public override IWorldState GlobalWorldState => _worldState; +} From df547bf4d0ff11306a94c48e0f7e68a26510f741 Mon Sep 17 00:00:00 2001 From: Amirul Ashraf Date: Wed, 8 Nov 2023 19:18:05 +0800 Subject: [PATCH 07/17] Boundary watcher --- .../TrieStoreBoundaryWatcher.cs | 7 ++++--- .../Blockchain/TestBlockchain.cs | 5 ++++- .../Nethermind.Init/InitializeStateDb.cs | 19 ++++++++++--------- .../Nethermind.State/IWorldStateManager.cs | 2 ++ .../ReadOnlyWorldStateManager.cs | 9 +++++++++ .../Nethermind.State/WorldStateManager.cs | 10 +++++++++- 6 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/Nethermind/Nethermind.Blockchain/TrieStoreBoundaryWatcher.cs b/src/Nethermind/Nethermind.Blockchain/TrieStoreBoundaryWatcher.cs index 8aebd45a381..c1c76a317d5 100644 --- a/src/Nethermind/Nethermind.Blockchain/TrieStoreBoundaryWatcher.cs +++ b/src/Nethermind/Nethermind.Blockchain/TrieStoreBoundaryWatcher.cs @@ -4,20 +4,21 @@ using System; using Nethermind.Blockchain.Find; using Nethermind.Logging; +using Nethermind.State; using Nethermind.Trie.Pruning; namespace Nethermind.Blockchain { /// - /// Watches state persistence in with and saves it in . + /// Watches state persistence in with and saves it in . /// public class TrieStoreBoundaryWatcher : IDisposable { - private readonly ITrieStore _trieStore; + private readonly IWorldStateManager _trieStore; private readonly IBlockTree _blockTree; private readonly ILogger _logger; - public TrieStoreBoundaryWatcher(ITrieStore trieStore, IBlockTree blockTree, ILogManager logManager) + public TrieStoreBoundaryWatcher(IWorldStateManager trieStore, IBlockTree blockTree, ILogManager logManager) { _trieStore = trieStore; _blockTree = blockTree; diff --git a/src/Nethermind/Nethermind.Core.Test/Blockchain/TestBlockchain.cs b/src/Nethermind/Nethermind.Core.Test/Blockchain/TestBlockchain.cs index 103a6d48a3f..4f6fd8a9842 100644 --- a/src/Nethermind/Nethermind.Core.Test/Blockchain/TestBlockchain.cs +++ b/src/Nethermind/Nethermind.Core.Test/Blockchain/TestBlockchain.cs @@ -35,6 +35,7 @@ using Nethermind.State.Repositories; using Nethermind.Trie.Pruning; using Nethermind.TxPool; +using NSubstitute; using BlockTree = Nethermind.Blockchain.BlockTree; namespace Nethermind.Core.Test.Blockchain; @@ -157,7 +158,9 @@ protected virtual async Task Build(ISpecProvider? specProvider = NonceManager = new NonceManager(chainHeadInfoProvider.AccountStateProvider); - _trieStoreWatcher = new TrieStoreBoundaryWatcher(TrieStore, BlockTree, LogManager); + WorldStateManager stateManager = new(State, TrieStore, DbProvider, ReadOnlyTrieStore, LimboLogs.Instance); + + _trieStoreWatcher = new TrieStoreBoundaryWatcher(stateManager, BlockTree, LogManager); ReceiptStorage = new InMemoryReceiptStorage(); VirtualMachine virtualMachine = new(new BlockhashProvider(BlockTree, LogManager), SpecProvider, LogManager); diff --git a/src/Nethermind/Nethermind.Init/InitializeStateDb.cs b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs index d409d75a854..f33ff3f2388 100644 --- a/src/Nethermind/Nethermind.Init/InitializeStateDb.cs +++ b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs @@ -135,20 +135,21 @@ public Task Execute(CancellationToken cancellationToken) }; } - // TODO: Don't forget this - TrieStoreBoundaryWatcher trieStoreBoundaryWatcher = new(trieStore, _api.BlockTree!, _api.LogManager); - getApi.DisposeStack.Push(trieStoreBoundaryWatcher); - getApi.DisposeStack.Push(trieStore); - IReadOnlyTrieStore readOnlyTrieStore = setApi.ReadOnlyTrieStore = trieStore.AsReadOnly(cachedStateDb); - IWorldStateManager readOnlyStateManager = setApi.WorldStateManager = new WorldStateManager( + IWorldStateManager stateManager = setApi.WorldStateManager = new WorldStateManager( worldState, + trieStore, getApi.DbProvider, readOnlyTrieStore, getApi.LogManager); - setApi.ChainHeadStateProvider = new ChainHeadReadOnlyStateProvider(getApi.BlockTree, readOnlyStateManager.GlobalStateReader); + // TODO: Don't forget this + TrieStoreBoundaryWatcher trieStoreBoundaryWatcher = new(stateManager, _api.BlockTree!, _api.LogManager); + getApi.DisposeStack.Push(trieStoreBoundaryWatcher); + getApi.DisposeStack.Push(trieStore); + + setApi.ChainHeadStateProvider = new ChainHeadReadOnlyStateProvider(getApi.BlockTree, stateManager.GlobalStateReader); worldState.StateRoot = getApi.BlockTree!.Head?.StateRoot ?? Keccak.EmptyTreeHash; @@ -159,7 +160,7 @@ public Task Execute(CancellationToken cancellationToken) try { _logger!.Info("Collecting trie stats and verifying that no nodes are missing..."); - IWorldState diagStateProvider = readOnlyStateManager.GlobalWorldState; + IWorldState diagStateProvider = stateManager.GlobalWorldState; diagStateProvider.StateRoot = getApi.BlockTree!.Head?.StateRoot ?? Keccak.EmptyTreeHash; TrieStats stats = diagStateProvider.CollectStats(getApi.DbProvider.CodeDb, _api.LogManager); _logger.Info($"Starting from {getApi.BlockTree.Head?.Number} {getApi.BlockTree.Head?.StateRoot}{Environment.NewLine}" + stats); @@ -177,7 +178,7 @@ public Task Execute(CancellationToken cancellationToken) worldState.StateRoot = getApi.BlockTree.Head.StateRoot; } - InitializeFullPruning(pruningConfig, initConfig, _api, readOnlyStateManager.GlobalStateReader); + InitializeFullPruning(pruningConfig, initConfig, _api, stateManager.GlobalStateReader); return Task.CompletedTask; } diff --git a/src/Nethermind/Nethermind.State/IWorldStateManager.cs b/src/Nethermind/Nethermind.State/IWorldStateManager.cs index 4b58bd7c854..0a84fe0fd18 100644 --- a/src/Nethermind/Nethermind.State/IWorldStateManager.cs +++ b/src/Nethermind/Nethermind.State/IWorldStateManager.cs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LGPL-3.0-only using System; +using Nethermind.Trie.Pruning; namespace Nethermind.State; @@ -10,4 +11,5 @@ public interface IWorldStateManager IWorldState GlobalWorldState { get; } IStateReader GlobalStateReader { get; } (IWorldState, IStateReader, Action) CreateResettableWorldState(); + event EventHandler? ReorgBoundaryReached; } diff --git a/src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs b/src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs index 83547af004e..d40f27b05a9 100644 --- a/src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs +++ b/src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs @@ -9,6 +9,9 @@ namespace Nethermind.State; +/// +/// Mainly to make it easier for test +/// public class ReadOnlyWorldStateManager: IWorldStateManager { private IReadOnlyTrieStore? _readOnlyTrieStore; @@ -43,4 +46,10 @@ ILogManager logManager readOnlyDbProvider.ClearTempChanges ); } + + public virtual event EventHandler? ReorgBoundaryReached + { + add => throw new InvalidOperationException("Unsupported operation"); + remove => throw new InvalidOperationException("Unsupported operation"); + } } diff --git a/src/Nethermind/Nethermind.State/WorldStateManager.cs b/src/Nethermind/Nethermind.State/WorldStateManager.cs index ce401bca64b..da79cec79a7 100644 --- a/src/Nethermind/Nethermind.State/WorldStateManager.cs +++ b/src/Nethermind/Nethermind.State/WorldStateManager.cs @@ -2,7 +2,6 @@ // SPDX-License-Identifier: LGPL-3.0-only using System; -using Nethermind.Core; using Nethermind.Db; using Nethermind.Logging; using Nethermind.Trie.Pruning; @@ -12,16 +11,25 @@ namespace Nethermind.State; public class WorldStateManager: ReadOnlyWorldStateManager { private readonly IWorldState _worldState; + private readonly ITrieStore _trieStore; public WorldStateManager( IWorldState worldState, + ITrieStore trieStore, IDbProvider dbProvider, IReadOnlyTrieStore? readOnlyTrieStore, ILogManager logManager ) : base(dbProvider, readOnlyTrieStore, logManager) { _worldState = worldState; + _trieStore = trieStore; } public override IWorldState GlobalWorldState => _worldState; + + public override event EventHandler? ReorgBoundaryReached + { + add => _trieStore.ReorgBoundaryReached += value; + remove => _trieStore.ReorgBoundaryReached -= value; + } } From b5931bfd08f1ca4c274b32a7f629f0cc3f179e58 Mon Sep 17 00:00:00 2001 From: Amirul Ashraf Date: Wed, 8 Nov 2023 19:26:41 +0800 Subject: [PATCH 08/17] Dont need this anymore --- src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs | 1 - src/Nethermind/Nethermind.Api/NethermindApi.cs | 1 - src/Nethermind/Nethermind.Init/InitializeStateDb.cs | 7 +++---- src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs | 1 - src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs | 2 -- .../Modules/DebugModule/DebugModuleFactory.cs | 3 --- .../AuRaMergeEngineModuleTests.cs | 1 - .../Nethermind.Merge.Plugin/MergePlugin.BlockProducer.cs | 1 - src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs | 1 - .../Synchronization/MergeSynchronizer.cs | 2 -- .../Nethermind.Optimism/InitializeBlockProducerOptimism.cs | 1 - src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs | 1 - .../Nethermind.Runner.Test/Ethereum/ContextWithMocks.cs | 3 +-- src/Nethermind/Nethermind.State/IWorldStateManager.cs | 7 +++++++ .../OldStyleFullSynchronizerTests.cs | 1 - .../Nethermind.Synchronization.Test/SyncThreadTests.cs | 1 - .../Nethermind.Synchronization.Test/SynchronizerTests.cs | 2 -- src/Nethermind/Nethermind.Synchronization/Synchronizer.cs | 7 +------ 18 files changed, 12 insertions(+), 31 deletions(-) diff --git a/src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs b/src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs index a8b108393c5..b19d307b648 100644 --- a/src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs +++ b/src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs @@ -43,7 +43,6 @@ public interface IApiWithBlockchain : IApiWithStores, IBlockchainBridgeFactory IUnclesValidator? UnclesValidator { get; set; } IHeaderValidator? HeaderValidator { get; set; } IManualBlockProductionTrigger ManualBlockProductionTrigger { get; } - IReadOnlyTrieStore? ReadOnlyTrieStore { get; set; } IRewardCalculatorSource? RewardCalculatorSource { get; set; } /// /// PoS switcher for The Merge diff --git a/src/Nethermind/Nethermind.Api/NethermindApi.cs b/src/Nethermind/Nethermind.Api/NethermindApi.cs index 18ce2c7d9a5..3574e6bbb92 100644 --- a/src/Nethermind/Nethermind.Api/NethermindApi.cs +++ b/src/Nethermind/Nethermind.Api/NethermindApi.cs @@ -194,7 +194,6 @@ public ISealEngine SealEngine public ITimerFactory TimerFactory { get; } = Core.Timers.TimerFactory.Default; public ITransactionProcessor? TransactionProcessor { get; set; } public ITrieStore? TrieStore { get; set; } - public IReadOnlyTrieStore? ReadOnlyTrieStore { get; set; } public ITxSender? TxSender { get; set; } public INonceManager? NonceManager { get; set; } public ITxPool? TxPool { get; set; } diff --git a/src/Nethermind/Nethermind.Init/InitializeStateDb.cs b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs index f33ff3f2388..d17e5ab8d04 100644 --- a/src/Nethermind/Nethermind.Init/InitializeStateDb.cs +++ b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs @@ -115,7 +115,7 @@ public Task Execute(CancellationToken cancellationToken) getApi.LogManager); setApi.TrieStore = trieStore; - IWorldState worldState = setApi.WorldState = syncConfig.TrieHealing + IWorldState worldState = syncConfig.TrieHealing ? new HealingWorldState( trieStore, codeDb, @@ -135,13 +135,11 @@ public Task Execute(CancellationToken cancellationToken) }; } - IReadOnlyTrieStore readOnlyTrieStore = setApi.ReadOnlyTrieStore = trieStore.AsReadOnly(cachedStateDb); - IWorldStateManager stateManager = setApi.WorldStateManager = new WorldStateManager( worldState, trieStore, getApi.DbProvider, - readOnlyTrieStore, + trieStore.AsReadOnly(cachedStateDb), getApi.LogManager); // TODO: Don't forget this @@ -149,6 +147,7 @@ public Task Execute(CancellationToken cancellationToken) getApi.DisposeStack.Push(trieStoreBoundaryWatcher); getApi.DisposeStack.Push(trieStore); + setApi.WorldState = stateManager.GlobalWorldState; setApi.ChainHeadStateProvider = new ChainHeadReadOnlyStateProvider(getApi.BlockTree, stateManager.GlobalStateReader); worldState.StateRoot = getApi.BlockTree!.Head?.StateRoot ?? Keccak.EmptyTreeHash; diff --git a/src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs b/src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs index fbc1d584feb..c939d5841e6 100644 --- a/src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs +++ b/src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs @@ -152,7 +152,6 @@ private async Task Initialize(CancellationToken cancellationToken) blockDownloaderFactory, _api.Pivot, _api.ProcessExit!, - _api.ReadOnlyTrieStore!, _api.BetterPeerStrategy, _api.ChainSpec, _api.WorldStateManager!.GlobalStateReader, diff --git a/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs b/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs index 9c8a89b27c4..7dc22287e4e 100644 --- a/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs +++ b/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs @@ -88,7 +88,6 @@ public virtual async Task Execute(CancellationToken cancellationToken) if (_api.ReceiptStorage is null) throw new StepDependencyException(nameof(_api.ReceiptStorage)); if (_api.GasPriceOracle is null) throw new StepDependencyException(nameof(_api.GasPriceOracle)); if (_api.EthSyncingInfo is null) throw new StepDependencyException(nameof(_api.EthSyncingInfo)); - if (_api.ReadOnlyTrieStore is null) throw new StepDependencyException(nameof(_api.ReadOnlyTrieStore)); EthModuleFactory ethModuleFactory = new( @@ -129,7 +128,6 @@ public virtual async Task Execute(CancellationToken cancellationToken) _api.RewardCalculatorSource, _api.ReceiptStorage, new ReceiptMigration(_api), - _api.ReadOnlyTrieStore, _api.ConfigProvider, _api.SpecProvider, _api.SyncModeSelector, diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/DebugModule/DebugModuleFactory.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/DebugModule/DebugModuleFactory.cs index 87bad42c986..91f183533b2 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/DebugModule/DebugModuleFactory.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/DebugModule/DebugModuleFactory.cs @@ -31,7 +31,6 @@ public class DebugModuleFactory : ModuleFactoryBase private readonly IRewardCalculatorSource _rewardCalculatorSource; private readonly IReceiptStorage _receiptStorage; private readonly IReceiptsMigration _receiptsMigration; - private readonly IReadOnlyTrieStore _trieStore; private readonly IConfigProvider _configProvider; private readonly ISpecProvider _specProvider; private readonly ILogManager _logManager; @@ -52,7 +51,6 @@ public DebugModuleFactory( IRewardCalculatorSource rewardCalculator, IReceiptStorage receiptStorage, IReceiptsMigration receiptsMigration, - IReadOnlyTrieStore trieStore, IConfigProvider configProvider, ISpecProvider specProvider, ISyncModeSelector syncModeSelector, @@ -68,7 +66,6 @@ public DebugModuleFactory( _rewardCalculatorSource = rewardCalculator ?? throw new ArgumentNullException(nameof(rewardCalculator)); _receiptStorage = receiptStorage ?? throw new ArgumentNullException(nameof(receiptStorage)); _receiptsMigration = receiptsMigration ?? throw new ArgumentNullException(nameof(receiptsMigration)); - _trieStore = (trieStore ?? throw new ArgumentNullException(nameof(trieStore))); _configProvider = configProvider ?? throw new ArgumentNullException(nameof(configProvider)); _specProvider = specProvider ?? throw new ArgumentNullException(nameof(specProvider)); _logManager = logManager ?? throw new ArgumentNullException(nameof(logManager)); diff --git a/src/Nethermind/Nethermind.Merge.AuRa.Test/AuRaMergeEngineModuleTests.cs b/src/Nethermind/Nethermind.Merge.AuRa.Test/AuRaMergeEngineModuleTests.cs index 9b8c84d5841..77c9d61c4b5 100644 --- a/src/Nethermind/Nethermind.Merge.AuRa.Test/AuRaMergeEngineModuleTests.cs +++ b/src/Nethermind/Nethermind.Merge.AuRa.Test/AuRaMergeEngineModuleTests.cs @@ -110,7 +110,6 @@ protected override IBlockProcessor CreateBlockProcessor() { BlockTree = BlockTree, DbProvider = DbProvider, - ReadOnlyTrieStore = ReadOnlyTrieStore, WorldStateManager = _readOnlyWorldStateManager, SpecProvider = SpecProvider, TransactionComparerProvider = TransactionComparerProvider, diff --git a/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.BlockProducer.cs b/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.BlockProducer.cs index a8436b8813f..dd36585b986 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.BlockProducer.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.BlockProducer.cs @@ -35,7 +35,6 @@ public virtual async Task InitBlockProducer(IConsensusPlugin con if (_api.ReceiptStorage is null) throw new ArgumentNullException(nameof(_api.ReceiptStorage)); if (_api.TxPool is null) throw new ArgumentNullException(nameof(_api.TxPool)); if (_api.DbProvider is null) throw new ArgumentNullException(nameof(_api.DbProvider)); - if (_api.ReadOnlyTrieStore is null) throw new ArgumentNullException(nameof(_api.ReadOnlyTrieStore)); if (_api.BlockchainProcessor is null) throw new ArgumentNullException(nameof(_api.BlockchainProcessor)); if (_api.HeaderValidator is null) throw new ArgumentNullException(nameof(_api.HeaderValidator)); if (_mergeBlockProductionPolicy is null) throw new ArgumentNullException(nameof(_mergeBlockProductionPolicy)); diff --git a/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs b/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs index c221bdc6b99..ac996813065 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs @@ -432,7 +432,6 @@ public Task InitSynchronization() _mergeConfig, _invalidChainTracker, _api.ProcessExit!, - _api.ReadOnlyTrieStore!, _api.BetterPeerStrategy, _api.ChainSpec, _beaconSync, diff --git a/src/Nethermind/Nethermind.Merge.Plugin/Synchronization/MergeSynchronizer.cs b/src/Nethermind/Nethermind.Merge.Plugin/Synchronization/MergeSynchronizer.cs index 6c01dae30a0..2be83e23cd7 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin/Synchronization/MergeSynchronizer.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin/Synchronization/MergeSynchronizer.cs @@ -52,7 +52,6 @@ public MergeSynchronizer( IMergeConfig mergeConfig, IInvalidChainTracker invalidChainTracker, IProcessExitSource exitSource, - IReadOnlyTrieStore readOnlyTrieStore, IBetterPeerStrategy betterPeerStrategy, ChainSpec chainSpec, IBeaconSyncStrategy beaconSync, @@ -69,7 +68,6 @@ public MergeSynchronizer( blockDownloaderFactory, pivot, exitSource, - readOnlyTrieStore, betterPeerStrategy, chainSpec, stateReader, diff --git a/src/Nethermind/Nethermind.Optimism/InitializeBlockProducerOptimism.cs b/src/Nethermind/Nethermind.Optimism/InitializeBlockProducerOptimism.cs index 91719067bca..3de030e80ce 100644 --- a/src/Nethermind/Nethermind.Optimism/InitializeBlockProducerOptimism.cs +++ b/src/Nethermind/Nethermind.Optimism/InitializeBlockProducerOptimism.cs @@ -23,7 +23,6 @@ protected override Task BuildProducer() { if (_api.DbProvider is null) throw new StepDependencyException(nameof(_api.DbProvider)); if (_api.BlockTree is null) throw new StepDependencyException(nameof(_api.BlockTree)); - if (_api.ReadOnlyTrieStore is null) throw new StepDependencyException(nameof(_api.ReadOnlyTrieStore)); if (_api.SpecProvider is null) throw new StepDependencyException(nameof(_api.SpecProvider)); if (_api.RewardCalculatorSource is null) throw new StepDependencyException(nameof(_api.RewardCalculatorSource)); if (_api.ReceiptStorage is null) throw new StepDependencyException(nameof(_api.ReceiptStorage)); diff --git a/src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs b/src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs index 41149f856e3..804b34d7338 100644 --- a/src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs +++ b/src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs @@ -165,7 +165,6 @@ public Task InitSynchronization() _mergeConfig, _invalidChainTracker, _api.ProcessExit!, - _api.ReadOnlyTrieStore!, _api.BetterPeerStrategy, _api.ChainSpec, _beaconSync, diff --git a/src/Nethermind/Nethermind.Runner.Test/Ethereum/ContextWithMocks.cs b/src/Nethermind/Nethermind.Runner.Test/Ethereum/ContextWithMocks.cs index ca10e2caa80..0550a49d49f 100644 --- a/src/Nethermind/Nethermind.Runner.Test/Ethereum/ContextWithMocks.cs +++ b/src/Nethermind/Nethermind.Runner.Test/Ethereum/ContextWithMocks.cs @@ -106,7 +106,6 @@ public static NethermindApi ContextWithMocks() WebSocketsManager = Substitute.For(), ChainLevelInfoRepository = Substitute.For(), TrieStore = Substitute.For(), - ReadOnlyTrieStore = Substitute.For(), BlockProducerEnvFactory = Substitute.For(), TransactionComparerProvider = Substitute.For(), GasPriceOracle = Substitute.For(), @@ -121,7 +120,7 @@ public static NethermindApi ContextWithMocks() WitnessRepository = Substitute.For() }; - api.WorldStateManager = new ReadOnlyWorldStateManager(api.DbProvider, api.ReadOnlyTrieStore, LimboLogs.Instance); + api.WorldStateManager = new ReadOnlyWorldStateManager(api.DbProvider, Substitute.For(), LimboLogs.Instance); return api; } } diff --git a/src/Nethermind/Nethermind.State/IWorldStateManager.cs b/src/Nethermind/Nethermind.State/IWorldStateManager.cs index 0a84fe0fd18..c6f676a8df9 100644 --- a/src/Nethermind/Nethermind.State/IWorldStateManager.cs +++ b/src/Nethermind/Nethermind.State/IWorldStateManager.cs @@ -10,6 +10,13 @@ public interface IWorldStateManager { IWorldState GlobalWorldState { get; } IStateReader GlobalStateReader { get; } + + /// + /// Used by read only tasks that need to execute blocks. + /// Why does it need an `IStateReader`? I'm not sure, but I don't wanna break things. + /// The Action here is a resetter. Previously an explicit DbProvider's read only implementation need to be reset. + /// + /// (IWorldState, IStateReader, Action) CreateResettableWorldState(); event EventHandler? ReorgBoundaryReached; } diff --git a/src/Nethermind/Nethermind.Synchronization.Test/OldStyleFullSynchronizerTests.cs b/src/Nethermind/Nethermind.Synchronization.Test/OldStyleFullSynchronizerTests.cs index eeda9bd0741..2d99f6b3d99 100644 --- a/src/Nethermind/Nethermind.Synchronization.Test/OldStyleFullSynchronizerTests.cs +++ b/src/Nethermind/Nethermind.Synchronization.Test/OldStyleFullSynchronizerTests.cs @@ -80,7 +80,6 @@ public async Task Setup() blockDownloaderFactory, pivot, Substitute.For(), - trieStore.AsReadOnly(), bestPeerStrategy, new ChainSpec(), stateReader, diff --git a/src/Nethermind/Nethermind.Synchronization.Test/SyncThreadTests.cs b/src/Nethermind/Nethermind.Synchronization.Test/SyncThreadTests.cs index aac4193324f..4753b1bbcda 100644 --- a/src/Nethermind/Nethermind.Synchronization.Test/SyncThreadTests.cs +++ b/src/Nethermind/Nethermind.Synchronization.Test/SyncThreadTests.cs @@ -360,7 +360,6 @@ private SyncTestContext CreateSyncManager(int index) blockDownloaderFactory, pivot, Substitute.For(), - trieStore.AsReadOnly(), bestPeerStrategy, new ChainSpec(), stateReader, diff --git a/src/Nethermind/Nethermind.Synchronization.Test/SynchronizerTests.cs b/src/Nethermind/Nethermind.Synchronization.Test/SynchronizerTests.cs index 6807ea4cc0e..f5b04a53354 100644 --- a/src/Nethermind/Nethermind.Synchronization.Test/SynchronizerTests.cs +++ b/src/Nethermind/Nethermind.Synchronization.Test/SynchronizerTests.cs @@ -368,7 +368,6 @@ ISyncConfig GetSyncConfig() => mergeConfig, invalidChainTracker, Substitute.For(), - trieStore.AsReadOnly(), bestPeerStrategy, new ChainSpec(), No.BeaconSync, @@ -395,7 +394,6 @@ ISyncConfig GetSyncConfig() => blockDownloaderFactory, pivot, Substitute.For(), - trieStore.AsReadOnly(), bestPeerStrategy, new ChainSpec(), reader, diff --git a/src/Nethermind/Nethermind.Synchronization/Synchronizer.cs b/src/Nethermind/Nethermind.Synchronization/Synchronizer.cs index b5b3d951a0c..fee01a19fe9 100644 --- a/src/Nethermind/Nethermind.Synchronization/Synchronizer.cs +++ b/src/Nethermind/Nethermind.Synchronization/Synchronizer.cs @@ -85,9 +85,6 @@ public class Synchronizer : ISynchronizer SnapSyncFeed, _logManager); - private readonly IReadOnlyTrieStore _readOnlyTrieStore; - - protected ISyncModeSelector? _syncModeSelector; private readonly IStateReader _stateReader; @@ -111,7 +108,6 @@ public Synchronizer( IBlockDownloaderFactory blockDownloaderFactory, IPivot pivot, IProcessExitSource processExitSource, - IReadOnlyTrieStore readOnlyTrieStore, IBetterPeerStrategy betterPeerStrategy, ChainSpec chainSpec, IStateReader stateReader, @@ -131,8 +127,7 @@ public Synchronizer( _logManager = logManager ?? throw new ArgumentNullException(nameof(logManager)); _betterPeerStrategy = betterPeerStrategy ?? throw new ArgumentNullException(nameof(betterPeerStrategy)); _chainSpec = chainSpec ?? throw new ArgumentNullException(nameof(chainSpec)); - _readOnlyTrieStore = readOnlyTrieStore ?? throw new ArgumentNullException(nameof(readOnlyTrieStore)); - _stateReader = stateReader ?? throw new ArgumentNullException(nameof(readOnlyTrieStore)); + _stateReader = stateReader ?? throw new ArgumentNullException(nameof(_stateReader)); _syncReport = new SyncReport(_syncPeerPool!, nodeStatsManager!, _syncConfig, _pivot, logManager); From 96b68ffb0e9c7cba818a675c9eacf1eb3ce25c34 Mon Sep 17 00:00:00 2001 From: Amirul Ashraf Date: Wed, 8 Nov 2023 19:30:43 +0800 Subject: [PATCH 09/17] Minor comment --- src/Nethermind/Nethermind.Init/InitializeStateDb.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Nethermind/Nethermind.Init/InitializeStateDb.cs b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs index d17e5ab8d04..29756c1c8e3 100644 --- a/src/Nethermind/Nethermind.Init/InitializeStateDb.cs +++ b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs @@ -113,6 +113,8 @@ public Task Execute(CancellationToken cancellationToken) pruningStrategy, persistenceStrategy, getApi.LogManager); + + // TODO: Needed by node serving. Probably should use `StateReader` instead. setApi.TrieStore = trieStore; IWorldState worldState = syncConfig.TrieHealing @@ -135,6 +137,7 @@ public Task Execute(CancellationToken cancellationToken) }; } + // This is probably the point where a different state implementation would switch. IWorldStateManager stateManager = setApi.WorldStateManager = new WorldStateManager( worldState, trieStore, From d94588ae7b61957579f1fa95c7964162ec85954c Mon Sep 17 00:00:00 2001 From: Amirul Ashraf Date: Wed, 8 Nov 2023 19:35:24 +0800 Subject: [PATCH 10/17] Reducing change --- src/Nethermind/Nethermind.Analytics/AnalyticsPlugin.cs | 2 +- src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs | 1 + .../Nethermind.Consensus.Clique/CliquePlugin.cs | 2 +- .../Nethermind.Consensus.Ethash/NethDevPlugin.cs | 2 +- .../Nethermind.Init/Steps/InitializeBlockchain.cs | 4 ++-- .../Nethermind.Init/Steps/InitializeNetwork.cs | 2 +- .../Nethermind.Init/Steps/RegisterRpcModules.cs | 3 ++- .../EngineModuleTests.Synchronization.cs | 2 +- src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs | 9 +++++---- src/Nethermind/Nethermind.Mev/MevPlugin.cs | 2 +- src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs | 4 ++-- 11 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/Nethermind/Nethermind.Analytics/AnalyticsPlugin.cs b/src/Nethermind/Nethermind.Analytics/AnalyticsPlugin.cs index d483df6c7fc..503ce7ffc4c 100644 --- a/src/Nethermind/Nethermind.Analytics/AnalyticsPlugin.cs +++ b/src/Nethermind/Nethermind.Analytics/AnalyticsPlugin.cs @@ -91,7 +91,7 @@ public Task InitRpcModules() { var (getFromAPi, _) = _api.ForRpc; AnalyticsRpcModule analyticsRpcModule = new( - getFromAPi.BlockTree, getFromAPi.WorldStateManager!.GlobalStateReader, getFromAPi.LogManager); + getFromAPi.BlockTree, getFromAPi.StateReader, getFromAPi.LogManager); getFromAPi.RpcModuleProvider.Register(new SingletonModulePool(analyticsRpcModule)); return Task.CompletedTask; } diff --git a/src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs b/src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs index b19d307b648..97ebb787cf2 100644 --- a/src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs +++ b/src/Nethermind/Nethermind.Api/IApiWithBlockchain.cs @@ -59,6 +59,7 @@ public interface IApiWithBlockchain : IApiWithStores, IBlockchainBridgeFactory /// IWorldState? WorldState { get; set; } IReadOnlyStateProvider? ChainHeadStateProvider { get; set; } + IStateReader? StateReader { get; set; } IWorldStateManager? WorldStateManager { get; set; } ITransactionProcessor? TransactionProcessor { get; set; } ITrieStore? TrieStore { get; set; } diff --git a/src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs b/src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs index a97f6f3e762..9b36dc86353 100644 --- a/src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs +++ b/src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs @@ -114,7 +114,7 @@ public Task InitBlockProducer(IBlockProductionTrigger? blockProd readOnlyBlockTree, producerProcessor, getFromApi.BlockPreprocessor, - getFromApi.WorldStateManager!.GlobalStateReader, + getFromApi.StateReader, getFromApi.LogManager, BlockchainProcessor.Options.NoReceipts); diff --git a/src/Nethermind/Nethermind.Consensus.Ethash/NethDevPlugin.cs b/src/Nethermind/Nethermind.Consensus.Ethash/NethDevPlugin.cs index b5254e1ade3..7220b3c2bba 100644 --- a/src/Nethermind/Nethermind.Consensus.Ethash/NethDevPlugin.cs +++ b/src/Nethermind/Nethermind.Consensus.Ethash/NethDevPlugin.cs @@ -83,7 +83,7 @@ public Task InitBlockProducer(IBlockProductionTrigger? blockProd readOnlyBlockTree, producerProcessor, getFromApi.BlockPreprocessor, - getFromApi.WorldStateManager!.GlobalStateReader, + getFromApi.StateReader, getFromApi.LogManager, BlockchainProcessor.Options.NoReceipts); diff --git a/src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs b/src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs index fef3be37609..798b19e6b91 100644 --- a/src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs +++ b/src/Nethermind/Nethermind.Init/Steps/InitializeBlockchain.cs @@ -70,7 +70,7 @@ protected virtual Task InitBlockchain() IInitConfig initConfig = getApi.Config(); IBlocksConfig blocksConfig = getApi.Config(); - IStateReader stateReader = setApi.WorldStateManager!.GlobalStateReader; + IStateReader stateReader = setApi.StateReader!; ITxPool txPool = _api.TxPool = CreateTxPool(); ReceiptCanonicalityMonitor receiptCanonicalityMonitor = new(getApi.BlockTree, getApi.ReceiptStorage, _api.LogManager); @@ -185,7 +185,7 @@ protected virtual IBlockProductionPolicy CreateBlockProductionPolicy() => protected virtual TxPool.TxPool CreateTxPool() => new(_api.EthereumEcdsa!, _api.BlobTxStorage ?? NullBlobTxStorage.Instance, - new ChainHeadInfoProvider(_api.SpecProvider!, _api.BlockTree!, _api.WorldStateManager!.GlobalStateReader!), + new ChainHeadInfoProvider(_api.SpecProvider!, _api.BlockTree!, _api.StateReader!), _api.Config(), _api.TxValidator!, _api.LogManager, diff --git a/src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs b/src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs index c939d5841e6..8c6977c05db 100644 --- a/src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs +++ b/src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs @@ -154,7 +154,7 @@ private async Task Initialize(CancellationToken cancellationToken) _api.ProcessExit!, _api.BetterPeerStrategy, _api.ChainSpec, - _api.WorldStateManager!.GlobalStateReader, + _api.StateReader!, _api.LogManager); } diff --git a/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs b/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs index 7dc22287e4e..b4aaab22365 100644 --- a/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs +++ b/src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs @@ -60,6 +60,7 @@ public virtual async Task Execute(CancellationToken cancellationToken) if (_api.SpecProvider is null) throw new StepDependencyException(nameof(_api.SpecProvider)); if (_api.SyncModeSelector is null) throw new StepDependencyException(nameof(_api.SyncModeSelector)); if (_api.TxSender is null) throw new StepDependencyException(nameof(_api.TxSender)); + if (_api.StateReader is null) throw new StepDependencyException(nameof(_api.StateReader)); if (_api.WorldStateManager is null) throw new StepDependencyException(nameof(_api.WorldStateManager)); if (_api.PeerManager is null) throw new StepDependencyException(nameof(_api.PeerManager)); @@ -97,7 +98,7 @@ public virtual async Task Execute(CancellationToken cancellationToken) _api.BlockTree, rpcConfig, _api.LogManager, - _api.WorldStateManager.GlobalStateReader, + _api.StateReader, _api, _api.SpecProvider, _api.ReceiptStorage, diff --git a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Synchronization.cs b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Synchronization.cs index ab2c11c5a0a..335b0707324 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Synchronization.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Synchronization.cs @@ -934,7 +934,7 @@ private MultiSyncModeSelector CreateMultiSyncModeSelector(MergeTestBlockchain ch SyncProgressResolver syncProgressResolver = new( chain.BlockTree, - new FullStateFinder(chain.BlockTree, chain._readOnlyWorldStateManager.GlobalStateReader), + new FullStateFinder(chain.BlockTree, chain.StateReader), new SyncConfig(), Substitute.For>(), Substitute.For>(), diff --git a/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs b/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs index ac996813065..a8f65248086 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin/MergePlugin.cs @@ -263,7 +263,7 @@ public Task InitRpcModules() if (_api.BlockValidator is null) throw new ArgumentNullException(nameof(_api.BlockValidator)); if (_api.BlockProcessingQueue is null) throw new ArgumentNullException(nameof(_api.BlockProcessingQueue)); if (_api.SpecProvider is null) throw new ArgumentNullException(nameof(_api.SpecProvider)); - if (_api.WorldStateManager is null) throw new ArgumentNullException(nameof(_api.WorldStateManager)); + if (_api.StateReader is null) throw new ArgumentNullException(nameof(_api.StateReader)); if (_beaconPivot is null) throw new ArgumentNullException(nameof(_beaconPivot)); if (_beaconSync is null) throw new ArgumentNullException(nameof(_beaconSync)); if (_blockProductionTrigger is null) throw new ArgumentNullException(nameof(_blockProductionTrigger)); @@ -289,7 +289,7 @@ public Task InitRpcModules() { DefaultHttpClient httpClient = new(new HttpClient(), _api.EthereumJsonSerializer, _api.LogManager, retryDelayMilliseconds: 100); IBoostRelay boostRelay = new BoostRelay(httpClient, _mergeConfig.BuilderRelayUrl); - BoostBlockImprovementContextFactory boostBlockImprovementContextFactory = new(_blockProductionTrigger, TimeSpan.FromSeconds(_blocksConfig.SecondsPerSlot), boostRelay, _api.WorldStateManager.GlobalStateReader); + BoostBlockImprovementContextFactory boostBlockImprovementContextFactory = new(_blockProductionTrigger, TimeSpan.FromSeconds(_blocksConfig.SecondsPerSlot), boostRelay, _api.StateReader); improvementContextFactory = boostBlockImprovementContextFactory; } @@ -371,6 +371,7 @@ public Task InitSynchronization() if (_api.NodeStatsManager is null) throw new ArgumentNullException(nameof(_api.NodeStatsManager)); if (_api.HeaderValidator is null) throw new ArgumentNullException(nameof(_api.HeaderValidator)); if (_api.PeerDifficultyRefreshPool is null) throw new ArgumentNullException(nameof(_api.PeerDifficultyRefreshPool)); + if (_api.StateReader is null) throw new ArgumentNullException(nameof(_api.StateReader)); // ToDo strange place for validators initialization PeerRefresher peerRefresher = new(_api.PeerDifficultyRefreshPool, _api.TimerFactory, _api.LogManager); @@ -415,7 +416,7 @@ public Task InitSynchronization() _api.SealValidator!, _syncConfig, _api.BetterPeerStrategy!, - new FullStateFinder(_api.BlockTree, _api.WorldStateManager!.GlobalStateReader), + new FullStateFinder(_api.BlockTree, _api.StateReader), _api.LogManager); MergeSynchronizer synchronizer = new MergeSynchronizer( @@ -435,7 +436,7 @@ public Task InitSynchronization() _api.BetterPeerStrategy, _api.ChainSpec, _beaconSync, - _api.WorldStateManager.GlobalStateReader, + _api.StateReader, _api.LogManager ); _api.Synchronizer = synchronizer; diff --git a/src/Nethermind/Nethermind.Mev/MevPlugin.cs b/src/Nethermind/Nethermind.Mev/MevPlugin.cs index f1ec8a96d4b..7a8b4bd2618 100644 --- a/src/Nethermind/Nethermind.Mev/MevPlugin.cs +++ b/src/Nethermind/Nethermind.Mev/MevPlugin.cs @@ -114,7 +114,7 @@ public Task InitRpcModules() MevModuleFactory mevModuleFactory = new(rpcConfig, BundlePool, getFromApi.BlockTree!, - getFromApi.WorldStateManager!.GlobalStateReader, + getFromApi.StateReader!, TracerFactory, getFromApi.SpecProvider!, getFromApi.EngineSigner); diff --git a/src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs b/src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs index 804b34d7338..d95345a7fbb 100644 --- a/src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs +++ b/src/Nethermind/Nethermind.Optimism/OptimismPlugin.cs @@ -148,7 +148,7 @@ public Task InitSynchronization() _api.SealValidator!, _syncConfig, _api.BetterPeerStrategy!, - new FullStateFinder(_api.BlockTree, _api.WorldStateManager!.GlobalStateReader), + new FullStateFinder(_api.BlockTree, _api.StateReader!), _api.LogManager); _api.Synchronizer = new MergeSynchronizer( @@ -168,7 +168,7 @@ public Task InitSynchronization() _api.BetterPeerStrategy, _api.ChainSpec, _beaconSync, - _api.WorldStateManager.GlobalStateReader, + _api.StateReader!, _api.LogManager ); From 34479ca6dce7f9b4f3670f80a16e53a906e3b3bd Mon Sep 17 00:00:00 2001 From: Amirul Ashraf Date: Wed, 8 Nov 2023 20:13:36 +0800 Subject: [PATCH 11/17] Missed a setter --- src/Nethermind/Nethermind.Init/InitializeStateDb.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Nethermind/Nethermind.Init/InitializeStateDb.cs b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs index 29756c1c8e3..2c8e28acfb3 100644 --- a/src/Nethermind/Nethermind.Init/InitializeStateDb.cs +++ b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs @@ -151,6 +151,7 @@ public Task Execute(CancellationToken cancellationToken) getApi.DisposeStack.Push(trieStore); setApi.WorldState = stateManager.GlobalWorldState; + setApi.StateReader = stateManager.GlobalStateReader; setApi.ChainHeadStateProvider = new ChainHeadReadOnlyStateProvider(getApi.BlockTree, stateManager.GlobalStateReader); worldState.StateRoot = getApi.BlockTree!.Head?.StateRoot ?? Keccak.EmptyTreeHash; From 78d305227e59a5c698c7190643ec975530d94c8c Mon Sep 17 00:00:00 2001 From: Amirul Ashraf Date: Wed, 8 Nov 2023 20:51:06 +0800 Subject: [PATCH 12/17] Some tests --- .../Blockchain/TestBlockchain.cs | 10 ++- .../Nethermind.Init/InitializeStateDb.cs | 1 - .../WorldStateManagerTests.cs | 65 +++++++++++++++++++ .../ReadOnlyWorldStateManager.cs | 9 ++- .../Nethermind.State/WorldStateManager.cs | 3 +- 5 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 src/Nethermind/Nethermind.State.Test/WorldStateManagerTests.cs diff --git a/src/Nethermind/Nethermind.Core.Test/Blockchain/TestBlockchain.cs b/src/Nethermind/Nethermind.Core.Test/Blockchain/TestBlockchain.cs index 4f6fd8a9842..1163effe3da 100644 --- a/src/Nethermind/Nethermind.Core.Test/Blockchain/TestBlockchain.cs +++ b/src/Nethermind/Nethermind.Core.Test/Blockchain/TestBlockchain.cs @@ -50,7 +50,7 @@ public class TestBlockchain : IDisposable public IReceiptStorage ReceiptStorage { get; set; } = null!; public ITxPool TxPool { get; set; } = null!; public IDb CodeDb => DbProvider.CodeDb; - public IWorldStateManager _readOnlyWorldStateManager = null!; + public IWorldStateManager WorldStateManager { get; set; } = null!; public IBlockProcessor BlockProcessor { get; set; } = null!; public IBeaconBlockRootHandler BeaconBlockRootHandler { get; set; } = null!; public IBlockchainProcessor BlockchainProcessor { get; set; } = null!; @@ -141,7 +141,7 @@ protected virtual async Task Build(ISpecProvider? specProvider = State.CommitTree(0); ReadOnlyTrieStore = TrieStore.AsReadOnly(StateDb); - _readOnlyWorldStateManager = new ReadOnlyWorldStateManager(DbProvider, ReadOnlyTrieStore, LimboLogs.Instance); + WorldStateManager = new WorldStateManager(State, TrieStore, DbProvider, LimboLogs.Instance); StateReader = new StateReader(ReadOnlyTrieStore, CodeDb, LogManager); BlockTree = Builders.Build.A.BlockTree() @@ -158,9 +158,7 @@ protected virtual async Task Build(ISpecProvider? specProvider = NonceManager = new NonceManager(chainHeadInfoProvider.AccountStateProvider); - WorldStateManager stateManager = new(State, TrieStore, DbProvider, ReadOnlyTrieStore, LimboLogs.Instance); - - _trieStoreWatcher = new TrieStoreBoundaryWatcher(stateManager, BlockTree, LogManager); + _trieStoreWatcher = new TrieStoreBoundaryWatcher(WorldStateManager, BlockTree, LogManager); ReceiptStorage = new InMemoryReceiptStorage(); VirtualMachine virtualMachine = new(new BlockhashProvider(BlockTree, LogManager), SpecProvider, LogManager); @@ -252,7 +250,7 @@ protected virtual IBlockProducer CreateTestBlockProducer(TxPoolTxSource txPoolTx BlocksConfig blocksConfig = new(); BlockProducerEnvFactory blockProducerEnvFactory = new( - _readOnlyWorldStateManager, + WorldStateManager, BlockTree, SpecProvider, BlockValidator, diff --git a/src/Nethermind/Nethermind.Init/InitializeStateDb.cs b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs index 2c8e28acfb3..dc655ebb7db 100644 --- a/src/Nethermind/Nethermind.Init/InitializeStateDb.cs +++ b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs @@ -142,7 +142,6 @@ public Task Execute(CancellationToken cancellationToken) worldState, trieStore, getApi.DbProvider, - trieStore.AsReadOnly(cachedStateDb), getApi.LogManager); // TODO: Don't forget this diff --git a/src/Nethermind/Nethermind.State.Test/WorldStateManagerTests.cs b/src/Nethermind/Nethermind.State.Test/WorldStateManagerTests.cs new file mode 100644 index 00000000000..cf14b7b97e7 --- /dev/null +++ b/src/Nethermind/Nethermind.State.Test/WorldStateManagerTests.cs @@ -0,0 +1,65 @@ +// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited +// SPDX-License-Identifier: LGPL-3.0-only + +using System; +using FluentAssertions; +using Nethermind.Core; +using Nethermind.Core.Crypto; +using Nethermind.Db; +using Nethermind.Logging; +using Nethermind.Specs; +using Nethermind.State; +using Nethermind.Trie.Pruning; +using NSubstitute; +using NUnit.Framework; + +namespace Nethermind.Store.Test; + +public class WorldStateManagerTests +{ + [Test] + public void ShouldProxyGlobalWorldState() + { + IWorldState worldState = Substitute.For(); + ITrieStore trieStore = Substitute.For(); + IDbProvider dbProvider = TestMemDbProvider.Init(); + WorldStateManager worldStateManager = new WorldStateManager(worldState, trieStore, dbProvider, LimboLogs.Instance); + + worldStateManager.GlobalWorldState.Should().Be(worldState); + } + + [Test] + public void ShouldProxyReorgBoundaryEvent() + { + IWorldState worldState = Substitute.For(); + ITrieStore trieStore = Substitute.For(); + IDbProvider dbProvider = TestMemDbProvider.Init(); + WorldStateManager worldStateManager = new WorldStateManager(worldState, trieStore, dbProvider, LimboLogs.Instance); + + bool gotEvent = false; + worldStateManager.ReorgBoundaryReached += (sender, reached) => gotEvent = true; + trieStore.ReorgBoundaryReached += Raise.EventWith(new ReorgBoundaryReached(1)); + + gotEvent.Should().BeTrue(); + } + + [Test] + public void ShouldCreateTemporaryWorldState_AndCanReset() + { + IWorldState worldState = Substitute.For(); + ITrieStore trieStore = Substitute.For(); + IDbProvider dbProvider = TestMemDbProvider.Init(); + WorldStateManager worldStateManager = new WorldStateManager(worldState, trieStore, dbProvider, LimboLogs.Instance); + + (IWorldState tempWorldState, IStateReader stateReader, Action reset) = worldStateManager.CreateResettableWorldState(); + + byte[] code = new byte[] { 1 }; + Hash256 codeHash = Keccak.Compute(code); + tempWorldState.CreateAccount(Address.Zero, 0, 0); + tempWorldState.InsertCode(Address.Zero, code, MainnetSpecProvider.Instance.GenesisSpec); + + stateReader.GetCode(codeHash).Should().NotBeNull(); + reset(); + stateReader.GetCode(codeHash).Should().BeNull(); + } +} diff --git a/src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs b/src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs index d40f27b05a9..eb412c5d505 100644 --- a/src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs +++ b/src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs @@ -39,12 +39,15 @@ ILogManager logManager public (IWorldState, IStateReader, Action) CreateResettableWorldState() { ReadOnlyDbProvider readOnlyDbProvider = _dbProvider.AsReadOnly(false); - IKeyValueStore codeDb = readOnlyDbProvider.GetDb(DbNames.Code).AsReadOnly(true); + ReadOnlyDb codeDb = readOnlyDbProvider.GetDb(DbNames.Code).AsReadOnly(true); return ( new WorldState(_readOnlyTrieStore, codeDb, _logManager), new StateReader(_readOnlyTrieStore, codeDb, _logManager), - readOnlyDbProvider.ClearTempChanges - ); + () => + { + readOnlyDbProvider.ClearTempChanges(); + codeDb.ClearTempChanges(); + }); } public virtual event EventHandler? ReorgBoundaryReached diff --git a/src/Nethermind/Nethermind.State/WorldStateManager.cs b/src/Nethermind/Nethermind.State/WorldStateManager.cs index da79cec79a7..ddc74445be9 100644 --- a/src/Nethermind/Nethermind.State/WorldStateManager.cs +++ b/src/Nethermind/Nethermind.State/WorldStateManager.cs @@ -17,9 +17,8 @@ public WorldStateManager( IWorldState worldState, ITrieStore trieStore, IDbProvider dbProvider, - IReadOnlyTrieStore? readOnlyTrieStore, ILogManager logManager - ) : base(dbProvider, readOnlyTrieStore, logManager) + ) : base(dbProvider, trieStore.AsReadOnly(), logManager) { _worldState = worldState; _trieStore = trieStore; From fc1a5a6700601f51938b79c3d9810285db5a3c69 Mon Sep 17 00:00:00 2001 From: Amirul Ashraf Date: Wed, 8 Nov 2023 21:01:24 +0800 Subject: [PATCH 13/17] Fix build --- ...tionRpcModuleTests.TestAccountAbstractionRpcBlockchain.cs | 4 ++-- .../Contract/AuRaContractGasLimitOverrideTests.cs | 2 +- .../Nethermind.AuRa.Test/Contract/TxPriorityContractTests.cs | 2 +- .../Transactions/TxCertifierFilterTests.cs | 2 +- .../Transactions/TxPermissionFilterTest.cs | 4 ++-- src/Nethermind/Nethermind.Init/InitializeStateDb.cs | 2 +- .../Nethermind.JsonRpc.Test/Modules/TestRpcBlockchain.cs | 2 +- .../Nethermind.JsonRpc.Test/Modules/TraceRpcModuleTests.cs | 2 +- .../Nethermind.Merge.AuRa.Test/AuRaMergeEngineModuleTests.cs | 4 ++-- .../Nethermind.Merge.Plugin.Test/EngineModuleTests.Setup.cs | 2 +- .../MevRpcModuleTests.TestMevRpcBlockchain.cs | 4 ++-- src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs | 5 +++-- src/Nethermind/Nethermind.State/WorldStateManager.cs | 2 +- 13 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/Nethermind/Nethermind.AccountAbstraction.Test/AccountAbstractionRpcModuleTests.TestAccountAbstractionRpcBlockchain.cs b/src/Nethermind/Nethermind.AccountAbstraction.Test/AccountAbstractionRpcModuleTests.TestAccountAbstractionRpcBlockchain.cs index f976fe49fd3..556fdf6efbb 100644 --- a/src/Nethermind/Nethermind.AccountAbstraction.Test/AccountAbstractionRpcModuleTests.TestAccountAbstractionRpcBlockchain.cs +++ b/src/Nethermind/Nethermind.AccountAbstraction.Test/AccountAbstractionRpcModuleTests.TestAccountAbstractionRpcBlockchain.cs @@ -99,7 +99,7 @@ protected override IBlockProducer CreateTestBlockProducer(TxPoolTxSource txPoolT SpecProvider.UpdateMergeTransitionInfo(1, 0); BlockProducerEnvFactory blockProducerEnvFactory = new BlockProducerEnvFactory( - _readOnlyWorldStateManager, + WorldStateManager, BlockTree, SpecProvider, BlockValidator, @@ -211,7 +211,7 @@ protected override BlockProcessor CreateBlockProcessor() UserOperationSimulator[entryPoint] = new( UserOperationTxBuilder[entryPoint], ReadOnlyState, - new ReadOnlyTxProcessingEnvFactory(_readOnlyWorldStateManager, BlockTree, SpecProvider, LogManager), + new ReadOnlyTxProcessingEnvFactory(WorldStateManager, BlockTree, SpecProvider, LogManager), EntryPointContractAbi, entryPoint!, WhitelistedPayamsters, diff --git a/src/Nethermind/Nethermind.AuRa.Test/Contract/AuRaContractGasLimitOverrideTests.cs b/src/Nethermind/Nethermind.AuRa.Test/Contract/AuRaContractGasLimitOverrideTests.cs index defd82ddb06..aa52e803a15 100644 --- a/src/Nethermind/Nethermind.AuRa.Test/Contract/AuRaContractGasLimitOverrideTests.cs +++ b/src/Nethermind/Nethermind.AuRa.Test/Contract/AuRaContractGasLimitOverrideTests.cs @@ -83,7 +83,7 @@ protected override BlockProcessor CreateBlockProcessor() KeyValuePair blockGasLimitContractTransition = ChainSpec.AuRa.BlockGasLimitContractTransitions.First(); BlockGasLimitContract gasLimitContract = new(AbiEncoder.Instance, blockGasLimitContractTransition.Value, blockGasLimitContractTransition.Key, new ReadOnlyTxProcessingEnv( - _readOnlyWorldStateManager, + WorldStateManager, BlockTree, SpecProvider, LimboLogs.Instance)); GasLimitOverrideCache = new AuRaContractGasLimitOverride.Cache(); diff --git a/src/Nethermind/Nethermind.AuRa.Test/Contract/TxPriorityContractTests.cs b/src/Nethermind/Nethermind.AuRa.Test/Contract/TxPriorityContractTests.cs index 416a338de0f..276671dab2a 100644 --- a/src/Nethermind/Nethermind.AuRa.Test/Contract/TxPriorityContractTests.cs +++ b/src/Nethermind/Nethermind.AuRa.Test/Contract/TxPriorityContractTests.cs @@ -254,7 +254,7 @@ protected override TxPoolTxSource CreateTxPoolTxSource() TxPoolTxSource txPoolTxSource = base.CreateTxPoolTxSource(); TxPriorityContract = new TxPriorityContract(AbiEncoder.Instance, TestItem.AddressA, - new ReadOnlyTxProcessingEnv(_readOnlyWorldStateManager, BlockTree, SpecProvider, LimboLogs.Instance)); + new ReadOnlyTxProcessingEnv(WorldStateManager, BlockTree, SpecProvider, LimboLogs.Instance)); Priorities = new DictionaryContractDataStore( new TxPriorityContract.DestinationSortedListContractDataStoreCollection(), diff --git a/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxCertifierFilterTests.cs b/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxCertifierFilterTests.cs index c9fbcbcb847..0e6d4c5bde4 100644 --- a/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxCertifierFilterTests.cs +++ b/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxCertifierFilterTests.cs @@ -137,7 +137,7 @@ protected override BlockProcessor CreateBlockProcessor() { AbiEncoder abiEncoder = AbiEncoder.Instance; ReadOnlyTransactionProcessorSource = new ReadOnlyTxProcessingEnv( - _readOnlyWorldStateManager, + WorldStateManager, BlockTree, SpecProvider, LimboLogs.Instance); RegisterContract = new RegisterContract(abiEncoder, ChainSpec.Parameters.Registrar, ReadOnlyTransactionProcessorSource); diff --git a/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxPermissionFilterTest.cs b/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxPermissionFilterTest.cs index ca279aa5de5..6c9553f5b19 100644 --- a/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxPermissionFilterTest.cs +++ b/src/Nethermind/Nethermind.AuRa.Test/Transactions/TxPermissionFilterTest.cs @@ -276,13 +276,13 @@ protected override BlockProcessor CreateBlockProcessor() IReadOnlyTrieStore trieStore = new TrieStore(DbProvider.StateDb, LimboLogs.Instance).AsReadOnly(); IReadOnlyTxProcessorSource txProcessorSource = new ReadOnlyTxProcessingEnv( - _readOnlyWorldStateManager, + WorldStateManager, BlockTree, SpecProvider, LimboLogs.Instance); VersionedTransactionPermissionContract transactionPermissionContract = new(AbiEncoder.Instance, _contractAddress, 1, - new ReadOnlyTxProcessingEnv(_readOnlyWorldStateManager, BlockTree, SpecProvider, LimboLogs.Instance), TransactionPermissionContractVersions, LimboLogs.Instance, SpecProvider); + new ReadOnlyTxProcessingEnv(WorldStateManager, BlockTree, SpecProvider, LimboLogs.Instance), TransactionPermissionContractVersions, LimboLogs.Instance, SpecProvider); TxPermissionFilterCache = new PermissionBasedTxFilter.Cache(); PermissionBasedTxFilter = new PermissionBasedTxFilter(transactionPermissionContract, TxPermissionFilterCache, LimboLogs.Instance); diff --git a/src/Nethermind/Nethermind.Init/InitializeStateDb.cs b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs index dc655ebb7db..a82b57becc2 100644 --- a/src/Nethermind/Nethermind.Init/InitializeStateDb.cs +++ b/src/Nethermind/Nethermind.Init/InitializeStateDb.cs @@ -31,7 +31,7 @@ namespace Nethermind.Init; [RunnerStepDependencies(typeof(InitializePlugins), typeof(InitializeBlockTree), typeof(SetupKeyStore))] -public class InitializeStateDb: IStep +public class InitializeStateDb : IStep { private readonly INethermindApi _api; private ILogger? _logger; diff --git a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TestRpcBlockchain.cs b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TestRpcBlockchain.cs index 0a0c5c49e75..753dcd8ec13 100644 --- a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TestRpcBlockchain.cs +++ b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TestRpcBlockchain.cs @@ -123,7 +123,7 @@ protected override async Task Build(ISpecProvider? specProvider IFilterManager filterManager = new FilterManager(filterStore, BlockProcessor, TxPool, LimboLogs.Instance); ReadOnlyTxProcessingEnv processingEnv = new( - _readOnlyWorldStateManager, + WorldStateManager, new ReadOnlyBlockTree(BlockTree), SpecProvider, LimboLogs.Instance); diff --git a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TraceRpcModuleTests.cs b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TraceRpcModuleTests.cs index b4b7181b16e..26de8245dab 100644 --- a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TraceRpcModuleTests.cs +++ b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TraceRpcModuleTests.cs @@ -50,7 +50,7 @@ public async Task Build(ISpecProvider? specProvider = null, bool isAura = false) new(Blockchain.EthereumEcdsa, Blockchain.SpecProvider); IReceiptFinder receiptFinder = new FullInfoReceiptFinder(Blockchain.ReceiptStorage, receiptsRecovery, Blockchain.BlockFinder); ReadOnlyTxProcessingEnv txProcessingEnv = - new(Blockchain._readOnlyWorldStateManager, Blockchain.BlockTree.AsReadOnly(), Blockchain.SpecProvider, Blockchain.LogManager); + new(Blockchain.WorldStateManager, Blockchain.BlockTree.AsReadOnly(), Blockchain.SpecProvider, Blockchain.LogManager); RewardCalculator rewardCalculatorSource = new(Blockchain.SpecProvider); IRewardCalculator rewardCalculator = rewardCalculatorSource.Get(txProcessingEnv.TransactionProcessor); diff --git a/src/Nethermind/Nethermind.Merge.AuRa.Test/AuRaMergeEngineModuleTests.cs b/src/Nethermind/Nethermind.Merge.AuRa.Test/AuRaMergeEngineModuleTests.cs index 77c9d61c4b5..bcce08fd888 100644 --- a/src/Nethermind/Nethermind.Merge.AuRa.Test/AuRaMergeEngineModuleTests.cs +++ b/src/Nethermind/Nethermind.Merge.AuRa.Test/AuRaMergeEngineModuleTests.cs @@ -110,7 +110,7 @@ protected override IBlockProcessor CreateBlockProcessor() { BlockTree = BlockTree, DbProvider = DbProvider, - WorldStateManager = _readOnlyWorldStateManager, + WorldStateManager = WorldStateManager, SpecProvider = SpecProvider, TransactionComparerProvider = TransactionComparerProvider, TxPool = TxPool @@ -157,7 +157,7 @@ protected override IBlockProducer CreateTestBlockProducer(TxPoolTxSource txPoolT _api!, new AuRaConfig(), new DisposableStack(), - _readOnlyWorldStateManager, + WorldStateManager, BlockTree, SpecProvider, BlockValidator, diff --git a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Setup.cs b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Setup.cs index 6b7801bd78c..982c5a1741b 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Setup.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.Setup.cs @@ -188,7 +188,7 @@ protected override IBlockProducer CreateTestBlockProducer(TxPoolTxSource txPoolT targetAdjustedGasLimitCalculator); BlockProducerEnvFactory blockProducerEnvFactory = new( - _readOnlyWorldStateManager!, + WorldStateManager!, BlockTree, SpecProvider, BlockValidator, diff --git a/src/Nethermind/Nethermind.Mev.Test/MevRpcModuleTests.TestMevRpcBlockchain.cs b/src/Nethermind/Nethermind.Mev.Test/MevRpcModuleTests.TestMevRpcBlockchain.cs index 039584b0846..6824ebb1800 100644 --- a/src/Nethermind/Nethermind.Mev.Test/MevRpcModuleTests.TestMevRpcBlockchain.cs +++ b/src/Nethermind/Nethermind.Mev.Test/MevRpcModuleTests.TestMevRpcBlockchain.cs @@ -92,7 +92,7 @@ protected override IBlockProducer CreateTestBlockProducer(TxPoolTxSource txPoolT SpecProvider.UpdateMergeTransitionInfo(1, 0); BlockProducerEnvFactory blockProducerEnvFactory = new( - _readOnlyWorldStateManager, + WorldStateManager, BlockTree, SpecProvider, BlockValidator, @@ -205,7 +205,7 @@ protected override BlockProcessor CreateBlockProcessor() _tracerFactory = new TracerFactory( BlockTree, - _readOnlyWorldStateManager, + WorldStateManager, BlockPreprocessorStep, SpecProvider, LogManager, diff --git a/src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs b/src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs index eb412c5d505..d3d915ac492 100644 --- a/src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs +++ b/src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs @@ -12,7 +12,7 @@ namespace Nethermind.State; /// /// Mainly to make it easier for test /// -public class ReadOnlyWorldStateManager: IWorldStateManager +public class ReadOnlyWorldStateManager : IWorldStateManager { private IReadOnlyTrieStore? _readOnlyTrieStore; private ILogManager _logManager; @@ -47,7 +47,8 @@ ILogManager logManager { readOnlyDbProvider.ClearTempChanges(); codeDb.ClearTempChanges(); - }); + } + ); } public virtual event EventHandler? ReorgBoundaryReached diff --git a/src/Nethermind/Nethermind.State/WorldStateManager.cs b/src/Nethermind/Nethermind.State/WorldStateManager.cs index ddc74445be9..cfb570a8daa 100644 --- a/src/Nethermind/Nethermind.State/WorldStateManager.cs +++ b/src/Nethermind/Nethermind.State/WorldStateManager.cs @@ -8,7 +8,7 @@ namespace Nethermind.State; -public class WorldStateManager: ReadOnlyWorldStateManager +public class WorldStateManager : ReadOnlyWorldStateManager { private readonly IWorldState _worldState; private readonly ITrieStore _trieStore; From a655a7c2ba49910643d3f2342d3f978c6c1ecb9f Mon Sep 17 00:00:00 2001 From: Amirul Ashraf Date: Thu, 9 Nov 2023 20:20:35 +0800 Subject: [PATCH 14/17] Fix build --- .../Nethermind.JsonRpc.Benchmark/EthModuleBenchmarks.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Nethermind/Nethermind.JsonRpc.Benchmark/EthModuleBenchmarks.cs b/src/Nethermind/Nethermind.JsonRpc.Benchmark/EthModuleBenchmarks.cs index 4300f11f7f8..c846f9c8169 100644 --- a/src/Nethermind/Nethermind.JsonRpc.Benchmark/EthModuleBenchmarks.cs +++ b/src/Nethermind/Nethermind.JsonRpc.Benchmark/EthModuleBenchmarks.cs @@ -63,6 +63,8 @@ public void GlobalSetup() stateProvider.Commit(spec); stateProvider.CommitTree(0); + WorldStateManager stateManager = new WorldStateManager(stateProvider, trieStore, dbProvider, LimboLogs.Instance); + StateReader stateReader = new(trieStore, codeDb, LimboLogs.Instance); ChainLevelInfoRepository chainLevelInfoRepository = new(blockInfoDb); @@ -120,8 +122,7 @@ TransactionProcessor transactionProcessor BlockchainBridge bridge = new( new ReadOnlyTxProcessingEnv( - new ReadOnlyDbProvider(dbProvider, false), - trieStore.AsReadOnly(), + stateManager, new ReadOnlyBlockTree(blockTree), specProvider, LimboLogs.Instance), From ac1444e3c8662d72b539bb96231d1ebfc5bda127 Mon Sep 17 00:00:00 2001 From: Amirul Ashraf Date: Tue, 5 Dec 2023 15:29:20 +0800 Subject: [PATCH 15/17] Addressing comment --- .../StartBlockProducerAuRa.cs | 2 +- .../CliquePlugin.cs | 2 +- .../Processing/OneTimeProcessor.cs | 8 +++---- .../Processing/ReadOnlyChainProcessingEnv.cs | 3 +-- .../Processing/ReadOnlyTxProcessingEnv.cs | 7 ++----- .../Producers/BlockProducerEnvFactory.cs | 3 ++- .../Modules/TraceRpcModuleTests.cs | 1 - .../Modules/DebugModule/DebugModuleFactory.cs | 1 - .../Modules/Proof/ProofModuleFactory.cs | 2 +- .../Modules/Trace/TraceModuleFactory.cs | 1 - .../Nethermind.Mev/Execution/TracerFactory.cs | 2 +- .../WorldStateManagerTests.cs | 5 +++-- .../Nethermind.State/IWorldStateManager.cs | 2 +- .../ReadOnlyWorldStateManager.cs | 21 +++++++------------ .../Nethermind.State/StateProvider.cs | 5 +++++ 15 files changed, 29 insertions(+), 36 deletions(-) diff --git a/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/StartBlockProducerAuRa.cs b/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/StartBlockProducerAuRa.cs index 559e6303235..b419ed44f3a 100644 --- a/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/StartBlockProducerAuRa.cs +++ b/src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/StartBlockProducerAuRa.cs @@ -251,7 +251,7 @@ BlockProducerEnv Create() BlockchainProcessor.Options.NoReceipts); OneTimeChainProcessor chainProcessor = new( - txProcessingEnv.ResetDb, + txProcessingEnv.StateProvider, blockchainProcessor); return new BlockProducerEnv() diff --git a/src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs b/src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs index 9b36dc86353..e83e633064d 100644 --- a/src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs +++ b/src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs @@ -119,7 +119,7 @@ public Task InitBlockProducer(IBlockProductionTrigger? blockProd BlockchainProcessor.Options.NoReceipts); OneTimeChainProcessor chainProcessor = new( - producerEnv.ResetDb, + producerEnv.StateProvider, producerChainProcessor); ITxFilterPipeline txFilterPipeline = diff --git a/src/Nethermind/Nethermind.Consensus/Processing/OneTimeProcessor.cs b/src/Nethermind/Nethermind.Consensus/Processing/OneTimeProcessor.cs index 0b84e72f298..b2d0b8c14a8 100644 --- a/src/Nethermind/Nethermind.Consensus/Processing/OneTimeProcessor.cs +++ b/src/Nethermind/Nethermind.Consensus/Processing/OneTimeProcessor.cs @@ -15,13 +15,13 @@ public class OneTimeChainProcessor : IBlockchainProcessor public ITracerBag Tracers => _processor.Tracers; private readonly IBlockchainProcessor _processor; - private readonly Action _clearTemporaryState; + private readonly IWorldState _worldState; private object _lock = new(); - public OneTimeChainProcessor(Action clearTemporaryState, IBlockchainProcessor processor) + public OneTimeChainProcessor(IWorldState worldState, IBlockchainProcessor processor) { - _clearTemporaryState = clearTemporaryState ?? throw new ArgumentNullException(nameof(clearTemporaryState)); + _worldState = worldState ?? throw new ArgumentNullException(nameof(worldState)); _processor = processor ?? throw new ArgumentNullException(nameof(processor)); } @@ -46,7 +46,7 @@ public Task StopAsync(bool processRemainingBlocks = false) } finally { - _clearTemporaryState.Invoke(); + _worldState.Reset(); } return result; diff --git a/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyChainProcessingEnv.cs b/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyChainProcessingEnv.cs index 53a3cf864ed..6fff1bc71d9 100644 --- a/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyChainProcessingEnv.cs +++ b/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyChainProcessingEnv.cs @@ -31,7 +31,6 @@ public ReadOnlyChainProcessingEnv( IBlockPreprocessorStep recoveryStep, IRewardCalculator rewardCalculator, IReceiptStorage receiptStorage, - Action resetAction, ISpecProvider specProvider, ILogManager logManager, IBlockProcessor.IBlockTransactionsExecutor? blockTransactionsExecutor = null) @@ -53,7 +52,7 @@ public ReadOnlyChainProcessingEnv( _blockProcessingQueue = new BlockchainProcessor(_txEnv.BlockTree, BlockProcessor, recoveryStep, _txEnv.StateReader, logManager, BlockchainProcessor.Options.NoReceipts); BlockProcessingQueue = _blockProcessingQueue; - ChainProcessor = new OneTimeChainProcessor(resetAction, _blockProcessingQueue); + ChainProcessor = new OneTimeChainProcessor(txEnv.StateProvider, _blockProcessingQueue); } public void Dispose() diff --git a/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyTxProcessingEnv.cs b/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyTxProcessingEnv.cs index df480143b76..66d1e91cd4e 100644 --- a/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyTxProcessingEnv.cs +++ b/src/Nethermind/Nethermind.Consensus/Processing/ReadOnlyTxProcessingEnv.cs @@ -22,7 +22,6 @@ public class ReadOnlyTxProcessingEnv : IReadOnlyTxProcessorSource public IBlockTree BlockTree { get; } public IBlockhashProvider BlockhashProvider { get; } public IVirtualMachine Machine { get; } - public Action ResetDb { get; } public ReadOnlyTxProcessingEnv( IWorldStateManager worldStateManager, @@ -42,10 +41,8 @@ public ReadOnlyTxProcessingEnv( if (specProvider is null) throw new ArgumentNullException(nameof(specProvider)); if (worldStateManager is null) throw new ArgumentNullException(nameof(worldStateManager)); - (IWorldState worldState, IStateReader stateReader, Action reset) = worldStateManager.CreateResettableWorldState(); - StateReader = stateReader; - StateProvider = worldState; - ResetDb = reset; + StateReader = worldStateManager.GlobalStateReader; + StateProvider = worldStateManager.CreateResettableWorldState(); BlockTree = readOnlyBlockTree ?? throw new ArgumentNullException(nameof(readOnlyBlockTree)); BlockhashProvider = new BlockhashProvider(BlockTree, logManager); diff --git a/src/Nethermind/Nethermind.Consensus/Producers/BlockProducerEnvFactory.cs b/src/Nethermind/Nethermind.Consensus/Producers/BlockProducerEnvFactory.cs index 2670664d742..05d36c9a321 100644 --- a/src/Nethermind/Nethermind.Consensus/Producers/BlockProducerEnvFactory.cs +++ b/src/Nethermind/Nethermind.Consensus/Producers/BlockProducerEnvFactory.cs @@ -12,6 +12,7 @@ using Nethermind.Consensus.Withdrawals; using Nethermind.Core.Specs; using Nethermind.Db; +using Nethermind.Evm.Tracing.GethStyle.JavaScript; using Nethermind.Logging; using Nethermind.State; using Nethermind.TxPool; @@ -88,7 +89,7 @@ public virtual BlockProducerEnv Create(ITxSource? additionalTxSource = null) BlockchainProcessor.Options.NoReceipts); OneTimeChainProcessor chainProcessor = new( - txProcessingEnv.ResetDb, + txProcessingEnv.StateProvider, blockchainProcessor); return new BlockProducerEnv diff --git a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TraceRpcModuleTests.cs b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TraceRpcModuleTests.cs index 26de8245dab..e7c13817945 100644 --- a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TraceRpcModuleTests.cs +++ b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/TraceRpcModuleTests.cs @@ -64,7 +64,6 @@ public async Task Build(ISpecProvider? specProvider = null, bool isAura = false) Blockchain.BlockPreprocessorStep, rewardCalculator, Blockchain.ReceiptStorage, - txProcessingEnv.ResetDb, Blockchain.SpecProvider, Blockchain.LogManager, transactionsExecutor); diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/DebugModule/DebugModuleFactory.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/DebugModule/DebugModuleFactory.cs index 91f183533b2..92f25eeea44 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/DebugModule/DebugModuleFactory.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/DebugModule/DebugModuleFactory.cs @@ -90,7 +90,6 @@ public override IDebugRpcModule Create() _recoveryStep, _rewardCalculatorSource.Get(txEnv.TransactionProcessor), _receiptStorage, - txEnv.ResetDb, _specProvider, _logManager, transactionsExecutor); diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofModuleFactory.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofModuleFactory.cs index b7443b6e6ac..590f23df523 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofModuleFactory.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Proof/ProofModuleFactory.cs @@ -50,7 +50,7 @@ public override IProofRpcModule Create() _worldStateManager, _blockTree, _specProvider, _logManager); ReadOnlyChainProcessingEnv chainProcessingEnv = new( - txProcessingEnv, Always.Valid, _recoveryStep, NoBlockRewards.Instance, new InMemoryReceiptStorage(), txProcessingEnv.ResetDb, _specProvider, _logManager); + txProcessingEnv, Always.Valid, _recoveryStep, NoBlockRewards.Instance, new InMemoryReceiptStorage(), _specProvider, _logManager); Tracer tracer = new( txProcessingEnv.StateProvider, diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Trace/TraceModuleFactory.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Trace/TraceModuleFactory.cs index bfc33407e14..814bcca829c 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Trace/TraceModuleFactory.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Trace/TraceModuleFactory.cs @@ -73,7 +73,6 @@ public override ITraceRpcModule Create() _recoveryStep, rewardCalculator, _receiptStorage, - txProcessingEnv.ResetDb, _specProvider, _logManager, transactionsExecutor); diff --git a/src/Nethermind/Nethermind.Mev/Execution/TracerFactory.cs b/src/Nethermind/Nethermind.Mev/Execution/TracerFactory.cs index a01b530956e..7fd9f25a611 100644 --- a/src/Nethermind/Nethermind.Mev/Execution/TracerFactory.cs +++ b/src/Nethermind/Nethermind.Mev/Execution/TracerFactory.cs @@ -47,7 +47,7 @@ public ITracer Create() _worldStateManager, _blockTree, _specProvider, _logManager); ReadOnlyChainProcessingEnv chainProcessingEnv = new( - txProcessingEnv, Always.Valid, _recoveryStep, NoBlockRewards.Instance, new InMemoryReceiptStorage(), txProcessingEnv.ResetDb, _specProvider, _logManager); + txProcessingEnv, Always.Valid, _recoveryStep, NoBlockRewards.Instance, new InMemoryReceiptStorage(), _specProvider, _logManager); return CreateTracer(txProcessingEnv, chainProcessingEnv); } diff --git a/src/Nethermind/Nethermind.State.Test/WorldStateManagerTests.cs b/src/Nethermind/Nethermind.State.Test/WorldStateManagerTests.cs index cf14b7b97e7..754cad26b66 100644 --- a/src/Nethermind/Nethermind.State.Test/WorldStateManagerTests.cs +++ b/src/Nethermind/Nethermind.State.Test/WorldStateManagerTests.cs @@ -51,7 +51,8 @@ public void ShouldCreateTemporaryWorldState_AndCanReset() IDbProvider dbProvider = TestMemDbProvider.Init(); WorldStateManager worldStateManager = new WorldStateManager(worldState, trieStore, dbProvider, LimboLogs.Instance); - (IWorldState tempWorldState, IStateReader stateReader, Action reset) = worldStateManager.CreateResettableWorldState(); + IWorldState tempWorldState = worldStateManager.CreateResettableWorldState(); + IStateReader stateReader = worldStateManager.GlobalStateReader; byte[] code = new byte[] { 1 }; Hash256 codeHash = Keccak.Compute(code); @@ -59,7 +60,7 @@ public void ShouldCreateTemporaryWorldState_AndCanReset() tempWorldState.InsertCode(Address.Zero, code, MainnetSpecProvider.Instance.GenesisSpec); stateReader.GetCode(codeHash).Should().NotBeNull(); - reset(); + tempWorldState.Reset(); stateReader.GetCode(codeHash).Should().BeNull(); } } diff --git a/src/Nethermind/Nethermind.State/IWorldStateManager.cs b/src/Nethermind/Nethermind.State/IWorldStateManager.cs index c6f676a8df9..5893166d113 100644 --- a/src/Nethermind/Nethermind.State/IWorldStateManager.cs +++ b/src/Nethermind/Nethermind.State/IWorldStateManager.cs @@ -17,6 +17,6 @@ public interface IWorldStateManager /// The Action here is a resetter. Previously an explicit DbProvider's read only implementation need to be reset. /// /// - (IWorldState, IStateReader, Action) CreateResettableWorldState(); + IWorldState CreateResettableWorldState(); event EventHandler? ReorgBoundaryReached; } diff --git a/src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs b/src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs index d3d915ac492..d2a9b5ebce1 100644 --- a/src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs +++ b/src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs @@ -14,9 +14,11 @@ namespace Nethermind.State; /// public class ReadOnlyWorldStateManager : IWorldStateManager { + private IReadOnlyDbProvider _readOnlyDbProvider; private IReadOnlyTrieStore? _readOnlyTrieStore; private ILogManager _logManager; private readonly IDbProvider _dbProvider; + private readonly ReadOnlyDb _codeDb; public ReadOnlyWorldStateManager( IDbProvider dbProvider, @@ -28,27 +30,18 @@ ILogManager logManager _dbProvider = dbProvider; _logManager = logManager; - IKeyValueStore codeDb = _dbProvider.AsReadOnly(false).GetDb(DbNames.Code); - GlobalStateReader = new StateReader(_readOnlyTrieStore, codeDb, _logManager); + _readOnlyDbProvider = _dbProvider.AsReadOnly(false); + _codeDb = _readOnlyDbProvider.GetDb(DbNames.Code).AsReadOnly(true); + GlobalStateReader = new StateReader(_readOnlyTrieStore, _codeDb, _logManager); } public virtual IWorldState GlobalWorldState => throw new InvalidOperationException("global world state not supported"); public IStateReader GlobalStateReader { get; } - public (IWorldState, IStateReader, Action) CreateResettableWorldState() + public IWorldState CreateResettableWorldState() { - ReadOnlyDbProvider readOnlyDbProvider = _dbProvider.AsReadOnly(false); - ReadOnlyDb codeDb = readOnlyDbProvider.GetDb(DbNames.Code).AsReadOnly(true); - return ( - new WorldState(_readOnlyTrieStore, codeDb, _logManager), - new StateReader(_readOnlyTrieStore, codeDb, _logManager), - () => - { - readOnlyDbProvider.ClearTempChanges(); - codeDb.ClearTempChanges(); - } - ); + return new WorldState(_readOnlyTrieStore, _codeDb, _logManager); } public virtual event EventHandler? ReorgBoundaryReached diff --git a/src/Nethermind/Nethermind.State/StateProvider.cs b/src/Nethermind/Nethermind.State/StateProvider.cs index f24e6ca45a3..ac0b6c7334d 100644 --- a/src/Nethermind/Nethermind.State/StateProvider.cs +++ b/src/Nethermind/Nethermind.State/StateProvider.cs @@ -801,6 +801,11 @@ public void Reset() _currentPosition = Resettable.EmptyPosition; Array.Clear(_changes, 0, _changes.Length); _needsStateRootUpdate = false; + + if (_codeDb is IReadOnlyDb readOnlyDb) + { + readOnlyDb.ClearTempChanges(); + } } public void CommitTree(long blockNumber) From e6403994595c51faaab21d1b1d0a7c48b7569cdf Mon Sep 17 00:00:00 2001 From: Amirul Ashraf Date: Tue, 5 Dec 2023 15:29:52 +0800 Subject: [PATCH 16/17] Minor comment adjustment --- src/Nethermind/Nethermind.State/IWorldStateManager.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Nethermind/Nethermind.State/IWorldStateManager.cs b/src/Nethermind/Nethermind.State/IWorldStateManager.cs index 5893166d113..26a01d09f67 100644 --- a/src/Nethermind/Nethermind.State/IWorldStateManager.cs +++ b/src/Nethermind/Nethermind.State/IWorldStateManager.cs @@ -13,10 +13,9 @@ public interface IWorldStateManager /// /// Used by read only tasks that need to execute blocks. - /// Why does it need an `IStateReader`? I'm not sure, but I don't wanna break things. - /// The Action here is a resetter. Previously an explicit DbProvider's read only implementation need to be reset. /// /// IWorldState CreateResettableWorldState(); + event EventHandler? ReorgBoundaryReached; } From 45bfeeecfb1b4811ed9db4930698f96a8f384b9b Mon Sep 17 00:00:00 2001 From: Amirul Ashraf Date: Thu, 7 Dec 2023 20:19:54 +0800 Subject: [PATCH 17/17] Fix startup block tree fixer missed --- .../Visitors/StartupTreeFixerTests.cs | 13 +++++++------ .../Visitors/StartupBlockTreeFixer.cs | 9 +++++---- .../Nethermind.Init/Steps/ReviewBlockTree.cs | 2 +- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Nethermind/Nethermind.Blockchain.Test/Visitors/StartupTreeFixerTests.cs b/src/Nethermind/Nethermind.Blockchain.Test/Visitors/StartupTreeFixerTests.cs index 695a8c96a35..a6cc779152e 100644 --- a/src/Nethermind/Nethermind.Blockchain.Test/Visitors/StartupTreeFixerTests.cs +++ b/src/Nethermind/Nethermind.Blockchain.Test/Visitors/StartupTreeFixerTests.cs @@ -15,6 +15,8 @@ using Nethermind.State.Repositories; using Nethermind.Db.Blooms; using Nethermind.JsonRpc.Test.Modules; +using Nethermind.State; +using NSubstitute; using NUnit.Framework; namespace Nethermind.Blockchain.Test.Visitors @@ -72,7 +74,7 @@ public async Task Deletes_everything_after_the_missing_level() .WithDatabaseFrom(builder) .TestObject; - StartupBlockTreeFixer fixer = new(new SyncConfig(), tree, new MemDb(), LimboNoErrorLogger.Instance); + StartupBlockTreeFixer fixer = new(new SyncConfig(), tree, Substitute.For(), LimboNoErrorLogger.Instance); await tree.Accept(fixer, CancellationToken.None); Assert.Null(blockInfosDb.Get(3), "level 3"); @@ -110,7 +112,7 @@ public async Task Suggesting_blocks_works_correctly_after_processor_restart(int testRpc.BlockchainProcessor = newBlockchainProcessor; // fixing after restart - StartupBlockTreeFixer fixer = new(new SyncConfig(), tree, testRpc.DbProvider.StateDb, LimboNoErrorLogger.Instance, 5); + StartupBlockTreeFixer fixer = new(new SyncConfig(), tree, testRpc.StateReader, LimboNoErrorLogger.Instance, 5); await tree.Accept(fixer, CancellationToken.None); // waiting for N new heads @@ -144,8 +146,7 @@ public async Task Fixer_should_not_suggest_block_without_state(int suggestedBloc testRpc.BlockchainProcessor = newBlockchainProcessor; // we create a new empty db for stateDb so we shouldn't suggest new blocks - MemDb stateDb = new(); - IBlockTreeVisitor fixer = new StartupBlockTreeFixer(new SyncConfig(), tree, stateDb, LimboNoErrorLogger.Instance, 5); + IBlockTreeVisitor fixer = new StartupBlockTreeFixer(new SyncConfig(), tree, Substitute.For(), LimboNoErrorLogger.Instance, 5); BlockVisitOutcome result = await fixer.VisitBlock(tree.Head!, CancellationToken.None); Assert.That(result, Is.EqualTo(BlockVisitOutcome.None)); @@ -166,7 +167,7 @@ public async Task Fixer_should_not_suggest_block_with_null_block() newBlockchainProcessor.Start(); testRpc.BlockchainProcessor = newBlockchainProcessor; - IBlockTreeVisitor fixer = new StartupBlockTreeFixer(new SyncConfig(), tree, testRpc.DbProvider.StateDb, LimboNoErrorLogger.Instance, 5); + IBlockTreeVisitor fixer = new StartupBlockTreeFixer(new SyncConfig(), tree, testRpc.StateReader, LimboNoErrorLogger.Instance, 5); BlockVisitOutcome result = await fixer.VisitBlock(null!, CancellationToken.None); Assert.That(result, Is.EqualTo(BlockVisitOutcome.None)); @@ -213,7 +214,7 @@ public async Task When_head_block_is_followed_by_a_block_bodies_gap_it_should_de tree.UpdateMainChain(block2); - StartupBlockTreeFixer fixer = new(new SyncConfig(), tree, new MemDb(), LimboNoErrorLogger.Instance); + StartupBlockTreeFixer fixer = new(new SyncConfig(), tree, Substitute.For(), LimboNoErrorLogger.Instance); await tree.Accept(fixer, CancellationToken.None); Assert.Null(blockInfosDb.Get(3), "level 3"); diff --git a/src/Nethermind/Nethermind.Blockchain/Visitors/StartupBlockTreeFixer.cs b/src/Nethermind/Nethermind.Blockchain/Visitors/StartupBlockTreeFixer.cs index ba61002020f..2dc9658cb99 100644 --- a/src/Nethermind/Nethermind.Blockchain/Visitors/StartupBlockTreeFixer.cs +++ b/src/Nethermind/Nethermind.Blockchain/Visitors/StartupBlockTreeFixer.cs @@ -12,6 +12,7 @@ using Nethermind.Core.Crypto; using Nethermind.Db; using Nethermind.Logging; +using Nethermind.State; namespace Nethermind.Blockchain.Visitors { @@ -19,7 +20,7 @@ public class StartupBlockTreeFixer : IBlockTreeVisitor { public const int DefaultBatchSize = 4000; private readonly IBlockTree _blockTree; - private readonly IDb _stateDb; + private readonly IStateReader _stateReader; private readonly ILogger _logger; private long _startNumber; private long _blocksToLoad; @@ -42,12 +43,12 @@ public class StartupBlockTreeFixer : IBlockTreeVisitor public StartupBlockTreeFixer( ISyncConfig syncConfig, IBlockTree blockTree, - IDb stateDb, + IStateReader stateReader, ILogger logger, long batchSize = DefaultBatchSize) { _blockTree = blockTree ?? throw new ArgumentNullException(nameof(blockTree)); - _stateDb = stateDb; + _stateReader = stateReader; _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _batchSize = batchSize; @@ -242,7 +243,7 @@ private bool CanSuggestBlocks(Block block) { BlockHeader? parentHeader = _blockTree.FindParentHeader(block.Header, BlockTreeLookupOptions.TotalDifficultyNotNeeded); if (parentHeader is null || parentHeader.StateRoot is null || - _stateDb.Get(parentHeader.StateRoot) is null) + !_stateReader.HasStateForRoot(parentHeader.StateRoot)) return false; } else diff --git a/src/Nethermind/Nethermind.Init/Steps/ReviewBlockTree.cs b/src/Nethermind/Nethermind.Init/Steps/ReviewBlockTree.cs index f5ca3f245cc..b9dd8fb4797 100644 --- a/src/Nethermind/Nethermind.Init/Steps/ReviewBlockTree.cs +++ b/src/Nethermind/Nethermind.Init/Steps/ReviewBlockTree.cs @@ -59,7 +59,7 @@ await _api.BlockTree.Accept(loader, cancellationToken).ContinueWith(t => } else { - StartupBlockTreeFixer fixer = new(syncConfig, _api.BlockTree, _api.DbProvider!.StateDb, _logger!); + StartupBlockTreeFixer fixer = new(syncConfig, _api.BlockTree, _api.WorldStateManager!.GlobalStateReader, _logger!); await _api.BlockTree.Accept(fixer, cancellationToken).ContinueWith(t => { if (t.IsFaulted)