Skip to content

Commit

Permalink
Rpcdaemon as lib 2 (#943)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored Aug 20, 2020
1 parent fd7e91a commit f827f86
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 29 deletions.
7 changes: 7 additions & 0 deletions cmd/integration/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ var rootCmd = &cobra.Command{
Use: "integration",
Short: "long and heavy integration tests for turbo-geth",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if err := utils.SetupCobra(cmd); err != nil {
panic(err)
}

if len(chaindata) > 0 {
db := ethdb.MustOpen(chaindata)
defer db.Close()
Expand All @@ -20,6 +24,9 @@ var rootCmd = &cobra.Command{
}
}
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
defer utils.StopDebug()
},
}

func RootCommand() *cobra.Command {
Expand Down
4 changes: 0 additions & 4 deletions cmd/integration/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import (

func main() {
rootCmd := commands.RootCommand()
if err := utils.SetupCobra(rootCmd); err != nil {
panic(err)
}
defer utils.StopDebug()

if err := rootCmd.ExecuteContext(utils.RootContext()); err != nil {
fmt.Println(err)
Expand Down
19 changes: 14 additions & 5 deletions cmd/rpcdaemon/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ type Flags struct {
var rootCmd = &cobra.Command{
Use: "rpcdaemon",
Short: "rpcdaemon is JSON RPC server that connects to turbo-geth node for remote DB access",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := utils.SetupCobra(cmd); err != nil {
return err
}
return nil
},
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
utils.StopDebug()
return nil
},
}

func RootCommand() (*cobra.Command, *Flags) {
Expand Down Expand Up @@ -70,20 +80,18 @@ func OpenDB(cfg Flags) (ethdb.KV, ethdb.Backend, error) {
return db, txPool, err
}

func StartRpcServer(ctx context.Context, cfg Flags, rpcAPI []rpc.API) {
func StartRpcServer(ctx context.Context, cfg Flags, rpcAPI []rpc.API) error {
// register apis and create handler stack
httpEndpoint := fmt.Sprintf("%s:%d", cfg.HttpListenAddress, cfg.HttpPort)
srv := rpc.NewServer()
if err := node.RegisterApisFromWhitelist(rpcAPI, cfg.API, srv, false); err != nil {
log.Error("Could not start register RPC apis", "error", err)
return
return fmt.Errorf("could not start register RPC apis: %w", err)
}
handler := node.NewHTTPHandlerStack(srv, cfg.HttpCORSDomain, cfg.HttpVirtualHost)

listener, _, err := node.StartHTTPEndpoint(httpEndpoint, rpc.DefaultHTTPTimeouts, handler)
if err != nil {
log.Error("Could not start RPC api", "error", err)
return
return fmt.Errorf("could not start RPC api: %w", err)
}
extapiURL := fmt.Sprintf("http://%s", httpEndpoint)
log.Info("HTTP endpoint opened", "url", extapiURL)
Expand All @@ -94,4 +102,5 @@ func StartRpcServer(ctx context.Context, cfg Flags, rpcAPI []rpc.API) {
}()
sig := <-ctx.Done()
log.Info("Exiting...", "signal", sig)
return nil
}
14 changes: 5 additions & 9 deletions cmd/rpcdaemon/commands/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package commands
import (
"context"
"fmt"

"github.com/ledgerwatch/turbo-geth/cmd/rpcdaemon/cli"
"github.com/ledgerwatch/turbo-geth/core/rawdb"
"github.com/ledgerwatch/turbo-geth/ethdb"
Expand Down Expand Up @@ -34,7 +33,7 @@ func (api *APIImpl) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber
}

func APIList(db ethdb.KV, eth ethdb.Backend, cfg cli.Flags, customApiList []rpc.API) []rpc.API {
var rpcAPI []rpc.API
var defaultAPIList []rpc.API

dbReader := ethdb.NewObjectDatabase(db)
apiImpl := NewAPI(db, dbReader, eth, cfg.Gascap)
Expand All @@ -44,32 +43,29 @@ func APIList(db ethdb.KV, eth ethdb.Backend, cfg cli.Flags, customApiList []rpc.
for _, enabledAPI := range cfg.API {
switch enabledAPI {
case "eth":
rpcAPI = append(rpcAPI, rpc.API{
defaultAPIList = append(defaultAPIList, rpc.API{
Namespace: "eth",
Public: true,
Service: EthAPI(apiImpl),
Version: "1.0",
})
case "debug":
rpcAPI = append(rpcAPI, rpc.API{
defaultAPIList = append(defaultAPIList, rpc.API{
Namespace: "debug",
Public: true,
Service: PrivateDebugAPI(dbgAPIImpl),
Version: "1.0",
})
case "net":
rpcAPI = append(rpcAPI, rpc.API{
defaultAPIList = append(defaultAPIList, rpc.API{
Namespace: "net",
Public: true,
Service: NetAPI(netImpl),
Version: "1.0",
})

default:
// TODO: enable validation after checking customApiList
//log.Error("Unrecognised", "api", enabledAPI)
}
}

return append(rpcAPI, customApiList...)
return append(defaultAPIList, customApiList...)
}
12 changes: 3 additions & 9 deletions cmd/rpcdaemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,15 @@ import (

func main() {
cmd, cfg := cli.RootCommand()
if err := utils.SetupCobra(cmd); err != nil {
panic(err)
}
defer utils.StopDebug()

cmd.RunE = func(cmd *cobra.Command, args []string) error {
db, txPool, err := cli.OpenDB(*cfg)
db, backend, err := cli.OpenDB(*cfg)
if err != nil {
log.Error("Could not connect to remoteDb", "error", err)
return nil
}

var rpcAPI = commands.APIList(db, txPool, *cfg, nil)
cli.StartRpcServer(cmd.Context(), *cfg, rpcAPI)
return nil
var apiList = commands.APIList(db, backend, *cfg, nil)
return cli.StartRpcServer(cmd.Context(), *cfg, apiList)
}

if err := cmd.ExecuteContext(utils.RootContext()); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/state/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
)

func init() {
utils.CobraFlags(rootCmd, append(debug.Flags, utils.MetricsEnabledFlag, utils.MetricsEnabledExpensiveFlag, utils.MetricsHTTPFlag, utils.MetricsPortFlag))
utils.CobraFlags(rootCmd, append(debug.Flags, utils.MetricFlags...))
rootCmd.PersistentFlags().StringVar(&genesisPath, "genesis", "", "path to genesis.json file")
}

Expand Down
6 changes: 5 additions & 1 deletion internal/debug/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import (
"os/signal"
"runtime"
"syscall"
"time"

"github.com/fjl/memsize/memsizeui"
"github.com/ledgerwatch/turbo-geth/log"
"github.com/ledgerwatch/turbo-geth/metrics"
"github.com/ledgerwatch/turbo-geth/metrics/exp"

"github.com/spf13/cobra"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -219,6 +219,10 @@ func SetupCobra(cmd *cobra.Command) error {
return err
}

if metrics.Enabled {
go metrics.CollectProcessMetrics(3 * time.Second) // Start system runtime metrics collection
}

if metrics.Enabled && metricsAddr != "" {
address := fmt.Sprintf("%s:%d", metricsAddr, metricsPort)
log.Info("Enabling stand-alone metrics HTTP endpoint", "addr", address)
Expand Down

0 comments on commit f827f86

Please sign in to comment.