Skip to content

Commit f827f86

Browse files
Rpcdaemon as lib 2 (#943)
1 parent fd7e91a commit f827f86

File tree

7 files changed

+35
-29
lines changed

7 files changed

+35
-29
lines changed

cmd/integration/commands/root.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ var rootCmd = &cobra.Command{
1212
Use: "integration",
1313
Short: "long and heavy integration tests for turbo-geth",
1414
PersistentPreRun: func(cmd *cobra.Command, args []string) {
15+
if err := utils.SetupCobra(cmd); err != nil {
16+
panic(err)
17+
}
18+
1519
if len(chaindata) > 0 {
1620
db := ethdb.MustOpen(chaindata)
1721
defer db.Close()
@@ -20,6 +24,9 @@ var rootCmd = &cobra.Command{
2024
}
2125
}
2226
},
27+
PersistentPostRun: func(cmd *cobra.Command, args []string) {
28+
defer utils.StopDebug()
29+
},
2330
}
2431

2532
func RootCommand() *cobra.Command {

cmd/integration/main.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ import (
99

1010
func main() {
1111
rootCmd := commands.RootCommand()
12-
if err := utils.SetupCobra(rootCmd); err != nil {
13-
panic(err)
14-
}
15-
defer utils.StopDebug()
1612

1713
if err := rootCmd.ExecuteContext(utils.RootContext()); err != nil {
1814
fmt.Println(err)

cmd/rpcdaemon/cli/config.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ type Flags struct {
2626
var rootCmd = &cobra.Command{
2727
Use: "rpcdaemon",
2828
Short: "rpcdaemon is JSON RPC server that connects to turbo-geth node for remote DB access",
29+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
30+
if err := utils.SetupCobra(cmd); err != nil {
31+
return err
32+
}
33+
return nil
34+
},
35+
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
36+
utils.StopDebug()
37+
return nil
38+
},
2939
}
3040

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

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

8392
listener, _, err := node.StartHTTPEndpoint(httpEndpoint, rpc.DefaultHTTPTimeouts, handler)
8493
if err != nil {
85-
log.Error("Could not start RPC api", "error", err)
86-
return
94+
return fmt.Errorf("could not start RPC api: %w", err)
8795
}
8896
extapiURL := fmt.Sprintf("http://%s", httpEndpoint)
8997
log.Info("HTTP endpoint opened", "url", extapiURL)
@@ -94,4 +102,5 @@ func StartRpcServer(ctx context.Context, cfg Flags, rpcAPI []rpc.API) {
94102
}()
95103
sig := <-ctx.Done()
96104
log.Info("Exiting...", "signal", sig)
105+
return nil
97106
}

cmd/rpcdaemon/commands/daemon.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package commands
33
import (
44
"context"
55
"fmt"
6-
76
"github.com/ledgerwatch/turbo-geth/cmd/rpcdaemon/cli"
87
"github.com/ledgerwatch/turbo-geth/core/rawdb"
98
"github.com/ledgerwatch/turbo-geth/ethdb"
@@ -34,7 +33,7 @@ func (api *APIImpl) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber
3433
}
3534

3635
func APIList(db ethdb.KV, eth ethdb.Backend, cfg cli.Flags, customApiList []rpc.API) []rpc.API {
37-
var rpcAPI []rpc.API
36+
var defaultAPIList []rpc.API
3837

3938
dbReader := ethdb.NewObjectDatabase(db)
4039
apiImpl := NewAPI(db, dbReader, eth, cfg.Gascap)
@@ -44,32 +43,29 @@ func APIList(db ethdb.KV, eth ethdb.Backend, cfg cli.Flags, customApiList []rpc.
4443
for _, enabledAPI := range cfg.API {
4544
switch enabledAPI {
4645
case "eth":
47-
rpcAPI = append(rpcAPI, rpc.API{
46+
defaultAPIList = append(defaultAPIList, rpc.API{
4847
Namespace: "eth",
4948
Public: true,
5049
Service: EthAPI(apiImpl),
5150
Version: "1.0",
5251
})
5352
case "debug":
54-
rpcAPI = append(rpcAPI, rpc.API{
53+
defaultAPIList = append(defaultAPIList, rpc.API{
5554
Namespace: "debug",
5655
Public: true,
5756
Service: PrivateDebugAPI(dbgAPIImpl),
5857
Version: "1.0",
5958
})
6059
case "net":
61-
rpcAPI = append(rpcAPI, rpc.API{
60+
defaultAPIList = append(defaultAPIList, rpc.API{
6261
Namespace: "net",
6362
Public: true,
6463
Service: NetAPI(netImpl),
6564
Version: "1.0",
6665
})
6766

68-
default:
69-
// TODO: enable validation after checking customApiList
70-
//log.Error("Unrecognised", "api", enabledAPI)
7167
}
7268
}
7369

74-
return append(rpcAPI, customApiList...)
70+
return append(defaultAPIList, customApiList...)
7571
}

cmd/rpcdaemon/main.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,15 @@ import (
1212

1313
func main() {
1414
cmd, cfg := cli.RootCommand()
15-
if err := utils.SetupCobra(cmd); err != nil {
16-
panic(err)
17-
}
18-
defer utils.StopDebug()
19-
2015
cmd.RunE = func(cmd *cobra.Command, args []string) error {
21-
db, txPool, err := cli.OpenDB(*cfg)
16+
db, backend, err := cli.OpenDB(*cfg)
2217
if err != nil {
2318
log.Error("Could not connect to remoteDb", "error", err)
2419
return nil
2520
}
2621

27-
var rpcAPI = commands.APIList(db, txPool, *cfg, nil)
28-
cli.StartRpcServer(cmd.Context(), *cfg, rpcAPI)
29-
return nil
22+
var apiList = commands.APIList(db, backend, *cfg, nil)
23+
return cli.StartRpcServer(cmd.Context(), *cfg, apiList)
3024
}
3125

3226
if err := cmd.ExecuteContext(utils.RootContext()); err != nil {

cmd/state/commands/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var (
2121
)
2222

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

internal/debug/flags.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ import (
2424
"os/signal"
2525
"runtime"
2626
"syscall"
27+
"time"
2728

2829
"github.com/fjl/memsize/memsizeui"
2930
"github.com/ledgerwatch/turbo-geth/log"
3031
"github.com/ledgerwatch/turbo-geth/metrics"
3132
"github.com/ledgerwatch/turbo-geth/metrics/exp"
32-
3333
"github.com/spf13/cobra"
3434
"github.com/urfave/cli"
3535
)
@@ -219,6 +219,10 @@ func SetupCobra(cmd *cobra.Command) error {
219219
return err
220220
}
221221

222+
if metrics.Enabled {
223+
go metrics.CollectProcessMetrics(3 * time.Second) // Start system runtime metrics collection
224+
}
225+
222226
if metrics.Enabled && metricsAddr != "" {
223227
address := fmt.Sprintf("%s:%d", metricsAddr, metricsPort)
224228
log.Info("Enabling stand-alone metrics HTTP endpoint", "addr", address)

0 commit comments

Comments
 (0)