55using Nethermind . Core ;
66using Nethermind . Core . Crypto ;
77using Nethermind . Core . Extensions ;
8- using Nethermind . Evm ;
98using Nethermind . Evm . State ;
109using Nethermind . Int256 ;
1110using Nethermind . Logging ;
12- using Nethermind . Trie ;
13- using Nethermind . Trie . Pruning ;
14- using NonBlocking ;
1511
1612namespace 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 {
0 commit comments