add "small2" and refactor

This commit is contained in:
Roy Crihfield 2023-09-22 01:49:25 +08:00
parent 6e0b733323
commit 4f2242eb71
21 changed files with 56 additions and 13 deletions

View File

@ -1,15 +1,16 @@
package util_test
package chaindata_test
import (
"testing"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/cerc-io/eth-testing/chaindata/util"
)
func testReadChainData(t *testing.T, name string) {
ChainDataPath, AncientDataPath := util.ChainDataPaths(name)
ChainDataPath, AncientDataPath := util.GetChainData(name)
kvdb, ldberr := rawdb.NewLevelDBDatabase(ChainDataPath, 1024, 256, "vdb-geth", true)
if ldberr != nil {
@ -33,10 +34,15 @@ func testReadChainData(t *testing.T, name string) {
if header == nil {
t.Fatalf("unable to read canonical header at height %d", height)
}
sdb := state.NewDatabase(edb)
_, err = sdb.OpenTrie(header.Root)
if err != nil {
t.Fatal(err)
}
}
func TestReadChainData(t *testing.T) {
for _, name := range []string{"small"} {
for _, name := range []string{"small", "small2"} {
t.Run(name, func(t *testing.T) { testReadChainData(t, name) })
}
}

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1 @@
<EFBFBD><01>

View File

@ -0,0 +1 @@
<EFBFBD><01>

Binary file not shown.

View File

@ -0,0 +1 @@
<EFBFBD><01>

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1 @@
<EFBFBD><01>

Binary file not shown.

View File

@ -0,0 +1 @@
<EFBFBD><01>

View File

@ -5,5 +5,5 @@ import (
)
var (
ChainDataPath, AncientDataPath = util.ChainDataPaths("small")
ChainDataPath, AncientDataPath = util.GetChainData("small")
)

View File

@ -0,0 +1,9 @@
package small2
import (
"github.com/cerc-io/eth-testing/chaindata/util"
)
var (
ChainDataPath, AncientDataPath = util.GetChainData("small2")
)

View File

@ -1,32 +1,55 @@
package util
import (
"errors"
"os"
"path/filepath"
"runtime"
)
// ChainDataPath returns the absolute paths to fixture chaindata for the given name.
// Currently the only chains are:
// - small: 32-block small-size chain
// - medium: a short medium-sized chain
func ChainDataPaths(chain string) (string, string) {
type ChainData struct {
Path, AncientPath string
}
// List of names of chaindata fixtures accessible via ChainDataPaths
var FixtureChains = []string{
"small", "small2",
}
func IsFixture(chain string) bool {
has := false
for _, fixture := range FixtureChains {
if chain == fixture {
has = true
break
}
}
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
if !IsFixture(chain) {
return nil, errors.New("no fixture named " + chain)
}
_, thisPath, _, ok := runtime.Caller(0)
if !ok {
panic("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, err := filepath.Abs(chainPath)
if err != nil {
panic("cannot resolve path " + chainPath)
return nil, errors.New("cannot resolve path " + chainPath)
}
ancientdataPath := filepath.Join(chaindataPath, "ancient")
if _, err := os.Stat(chaindataPath); err != nil {
panic("must populate chaindata at " + chaindataPath)
return nil, errors.New("must populate chaindata at " + chaindataPath)
}
return chaindataPath, ancientdataPath
return &ChainData{chaindataPath, ancientdataPath}, nil
}