From 93377f95bf570f0acba53c960b6f2e2aac769cfc Mon Sep 17 00:00:00 2001 From: ymizuguchi Date: Wed, 11 Dec 2024 19:58:18 +0900 Subject: [PATCH] fix test code for trie --- tests/gen_trietest.go | 42 ------------------------------------- tests/trie_test.go | 8 +++---- tests/trie_test_util.go | 46 ++++++++++++++++------------------------- 3 files changed, 21 insertions(+), 75 deletions(-) delete mode 100644 tests/gen_trietest.go diff --git a/tests/gen_trietest.go b/tests/gen_trietest.go deleted file mode 100644 index bbe4ab0ab6..0000000000 --- a/tests/gen_trietest.go +++ /dev/null @@ -1,42 +0,0 @@ -// Code generated by github.com/fjl/gencodec. DO NOT EDIT. - -package tests - -import ( - "encoding/json" - - "github.com/ethereum/go-ethereum/common" -) - -var _ = (*trieTestMarshaling)(nil) - -// MarshalJSON marshals as JSON. -func (t TrieTest) MarshalJSON() ([]byte, error) { - type TrieTest struct { - In [][]string `json:"in"` - Root common.Hash `json:"root"` - } - var enc TrieTest - enc.In = t.In - enc.Root = t.Root - return json.Marshal(&enc) -} - -// UnmarshalJSON unmarshals from JSON. -func (t *TrieTest) UnmarshalJSON(input []byte) error { - type TrieTest struct { - In [][]string `json:"in"` - Root *common.Hash `json:"root"` - } - var dec TrieTest - if err := json.Unmarshal(input, &dec); err != nil { - return err - } - if dec.In != nil { - t.In = dec.In - } - if dec.Root != nil { - t.Root = *dec.Root - } - return nil -} diff --git a/tests/trie_test.go b/tests/trie_test.go index 6f82eaf03e..9a81c00bf3 100644 --- a/tests/trie_test.go +++ b/tests/trie_test.go @@ -1,9 +1,8 @@ package tests import ( + "strings" "testing" - - "github.com/ethereum/go-ethereum/params" ) func TestTrie(t *testing.T) { @@ -14,12 +13,11 @@ func TestTrie(t *testing.T) { tm.skipLoad("hex_encoded_securetrie_test.json") tm.skipLoad("trieanyorder_secureTrie.json") tm.skipLoad("trieanyorder.json") - tm.skipLoad("trietest_secureTrie.json") tm.skipLoad("trietestnextprev.json") tm.walk(t, trieTestDir, func(t *testing.T, name string, test *TrieTest) { - cfg := params.MainnetChainConfig - if err := tm.checkFailure(t, test.Run(cfg)); err != nil { + secure := strings.Contains(name, "secure") + if err := tm.checkFailure(t, test.Run(secure)); err != nil { t.Error(err) } }) diff --git a/tests/trie_test_util.go b/tests/trie_test_util.go index 893126028a..bb2e45e255 100644 --- a/tests/trie_test_util.go +++ b/tests/trie_test_util.go @@ -2,47 +2,37 @@ package tests import ( "fmt" + "strings" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/trie" ) -//go:generate go run github.com/fjl/gencodec -type TrieTest -field-override trieTestMarshaling -out gen_trietest.go - type TrieTest struct { In [][]string `json:"in"` Root common.Hash `json:"root"` } -type trieTestMarshaling struct { - In [][]string `json:"in"` - Root common.Hash `json:"root"` -} - -func (tt *TrieTest) Run(config *params.ChainConfig) error { - // dbConf := new(triedb.Config) - // tdb := triedb.NewDatabase(rawdb.NewMemoryDatabase(), dbConf) - // trie := trie.NewEmpty(tdb) - id := &trie.ID{ - Root: tt.Root, - } - +func (tt *TrieTest) Run(secure bool) error { + tr := trie.NewEmpty(nil) for _, slices := range tt.In { - slices := slices - if len(slices) == 0 { - return fmt.Errorf("empty input") - } + key := []byte(slices[0]) + val := []byte(slices[1]) - for _, v := range slices { - id.Owner = common.HexToHash(v) - tr, _ := trie.New(id, nil) - actual := tr.Hash() - - if id.Root != actual { - return fmt.Errorf("root hash mismatch: %s != %s", id.Root.Hex(), actual.Hex()) - } + if strings.HasPrefix(slices[0], "0x") { + key, _ = FromHex(slices[0]) } + if secure { + key = crypto.Keccak256(key) + } + if strings.HasPrefix(slices[1], "0x") { + val, _ = FromHex(slices[1]) + } + tr.Update(key, val) + } + if have, want := tr.Hash(), tt.Root; have != want { + return fmt.Errorf("root mismatch: have %#x want %#x", have, want) } return nil }