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() { func stateSnapshot() {
snapConfig := &snapshot.Config{} snapConfig := &snapshot.Config{}
snapConfig.Init() 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 { if err != nil {
logWithCommand.Fatal(err) logWithCommand.Fatal(err)
} }

View File

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

View File

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

View File

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