Skip to content

Commit

Permalink
feat: aggregate node config in a single struct
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoura committed Feb 15, 2024
1 parent cbab2d2 commit 0b23315
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 107 deletions.
25 changes: 18 additions & 7 deletions cmd/cartesi-rollups-node/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,39 @@ func newHttpServiceHandler(nodeConfig config.NodeConfig) http.Handler {
handler := http.NewServeMux()
handler.Handle("/healthz", http.HandlerFunc(healthcheckHandler))

graphqlProxy, err := newReverseProxy(nodeConfig, getPort(nodeConfig, portOffsetGraphQLServer))
graphqlProxy, err := newReverseProxy(
nodeConfig.CartesiHttpAddress,
getPort(nodeConfig.CartesiHttpPort, portOffsetGraphQLServer),
)
if err != nil {
config.ErrorLogger.Fatal(err)
}
handler.Handle("/graphql", graphqlProxy)

dispatcherProxy, err := newReverseProxy(nodeConfig, getPort(nodeConfig, portOffsetDispatcher))
dispatcherProxy, err := newReverseProxy(
nodeConfig.CartesiHttpAddress,
getPort(nodeConfig.CartesiHttpPort, portOffsetDispatcher),
)
if err != nil {
config.ErrorLogger.Fatal(err)
}
handler.Handle("/metrics", dispatcherProxy)

inspectProxy, err := newReverseProxy(nodeConfig, getPort(nodeConfig, portOffsetInspectServer))
inspectProxy, err := newReverseProxy(
nodeConfig.CartesiHttpAddress,
getPort(nodeConfig.CartesiHttpPort, portOffsetInspectServer),
)
if err != nil {
config.ErrorLogger.Fatal(err)
}
handler.Handle("/inspect", inspectProxy)
handler.Handle("/inspect/", inspectProxy)

if nodeConfig.CartesiFeatureHostMode {
hostProxy, err := newReverseProxy(nodeConfig,
getPort(nodeConfig, portOffsetHostRunnerRollups))
hostProxy, err := newReverseProxy(
nodeConfig.CartesiHttpAddress,
getPort(nodeConfig.CartesiHttpPort, portOffsetHostRunnerRollups),
)
if err != nil {
config.ErrorLogger.Fatal(err)
}
Expand All @@ -51,10 +62,10 @@ func healthcheckHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}

func newReverseProxy(nodeConfig config.NodeConfig, port int) (*httputil.ReverseProxy, error) {
func newReverseProxy(httpAddress string, port int) (*httputil.ReverseProxy, error) {
urlStr := fmt.Sprintf(
"http://%v:%v/",
nodeConfig.CartesiHttpAddress,
httpAddress,
port,
)

Expand Down
142 changes: 100 additions & 42 deletions cmd/cartesi-rollups-node/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,20 @@ const (
)

// Get the port of the given service.
func getPort(nodeConfig config.NodeConfig, offset portOffset) int {
return nodeConfig.CartesiHttpPort + int(offset)
func getPort(httpPort int, offset portOffset) int {
return httpPort + int(offset)
}

// Get the redis endpoint based on whether the experimental sunodo validator mode is enabled.
func getRedisEndpoint(nodeConfig config.NodeConfig) string {
if nodeConfig.CartesiExperimentalSunodoValidatorEnabled {
return nodeConfig.CartesiExperimentalSunodoValidatorRedisEndpoint
} else {
return fmt.Sprintf("redis://%v:%v", localhost, getPort(nodeConfig, portOffsetRedis))
return fmt.Sprintf(
"redis://%v:%v",
localhost,
getPort(nodeConfig.CartesiHttpPort, portOffsetRedis),
)
}
}

Expand All @@ -70,15 +74,17 @@ func getRustLog(nodeConfig config.NodeConfig, rustModule string) string {
func newAdvanceRunner(nodeConfig config.NodeConfig) services.CommandService {
var s services.CommandService
s.Name = "advance-runner"
s.HealthcheckPort = getPort(nodeConfig, portOffsetAdvanceRunner)
s.HealthcheckPort = getPort(nodeConfig.CartesiHttpPort, portOffsetAdvanceRunner)
s.Path = "cartesi-rollups-advance-runner"
s.Env = append(s.Env, "LOG_ENABLE_TIMESTAMP=false")
s.Env = append(s.Env, "LOG_ENABLE_COLOR=false")
s.Env = append(s.Env, getRustLog(nodeConfig, "advance_runner"))
s.Env = append(s.Env,
fmt.Sprintf("SERVER_MANAGER_ENDPOINT=http://%v:%v",
localhost,
getPort(nodeConfig, portOffsetServerManager)))
getPort(nodeConfig.CartesiHttpPort, portOffsetServerManager),
),
)
s.Env = append(s.Env,
fmt.Sprintf("SESSION_ID=%v", serverManagerSessionId))
s.Env = append(s.Env,
Expand All @@ -91,7 +97,9 @@ func newAdvanceRunner(nodeConfig config.NodeConfig) services.CommandService {
fmt.Sprintf("PROVIDER_HTTP_ENDPOINT=%v", nodeConfig.CartesiBlockchainHttpEndpoint))
s.Env = append(s.Env,
fmt.Sprintf("ADVANCE_RUNNER_HEALTHCHECK_PORT=%v",
getPort(nodeConfig, portOffsetAdvanceRunner)))
getPort(nodeConfig.CartesiHttpPort, portOffsetAdvanceRunner),
),
)
s.Env = append(s.Env,
fmt.Sprintf("READER_MODE=%v", nodeConfig.CartesiFeatureDisableClaimer))
if nodeConfig.CartesiFeatureHostMode || nodeConfig.CartesiFeatureDisableMachineHashCheck {
Expand All @@ -108,7 +116,7 @@ func newAdvanceRunner(nodeConfig config.NodeConfig) services.CommandService {
func newAuthorityClaimer(nodeConfig config.NodeConfig) services.CommandService {
var s services.CommandService
s.Name = "authority-claimer"
s.HealthcheckPort = getPort(nodeConfig, portOffsetAuthorityClaimer)
s.HealthcheckPort = getPort(nodeConfig.CartesiHttpPort, portOffsetAuthorityClaimer)
s.Path = "cartesi-rollups-authority-claimer"
s.Env = append(s.Env, "LOG_ENABLE_TIMESTAMP=false")
s.Env = append(s.Env, "LOG_ENABLE_COLOR=false")
Expand All @@ -132,9 +140,12 @@ func newAuthorityClaimer(nodeConfig config.NodeConfig) services.CommandService {
s.Env = append(s.Env,
fmt.Sprintf("GENESIS_BLOCK=%v", nodeConfig.CartesiContractsInputBoxDeploymentBlockNumber))
s.Env = append(s.Env,
fmt.Sprintf("AUTHORITY_CLAIMER_HTTP_SERVER_PORT=%v",
getPort(nodeConfig, portOffsetAuthorityClaimer)))
switch auth := config.GetAuth().(type) {
fmt.Sprintf(
"AUTHORITY_CLAIMER_HTTP_SERVER_PORT=%v",
getPort(nodeConfig.CartesiHttpPort, portOffsetAuthorityClaimer),
),
)
switch auth := nodeConfig.CartesiAuth.(type) {
case config.AuthMnemonic:
s.Env = append(s.Env,
fmt.Sprintf("TX_SIGNING_MNEMONIC=%v", auth.Mnemonic))
Expand All @@ -155,14 +166,17 @@ func newAuthorityClaimer(nodeConfig config.NodeConfig) services.CommandService {
func newDispatcher(nodeConfig config.NodeConfig) services.CommandService {
var s services.CommandService
s.Name = "dispatcher"
s.HealthcheckPort = getPort(nodeConfig, portOffsetDispatcher)
s.HealthcheckPort = getPort(nodeConfig.CartesiHttpPort, portOffsetDispatcher)
s.Path = "cartesi-rollups-dispatcher"
s.Env = append(s.Env, "LOG_ENABLE_TIMESTAMP=false")
s.Env = append(s.Env, "LOG_ENABLE_COLOR=false")
s.Env = append(s.Env, getRustLog(nodeConfig, "dispatcher"))
s.Env = append(s.Env,
fmt.Sprintf("SC_GRPC_ENDPOINT=http://%v:%v",
localhost, getPort(nodeConfig, portOffsetStateServer)))
fmt.Sprintf(
"SC_GRPC_ENDPOINT=http://%v:%v",
localhost, getPort(nodeConfig.CartesiHttpPort, portOffsetStateServer),
),
)
s.Env = append(s.Env,
fmt.Sprintf("SC_DEFAULT_CONFIRMATIONS=%v", nodeConfig.CartesiBlockchainFinalityOffset))
s.Env = append(s.Env,
Expand All @@ -183,15 +197,19 @@ func newDispatcher(nodeConfig config.NodeConfig) services.CommandService {
s.Env = append(s.Env,
fmt.Sprintf("CHAIN_ID=%v", nodeConfig.CartesiBlockchainId))
s.Env = append(s.Env,
fmt.Sprintf("DISPATCHER_HTTP_SERVER_PORT=%v", getPort(nodeConfig, portOffsetDispatcher)))
fmt.Sprintf(
"DISPATCHER_HTTP_SERVER_PORT=%v",
getPort(nodeConfig.CartesiHttpPort, portOffsetDispatcher),
),
)
s.Env = append(s.Env, os.Environ()...)
return s
}

func newGraphQLServer(nodeConfig config.NodeConfig) services.CommandService {
var s services.CommandService
s.Name = "graphql-server"
s.HealthcheckPort = getPort(nodeConfig, portOffsetGraphQLHealthcheck)
s.HealthcheckPort = getPort(nodeConfig.CartesiHttpPort, portOffsetGraphQLHealthcheck)
s.Path = "cartesi-rollups-graphql-server"
s.Env = append(s.Env, "LOG_ENABLE_TIMESTAMP=false")
s.Env = append(s.Env, "LOG_ENABLE_COLOR=false")
Expand All @@ -200,39 +218,49 @@ func newGraphQLServer(nodeConfig config.NodeConfig) services.CommandService {
fmt.Sprintf("POSTGRES_ENDPOINT=%v", nodeConfig.CartesiPostgresEndpoint))
s.Env = append(s.Env, fmt.Sprintf("GRAPHQL_HOST=%v", localhost))
s.Env = append(s.Env,
fmt.Sprintf("GRAPHQL_PORT=%v", getPort(nodeConfig, portOffsetGraphQLServer)))
fmt.Sprintf(
"GRAPHQL_PORT=%v",
getPort(nodeConfig.CartesiHttpPort, portOffsetGraphQLServer)),
)
s.Env = append(s.Env,
fmt.Sprintf("GRAPHQL_HEALTHCHECK_PORT=%v",
getPort(nodeConfig, portOffsetGraphQLHealthcheck)))
getPort(nodeConfig.CartesiHttpPort, portOffsetGraphQLHealthcheck)))
s.Env = append(s.Env, os.Environ()...)
return s
}

func newHostRunner(nodeConfig config.NodeConfig) services.CommandService {
var s services.CommandService
s.Name = "host-runner"
s.HealthcheckPort = getPort(nodeConfig, portOffsetHostRunnerHealthcheck)
s.HealthcheckPort = getPort(nodeConfig.CartesiHttpPort, portOffsetHostRunnerHealthcheck)
s.Path = "cartesi-rollups-host-runner"
s.Env = append(s.Env, "LOG_ENABLE_TIMESTAMP=false")
s.Env = append(s.Env, "LOG_ENABLE_COLOR=false")
s.Env = append(s.Env, getRustLog(nodeConfig, "host_runner"))
s.Env = append(s.Env, fmt.Sprintf("GRPC_SERVER_MANAGER_ADDRESS=%v", localhost))
s.Env = append(s.Env,
fmt.Sprintf("GRPC_SERVER_MANAGER_PORT=%v", getPort(nodeConfig, portOffsetServerManager)))
fmt.Sprintf(
"GRPC_SERVER_MANAGER_PORT=%v",
getPort(nodeConfig.CartesiHttpPort, portOffsetServerManager),
),
)
s.Env = append(s.Env, fmt.Sprintf("HTTP_ROLLUP_SERVER_ADDRESS=%v", localhost))
s.Env = append(s.Env,
fmt.Sprintf("HTTP_ROLLUP_SERVER_PORT=%v", getPort(nodeConfig, portOffsetHostRunnerRollups)))
fmt.Sprintf("HTTP_ROLLUP_SERVER_PORT=%v",
getPort(nodeConfig.CartesiHttpPort, portOffsetHostRunnerRollups),
),
)
s.Env = append(s.Env,
fmt.Sprintf("HOST_RUNNER_HEALTHCHECK_PORT=%v",
getPort(nodeConfig, portOffsetHostRunnerHealthcheck)))
getPort(nodeConfig.CartesiHttpPort, portOffsetHostRunnerHealthcheck)))
s.Env = append(s.Env, os.Environ()...)
return s
}

func newIndexer(nodeConfig config.NodeConfig) services.CommandService {
var s services.CommandService
s.Name = "indexer"
s.HealthcheckPort = getPort(nodeConfig, portOffsetIndexer)
s.HealthcheckPort = getPort(nodeConfig.CartesiHttpPort, portOffsetIndexer)
s.Path = "cartesi-rollups-indexer"
s.Env = append(s.Env, "LOG_ENABLE_TIMESTAMP=false")
s.Env = append(s.Env, "LOG_ENABLE_COLOR=false")
Expand All @@ -246,40 +274,58 @@ func newIndexer(nodeConfig config.NodeConfig) services.CommandService {
s.Env = append(s.Env,
fmt.Sprintf("REDIS_ENDPOINT=%v", getRedisEndpoint(nodeConfig)))
s.Env = append(s.Env,
fmt.Sprintf("INDEXER_HEALTHCHECK_PORT=%v", getPort(nodeConfig, portOffsetIndexer)))
fmt.Sprintf(
"INDEXER_HEALTHCHECK_PORT=%v",
getPort(nodeConfig.CartesiHttpPort, portOffsetIndexer),
),
)
s.Env = append(s.Env, os.Environ()...)
return s
}

func newInspectServer(nodeConfig config.NodeConfig) services.CommandService {
var s services.CommandService
s.Name = "inspect-server"
s.HealthcheckPort = getPort(nodeConfig, portOffsetInspectHealthcheck)
s.HealthcheckPort = getPort(nodeConfig.CartesiHttpPort, portOffsetInspectHealthcheck)
s.Path = "cartesi-rollups-inspect-server"
s.Env = append(s.Env, "LOG_ENABLE_TIMESTAMP=false")
s.Env = append(s.Env, "LOG_ENABLE_COLOR=false")
s.Env = append(s.Env, getRustLog(nodeConfig, "inspect_server"))
s.Env = append(s.Env,
fmt.Sprintf("INSPECT_SERVER_ADDRESS=%v:%v",
localhost, getPort(nodeConfig, portOffsetInspectServer)))
fmt.Sprintf(
"INSPECT_SERVER_ADDRESS=%v:%v",
localhost,
getPort(nodeConfig.CartesiHttpPort, portOffsetInspectServer),
),
)
s.Env = append(s.Env,
fmt.Sprintf("SERVER_MANAGER_ADDRESS=%v:%v",
localhost, getPort(nodeConfig, portOffsetServerManager)))
fmt.Sprintf(
"SERVER_MANAGER_ADDRESS=%v:%v",
localhost,
getPort(nodeConfig.CartesiHttpPort, portOffsetServerManager),
),
)
s.Env = append(s.Env,
fmt.Sprintf("SESSION_ID=%v", serverManagerSessionId))
s.Env = append(s.Env,
fmt.Sprintf("INSPECT_SERVER_HEALTHCHECK_PORT=%v",
getPort(nodeConfig, portOffsetInspectHealthcheck)))
fmt.Sprintf(
"INSPECT_SERVER_HEALTHCHECK_PORT=%v",
getPort(nodeConfig.CartesiHttpPort, portOffsetInspectHealthcheck),
),
)
s.Env = append(s.Env, os.Environ()...)
return s
}

