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

132 lines
3.9 KiB
Go
Raw Normal View History

2020-07-01 18:44:59 +00:00
// Copyright © 2020 Vulcanize, Inc
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package snapshot
import (
"time"
"github.com/ethereum/go-ethereum/statediff/indexer/database/sql/postgres"
2021-12-14 06:50:19 +00:00
ethNode "github.com/ethereum/go-ethereum/statediff/indexer/node"
2020-07-01 18:44:59 +00:00
"github.com/spf13/viper"
)
const (
2021-12-15 07:23:18 +00:00
ANCIENT_DB_PATH = "ANCIENT_DB_PATH"
ETH_CLIENT_NAME = "ETH_CLIENT_NAME"
ETH_GENESIS_BLOCK = "ETH_GENESIS_BLOCK"
ETH_NETWORK_ID = "ETH_NETWORK_ID"
ETH_NODE_ID = "ETH_NODE_ID"
LVL_DB_PATH = "LVL_DB_PATH"
2020-07-01 18:44:59 +00:00
)
// SnapshotMode specifies the snapshot data output method
type SnapshotMode string
const (
PgSnapshot SnapshotMode = "postgres"
FileSnapshot SnapshotMode = "file"
)
2022-01-26 17:30:28 +00:00
// Config contains params for both databases the service uses
type Config struct {
Eth *EthConfig
DB *DBConfig
File *FileConfig
}
// EthConfig is config parameters for the chain.
type EthConfig struct {
LevelDBPath string
AncientDBPath string
NodeInfo ethNode.Info
2022-01-26 17:30:28 +00:00
}
2022-01-11 00:59:26 +00:00
// DBConfig is config parameters for DB.
type DBConfig struct {
URI string
ConnConfig postgres.Config
2022-01-11 00:59:26 +00:00
}
type FileConfig struct {
OutputDir string
2022-01-11 00:59:26 +00:00
}
2022-01-26 17:30:28 +00:00
func NewConfig() *Config {
ret := &Config{
&EthConfig{},
&DBConfig{},
&FileConfig{},
}
2022-01-26 17:30:28 +00:00
ret.Init()
return ret
2020-07-01 18:44:59 +00:00
}
2021-12-14 06:50:19 +00:00
// Init Initialises config
2020-07-01 18:44:59 +00:00
func (c *Config) Init() {
2021-12-15 07:23:18 +00:00
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)
c.Eth.NodeInfo = ethNode.Info{
2020-07-01 18:44:59 +00:00
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"),
2020-07-01 18:44:59 +00:00
}
2022-01-11 00:59:26 +00:00
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")
c.DB.Init()
c.File.Init()
2020-07-01 18:44:59 +00:00
}
func (c *DBConfig) Init() {
viper.BindEnv("database.name", "DATABASE_NAME")
viper.BindEnv("database.hostname", "DATABASE_HOSTNAME")
viper.BindEnv("database.port", "DATABASE_PORT")
viper.BindEnv("database.user", "DATABASE_USER")
viper.BindEnv("database.password", "DATABASE_PASSWORD")
viper.BindEnv("database.maxIdle", "DATABASE_MAX_IDLE_CONNECTIONS")
viper.BindEnv("database.maxOpen", "DATABASE_MAX_OPEN_CONNECTIONS")
viper.BindEnv("database.maxLifetime", "DATABASE_MAX_CONN_LIFETIME")
dbParams := postgres.Config{}
// DB params
dbParams.DatabaseName = viper.GetString("database.name")
dbParams.Hostname = viper.GetString("database.hostname")
dbParams.Port = viper.GetInt("database.port")
dbParams.Username = viper.GetString("database.user")
dbParams.Password = viper.GetString("database.password")
2022-01-11 00:59:26 +00:00
// Connection config
dbParams.MaxIdle = viper.GetInt("database.maxIdle")
dbParams.MaxConns = viper.GetInt("database.maxOpen")
dbParams.MaxConnLifetime = time.Duration(viper.GetInt("database.maxLifetime")) * time.Second
c.ConnConfig = dbParams
c.URI = dbParams.DbConnectionString()
}
func (c *FileConfig) Init() {
viper.BindEnv("file.outputDir", "FILE_OUTPUT_DIR")
c.OutputDir = viper.GetString("file.outputDir")
}