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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public async Task mingas_empty_after_init()
}

[Test]
[Retry(3)]
public async Task whitelist_should_return_correctly()
{
using TxPermissionContractBlockchainWithBlocks chain = await TestContractBlockchain.ForTest<TxPermissionContractBlockchainWithBlocks, TxPriorityContractTests>();
Expand Down
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();
}
}
13 changes: 6 additions & 7 deletions src/Nethermind/Nethermind.Consensus/Tracing/GethStyleTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ public GethLikeTxTrace Trace(Hash256 blockHash, int txIndex, GethTraceOptions op

try
{
Dictionary<Address, AccountOverride>? stateOverride = options.StateOverrides;
using IOverridableTxProcessingScope? scope = _env.BuildAndOverride(block.Header, stateOverride);

return Trace(block, tx.Hash, cancellationToken, options, ProcessingOptions.TraceTransactions);
}
finally
Expand Down Expand Up @@ -125,6 +122,7 @@ public GethLikeTxTrace Trace(Hash256 blockHash, int txIndex, GethTraceOptions op
if (tx.Hash is null) throw new InvalidOperationException("Cannot trace transactions without tx hash set.");

block = block.WithReplacedBodyCloned(BlockBody.WithOneTransactionOnly(tx));
using IOverridableTxProcessingScope? scope = _env.BuildAndOverride(block.Header, options.StateOverrides);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think scope needs to be nullable here and in other places.

IBlockTracer<GethLikeTxTrace> blockTracer = CreateOptionsTracer(block.Header, options with { TxHash = tx.Hash });
try
{
Expand Down Expand Up @@ -178,10 +176,9 @@ public IEnumerable<string> TraceBadBlockToFile(Hash256 blockHash, GethTraceOptio
ArgumentNullException.ThrowIfNull(options);

var block = _badBlockStore
.GetAll()
.Where(b => b.Hash == blockHash)
.FirstOrDefault()
?? throw new InvalidOperationException($"No historical block found for {blockHash}");
.GetAll()
.FirstOrDefault(b => b.Hash == blockHash)
?? throw new InvalidOperationException($"No historical block found for {blockHash}");

var tracer = new GethLikeBlockFileTracer(block, options, _fileSystem);

Expand All @@ -195,6 +192,7 @@ public IEnumerable<string> TraceBadBlockToFile(Hash256 blockHash, GethTraceOptio
{
ArgumentNullException.ThrowIfNull(txHash);

using IOverridableTxProcessingScope? scope = _env.BuildAndOverride(block.Header, options.StateOverrides);
IBlockTracer<GethLikeTxTrace> tracer = CreateOptionsTracer(block.Header, options with { TxHash = txHash });

try
Expand Down Expand Up @@ -232,6 +230,7 @@ private IReadOnlyCollection<GethLikeTxTrace> TraceBlock(Block? block, GethTraceO
if (!_blockTree.IsMainChain(parent.Hash)) throw new InvalidOperationException("Cannot trace orphaned blocks");
}

using IOverridableTxProcessingScope? scope = _env.BuildAndOverride(block.Header, options.StateOverrides);
IBlockTracer<GethLikeTxTrace> tracer = CreateOptionsTracer(block.Header, options);
try
{
Expand Down
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Evm/StateOverridesExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static void ApplyStateOverrides(
state.Commit(spec);
state.CommitTree(blockNumber);
state.RecalculateStateRoot();

}

private static void UpdateState(this IWorldState stateProvider, AccountOverride accountOverride, Address address)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ public async Task When_empty_response_received_return_lesser_quality()
}

[Test]
[Parallelizable(ParallelScope.None)] // TODO: Investigate why it fails with parralelization
public async Task When_empty_response_received_with_no_peer_return_not_allocated()
{
DbContext dbContext = new(_logger, _logManager);
Expand Down