Skip to content

Commit 59fcecb

Browse files
committed
Debugging
1 parent a70d057 commit 59fcecb

File tree

5 files changed

+60
-15
lines changed

5 files changed

+60
-15
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Nethermind.Int256;
1717
using Nethermind.Logging;
1818
using Nethermind.State.Flat.Persistence;
19+
using Nethermind.State.Flat.ScopeProvider;
1920
using Nethermind.Trie;
2021
using Nethermind.Trie.Pruning;
2122
using Prometheus;
@@ -681,6 +682,10 @@ public void Add(Snapshot snapshot)
681682
if (toSelfDestructStorage.Value)
682683
{
683684
int deleted = batch.SelfDestruct(toSelfDestructStorage.Key.Value);
685+
if (toSelfDestructStorage.Key.Value == WorldStateScope.DebugAddress)
686+
{
687+
Console.Error.WriteLine($"Selfdestruct should skip {toSelfDestructStorage.Key}");
688+
}
684689
if (deleted > 0)
685690
{
686691
_logger.Warn($"Should selfdestruct {toSelfDestructStorage.Key}. Deleted {deleted}");
@@ -689,7 +694,13 @@ public void Add(Snapshot snapshot)
689694
continue;
690695
}
691696

692-
batch.SelfDestruct(toSelfDestructStorage.Key.Value);
697+
int num = batch.SelfDestruct(toSelfDestructStorage.Key.Value);
698+
if (toSelfDestructStorage.Key.Value == WorldStateScope.DebugAddress)
699+
{
700+
using var r = LeaseReader();
701+
bool _ = r.TryGetSlot(WorldStateScope.DebugAddress, WorldStateScope.DebugSlot, out var value);
702+
Console.Error.WriteLine($"Selfdestructed {toSelfDestructStorage.Key} {num}, {value?.ToHexString()}");
703+
}
693704
counter++;
694705
}
695706
_flatdiffimes.WithLabels("persistence", "self_destruct").Observe(Stopwatch.GetTimestamp() - sw);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,14 @@ private ReadOnlySpan<byte> EncodeAccountKey(Span<byte> buffer, in Address addr)
202202
if (_configuration.UsePreimage)
203203
{
204204
addr.Bytes.CopyTo(buffer);
205-
return buffer[..StorageHashPrefixLength];
205+
return buffer[..StateKeyPrefixLength];
206206
}
207207
else
208208
{
209209
ValueHash256 hashBuffer = ValueKeccak.Zero;
210210
hashBuffer = addr.ToAccountPath;
211211
hashBuffer.Bytes[..StorageHashPrefixLength].CopyTo(buffer);
212-
return buffer[..StorageHashPrefixLength];
212+
return buffer[..StateKeyPrefixLength];
213213
}
214214
}
215215

@@ -322,7 +322,7 @@ public void Dispose()
322322
public int SelfDestruct(Address addr)
323323
{
324324
ValueHash256 accountPath = addr.ToAccountPath;
325-
Span<byte> firstKey = stackalloc byte[StorageNodesKeyLength];
325+
Span<byte> firstKey = stackalloc byte[StorageHashPrefixLength]; // Because slot 0 is a thing, its just the address prefix.
326326
Span<byte> lastKey = stackalloc byte[StorageNodesKeyLength];
327327
firstKey.Fill(0x00);
328328
lastKey.Fill(0xff);
@@ -350,6 +350,7 @@ public int SelfDestruct(Address addr)
350350
}
351351
}
352352

353+
removedEntry = 0; // Debug
353354
// for storage the prefix might change depending on the encoding
354355
firstKey.Fill(0x00);
355356
lastKey.Fill(0xff);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public byte[] Get(in UInt256 index)
7575
var treeValue = _tree.Get(index);
7676
if (!Bytes.AreEqual(treeValue, value))
7777
{
78-
throw new Exception($"Get slot got wrong value. Address {_address}, {_tree.RootHash}, {index}. Tree: {treeValue?.ToHexString()} vs Flat: {value?.ToHexString()}");
78+
throw new Exception($"Get slot got wrong value. Address {_address}, {_tree.RootHash}, {index}. Tree: {treeValue?.ToHexString()} vs Flat: {value?.ToHexString()}. Self destruct it {_selfDestructKnownStateIdx}");
7979
}
8080
}
8181

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Nethermind.Core;
1313
using Nethermind.Core.Collections;
1414
using Nethermind.Core.Crypto;
15+
using Nethermind.Core.Extensions;
1516
using Nethermind.Evm.State;
1617
using Nethermind.Int256;
1718
using Nethermind.Logging;
@@ -21,11 +22,9 @@ namespace Nethermind.State.Flat.ScopeProvider;
2122

