fix test code for trie

This commit is contained in:
ymizuguchi 2024-12-11 19:58:18 +09:00
parent 14831cc17f
commit 93377f95bf
3 changed files with 21 additions and 75 deletions

View file

@ -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
}

View file

@ -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)
}
})

View file

@ -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
}