Skip to content

Commit 3e910b7

Browse files
committed
fix: closure capture race in generateTrieRoot goroutines
1 parent aa1a8da commit 3e910b7

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

core/state/snapshot/conversion.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -298,23 +298,26 @@ func generateTrieRoot(db ethdb.KeyValueWriter, scheme string, it Iterator, accou
298298
return stop(err)
299299
}
300300
// Fetch the next account and process it concurrently
301-
account, err := types.FullAccount(it.(AccountIterator).Account())
301+
acc, err := types.FullAccount(it.(AccountIterator).Account())
302302
if err != nil {
303303
return stop(err)
304304
}
305-
go func(hash common.Hash) {
306-
subroot, err := leafCallback(db, hash, common.BytesToHash(account.CodeHash), stats)
305+
h := it.Hash()
306+
ch := common.BytesToHash(acc.CodeHash)
307+
wr := acc.Root
308+
go func(hash common.Hash, codeHash common.Hash, wantRoot common.Hash) {
309+
subroot, err := leafCallback(db, hash, codeHash, stats)
307310
if err != nil {
308311
results <- err
309312
return
310313
}
311-
if account.Root != subroot {
312-
results <- fmt.Errorf("invalid subroot(path %x), want %x, have %x", hash, account.Root, subroot)
314+
if wantRoot != subroot {
315+
results <- fmt.Errorf("invalid subroot(path %x), want %x, have %x", hash, wantRoot, subroot)
313316
return
314317
}
315318
results <- nil
316-
}(it.Hash())
317-
fullData, err = rlp.EncodeToBytes(account)
319+
}(h, ch, wr)
320+
fullData, err = rlp.EncodeToBytes(acc)
318321
if err != nil {
319322
return stop(err)
320323
}

triedb/pathdb/verifier.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,23 +294,26 @@ func generateTrieRoot(it Iterator, account common.Hash, generatorFn trieHasherFn
294294
return stop(err)
295295
}
296296
// Fetch the next account and process it concurrently
297-
account, err := types.FullAccount(it.(AccountIterator).Account())
297+
acc, err := types.FullAccount(it.(AccountIterator).Account())
298298
if err != nil {
299299
return stop(err)
300300
}
301-
go func(hash common.Hash) {
302-
subroot, err := leafCallback(hash, common.BytesToHash(account.CodeHash), stats)
301+
h := it.Hash()
302+
ch := common.BytesToHash(acc.CodeHash)
303+
wr := acc.Root
304+
go func(hash common.Hash, codeHash common.Hash, wantRoot common.Hash) {
305+
subroot, err := leafCallback(hash, codeHash, stats)
303306
if err != nil {
304307
results <- err
305308
return
306309
}
307-
if account.Root != subroot {
308-
results <- fmt.Errorf("invalid subroot(path %x), want %x, have %x", hash, account.Root, subroot)
310+
if wantRoot != subroot {
311+
results <- fmt.Errorf("invalid subroot(path %x), want %x, have %x", hash, wantRoot, subroot)
309312
return
310313
}
311314
results <- nil
312-
}(it.Hash())
313-
fullData, err = rlp.EncodeToBytes(account)
315+
}(h, ch, wr)
316+
fullData, err = rlp.EncodeToBytes(acc)
314317
if err != nil {
315318
return stop(err)
316319
}

0 commit comments

Comments
 (0)