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

Fix state override accessing state #8168

Merged
merged 6 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,12 @@ protected virtual ITransactionProcessor CreateTransactionProcessor() =>

IOverridableTxProcessingScope IOverridableTxProcessorSource.Build(Hash256 stateRoot) => Build(stateRoot);

public OverridableTxProcessingScope Build(Hash256 stateRoot)
{
Hash256 originalStateRoot = StateProvider.StateRoot;
StateProvider.StateRoot = stateRoot;
return new(CodeInfoRepository, TransactionProcessor, StateProvider, originalStateRoot);
}
public OverridableTxProcessingScope Build(Hash256 stateRoot) => new(CodeInfoRepository, TransactionProcessor, StateProvider, stateRoot);

IOverridableTxProcessingScope IOverridableTxProcessorSource.BuildAndOverride(BlockHeader header, Dictionary<Address, AccountOverride>? stateOverride)
{
OverridableTxProcessingScope scope = Build(header.StateRoot ?? throw new ArgumentException($"Block {header.Hash} state root is null", nameof(header)));
if (stateOverride != null)
if (stateOverride is not null)
{
scope.WorldState.ApplyStateOverrides(scope.CodeInfoRepository, stateOverride, SpecProvider.GetSpec(header), header.Number);
header.StateRoot = scope.WorldState.StateRoot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,34 @@

namespace Nethermind.Consensus.Processing;

public class OverridableTxProcessingScope(
IOverridableCodeInfoRepository codeInfoRepository,
ITransactionProcessor transactionProcessor,
IOverridableWorldState worldState,
Hash256 originalStateRoot
) : IOverridableTxProcessingScope
public class OverridableTxProcessingScope : IOverridableTxProcessingScope
{
public IOverridableCodeInfoRepository CodeInfoRepository => codeInfoRepository;
public ITransactionProcessor TransactionProcessor => transactionProcessor;
public IWorldState WorldState => worldState;
private readonly IOverridableCodeInfoRepository _codeInfoRepository;
private readonly ITransactionProcessor _transactionProcessor;
private readonly IOverridableWorldState _worldState;

public void Dispose()
public OverridableTxProcessingScope(IOverridableCodeInfoRepository codeInfoRepository,
ITransactionProcessor transactionProcessor,
IOverridableWorldState worldState,
Hash256 stateRoot)
{
worldState.StateRoot = originalStateRoot;
worldState.Reset();
worldState.ResetOverrides();
codeInfoRepository.ResetOverrides();
_codeInfoRepository = codeInfoRepository;
_transactionProcessor = transactionProcessor;
_worldState = worldState;
Reset();
alexb5dh marked this conversation as resolved.
Show resolved Hide resolved
_worldState.StateRoot = stateRoot;
}

public IOverridableCodeInfoRepository CodeInfoRepository => _codeInfoRepository;
public ITransactionProcessor TransactionProcessor => _transactionProcessor;
public IWorldState WorldState => _worldState;

public void Dispose() => Reset();

private void Reset()
{
_worldState.Reset();
_worldState.ResetOverrides();
_codeInfoRepository.ResetOverrides();
}
}
8 changes: 4 additions & 4 deletions src/Nethermind/Nethermind.Evm/StateOverridesExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public static void ApplyStateOverrides(
state.UpdateCode(overridableCodeInfoRepository, spec, accountOverride, address);
state.UpdateState(accountOverride, address);
}
}

state.Commit(spec);
state.CommitTree(blockNumber);
state.RecalculateStateRoot();
state.Commit(spec);
state.CommitTree(blockNumber);
state.RecalculateStateRoot();
}
}

private static void UpdateState(this IWorldState stateProvider, AccountOverride accountOverride, Address address)
Expand Down
Loading