ipld-eth-state-snapshot/pkg/snapshot/service_test.go

100 lines
2.3 KiB
Go
Raw Normal View History

2022-01-11 00:59:15 +00:00
package snapshot
import (
"fmt"
"os"
2022-01-11 00:59:15 +00:00
"path/filepath"
"testing"
"github.com/golang/mock/gomock"
2022-01-11 00:59:15 +00:00
ethNode "github.com/ethereum/go-ethereum/statediff/indexer/node"
"github.com/ethereum/go-ethereum/statediff/indexer/postgres"
fixt "github.com/vulcanize/eth-pg-ipfs-state-snapshot/fixture"
"github.com/vulcanize/eth-pg-ipfs-state-snapshot/pkg/snapshot/mock"
2022-01-11 00:59:15 +00:00
)
func testConfig(leveldbpath, ancientdbpath string) *Config {
2022-01-11 05:37:27 +00:00
dbParams := postgres.ConnectionParams{
Name: "snapshot_test",
Hostname: "localhost",
Port: 5432,
User: "tester",
Password: "test_pw",
}
2022-01-11 00:59:26 +00:00
connconfig := postgres.ConnectionConfig{
2022-01-11 00:59:15 +00:00
MaxIdle: 0,
MaxLifetime: 0,
MaxOpen: 4,
}
nodeinfo := ethNode.Info{
ID: "eth_node_id",
ClientName: "eth_client",
GenesisBlock: "X",
NetworkID: "eth_network",
ChainID: 0,
}
return &Config{
2022-01-11 00:59:26 +00:00
DB: &DBConfig{
Node: nodeinfo,
2022-01-11 05:37:27 +00:00
URI: postgres.DbConnectionString(dbParams),
2022-01-11 00:59:26 +00:00
ConnConfig: connconfig,
},
Eth: &EthConfig{
LevelDBPath: leveldbpath,
AncientDBPath: ancientdbpath,
},
2022-01-11 00:59:15 +00:00
}
}
func TestCreateSnapshot(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
fixture_path := filepath.Join(wd, "..", "..", "fixture")
datadir := filepath.Join(fixture_path, "chaindata")
if _, err := os.Stat(datadir); err != nil {
t.Fatal("no chaindata:", err)
}
config := testConfig(datadir, filepath.Join(datadir, "ancient"))
fmt.Printf("config: %+v %+v\n", config.DB, config.Eth)
edb, err := NewLevelDB(config.Eth)
if err != nil {
t.Fatal(err)
}
workers := 8
pub := mock.NewMockPublisher(t)
pub.EXPECT().PublishHeader(gomock.Eq(fixt.PublishHeader))
pub.EXPECT().BeginTx().
Times(workers)
pub.EXPECT().PrepareTxForBatch(gomock.Any(), gomock.Any()).
Times(workers)
pub.EXPECT().PublishStateNode(gomock.Any(), mock.AnyOf(int64(0), int64(1)), gomock.Any()).
Times(workers)
2022-01-16 23:54:55 +00:00
// TODO: fixtures for storage node
// pub.EXPECT().PublishStorageNode(gomock.Eq(fixt.StorageNode), gomock.Eq(int64(0)), gomock.Any())
pub.EXPECT().CommitTx(gomock.Any()).
Times(workers)
2022-01-11 00:59:15 +00:00
service, err := NewSnapshotService(edb, pub)
2022-01-11 00:59:15 +00:00
if err != nil {
t.Fatal(err)
}
params := SnapshotParams{Height: 1, Workers: uint(workers)}
2022-01-11 00:59:15 +00:00
err = service.CreateSnapshot(params)
if err != nil {
t.Fatal(err)
}
// err = service.CreateLatestSnapshot(0)
// if err != nil {
// t.Fatal(err)
// }
}