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)
}
// 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 {
return stop(err)
}
go func(hash common.Hash) {
subroot, err := leafCallback(db, hash, common.BytesToHash(account.CodeHash), stats)
h := it.Hash()
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 {
results <- err
return
}
if account.Root != subroot {
results <- fmt.Errorf("invalid subroot(path %x), want %x, have %x", hash, account.Root, subroot)
if wantRoot != subroot {
results <- fmt.Errorf("invalid subroot(path %x), want %x, have %x", hash, wantRoot, subroot)
return
}
results <- nil
}(it.Hash())
fullData, err = rlp.EncodeToBytes(account)
}(h, ch, wr)
fullData, err = rlp.EncodeToBytes(acc)
if err != nil {
return stop(err)
}

View file

@ -294,23 +294,26 @@ func generateTrieRoot(it Iterator, account common.Hash, generatorFn trieHasherFn
return stop(err)
}
// 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 {
return stop(err)
}
go func(hash common.Hash) {
subroot, err := leafCallback(hash, common.BytesToHash(account.CodeHash), stats)
h := it.Hash()
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 {
results <- err
return
}
if account.Root != subroot {
results <- fmt.Errorf("invalid subroot(path %x), want %x, have %x", hash, account.Root, subroot)
if wantRoot != subroot {
results <- fmt.Errorf("invalid subroot(path %x), want %x, have %x", hash, wantRoot, subroot)
return
}
results <- nil
}(it.Hash())
fullData, err = rlp.EncodeToBytes(account)
}(h, ch, wr)
fullData, err = rlp.EncodeToBytes(acc)
if err != nil {
return stop(err)
}