forked from cerc-io/ipld-eth-server
combine resync and supernode configs; load config params from env variables if available; allow config params to be passed in as cli flags
This commit is contained in:
+27
-1
@@ -16,7 +16,20 @@
|
||||
|
||||
package config
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// Env variables
|
||||
const (
|
||||
DATABASE_NAME = "DATABASE_NAME"
|
||||
DATABASE_HOSTNAME = "DATABASE_HOSTNAME"
|
||||
DATABASE_PORT = "DATABASE_PORT"
|
||||
DATABASE_USER = "DATABASE_USER"
|
||||
DATABASE_PASSWORD = "DATABASE_PASSWORD"
|
||||
)
|
||||
|
||||
type Database struct {
|
||||
Hostname string
|
||||
@@ -37,3 +50,16 @@ func DbConnectionString(dbConfig Database) string {
|
||||
}
|
||||
return fmt.Sprintf("postgresql://%s:%d/%s?sslmode=disable", dbConfig.Hostname, dbConfig.Port, dbConfig.Name)
|
||||
}
|
||||
|
||||
func (d *Database) 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)
|
||||
d.Name = viper.GetString("database.name")
|
||||
d.Hostname = viper.GetString("database.hostname")
|
||||
d.Port = viper.GetInt("database.port")
|
||||
d.User = viper.GetString("database.user")
|
||||
d.Password = viper.GetString("database.password")
|
||||
}
|
||||
|
||||
+58
-66
@@ -22,19 +22,30 @@ import (
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcd/rpcclient"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/config"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/eth/client"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/eth/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/eth/node"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/postgres"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/super_node/shared"
|
||||
"github.com/vulcanize/vulcanizedb/utils"
|
||||
)
|
||||
|
||||
// Env variables
|
||||
const (
|
||||
SUPERNODE_CHAIN = "SUPERNODE_CHAIN"
|
||||
SUPERNODE_SYNC = "SUPERNODE_SYNC"
|
||||
SUPERNODE_WORKERS = "SUPERNODE_WORKERS"
|
||||
SUPERNODE_SERVER = "SUPERNODE_SERVER"
|
||||
SUPERNODE_WS_PATH = "SUPERNODE_WS_PATH"
|
||||
SUPERNODE_IPC_PATH = "SUPERNODE_IPC_PATH"
|
||||
SUPERNODE_HTTP_PATH = "SUPERNODE_HTTP_PATH"
|
||||
SUPERNODE_BACKFILL = "SUPERNODE_BACKFILL"
|
||||
SUPERNODE_FREQUENCY = "SUPERNODE_FREQUENCY"
|
||||
SUPERNODE_BATCH_SIZE = "SUPERNODE_BATCH_SIZE"
|
||||
SUPERNODE_BATCH_NUMBER = "SUPERNODE_BATCH_NUMBER"
|
||||
)
|
||||
|
||||
// Config struct
|
||||
type Config struct {
|
||||
// Ubiquitous fields
|
||||
@@ -67,21 +78,27 @@ func NewSuperNodeConfig() (*Config, error) {
|
||||
c := new(Config)
|
||||
var err error
|
||||
|
||||
viper.BindEnv("superNode.chain", SUPERNODE_CHAIN)
|
||||
viper.BindEnv("superNode.sync", SUPERNODE_SYNC)
|
||||
viper.BindEnv("superNode.workers", SUPERNODE_WORKERS)
|
||||
viper.BindEnv("ethereum.wsPath", shared.ETH_WS_PATH)
|
||||
viper.BindEnv("bitcoin.wsPath", shared.BTC_WS_PATH)
|
||||
viper.BindEnv("superNode.server", SUPERNODE_SERVER)
|
||||
viper.BindEnv("superNode.wsPath", SUPERNODE_WS_PATH)
|
||||
viper.BindEnv("superNode.ipcPath", SUPERNODE_IPC_PATH)
|
||||
viper.BindEnv("superNode.httpPath", SUPERNODE_HTTP_PATH)
|
||||
viper.BindEnv("superNode.backFill", SUPERNODE_BACKFILL)
|
||||
|
||||
chain := viper.GetString("superNode.chain")
|
||||
c.Chain, err = shared.NewChainType(chain)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ipfsPath := viper.GetString("superNode.ipfsPath")
|
||||
if ipfsPath == "" {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ipfsPath = filepath.Join(home, ".ipfs")
|
||||
c.IPFSPath, err = shared.GetIPFSPath()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.IPFSPath = ipfsPath
|
||||
|
||||
c.Sync = viper.GetBool("superNode.sync")
|
||||
if c.Sync {
|
||||
@@ -92,25 +109,14 @@ func NewSuperNodeConfig() (*Config, error) {
|
||||
c.Workers = workers
|
||||
switch c.Chain {
|
||||
case shared.Ethereum:
|
||||
c.NodeInfo, c.WSClient, err = getEthNodeAndClient(fmt.Sprintf("ws://%s", viper.GetString("ethereum.wsPath")))
|
||||
ethWS := viper.GetString("ethereum.wsPath")
|
||||
c.NodeInfo, c.WSClient, err = shared.GetEthNodeAndClient(fmt.Sprintf("ws://%s", ethWS))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case shared.Bitcoin:
|
||||
c.NodeInfo = core.Node{
|
||||
ID: viper.GetString("bitcoin.nodeID"),
|
||||
ClientName: viper.GetString("bitcoin.clientName"),
|
||||
GenesisBlock: viper.GetString("bitcoin.genesisBlock"),
|
||||
NetworkID: viper.GetString("bitcoin.networkID"),
|
||||
}
|
||||
// For bitcoin we load in node info from the config because there is no RPC endpoint to retrieve this from the node
|
||||
c.WSClient = &rpcclient.ConnConfig{
|
||||
Host: viper.GetString("bitcoin.wsPath"),
|
||||
HTTPPostMode: true, // Bitcoin core only supports HTTP POST mode
|
||||
DisableTLS: true, // Bitcoin core does not provide TLS by default
|
||||
Pass: viper.GetString("bitcoin.pass"),
|
||||
User: viper.GetString("bitcoin.user"),
|
||||
}
|
||||
btcWS := viper.GetString("bitcoin.wsPath")
|
||||
c.NodeInfo, c.WSClient = shared.GetBtcNodeAndClient(btcWS)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,47 +143,43 @@ func NewSuperNodeConfig() (*Config, error) {
|
||||
c.HTTPEndpoint = httpPath
|
||||
}
|
||||
|
||||
c.DBConfig = config.Database{
|
||||
Name: viper.GetString("database.name"),
|
||||
Hostname: viper.GetString("database.hostname"),
|
||||
Port: viper.GetInt("database.port"),
|
||||
User: viper.GetString("database.user"),
|
||||
Password: viper.GetString("database.password"),
|
||||
}
|
||||
|
||||
db := utils.LoadPostgres(c.DBConfig, c.NodeInfo)
|
||||
c.DB = &db
|
||||
c.Quit = make(chan bool)
|
||||
if viper.GetBool("superNode.backFill") {
|
||||
if err := c.BackFillFields(chain); err != nil {
|
||||
c.BackFill = viper.GetBool("superNode.backFill")
|
||||
if c.BackFill {
|
||||
if err := c.BackFillFields(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
c.DBConfig.Init()
|
||||
db := utils.LoadPostgres(c.DBConfig, c.NodeInfo)
|
||||
c.DB = &db
|
||||
c.Quit = make(chan bool)
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// BackFillFields is used to fill in the BackFill fields of the config
|
||||
func (sn *Config) BackFillFields(chain string) error {
|
||||
sn.BackFill = true
|
||||
var httpClient interface{}
|
||||
func (c *Config) BackFillFields() error {
|
||||
var err error
|
||||
switch sn.Chain {
|
||||
|
||||
viper.BindEnv("ethereum.httpPath", shared.ETH_HTTP_PATH)
|
||||
viper.BindEnv("bitcoin.httpPath", shared.BTC_HTTP_PATH)
|
||||
viper.BindEnv("superNode.frequency", SUPERNODE_FREQUENCY)
|
||||
viper.BindEnv("superNode.batchSize", SUPERNODE_BATCH_SIZE)
|
||||
viper.BindEnv("superNode.batchNumber", SUPERNODE_BATCH_NUMBER)
|
||||
|
||||
switch c.Chain {
|
||||
case shared.Ethereum:
|
||||
_, httpClient, err = getEthNodeAndClient(fmt.Sprintf("http://%s", viper.GetString("ethereum.httpPath")))
|
||||
ethHTTP := viper.GetString("ethereum.httpPath")
|
||||
c.NodeInfo, c.HTTPClient, err = shared.GetEthNodeAndClient(fmt.Sprintf("http://%s", ethHTTP))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case shared.Bitcoin:
|
||||
httpClient = &rpcclient.ConnConfig{
|
||||
Host: viper.GetString("bitcoin.httpPath"),
|
||||
HTTPPostMode: true, // Bitcoin core only supports HTTP POST mode
|
||||
DisableTLS: true, // Bitcoin core does not provide TLS by default
|
||||
Pass: viper.GetString("bitcoin.pass"),
|
||||
User: viper.GetString("bitcoin.user"),
|
||||
}
|
||||
btcHTTP := viper.GetString("bitcoin.httpPath")
|
||||
c.NodeInfo, c.HTTPClient = shared.GetBtcNodeAndClient(btcHTTP)
|
||||
}
|
||||
sn.HTTPClient = httpClient
|
||||
|
||||
freq := viper.GetInt("superNode.frequency")
|
||||
var frequency time.Duration
|
||||
if freq <= 0 {
|
||||
@@ -185,18 +187,8 @@ func (sn *Config) BackFillFields(chain string) error {
|
||||
} else {
|
||||
frequency = time.Second * time.Duration(freq)
|
||||
}
|
||||
sn.Frequency = frequency
|
||||
sn.BatchSize = uint64(viper.GetInt64("superNode.batchSize"))
|
||||
sn.BatchNumber = uint64(viper.GetInt64("superNode.batchNumber"))
|
||||
c.Frequency = frequency
|
||||
c.BatchSize = uint64(viper.GetInt64("superNode.batchSize"))
|
||||
c.BatchNumber = uint64(viper.GetInt64("superNode.batchNumber"))
|
||||
return nil
|
||||
}
|
||||
|
||||
func getEthNodeAndClient(path string) (core.Node, interface{}, error) {
|
||||
rawRPCClient, err := rpc.Dial(path)
|
||||
if err != nil {
|
||||
return core.Node{}, nil, err
|
||||
}
|
||||
rpcClient := client.NewRPCClient(rawRPCClient, path)
|
||||
vdbNode := node.MakeNode(rpcClient)
|
||||
return vdbNode, rpcClient, nil
|
||||
}
|
||||
|
||||
@@ -18,22 +18,27 @@ package resync
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/btcsuite/btcd/rpcclient"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/config"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/eth/client"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/eth/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/eth/node"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/postgres"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/super_node/shared"
|
||||
"github.com/vulcanize/vulcanizedb/utils"
|
||||
)
|
||||
|
||||
// Env variables
|
||||
const (
|
||||
RESYNC_CHAIN = "RESYNC_CHAIN"
|
||||
RESYNC_START = "RESYNC_START"
|
||||
RESYNC_STOP = "RESYNC_STOP"
|
||||
RESYNC_BATCH_SIZE = "RESYNC_BATCH_SIZE"
|
||||
RESYNC_BATCH_NUMBER = "RESYNC_BATCH_NUMBER"
|
||||
RESYNC_CLEAR_OLD_CACHE = "RESYNC_CLEAR_OLD_CACHE"
|
||||
RESYNC_TYPE = "RESYNC_TYPE"
|
||||
)
|
||||
|
||||
// Config holds the parameters needed to perform a resync
|
||||
type Config struct {
|
||||
Chain shared.ChainType // The type of resync to perform
|
||||
@@ -58,26 +63,26 @@ type Config struct {
|
||||
func NewReSyncConfig() (*Config, error) {
|
||||
c := new(Config)
|
||||
var err error
|
||||
|
||||
viper.BindEnv("resync.start", RESYNC_START)
|
||||
viper.BindEnv("resync.stop", RESYNC_STOP)
|
||||
viper.BindEnv("resync.clearOldCache", RESYNC_CLEAR_OLD_CACHE)
|
||||
viper.BindEnv("resync.type", RESYNC_TYPE)
|
||||
viper.BindEnv("resync.chain", RESYNC_CHAIN)
|
||||
viper.BindEnv("ethereum.httpPath", shared.ETH_HTTP_PATH)
|
||||
viper.BindEnv("bitcoin.httpPath", shared.BTC_HTTP_PATH)
|
||||
viper.BindEnv("resync.batchSize", RESYNC_BATCH_SIZE)
|
||||
viper.BindEnv("resync.batchNumber", RESYNC_BATCH_NUMBER)
|
||||
|
||||
start := uint64(viper.GetInt64("resync.start"))
|
||||
stop := uint64(viper.GetInt64("resync.stop"))
|
||||
c.Ranges = [][2]uint64{{start, stop}}
|
||||
ipfsPath := viper.GetString("resync.ipfsPath")
|
||||
if ipfsPath == "" {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ipfsPath = filepath.Join(home, ".ipfs")
|
||||
}
|
||||
c.IPFSPath = ipfsPath
|
||||
c.DBConfig = config.Database{
|
||||
Name: viper.GetString("database.name"),
|
||||
Hostname: viper.GetString("database.hostname"),
|
||||
Port: viper.GetInt("database.port"),
|
||||
User: viper.GetString("database.user"),
|
||||
Password: viper.GetString("database.password"),
|
||||
}
|
||||
c.ClearOldCache = viper.GetBool("resync.clearOldCache")
|
||||
|
||||
c.IPFSPath, err = shared.GetIPFSPath()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resyncType := viper.GetString("resync.type")
|
||||
c.ResyncType, err = shared.GenerateResyncTypeFromString(resyncType)
|
||||
if err != nil {
|
||||
@@ -97,40 +102,22 @@ func NewReSyncConfig() (*Config, error) {
|
||||
|
||||
switch c.Chain {
|
||||
case shared.Ethereum:
|
||||
c.NodeInfo, c.HTTPClient, err = getEthNodeAndClient(fmt.Sprintf("http://%s", viper.GetString("ethereum.httpPath")))
|
||||
ethHTTP := viper.GetString("ethereum.httpPath")
|
||||
c.NodeInfo, c.HTTPClient, err = shared.GetEthNodeAndClient(fmt.Sprintf("http://%s", ethHTTP))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case shared.Bitcoin:
|
||||
c.NodeInfo = core.Node{
|
||||
ID: viper.GetString("bitcoin.nodeID"),
|
||||
ClientName: viper.GetString("bitcoin.clientName"),
|
||||
GenesisBlock: viper.GetString("bitcoin.genesisBlock"),
|
||||
NetworkID: viper.GetString("bitcoin.networkID"),
|
||||
}
|
||||
// For bitcoin we load in node info from the config because there is no RPC endpoint to retrieve this from the node
|
||||
c.HTTPClient = &rpcclient.ConnConfig{
|
||||
Host: viper.GetString("bitcoin.httpPath"),
|
||||
HTTPPostMode: true, // Bitcoin core only supports HTTP POST mode
|
||||
DisableTLS: true, // Bitcoin core does not provide TLS by default
|
||||
Pass: viper.GetString("bitcoin.pass"),
|
||||
User: viper.GetString("bitcoin.user"),
|
||||
}
|
||||
btcHTTP := viper.GetString("bitcoin.httpPath")
|
||||
c.NodeInfo, c.HTTPClient = shared.GetBtcNodeAndClient(btcHTTP)
|
||||
}
|
||||
|
||||
c.DBConfig.Init()
|
||||
db := utils.LoadPostgres(c.DBConfig, c.NodeInfo)
|
||||
c.DB = &db
|
||||
|
||||
c.Quit = make(chan bool)
|
||||
c.BatchSize = uint64(viper.GetInt64("resync.batchSize"))
|
||||
c.BatchNumber = uint64(viper.GetInt64("resync.batchNumber"))
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func getEthNodeAndClient(path string) (core.Node, interface{}, error) {
|
||||
rawRPCClient, err := rpc.Dial(path)
|
||||
if err != nil {
|
||||
return core.Node{}, nil, err
|
||||
}
|
||||
rpcClient := client.NewRPCClient(rawRPCClient, path)
|
||||
vdbNode := node.MakeNode(rpcClient)
|
||||
return vdbNode, rpcClient, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
// VulcanizeDB
|
||||
// Copyright © 2019 Vulcanize
|
||||
|
||||
// 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 shared
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
|
||||
"github.com/btcsuite/btcd/rpcclient"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/eth/client"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/eth/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/eth/node"
|
||||
)
|
||||
|
||||
// Env variables
|
||||
const (
|
||||
IPFS_PATH = "IPFS_PATH"
|
||||
|
||||
ETH_WS_PATH = "ETH_WS_PATH"
|
||||
ETH_HTTP_PATH = "ETH_HTTP_PATH"
|
||||
|
||||
BTC_WS_PATH = "BTC_WS_PATH"
|
||||
BTC_HTTP_PATH = "BTC_HTTP_PATH"
|
||||
BTC_NODE_PASSWORD = "BTC_NODE_PASSWORD"
|
||||
BTC_NODE_USER = "BTC_NODE_USER"
|
||||
BTC_NODE_ID = "BTC_NODE_ID"
|
||||
BTC_CLIENT_NAME = "BTC_CLIENT_NAME"
|
||||
BTC_GENESIS_BLOCK = "BTC_GENESIS_BLOCK"
|
||||
BTC_NETWORK_ID = "BTC_NETWORK_ID"
|
||||
)
|
||||
|
||||
// GetEthNodeAndClient returns eth node info and client from path url
|
||||
func GetEthNodeAndClient(path string) (core.Node, core.RPCClient, error) {
|
||||
rawRPCClient, err := rpc.Dial(path)
|
||||
if err != nil {
|
||||
return core.Node{}, nil, err
|
||||
}
|
||||
rpcClient := client.NewRPCClient(rawRPCClient, path)
|
||||
vdbNode := node.MakeNode(rpcClient)
|
||||
return vdbNode, rpcClient, nil
|
||||
}
|
||||
|
||||
// GetIPFSPath returns the ipfs path from the config or env variable
|
||||
func GetIPFSPath() (string, error) {
|
||||
viper.BindEnv("ipfs.path", IPFS_PATH)
|
||||
ipfsPath := viper.GetString("ipfs.path")
|
||||
if ipfsPath == "" {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
ipfsPath = filepath.Join(home, ".ipfs")
|
||||
}
|
||||
return ipfsPath, nil
|
||||
}
|
||||
|
||||
// GetBtcNodeAndClient returns btc node info from path url
|
||||
func GetBtcNodeAndClient(path string) (core.Node, *rpcclient.ConnConfig) {
|
||||
viper.BindEnv("bitcoin.nodeID", BTC_NODE_ID)
|
||||
viper.BindEnv("bitcoin.clientName", BTC_CLIENT_NAME)
|
||||
viper.BindEnv("bitcoin.genesisBlock", BTC_GENESIS_BLOCK)
|
||||
viper.BindEnv("bitcoin.networkID", BTC_NETWORK_ID)
|
||||
viper.BindEnv("bitcoin.pass", BTC_NODE_PASSWORD)
|
||||
viper.BindEnv("bitcoin.user", BTC_NODE_USER)
|
||||
|
||||
// For bitcoin we load in node info from the config because there is no RPC endpoint to retrieve this from the node
|
||||
return core.Node{
|
||||
ID: viper.GetString("bitcoin.nodeID"),
|
||||
ClientName: viper.GetString("bitcoin.clientName"),
|
||||
GenesisBlock: viper.GetString("bitcoin.genesisBlock"),
|
||||
NetworkID: viper.GetString("bitcoin.networkID"),
|
||||
}, &rpcclient.ConnConfig{
|
||||
Host: path,
|
||||
HTTPPostMode: true, // Bitcoin core only supports HTTP POST mode
|
||||
DisableTLS: true, // Bitcoin core does not provide TLS by default
|
||||
Pass: viper.GetString("bitcoin.pass"),
|
||||
User: viper.GetString("bitcoin.user"),
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,6 @@ import (
|
||||
"bytes"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/ipfs"
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user