2020-02-19 22:32:59 +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-08-31 15:58:16 +00:00
|
|
|
package serve
|
2020-02-19 22:32:59 +00:00
|
|
|
|
|
|
|
import (
|
2021-04-14 15:53:44 +00:00
|
|
|
"errors"
|
2020-10-26 13:58:37 +00:00
|
|
|
"fmt"
|
2020-10-20 20:33:18 +00:00
|
|
|
"math/big"
|
2020-05-30 03:02:47 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-02-25 22:38:27 +00:00
|
|
|
|
2020-10-26 13:58:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
"github.com/vulcanize/ipld-eth-indexer/pkg/shared"
|
|
|
|
|
2020-10-20 20:33:18 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2020-09-02 15:19:25 +00:00
|
|
|
|
2020-10-20 20:33:18 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2020-02-25 22:38:27 +00:00
|
|
|
"github.com/spf13/viper"
|
2020-09-02 15:19:25 +00:00
|
|
|
"github.com/vulcanize/ipld-eth-indexer/pkg/postgres"
|
|
|
|
"github.com/vulcanize/ipld-eth-indexer/utils"
|
2020-10-21 10:53:38 +00:00
|
|
|
"github.com/vulcanize/ipld-eth-server/pkg/prom"
|
2020-10-20 20:33:18 +00:00
|
|
|
|
|
|
|
"github.com/vulcanize/ipld-eth-server/pkg/eth"
|
2020-02-19 22:32:59 +00:00
|
|
|
)
|
|
|
|
|
2020-05-30 03:02:47 +00:00
|
|
|
// Env variables
|
|
|
|
const (
|
2020-08-31 15:47:06 +00:00
|
|
|
SERVER_WS_PATH = "SERVER_WS_PATH"
|
|
|
|
SERVER_IPC_PATH = "SERVER_IPC_PATH"
|
|
|
|
SERVER_HTTP_PATH = "SERVER_HTTP_PATH"
|
2020-05-30 03:02:47 +00:00
|
|
|
|
|
|
|
SERVER_MAX_IDLE_CONNECTIONS = "SERVER_MAX_IDLE_CONNECTIONS"
|
|
|
|
SERVER_MAX_OPEN_CONNECTIONS = "SERVER_MAX_OPEN_CONNECTIONS"
|
|
|
|
SERVER_MAX_CONN_LIFETIME = "SERVER_MAX_CONN_LIFETIME"
|
2020-10-20 20:33:18 +00:00
|
|
|
|
|
|
|
ETH_DEFAULT_SENDER_ADDR = "ETH_DEFAULT_SENDER_ADDR"
|
2020-10-27 17:42:22 +00:00
|
|
|
ETH_RPC_GAS_CAP = "ETH_RPC_GAS_CAP"
|
2021-03-10 16:18:32 +00:00
|
|
|
ETH_CHAIN_CONFIG = "ETH_CHAIN_CONFIG"
|
2021-02-24 16:50:26 +00:00
|
|
|
ETH_SUPPORTS_STATEDIFF = "ETH_SUPPORTS_STATEDIFF"
|
2020-05-30 03:02:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config struct
|
2020-02-19 22:32:59 +00:00
|
|
|
type Config struct {
|
2021-04-14 15:53:44 +00:00
|
|
|
DB *postgres.DB
|
|
|
|
DBConfig postgres.Config
|
|
|
|
|
|
|
|
WSEnabled bool
|
|
|
|
WSEndpoint string
|
|
|
|
|
|
|
|
HTTPEnabled bool
|
|
|
|
HTTPEndpoint string
|
|
|
|
|
|
|
|
IPCEnabled bool
|
|
|
|
IPCEndpoint string
|
|
|
|
|
|
|
|
EthGraphqlEnabled bool
|
|
|
|
EthGraphqlEndpoint string
|
|
|
|
|
|
|
|
IpldGraphqlEnabled bool
|
|
|
|
IpldGraphqlEndpoint string
|
|
|
|
IpldPostgraphileEndpoint string
|
|
|
|
TracingHttpEndpoint string
|
|
|
|
TracingPostgraphileEndpoint string
|
|
|
|
|
2021-02-24 16:50:26 +00:00
|
|
|
ChainConfig *params.ChainConfig
|
|
|
|
DefaultSender *common.Address
|
|
|
|
RPCGasCap *big.Int
|
2021-04-16 13:56:02 +00:00
|
|
|
EthHttpEndpoint string
|
2021-02-24 16:50:26 +00:00
|
|
|
Client *rpc.Client
|
|
|
|
SupportStateDiff bool
|
2020-02-25 22:38:27 +00:00
|
|
|
}
|
|
|
|
|
2020-06-30 00:16:52 +00:00
|
|
|
// NewConfig is used to initialize a watcher config from a .toml file
|
|
|
|
// Separate chain watcher instances need to be ran with separate ipfs path in order to avoid lock contention on the ipfs repository lockfile
|
|
|
|
func NewConfig() (*Config, error) {
|
2020-02-25 22:38:27 +00:00
|
|
|
c := new(Config)
|
2020-08-31 15:47:06 +00:00
|
|
|
|
2020-10-27 17:42:22 +00:00
|
|
|
viper.BindEnv("ethereum.httpPath", shared.ETH_HTTP_PATH)
|
2020-10-20 20:33:18 +00:00
|
|
|
viper.BindEnv("ethereum.defaultSender", ETH_DEFAULT_SENDER_ADDR)
|
|
|
|
viper.BindEnv("ethereum.rpcGasCap", ETH_RPC_GAS_CAP)
|
2021-03-10 16:18:32 +00:00
|
|
|
viper.BindEnv("ethereum.chainConfig", ETH_CHAIN_CONFIG)
|
2021-02-24 16:50:26 +00:00
|
|
|
viper.BindEnv("ethereum.supportsStateDiff", ETH_SUPPORTS_STATEDIFF)
|
2020-05-30 03:02:47 +00:00
|
|
|
|
|
|
|
c.DBConfig.Init()
|
|
|
|
|
2020-10-26 13:58:37 +00:00
|
|
|
ethHTTP := viper.GetString("ethereum.httpPath")
|
2021-04-16 13:56:02 +00:00
|
|
|
ethHTTPEndpoint := fmt.Sprintf("http://%s", ethHTTP)
|
|
|
|
nodeInfo, cli, err := shared.GetEthNodeAndClient(ethHTTPEndpoint)
|
2020-10-26 13:58:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c.Client = cli
|
2021-02-24 16:50:26 +00:00
|
|
|
c.SupportStateDiff = viper.GetBool("ethereum.supportsStateDiff")
|
2021-04-16 13:56:02 +00:00
|
|
|
c.EthHttpEndpoint = ethHTTPEndpoint
|
2020-10-26 13:58:37 +00:00
|
|
|
|
2021-04-14 15:53:44 +00:00
|
|
|
// websocket server
|
|
|
|
wsEnabled := viper.GetBool("eth.server.ws")
|
|
|
|
if wsEnabled {
|
|
|
|
wsPath := viper.GetString("eth.server.wsPath")
|
|
|
|
if wsPath == "" {
|
|
|
|
wsPath = "127.0.0.1:8080"
|
|
|
|
}
|
|
|
|
c.WSEndpoint = wsPath
|
2020-08-31 15:47:06 +00:00
|
|
|
}
|
2021-04-14 15:53:44 +00:00
|
|
|
c.WSEnabled = wsEnabled
|
|
|
|
|
|
|
|
// ipc server
|
|
|
|
ipcEnabled := viper.GetBool("eth.server.ipc")
|
|
|
|
if ipcEnabled {
|
|
|
|
ipcPath := viper.GetString("eth.server.ipcPath")
|
|
|
|
if ipcPath == "" {
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ipcPath = filepath.Join(home, ".vulcanize/vulcanize.ipc")
|
2020-05-30 03:02:47 +00:00
|
|
|
}
|
2021-04-14 15:53:44 +00:00
|
|
|
c.IPCEndpoint = ipcPath
|
2020-05-30 03:02:47 +00:00
|
|
|
}
|
2021-04-14 15:53:44 +00:00
|
|
|
c.IPCEnabled = ipcEnabled
|
|
|
|
|
|
|
|
// http server
|
|
|
|
httpEnabled := viper.GetBool("eth.server.http")
|
|
|
|
if httpEnabled {
|
|
|
|
httpPath := viper.GetString("eth.server.httpPath")
|
|
|
|
if httpPath == "" {
|
|
|
|
httpPath = "127.0.0.1:8081"
|
|
|
|
}
|
|
|
|
c.HTTPEndpoint = httpPath
|
|
|
|
}
|
|
|
|
c.HTTPEnabled = httpEnabled
|
|
|
|
|
|
|
|
// eth graphql endpoint
|
|
|
|
ethGraphqlEnabled := viper.GetBool("eth.server.graphql")
|
|
|
|
if ethGraphqlEnabled {
|
|
|
|
ethGraphqlPath := viper.GetString("eth.server.graphqlPath")
|
|
|
|
if ethGraphqlPath == "" {
|
|
|
|
ethGraphqlPath = "127.0.0.1:8082"
|
|
|
|
}
|
|
|
|
c.EthGraphqlEndpoint = ethGraphqlPath
|
2020-08-31 15:47:06 +00:00
|
|
|
}
|
2021-04-14 15:53:44 +00:00
|
|
|
c.EthGraphqlEnabled = ethGraphqlEnabled
|
|
|
|
|
|
|
|
// ipld graphql endpoint
|
|
|
|
ipldGraphqlEnabled := viper.GetBool("ipld.server.graphql")
|
|
|
|
if ipldGraphqlEnabled {
|
|
|
|
ipldGraphqlPath := viper.GetString("ipld.server.graphqlPath")
|
|
|
|
if ipldGraphqlPath == "" {
|
|
|
|
ipldGraphqlPath = "127.0.0.1:8083"
|
|
|
|
}
|
|
|
|
c.IpldGraphqlEndpoint = ipldGraphqlPath
|
|
|
|
|
|
|
|
ipldPostgraphilePath := viper.GetString("ipld.postgraphilePath")
|
|
|
|
if ipldPostgraphilePath == "" {
|
|
|
|
return nil, errors.New("ipld-postgraphile-path parameter is empty")
|
|
|
|
}
|
|
|
|
c.IpldPostgraphileEndpoint = ipldPostgraphilePath
|
|
|
|
|
|
|
|
tracingHttpEndpoint := viper.GetString("tracing.httpPath")
|
|
|
|
tracingPostgraphilePath := viper.GetString("tracing.postgraphilePath")
|
|
|
|
|
|
|
|
// these two parameters either can be both empty or both set
|
|
|
|
if (tracingHttpEndpoint == "" && tracingPostgraphilePath != "") || (tracingHttpEndpoint != "" && tracingPostgraphilePath == "") {
|
|
|
|
return nil, errors.New("tracing.httpPath and tracing.postgraphilePath parameters either can be both empty or both set")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.TracingHttpEndpoint = tracingHttpEndpoint
|
|
|
|
c.TracingPostgraphileEndpoint = tracingPostgraphilePath
|
|
|
|
}
|
|
|
|
c.IpldGraphqlEnabled = ipldGraphqlEnabled
|
|
|
|
|
2020-08-31 15:47:06 +00:00
|
|
|
overrideDBConnConfig(&c.DBConfig)
|
2021-03-10 10:13:43 +00:00
|
|
|
serveDB := utils.LoadPostgres(c.DBConfig, nodeInfo, false)
|
2020-10-19 13:07:29 +00:00
|
|
|
prom.RegisterDBCollector(c.DBConfig.Name, serveDB.DB)
|
2020-08-31 15:47:06 +00:00
|
|
|
c.DB = &serveDB
|
2020-05-30 03:02:47 +00:00
|
|
|
|
2020-10-20 20:33:18 +00:00
|
|
|
defaultSenderStr := viper.GetString("ethereum.defaultSender")
|
|
|
|
if defaultSenderStr != "" {
|
|
|
|
sender := common.HexToAddress(defaultSenderStr)
|
|
|
|
c.DefaultSender = &sender
|
|
|
|
}
|
|
|
|
rpcGasCapStr := viper.GetString("ethereum.rpcGasCap")
|
|
|
|
if rpcGasCapStr != "" {
|
|
|
|
if rpcGasCap, ok := new(big.Int).SetString(rpcGasCapStr, 10); ok {
|
|
|
|
c.RPCGasCap = rpcGasCap
|
|
|
|
}
|
|
|
|
}
|
2021-03-10 16:18:32 +00:00
|
|
|
chainConfigPath := viper.GetString("ethereum.chainConfig")
|
|
|
|
if chainConfigPath != "" {
|
|
|
|
c.ChainConfig, err = eth.LoadConfig(chainConfigPath)
|
|
|
|
} else {
|
|
|
|
c.ChainConfig, err = eth.ChainConfig(nodeInfo.ChainID)
|
|
|
|
}
|
2020-10-20 20:33:18 +00:00
|
|
|
return c, err
|
2020-05-30 03:02:47 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 15:47:06 +00:00
|
|
|
func overrideDBConnConfig(con *postgres.Config) {
|
|
|
|
viper.BindEnv("database.server.maxIdle", SERVER_MAX_IDLE_CONNECTIONS)
|
|
|
|
viper.BindEnv("database.server.maxOpen", SERVER_MAX_OPEN_CONNECTIONS)
|
|
|
|
viper.BindEnv("database.server.maxLifetime", SERVER_MAX_CONN_LIFETIME)
|
|
|
|
con.MaxIdle = viper.GetInt("database.server.maxIdle")
|
|
|
|
con.MaxOpen = viper.GetInt("database.server.maxOpen")
|
|
|
|
con.MaxLifetime = viper.GetInt("database.server.maxLifetime")
|
2020-02-19 22:32:59 +00:00
|
|
|
}
|