@@ -47,7 +47,7 @@ var zeroTreeIndex uint256.Int
4747// collect the key-values in a way that is efficient.
4848type keyValueMigrator struct {
4949 // leafData contains the values for the future leaf for a particular VKT branch.
50- leafData [] migratedKeyValue
50+ leafData map [ branchKey ] * migratedKeyValue
5151
5252 // When prepare() is called, it will start a background routine that will process the leafData
5353 // saving the result in newLeaves to be used by migrateCollectedKeyValues(). The background
@@ -66,7 +66,7 @@ func newKeyValueMigrator() *keyValueMigrator {
6666 _ = verkle .GetConfig ()
6767 return & keyValueMigrator {
6868 processingReady : make (chan struct {}),
69- leafData : make ([] migratedKeyValue , 0 , 10_000 ),
69+ leafData : make (map [ branchKey ] * migratedKeyValue , 10_000 ),
7070 }
7171}
7272
@@ -138,19 +138,17 @@ func (kvm *keyValueMigrator) addAccountCode(addr []byte, codeSize uint64, chunks
138138}
139139
140140func (kvm * keyValueMigrator ) getOrInitLeafNodeData (bk branchKey ) * verkle.BatchNewLeafNodeData {
141- // Remember that keyValueMigration receives actions ordered by (address, subtreeIndex).
142- // This means that we can assume that the last element of leafData is the one that we
143- // are looking for, or that we need to create a new one.
144- if len (kvm .leafData ) == 0 || kvm .leafData [len (kvm .leafData )- 1 ].branchKey != bk {
145- kvm .leafData = append (kvm .leafData , migratedKeyValue {
146- branchKey : bk ,
147- leafNodeData : verkle.BatchNewLeafNodeData {
148- Stem : nil , // It will be calculated in the prepare() phase, since it's CPU heavy.
149- Values : make (map [byte ][]byte ),
150- },
151- })
141+ if ld , ok := kvm .leafData [bk ]; ok {
142+ return & ld .leafNodeData
152143 }
153- return & kvm .leafData [len (kvm .leafData )- 1 ].leafNodeData
144+ kvm .leafData [bk ] = & migratedKeyValue {
145+ branchKey : bk ,
146+ leafNodeData : verkle.BatchNewLeafNodeData {
147+ Stem : nil , // It will be calculated in the prepare() phase, since it's CPU heavy.
148+ Values : make (map [byte ][]byte , 256 ),
149+ },
150+ }
151+ return & kvm .leafData [bk ].leafNodeData
154152}
155153
156154func (kvm * keyValueMigrator ) prepare () {
@@ -159,6 +157,10 @@ func (kvm *keyValueMigrator) prepare() {
159157 go func () {
160158 // Step 1: We split kvm.leafData in numBatches batches, and we process each batch in a separate goroutine.
161159 // This fills each leafNodeData.Stem with the correct value.
160+ leafData := make ([]migratedKeyValue , 0 , len (kvm .leafData ))
161+ for _ , v := range kvm .leafData {
162+ leafData = append (leafData , * v )
163+ }
162164 var wg sync.WaitGroup
163165 batchNum := runtime .NumCPU ()
164166 batchSize := (len (kvm .leafData ) + batchNum - 1 ) / batchNum
@@ -170,7 +172,7 @@ func (kvm *keyValueMigrator) prepare() {
170172 }
171173 wg .Add (1 )
172174
173- batch := kvm . leafData [start :end ]
175+ batch := leafData [start :end ]
174176 go func () {
175177 defer wg .Done ()
176178 var currAddr common.Address
@@ -190,8 +192,8 @@ func (kvm *keyValueMigrator) prepare() {
190192
191193 // Step 2: Now that we have all stems (i.e: tree keys) calculated, we can create the new leaves.
192194 nodeValues := make ([]verkle.BatchNewLeafNodeData , len (kvm .leafData ))
193- for i := range kvm . leafData {
194- nodeValues [i ] = kvm . leafData [i ].leafNodeData
195+ for i := range leafData {
196+ nodeValues [i ] = leafData [i ].leafNodeData
195197 }
196198
197199 // Create all leaves in batch mode so we can optimize cryptography operations.
@@ -306,7 +308,12 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash, maxMovedC
306308 if err != nil {
307309 return err
308310 }
309- stIt .Next ()
311+ processed := stIt .Next ()
312+ if processed {
313+ log .Debug ("account has storage and a next item" )
314+ } else {
315+ log .Debug ("account has storage and NO next item" )
316+ }
310317
311318 // fdb.StorageProcessed will be initialized to `true` if the
312319 // entire storage for an account was not entirely processed
@@ -315,6 +322,7 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash, maxMovedC
315322 // If the entire storage was processed, then the iterator was
316323 // created in vain, but it's ok as this will not happen often.
317324 for ; ! migrdb .GetStorageProcessed () && count < maxMovedCount ; count ++ {
325+ log .Trace ("Processing storage" , "count" , count , "slot" , stIt .Slot (), "storage processed" , migrdb .GetStorageProcessed (), "current account" , migrdb .GetCurrentAccountAddress (), "current account hash" , migrdb .GetCurrentAccountHash ())
318326 var (
319327 value []byte // slot value after RLP decoding
320328 safeValue [32 ]byte // 32-byte aligned value
@@ -337,6 +345,7 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash, maxMovedC
337345 return fmt .Errorf ("slotnr len is zero is not 32: %d" , len (slotnr ))
338346 }
339347 }
348+ log .Trace ("found slot number" , "number" , slotnr )
340349 if crypto .Keccak256Hash (slotnr [:]) != stIt .Hash () {
341350 return fmt .Errorf ("preimage file does not match storage hash: %s!=%s" , crypto .Keccak256Hash (slotnr ), stIt .Hash ())
342351 }
@@ -378,6 +387,7 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash, maxMovedC
378387 // Move to the next account, if available - or end
379388 // the transition otherwise.
380389 if accIt .Next () {
390+ log .Trace ("Found another account to convert" , "hash" , accIt .Hash ())
381391 var addr common.Address
382392 if hasPreimagesBin {
383393 if _ , err := io .ReadFull (fpreimages , addr [:]); err != nil {
@@ -393,6 +403,7 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash, maxMovedC
393403 if crypto .Keccak256Hash (addr [:]) != accIt .Hash () {
394404 return fmt .Errorf ("preimage file does not match account hash: %s != %s" , crypto .Keccak256Hash (addr [:]), accIt .Hash ())
395405 }
406+ log .Trace ("Converting account address" , "hash" , accIt .Hash (), "addr" , addr )
396407 preimageSeek += int64 (len (addr ))
397408 migrdb .SetCurrentAccountAddress (addr )
398409 } else {
0 commit comments