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 ( import (
"errors" "errors"
@ -7,8 +7,8 @@ import (
"runtime" "runtime"
) )
type ChainData struct { type Paths struct {
Path, AncientPath string ChainData, Ancient string
} }
// List of names of chaindata fixtures accessible via ChainDataPaths // List of names of chaindata fixtures accessible via ChainDataPaths
@ -27,9 +27,8 @@ func IsFixture(chain string) bool {
return has return has
} }
// GetChainData returns the absolute paths to fixture chaindata for the given name. // GetFixture returns the absolute paths to fixture chaindata for the given name.
func GetChainData(chain string) (*ChainData, error) { func GetFixture(chain string) (*Paths, error) {
// fail if chain not in FixtureChains
if !IsFixture(chain) { if !IsFixture(chain) {
return nil, errors.New("no fixture named " + 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") return nil, errors.New("could not get function source path")
} }
chainPath := filepath.Join(filepath.Dir(thisPath), "..", "data", chain) chaindataPath := filepath.Join(filepath.Dir(thisPath), "_data", chain)
chaindataPath, err := filepath.Abs(chainPath)
if err != nil {
return nil, errors.New("cannot resolve path " + chainPath)
}
ancientdataPath := filepath.Join(chaindataPath, "ancient") ancientdataPath := filepath.Join(chaindataPath, "ancient")
if _, err := os.Stat(chaindataPath); err != nil { 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/rawdb"
"github.com/ethereum/go-ethereum/core/state" "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) { func testReadChainData(t *testing.T, data *chaindata.Paths) {
ChainDataPath, AncientDataPath := util.GetChainData(name) kvdb, ldberr := rawdb.NewLevelDBDatabase(data.ChainData, 1024, 256, t.Name(), true)
kvdb, ldberr := rawdb.NewLevelDBDatabase(ChainDataPath, 1024, 256, "vdb-geth", true)
if ldberr != nil { if ldberr != nil {
t.Fatal(ldberr) t.Fatal(ldberr)
} }
edb, err := rawdb.NewDatabaseWithFreezer(kvdb, AncientDataPath, "vdb-geth", true) edb, err := rawdb.NewDatabaseWithFreezer(kvdb, data.Ancient, t.Name(), true)
if err != nil {
t.Fatal(err)
}
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer edb.Close() defer edb.Close()
// Check that we can open and traverse a trie at the head block
hash := rawdb.ReadHeadHeaderHash(edb) hash := rawdb.ReadHeadHeaderHash(edb)
height := rawdb.ReadHeaderNumber(edb, hash) height := rawdb.ReadHeaderNumber(edb, hash)
if height == nil { 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) t.Fatalf("unable to read canonical header at height %d", height)
} }
sdb := state.NewDatabase(edb) sdb := state.NewDatabase(edb)
_, err = sdb.OpenTrie(header.Root) tree, err := sdb.OpenTrie(header.Root)
if err != nil { if err != nil {
t.Fatal(err) 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) { func TestReadChainData(t *testing.T) {
for _, name := range []string{"small", "small2"} { 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 package small
import ( import (
"github.com/cerc-io/eth-testing/chaindata/util" "github.com/cerc-io/eth-testing/chaindata"
) )
var ( 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 package small2
import ( import (
"github.com/cerc-io/eth-testing/chaindata/util" "github.com/cerc-io/eth-testing/chaindata"
) )
var ( var (
ChainDataPath, AncientDataPath = util.GetChainData("small2") ChainData, err = chaindata.GetFixture("small2")
) )
func init() {
if err != nil {
panic(err)
}
}