2021-12-30 14:28:25 +00:00
|
|
|
// VulcanizeDB
|
2021-12-30 21:56:17 +00:00
|
|
|
// Copyright © 2022 Vulcanize
|
2021-12-30 14:28:25 +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 (
|
2021-12-30 21:56:17 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync"
|
2021-12-30 14:28:25 +00:00
|
|
|
|
2021-12-30 21:56:17 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-12-30 14:28:25 +00:00
|
|
|
"github.com/spf13/cobra"
|
2021-12-31 18:17:57 +00:00
|
|
|
"github.com/spf13/viper"
|
2021-12-30 21:56:17 +00:00
|
|
|
|
2021-12-31 18:17:57 +00:00
|
|
|
leveldb_ethdb_rpc "github.com/vulcanize/leveldb-ethdb-rpc/pkg"
|
2021-12-30 21:56:17 +00:00
|
|
|
srpc "github.com/vulcanize/leveldb-ethdb-rpc/pkg/rpc"
|
2021-12-30 22:10:12 +00:00
|
|
|
"github.com/vulcanize/leveldb-ethdb-rpc/version"
|
2021-12-30 21:56:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
subCommand string
|
|
|
|
logWithCommand log.Entry
|
2021-12-30 14:28:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// serveCmd represents the serve command
|
|
|
|
var serveCmd = &cobra.Command{
|
|
|
|
Use: "serve",
|
2021-12-30 21:56:17 +00:00
|
|
|
Short: "RPC Server for LevelDB eth.Database",
|
|
|
|
Long: `This service exposes a remote RPC server interface for a local levelDB backed ethdb.Database`,
|
2021-12-30 14:28:25 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-12-30 21:56:17 +00:00
|
|
|
subCommand = cmd.CalledAs()
|
|
|
|
logWithCommand = *log.WithField("SubCommand", subCommand)
|
|
|
|
serve()
|
2021-12-30 14:28:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-12-30 21:56:17 +00:00
|
|
|
func serve() {
|
2021-12-30 22:10:12 +00:00
|
|
|
logWithCommand.Infof("running ipld-eth-server version: %s", version.VersionWithMeta)
|
2021-12-30 21:56:17 +00:00
|
|
|
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
logWithCommand.Debug("loading server configuration variables")
|
2021-12-31 18:17:57 +00:00
|
|
|
serverConfig, err := leveldb_ethdb_rpc.NewConfig()
|
2021-12-30 21:56:17 +00:00
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
|
|
|
logWithCommand.Infof("server config: %+v", serverConfig)
|
|
|
|
logWithCommand.Debug("initializing new server service")
|
2021-12-31 18:17:57 +00:00
|
|
|
server, err := leveldb_ethdb_rpc.NewServer(serverConfig)
|
2021-12-30 21:56:17 +00:00
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logWithCommand.Info("starting up servers")
|
|
|
|
server.Serve(wg)
|
|
|
|
if err := startServers(server, serverConfig); err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
shutdown := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(shutdown, os.Interrupt)
|
|
|
|
<-shutdown
|
|
|
|
server.Stop()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2021-12-31 18:17:57 +00:00
|
|
|
func startServers(server leveldb_ethdb_rpc.Server, settings *leveldb_ethdb_rpc.Config) error {
|
2021-12-30 21:56:17 +00:00
|
|
|
if settings.IPCEnabled {
|
|
|
|
logWithCommand.Info("starting up IPC server")
|
|
|
|
_, _, err := srpc.StartIPCEndpoint(settings.IPCEndpoint, server.APIs())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logWithCommand.Info("IPC server is disabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
if settings.HTTPEnabled {
|
|
|
|
logWithCommand.Info("starting up HTTP server")
|
|
|
|
_, err := srpc.StartHTTPEndpoint(settings.HTTPEndpoint, server.APIs(), []string{"leveldb"}, nil, []string{"*"}, rpc.HTTPTimeouts{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logWithCommand.Info("HTTP server is disabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-30 14:28:25 +00:00
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(serveCmd)
|
|
|
|
|
2021-12-31 18:17:57 +00:00
|
|
|
// CLI flags
|
|
|
|
serveCmd.PersistentFlags().Bool("ipc-enabled", false, "turn on ipc server")
|
|
|
|
serveCmd.PersistentFlags().String("ipc-path", "", "ipc server endpoint")
|
|
|
|
serveCmd.PersistentFlags().Bool("http-enabled", true, "turn on http server; on by default")
|
|
|
|
serveCmd.PersistentFlags().String("http-path", "127.0.0.1:8500", "http server endpoint; default = 127.0.0.1:8545")
|
|
|
|
|
|
|
|
serveCmd.PersistentFlags().String("leveldb-path", "", "leveldb filesystem path")
|
|
|
|
serveCmd.PersistentFlags().Int("leveldb-cache-size", 0, "leveldb cache size")
|
|
|
|
serveCmd.PersistentFlags().String("leveldb-ancient-path", "", "filesystem path to freezer")
|
|
|
|
serveCmd.PersistentFlags().String("leveldb-namespace", "eth/db/chaindata/", "leveldb namespace")
|
|
|
|
|
|
|
|
// toml bindings
|
|
|
|
viper.BindPFlag(leveldb_ethdb_rpc.TOML_IPC_ENABLED, serveCmd.PersistentFlags().Lookup("ipc-enabled"))
|
|
|
|
viper.BindPFlag(leveldb_ethdb_rpc.TOML_IPC_ENDPOINT, serveCmd.PersistentFlags().Lookup("ipc-path"))
|
|
|
|
viper.BindPFlag(leveldb_ethdb_rpc.TOML_HTTP_ENABLED, serveCmd.PersistentFlags().Lookup("http-enabled"))
|
|
|
|
viper.BindPFlag(leveldb_ethdb_rpc.TOML_HTTP_ENDPOINT, serveCmd.PersistentFlags().Lookup("http-path"))
|
|
|
|
|
|
|
|
viper.BindPFlag(leveldb_ethdb_rpc.TOML_LEVELDB_PATH, serveCmd.PersistentFlags().Lookup("leveldb-path"))
|
|
|
|
viper.BindPFlag(leveldb_ethdb_rpc.TOML_LEVELDB_CACHE_SIZE, serveCmd.PersistentFlags().Lookup("leveldb-cache-size"))
|
|
|
|
viper.BindPFlag(leveldb_ethdb_rpc.TOML_LEVELDB_ANCIENT_PATH, serveCmd.PersistentFlags().Lookup("leveldb-ancient-path"))
|
|
|
|
viper.BindPFlag(leveldb_ethdb_rpc.TOML_LEVELDB_NAMESPACE, serveCmd.PersistentFlags().Lookup("leveldb-namespace"))
|
2021-12-30 14:28:25 +00:00
|
|
|
}
|