ipld-eth-server/cmd/serve.go

119 lines
4.0 KiB
Go
Raw Normal View History

2020-08-31 15:58:16 +00:00
// Copyright © 2020 Vulcanize, Inc
//
// 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 (
"os"
"os/signal"
2020-09-02 15:19:25 +00:00
"sync"
2020-08-31 15:58:16 +00:00
"github.com/ethereum/go-ethereum/rpc"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
2020-09-02 15:19:25 +00:00
"github.com/vulcanize/ipld-eth-indexer/pkg/eth"
2020-08-31 15:58:16 +00:00
srpc "github.com/vulcanize/ipld-eth-server/pkg/rpc"
2020-09-02 15:19:25 +00:00
s "github.com/vulcanize/ipld-eth-server/pkg/serve"
2020-08-31 15:58:16 +00:00
v "github.com/vulcanize/ipld-eth-server/version"
)
2020-09-02 15:19:25 +00:00
// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve",
2020-08-31 15:58:16 +00:00
Short: "serve chain data from PG-IPFS",
Long: `This command configures a VulcanizeDB ipld-eth-server.
`,
Run: func(cmd *cobra.Command, args []string) {
subCommand = cmd.CalledAs()
logWithCommand = *log.WithField("SubCommand", subCommand)
2020-09-02 15:19:25 +00:00
serve()
2020-08-31 15:58:16 +00:00
},
}
2020-09-02 15:19:25 +00:00
func serve() {
2020-08-31 15:58:16 +00:00
logWithCommand.Infof("running ipld-eth-server version: %s", v.VersionWithMeta)
var forwardPayloadChan chan eth.ConvertedPayload
2020-09-02 15:19:25 +00:00
wg := new(sync.WaitGroup)
logWithCommand.Debug("loading server configuration variables")
serverConfig, err := s.NewConfig()
2020-08-31 15:58:16 +00:00
if err != nil {
logWithCommand.Fatal(err)
}
2020-09-02 15:19:25 +00:00
logWithCommand.Infof("server config: %+v", serverConfig)
logWithCommand.Debug("initializing new server service")
server, err := s.NewServer(serverConfig)
2020-08-31 15:58:16 +00:00
if err != nil {
logWithCommand.Fatal(err)
}
2020-09-02 15:19:25 +00:00
logWithCommand.Info("starting up server servers")
forwardPayloadChan = make(chan eth.ConvertedPayload, s.PayloadChanBufferSize)
server.Serve(wg, forwardPayloadChan)
if err := startServers(server, serverConfig); err != nil {
2020-08-31 15:58:16 +00:00
logWithCommand.Fatal(err)
}
shutdown := make(chan os.Signal)
signal.Notify(shutdown, os.Interrupt)
<-shutdown
2020-09-02 15:19:25 +00:00
server.Stop()
2020-08-31 15:58:16 +00:00
wg.Wait()
}
2020-09-02 15:19:25 +00:00
func startServers(server s.Server, settings *s.Config) error {
logWithCommand.Info("starting up IPC server")
_, _, err := srpc.StartIPCEndpoint(settings.IPCEndpoint, server.APIs())
2020-08-31 15:58:16 +00:00
if err != nil {
return err
}
logWithCommand.Info("starting up WS server")
_, _, err = srpc.StartWSEndpoint(settings.WSEndpoint, server.APIs(), []string{"vdb"}, nil, true)
2020-08-31 15:58:16 +00:00
if err != nil {
return err
}
logWithCommand.Info("starting up HTTP server")
_, _, err = srpc.StartHTTPEndpoint(settings.HTTPEndpoint, server.APIs(), []string{"eth"}, nil, []string{"*"}, rpc.HTTPTimeouts{})
2020-08-31 15:58:16 +00:00
return err
}
func init() {
2020-09-02 15:19:25 +00:00
rootCmd.AddCommand(serveCmd)
2020-08-31 15:58:16 +00:00
// flags for all config variables
2020-09-02 15:19:25 +00:00
serveCmd.PersistentFlags().String("server-ws-path", "", "vdb server ws path")
serveCmd.PersistentFlags().String("server-http-path", "", "vdb server http path")
serveCmd.PersistentFlags().String("server-ipc-path", "", "vdb server ipc path")
2020-08-31 15:58:16 +00:00
serveCmd.PersistentFlags().String("eth-chain-id", "1", "eth chain id")
serveCmd.PersistentFlags().String("eth-default-sender", "", "default sender address")
serveCmd.PersistentFlags().String("eth-rpc-gas-cap", "", "rpc gas cap (for eth_Call execution)")
2020-08-31 15:58:16 +00:00
// and their bindings
2020-09-02 15:19:25 +00:00
viper.BindPFlag("server.wsPath", serveCmd.PersistentFlags().Lookup("server-ws-path"))
viper.BindPFlag("server.httpPath", serveCmd.PersistentFlags().Lookup("server-http-path"))
viper.BindPFlag("server.ipcPath", serveCmd.PersistentFlags().Lookup("server-ipc-path"))
2020-10-21 11:22:43 +00:00
viper.BindPFlag("ethereum.chainID", serveCmd.PersistentFlags().Lookup("eth-chain-id"))
viper.BindPFlag("ethereum.defaultSender", serveCmd.PersistentFlags().Lookup("eth-default-sender"))
viper.BindPFlag("ethereum.rpcGasCap", serveCmd.PersistentFlags().Lookup("eth-rpc-gas-cap"))
2020-08-31 15:58:16 +00:00
}