more refactor

This commit is contained in:
Roy Crihfield 2023-09-24 14:42:53 +08:00
parent 5076d731d7
commit e53d799563
38 changed files with 43 additions and 29 deletions

View File

@ -1,4 +1,4 @@
package util
package chaindata
import (
"errors"
@ -7,8 +7,8 @@ import (
"runtime"
)
type ChainData struct {
Path, AncientPath string
type Paths struct {
ChainData, Ancient string
}
// List of names of chaindata fixtures accessible via ChainDataPaths
@ -27,9 +27,8 @@ func IsFixture(chain string) bool {
return has
}
// GetChainData returns the absolute paths to fixture chaindata for the given name.
func GetChainData(chain string) (*ChainData, error) {
// fail if chain not in FixtureChains
// GetFixture returns the absolute paths to fixture chaindata for the given name.
func GetFixture(chain string) (*Paths, error) {
if !IsFixture(chain) {
return nil, errors.New("no fixture named " + chain)
}
@ -39,17 +38,12 @@ func GetChainData(chain string) (*ChainData, error) {
return nil, errors.New("could not get function source path")
}
chainPath := filepath.Join(filepath.Dir(thisPath), "..", "data", chain)
chaindataPath, err := filepath.Abs(chainPath)
if err != nil {
return nil, errors.New("cannot resolve path " + chainPath)
}
chaindataPath := filepath.Join(filepath.Dir(thisPath), "_data", chain)
ancientdataPath := filepath.Join(chaindataPath, "ancient")
if _, err := os.Stat(chaindataPath); err != nil {
return nil, errors.New("must populate chaindata at " + chaindataPath)
return nil, errors.New("cannot access chaindata at " + chaindataPath)
}
return &ChainData{chaindataPath, ancientdataPath}, nil
return &Paths{chaindataPath, ancientdataPath}, nil
}

View File

@ -6,25 +6,21 @@ import (
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/cerc-io/eth-testing/chaindata/util"
"github.com/cerc-io/eth-testing/chaindata"
)
func testReadChainData(t *testing.T, name string) {
ChainDataPath, AncientDataPath := util.GetChainData(name)
kvdb, ldberr := rawdb.NewLevelDBDatabase(ChainDataPath, 1024, 256, "vdb-geth", true)
func testReadChainData(t *testing.T, data *chaindata.Paths) {
kvdb, ldberr := rawdb.NewLevelDBDatabase(data.ChainData, 1024, 256, t.Name(), true)
if ldberr != nil {
t.Fatal(ldberr)
}
edb, err := rawdb.NewDatabaseWithFreezer(kvdb, AncientDataPath, "vdb-geth", true)
if err != nil {
t.Fatal(err)
}
edb, err := rawdb.NewDatabaseWithFreezer(kvdb, data.Ancient, t.Name(), true)
if err != nil {
t.Fatal(err)
}
defer edb.Close()
// Check that we can open and traverse a trie at the head block
hash := rawdb.ReadHeadHeaderHash(edb)
height := rawdb.ReadHeaderNumber(edb, hash)
if height == nil {
@ -35,14 +31,26 @@ func testReadChainData(t *testing.T, name string) {
t.Fatalf("unable to read canonical header at height %d", height)
}
sdb := state.NewDatabase(edb)
_, err = sdb.OpenTrie(header.Root)
tree, err := sdb.OpenTrie(header.Root)
if err != nil {
t.Fatal(err)
}
it := tree.NodeIterator(nil)
for it.Next(true) {
}
if err := it.Error(); err != nil {
t.Fatal(err)
}
}
func TestReadChainData(t *testing.T) {
for _, name := range []string{"small", "small2"} {
t.Run(name, func(t *testing.T) { testReadChainData(t, name) })
t.Run(name, func(t *testing.T) {
data, err := chaindata.GetFixture(name)
if err != nil {
t.Fatal(err)
}
testReadChainData(t, data)
})
}
}

View File

@ -1,9 +1,15 @@
package small
import (
"github.com/cerc-io/eth-testing/chaindata/util"
"github.com/cerc-io/eth-testing/chaindata"
)
var (
ChainDataPath, AncientDataPath = util.GetChainData("small")
ChainData, err = chaindata.GetFixture("small")
)
func init() {
if err != nil {
panic(err)
}
}

View File

@ -1,9 +1,15 @@
package small2
import (
"github.com/cerc-io/eth-testing/chaindata/util"
"github.com/cerc-io/eth-testing/chaindata"
)
var (
ChainDataPath, AncientDataPath = util.GetChainData("small2")
ChainData, err = chaindata.GetFixture("small2")
)
func init() {
if err != nil {
panic(err)
}
}