Skip to content

Commit e92fdf8

Browse files
committed
Hacky verifytrie
1 parent ff9809e commit e92fdf8

17 files changed

+611
-98
lines changed

src/Nethermind/Nethermind.Db/FlatDbConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class FlatDbConfig: IFlatDbConfig
5151
public int CompactInterval { get; set; } = 4;
5252
public int MaxInFlightCompactJob { get; set; } = 32;
5353
public bool ReadWithTrie { get; set; } = false;
54-
public bool VerifyWithTrie { get; set; } = false;
54+
public bool VerifyWithTrie { get; set; } = true;
5555
public bool InlineCompaction { get; set; } = false;
5656

5757
// 1 GB is enough for 19% dirty load. Without it, then the diff layers on its own have around 35% dirty load.

src/Nethermind/Nethermind.State/Flat/FlatDiffRepository.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public FlatDiffRepository(
104104
private RefCountingPersistenceReader? _cachedReader = null;
105105
private readonly TrieNodeCache _trieNodeCache;
106106

107-
private RefCountingPersistenceReader LeaseReader()
107+
public IPersistence.IPersistenceReader LeaseReader()
108108
{
109109
using var _ = _readerCacheLock.EnterScope();
110110
var cachedReader = _cachedReader;
@@ -798,4 +798,33 @@ public bool HasStateForBlock(StateId stateId)
798798
if (_currentPersistedState == stateId) return true;
799799
return false;
800800
}
801+
802+
public StateId? FindStateIdForStateRoot(Hash256 stateRoot)
803+
{
804+
using (EnterRepolockReadOnly())
805+
{
806+
foreach (var stateId in _inMemorySnapshotStore.GetKeysBetween(new StateId(0, Hash256.Zero), new StateId(long.MaxValue, Keccak.MaxValue)))
807+
{
808+
if (stateId.stateRoot == stateRoot) return stateId;
809+
}
810+
811+
if (_currentPersistedState.stateRoot == stateRoot)
812+
{
813+
return _currentPersistedState;
814+
}
815+
}
816+
817+
return null;
818+
}
819+
820+
public StateId? FindLatestAvailableState()
821+
{
822+
using (EnterRepolockReadOnly())
823+
{
824+
StateId? lastInMemory = _inMemorySnapshotStore.GetLast();
825+
if (lastInMemory != null) return lastInMemory;
826+
827+
return _currentPersistedState;
828+
}
829+
}
801830
}

src/Nethermind/Nethermind.State/Flat/FlatStateReader.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88
using Nethermind.Core.Utils;
99
using Nethermind.Db;
1010
using Nethermind.Int256;
11+
using Nethermind.Logging;
12+
using Nethermind.State.Flat.ScopeProvider;
1113
using Nethermind.Trie;
1214

1315
namespace Nethermind.State.Flat;
1416

1517
public class FlatStateReader(
1618
[KeyFilter(DbNames.Code)] IDb codeDb,
1719
ReadonlyReaderRepository readonlyReaderRepositor,
18-
IFlatDiffRepository flatDiffRepository
20+
IFlatDiffRepository flatDiffRepository,
21+
ILogManager logManager
1922
): IStateReader
2023
{
2124
public bool TryGetAccount(BlockHeader? baseBlock, Address address, out AccountStruct account)
@@ -62,7 +65,26 @@ public ReadOnlySpan<byte> GetStorage(BlockHeader? baseBlock, Address address, in
6265

6366
public void RunTreeVisitor<TCtx>(ITreeVisitor<TCtx> treeVisitor, Hash256 stateRoot, VisitingOptions? visitingOptions = null) where TCtx : struct, INodeContext<TCtx>
6467
{
65-
throw new NotImplementedException();
68+
StateId? stateId = flatDiffRepository.FindStateIdForStateRoot(stateRoot);
69+
if (stateId is null)
70+
{
71+
throw new InvalidOperationException($"State root {stateRoot} not found");
72+
}
73+
74+
using RefCountingDisposableBox<SnapshotBundle> readerBox = readonlyReaderRepositor.GatherReadOnlyReaderAtBaseBlock(stateId.Value);
75+
if (readerBox is null)
76+
{
77+
throw new InvalidOperationException($"State root {stateRoot} not found");
78+
}
79+
80+
SnapshotBundle reader = readerBox.Item;
81+
StateTrieStoreAdapter trieStoreAdapter = new StateTrieStoreAdapter(
82+
reader,
83+
new ConcurrencyQuota(),
84+
false);
85+
86+
PatriciaTree patriciaTree = new PatriciaTree(trieStoreAdapter, logManager);
87+
patriciaTree.Accept(treeVisitor, stateRoot, visitingOptions);
6688
}
6789

6890
public bool HasStateForBlock(BlockHeader? baseBlock)

0 commit comments

Comments
 (0)