Skip to content

Commit b5731c0

Browse files
committed
Code cleanup
1 parent 59fcecb commit b5731c0

15 files changed

+193
-327
lines changed

src/Nethermind/Nethermind.Init/Modules/FlatWorldStateModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected override void Load(ContainerBuilder builder)
3434
.AddColumnDatabase<FlatDbColumns>(DbNames.Flat)
3535
.AddSingleton<IPersistence, RocksdbPersistence>()
3636
// .AddSingleton<IPersistence, TrieOnlyRocksdbPersistence>()
37-
.AddSingleton<TrieStoreTrieCacheWarmer>()
37+
.AddSingleton<TrieWarmer>()
3838

3939
// These fake db are workaround for missing metrics with column db. Probably not a good idea though as
4040
// a failure in writes in one of the DB will break the db.

src/Nethermind/Nethermind.State/Flat/CachedResource.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@
22
// SPDX-License-Identifier: LGPL-3.0-only
33

44
using System.Collections.Concurrent;
5+
using Nethermind.Core;
56
using Nethermind.Core.Crypto;
7+
using Nethermind.Int256;
68
using Nethermind.Trie;
79

810
namespace Nethermind.State.Flat;
911

1012
public record CachedResource(
11-
ConcurrentDictionary<TreePath, TrieNode> LoadedStateNodes,
12-
ConcurrentDictionary<(Hash256AsKey, TreePath), TrieNode> LoadedStorageNodes
13+
ConcurrentDictionary<TreePath, TrieNode> TrieWarmerLoadedNodes,
14+
ConcurrentDictionary<(Hash256AsKey, TreePath), TrieNode> LoadedStorageNodes,
15+
ConcurrentDictionary<(AddressAsKey, UInt256?), bool> PrewarmedAddresses
1316
)
1417
{
1518
public void Clear()
1619
{
17-
LoadedStateNodes.Clear();
20+
TrieWarmerLoadedNodes.Clear();
1821
LoadedStorageNodes.Clear();
22+
PrewarmedAddresses.Clear();
1923
}
2024
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ private SnapshotBundle GatherCache(StateId baseBlock, long? earliestExclusive =
348348

349349
StateId bigCacheState = _currentPersistedState;
350350

351+
// TODO: Determine if using a linked list of snapshot make more sense. Measure the impact of this loop and the
352+
// dispose loop.
351353
string exitReason = "";
352354
StateId current = baseBlock;
353355
while(TryLeaseCompactedState(current, out var entry) || TryLeaseState(current, out entry))
@@ -386,6 +388,7 @@ private SnapshotBundle GatherCache(StateId baseBlock, long? earliestExclusive =
386388
bigCacheReader = new NoopPersistenceReader();
387389
}
388390

391+
// TODO: Measure this
389392
knownStates.Reverse();
390393

391394
if (_logger.IsTrace) _logger.Trace($"Gathered {baseBlock}. Earliest is {earliestExclusive}, Got {knownStates.Count} known states, {_currentPersistedState}");
@@ -682,7 +685,7 @@ public void Add(Snapshot snapshot)
682685
if (toSelfDestructStorage.Value)
683686
{
684687
int deleted = batch.SelfDestruct(toSelfDestructStorage.Key.Value);
685-
if (toSelfDestructStorage.Key.Value == WorldStateScope.DebugAddress)
688+
if (toSelfDestructStorage.Key.Value == FlatWorldStateScope.DebugAddress)
686689
{
687690
Console.Error.WriteLine($"Selfdestruct should skip {toSelfDestructStorage.Key}");
688691
}
@@ -695,10 +698,10 @@ public void Add(Snapshot snapshot)
695698
}
696699

697700
int num = batch.SelfDestruct(toSelfDestructStorage.Key.Value);
698-
if (toSelfDestructStorage.Key.Value == WorldStateScope.DebugAddress)
701+
if (toSelfDestructStorage.Key.Value == FlatWorldStateScope.DebugAddress)
699702
{
700703
using var r = LeaseReader();
701-
bool _ = r.TryGetSlot(WorldStateScope.DebugAddress, WorldStateScope.DebugSlot, out var value);
704+
bool _ = r.TryGetSlot(FlatWorldStateScope.DebugAddress, FlatWorldStateScope.DebugSlot, out var value);
702705
Console.Error.WriteLine($"Selfdestructed {toSelfDestructStorage.Key} {num}, {value?.ToHexString()}");
703706
}
704707
counter++;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public ReadOnlySpan<byte> GetStorage(BlockHeader? baseBlock, Address address, in
5151
}
5252

5353
SnapshotBundle reader = readerBox.Item;
54-
if (reader.TryGetSlot(address, index, reader.DetermineSelfDestructStateIdx(address), out byte[] value))
54+
if (reader.TryGetSlot(address, index, reader.DetermineSelfDestructSnapshotIdx(address), out byte[] value))
5555
{
5656
return value;
5757
}

src/Nethermind/Nethermind.State/Flat/Persistence/RocksdbPersistence.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ public bool TryGetAccount(Address address, out Account? acc)
495495
Span<byte> value = _state.GetSpan(_mainDb.EncodeAccountKey(stackalloc byte[StateKeyPrefixLength], address));
496496
try
497497
{
498-
if (address == WorldStateScope.DebugAddress)
498+
if (address == FlatWorldStateScope.DebugAddress)
499499
{
500500
Console.Error.WriteLine($"Get {address}, got {value.ToHexString()}");
501501
}

src/Nethermind/Nethermind.State/Flat/ScopeProvider/FlatScopeProvider.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public class FlatScopeProvider : IWorldStateScopeProvider
1616
private readonly TrieStoreScopeProvider.KeyValueWithBatchingBackedCodeDb _codeDb;
1717
private readonly bool _isReadOnly;
1818
private readonly FlatDiffRepository.Configuration _configuration;
19-
private readonly ITrieStoreTrieCacheWarmer _trieWarmer;
19+
private readonly ITrieWarmer _trieWarmer;
2020

2121
public FlatScopeProvider(
2222
[KeyFilter(DbNames.Code)] IDb codeDb,
2323
IFlatDiffRepository flatDiffRepository,
2424
FlatDiffRepository.Configuration configuration,
25-
ITrieStoreTrieCacheWarmer trieWarmer,
25+
ITrieWarmer trieWarmer,
2626
ILogManager logManager,
2727
bool isReadOnly = false)
2828
{
@@ -43,15 +43,15 @@ public IWorldStateScopeProvider.IScope BeginScope(BlockHeader? baseBlock)
4343
{
4444
StateId currentState = new StateId(baseBlock);
4545
SnapshotBundle snapshotBundle = _flatDiffRepository.GatherReaderAtBaseBlock(currentState);
46-
if (_trieWarmer is NoopTrieStoreTrieCacheWarmer) snapshotBundle.SetPrewarmer();
46+
if (_trieWarmer is NoopTrieWarmer) snapshotBundle.SetPrewarmer();
4747

48-
ITrieStoreTrieCacheWarmer warmer = _trieWarmer;
48+
ITrieWarmer warmer = _trieWarmer;
4949
if (_configuration.DisableTrieWarmer)
5050
{
51-
warmer = new NoopTrieStoreTrieCacheWarmer();
51+
warmer = new NoopTrieWarmer();
5252
}
5353

54-
return new WorldStateScope(
54+
return new FlatWorldStateScope(
5555
currentState,
5656
snapshotBundle,
5757
_codeDb,

src/Nethermind/Nethermind.State/Flat/ScopeProvider/StorageTree.cs renamed to src/Nethermind/Nethermind.State/Flat/ScopeProvider/FlatStorageTree.cs

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,32 @@
55
using Nethermind.Core;
66
using Nethermind.Core.Crypto;
77
using Nethermind.Core.Extensions;
8-
using Nethermind.Evm;
98
using Nethermind.Evm.State;
109
using Nethermind.Int256;
1110
using Nethermind.Logging;
12-
using Nethermind.Trie;
13-
using Nethermind.Trie.Pruning;
14-
using NonBlocking;
1511

1612
namespace Nethermind.State.Flat.ScopeProvider;
1713

18-
public class StorageTree : IWorldStateScopeProvider.IStorageTree
14+
public class FlatStorageTree : IWorldStateScopeProvider.IStorageTree
1915
{
20-
private readonly State.StorageTree _tree;
21-
private readonly State.StorageTree _warmupStorageTree;
16+
private readonly StorageTree _tree;
17+
private readonly StorageTree _warmupStorageTree;
2218
private readonly Address _address;
2319
private readonly FlatDiffRepository.Configuration _config;
24-
private readonly ITrieStoreTrieCacheWarmer _trieCacheWarmer;
25-
private readonly WorldStateScope _scope;
20+
private readonly ITrieWarmer _trieCacheWarmer;
21+
private readonly FlatWorldStateScope _scope;
2622
private readonly SnapshotBundle _bundle;
2723
private readonly Hash256 _addressHash;
28-
private int _selfDestructKnownStateIdx;
2924
private readonly StorageTrieStoreAdapter _storageTrieAdapter;
3025
private readonly StorageTrieStoreAdapter _warmerStorageTrieAdapter;
3126

32-
public StorageTree(
33-
WorldStateScope scope,
34-
ITrieStoreTrieCacheWarmer trieCacheWarmer,
27+
// This number is the idx of the snapshot in the SnapshotBundle where a clear for this account was found.
28+
// This is passed to TryGetSlot which prevent it from reading before self destruct.
29+
private int _selfDestructKnownStateIdx;
30+
31+
public FlatStorageTree(
32+
FlatWorldStateScope scope,
33+
ITrieWarmer trieCacheWarmer,
3534
SnapshotBundle bundle,
3635
FlatDiffRepository.Configuration config,
3736
ConcurrencyQuota concurrencyQuota,
@@ -44,22 +43,23 @@ public StorageTree(
4443
_bundle = bundle;
4544
_address = address;
4645
_addressHash = address.ToAccountPath.ToHash256();
47-
_selfDestructKnownStateIdx = bundle.DetermineSelfDestructStateIdx(address);
46+
_selfDestructKnownStateIdx = bundle.DetermineSelfDestructSnapshotIdx(address);
4847

4948
_storageTrieAdapter = new StorageTrieStoreAdapter(bundle, concurrencyQuota, _addressHash, _selfDestructKnownStateIdx,
5049
isTrieWarmer: false);
5150
_warmerStorageTrieAdapter = new StorageTrieStoreAdapter(bundle, concurrencyQuota, _addressHash, _selfDestructKnownStateIdx,
5251
isTrieWarmer: true);
5352

54-
_tree = new State.StorageTree(_storageTrieAdapter, storageRoot, logManager);
53+
_tree = new StorageTree(_storageTrieAdapter, storageRoot, logManager);
5554
_tree.RootHash = storageRoot;
5655

57-
_warmupStorageTree = new State.StorageTree(_warmerStorageTrieAdapter, logManager);
56+
_warmupStorageTree = new StorageTree(_warmerStorageTrieAdapter, logManager);
5857
_warmupStorageTree.RootHash = storageRoot;
5958

6059
_config = config;
6160

6261
// In case its all write.
62+
// TODO: Check hint set is working or not.
6363
_trieCacheWarmer.PushJob(_scope, null, this, 0, _scope.HintSequenceId);
6464
}
6565

@@ -92,6 +92,11 @@ public byte[] Get(in UInt256 index)
9292

9393
public void HintGet(in UInt256 index, byte[]? value)
9494
{
95+
// Note: VERY hot code.
96+
// 90% of the read goes through prewarmer, not actually go through this class, meaning this method is called
97+
// a lot. Unlike with account, setting the setted slot have a measurable net negative impact on performance.
98+
// Trying to set this value async through trie warmer proved to be hard to pull of and result in random invalid
99+
// block.
95100
WarmUpSlot(index);
96101
}
97102

@@ -105,53 +110,42 @@ private void WarmUpSlot(UInt256 index)
105110
_trieCacheWarmer.PushJob(_scope, null, this, index, _scope.HintSequenceId);
106111
}
107112

113+
// Called by trie warmer.
108114
public bool WarUpStorageTrie(UInt256 index, int sequenceId)
109115
{
110-
if (HintSequenceId != sequenceId) return false;
116+
if (_scope.HintSequenceId != sequenceId) return false;
111117

112-
if (ShouldPrewarm(index))
118+
if (_bundle.ShouldPrewarm(_address, index))
113119
{
114120
// Note: storage tree root not changed after write batch. Also not cleared. So the result is not correct.
121+
// this is just to warm up the nodes.
115122
_ = _warmupStorageTree.Get(index);
116-
MaybePreReadSlot(index, sequenceId);
117123
}
118124

119125
return true;
120126
}
121127

122-
public int HintSequenceId => _bundle.HintSequenceId;
123-
124-
public bool TryGet(in UInt256 index, out byte[]? value)
128+
private bool TryGet(in UInt256 index, out byte[]? value)
125129
{
126130
return _bundle.TryGetSlot(_address, index, _selfDestructKnownStateIdx, out value);
127131
}
128132

129-
public bool ShouldPrewarm(UInt256 index)
130-
{
131-
return _bundle.ShouldPrewarm(_address, index);
132-
}
133-
134-
public void MaybePreReadSlot(UInt256 slot, int sequenceId)
135-
{
136-
_bundle.MaybePreReadSlot(_address, slot, sequenceId);
137-
}
138-
139133
public byte[] Get(in ValueHash256 hash)
140134
{
141135
throw new Exception("Not supported");
142136
}
143137

144-
public void Set(UInt256 slot, byte[] value)
138+
private void Set(UInt256 slot, byte[] value)
145139
{
146-
if (_address == WorldStateScope.DebugAddress && slot == WorldStateScope.DebugSlot) Console.Error.WriteLine($"set {_address} {slot}, {value?.ToHexString()}");
140+
if (_address == FlatWorldStateScope.DebugAddress && slot == FlatWorldStateScope.DebugSlot) Console.Error.WriteLine($"set {_address} {slot}, {value?.ToHexString()}");
147141
_bundle.SetChangedSlot(_address, slot, value);
148142
}
149143

150-
public void SelfDestruct()
144+
private void SelfDestruct()
151145
{
152-
if (_address == WorldStateScope.DebugAddress) Console.Error.WriteLine($"Self destruct {_address}");
146+
if (_address == FlatWorldStateScope.DebugAddress) Console.Error.WriteLine($"Self destruct {_address}");
153147
_bundle.Clear(_address, _addressHash);
154-
_selfDestructKnownStateIdx = _bundle.GetSelfDestructKnownStateId();
148+
_selfDestructKnownStateIdx = _bundle.DetermineSelfDestructSnapshotIdx(_address);
155149

156150
// Technically, they wont actually matter as the trie will traverse the existing path anyway and on self destruct
157151
// it will just get blocked on root, so this is more of an optimization.
@@ -181,7 +175,7 @@ public IWorldStateScopeProvider.IStorageWriteBatch CreateWriteBatch(int estimate
181175

182176
private class StorageTreeBulkWriteBatch(
183177
TrieStoreScopeProvider.StorageTreeBulkWriteBatch storageTreeBulkWriteBatch,
184-
StorageTree storageTree) : IWorldStateScopeProvider.IStorageWriteBatch
178+
FlatStorageTree storageTree) : IWorldStateScopeProvider.IStorageWriteBatch
185179
{
186180
public void Set(in UInt256 index, byte[] value)
187181
{

src/Nethermind/Nethermind.State/Flat/ScopeProvider/FlatWorldStateManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public FlatWorldStateManager(
3030
IFlatDiffRepository flatDiffRepository,
3131
FlatDiffRepository.Configuration configuration,
3232
FlatStateReader flatStateReader,
33-
TrieStoreTrieCacheWarmer trieWarmer,
33+
TrieWarmer trieWarmer,
3434
IProcessExitSource exitSource,
3535
[KeyFilter(DbNames.Code)] IDb codeDb,
3636
ILogManager logManager
@@ -51,7 +51,7 @@ ILogManager logManager
5151
public IReadOnlyKeyValueStore? HashServer => null;
5252
public IWorldStateScopeProvider CreateResettableWorldState()
5353
{
54-
return new FlatScopeProvider(_codeDb, _flatDiffRepository, _configuration, new NoopTrieStoreTrieCacheWarmer(), _logManager, isReadOnly: true);
54+
return new FlatScopeProvider(_codeDb, _flatDiffRepository, _configuration, new NoopTrieWarmer(), _logManager, isReadOnly: true);
5555
}
5656

5757
event EventHandler<ReorgBoundaryReached>? IWorldStateManager.ReorgBoundaryReached
@@ -66,7 +66,7 @@ public IOverridableWorldScope CreateOverridableWorldScope()
6666
_codeDb,
6767
_flatDiffRepository,
6868
_configuration,
69-
new NoopTrieStoreTrieCacheWarmer(),
69+
new NoopTrieWarmer(),
7070
_logManager,
7171
isReadOnly: true);
7272
return new FakeOverridableWorldScope(scopeProvider, _flatStateReader);

0 commit comments

Comments
 (0)