core/state: fix state iterator (#19127)

This commit is contained in:
Daniel Liu 2025-03-26 18:11:12 +08:00 committed by Daniel Liu
parent 3fd025e814
commit c73a0aa929
4 changed files with 19 additions and 44 deletions

View file

@ -25,7 +25,6 @@ import (
"github.com/XinFinOrg/XDPoSChain/crypto" "github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/log" "github.com/XinFinOrg/XDPoSChain/log"
"github.com/XinFinOrg/XDPoSChain/params" "github.com/XinFinOrg/XDPoSChain/params"
"github.com/XinFinOrg/XDPoSChain/rlp"
"github.com/XinFinOrg/XDPoSChain/trie" "github.com/XinFinOrg/XDPoSChain/trie"
) )
@ -128,14 +127,7 @@ func getCommonBackend(t *testing.T, chainConfig *params.ChainConfig) *backends.S
code, _ := contractBackendForSC.CodeAt(ctx, validatorSCAddr, nil) code, _ := contractBackendForSC.CodeAt(ctx, validatorSCAddr, nil)
storage := make(map[common.Hash]common.Hash) storage := make(map[common.Hash]common.Hash)
f := func(key, val common.Hash) bool { f := func(key, val common.Hash) bool {
decode := []byte{} storage[key] = val
trim := bytes.TrimLeft(val.Bytes(), "\x00")
err := rlp.DecodeBytes(trim, &decode)
if err != nil {
t.Fatalf("Failed while decode byte")
}
storage[key] = common.BytesToHash(decode)
log.Info("DecodeBytes", "value", val.String(), "decode", storage[key].String())
return true return true
} }
err = contractBackendForSC.ForEachStorageAt(ctx, validatorSCAddr, nil, f) err = contractBackendForSC.ForEachStorageAt(ctx, validatorSCAddr, nil, f)

View file

