Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/isolate overrideable world state #7968

Merged
merged 7 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Nethermind/Nethermind.Api/NethermindApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,10 @@ public NethermindApi(IConfigProvider configProvider, IJsonSerializer jsonSeriali
public IBlockchainBridge CreateBlockchainBridge()
{
ReadOnlyBlockTree readOnlyTree = BlockTree!.AsReadOnly();
OverridableWorldStateManager overridableWorldStateManager = new(DbProvider!, WorldStateManager!.TrieStore, LogManager);

// TODO: reuse the same trie cache here
OverridableTxProcessingEnv txProcessingEnv = new(
overridableWorldStateManager,
WorldStateManager!.CreateOverridableWorldScope(),
readOnlyTree,
SpecProvider!,
LogManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ protected override void RegisterTraceRpcModule(IRpcModuleProvider rpcModuleProvi
StepDependencyException.ThrowIfNull(_api.SpecProvider);

AuRaTraceModuleFactory traceModuleFactory = new(
_api.WorldStateManager.TrieStore,
_api.DbProvider,
_api.WorldStateManager,
_api.BlockTree,
_jsonRpcConfig,
_api.BlockPreprocessor,
Expand All @@ -82,8 +81,7 @@ protected override void RegisterTraceRpcModule(IRpcModuleProvider rpcModuleProvi
}

protected class AuRaTraceModuleFactory(
IReadOnlyTrieStore trieStore,
IDbProvider dbProvider,
IWorldStateManager worldStateManager,
IBlockTree blockTree,
IJsonRpcConfig jsonRpcConfig,
IBlockPreprocessorStep recoveryStep,
Expand All @@ -93,10 +91,10 @@ protected class AuRaTraceModuleFactory(
IPoSSwitcher poSSwitcher,
ILogManager logManager,
IAuRaBlockProcessorFactory factory)
: TraceModuleFactory(trieStore, dbProvider, blockTree, jsonRpcConfig, recoveryStep, rewardCalculatorSource,
: TraceModuleFactory(worldStateManager, blockTree, jsonRpcConfig, recoveryStep, rewardCalculatorSource,
receiptFinder, specProvider, poSSwitcher, logManager)
{
protected override ReadOnlyChainProcessingEnv CreateChainProcessingEnv(OverridableWorldStateManager worldStateManager,
protected override ReadOnlyChainProcessingEnv CreateChainProcessingEnv(IOverridableWorldScope worldStateManager,
IBlockProcessor.IBlockTransactionsExecutor transactionsExecutor, IReadOnlyTxProcessingScope scope,
IRewardCalculator rewardCalculator)
{
Expand Down Expand Up @@ -202,7 +200,7 @@ protected override void RegisterDebugRpcModule(IRpcModuleProvider rpcModuleProvi
StepDependencyException.ThrowIfNull(_api.SpecProvider);

AuRaDebugModuleFactory debugModuleFactory = new(
_api.WorldStateManager.TrieStore,
_api.WorldStateManager,
_api.DbProvider,
_api.BlockTree,
_jsonRpcConfig,
Expand All @@ -223,7 +221,7 @@ protected override void RegisterDebugRpcModule(IRpcModuleProvider rpcModuleProvi
}

protected class AuRaDebugModuleFactory(
IReadOnlyTrieStore trieStore,
IWorldStateManager worldStateManager,
IDbProvider dbProvider,
IBlockTree blockTree,
IJsonRpcConfig jsonRpcConfig,
Expand All @@ -239,12 +237,12 @@ protected class AuRaDebugModuleFactory(
IFileSystem fileSystem,
ILogManager logManager,
IAuRaBlockProcessorFactory factory)
: DebugModuleFactory(trieStore, dbProvider, blockTree, jsonRpcConfig, blockValidator, recoveryStep,
: DebugModuleFactory(worldStateManager, dbProvider, blockTree, jsonRpcConfig, blockValidator, recoveryStep,
rewardCalculator, receiptStorage, receiptsMigration, configProvider, specProvider, syncModeSelector,
badBlockStore, fileSystem, logManager)
{
protected override ReadOnlyChainProcessingEnv CreateReadOnlyChainProcessingEnv(IReadOnlyTxProcessingScope scope,
OverridableWorldStateManager worldStateManager, BlockProcessor.BlockValidationTransactionsExecutor transactionsExecutor)
IOverridableWorldScope worldStateManager, BlockProcessor.BlockValidationTransactionsExecutor transactionsExecutor)
{
return new AuRaReadOnlyChainProcessingEnv(
scope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,24 @@ public class OverridableTxProcessingEnv : IOverridableTxProcessorSource
protected ILogManager LogManager { get; }

private readonly Lazy<ITransactionProcessor> _transactionProcessorLazy;
protected OverridableWorldState StateProvider { get; }
protected IOverridableWorldState StateProvider { get; }
protected OverridableCodeInfoRepository CodeInfoRepository { get; }
protected IVirtualMachine Machine { get; }
protected ITransactionProcessor TransactionProcessor => _transactionProcessorLazy.Value;

public OverridableTxProcessingEnv(
OverridableWorldStateManager worldStateManager,
IOverridableWorldScope overridableScope,
IReadOnlyBlockTree readOnlyBlockTree,
ISpecProvider specProvider,
ILogManager? logManager
)
{
SpecProvider = specProvider;
StateReader = worldStateManager.GlobalStateReader;
StateReader = overridableScope.GlobalStateReader;
BlockTree = readOnlyBlockTree;
IBlockhashProvider blockhashProvider = new BlockhashProvider(BlockTree, specProvider, StateProvider, logManager);
LogManager = logManager;

StateProvider = (OverridableWorldState)worldStateManager.CreateResettableWorldState();
StateProvider = overridableScope.WorldState;

CodeInfoRepository = new(new CodeInfoRepository());
Machine = new VirtualMachine(blockhashProvider, specProvider, CodeInfoRepository, logManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Nethermind.Consensus.Processing;
public class OverridableTxProcessingScope(
IOverridableCodeInfoRepository codeInfoRepository,
ITransactionProcessor transactionProcessor,
OverridableWorldState worldState,
IOverridableWorldState worldState,
Hash256 originalStateRoot
) : IOverridableTxProcessingScope
{
Expand Down
16 changes: 6 additions & 10 deletions src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class BlockchainBridgeTests
private IDbProvider _dbProvider;

private class TestReadOnlyTxProcessingEnv(
OverridableWorldStateManager worldStateManager,
IOverridableWorldScope worldStateManager,
IReadOnlyBlockTree blockTree,
ISpecProvider specProvider,
ILogManager logManager,
Expand All @@ -72,13 +72,12 @@ public async Task SetUp()
_ethereumEcdsa = Substitute.For<IEthereumEcdsa>();
_specProvider = MainnetSpecProvider.Instance;

IReadOnlyTrieStore trieStore = new TrieStore(_dbProvider.StateDb, LimboLogs.Instance).AsReadOnly();

OverridableWorldStateManager worldStateManager = new(_dbProvider, trieStore, LimboLogs.Instance);
WorldStateManager worldStateManager = WorldStateManager.CreateForTest(_dbProvider, LimboLogs.Instance);
IOverridableWorldScope overridableWorldScope = worldStateManager.CreateOverridableWorldScope();

IReadOnlyBlockTree readOnlyBlockTree = _blockTree.AsReadOnly();
TestReadOnlyTxProcessingEnv processingEnv = new(
worldStateManager,
overridableWorldScope,
readOnlyBlockTree,
_specProvider,
LimboLogs.Instance,
Expand Down Expand Up @@ -207,13 +206,10 @@ public void Call_uses_valid_beneficiary()
[TestCase(0)]
public void Bridge_head_is_correct(long headNumber)
{
IReadOnlyTrieStore trieStore = new TrieStore(_dbProvider.StateDb, LimboLogs.Instance).AsReadOnly();

OverridableWorldStateManager worldStateManager =
new(_dbProvider, trieStore, LimboLogs.Instance);
WorldStateManager worldStateManager = WorldStateManager.CreateForTest(_dbProvider, LimboLogs.Instance);
IReadOnlyBlockTree roBlockTree = _blockTree.AsReadOnly();
OverridableTxProcessingEnv processingEnv = new(
worldStateManager,
worldStateManager.CreateOverridableWorldScope(),
roBlockTree,
_specProvider,
LimboLogs.Instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Nethermind.Logging;
using Nethermind.State;
using Nethermind.State.Repositories;
using Nethermind.Trie.Pruning;

namespace Nethermind.Facade.Simulate;

Expand All @@ -25,9 +24,7 @@ public class SimulateReadOnlyBlocksProcessingEnvFactory(
public SimulateReadOnlyBlocksProcessingEnv Create(bool validate)
{
IReadOnlyDbProvider editableDbProvider = new ReadOnlyDbProvider(dbProvider, true);
OverlayTrieStore overlayTrieStore = new(editableDbProvider.StateDb, worldStateManager.TrieStore, logManager);
OverlayWorldStateManager overlayWorldStateManager = new(editableDbProvider, overlayTrieStore, logManager);
IWorldState worldState = overlayWorldStateManager.GlobalWorldState;
IWorldState worldState = worldStateManager.CreateOverlayWorldState(editableDbProvider.StateDb, editableDbProvider.CodeDb);
BlockTree tempBlockTree = CreateTempBlockTree(editableDbProvider, specProvider, logManager, editableDbProvider);

return new SimulateReadOnlyBlocksProcessingEnv(
Expand Down
5 changes: 2 additions & 3 deletions src/Nethermind/Nethermind.Init/Steps/RegisterRpcModules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ protected virtual void RegisterDebugRpcModule(IRpcModuleProvider rpcModuleProvid
StepDependencyException.ThrowIfNull(_api.SpecProvider);

DebugModuleFactory debugModuleFactory = new(
_api.WorldStateManager.TrieStore,
_api.WorldStateManager,
_api.DbProvider,
_api.BlockTree,
_jsonRpcConfig,
Expand Down Expand Up @@ -285,8 +285,7 @@ protected ModuleFactoryBase<ITraceRpcModule> CreateTraceModuleFactory()
StepDependencyException.ThrowIfNull(_api.SpecProvider);

return new TraceModuleFactory(
_api.WorldStateManager.TrieStore,
_api.DbProvider,
_api.WorldStateManager,
_api.BlockTree,
_jsonRpcConfig,
_api.BlockPreprocessor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public void GlobalSetup()
stateProvider.Commit(spec);
stateProvider.CommitTree(0);

OverridableWorldStateManager stateManager = new(dbProvider, trieStore.AsReadOnly(), LimboLogs.Instance);
WorldStateManager stateManager = new(stateProvider, trieStore, dbProvider, LimboLogs.Instance);
OverridableWorldStateManager overridableScope = new(dbProvider, trieStore.AsReadOnly(), LimboLogs.Instance);

StateReader stateReader = new(trieStore, codeDb, LimboLogs.Instance);

Expand Down Expand Up @@ -138,7 +139,7 @@ TransactionProcessor transactionProcessor

BlockchainBridge bridge = new(
new OverridableTxProcessingEnv(
stateManager,
overridableScope,
new ReadOnlyBlockTree(blockTree),
specProvider,
LimboLogs.Instance),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static async Task<Context> Create(ISpecProvider? specProvider = null, boo
IReceiptsMigration receiptsMigration = Substitute.For<IReceiptsMigration>();
ISyncModeSelector syncModeSelector = Substitute.For<ISyncModeSelector>();
var factory = new DebugModuleFactory(
blockchain.WorldStateManager.TrieStore,
blockchain.WorldStateManager,
blockchain.DbProvider,
blockchain.BlockTree,
blockchain.RpcConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class TestRpcBlockchain : TestBlockchain
public ITxSender TxSender { get; private set; } = null!;
public IReceiptFinder ReceiptFinder { get; private set; } = null!;
public IGasPriceOracle GasPriceOracle { get; private set; } = null!;
public OverridableWorldStateManager OverridableWorldStateManager { get; private set; } = null!;
public IOverridableWorldScope OverridableWorldStateManager { get; private set; } = null!;

public IKeyStore KeyStore { get; } = new MemKeyStore(TestItem.PrivateKeys, Path.Combine("testKeyStoreDir", Path.GetRandomFileName()));
public IWallet TestWallet { get; } =
Expand Down Expand Up @@ -150,9 +150,9 @@ protected override async Task<TestBlockchain> Build(
IFilterManager filterManager = new FilterManager(filterStore, BlockProcessor, TxPool, LimboLogs.Instance);
var dbProvider = new ReadOnlyDbProvider(DbProvider, false);
IReadOnlyBlockTree? roBlockTree = BlockTree!.AsReadOnly();
OverridableWorldStateManager overridableWorldStateManager = new(DbProvider, WorldStateManager.TrieStore, LogManager);
IOverridableWorldScope overridableWorldStateManager = WorldStateManager.CreateOverridableWorldScope();
OverridableTxProcessingEnv processingEnv = new(
overridableWorldStateManager,
WorldStateManager.CreateOverridableWorldScope(),
roBlockTree,
SpecProvider,
LimboLogs.Instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ public async Task Build(ISpecProvider? specProvider = null, bool isAura = false)
}

Factory = new(
Blockchain.OverridableWorldStateManager.TrieStore,
Blockchain.DbProvider,
Blockchain.WorldStateManager,
Blockchain.BlockTree,
JsonRpcConfig,
Blockchain.BlockPreprocessorStep,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace Nethermind.JsonRpc.Modules.DebugModule;

public class DebugModuleFactory : ModuleFactoryBase<IDebugRpcModule>
{
private readonly IReadOnlyTrieStore _trieStore;
private readonly IJsonRpcConfig _jsonRpcConfig;
private readonly IBlockValidator _blockValidator;
protected readonly IRewardCalculatorSource _rewardCalculatorSource;
Expand All @@ -39,9 +38,10 @@ public class DebugModuleFactory : ModuleFactoryBase<IDebugRpcModule>
private readonly ISyncModeSelector _syncModeSelector;
private readonly IBadBlockStore _badBlockStore;
private readonly IFileSystem _fileSystem;
private readonly IWorldStateManager _worldStateManager;

public DebugModuleFactory(
IReadOnlyTrieStore trieStore,
IWorldStateManager worldStateManager,
IDbProvider dbProvider,
IBlockTree blockTree,
IJsonRpcConfig jsonRpcConfig,
Expand All @@ -57,7 +57,7 @@ public DebugModuleFactory(
IFileSystem fileSystem,
ILogManager logManager)
{
_trieStore = trieStore;
_worldStateManager = worldStateManager;
_dbProvider = dbProvider.AsReadOnly(false);
_blockTree = blockTree.AsReadOnly();
_jsonRpcConfig = jsonRpcConfig ?? throw new ArgumentNullException(nameof(jsonRpcConfig));
Expand All @@ -76,7 +76,7 @@ public DebugModuleFactory(

public override IDebugRpcModule Create()
{
OverridableWorldStateManager worldStateManager = new(_dbProvider, _trieStore, _logManager);
IOverridableWorldScope worldStateManager = _worldStateManager.CreateOverridableWorldScope();
OverridableTxProcessingEnv txEnv = new(worldStateManager, _blockTree, _specProvider, _logManager);

IReadOnlyTxProcessingScope scope = txEnv.Build(Keccak.EmptyTreeHash);
Expand Down Expand Up @@ -111,7 +111,7 @@ public override IDebugRpcModule Create()
}

protected virtual ReadOnlyChainProcessingEnv CreateReadOnlyChainProcessingEnv(IReadOnlyTxProcessingScope scope,
OverridableWorldStateManager worldStateManager, BlockProcessor.BlockValidationTransactionsExecutor transactionsExecutor)
IOverridableWorldScope worldStateManager, BlockProcessor.BlockValidationTransactionsExecutor transactionsExecutor)
{
return new ReadOnlyChainProcessingEnv(
scope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@
using Nethermind.Consensus.Validators;
using Nethermind.Core.Crypto;
using Nethermind.Core.Specs;
using Nethermind.Db;
using Nethermind.Evm.TransactionProcessing;
using Nethermind.Logging;
using Nethermind.State;
using Nethermind.Trie.Pruning;

namespace Nethermind.JsonRpc.Modules.Trace;

public class TraceModuleFactory(
IReadOnlyTrieStore trieStore,
IDbProvider dbProvider,
IWorldStateManager worldStateManager,
IBlockTree blockTree,
IJsonRpcConfig jsonRpcConfig,
IBlockPreprocessorStep recoveryStep,
Expand All @@ -31,7 +28,6 @@ public class TraceModuleFactory(
IPoSSwitcher poSSwitcher,
ILogManager logManager) : ModuleFactoryBase<ITraceRpcModule>
{
protected readonly IReadOnlyTrieStore _trieStore = trieStore;
protected readonly IReadOnlyBlockTree _blockTree = blockTree.AsReadOnly();
protected readonly IJsonRpcConfig _jsonRpcConfig = jsonRpcConfig ?? throw new ArgumentNullException(nameof(jsonRpcConfig));
protected readonly IReceiptStorage _receiptStorage = receiptFinder ?? throw new ArgumentNullException(nameof(receiptFinder));
Expand All @@ -41,24 +37,24 @@ public class TraceModuleFactory(
protected readonly IRewardCalculatorSource _rewardCalculatorSource = rewardCalculatorSource ?? throw new ArgumentNullException(nameof(rewardCalculatorSource));
protected readonly IPoSSwitcher _poSSwitcher = poSSwitcher ?? throw new ArgumentNullException(nameof(poSSwitcher));

protected virtual OverridableTxProcessingEnv CreateTxProcessingEnv(OverridableWorldStateManager worldStateManager) => new(worldStateManager, _blockTree, _specProvider, _logManager);
protected virtual OverridableTxProcessingEnv CreateTxProcessingEnv(IOverridableWorldScope worldScope) => new(worldScope, _blockTree, _specProvider, _logManager);

protected virtual ReadOnlyChainProcessingEnv CreateChainProcessingEnv(OverridableWorldStateManager worldStateManager, IBlockProcessor.IBlockTransactionsExecutor transactionsExecutor, IReadOnlyTxProcessingScope scope, IRewardCalculator rewardCalculator) => new(
protected virtual ReadOnlyChainProcessingEnv CreateChainProcessingEnv(IOverridableWorldScope worldScope, IBlockProcessor.IBlockTransactionsExecutor transactionsExecutor, IReadOnlyTxProcessingScope scope, IRewardCalculator rewardCalculator) => new(
scope,
Always.Valid,
_recoveryStep,
rewardCalculator,
_receiptStorage,
_specProvider,
_blockTree,
worldStateManager.GlobalStateReader,
worldScope.GlobalStateReader,
_logManager,
transactionsExecutor);

public override ITraceRpcModule Create()
{
OverridableWorldStateManager worldStateManager = new(dbProvider, _trieStore, logManager);
OverridableTxProcessingEnv txProcessingEnv = CreateTxProcessingEnv(worldStateManager);
IOverridableWorldScope overridableScope = worldStateManager.CreateOverridableWorldScope();
OverridableTxProcessingEnv txProcessingEnv = CreateTxProcessingEnv(overridableScope);
IReadOnlyTxProcessingScope scope = txProcessingEnv.Build(Keccak.EmptyTreeHash);

IRewardCalculator rewardCalculator =
Expand All @@ -68,8 +64,8 @@ public override ITraceRpcModule Create()
RpcBlockTransactionsExecutor rpcBlockTransactionsExecutor = new(scope.TransactionProcessor, scope.WorldState);
BlockProcessor.BlockValidationTransactionsExecutor executeBlockTransactionsExecutor = new(scope.TransactionProcessor, scope.WorldState);

ReadOnlyChainProcessingEnv traceProcessingEnv = CreateChainProcessingEnv(worldStateManager, rpcBlockTransactionsExecutor, scope, rewardCalculator);
ReadOnlyChainProcessingEnv executeProcessingEnv = CreateChainProcessingEnv(worldStateManager, executeBlockTransactionsExecutor, scope, rewardCalculator);
ReadOnlyChainProcessingEnv traceProcessingEnv = CreateChainProcessingEnv(overridableScope, rpcBlockTransactionsExecutor, scope, rewardCalculator);
ReadOnlyChainProcessingEnv executeProcessingEnv = CreateChainProcessingEnv(overridableScope, executeBlockTransactionsExecutor, scope, rewardCalculator);

Tracer tracer = new(scope.WorldState, traceProcessingEnv.ChainProcessor, executeProcessingEnv.ChainProcessor,
traceOptions: ProcessingOptions.TraceTransactions);
Expand Down
Loading
Loading