From e53d79956336f9a0005a0eabf77d68f3b501d82a Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Sun, 24 Sep 2023 14:42:53 +0800 Subject: [PATCH] more refactor --- chaindata/{data => _data}/.gitignore | 0 chaindata/{data => _data}/small/000002.ldb | Bin chaindata/{data => _data}/small/000004.ldb | Bin .../small/ancient/bodies.0000.cdat | 0 .../{data => _data}/small/ancient/bodies.cidx | Bin .../{data => _data}/small/ancient/bodies.meta | 0 .../small/ancient/diffs.0000.rdat | 0 .../{data => _data}/small/ancient/diffs.meta | 0 .../{data => _data}/small/ancient/diffs.ridx | Bin .../small/ancient/hashes.0000.rdat | 0 .../{data => _data}/small/ancient/hashes.meta | 0 .../{data => _data}/small/ancient/hashes.ridx | Bin .../small/ancient/headers.0000.cdat | 0 .../small/ancient/headers.cidx | Bin .../small/ancient/headers.meta | 0 .../small/ancient/receipts.0000.cdat | 0 .../small/ancient/receipts.cidx | Bin .../small/ancient/receipts.meta | 0 chaindata/{data => _data}/small2/000002.ldb | Bin .../small2/ancient/chain/bodies.0000.cdat | 0 .../small2/ancient/chain/bodies.cidx | Bin .../small2/ancient/chain/bodies.meta | 0 .../small2/ancient/chain/diffs.0000.rdat | 0 .../small2/ancient/chain/diffs.meta | 0 .../small2/ancient/chain/diffs.ridx | Bin .../small2/ancient/chain/hashes.0000.rdat | 0 .../small2/ancient/chain/hashes.meta | 0 .../small2/ancient/chain/hashes.ridx | Bin .../small2/ancient/chain/headers.0000.cdat | 0 .../small2/ancient/chain/headers.cidx | Bin .../small2/ancient/chain/headers.meta | 0 .../small2/ancient/chain/receipts.0000.cdat | 0 .../small2/ancient/chain/receipts.cidx | Bin .../small2/ancient/chain/receipts.meta | 0 chaindata/{util => }/chaindata.go | 22 +++++-------- chaindata/chaindata_test.go | 30 +++++++++++------- chaindata/small/chain.go | 10 ++++-- chaindata/small2/chain.go | 10 ++++-- 38 files changed, 43 insertions(+), 29 deletions(-) rename chaindata/{data => _data}/.gitignore (100%) rename chaindata/{data => _data}/small/000002.ldb (100%) rename chaindata/{data => _data}/small/000004.ldb (100%) rename chaindata/{data => _data}/small/ancient/bodies.0000.cdat (100%) rename chaindata/{data => _data}/small/ancient/bodies.cidx (100%) rename chaindata/{data => _data}/small/ancient/bodies.meta (100%) rename chaindata/{data => _data}/small/ancient/diffs.0000.rdat (100%) rename chaindata/{data => _data}/small/ancient/diffs.meta (100%) rename chaindata/{data => _data}/small/ancient/diffs.ridx (100%) rename chaindata/{data => _data}/small/ancient/hashes.0000.rdat (100%) rename chaindata/{data => _data}/small/ancient/hashes.meta (100%) rename chaindata/{data => _data}/small/ancient/hashes.ridx (100%) rename chaindata/{data => _data}/small/ancient/headers.0000.cdat (100%) rename chaindata/{data => _data}/small/ancient/headers.cidx (100%) rename chaindata/{data => _data}/small/ancient/headers.meta (100%) rename chaindata/{data => _data}/small/ancient/receipts.0000.cdat (100%) rename chaindata/{data => _data}/small/ancient/receipts.cidx (100%) rename chaindata/{data => _data}/small/ancient/receipts.meta (100%) rename chaindata/{data => _data}/small2/000002.ldb (100%) rename chaindata/{data => _data}/small2/ancient/chain/bodies.0000.cdat (100%) rename chaindata/{data => _data}/small2/ancient/chain/bodies.cidx (100%) rename chaindata/{data => _data}/small2/ancient/chain/bodies.meta (100%) rename chaindata/{data => _data}/small2/ancient/chain/diffs.0000.rdat (100%) rename chaindata/{data => _data}/small2/ancient/chain/diffs.meta (100%) rename chaindata/{data => _data}/small2/ancient/chain/diffs.ridx (100%) rename chaindata/{data => _data}/small2/ancient/chain/hashes.0000.rdat (100%) rename chaindata/{data => _data}/small2/ancient/chain/hashes.meta (100%) rename chaindata/{data => _data}/small2/ancient/chain/hashes.ridx (100%) rename chaindata/{data => _data}/small2/ancient/chain/headers.0000.cdat (100%) rename chaindata/{data => _data}/small2/ancient/chain/headers.cidx (100%) rename chaindata/{data => _data}/small2/ancient/chain/headers.meta (100%) rename chaindata/{data => _data}/small2/ancient/chain/receipts.0000.cdat (100%) rename chaindata/{data => _data}/small2/ancient/chain/receipts.cidx (100%) rename chaindata/{data => _data}/small2/ancient/chain/receipts.meta (100%) rename chaindata/{util => }/chaindata.go (54%) diff --git a/chaindata/data/.gitignore b/chaindata/_data/.gitignore similarity index 100% rename from chaindata/data/.gitignore rename to chaindata/_data/.gitignore diff --git a/chaindata/data/small/000002.ldb b/chaindata/_data/small/000002.ldb similarity index 100% rename from chaindata/data/small/000002.ldb rename to chaindata/_data/small/000002.ldb diff --git a/chaindata/data/small/000004.ldb b/chaindata/_data/small/000004.ldb similarity index 100% rename from chaindata/data/small/000004.ldb rename to chaindata/_data/small/000004.ldb diff --git a/chaindata/data/small/ancient/bodies.0000.cdat b/chaindata/_data/small/ancient/bodies.0000.cdat similarity index 100% rename from chaindata/data/small/ancient/bodies.0000.cdat rename to chaindata/_data/small/ancient/bodies.0000.cdat diff --git a/chaindata/data/small/ancient/bodies.cidx b/chaindata/_data/small/ancient/bodies.cidx similarity index 100% rename from chaindata/data/small/ancient/bodies.cidx rename to chaindata/_data/small/ancient/bodies.cidx diff --git a/chaindata/data/small/ancient/bodies.meta b/chaindata/_data/small/ancient/bodies.meta similarity index 100% rename from chaindata/data/small/ancient/bodies.meta rename to chaindata/_data/small/ancient/bodies.meta diff --git a/chaindata/data/small/ancient/diffs.0000.rdat b/chaindata/_data/small/ancient/diffs.0000.rdat similarity index 100% rename from chaindata/data/small/ancient/diffs.0000.rdat rename to chaindata/_data/small/ancient/diffs.0000.rdat diff --git a/chaindata/data/small/ancient/diffs.meta b/chaindata/_data/small/ancient/diffs.meta similarity index 100% rename from chaindata/data/small/ancient/diffs.meta rename to chaindata/_data/small/ancient/diffs.meta diff --git a/chaindata/data/small/ancient/diffs.ridx b/chaindata/_data/small/ancient/diffs.ridx similarity index 100% rename from chaindata/data/small/ancient/diffs.ridx rename to chaindata/_data/small/ancient/diffs.ridx diff --git a/chaindata/data/small/ancient/hashes.0000.rdat b/chaindata/_data/small/ancient/hashes.0000.rdat similarity index 100% rename from chaindata/data/small/ancient/hashes.0000.rdat rename to chaindata/_data/small/ancient/hashes.0000.rdat diff --git a/chaindata/data/small/ancient/hashes.meta b/chaindata/_data/small/ancient/hashes.meta similarity index 100% rename from chaindata/data/small/ancient/hashes.meta rename to chaindata/_data/small/ancient/hashes.meta diff --git a/chaindata/data/small/ancient/hashes.ridx b/chaindata/_data/small/ancient/hashes.ridx similarity index 100% rename from chaindata/data/small/ancient/hashes.ridx rename to chaindata/_data/small/ancient/hashes.ridx diff --git a/chaindata/data/small/ancient/headers.0000.cdat b/chaindata/_data/small/ancient/headers.0000.cdat similarity index 100% rename from chaindata/data/small/ancient/headers.0000.cdat rename to chaindata/_data/small/ancient/headers.0000.cdat diff --git a/chaindata/data/small/ancient/headers.cidx b/chaindata/_data/small/ancient/headers.cidx similarity index 100% rename from chaindata/data/small/ancient/headers.cidx rename to chaindata/_data/small/ancient/headers.cidx diff --git a/chaindata/data/small/ancient/headers.meta b/chaindata/_data/small/ancient/headers.meta similarity index 100% rename from chaindata/data/small/ancient/headers.meta rename to chaindata/_data/small/ancient/headers.meta diff --git a/chaindata/data/small/ancient/receipts.0000.cdat b/chaindata/_data/small/ancient/receipts.0000.cdat similarity index 100% rename from chaindata/data/small/ancient/receipts.0000.cdat rename to chaindata/_data/small/ancient/receipts.0000.cdat diff --git a/chaindata/data/small/ancient/receipts.cidx b/chaindata/_data/small/ancient/receipts.cidx similarity index 100% rename from chaindata/data/small/ancient/receipts.cidx rename to chaindata/_data/small/ancient/receipts.cidx diff --git a/chaindata/data/small/ancient/receipts.meta b/chaindata/_data/small/ancient/receipts.meta similarity index 100% rename from chaindata/data/small/ancient/receipts.meta rename to chaindata/_data/small/ancient/receipts.meta diff --git a/chaindata/data/small2/000002.ldb b/chaindata/_data/small2/000002.ldb similarity index 100% rename from chaindata/data/small2/000002.ldb rename to chaindata/_data/small2/000002.ldb diff --git a/chaindata/data/small2/ancient/chain/bodies.0000.cdat b/chaindata/_data/small2/ancient/chain/bodies.0000.cdat similarity index 100% rename from chaindata/data/small2/ancient/chain/bodies.0000.cdat rename to chaindata/_data/small2/ancient/chain/bodies.0000.cdat diff --git a/chaindata/data/small2/ancient/chain/bodies.cidx b/chaindata/_data/small2/ancient/chain/bodies.cidx similarity index 100% rename from chaindata/data/small2/ancient/chain/bodies.cidx rename to chaindata/_data/small2/ancient/chain/bodies.cidx diff --git a/chaindata/data/small2/ancient/chain/bodies.meta b/chaindata/_data/small2/ancient/chain/bodies.meta similarity index 100% rename from chaindata/data/small2/ancient/chain/bodies.meta rename to chaindata/_data/small2/ancient/chain/bodies.meta diff --git a/chaindata/data/small2/ancient/chain/diffs.0000.rdat b/chaindata/_data/small2/ancient/chain/diffs.0000.rdat similarity index 100% rename from chaindata/data/small2/ancient/chain/diffs.0000.rdat rename to chaindata/_data/small2/ancient/chain/diffs.0000.rdat diff --git a/chaindata/data/small2/ancient/chain/diffs.meta b/chaindata/_data/small2/ancient/chain/diffs.meta similarity index 100% rename from chaindata/data/small2/ancient/chain/diffs.meta rename to chaindata/_data/small2/ancient/chain/diffs.meta diff --git a/chaindata/data/small2/ancient/chain/diffs.ridx b/chaindata/_data/small2/ancient/chain/diffs.ridx similarity index 100% rename from chaindata/data/small2/ancient/chain/diffs.ridx rename to chaindata/_data/small2/ancient/chain/diffs.ridx diff --git a/chaindata/data/small2/ancient/chain/hashes.0000.rdat b/chaindata/_data/small2/ancient/chain/hashes.0000.rdat similarity index 100% rename from chaindata/data/small2/ancient/chain/hashes.0000.rdat rename to chaindata/_data/small2/ancient/chain/hashes.0000.rdat diff --git a/chaindata/data/small2/ancient/chain/hashes.meta b/chaindata/_data/small2/ancient/chain/hashes.meta similarity index 100% rename from chaindata/data/small2/ancient/chain/hashes.meta rename to chaindata/_data/small2/ancient/chain/hashes.meta diff --git a/chaindata/data/small2/ancient/chain/hashes.ridx b/chaindata/_data/small2/ancient/chain/hashes.ridx similarity index 100% rename from chaindata/data/small2/ancient/chain/hashes.ridx rename to chaindata/_data/small2/ancient/chain/hashes.ridx diff --git a/chaindata/data/small2/ancient/chain/headers.0000.cdat b/chaindata/_data/small2/ancient/chain/headers.0000.cdat similarity index 100% rename from chaindata/data/small2/ancient/chain/headers.0000.cdat rename to chaindata/_data/small2/ancient/chain/headers.0000.cdat diff --git a/chaindata/data/small2/ancient/chain/headers.cidx b/chaindata/_data/small2/ancient/chain/headers.cidx similarity index 100% rename from chaindata/data/small2/ancient/chain/headers.cidx rename to chaindata/_data/small2/ancient/chain/headers.cidx diff --git a/chaindata/data/small2/ancient/chain/headers.meta b/chaindata/_data/small2/ancient/chain/headers.meta similarity index 100% rename from chaindata/data/small2/ancient/chain/headers.meta rename to chaindata/_data/small2/ancient/chain/headers.meta diff --git a/chaindata/data/small2/ancient/chain/receipts.0000.cdat b/chaindata/_data/small2/ancient/chain/receipts.0000.cdat similarity index 100% rename from chaindata/data/small2/ancient/chain/receipts.0000.cdat rename to chaindata/_data/small2/ancient/chain/receipts.0000.cdat diff --git a/chaindata/data/small2/ancient/chain/receipts.cidx b/chaindata/_data/small2/ancient/chain/receipts.cidx similarity index 100% rename from chaindata/data/small2/ancient/chain/receipts.cidx rename to chaindata/_data/small2/ancient/chain/receipts.cidx diff --git a/chaindata/data/small2/ancient/chain/receipts.meta b/chaindata/_data/small2/ancient/chain/receipts.meta similarity index 100% rename from chaindata/data/small2/ancient/chain/receipts.meta rename to chaindata/_data/small2/ancient/chain/receipts.meta diff --git a/chaindata/util/chaindata.go b/chaindata/chaindata.go similarity index 54% rename from chaindata/util/chaindata.go rename to chaindata/chaindata.go index 7f1d5b0..5e7f1ce 100644 --- a/chaindata/util/chaindata.go +++ b/chaindata/chaindata.go @@ -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 } diff --git a/chaindata/chaindata_test.go b/chaindata/chaindata_test.go index 0583364..78cf0e4 100644 --- a/chaindata/chaindata_test.go +++ b/chaindata/chaindata_test.go @@ -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) + }) } } diff --git a/chaindata/small/chain.go b/chaindata/small/chain.go index 75a891b..b86234e 100644 --- a/chaindata/small/chain.go +++ b/chaindata/small/chain.go @@ -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) + } +} diff --git a/chaindata/small2/chain.go b/chaindata/small2/chain.go index 1e5f392..2e3b929 100644 --- a/chaindata/small2/chain.go +++ b/chaindata/small2/chain.go @@ -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) + } +}