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-03 18:22:29 +00:00
|
|
|
package shared
|
2020-01-17 23:16:01 +00:00
|
|
|
|
|
|
|
import (
|
2020-02-13 22:50:56 +00:00
|
|
|
"fmt"
|
2020-02-10 17:16:57 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2020-02-10 15:00:55 +00:00
|
|
|
"github.com/btcsuite/btcd/rpcclient"
|
2020-01-17 23:16:01 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/config"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/client"
|
|
|
|
vRpc "github.com/vulcanize/vulcanizedb/pkg/eth/converters/rpc"
|
2020-01-29 19:00:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/core"
|
2020-01-17 23:16:01 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/node"
|
2020-02-10 15:00:55 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/postgres"
|
2020-01-17 23:16:01 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/utils"
|
|
|
|
)
|
|
|
|
|
2020-02-03 18:22:29 +00:00
|
|
|
// SuperNodeConfig struct
|
|
|
|
type SuperNodeConfig struct {
|
2020-01-17 23:16:01 +00:00
|
|
|
// Ubiquitous fields
|
|
|
|
Chain ChainType
|
|
|
|
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-01-21 19:12:35 +00:00
|
|
|
BackFill bool
|
2020-01-30 22:35:31 +00:00
|
|
|
HTTPClient interface{}
|
2020-01-21 19:12:35 +00:00
|
|
|
Frequency time.Duration
|
|
|
|
BatchSize uint64
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-13 22:50:56 +00:00
|
|
|
// NewSuperNodeConfigs is used to initialize multiple SuperNode configs from a single config .toml file
|
|
|
|
// Separate chain supernode instances need to be ran in the same process in order to avoid lock contention on the ipfs repository
|
|
|
|
func NewSuperNodeConfigs() ([]*SuperNodeConfig, error) {
|
|
|
|
chains := viper.GetStringSlice("superNode.chains")
|
|
|
|
configs := make([]*SuperNodeConfig, len(chains))
|
2020-01-17 23:16:01 +00:00
|
|
|
var err error
|
|
|
|
ipfsPath := viper.GetString("superNode.ipfsPath")
|
|
|
|
if ipfsPath == "" {
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ipfsPath = filepath.Join(home, ".ipfs")
|
|
|
|
}
|
2020-02-13 22:50:56 +00:00
|
|
|
for i, chain := range chains {
|
|
|
|
sn := new(SuperNodeConfig)
|
|
|
|
sn.Chain, err = NewChainType(chain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sn.DBConfig = config.Database{
|
|
|
|
Name: viper.GetString(fmt.Sprintf("superNode.%s.database.name", chain)),
|
|
|
|
Hostname: viper.GetString(fmt.Sprintf("superNode.%s.database.hostname", chain)),
|
|
|
|
Port: viper.GetInt(fmt.Sprintf("superNode.%s.database.port", chain)),
|
|
|
|
User: viper.GetString(fmt.Sprintf("superNode.%s.database.user", chain)),
|
|
|
|
Password: viper.GetString(fmt.Sprintf("superNode.%s.database.password", chain)),
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
2020-02-13 22:50:56 +00:00
|
|
|
sn.IPFSPath = ipfsPath
|
|
|
|
sn.Serve = viper.GetBool(fmt.Sprintf("superNode.%s.server.on", chain))
|
|
|
|
sn.Sync = viper.GetBool(fmt.Sprintf("superNode.%s.sync.on", chain))
|
|
|
|
if sn.Sync {
|
|
|
|
workers := viper.GetInt("superNode.sync.workers")
|
|
|
|
if workers < 1 {
|
|
|
|
workers = 1
|
2020-02-10 15:00:55 +00:00
|
|
|
}
|
2020-02-13 22:50:56 +00:00
|
|
|
sn.Workers = workers
|
|
|
|
switch sn.Chain {
|
|
|
|
case Ethereum:
|
|
|
|
sn.NodeInfo, sn.WSClient, err = getEthNodeAndClient(viper.GetString("superNode.ethereum.sync.wsPath"))
|
|
|
|
case Bitcoin:
|
|
|
|
sn.NodeInfo = core.Node{
|
|
|
|
ID: viper.GetString("superNode.bitcoin.node.nodeID"),
|
|
|
|
ClientName: viper.GetString("superNode.bitcoin.node.clientName"),
|
|
|
|
GenesisBlock: viper.GetString("superNode.bitcoin.node.genesisBlock"),
|
|
|
|
NetworkID: viper.GetString("superNode.bitcoin.node.networkID"),
|
|
|
|
}
|
|
|
|
// For bitcoin we load in node info from the config because there is no RPC endpoint to retrieve this from the node
|
|
|
|
sn.WSClient = &rpcclient.ConnConfig{
|
|
|
|
Host: viper.GetString("superNode.bitcoin.sync.wsPath"),
|
|
|
|
HTTPPostMode: true, // Bitcoin core only supports HTTP POST mode
|
|
|
|
DisableTLS: true, // Bitcoin core does not provide TLS by default
|
|
|
|
Pass: viper.GetString("superNode.bitcoin.sync.pass"),
|
|
|
|
User: viper.GetString("superNode.bitcoin.sync.user"),
|
|
|
|
}
|
2020-02-10 15:00:55 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-13 22:50:56 +00:00
|
|
|
if sn.Serve {
|
|
|
|
wsPath := viper.GetString(fmt.Sprintf("superNode.%s.server.wsPath", chain))
|
|
|
|
if wsPath == "" {
|
|
|
|
wsPath = "ws://127.0.0.1:8546"
|
|
|
|
}
|
|
|
|
sn.WSEndpoint = wsPath
|
|
|
|
ipcPath := viper.GetString(fmt.Sprintf("superNode.%s.server.ipcPath", chain))
|
|
|
|
if ipcPath == "" {
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ipcPath = filepath.Join(home, ".vulcanize/vulcanize.ipc")
|
|
|
|
}
|
|
|
|
sn.IPCEndpoint = ipcPath
|
|
|
|
httpPath := viper.GetString(fmt.Sprintf("superNode.%s.server.httpPath", chain))
|
|
|
|
if httpPath == "" {
|
|
|
|
httpPath = "http://127.0.0.1:8545"
|
|
|
|
}
|
|
|
|
sn.HTTPEndpoint = httpPath
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
2020-02-13 22:50:56 +00:00
|
|
|
db := utils.LoadPostgres(sn.DBConfig, sn.NodeInfo)
|
|
|
|
sn.DB = &db
|
|
|
|
sn.Quit = make(chan bool)
|
|
|
|
if viper.GetBool(fmt.Sprintf("superNode.%s.backFill.on", chain)) {
|
|
|
|
if err := sn.BackFillFields(chain); err != nil {
|
2020-01-17 23:16:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-21 19:12:35 +00:00
|
|
|
}
|
2020-02-13 22:50:56 +00:00
|
|
|
configs[i] = sn
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
2020-02-13 22:50:56 +00:00
|
|
|
return configs, err
|
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-02-13 22:50:56 +00:00
|
|
|
func (sn *SuperNodeConfig) BackFillFields(chain string) error {
|
2020-01-21 19:12:35 +00:00
|
|
|
sn.BackFill = true
|
2020-02-10 15:00:55 +00:00
|
|
|
var httpClient interface{}
|
|
|
|
var err error
|
2020-02-10 17:16:57 +00:00
|
|
|
switch sn.Chain {
|
|
|
|
case Ethereum:
|
2020-02-13 22:50:56 +00:00
|
|
|
_, httpClient, err = getEthNodeAndClient(viper.GetString("superNode.ethereum.backFill.httpPath"))
|
2020-02-10 15:00:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-10 17:16:57 +00:00
|
|
|
case Bitcoin:
|
2020-02-10 15:00:55 +00:00
|
|
|
httpClient = &rpcclient.ConnConfig{
|
2020-02-13 22:50:56 +00:00
|
|
|
Host: viper.GetString("superNode.bitcoin.backFill.httpPath"),
|
2020-02-10 17:16:57 +00:00
|
|
|
HTTPPostMode: true, // Bitcoin core only supports HTTP POST mode
|
|
|
|
DisableTLS: true, // Bitcoin core does not provide TLS by default
|
2020-02-13 22:50:56 +00:00
|
|
|
Pass: viper.GetString("superNode.bitcoin.backFill.pass"),
|
|
|
|
User: viper.GetString("superNode.bitcoin.backFill.user"),
|
2020-02-10 15:00:55 +00:00
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
2020-01-21 19:12:35 +00:00
|
|
|
sn.HTTPClient = httpClient
|
2020-02-13 22:50:56 +00:00
|
|
|
freq := viper.GetInt(fmt.Sprintf("superNode.%s.backFill.frequency", chain))
|
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-01-21 19:12:35 +00:00
|
|
|
sn.Frequency = frequency
|
2020-02-13 22:50:56 +00:00
|
|
|
sn.BatchSize = uint64(viper.GetInt64(fmt.Sprintf("superNode.%s.backFill.batchSize", chain)))
|
2020-01-21 19:12:35 +00:00
|
|
|
return nil
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 17:16:57 +00:00
|
|
|
func getEthNodeAndClient(path string) (core.Node, interface{}, error) {
|
2020-02-10 15:00:55 +00:00
|
|
|
rawRPCClient, err := rpc.Dial(path)
|
|
|
|
if err != nil {
|
|
|
|
return core.Node{}, nil, err
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
2020-02-10 15:00:55 +00:00
|
|
|
rpcClient := client.NewRPCClient(rawRPCClient, path)
|
|
|
|
ethClient := ethclient.NewClient(rawRPCClient)
|
|
|
|
vdbEthClient := client.NewEthClient(ethClient)
|
|
|
|
vdbNode := node.MakeNode(rpcClient)
|
|
|
|
transactionConverter := vRpc.NewRPCTransactionConverter(ethClient)
|
|
|
|
blockChain := eth.NewBlockChain(vdbEthClient, rpcClient, vdbNode, transactionConverter)
|
|
|
|
return blockChain.Node(), rpcClient, nil
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|