From c73a0aa9297cb829a178ab0759f8d6749b146f37 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Wed, 26 Mar 2025 18:11:12 +0800 Subject: [PATCH] core/state: fix state iterator (#19127) --- consensus/tests/engine_v1_tests/helper.go | 10 +------- consensus/tests/engine_v2_tests/helper.go | 28 +++-------------------- contracts/validator/validator_test.go | 9 +------- core/state/statedb.go | 16 +++++++++++-- 4 files changed, 19 insertions(+), 44 deletions(-) diff --git a/consensus/tests/engine_v1_tests/helper.go b/consensus/tests/engine_v1_tests/helper.go index 1898421264..daaf79c00e 100644 --- a/consensus/tests/engine_v1_tests/helper.go +++ b/consensus/tests/engine_v1_tests/helper.go @@ -25,7 +25,6 @@ import ( "github.com/XinFinOrg/XDPoSChain/crypto" "github.com/XinFinOrg/XDPoSChain/log" "github.com/XinFinOrg/XDPoSChain/params" - "github.com/XinFinOrg/XDPoSChain/rlp" "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) storage := make(map[common.Hash]common.Hash) f := func(key, val common.Hash) bool { - decode := []byte{} - 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()) + storage[key] = val return true } err = contractBackendForSC.ForEachStorageAt(ctx, validatorSCAddr, nil, f) diff --git a/consensus/tests/engine_v2_tests/helper.go b/consensus/tests/engine_v2_tests/helper.go index bd9a512a77..0702c3f768 100644 --- a/consensus/tests/engine_v2_tests/helper.go +++ b/consensus/tests/engine_v2_tests/helper.go @@ -28,7 +28,6 @@ import ( "github.com/XinFinOrg/XDPoSChain/crypto" "github.com/XinFinOrg/XDPoSChain/log" "github.com/XinFinOrg/XDPoSChain/params" - "github.com/XinFinOrg/XDPoSChain/rlp" "github.com/XinFinOrg/XDPoSChain/trie" "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) storage := make(map[common.Hash]common.Hash) f := func(key, val common.Hash) bool { - decode := []byte{} - 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()) + storage[key] = val return true } 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) storage := make(map[common.Hash]common.Hash) f := func(key, val common.Hash) bool { - decode := []byte{} - 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()) + storage[key] = val return true } 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) storage := make(map[common.Hash]common.Hash) f := func(key, val common.Hash) bool { - decode := []byte{} - 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()) + storage[key] = val return true } err = contractBackendForSC.ForEachStorageAt(ctx, validatorSCAddr, nil, f) diff --git a/contracts/validator/validator_test.go b/contracts/validator/validator_test.go index 00ed583650..f710f8bb14 100644 --- a/contracts/validator/validator_test.go +++ b/contracts/validator/validator_test.go @@ -16,7 +16,6 @@ package validator import ( - "bytes" "context" "encoding/json" "fmt" @@ -36,7 +35,6 @@ import ( "github.com/XinFinOrg/XDPoSChain/crypto" "github.com/XinFinOrg/XDPoSChain/log" "github.com/XinFinOrg/XDPoSChain/params" - "github.com/XinFinOrg/XDPoSChain/rlp" ) var ( @@ -300,12 +298,7 @@ func TestStatedbUtils(t *testing.T) { code, _ := contractBackend.CodeAt(ctx, validatorAddress, nil) storage := make(map[common.Hash]common.Hash) f := func(key, val common.Hash) bool { - decode := []byte{} - 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()) + storage[key] = val return true } contractBackend.ForEachStorageAt(ctx, validatorAddress, nil, f) diff --git a/core/state/statedb.go b/core/state/statedb.go index 445da17edc..10b06717bb 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -597,13 +597,25 @@ func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common return nil } it := trie.NewIterator(so.getTrie(db.db).NodeIterator(nil)) + for it.Next() { key := common.BytesToHash(db.trie.GetKey(it.Key)) if value, dirty := so.dirtyStorage[key]; dirty { - cb(key, value) + if !cb(key, value) { + return nil + } 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 }