triedb/pathdb: add rawStorageKey check to encode/decode unit tests

This commit is contained in:
Zsolt Felfoldi 2025-01-17 01:20:38 +01:00
parent f458ddef40
commit 8757ad4499
2 changed files with 26 additions and 5 deletions

View file

@ -49,9 +49,9 @@ func randomStateSet(n int) (map[common.Address][]byte, map[common.Address]map[co
return accounts, storages return accounts, storages
} }
func makeHistory() *history { func makeHistory(rawStorageKey bool) *history {
accounts, storages := randomStateSet(3) accounts, storages := randomStateSet(3)
return newHistory(testrand.Hash(), types.EmptyRootHash, 0, accounts, storages, false) return newHistory(testrand.Hash(), types.EmptyRootHash, 0, accounts, storages, rawStorageKey)
} }
func makeHistories(n int) []*history { func makeHistories(n int) []*history {
@ -70,10 +70,15 @@ func makeHistories(n int) []*history {
} }
func TestEncodeDecodeHistory(t *testing.T) { func TestEncodeDecodeHistory(t *testing.T) {
testEncodeDecodeHistory(t, false)
testEncodeDecodeHistory(t, true)
}
func testEncodeDecodeHistory(t *testing.T, rawStorageKey bool) {
var ( var (
m meta m meta
dec history dec history
obj = makeHistory() obj = makeHistory(rawStorageKey)
) )
// check if meta data can be correctly encode/decode // check if meta data can be correctly encode/decode
blob := obj.meta.encode() blob := obj.meta.encode()

View file

@ -290,6 +290,11 @@ func TestStateRevertStorageNullMarker(t *testing.T) {
} }
func TestStatesEncode(t *testing.T) { func TestStatesEncode(t *testing.T) {
testStatesEncode(t, false)
testStatesEncode(t, true)
}
func testStatesEncode(t *testing.T, rawStorageKey bool) {
s := newStates( s := newStates(
map[common.Hash][]byte{ map[common.Hash][]byte{
{0x1}: {0x1}, {0x1}: {0x1},
@ -299,7 +304,7 @@ func TestStatesEncode(t *testing.T) {
common.Hash{0x1}: {0x1}, common.Hash{0x1}: {0x1},
}, },
}, },
false, rawStorageKey,
) )
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
if err := s.encode(buf); err != nil { if err := s.encode(buf); err != nil {
@ -315,9 +320,17 @@ func TestStatesEncode(t *testing.T) {
if !reflect.DeepEqual(s.storageData, dec.storageData) { if !reflect.DeepEqual(s.storageData, dec.storageData) {
t.Fatal("Unexpected storage data") t.Fatal("Unexpected storage data")
} }
if s.rawStorageKey != dec.rawStorageKey {
t.Fatal("Unexpected rawStorageKey flag")
}
} }
func TestStateWithOriginEncode(t *testing.T) { func TestStateWithOriginEncode(t *testing.T) {
testStateWithOriginEncode(t, false)
testStateWithOriginEncode(t, true)
}
func testStateWithOriginEncode(t *testing.T, rawStorageKey bool) {
s := NewStateSetWithOrigin( s := NewStateSetWithOrigin(
map[common.Hash][]byte{ map[common.Hash][]byte{
{0x1}: {0x1}, {0x1}: {0x1},
@ -335,7 +348,7 @@ func TestStateWithOriginEncode(t *testing.T) {
common.Hash{0x1}: {0x1}, common.Hash{0x1}: {0x1},
}, },
}, },
false, rawStorageKey,
) )
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
if err := s.encode(buf); err != nil { if err := s.encode(buf); err != nil {
@ -357,6 +370,9 @@ func TestStateWithOriginEncode(t *testing.T) {
if !reflect.DeepEqual(s.storageOrigin, dec.storageOrigin) { if !reflect.DeepEqual(s.storageOrigin, dec.storageOrigin) {
t.Fatal("Unexpected storage origin data") t.Fatal("Unexpected storage origin data")
} }
if s.rawStorageKey != dec.rawStorageKey {
t.Fatal("Unexpected rawStorageKey flag")
}
} }
func TestStateSizeTracking(t *testing.T) { func TestStateSizeTracking(t *testing.T) {