func newRedis(nodeConfig config.NodeConfig) services.CommandService {
var s services.CommandService
s.Name = "redis"
s.HealthcheckPort = getPort(nodeConfig, portOffsetRedis)
s.HealthcheckPort = getPort(nodeConfig.CartesiHttpPort, portOffsetRedis)
s.Path = "redis-server"
s.Args = append(s.Args, "--port", fmt.Sprint(getPort(nodeConfig, portOffsetRedis)))
s.Args = append(s.Args,
"--port",
fmt.Sprint(getPort(nodeConfig.CartesiHttpPort, portOffsetRedis)),
)
// Disable persistence with --save and --appendonly config
s.Args = append(s.Args, "--save", "")
s.Args = append(s.Args, "--appendonly", "no")
Expand All @@ -290,11 +336,15 @@ func newRedis(nodeConfig config.NodeConfig) services.CommandService {
func newServerManager(nodeConfig config.NodeConfig) services.ServerManager {
var s services.ServerManager
s.Name = "server-manager"
s.HealthcheckPort = getPort(nodeConfig, portOffsetServerManager)
s.HealthcheckPort = getPort(nodeConfig.CartesiHttpPort, portOffsetServerManager)
s.Path = "server-manager"
s.Args = append(s.Args,
fmt.Sprintf("--manager-address=%v:%v",
localhost, getPort(nodeConfig, portOffsetServerManager)))
fmt.Sprintf(
"--manager-address=%v:%v",
localhost,
getPort(nodeConfig.CartesiHttpPort, portOffsetServerManager),
),
)
s.Env = append(s.Env, "REMOTE_CARTESI_MACHINE_LOG_LEVEL=info")
if nodeConfig.CartesiLogLevel == config.LogLevelDebug {
s.Env = append(s.Env, "SERVER_MANAGER_LOG_LEVEL=info")
Expand All @@ -308,15 +358,17 @@ func newServerManager(nodeConfig config.NodeConfig) services.ServerManager {
func newStateServer(nodeConfig config.NodeConfig) services.CommandService {
var s services.CommandService
s.Name = "state-server"
s.HealthcheckPort = getPort(nodeConfig, portOffsetStateServer)
s.HealthcheckPort = getPort(nodeConfig.CartesiHttpPort, portOffsetStateServer)
s.Path = "cartesi-rollups-state-server"
s.Env = append(s.Env, "LOG_ENABLE_TIMESTAMP=false")
s.Env = append(s.Env, "LOG_ENABLE_COLOR=false")
s.Env = append(s.Env, getRustLog(nodeConfig, "state_server"))
s.Env = append(s.Env, "SF_CONCURRENT_EVENTS_FETCH=1")
s.Env = append(s.Env,
fmt.Sprintf("SF_GENESIS_BLOCK=%v",
nodeConfig.CartesiContractsInputBoxDeploymentBlockNumber))
fmt.Sprintf(
"SF_GENESIS_BLOCK=%v",
nodeConfig.CartesiContractsInputBoxDeploymentBlockNumber),
)
s.Env = append(s.Env,
fmt.Sprintf("SF_SAFETY_MARGIN=%v", nodeConfig.CartesiBlockchainFinalityOffset))
s.Env = append(s.Env,
Expand All @@ -326,8 +378,11 @@ func newStateServer(nodeConfig config.NodeConfig) services.CommandService {
s.Env = append(s.Env,
fmt.Sprintf("BLOCKCHAIN_BLOCK_TIMEOUT=%v", nodeConfig.CartesiBlockchainBlockTimeout))
s.Env = append(s.Env,
fmt.Sprintf("SS_SERVER_ADDRESS=%v:%v",
localhost, getPort(nodeConfig, portOffsetStateServer)))
fmt.Sprintf(
"SS_SERVER_ADDRESS=%v:%v",
localhost, getPort(nodeConfig.CartesiHttpPort, portOffsetStateServer),
),
)
s.Env = append(s.Env, os.Environ()...)
return s
}
Expand All @@ -340,8 +395,11 @@ func newSupervisorService(s []services.Service) services.SupervisorService {
}

func newHttpService(nodeConfig config.NodeConfig) services.HttpService {
addr := fmt.Sprintf("%v:%v", nodeConfig.CartesiHttpAddress,
getPort(nodeConfig, portOffsetProxy))
addr := fmt.Sprintf(
"%v:%v",
nodeConfig.CartesiHttpAddress,
getPort(nodeConfig.CartesiHttpPort, portOffsetProxy),
)
handler := newHttpServiceHandler(nodeConfig)
return services.HttpService{
Name: "http",
Expand Down
1 change: 1 addition & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ When enabled, the node does not start the authority-claimer service and the Redi
External Redis endpoint for the node when running in the experimental sunodo validator mode.

* **Type:** `string`
* **Default:** `""`

## `CARTESI_FEATURE_DISABLE_CLAIMER`

Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var (
// Custom GETs
// ------------------------------------------------------------------------------------------------

func GetAuth() Auth {
func getAuth() Auth {
// getting the (optional) account index
index, _ := getCartesiAuthMnemonicAccountIndex()

Expand Down
1 change: 1 addition & 0 deletions internal/config/generate/Config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ description = """
When enabled, the node does not start the authority-claimer service and the Redis server."""

[experimental.CARTESI_EXPERIMENTAL_SUNODO_VALIDATOR_REDIS_ENDPOINT]
default = ""
go-type = "string"
description = """
External Redis endpoint for the node when running in the experimental sunodo validator mode."""
Expand Down
Loading

0 comments on commit 0b23315

Please sign in to comment.