2223
public class WorldStateScope : IWorldStateScopeProvider.IScope
2324
{
24-
// internal static Address DebugAddress = new Address("0xf664829682daf488be5318dae198a69ce27fb31b");
25-
// internal static Address DebugAddress = new Address("0xc5f60fa4613493931b605b6da1e9febbdeb61e16");
26-
// internal static Address DebugAddress = new Address("0xc7d3e431be6222543364408a94c12c0d089be305");
27-
internal static Address DebugAddress = new Address("0x8d8dfbd04db0942d79bb1bb038e8876bb67ff825");
28-
internal static UInt256 DebugSlot = UInt256.Parse("6489");
25+
// internal static Address DebugAddress = new Address("0x2c2b2df915e31d27e7a24c7c3cf9b114208a45e0");
26+
internal static Address DebugAddress = new Address("0x6ffedc1562918c07ae49b0ba210e6d80c7d61eab");
27+
internal static UInt256 DebugSlot = UInt256.Parse("0");
2928

3029
private StateId _currentStateId;
3130
private readonly SnapshotBundle _snapshotBundle;
@@ -178,7 +177,7 @@ private StorageTree CreateStorageTreeImpl(Address address)
178177
if (address == DebugAddress)
179178
{
180179
var val = storage.Get(DebugSlot);
181-
Console.Error.WriteLine($"Debug value is {val}");
180+
Console.Error.WriteLine($"Debug value is {val?.ToHexString()}");
182181
}
183182

184183
return storage;

src/Nethermind/Nethermind.State/Flat/SnapshotBundle.cs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ private void SetupMetric()
150150
}
151151

152152
public bool TryGetAccount(Address address, out Account? acc)
153+
{
154+
return DoTryGetAccount(address, false, out acc);
155+
}
156+
157+
private bool DoTryGetAccount(Address address, bool excludeChanged, out Account? acc)
153158
{
154159
if (_isDisposed)
155160
{
@@ -158,7 +163,7 @@ public bool TryGetAccount(Address address, out Account? acc)
158163
}
159164

160165
_accountGet.Inc();
161-
if (!_isReadOnly)
166+
if (!_isReadOnly && !excludeChanged)
162167
{
163168
if (_changedAccounts.TryGetValue(address, out acc)) return true;
164169
}
@@ -280,7 +285,14 @@ public TrieNode FindStateNodeOrUnknown(in TreePath path, Hash256 hash, bool isTr
280285
{
281286
if (!_isReadOnly)
282287
{
283-
node = _changedStateNodes.GetOrAdd(path, new TrieNode(NodeType.Unknown, hash));
288+
if (isTrieWarmer)
289+
{
290+
node = _cachedResource.LoadedStateNodes.GetOrAdd(path, new TrieNode(NodeType.Unknown, hash));
291+
}
292+
else
293+
{
294+
node = _changedStateNodes.GetOrAdd(path, new TrieNode(NodeType.Unknown, hash));
295+
}
284296
}
285297
else
286298
{
@@ -357,7 +369,14 @@ public TrieNode FindStorageNodeOrUnknown(Hash256AsKey address, in TreePath path,
357369
}
358370
else
359371
{
360-
node = _changedStorageNodes.GetOrAdd((address, path), new TrieNode(NodeType.Unknown, hash));
372+
if (isTrieWarmer)
373+
{
374+
node = _cachedResource.LoadedStorageNodes.GetOrAdd((address, path), new TrieNode(NodeType.Unknown, hash));
375+
}
376+
else
377+
{
378+
node = _changedStorageNodes.GetOrAdd((address, path), new TrieNode(NodeType.Unknown, hash));
379+
}
361380
}
362381
}
363382

@@ -474,6 +493,10 @@ public void SetNode(Hash256AsKey addr, in TreePath path, TrieNode newNode)
474493

475494
public void SetAccount(AddressAsKey addr, Account account)
476495
{
496+
if (addr == WorldStateScope.DebugAddress)
497+
{
498+
Console.Error.WriteLine($"set to {account}");
499+
}
477500
_changedAccounts[addr] = account;
478501
}
479502

@@ -696,11 +719,22 @@ public void Clear(Address address, Hash256AsKey addressHash)
696719
{
697720
if (_isReadOnly) throw new InvalidOperationException("Read only snapshot bundle");
698721
bool isNewAccount = false;
699-
if (TryGetAccount(address, out Account? account))
722+
if (DoTryGetAccount(address, excludeChanged: true, out Account? account))
700723
{
701724
// So... a clear is always sent even on new account. This makes is a minor optimization as
702725
// it skip persistence, but probably need to make sure it does not send it at all in the first place.
703726
isNewAccount = account == null;
727+
if (address == WorldStateScope.DebugAddress)
728+
{
729+
Console.Error.WriteLine("The clear is is new");
730+
}
731+
}
732+
else
733+
{
734+
if (address == WorldStateScope.DebugAddress)
735+
{
736+
Console.Error.WriteLine("The clear is not new");
737+
}
704738
}
705739
_selfDestructedAccountAddresses.TryAdd(address, isNewAccount);
706740

0 commit comments

Comments
 (0)