-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathroot.go
More file actions
108 lines (94 loc) · 3.86 KB
/
Copy pathroot.go
File metadata and controls
108 lines (94 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)
package root
import (
"context"
"github.com/cartesi/rollups-node/internal/cli"
"github.com/cartesi/rollups-node/internal/config"
"github.com/cartesi/rollups-node/internal/evmreader"
"github.com/cartesi/rollups-node/internal/repository/factory"
"github.com/cartesi/rollups-node/internal/version"
"github.com/cartesi/rollups-node/pkg/service"
"github.com/spf13/cobra"
)
var (
logLevel string
logColor bool
defaultBlockString string
blockchainHTTPEndpoint string
pollInterval string
databaseConnection string
maxStartupTime string
enableInputReader bool
telemetryAddress string
cfg *config.EvmreaderConfig
maxBlockRange uint64
)
var Cmd = &cobra.Command{
Use: "cartesi-rollups-" + config.ServiceEvmReader,
Short: "Runs cartesi-rollups-" + config.ServiceEvmReader,
Long: "Runs cartesi-rollups-" + config.ServiceEvmReader + " in standalone mode",
Run: run,
Version: version.BuildVersion,
}
func init() {
flags := Cmd.Flags()
config.SetDefaults()
cli.AddFlagStrVarP(flags, &defaultBlockString, "default-block", "d", config.BLOCKCHAIN_DEFAULT_BLOCK,
"Default block to be used when fetching new blocks.\nOne of 'latest', 'safe', 'pending', 'finalized'")
cli.AddFlagStrVar(flags, &telemetryAddress, "telemetry-address", config.EVM_READER_TELEMETRY_ADDRESS,
"Health check and metrics address and port")
cli.AddFlagStrVar(flags, &logLevel, "log-level", config.LOG_LEVEL,
"Log level: debug, info, warn or error")
cli.AddFlagBoolVar(flags, &logColor, "log-color", config.LOG_COLOR,
"Tint the logs (colored output)")
cli.AddFlagStrVar(flags, &databaseConnection, "database-connection", config.DATABASE_CONNECTION,
"Database connection string in the URL format\n(eg.: 'postgres://user:password@hostname:port/database') ")
cli.AddFlagStrVar(flags, &blockchainHTTPEndpoint, "blockchain-http-endpoint", config.BLOCKCHAIN_HTTP_ENDPOINT,
"Blockchain http endpoint")
cli.AddFlagStrVar(flags, &pollInterval, "poll-interval", config.EVM_READER_POLLING_INTERVAL,
"Poll interval")
cli.AddFlagStrVar(flags, &maxStartupTime, "max-startup-time", config.MAX_STARTUP_TIME,
"Maximum startup time in seconds")
cli.AddFlagBoolVar(flags, &enableInputReader, "input-reader", config.FEATURE_INPUT_READER_ENABLED,
"Enable or disable the input reader (for external input readers)")
cli.AddFlagUint64Var(flags, &maxBlockRange, "max-block-range", config.BLOCKCHAIN_MAX_BLOCK_RANGE,
"Maximum number of blocks in a single query. large queries will be split automatically. Zero for unlimited.")
// TODO: validate on preRunE
Cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
var err error
cfg, err = config.LoadEvmreaderConfig()
if err != nil {
return err
}
return nil
}
}
func run(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithTimeout(context.Background(), cfg.MaxStartupTime)
defer cancel()
name := config.ServiceEvmReader
logger := service.NewLogger(name, cfg.LogLevel, cfg.LogColor)
repo, err := factory.NewRepositoryFromConnectionString(ctx, cfg.DatabaseConnection.Raw())
cli.CheckErr(logger, err)
defer repo.Close()
supCfg := &service.SupervisorConfigs{
BaseConfigs: service.BaseConfigs{Name: name, Logger: logger},
EnableSignalHandling: true,
TelemetryCreate: true,
TelemetryAddress: cfg.EvmReaderTelemetryAddress,
Factories: []service.FactoryFunction{
func(ctx context.Context, sup *service.Supervisor) (service.SupervisedService, error) {
return evmreader.Create(ctx, &evmreader.CreateInfo{
Config: *cfg,
Logger: sup.Logger,
Repository: repo,
})
},
},
}
sup, err := service.NewSupervisor(ctx, supCfg)
cli.CheckErr(logger, err)
defer sup.Close()
cli.CheckErr(logger, sup.Serve())
}