mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 09:53:48 +00:00
fix test code for trie
This commit is contained in:
parent
14831cc17f
commit
93377f95bf
3 changed files with 21 additions and 75 deletions
|
|
@ -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
|
|
||||||
}
|
|
||||||
|
|
@ -1,9 +1,8 @@
|
||||||
package tests
|
package tests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/params"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTrie(t *testing.T) {
|
func TestTrie(t *testing.T) {
|
||||||
|
|
@ -14,12 +13,11 @@ func TestTrie(t *testing.T) {
|
||||||
tm.skipLoad("hex_encoded_securetrie_test.json")
|
tm.skipLoad("hex_encoded_securetrie_test.json")
|
||||||
tm.skipLoad("trieanyorder_secureTrie.json")
|
tm.skipLoad("trieanyorder_secureTrie.json")
|
||||||
tm.skipLoad("trieanyorder.json")
|
tm.skipLoad("trieanyorder.json")
|
||||||
tm.skipLoad("trietest_secureTrie.json")
|
|
||||||
tm.skipLoad("trietestnextprev.json")
|
tm.skipLoad("trietestnextprev.json")
|
||||||
|
|
||||||
tm.walk(t, trieTestDir, func(t *testing.T, name string, test *TrieTest) {
|
tm.walk(t, trieTestDir, func(t *testing.T, name string, test *TrieTest) {
|
||||||
cfg := params.MainnetChainConfig
|
secure := strings.Contains(name, "secure")
|
||||||
if err := tm.checkFailure(t, test.Run(cfg)); err != nil {
|
if err := tm.checkFailure(t, test.Run(secure)); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -2,47 +2,37 @@ package tests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/trie"
|
"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 {
|
type TrieTest struct {
|
||||||
In [][]string `json:"in"`
|
In [][]string `json:"in"`
|
||||||
Root common.Hash `json:"root"`
|
Root common.Hash `json:"root"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type trieTestMarshaling struct {
|
func (tt *TrieTest) Run(secure bool) error {
|
||||||
In [][]string `json:"in"`
|
tr := trie.NewEmpty(nil)
|
||||||
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,
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, slices := range tt.In {
|
for _, slices := range tt.In {
|
||||||
slices := slices
|
key := []byte(slices[0])
|
||||||
if len(slices) == 0 {
|
val := []byte(slices[1])
|
||||||
return fmt.Errorf("empty input")
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, v := range slices {
|
if strings.HasPrefix(slices[0], "0x") {
|
||||||
id.Owner = common.HexToHash(v)
|
key, _ = FromHex(slices[0])
|
||||||
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 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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue