Skip to content

Commit

Permalink
Addressing comment
Browse files Browse the repository at this point in the history
  • Loading branch information
asdacap committed Dec 5, 2023
1 parent 3454619 commit 1f244a3
Show file tree
Hide file tree
Showing 15 changed files with 29 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ BlockProducerEnv Create()
BlockchainProcessor.Options.NoReceipts);

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

return new BlockProducerEnv()
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Consensus.Clique/CliquePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public Task<IBlockProducer> InitBlockProducer(IBlockProductionTrigger? blockProd
BlockchainProcessor.Options.NoReceipts);

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

ITxFilterPipeline txFilterPipeline =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -46,7 +46,7 @@ public Task StopAsync(bool processRemainingBlocks = false)
}
finally
{
_clearTemporaryState.Invoke();
_worldState.Reset();
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public ReadOnlyChainProcessingEnv(
IBlockPreprocessorStep recoveryStep,
IRewardCalculator rewardCalculator,
IReceiptStorage receiptStorage,
Action resetAction,
ISpecProvider specProvider,
ILogManager logManager,
IBlockProcessor.IBlockTransactionsExecutor? blockTransactionsExecutor = null)
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public override IDebugRpcModule Create()
_recoveryStep,
_rewardCalculatorSource.Get(txEnv.TransactionProcessor),
_receiptStorage,
txEnv.ResetDb,
_specProvider,
_logManager,
transactionsExecutor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public override ITraceRpcModule Create()
_recoveryStep,
rewardCalculator,
_receiptStorage,
txProcessingEnv.ResetDb,
_specProvider,
_logManager,
transactionsExecutor);
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Mev/Execution/TracerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,16 @@ 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);
tempWorldState.CreateAccount(Address.Zero, 0, 0);
tempWorldState.InsertCode(Address.Zero, code, MainnetSpecProvider.Instance.GenesisSpec);

stateReader.GetCode(codeHash).Should().NotBeNull();
reset();
tempWorldState.Reset();
stateReader.GetCode(codeHash).Should().BeNull();
}
}
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.State/IWorldStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
/// <returns></returns>
(IWorldState, IStateReader, Action) CreateResettableWorldState();
IWorldState CreateResettableWorldState();
event EventHandler<ReorgBoundaryReached>? ReorgBoundaryReached;
}
21 changes: 7 additions & 14 deletions src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ namespace Nethermind.State;
/// </summary>
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,
Expand All @@ -28,27 +30,18 @@ ILogManager logManager
_dbProvider = dbProvider;
_logManager = logManager;

IKeyValueStore codeDb = _dbProvider.AsReadOnly(false).GetDb<IDb>(DbNames.Code);
GlobalStateReader = new StateReader(_readOnlyTrieStore, codeDb, _logManager);
_readOnlyDbProvider = _dbProvider.AsReadOnly(false);
_codeDb = _readOnlyDbProvider.GetDb<IDb>(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<IDb>(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>? ReorgBoundaryReached
Expand Down
5 changes: 5 additions & 0 deletions src/Nethermind/Nethermind.State/StateProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1f244a3

Please sign in to comment.