This commit is contained in:
Roy Crihfield 2024-07-02 22:31:51 +08:00
parent bc2d50d33f
commit b76abeac48
2 changed files with 26 additions and 16 deletions

View File

@ -1,4 +1,4 @@
package chaindata package chains
import ( import (
"errors" "errors"
@ -14,7 +14,9 @@ type Paths struct {
// List of names of chaindata fixtures accessible via ChainDataPaths // List of names of chaindata fixtures accessible via ChainDataPaths
var FixtureChains = []string{ var FixtureChains = []string{
"small", "small2", "premerge1",
"premerge2",
"postmerge1",
} }
func IsFixture(chain string) bool { func IsFixture(chain string) bool {
@ -41,20 +43,25 @@ func GetFixture(chain string) (*Paths, error) {
return nil, errors.New("could not get function source path") return nil, errors.New("could not get function source path")
} }
chaindataPath := filepath.Join(filepath.Dir(thisPath), "_data", chain) chaindataPath := filepath.Join(filepath.Dir(thisPath), "data", chain, "geth", "chaindata")
if _, err := os.Stat(chaindataPath); err != nil { if _, err := os.Stat(chaindataPath); err != nil {
return nil, errors.New("cannot access chaindata at " + chaindataPath) return nil, errors.New("cannot access chaindata at " + chaindataPath)
} }
// Copy chaindata directory to a temporary directory // Copy chaindata directory to a temporary directory
// Note that we assume the ancient path is a subdirectory // Note that we assume the ancient path is a subdirectory
copyTo := filepath.Join(os.TempDir(), chain) copyTo, err := os.MkdirTemp("", chain+"-chaindata-*")
if err != nil {
return nil, err
}
if err := copyDir(chaindataPath, copyTo); err != nil { if err := copyDir(chaindataPath, copyTo); err != nil {
return nil, err return nil, err
} }
ancientCopy := filepath.Join(copyTo, "ancient")
return &Paths{copyTo, ancientCopy}, nil return &Paths{
copyTo,
filepath.Join(copyTo, "ancient"),
}, nil
} }
func copyDir(src, dest string) error { func copyDir(src, dest string) error {

View File

@ -1,4 +1,4 @@
package chaindata_test package chains_test
import ( import (
"testing" "testing"
@ -6,15 +6,18 @@ 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" "github.com/cerc-io/eth-testing/chains"
) )
func testReadChainData(t *testing.T, data *chaindata.Paths) { func testReadChainData(t *testing.T, data *chains.Paths) {
kvdb, ldberr := rawdb.NewLevelDBDatabase(data.ChainData, 1024, 256, t.Name(), true) edb, err := rawdb.Open(rawdb.OpenOptions{
if ldberr != nil { Directory: data.ChainData,
t.Fatal(ldberr) AncientsDirectory: data.Ancient,
} Namespace: t.Name(),
edb, err := rawdb.NewDatabaseWithFreezer(kvdb, data.Ancient, t.Name(), true) Cache: 1024,
Handles: 256,
ReadOnly: true,
})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -47,9 +50,9 @@ func testReadChainData(t *testing.T, data *chaindata.Paths) {
} }
func TestReadChainData(t *testing.T) { func TestReadChainData(t *testing.T) {
for _, name := range []string{"permerge1", "premerge2", "postmerge1"} { for _, name := range chains.FixtureChains {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
data, err := chaindata.GetFixture(name) data, err := chains.GetFixture(name)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }