Skip to content

Commit

Permalink
Log missing node (#7705)
Browse files Browse the repository at this point in the history
  • Loading branch information
asdacap authored Nov 4, 2024
1 parent 1318d52 commit a00c8f5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/Nethermind/Nethermind.State/StateProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ public void Accept(ITreeVisitor? visitor, Hash256? stateRoot, VisitingOptions? v
_tree.Accept(visitor, stateRoot, visitingOptions);
}

public void Accept<TCtx>(ITreeVisitor<TCtx>? visitor, Hash256? stateRoot, VisitingOptions? visitingOptions = null) where TCtx : struct, INodeContext<TCtx>
{
ArgumentNullException.ThrowIfNull(visitor);
ArgumentNullException.ThrowIfNull(stateRoot);

_tree.Accept(visitor, stateRoot, visitingOptions);
}

private bool _needsStateRootUpdate;

public void RecalculateStateRoot()
Expand Down
6 changes: 6 additions & 0 deletions src/Nethermind/Nethermind.State/WorldState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ public void Accept(ITreeVisitor visitor, Hash256 stateRoot, VisitingOptions? vis
{
_stateProvider.Accept(visitor, stateRoot, visitingOptions);
}

public void Accept<TContext>(ITreeVisitor<TContext> visitor, Hash256 stateRoot, VisitingOptions? visitingOptions = null) where TContext : struct, INodeContext<TContext>
{
_stateProvider.Accept(visitor, stateRoot, visitingOptions);
}

public bool AccountExists(Address address)
{
return _stateProvider.AccountExists(address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void TestOnTreeSyncFinish_CallVisit()

stateReader
.Received()
.RunTreeVisitor(Arg.Any<ITreeVisitor>(), Arg.Is(TestItem.KeccakA), Arg.Any<VisitingOptions>());
.RunTreeVisitor(Arg.Any<TrieStatsCollector>(), Arg.Is(TestItem.KeccakA), Arg.Any<VisitingOptions>());
}

[Test]
Expand All @@ -69,7 +69,7 @@ public async Task TestOnTreeSyncFinish_BlockProcessingQueue_UntilFinished()
ManualResetEvent treeVisitorBlocker = new ManualResetEvent(false);

stateReader
.When(sr => sr.RunTreeVisitor(Arg.Any<ITreeVisitor>(), Arg.Is(TestItem.KeccakA), Arg.Any<VisitingOptions>()))
.When(sr => sr.RunTreeVisitor(Arg.Any<TrieStatsCollector>(), Arg.Is(TestItem.KeccakA), Arg.Any<VisitingOptions>()))
.Do((ci) =>
{
treeVisitorBlocker.WaitOne();
Expand Down
22 changes: 13 additions & 9 deletions src/Nethermind/Nethermind.Trie/TrieStatsCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Nethermind.Trie
{
public class TrieStatsCollector : ITreeVisitor
public class TrieStatsCollector : ITreeVisitor<TreePathContextWithStorage>
{
private readonly ClockCache<ValueHash256, int> _existingCodeHash = new ClockCache<ValueHash256, int>(1024 * 8);
private readonly IKeyValueStore _codeKeyValueStore;
Expand All @@ -29,29 +29,32 @@ public TrieStatsCollector(IKeyValueStore codeKeyValueStore, ILogManager logManag
public TrieStats Stats { get; } = new();

public bool IsFullDbScan => true;
public void VisitTree(in TreePathContextWithStorage nodeContext, Hash256 rootHash, TrieVisitContext trieVisitContext)
{
}

public bool ShouldVisit(Hash256 nextNode)
public bool ShouldVisit(in TreePathContextWithStorage nodeContext, Hash256 nextNode)
{
return true;
}

public void VisitTree(Hash256 rootHash, TrieVisitContext trieVisitContext) { }

public void VisitMissingNode(Hash256 nodeHash, TrieVisitContext trieVisitContext)
public void VisitMissingNode(in TreePathContextWithStorage nodeContext, Hash256 nodeHash, TrieVisitContext trieVisitContext)
{
if (trieVisitContext.IsStorage)
{
if (_logger.IsWarn) _logger.Warn($"Missing node. Storage: {nodeContext.Storage} Path: {nodeContext.Path} Hash: {nodeHash}");
Interlocked.Increment(ref Stats._missingStorage);
}
else
{
if (_logger.IsWarn) _logger.Warn($"Missing node. Path: {nodeContext.Path} Hash: {nodeHash}");
Interlocked.Increment(ref Stats._missingState);
}

IncrementLevel(trieVisitContext);
}

public void VisitBranch(TrieNode node, TrieVisitContext trieVisitContext)
public void VisitBranch(in TreePathContextWithStorage nodeContext, TrieNode node, TrieVisitContext trieVisitContext)
{
_cancellationToken.ThrowIfCancellationRequested();

Expand All @@ -69,7 +72,7 @@ public void VisitBranch(TrieNode node, TrieVisitContext trieVisitContext)
IncrementLevel(trieVisitContext);
}

public void VisitExtension(TrieNode node, TrieVisitContext trieVisitContext)
public void VisitExtension(in TreePathContextWithStorage nodeContext, TrieNode node, TrieVisitContext trieVisitContext)
{
if (trieVisitContext.IsStorage)
{
Expand All @@ -85,7 +88,7 @@ public void VisitExtension(TrieNode node, TrieVisitContext trieVisitContext)
IncrementLevel(trieVisitContext);
}

public void VisitLeaf(TrieNode node, TrieVisitContext trieVisitContext, ReadOnlySpan<byte> value)
public void VisitLeaf(in TreePathContextWithStorage nodeContext, TrieNode node, TrieVisitContext trieVisitContext, ReadOnlySpan<byte> value)
{
long lastAccountNodeCount = _lastAccountNodeCount;
long currentNodeCount = Stats.NodesCount;
Expand All @@ -108,7 +111,7 @@ public void VisitLeaf(TrieNode node, TrieVisitContext trieVisitContext, ReadOnly
IncrementLevel(trieVisitContext);
}

public void VisitCode(Hash256 codeHash, TrieVisitContext trieVisitContext)
public void VisitCode(in TreePathContextWithStorage nodeContext, Hash256 codeHash, TrieVisitContext trieVisitContext)
{
ValueHash256 key = new ValueHash256(codeHash.Bytes);
bool codeExist = _existingCodeHash.TryGet(key, out int codeLength);
Expand All @@ -130,6 +133,7 @@ public void VisitCode(Hash256 codeHash, TrieVisitContext trieVisitContext)
}
else
{
if (_logger.IsWarn) _logger.Warn($"Missing code. Hash: {codeHash}");
Interlocked.Increment(ref Stats._missingCode);
}

Expand Down

0 comments on commit a00c8f5

Please sign in to comment.