diff --git a/tests/gen_trietest.go b/tests/gen_trietest.go new file mode 100644 index 0000000000..bbe4ab0ab6 --- /dev/null +++ b/tests/gen_trietest.go @@ -0,0 +1,42 @@ +// 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/init_test.go b/tests/init_test.go index effeec2b86..bca6fef08e 100644 --- a/tests/init_test.go +++ b/tests/init_test.go @@ -41,6 +41,7 @@ var ( transactionTestDir = filepath.Join(baseDir, "TransactionTests") rlpTestDir = filepath.Join(baseDir, "RLPTests") difficultyTestDir = filepath.Join(baseDir, "BasicTests") + trieTestDir = filepath.Join(baseDir, "TrieTests") executionSpecBlockchainTestDir = filepath.Join(".", "spec-tests", "fixtures", "blockchain_tests") executionSpecStateTestDir = filepath.Join(".", "spec-tests", "fixtures", "state_tests") benchmarksDir = filepath.Join(".", "evm-benchmarks", "benchmarks") diff --git a/tests/trie_test.go b/tests/trie_test.go new file mode 100644 index 0000000000..6f82eaf03e --- /dev/null +++ b/tests/trie_test.go @@ -0,0 +1,26 @@ +package tests + +import ( + "testing" + + "github.com/ethereum/go-ethereum/params" +) + +func TestTrie(t *testing.T) { + t.Parallel() + + tm := new(testMatcher) + + 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 { + t.Error(err) + } + }) +} diff --git a/tests/trie_test_util.go b/tests/trie_test_util.go new file mode 100644 index 0000000000..893126028a --- /dev/null +++ b/tests/trie_test_util.go @@ -0,0 +1,48 @@ +package tests + +import ( + "fmt" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/params" + "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, + } + + for _, slices := range tt.In { + slices := slices + if len(slices) == 0 { + return fmt.Errorf("empty input") + } + + 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()) + } + } + } + return nil +}