fix: closure capture race in generateTrieRoot goroutines

This commit is contained in:
Fibonacci747 2025-11-27 16:37:35 +00:00
parent aa1a8dacae
commit 3e910b7a4c
2 changed files with 20 additions and 14 deletions

View file

@ -298,23 +298,26 @@ func generateTrieRoot(db ethdb.KeyValueWriter, scheme string, it Iterator, accou
return stop(err) return stop(err)
} }
// Fetch the next account and process it concurrently // Fetch the next account and process it concurrently
account, err := types.FullAccount(it.(AccountIterator).Account()) acc, err := types.FullAccount(it.(AccountIterator).Account())
if err != nil { if err != nil {
return stop(err) return stop(err)
} }
go func(hash common.Hash) { h := it.Hash()
subroot, err := leafCallback(db, hash, common.BytesToHash(account.CodeHash), stats) ch := common.BytesToHash(acc.CodeHash)
wr := acc.Root
go func(hash common.Hash, codeHash common.Hash, wantRoot common.Hash) {
subroot, err := leafCallback(db, hash, codeHash, stats)
if err != nil { if err != nil {
results <- err results <- err
return return
} }
if account.Root != subroot { if wantRoot != subroot {
results <- fmt.Errorf("invalid subroot(path %x), want %x, have %x", hash, account.Root, subroot) results <- fmt.Errorf("invalid subroot(path %x), want %x, have %x", hash, wantRoot, subroot)
return return
} }
results <- nil results <- nil
}(it.Hash()) }(h, ch, wr)
fullData, err = rlp.EncodeToBytes(account) fullData, err = rlp.EncodeToBytes(acc)
if err != nil { if err != nil {
return stop(err) return stop(err)
} }

View file

@ -294,23 +294,26 @@ func generateTrieRoot(it Iterator, account common.Hash, generatorFn trieHasherFn
return stop(err) return stop(err)
} }
// Fetch the next account and process it concurrently // Fetch the next account and process it concurrently
account, err := types.FullAccount(it.(AccountIterator).Account()) acc, err := types.FullAccount(it.(AccountIterator).Account())
if err != nil { if err != nil {
return stop(err) return stop(err)
} }
go func(hash common.Hash) { h := it.Hash()
subroot, err := leafCallback(hash, common.BytesToHash(account.CodeHash), stats) ch := common.BytesToHash(acc.CodeHash)
wr := acc.Root
go func(hash common.Hash, codeHash common.Hash, wantRoot common.Hash) {
subroot, err := leafCallback(hash, codeHash, stats)
if err != nil { if err != nil {
results <- err results <- err
return return
} }
if account.Root != subroot { if wantRoot != subroot {
results <- fmt.Errorf("invalid subroot(path %x), want %x, have %x", hash, account.Root, subroot) results <- fmt.Errorf("invalid subroot(path %x), want %x, have %x", hash, wantRoot, subroot)
return return
} }
results <- nil results <- nil
}(it.Hash()) }(h, ch, wr)
fullData, err = rlp.EncodeToBytes(account) fullData, err = rlp.EncodeToBytes(acc)
if err != nil { if err != nil {
return stop(err) return stop(err)
} }