2020-08-19 05:58:45 +00:00
|
|
|
// Copyright © 2019 Vulcanize, Inc
|
2020-08-19 05:11:55 +00:00
|
|
|
//
|
|
|
|
// 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 cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-08-19 05:58:45 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync"
|
2020-08-19 05:11:55 +00:00
|
|
|
|
2020-11-16 10:15:53 +00:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
2020-08-19 05:58:45 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
"github.com/sirupsen/logrus"
|
2020-08-19 05:11:55 +00:00
|
|
|
"github.com/spf13/cobra"
|
2020-08-19 05:58:45 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2020-11-10 09:07:59 +00:00
|
|
|
ind "github.com/ethereum/go-ethereum/statediff/indexer"
|
|
|
|
"github.com/ethereum/go-ethereum/statediff/indexer/postgres"
|
2021-10-14 06:47:38 +00:00
|
|
|
|
2020-08-19 05:58:45 +00:00
|
|
|
sd "github.com/vulcanize/eth-statediff-service/pkg"
|
2020-08-19 05:11:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// serveCmd represents the serve command
|
|
|
|
var serveCmd = &cobra.Command{
|
|
|
|
Use: "serve",
|
2020-08-19 05:58:45 +00:00
|
|
|
Short: "Standup a standalone statediffing RPC service on top of leveldb",
|
|
|
|
Long: `Usage
|
2020-08-19 05:11:55 +00:00
|
|
|
|
2020-08-19 05:58:45 +00:00
|
|
|
./eth-statediff-service serve --config={path to toml config file}`,
|
2020-08-19 05:11:55 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2020-08-19 05:58:45 +00:00
|
|
|
subCommand = cmd.CalledAs()
|
|
|
|
logWithCommand = *logrus.WithField("SubCommand", subCommand)
|
|
|
|
serve()
|
2020-08-19 05:11:55 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-19 05:58:45 +00:00
|
|
|
func serve() {
|
|
|
|
logWithCommand.Info("starting statediff RPC service")
|
|
|
|
|
|
|
|
// load params
|
|
|
|
path := viper.GetString("leveldb.path")
|
|
|
|
ancientPath := viper.GetString("leveldb.ancient")
|
|
|
|
if path == "" || ancientPath == "" {
|
|
|
|
logWithCommand.Fatal("require a valid eth leveldb primary datastore path and ancient datastore path")
|
|
|
|
}
|
2020-11-10 09:07:59 +00:00
|
|
|
|
|
|
|
nodeInfo := GetEthNodeInfo()
|
|
|
|
config, err := chainConfig(nodeInfo.ChainID)
|
2020-08-19 05:58:45 +00:00
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create leveldb reader
|
2020-11-26 10:31:29 +00:00
|
|
|
logWithCommand.Info("Creating leveldb reader")
|
2020-08-19 05:58:45 +00:00
|
|
|
lvlDBReader, err := sd.NewLvlDBReader(path, ancientPath, config)
|
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create statediff service
|
2020-11-26 10:31:29 +00:00
|
|
|
logWithCommand.Info("Creating statediff service")
|
2020-11-16 09:49:59 +00:00
|
|
|
db, err := postgres.NewDB(postgres.DbConnectionString(GetDBParams()), GetDBConfig(), nodeInfo)
|
2020-11-10 09:07:59 +00:00
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
2021-10-14 06:47:38 +00:00
|
|
|
|
|
|
|
indexer, err := ind.NewStateDiffIndexer(config, db)
|
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-11-10 09:07:59 +00:00
|
|
|
statediffService, err := sd.NewStateDiffService(lvlDBReader, indexer, viper.GetUint("statediff.workers"))
|
2020-08-19 05:58:45 +00:00
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// start service and servers
|
2020-11-26 10:31:29 +00:00
|
|
|
logWithCommand.Info("Starting statediff service")
|
2020-08-19 05:58:45 +00:00
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
go statediffService.Loop(wg)
|
2020-11-26 10:31:29 +00:00
|
|
|
logWithCommand.Info("Starting RPC servers")
|
2020-08-19 05:58:45 +00:00
|
|
|
if err := startServers(statediffService); err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
2020-11-26 10:31:29 +00:00
|
|
|
logWithCommand.Info("RPC servers successfully spun up; awaiting requests")
|
2020-08-19 05:58:45 +00:00
|
|
|
|
|
|
|
// clean shutdown
|
|
|
|
shutdown := make(chan os.Signal)
|
|
|
|
signal.Notify(shutdown, os.Interrupt)
|
|
|
|
<-shutdown
|
2020-11-26 10:31:29 +00:00
|
|
|
logWithCommand.Info("Received interrupt signal, shutting down")
|
2020-08-19 05:58:45 +00:00
|
|
|
statediffService.Stop()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2020-08-19 05:11:55 +00:00
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(serveCmd)
|
|
|
|
|
2020-08-19 05:58:45 +00:00
|
|
|
serveCmd.PersistentFlags().String("http-path", "", "vdb server http path")
|
|
|
|
serveCmd.PersistentFlags().String("ipc-path", "", "vdb server ipc path")
|
2020-08-19 05:11:55 +00:00
|
|
|
|
2020-08-19 05:58:45 +00:00
|
|
|
viper.BindPFlag("server.httpPath", serveCmd.PersistentFlags().Lookup("http-path"))
|
|
|
|
viper.BindPFlag("server.ipcPath", serveCmd.PersistentFlags().Lookup("ipc-path"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func startServers(serv sd.IService) error {
|
2020-11-09 18:15:03 +00:00
|
|
|
viper.BindEnv("server.ipcPath", "SERVER_IPC_PATH")
|
|
|
|
viper.BindEnv("server.httpPath", "SERVER_HTTP_PATH")
|
2020-08-19 05:58:45 +00:00
|
|
|
ipcPath := viper.GetString("server.ipcPath")
|
|
|
|
httpPath := viper.GetString("server.httpPath")
|
|
|
|
if ipcPath == "" && httpPath == "" {
|
|
|
|
logWithCommand.Fatal("need an ipc path and/or an http path")
|
|
|
|
}
|
|
|
|
if ipcPath != "" {
|
2020-08-23 20:00:11 +00:00
|
|
|
logWithCommand.Info("starting up IPC server")
|
2020-08-19 05:58:45 +00:00
|
|
|
_, _, err := rpc.StartIPCEndpoint(ipcPath, serv.APIs())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if httpPath != "" {
|
2020-08-23 20:00:11 +00:00
|
|
|
logWithCommand.Info("starting up HTTP server")
|
2020-11-16 10:15:53 +00:00
|
|
|
handler := rpc.NewServer()
|
|
|
|
if _, _, err := node.StartHTTPEndpoint(httpPath, rpc.HTTPTimeouts{}, handler); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-19 05:58:45 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-08-19 05:11:55 +00:00
|
|
|
|
2020-08-19 05:58:45 +00:00
|
|
|
func chainConfig(chainID uint64) (*params.ChainConfig, error) {
|
|
|
|
switch chainID {
|
|
|
|
case 1:
|
|
|
|
return params.MainnetChainConfig, nil
|
|
|
|
case 3:
|
2020-11-16 09:49:59 +00:00
|
|
|
return params.RopstenChainConfig, nil // Ropsten
|
2020-08-19 05:58:45 +00:00
|
|
|
case 4:
|
|
|
|
return params.RinkebyChainConfig, nil
|
|
|
|
case 5:
|
|
|
|
return params.GoerliChainConfig, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("chain config for chainid %d not available", chainID)
|
|
|
|
}
|
2020-08-19 05:11:55 +00:00
|
|
|
}
|