eth-statediff-service/cmd/serve.go

148 lines
4.3 KiB
Go
Raw Normal View History

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 (
2022-09-02 05:42:55 +00:00
"net/http"
_ "net/http/pprof"
2020-08-19 05:58:45 +00:00
"os"
"os/signal"
2022-09-02 16:51:13 +00:00
"runtime"
2020-08-19 05:58:45 +00:00
"sync"
2020-08-19 05:11:55 +00:00
2020-08-19 05:58:45 +00:00
"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"
sd "github.com/cerc-io/eth-statediff-service/pkg"
srpc "github.com/cerc-io/eth-statediff-service/pkg/rpc"
2020-08-19 05:11:55 +00:00
)
// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve",
Short: "Stand up a standalone statediffing RPC service on top of LevelDB",
2020-08-19 05:58:45 +00:00
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
},
}
2021-10-25 19:06:05 +00:00
func init() {
rootCmd.AddCommand(serveCmd)
}
2022-09-02 16:51:13 +00:00
func maxParallelism() int {
maxProcs := runtime.GOMAXPROCS(0)
numCPU := runtime.NumCPU()
if maxProcs < numCPU {
return maxProcs
}
return numCPU
}
2020-08-19 05:58:45 +00:00
func serve() {
2021-10-21 17:06:06 +00:00
logWithCommand.Info("Running eth-statediff-service serve command")
2022-09-02 16:51:13 +00:00
logWithCommand.Infof("Parallelism: %d", maxParallelism())
2021-10-14 06:47:38 +00:00
reader, chainConf, nodeInfo := instantiateLevelDBReader()
// report latest block info
header, err := reader.GetLatestHeader()
if err != nil {
logWithCommand.Fatalf("Unable to determine latest header height and hash: %s", err.Error())
}
if header.Number == nil {
logWithCommand.Fatal("Latest header found in levelDB has a nil block height")
}
logWithCommand.Infof("Latest block found in the levelDB\r\nheight: %s, hash: %s", header.Number.String(), header.Hash().Hex())
statediffService, err := createStateDiffService(reader, chainConf, nodeInfo)
2020-08-19 05:58:45 +00:00
if err != nil {
logWithCommand.Fatal(err)
}
2022-09-21 19:33:15 +00:00
// Enable the pprof agent if configured
if viper.GetBool("debug.pprof") {
2022-09-02 05:42:55 +00:00
// See: https://www.farsightsecurity.com/blog/txt-record/go-remote-profiling-20161028/
2022-09-21 19:33:15 +00:00
// For security reasons: do not use the default http multiplexor elsewhere in this process.
2022-09-02 05:42:55 +00:00
go func() {
2022-09-02 16:51:13 +00:00
logWithCommand.Info("Starting pprof listener on port 6060")
2022-09-02 05:42:55 +00:00
logWithCommand.Fatal(http.ListenAndServe("localhost:6060", nil))
}()
2022-09-21 19:33:15 +00:00
}
// short circuit if we only want to perform prerun
if viper.GetBool("prerun.only") {
parallel := viper.GetBool("prerun.parallel")
if err := statediffService.Run(nil, parallel); err != nil {
logWithCommand.Fatal("Unable to perform prerun: %v", err)
}
return
}
2020-08-19 05:58:45 +00:00
// 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)
2021-10-25 19:26:41 +00:00
if err := statediffService.Loop(wg); err != nil {
logWithCommand.Fatalf("unable to start statediff service: %v", err)
}
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()
}
2021-10-25 19:06:05 +00:00
func startServers(serv sd.StateDiffService) error {
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")
2020-08-19 05:58:45 +00:00
}
if ipcPath != "" {
logWithCommand.Info("Starting up IPC server")
2021-10-25 20:39:39 +00:00
_, _, err := srpc.StartIPCEndpoint(ipcPath, serv.APIs())
2020-08-19 05:58:45 +00:00
if err != nil {
return err
}
}
if httpPath != "" {
logWithCommand.Info("Starting up HTTP server")
2021-10-25 20:39:39 +00:00
_, err := srpc.StartHTTPEndpoint(httpPath, serv.APIs(), []string{"statediff"}, nil, []string{"*"}, rpc.HTTPTimeouts{})
if err != nil {
2020-11-16 10:15:53 +00:00
return err
}
2021-10-25 20:39:39 +00:00
} else {
logWithCommand.Info("HTTP server is disabled")
2020-08-19 05:58:45 +00:00
}
2021-10-25 20:39:39 +00:00
2020-08-19 05:58:45 +00:00
return nil
}