create test code for trie

This commit is contained in:
mizuguchi_ef 2024-12-10 19:15:09 +09:00
parent 08e6bdb550
commit 14831cc17f
4 changed files with 117 additions and 0 deletions

42
tests/gen_trietest.go Normal file
View file

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

View file

@ -41,6 +41,7 @@ var (
transactionTestDir = filepath.Join(baseDir, "TransactionTests") transactionTestDir = filepath.Join(baseDir, "TransactionTests")
rlpTestDir = filepath.Join(baseDir, "RLPTests") rlpTestDir = filepath.Join(baseDir, "RLPTests")
difficultyTestDir = filepath.Join(baseDir, "BasicTests") difficultyTestDir = filepath.Join(baseDir, "BasicTests")
trieTestDir = filepath.Join(baseDir, "TrieTests")
executionSpecBlockchainTestDir = filepath.Join(".", "spec-tests", "fixtures", "blockchain_tests") executionSpecBlockchainTestDir = filepath.Join(".", "spec-tests", "fixtures", "blockchain_tests")
executionSpecStateTestDir = filepath.Join(".", "spec-tests", "fixtures", "state_tests") executionSpecStateTestDir = filepath.Join(".", "spec-tests", "fixtures", "state_tests")
benchmarksDir = filepath.Join(".", "evm-benchmarks", "benchmarks") benchmarksDir = filepath.Join(".", "evm-benchmarks", "benchmarks")

26
tests/trie_test.go Normal file
View file

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

48
tests/trie_test_util.go Normal file
View file

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