2020-01-17 23:16:01 +00:00
|
|
|
// 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/>.
|
|
|
|
|
2020-02-25 22:38:27 +00:00
|
|
|
package super_node
|
2020-01-17 23:16:01 +00:00
|
|
|
|
|
|
|
import (
|
2020-03-18 00:42:53 +00:00
|
|
|
"fmt"
|
2020-02-10 17:16:57 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/config"
|
2020-01-29 19:00:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/core"
|
2020-02-10 15:00:55 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/postgres"
|
2020-02-25 22:38:27 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/shared"
|
2020-01-17 23:16:01 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/utils"
|
|
|
|
)
|
|
|
|
|
2020-03-20 18:15:50 +00:00
|
|
|
// Env variables
|
|
|
|
const (
|
2020-04-04 01:36:37 +00:00
|
|
|
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"
|
|
|
|
SUPERNODE_VALIDATION_LEVEL = "SUPERNODE_VALIDATION_LEVEL"
|
2020-03-20 18:15:50 +00:00
|
|
|
)
|
|
|
|
|
2020-02-25 22:38:27 +00:00
|
|
|
// Config struct
|
|
|
|
type Config struct {
|
2020-01-17 23:16:01 +00:00
|
|
|
// Ubiquitous fields
|
2020-02-25 22:38:27 +00:00
|
|
|
Chain shared.ChainType
|
2020-01-17 23:16:01 +00:00
|
|
|
IPFSPath string
|
|
|
|
DB *postgres.DB
|
2020-01-21 19:12:35 +00:00
|
|
|
DBConfig config.Database
|
2020-01-17 23:16:01 +00:00
|
|
|
Quit chan bool
|
|
|
|
// Server fields
|
2020-01-21 19:12:35 +00:00
|
|
|
Serve bool
|
|
|
|
WSEndpoint string
|
|
|
|
HTTPEndpoint string
|
|
|
|
IPCEndpoint string
|
2020-01-17 23:16:01 +00:00
|
|
|
// Sync params
|
|
|
|
Sync bool
|
|
|
|
Workers int
|
2020-01-30 22:35:31 +00:00
|
|
|
WSClient interface{}
|
2020-01-17 23:16:01 +00:00
|
|
|
NodeInfo core.Node
|
|
|
|
// Backfiller params
|
2020-04-04 01:36:37 +00:00
|
|
|
BackFill bool
|
|
|
|
HTTPClient interface{}
|
|
|
|
Frequency time.Duration
|
|
|
|
BatchSize uint64
|
|
|
|
BatchNumber uint64
|
|
|
|
ValidationLevel int
|
2020-04-19 23:26:23 +00:00
|
|
|
Timeout time.Duration // HTTP connection timeout in seconds
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
|
|
|
|
2020-03-17 13:37:00 +00:00
|
|
|
// NewSuperNodeConfig is used to initialize a SuperNode config from a .toml file
|
|
|
|
// Separate chain supernode instances need to be ran with separate ipfs path in order to avoid lock contention on the ipfs repository lockfile
|
|
|
|
func NewSuperNodeConfig() (*Config, error) {
|
|
|
|
c := new(Config)
|
2020-01-17 23:16:01 +00:00
|
|
|
var err error
|
2020-03-17 13:37:00 +00:00
|
|
|
|
2020-03-20 18:15:50 +00:00
|
|
|
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)
|
|
|
|
|
2020-03-17 13:37:00 +00:00
|
|
|
chain := viper.GetString("superNode.chain")
|
|
|
|
c.Chain, err = shared.NewChainType(chain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-20 18:15:50 +00:00
|
|
|
c.IPFSPath, err = shared.GetIPFSPath()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
2020-03-17 13:37:00 +00:00
|
|
|
|
|
|
|
c.Sync = viper.GetBool("superNode.sync")
|
|
|
|
if c.Sync {
|
|
|
|
workers := viper.GetInt("superNode.workers")
|
|
|
|
if workers < 1 {
|
|
|
|
workers = 1
|
2020-02-10 15:00:55 +00:00
|
|
|
}
|
2020-03-17 13:37:00 +00:00
|
|
|
c.Workers = workers
|
|
|
|
switch c.Chain {
|
|
|
|
case shared.Ethereum:
|
2020-03-20 18:15:50 +00:00
|
|
|
ethWS := viper.GetString("ethereum.wsPath")
|
|
|
|
c.NodeInfo, c.WSClient, err = shared.GetEthNodeAndClient(fmt.Sprintf("ws://%s", ethWS))
|
2020-03-17 13:37:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-02-13 22:50:56 +00:00
|
|
|
}
|
2020-03-17 13:37:00 +00:00
|
|
|
case shared.Bitcoin:
|
2020-03-20 18:15:50 +00:00
|
|
|
btcWS := viper.GetString("bitcoin.wsPath")
|
|
|
|
c.NodeInfo, c.WSClient = shared.GetBtcNodeAndClient(btcWS)
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
2020-03-17 13:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Serve = viper.GetBool("superNode.server")
|
|
|
|
if c.Serve {
|
|
|
|
wsPath := viper.GetString("superNode.wsPath")
|
|
|
|
if wsPath == "" {
|
2020-03-18 00:42:53 +00:00
|
|
|
wsPath = "127.0.0.1:8080"
|
2020-03-17 13:37:00 +00:00
|
|
|
}
|
|
|
|
c.WSEndpoint = wsPath
|
|
|
|
ipcPath := viper.GetString("superNode.ipcPath")
|
|
|
|
if ipcPath == "" {
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
2020-01-17 23:16:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-17 13:37:00 +00:00
|
|
|
ipcPath = filepath.Join(home, ".vulcanize/vulcanize.ipc")
|
2020-01-21 19:12:35 +00:00
|
|
|
}
|
2020-03-17 13:37:00 +00:00
|
|
|
c.IPCEndpoint = ipcPath
|
|
|
|
httpPath := viper.GetString("superNode.httpPath")
|
|
|
|
if httpPath == "" {
|
2020-03-18 00:42:53 +00:00
|
|
|
httpPath = "127.0.0.1:8081"
|
2020-03-17 13:37:00 +00:00
|
|
|
}
|
|
|
|
c.HTTPEndpoint = httpPath
|
|
|
|
}
|
|
|
|
|
2020-03-20 18:15:50 +00:00
|
|
|
c.BackFill = viper.GetBool("superNode.backFill")
|
|
|
|
if c.BackFill {
|
|
|
|
if err := c.BackFillFields(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
2020-03-17 13:37:00 +00:00
|
|
|
|
2020-03-20 18:15:50 +00:00
|
|
|
c.DBConfig.Init()
|
2020-03-17 13:37:00 +00:00
|
|
|
db := utils.LoadPostgres(c.DBConfig, c.NodeInfo)
|
|
|
|
c.DB = &db
|
|
|
|
c.Quit = make(chan bool)
|
|
|
|
|
|
|
|
return c, nil
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 19:12:35 +00:00
|
|
|
// BackFillFields is used to fill in the BackFill fields of the config
|
2020-03-20 18:15:50 +00:00
|
|
|
func (c *Config) BackFillFields() error {
|
2020-02-10 15:00:55 +00:00
|
|
|
var err error
|
2020-03-20 18:15:50 +00:00
|
|
|
|
|
|
|
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)
|
2020-04-04 01:36:37 +00:00
|
|
|
viper.BindEnv("superNode.validationLevel", SUPERNODE_VALIDATION_LEVEL)
|
2020-04-20 13:14:02 +00:00
|
|
|
viper.BindEnv("superNode.timeout", shared.HTTP_TIMEOUT)
|
|
|
|
|
|
|
|
timeout := viper.GetInt("superNode.timeout")
|
|
|
|
if timeout < 15 {
|
|
|
|
timeout = 15
|
|
|
|
}
|
|
|
|
c.Timeout = time.Second * time.Duration(timeout)
|
2020-03-20 18:15:50 +00:00
|
|
|
|
|
|
|
switch c.Chain {
|
2020-02-25 22:38:27 +00:00
|
|
|
case shared.Ethereum:
|
2020-03-20 18:15:50 +00:00
|
|
|
ethHTTP := viper.GetString("ethereum.httpPath")
|
|
|
|
c.NodeInfo, c.HTTPClient, err = shared.GetEthNodeAndClient(fmt.Sprintf("http://%s", ethHTTP))
|
2020-02-10 15:00:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-25 22:38:27 +00:00
|
|
|
case shared.Bitcoin:
|
2020-03-20 18:15:50 +00:00
|
|
|
btcHTTP := viper.GetString("bitcoin.httpPath")
|
|
|
|
c.NodeInfo, c.HTTPClient = shared.GetBtcNodeAndClient(btcHTTP)
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
2020-03-20 18:15:50 +00:00
|
|
|
|
2020-03-17 13:37:00 +00:00
|
|
|
freq := viper.GetInt("superNode.frequency")
|
2020-01-17 23:16:01 +00:00
|
|
|
var frequency time.Duration
|
|
|
|
if freq <= 0 {
|
2020-02-10 17:16:57 +00:00
|
|
|
frequency = time.Second * 30
|
2020-01-17 23:16:01 +00:00
|
|
|
} else {
|
2020-02-10 17:16:57 +00:00
|
|
|
frequency = time.Second * time.Duration(freq)
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
2020-03-20 18:15:50 +00:00
|
|
|
c.Frequency = frequency
|
|
|
|
c.BatchSize = uint64(viper.GetInt64("superNode.batchSize"))
|
|
|
|
c.BatchNumber = uint64(viper.GetInt64("superNode.batchNumber"))
|
2020-04-04 01:36:37 +00:00
|
|
|
c.ValidationLevel = viper.GetInt("superNode.validationLevel")
|
2020-01-21 19:12:35 +00:00
|
|
|
return nil
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|