rf config, mock publisher

This commit is contained in:
Roy Crihfield 2022-01-10 18:59:26 -06:00
parent 802a5908df
commit 1fab8ac1c8
4 changed files with 55 additions and 29 deletions

View File

@ -40,7 +40,11 @@ var stateSnapshotCmd = &cobra.Command{
func stateSnapshot() {
snapConfig := &snapshot.Config{}
snapConfig.Init()
snapshotService, err := snapshot.NewSnapshotService(snapConfig)
pgDB, err := snapshot.NewPostgresDB(snapConfig.DB)
if err != nil {
logWithCommand.Fatal(err)
}
snapshotService, err := snapshot.NewSnapshotService(snapConfig.Eth, snapshot.NewPublisher(pgDB))
if err != nil {
logWithCommand.Fatal(err)
}

View File

@ -30,37 +30,47 @@ const (
LVL_DB_PATH = "LVL_DB_PATH"
)
// Config is config parameters for DB.
type Config struct {
// DBConfig is config parameters for DB.
type DBConfig struct {
Node ethNode.Info
URI string
ConnConfig postgres.ConnectionConfig
}
type EthConfig struct {
LevelDBPath string
AncientDBPath string
Node ethNode.Info
connectionURI string
DBConfig postgres.ConnectionConfig
}
type Config struct {
DB *DBConfig
Eth *EthConfig
}
// Init Initialises config
func (c *Config) Init() {
c.dbInit()
viper.BindEnv("leveldb.path", LVL_DB_PATH)
c.DB.dbInit()
viper.BindEnv("ethereum.nodeID", ETH_NODE_ID)
viper.BindEnv("ethereum.clientName", ETH_CLIENT_NAME)
viper.BindEnv("ethereum.genesisBlock", ETH_GENESIS_BLOCK)
viper.BindEnv("ethereum.networkID", ETH_NETWORK_ID)
viper.BindEnv("leveldb.ancient", ANCIENT_DB_PATH)
c.Node = ethNode.Info{
c.DB.Node = ethNode.Info{
ID: viper.GetString("ethereum.nodeID"),
ClientName: viper.GetString("ethereum.clientName"),
GenesisBlock: viper.GetString("ethereum.genesisBlock"),
NetworkID: viper.GetString("ethereum.networkID"),
ChainID: viper.GetUint64("ethereum.chainID"),
}
c.LevelDBPath = viper.GetString("leveldb.path")
c.AncientDBPath = viper.GetString("leveldb.ancient")
viper.BindEnv("leveldb.ancient", ANCIENT_DB_PATH)
viper.BindEnv("leveldb.path", LVL_DB_PATH)
c.Eth.AncientDBPath = viper.GetString("leveldb.ancient")
c.Eth.LevelDBPath = viper.GetString("leveldb.path")
}
func (c *Config) dbInit() {
func (c *DBConfig) dbInit() {
viper.BindEnv("database.name", postgres.DATABASE_NAME)
viper.BindEnv("database.hostname", postgres.DATABASE_HOSTNAME)
viper.BindEnv("database.port", postgres.DATABASE_PORT)
@ -78,9 +88,9 @@ func (c *Config) dbInit() {
dbParams.User = viper.GetString("database.user")
dbParams.Password = viper.GetString("database.password")
c.connectionURI = postgres.DbConnectionString(dbParams)
// DB config
c.DBConfig.MaxIdle = viper.GetInt("database.maxIdle")
c.DBConfig.MaxOpen = viper.GetInt("database.maxOpen")
c.DBConfig.MaxLifetime = viper.GetInt("database.maxLifetime")
c.URI = postgres.DbConnectionString(dbParams)
// Connection config
c.ConnConfig.MaxIdle = viper.GetInt("database.maxIdle")
c.ConnConfig.MaxOpen = viper.GetInt("database.maxOpen")
c.ConnConfig.MaxLifetime = viper.GetInt("database.maxLifetime")
}

View File

@ -55,13 +55,16 @@ type Service struct {
maxBatchSize uint
}
// NewSnapshotService creates Service.
func NewSnapshotService(con *Config) (*Service, error) {
pgDB, err := postgres.NewDB(con.connectionURI, con.DBConfig, con.Node)
func NewPostgresDB(con *DBConfig) (*postgres.DB, error) {
pgDB, err := postgres.NewDB(con.URI, con.ConnConfig, con.Node)
if err != nil {
return nil, err
}
return pgDB, nil
}
// NewSnapshotService creates Service.
func NewSnapshotService(con *EthConfig, pub *Publisher) (*Service, error) {
edb, err := rawdb.NewLevelDBDatabaseWithFreezer(con.LevelDBPath, 1024, 256, con.AncientDBPath, "eth-pg-ipfs-state-snapshot", false)
if err != nil {
return nil, err
@ -70,7 +73,7 @@ func NewSnapshotService(con *Config) (*Service, error) {
return &Service{
ethDB: edb,
stateDB: state.NewDatabase(edb),
ipfsPublisher: NewPublisher(pgDB),
ipfsPublisher: pub,
maxBatchSize: defaultBatchSize,
}, nil
}

View File

@ -16,7 +16,7 @@ func testConfig(leveldbpath, ancientdbpath string) *Config {
dbParams.User = "tester"
dbParams.Password = "test_pw"
uri := postgres.DbConnectionString(dbParams)
dbconfig := postgres.ConnectionConfig{
connconfig := postgres.ConnectionConfig{
MaxIdle: 0,
MaxLifetime: 0,
MaxOpen: 4,
@ -30,14 +30,22 @@ func testConfig(leveldbpath, ancientdbpath string) *Config {
}
return &Config{
LevelDBPath: leveldbpath,
AncientDBPath: ancientdbpath,
Node: nodeinfo,
connectionURI: uri,
DBConfig: dbconfig,
DB: &DBConfig{
Node: nodeinfo,
URI: uri,
ConnConfig: connconfig,
},
Eth: &EthConfig{
LevelDBPath: leveldbpath,
AncientDBPath: ancientdbpath,
},
}
}
func NewMockPublisher() *Publisher {
return nil
}
func TestCreateSnapshot(t *testing.T) {
datadir := t.TempDir()
config := testConfig(
@ -45,7 +53,8 @@ func TestCreateSnapshot(t *testing.T) {
filepath.Join(datadir, "ancient"),
)
service, err := NewSnapshotService(config)
pub := NewMockPublisher()
service, err := NewSnapshotService(config.Eth, pub)
if err != nil {
t.Fatal(err)
}