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 (
|
2021-12-14 06:50:19 +00:00
|
|
|
ethNode "github.com/ethereum/go-ethereum/statediff/indexer/node"
|
2021-12-13 15:01:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/statediff/indexer/postgres"
|
2020-07-01 18:44:59 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-12-14 06:50:19 +00:00
|
|
|
ancientDBPath = "ANCIENT_DB_PATH"
|
2021-12-13 15:01:32 +00:00
|
|
|
ethClientName = "ETH_CLIENT_NAME"
|
|
|
|
ethGenesisBlock = "ETH_GENESIS_BLOCK"
|
2021-12-14 06:50:19 +00:00
|
|
|
ethNetworkID = "ETH_NETWORK_ID"
|
|
|
|
ethNodeID = "ETH_NODE_ID"
|
|
|
|
lvlDBPath = "LVL_DB_PATH"
|
2020-07-01 18:44:59 +00:00
|
|
|
)
|
|
|
|
|
2021-12-14 06:50:19 +00:00
|
|
|
// Config is config parameters for DB.
|
2020-07-01 18:44:59 +00:00
|
|
|
type Config struct {
|
2020-08-03 15:46:35 +00:00
|
|
|
LevelDBPath string
|
|
|
|
AncientDBPath string
|
2021-12-14 06:50:19 +00:00
|
|
|
Node ethNode.Info
|
2021-12-13 15:01:32 +00:00
|
|
|
connectionURI string
|
|
|
|
DBConfig postgres.ConnectionConfig
|
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-13 15:01:32 +00:00
|
|
|
c.dbInit()
|
2021-12-14 06:50:19 +00:00
|
|
|
viper.BindEnv("leveldb.path", lvlDBPath)
|
|
|
|
viper.BindEnv("ethereum.nodeID", ethNodeID)
|
2021-12-13 15:01:32 +00:00
|
|
|
viper.BindEnv("ethereum.clientName", ethClientName)
|
|
|
|
viper.BindEnv("ethereum.genesisBlock", ethGenesisBlock)
|
2021-12-14 06:50:19 +00:00
|
|
|
viper.BindEnv("ethereum.networkID", ethNetworkID)
|
|
|
|
viper.BindEnv("leveldb.ancient", ancientDBPath)
|
2021-12-13 15:01:32 +00:00
|
|
|
|
2021-12-14 06:50:19 +00:00
|
|
|
c.Node = 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"),
|
2021-12-13 15:01:32 +00:00
|
|
|
ChainID: viper.GetUint64("ethereum.chainID"),
|
2020-07-01 18:44:59 +00:00
|
|
|
}
|
|
|
|
c.LevelDBPath = viper.GetString("leveldb.path")
|
2020-08-03 15:46:35 +00:00
|
|
|
c.AncientDBPath = viper.GetString("leveldb.ancient")
|
2020-07-01 18:44:59 +00:00
|
|
|
}
|
2021-12-13 15:01:32 +00:00
|
|
|
|
|
|
|
func (c *Config) dbInit() {
|
|
|
|
viper.BindEnv("database.name", postgres.DATABASE_NAME)
|
|
|
|
viper.BindEnv("database.hostname", postgres.DATABASE_HOSTNAME)
|
|
|
|
viper.BindEnv("database.port", postgres.DATABASE_PORT)
|
|
|
|
viper.BindEnv("database.user", postgres.DATABASE_USER)
|
|
|
|
viper.BindEnv("database.password", postgres.DATABASE_PASSWORD)
|
|
|
|
viper.BindEnv("database.maxIdle", postgres.DATABASE_MAX_IDLE_CONNECTIONS)
|
|
|
|
viper.BindEnv("database.maxOpen", postgres.DATABASE_MAX_OPEN_CONNECTIONS)
|
|
|
|
viper.BindEnv("database.maxLifetime", postgres.DATABASE_MAX_CONN_LIFETIME)
|
|
|
|
|
|
|
|
dbParams := postgres.ConnectionParams{}
|
|
|
|
// DB params
|
|
|
|
dbParams.Name = viper.GetString("database.name")
|
|
|
|
dbParams.Hostname = viper.GetString("database.hostname")
|
|
|
|
dbParams.Port = viper.GetInt("database.port")
|
|
|
|
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")
|
|
|
|
}
|