@ -28,7 +28,6 @@ import (
"github.com/XinFinOrg/XDPoSChain/crypto" "github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/log" "github.com/XinFinOrg/XDPoSChain/log"
"github.com/XinFinOrg/XDPoSChain/params" "github.com/XinFinOrg/XDPoSChain/params"
"github.com/XinFinOrg/XDPoSChain/rlp"
"github.com/XinFinOrg/XDPoSChain/trie" "github.com/XinFinOrg/XDPoSChain/trie"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -178,14 +177,7 @@ func getCommonBackend(t *testing.T, chainConfig *params.ChainConfig) *backends.S
code, _ := contractBackendForSC.CodeAt(ctx, validatorSCAddr, nil) code, _ := contractBackendForSC.CodeAt(ctx, validatorSCAddr, nil)
storage := make(map[common.Hash]common.Hash) storage := make(map[common.Hash]common.Hash)
f := func(key, val common.Hash) bool { f := func(key, val common.Hash) bool {
decode := []byte{} storage[key] = val
trim := bytes.TrimLeft(val.Bytes(), "\x00")
err := rlp.DecodeBytes(trim, &decode)
if err != nil {
t.Fatalf("Failed while decode byte")
}
storage[key] = common.BytesToHash(decode)
log.Info("DecodeBytes", "value", val.String(), "decode", storage[key].String())
return true return true
} }
err = contractBackendForSC.ForEachStorageAt(ctx, validatorSCAddr, nil, f) err = contractBackendForSC.ForEachStorageAt(ctx, validatorSCAddr, nil, f)
@ -262,14 +254,7 @@ func getMultiCandidatesBackend(t *testing.T, chainConfig *params.ChainConfig, n
code, _ := contractBackendForSC.CodeAt(ctx, validatorSCAddr, nil) code, _ := contractBackendForSC.CodeAt(ctx, validatorSCAddr, nil)
storage := make(map[common.Hash]common.Hash) storage := make(map[common.Hash]common.Hash)
f := func(key, val common.Hash) bool { f := func(key, val common.Hash) bool {
decode := []byte{} storage[key] = val
trim := bytes.TrimLeft(val.Bytes(), "\x00")
err := rlp.DecodeBytes(trim, &decode)
if err != nil {
t.Fatalf("Failed while decode byte")
}
storage[key] = common.BytesToHash(decode)
log.Info("DecodeBytes", "value", val.String(), "decode", storage[key].String())
return true return true
} }
err = contractBackendForSC.ForEachStorageAt(ctx, validatorSCAddr, nil, f) err = contractBackendForSC.ForEachStorageAt(ctx, validatorSCAddr, nil, f)
@ -347,14 +332,7 @@ func getProtectorObserverBackend(t *testing.T, chainConfig *params.ChainConfig)
code, _ := contractBackendForSC.CodeAt(ctx, validatorSCAddr, nil) code, _ := contractBackendForSC.CodeAt(ctx, validatorSCAddr, nil)
storage := make(map[common.Hash]common.Hash) storage := make(map[common.Hash]common.Hash)
f := func(key, val common.Hash) bool { f := func(key, val common.Hash) bool {
decode := []byte{} storage[key] = val
trim := bytes.TrimLeft(val.Bytes(), "\x00")
err := rlp.DecodeBytes(trim, &decode)
if err != nil {
t.Fatalf("Failed while decode byte")
}
storage[key] = common.BytesToHash(decode)
log.Info("DecodeBytes", "value", val.String(), "decode", storage[key].String())
return true return true
} }
err = contractBackendForSC.ForEachStorageAt(ctx, validatorSCAddr, nil, f) err = contractBackendForSC.ForEachStorageAt(ctx, validatorSCAddr, nil, f)

View file

@ -16,7 +16,6 @@
package validator package validator
import ( import (
"bytes"
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -36,7 +35,6 @@ import (
"github.com/XinFinOrg/XDPoSChain/crypto" "github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/log" "github.com/XinFinOrg/XDPoSChain/log"
"github.com/XinFinOrg/XDPoSChain/params" "github.com/XinFinOrg/XDPoSChain/params"
"github.com/XinFinOrg/XDPoSChain/rlp"
) )
var ( var (
@ -300,12 +298,7 @@ func TestStatedbUtils(t *testing.T) {
code, _ := contractBackend.CodeAt(ctx, validatorAddress, nil) code, _ := contractBackend.CodeAt(ctx, validatorAddress, nil)
storage := make(map[common.Hash]common.Hash) storage := make(map[common.Hash]common.Hash)
f := func(key, val common.Hash) bool { f := func(key, val common.Hash) bool {
decode := []byte{} storage[key] = val
trim := bytes.TrimLeft(val.Bytes(), "\x00")
rlp.DecodeBytes(trim, &decode)
storage[key] = common.BytesToHash(decode)
// t.Log("DecodeBytes", "value", val.String(), "decode", storage[key].String())
// t.Log("key", key.String(), "value", storage[key].String())
return true return true
} }
contractBackend.ForEachStorageAt(ctx, validatorAddress, nil, f) contractBackend.ForEachStorageAt(ctx, validatorAddress, nil, f)

View file

@ -597,13 +597,25 @@ func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common
return nil return nil
} }
it := trie.NewIterator(so.getTrie(db.db).NodeIterator(nil)) it := trie.NewIterator(so.getTrie(db.db).NodeIterator(nil))
for it.Next() { for it.Next() {
key := common.BytesToHash(db.trie.GetKey(it.Key)) key := common.BytesToHash(db.trie.GetKey(it.Key))
if value, dirty := so.dirtyStorage[key]; dirty { if value, dirty := so.dirtyStorage[key]; dirty {
cb(key, value) if !cb(key, value) {
return nil
}
continue continue
} }
cb(key, common.BytesToHash(it.Value))
if len(it.Value) > 0 {
_, content, _, err := rlp.Split(it.Value)
if err != nil {
return err
}
if !cb(key, common.BytesToHash(content)) {
return nil
}
}
} }
return nil return nil
} }