From 77d3e372b835357e73b78a1c549827189d51c3fa Mon Sep 17 00:00:00 2001 From: Francisco Moura Date: Thu, 15 Feb 2024 02:22:57 -0300 Subject: [PATCH] feat: aggregate node config in a single struct --- cmd/cartesi-rollups-node/handlers.go | 28 ++- cmd/cartesi-rollups-node/main.go | 30 +-- cmd/cartesi-rollups-node/services.go | 259 +++++++++++++++--------- docs/config.md | 1 + internal/config/config.go | 2 +- internal/config/generate/Config.toml | 1 + internal/config/generate/helpers.go | 2 + internal/config/generate/main.go | 113 +++++++++-- internal/config/get.go | 160 --------------- internal/config/log.go | 4 +- internal/config/nodeconfig.go | 288 +++++++++++++++++++++++++++ 11 files changed, 593 insertions(+), 295 deletions(-) delete mode 100644 internal/config/get.go create mode 100644 internal/config/nodeconfig.go diff --git a/cmd/cartesi-rollups-node/handlers.go b/cmd/cartesi-rollups-node/handlers.go index 329e76935..5239b77d4 100644 --- a/cmd/cartesi-rollups-node/handlers.go +++ b/cmd/cartesi-rollups-node/handlers.go @@ -12,31 +12,43 @@ import ( "github.com/cartesi/rollups-node/internal/config" ) -func newHttpServiceHandler() http.Handler { +func newHttpServiceHandler(nodeConfig config.NodeConfig) http.Handler { handler := http.NewServeMux() handler.Handle("/healthz", http.HandlerFunc(healthcheckHandler)) - graphqlProxy, err := newReverseProxy(getPort(portOffsetGraphQLServer)) + graphqlProxy, err := newReverseProxy( + nodeConfig.CartesiHttpAddress, + getPort(nodeConfig.CartesiHttpPort, portOffsetGraphQLServer), + ) if err != nil { config.ErrorLogger.Fatal(err) } handler.Handle("/graphql", graphqlProxy) - dispatcherProxy, err := newReverseProxy(getPort(portOffsetDispatcher)) + dispatcherProxy, err := newReverseProxy( + nodeConfig.CartesiHttpAddress, + getPort(nodeConfig.CartesiHttpPort, portOffsetDispatcher), + ) if err != nil { config.ErrorLogger.Fatal(err) } handler.Handle("/metrics", dispatcherProxy) - inspectProxy, err := newReverseProxy(getPort(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 config.GetCartesiFeatureHostMode() { - hostProxy, err := newReverseProxy(getPort(portOffsetHostRunnerRollups)) + if nodeConfig.CartesiFeatureHostMode { + hostProxy, err := newReverseProxy( + nodeConfig.CartesiHttpAddress, + getPort(nodeConfig.CartesiHttpPort, portOffsetHostRunnerRollups), + ) if err != nil { config.ErrorLogger.Fatal(err) } @@ -50,10 +62,10 @@ func healthcheckHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } -func newReverseProxy(port int) (*httputil.ReverseProxy, error) { +func newReverseProxy(httpAddress string, port int) (*httputil.ReverseProxy, error) { urlStr := fmt.Sprintf( "http://%v:%v/", - config.GetCartesiHttpAddress(), + httpAddress, port, ) diff --git a/cmd/cartesi-rollups-node/main.go b/cmd/cartesi-rollups-node/main.go index b9b7cab68..40ce218c2 100644 --- a/cmd/cartesi-rollups-node/main.go +++ b/cmd/cartesi-rollups-node/main.go @@ -20,35 +20,37 @@ func main() { ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer stop() - sunodoValidatorEnabled := config.GetCartesiExperimentalSunodoValidatorEnabled() + nodeConfig := config.NewNodeConfigFromEnv() + + sunodoValidatorEnabled := nodeConfig.CartesiExperimentalSunodoValidatorEnabled if !sunodoValidatorEnabled { // add Redis first - s = append(s, newRedis()) + s = append(s, newRedis(nodeConfig)) } // add services without dependencies - s = append(s, newGraphQLServer()) - s = append(s, newIndexer()) - s = append(s, newStateServer()) + s = append(s, newGraphQLServer(nodeConfig)) + s = append(s, newIndexer(nodeConfig)) + s = append(s, newStateServer(nodeConfig)) // start either the server manager or host runner - if config.GetCartesiFeatureHostMode() { - s = append(s, newHostRunner()) + if nodeConfig.CartesiFeatureHostMode { + s = append(s, newHostRunner(nodeConfig)) } else { - s = append(s, newServerManager()) + s = append(s, newServerManager(nodeConfig)) } // enable claimer if reader mode and sunodo validator mode are disabled - if !config.GetCartesiFeatureDisableClaimer() && !sunodoValidatorEnabled { - s = append(s, newAuthorityClaimer()) + if !nodeConfig.CartesiFeatureDisableClaimer && !sunodoValidatorEnabled { + s = append(s, newAuthorityClaimer(nodeConfig)) } // add services with dependencies - s = append(s, newAdvanceRunner()) // Depends on the server-manager/host-runner - s = append(s, newDispatcher()) // Depends on the state server - s = append(s, newInspectServer()) // Depends on the server-manager/host-runner + s = append(s, newAdvanceRunner(nodeConfig)) // Depends on the server-manager/host-runner + s = append(s, newDispatcher(nodeConfig)) // Depends on the state server + s = append(s, newInspectServer(nodeConfig)) // Depends on the server-manager/host-runner - s = append(s, newHttpService()) + s = append(s, newHttpService(nodeConfig)) ready := make(chan struct{}, 1) // logs startup time diff --git a/cmd/cartesi-rollups-node/services.go b/cmd/cartesi-rollups-node/services.go index ad5395d2f..ed169338d 100644 --- a/cmd/cartesi-rollups-node/services.go +++ b/cmd/cartesi-rollups-node/services.go @@ -37,23 +37,27 @@ const ( ) // Get the port of the given service. -func getPort(offset portOffset) int { - return config.GetCartesiHttpPort() + 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() string { - if config.GetCartesiExperimentalSunodoValidatorEnabled() { - return config.GetCartesiExperimentalSunodoValidatorRedisEndpoint() +func getRedisEndpoint(nodeConfig config.NodeConfig) string { + if nodeConfig.CartesiExperimentalSunodoValidatorEnabled { + return nodeConfig.CartesiExperimentalSunodoValidatorRedisEndpoint } else { - return fmt.Sprintf("redis://%v:%v", localhost, getPort(portOffsetRedis)) + return fmt.Sprintf( + "redis://%v:%v", + localhost, + getPort(nodeConfig.CartesiHttpPort, portOffsetRedis), + ) } } // Create the RUST_LOG variable using the config log level. // If the log level is set to debug, set tracing log for the given rust module. -func getRustLog(rustModule string) string { - switch config.GetCartesiLogLevel() { +func getRustLog(nodeConfig config.NodeConfig, rustModule string) string { + switch nodeConfig.CartesiLogLevel { case config.LogLevelDebug: return fmt.Sprintf("RUST_LOG=info,%v=trace", rustModule) case config.LogLevelInfo: @@ -67,72 +71,81 @@ func getRustLog(rustModule string) string { } } -func newAdvanceRunner() services.CommandService { +func newAdvanceRunner(nodeConfig config.NodeConfig) services.CommandService { var s services.CommandService s.Name = "advance-runner" - s.HealthcheckPort = getPort(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("advance_runner")) + s.Env = append(s.Env, getRustLog(nodeConfig, "advance_runner")) s.Env = append(s.Env, fmt.Sprintf("SERVER_MANAGER_ENDPOINT=http://%v:%v", localhost, - getPort(portOffsetServerManager))) + getPort(nodeConfig.CartesiHttpPort, portOffsetServerManager), + ), + ) s.Env = append(s.Env, fmt.Sprintf("SESSION_ID=%v", serverManagerSessionId)) s.Env = append(s.Env, - fmt.Sprintf("REDIS_ENDPOINT=%v", getRedisEndpoint())) + fmt.Sprintf("REDIS_ENDPOINT=%v", getRedisEndpoint(nodeConfig))) s.Env = append(s.Env, - fmt.Sprintf("CHAIN_ID=%v", config.GetCartesiBlockchainId())) + fmt.Sprintf("CHAIN_ID=%v", nodeConfig.CartesiBlockchainId)) s.Env = append(s.Env, - fmt.Sprintf("DAPP_CONTRACT_ADDRESS=%v", config.GetCartesiContractsApplicationAddress())) + fmt.Sprintf("DAPP_CONTRACT_ADDRESS=%v", nodeConfig.CartesiContractsApplicationAddress)) s.Env = append(s.Env, - fmt.Sprintf("PROVIDER_HTTP_ENDPOINT=%v", config.GetCartesiBlockchainHttpEndpoint())) + fmt.Sprintf("PROVIDER_HTTP_ENDPOINT=%v", nodeConfig.CartesiBlockchainHttpEndpoint)) s.Env = append(s.Env, - fmt.Sprintf("ADVANCE_RUNNER_HEALTHCHECK_PORT=%v", getPort(portOffsetAdvanceRunner))) + fmt.Sprintf("ADVANCE_RUNNER_HEALTHCHECK_PORT=%v", + getPort(nodeConfig.CartesiHttpPort, portOffsetAdvanceRunner), + ), + ) s.Env = append(s.Env, - fmt.Sprintf("READER_MODE=%v", config.GetCartesiFeatureDisableClaimer())) - if config.GetCartesiFeatureHostMode() || config.GetCartesiFeatureDisableMachineHashCheck() { + fmt.Sprintf("READER_MODE=%v", nodeConfig.CartesiFeatureDisableClaimer)) + if nodeConfig.CartesiFeatureHostMode || nodeConfig.CartesiFeatureDisableMachineHashCheck { s.Env = append(s.Env, "SNAPSHOT_VALIDATION_ENABLED=false") } - if !config.GetCartesiFeatureHostMode() { + if !nodeConfig.CartesiFeatureHostMode { s.Env = append(s.Env, - fmt.Sprintf("MACHINE_SNAPSHOT_PATH=%v", config.GetCartesiSnapshotDir())) + fmt.Sprintf("MACHINE_SNAPSHOT_PATH=%v", nodeConfig.CartesiSnapshotDir)) } s.Env = append(s.Env, os.Environ()...) return s } -func newAuthorityClaimer() services.CommandService { +func newAuthorityClaimer(nodeConfig config.NodeConfig) services.CommandService { var s services.CommandService s.Name = "authority-claimer" - s.HealthcheckPort = getPort(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") - s.Env = append(s.Env, getRustLog("authority_claimer")) + s.Env = append(s.Env, getRustLog(nodeConfig, "authority_claimer")) s.Env = append(s.Env, - fmt.Sprintf("TX_PROVIDER_HTTP_ENDPOINT=%v", config.GetCartesiBlockchainHttpEndpoint())) + fmt.Sprintf("TX_PROVIDER_HTTP_ENDPOINT=%v", nodeConfig.CartesiBlockchainHttpEndpoint)) s.Env = append(s.Env, - fmt.Sprintf("TX_CHAIN_ID=%v", config.GetCartesiBlockchainId())) + fmt.Sprintf("TX_CHAIN_ID=%v", nodeConfig.CartesiBlockchainId)) s.Env = append(s.Env, - fmt.Sprintf("TX_CHAIN_IS_LEGACY=%v", config.GetCartesiBlockchainIsLegacy())) + fmt.Sprintf("TX_CHAIN_IS_LEGACY=%v", nodeConfig.CartesiBlockchainIsLegacy)) s.Env = append(s.Env, - fmt.Sprintf("TX_DEFAULT_CONFIRMATIONS=%v", config.GetCartesiBlockchainFinalityOffset())) + fmt.Sprintf("TX_DEFAULT_CONFIRMATIONS=%v", nodeConfig.CartesiBlockchainFinalityOffset)) s.Env = append(s.Env, - fmt.Sprintf("REDIS_ENDPOINT=%v", getRedisEndpoint())) + fmt.Sprintf("REDIS_ENDPOINT=%v", getRedisEndpoint(nodeConfig))) s.Env = append(s.Env, - fmt.Sprintf("HISTORY_ADDRESS=%v", config.GetCartesiContractsHistoryAddress())) + fmt.Sprintf("HISTORY_ADDRESS=%v", nodeConfig.CartesiContractsHistoryAddress)) s.Env = append(s.Env, - fmt.Sprintf("AUTHORITY_ADDRESS=%v", config.GetCartesiContractsAuthorityAddress())) + fmt.Sprintf("AUTHORITY_ADDRESS=%v", nodeConfig.CartesiContractsAuthorityAddress)) s.Env = append(s.Env, - fmt.Sprintf("INPUT_BOX_ADDRESS=%v", config.GetCartesiContractsInputBoxAddress())) + fmt.Sprintf("INPUT_BOX_ADDRESS=%v", nodeConfig.CartesiContractsInputBoxAddress)) s.Env = append(s.Env, - fmt.Sprintf("GENESIS_BLOCK=%v", config.GetCartesiContractsInputBoxDeploymentBlockNumber())) + fmt.Sprintf("GENESIS_BLOCK=%v", nodeConfig.CartesiContractsInputBoxDeploymentBlockNumber)) s.Env = append(s.Env, - fmt.Sprintf("AUTHORITY_CLAIMER_HTTP_SERVER_PORT=%v", getPort(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)) @@ -150,128 +163,169 @@ func newAuthorityClaimer() services.CommandService { return s } -func newDispatcher() services.CommandService { +func newDispatcher(nodeConfig config.NodeConfig) services.CommandService { var s services.CommandService s.Name = "dispatcher" - s.HealthcheckPort = getPort(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("dispatcher")) + s.Env = append(s.Env, getRustLog(nodeConfig, "dispatcher")) s.Env = append(s.Env, - fmt.Sprintf("SC_GRPC_ENDPOINT=http://%v:%v", localhost, getPort(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", config.GetCartesiBlockchainFinalityOffset())) + fmt.Sprintf("SC_DEFAULT_CONFIRMATIONS=%v", nodeConfig.CartesiBlockchainFinalityOffset)) s.Env = append(s.Env, - fmt.Sprintf("REDIS_ENDPOINT=%v", getRedisEndpoint())) + fmt.Sprintf("REDIS_ENDPOINT=%v", getRedisEndpoint(nodeConfig))) s.Env = append(s.Env, - fmt.Sprintf("DAPP_ADDRESS=%v", config.GetCartesiContractsApplicationAddress())) + fmt.Sprintf("DAPP_ADDRESS=%v", nodeConfig.CartesiContractsApplicationAddress)) s.Env = append(s.Env, fmt.Sprintf("DAPP_DEPLOYMENT_BLOCK_NUMBER=%v", - config.GetCartesiContractsApplicationDeploymentBlockNumber())) + nodeConfig.CartesiContractsApplicationDeploymentBlockNumber)) s.Env = append(s.Env, - fmt.Sprintf("HISTORY_ADDRESS=%v", config.GetCartesiContractsHistoryAddress())) + fmt.Sprintf("HISTORY_ADDRESS=%v", nodeConfig.CartesiContractsHistoryAddress)) s.Env = append(s.Env, - fmt.Sprintf("AUTHORITY_ADDRESS=%v", config.GetCartesiContractsAuthorityAddress())) + fmt.Sprintf("AUTHORITY_ADDRESS=%v", nodeConfig.CartesiContractsAuthorityAddress)) s.Env = append(s.Env, - fmt.Sprintf("INPUT_BOX_ADDRESS=%v", config.GetCartesiContractsInputBoxAddress())) + fmt.Sprintf("INPUT_BOX_ADDRESS=%v", nodeConfig.CartesiContractsInputBoxAddress)) s.Env = append(s.Env, - fmt.Sprintf("RD_EPOCH_DURATION=%v", int(config.GetCartesiEpochDuration().Seconds()))) + fmt.Sprintf("RD_EPOCH_DURATION=%v", int(nodeConfig.CartesiEpochDuration.Seconds()))) s.Env = append(s.Env, - fmt.Sprintf("CHAIN_ID=%v", config.GetCartesiBlockchainId())) + fmt.Sprintf("CHAIN_ID=%v", nodeConfig.CartesiBlockchainId)) s.Env = append(s.Env, - fmt.Sprintf("DISPATCHER_HTTP_SERVER_PORT=%v", getPort(portOffsetDispatcher))) + fmt.Sprintf( + "DISPATCHER_HTTP_SERVER_PORT=%v", + getPort(nodeConfig.CartesiHttpPort, portOffsetDispatcher), + ), + ) s.Env = append(s.Env, os.Environ()...) return s } -func newGraphQLServer() services.CommandService { +func newGraphQLServer(nodeConfig config.NodeConfig) services.CommandService { var s services.CommandService s.Name = "graphql-server" - s.HealthcheckPort = getPort(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") - s.Env = append(s.Env, getRustLog("graphql_server")) + s.Env = append(s.Env, getRustLog(nodeConfig, "graphql_server")) s.Env = append(s.Env, - fmt.Sprintf("POSTGRES_ENDPOINT=%v", config.GetCartesiPostgresEndpoint())) + 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(portOffsetGraphQLServer))) + fmt.Sprintf( + "GRAPHQL_PORT=%v", + getPort(nodeConfig.CartesiHttpPort, portOffsetGraphQLServer)), + ) s.Env = append(s.Env, - fmt.Sprintf("GRAPHQL_HEALTHCHECK_PORT=%v", getPort(portOffsetGraphQLHealthcheck))) + fmt.Sprintf("GRAPHQL_HEALTHCHECK_PORT=%v", + getPort(nodeConfig.CartesiHttpPort, portOffsetGraphQLHealthcheck))) s.Env = append(s.Env, os.Environ()...) return s } -func newHostRunner() services.CommandService { +func newHostRunner(nodeConfig config.NodeConfig) services.CommandService { var s services.CommandService s.Name = "host-runner" - s.HealthcheckPort = getPort(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("host_runner")) + 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(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(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(portOffsetHostRunnerHealthcheck))) + fmt.Sprintf("HOST_RUNNER_HEALTHCHECK_PORT=%v", + getPort(nodeConfig.CartesiHttpPort, portOffsetHostRunnerHealthcheck))) s.Env = append(s.Env, os.Environ()...) return s } -func newIndexer() services.CommandService { +func newIndexer(nodeConfig config.NodeConfig) services.CommandService { var s services.CommandService s.Name = "indexer" - s.HealthcheckPort = getPort(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") - s.Env = append(s.Env, getRustLog("indexer")) + s.Env = append(s.Env, getRustLog(nodeConfig, "indexer")) s.Env = append(s.Env, - fmt.Sprintf("POSTGRES_ENDPOINT=%v", config.GetCartesiPostgresEndpoint())) + fmt.Sprintf("POSTGRES_ENDPOINT=%v", nodeConfig.CartesiPostgresEndpoint)) s.Env = append(s.Env, - fmt.Sprintf("CHAIN_ID=%v", config.GetCartesiBlockchainId())) + fmt.Sprintf("CHAIN_ID=%v", nodeConfig.CartesiBlockchainId)) s.Env = append(s.Env, - fmt.Sprintf("DAPP_CONTRACT_ADDRESS=%v", config.GetCartesiContractsApplicationAddress())) + fmt.Sprintf("DAPP_CONTRACT_ADDRESS=%v", nodeConfig.CartesiContractsApplicationAddress)) s.Env = append(s.Env, - fmt.Sprintf("REDIS_ENDPOINT=%v", getRedisEndpoint())) + fmt.Sprintf("REDIS_ENDPOINT=%v", getRedisEndpoint(nodeConfig))) s.Env = append(s.Env, - fmt.Sprintf("INDEXER_HEALTHCHECK_PORT=%v", getPort(portOffsetIndexer))) + fmt.Sprintf( + "INDEXER_HEALTHCHECK_PORT=%v", + getPort(nodeConfig.CartesiHttpPort, portOffsetIndexer), + ), + ) s.Env = append(s.Env, os.Environ()...) return s } -func newInspectServer() services.CommandService { +func newInspectServer(nodeConfig config.NodeConfig) services.CommandService { var s services.CommandService s.Name = "inspect-server" - s.HealthcheckPort = getPort(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("inspect_server")) + s.Env = append(s.Env, getRustLog(nodeConfig, "inspect_server")) s.Env = append(s.Env, - fmt.Sprintf("INSPECT_SERVER_ADDRESS=%v:%v", localhost, getPort(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(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(portOffsetInspectHealthcheck))) + fmt.Sprintf( + "INSPECT_SERVER_HEALTHCHECK_PORT=%v", + getPort(nodeConfig.CartesiHttpPort, portOffsetInspectHealthcheck), + ), + ) s.Env = append(s.Env, os.Environ()...) return s } -func newRedis() services.CommandService { +func newRedis(nodeConfig config.NodeConfig) services.CommandService { var s services.CommandService s.Name = "redis" - s.HealthcheckPort = getPort(portOffsetRedis) + s.HealthcheckPort = getPort(nodeConfig.CartesiHttpPort, portOffsetRedis) s.Path = "redis-server" - s.Args = append(s.Args, "--port", fmt.Sprint(getPort(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") @@ -279,15 +333,20 @@ func newRedis() services.CommandService { return s } -func newServerManager() services.ServerManager { +func newServerManager(nodeConfig config.NodeConfig) services.ServerManager { var s services.ServerManager s.Name = "server-manager" - s.HealthcheckPort = getPort(portOffsetServerManager) + s.HealthcheckPort = getPort(nodeConfig.CartesiHttpPort, portOffsetServerManager) s.Path = "server-manager" s.Args = append(s.Args, - fmt.Sprintf("--manager-address=%v:%v", localhost, getPort(portOffsetServerManager))) + fmt.Sprintf( + "--manager-address=%v:%v", + localhost, + getPort(nodeConfig.CartesiHttpPort, portOffsetServerManager), + ), + ) s.Env = append(s.Env, "REMOTE_CARTESI_MACHINE_LOG_LEVEL=info") - if config.GetCartesiLogLevel() == config.LogLevelDebug { + if nodeConfig.CartesiLogLevel == config.LogLevelDebug { s.Env = append(s.Env, "SERVER_MANAGER_LOG_LEVEL=info") } else { s.Env = append(s.Env, "SERVER_MANAGER_LOG_LEVEL=warning") @@ -296,28 +355,34 @@ func newServerManager() services.ServerManager { return s } -func newStateServer() services.CommandService { +func newStateServer(nodeConfig config.NodeConfig) services.CommandService { var s services.CommandService s.Name = "state-server" - s.HealthcheckPort = getPort(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("state_server")) + 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", - config.GetCartesiContractsInputBoxDeploymentBlockNumber())) + fmt.Sprintf( + "SF_GENESIS_BLOCK=%v", + nodeConfig.CartesiContractsInputBoxDeploymentBlockNumber), + ) s.Env = append(s.Env, - fmt.Sprintf("SF_SAFETY_MARGIN=%v", config.GetCartesiBlockchainFinalityOffset())) + fmt.Sprintf("SF_SAFETY_MARGIN=%v", nodeConfig.CartesiBlockchainFinalityOffset)) s.Env = append(s.Env, - fmt.Sprintf("BH_WS_ENDPOINT=%v", config.GetCartesiBlockchainWsEndpoint())) + fmt.Sprintf("BH_WS_ENDPOINT=%v", nodeConfig.CartesiBlockchainWsEndpoint)) s.Env = append(s.Env, - fmt.Sprintf("BH_HTTP_ENDPOINT=%v", config.GetCartesiBlockchainHttpEndpoint())) + fmt.Sprintf("BH_HTTP_ENDPOINT=%v", nodeConfig.CartesiBlockchainHttpEndpoint)) s.Env = append(s.Env, - fmt.Sprintf("BLOCKCHAIN_BLOCK_TIMEOUT=%v", config.GetCartesiBlockchainBlockTimeout())) + fmt.Sprintf("BLOCKCHAIN_BLOCK_TIMEOUT=%v", nodeConfig.CartesiBlockchainBlockTimeout)) s.Env = append(s.Env, - fmt.Sprintf("SS_SERVER_ADDRESS=%v:%v", localhost, getPort(portOffsetStateServer))) + fmt.Sprintf( + "SS_SERVER_ADDRESS=%v:%v", + localhost, getPort(nodeConfig.CartesiHttpPort, portOffsetStateServer), + ), + ) s.Env = append(s.Env, os.Environ()...) return s } @@ -329,9 +394,13 @@ func newSupervisorService(s []services.Service) services.SupervisorService { } } -func newHttpService() services.HttpService { - addr := fmt.Sprintf("%v:%v", config.GetCartesiHttpAddress(), getPort(portOffsetProxy)) - handler := newHttpServiceHandler() +func newHttpService(nodeConfig config.NodeConfig) services.HttpService { + addr := fmt.Sprintf( + "%v:%v", + nodeConfig.CartesiHttpAddress, + getPort(nodeConfig.CartesiHttpPort, portOffsetProxy), + ) + handler := newHttpServiceHandler(nodeConfig) return services.HttpService{ Name: "http", Address: addr, diff --git a/docs/config.md b/docs/config.md index 80d986e81..30db14afa 100644 --- a/docs/config.md +++ b/docs/config.md @@ -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` diff --git a/internal/config/config.go b/internal/config/config.go index 717cedd51..603e93f95 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -52,7 +52,7 @@ var ( // Custom GETs // ------------------------------------------------------------------------------------------------ -func GetAuth() Auth { +func getAuth() Auth { // getting the (optional) account index index, _ := getCartesiAuthMnemonicAccountIndex() diff --git a/internal/config/generate/Config.toml b/internal/config/generate/Config.toml index 5901676b5..ea3d3449f 100644 --- a/internal/config/generate/Config.toml +++ b/internal/config/generate/Config.toml @@ -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.""" diff --git a/internal/config/generate/helpers.go b/internal/config/generate/helpers.go index 0e476090b..f377c7941 100644 --- a/internal/config/generate/helpers.go +++ b/internal/config/generate/helpers.go @@ -76,6 +76,7 @@ func addCodeHeader(builder *strings.Builder) { addLine(builder, "") addLine(builder, `package config`) + addLine(builder, `import (`) addLine(builder, `"time"`) addLine(builder, `)`) @@ -86,6 +87,7 @@ func addCodeHeader(builder *strings.Builder) { addLine(builder, `Duration = time.Duration`) addLine(builder, `)`) addLine(builder, "") + } func addDocHeader(builder *strings.Builder) { diff --git a/internal/config/generate/main.go b/internal/config/generate/main.go index ff5346fce..db295d3b5 100644 --- a/internal/config/generate/main.go +++ b/internal/config/generate/main.go @@ -27,20 +27,86 @@ func main() { config := decodeTOML(data) envs := sortConfig(config) + for _, env := range envs { + env.validate() + } + writeDoc(envs) + writeCode(envs) +} + +func writeCode(envs []Env) { var code strings.Builder - var doc strings.Builder addCodeHeader(&code) - addDocHeader(&doc) - addLine(&doc, "") + //Validate envs + for i := range envs { + envs[i].validate() + } + + //Add get functions for _, env := range envs { - env.validate() addLine(&code, env.toFunction()) - addLine(&doc, env.toDoc()) } - writeToFile("get.go", formatCode(code.String())) + //Add NodeConfig Struct + addLine(&code, "type NodeConfig struct{") + for _, env := range envs { + if *env.Export { + addLine(&code, env.toStructMember()) + } + } + addLine(&code, "CartesiAuth Auth") + addLine(&code, "}") + + //Add init function from System Environment + addLine(&code, "") + addLine(&code, "func NewNodeConfigFromEnv() (NodeConfig){") + addLine(&code, "nodeConfig := NodeConfig{") + for _, env := range envs { + if *env.Export { + name := toStructMemberName(env.Name) + addLine(&code, name+": get"+name+"(),") + } + } + addLine(&code, "}") + addLine(&code, "nodeConfig.CartesiAuth = getAuth()") + addLine(&code, "return nodeConfig") + addLine(&code, "}") + + //Add init function from Default Values + addLine(&code, "") + addLine(&code, "func NewNodeConfig() (NodeConfig){") + addLine(&code, "nodeConfig := NodeConfig{}") + for _, env := range envs { + if *env.Export && env.Default != nil && *env.Default != "" { + name := toStructMemberName(env.Name) + varName := toVarName(name) + addLine(&code, varName+", err := "+toToFuncName(env.GoType)+`("`+*env.Default+`")`) + addLine(&code, "if err != nil {") + addLine(&code, "panic(err)") + addLine(&code, "}") + addLine(&code, "nodeConfig."+name+" = "+varName) + } + } + addLine(&code, `nodeConfig.CartesiAuth = AuthMnemonic{`) + addLine(&code, `Mnemonic: "test test test test test test test test test test test junk",`) + addLine(&code, `AccountIndex: 0,`) + addLine(&code, "}") + addLine(&code, "return nodeConfig") + addLine(&code, "}") + + writeToFile("nodeconfig.go", formatCode(code.String())) +} + +func writeDoc(envs []Env) { + var doc strings.Builder + + addDocHeader(&doc) + addLine(&doc, "") + for _, env := range envs { + addLine(&doc, env.toDoc()) + } writeToFile("../../docs/config.md", []byte(doc.String())) } @@ -92,7 +158,7 @@ func (e *Env) validate() { // Generates the get function for the environment variable. func (e Env) toFunction() string { - name := toFunctionName(e.Name) + name := toStructMemberName(e.Name) typ := e.GoType get := "get" vars := "v" @@ -103,16 +169,12 @@ func (e Env) toFunction() string { defaultValue = *e.Default } - to_ := []rune(e.GoType) - to_[0] = unicode.ToUpper(to_[0]) - to := "to" + string(to_) + to := toToFuncName(e.GoType) args := fmt.Sprintf(`"%s", "%s", %t, %t, %s`, e.Name, defaultValue, hasDefault, *e.Redact, to) - if *e.Export { - name = "Get" + name - } else { - name = "get" + name + name = "get" + name + if !*e.Export { typ = fmt.Sprintf("(%s, bool)", typ) get += "Optional" vars += ", ok" @@ -123,6 +185,15 @@ func (e Env) toFunction() string { return fmt.Sprintf("func %s() %s { %s }\n", name, typ, body) } +// Generates the Config Struct member for the envrionemnt variable. +func (e Env) toStructMember() string { + name := toStructMemberName(e.Name) + if !*e.Export { + name = toVarName(name) + } + return name + " " + e.GoType +} + // Generates the documentation entry for the environment variable. func (e Env) toDoc() string { s := fmt.Sprintf("## `%s`\n\n%s\n\n", e.Name, e.Description) @@ -134,7 +205,7 @@ func (e Env) toDoc() string { } // Splits the string by "_" and joins each substring with the first letter in upper case. -func toFunctionName(env string) string { +func toStructMemberName(env string) string { caser := cases.Title(language.English) words := strings.Split(env, "_") for i, word := range words { @@ -142,3 +213,15 @@ func toFunctionName(env string) string { } return strings.Join(words, "") } + +func toVarName(name string) string { + name_ := []rune(name) + name_[0] = unicode.ToLower(name_[0]) + return string(name_) +} + +func toToFuncName(goType string) string { + to_ := []rune(goType) + to_[0] = unicode.ToUpper(to_[0]) + return "to" + string(to_) +} diff --git a/internal/config/get.go b/internal/config/get.go deleted file mode 100644 index f3ce1a792..000000000 --- a/internal/config/get.go +++ /dev/null @@ -1,160 +0,0 @@ -// Code generated by internal/config/generate. -// DO NOT EDIT. - -// (c) Cartesi and individual authors (see AUTHORS) -// SPDX-License-Identifier: Apache-2.0 (see LICENSE) - -package config - -import ( - "time" -) - -type ( - Duration = time.Duration -) - -func getCartesiAuthAwsKmsKeyId() (string, bool) { - v, ok := getOptional("CARTESI_AUTH_AWS_KMS_KEY_ID", "", false, true, toString) - return v, ok -} - -func getCartesiAuthAwsKmsRegion() (string, bool) { - v, ok := getOptional("CARTESI_AUTH_AWS_KMS_REGION", "", false, true, toString) - return v, ok -} - -func getCartesiAuthMnemonic() (string, bool) { - v, ok := getOptional("CARTESI_AUTH_MNEMONIC", "", false, true, toString) - return v, ok -} - -func getCartesiAuthMnemonicAccountIndex() (int, bool) { - v, ok := getOptional("CARTESI_AUTH_MNEMONIC_ACCOUNT_INDEX", "0", true, true, toInt) - return v, ok -} - -func getCartesiAuthMnemonicFile() (string, bool) { - v, ok := getOptional("CARTESI_AUTH_MNEMONIC_FILE", "", false, true, toString) - return v, ok -} - -func GetCartesiBlockchainBlockTimeout() int { - v := get("CARTESI_BLOCKCHAIN_BLOCK_TIMEOUT", "60", true, false, toInt) - return v -} - -func GetCartesiBlockchainFinalityOffset() int { - v := get("CARTESI_BLOCKCHAIN_FINALITY_OFFSET", "10", true, false, toInt) - return v -} - -func GetCartesiBlockchainHttpEndpoint() string { - v := get("CARTESI_BLOCKCHAIN_HTTP_ENDPOINT", "", false, false, toString) - return v -} - -func GetCartesiBlockchainId() int { - v := get("CARTESI_BLOCKCHAIN_ID", "", false, false, toInt) - return v -} - -func GetCartesiBlockchainIsLegacy() bool { - v := get("CARTESI_BLOCKCHAIN_IS_LEGACY", "false", true, false, toBool) - return v -} - -func GetCartesiBlockchainWsEndpoint() string { - v := get("CARTESI_BLOCKCHAIN_WS_ENDPOINT", "", false, false, toString) - return v -} - -func GetCartesiContractsInputBoxDeploymentBlockNumber() int64 { - v := get("CARTESI_CONTRACTS_INPUT_BOX_DEPLOYMENT_BLOCK_NUMBER", "", false, false, toInt64) - return v -} - -func GetCartesiContractsApplicationAddress() string { - v := get("CARTESI_CONTRACTS_APPLICATION_ADDRESS", "", false, false, toString) - return v -} - -func GetCartesiContractsApplicationDeploymentBlockNumber() string { - v := get("CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER", "", false, false, toString) - return v -} - -func GetCartesiContractsAuthorityAddress() string { - v := get("CARTESI_CONTRACTS_AUTHORITY_ADDRESS", "", false, false, toString) - return v -} - -func GetCartesiContractsHistoryAddress() string { - v := get("CARTESI_CONTRACTS_HISTORY_ADDRESS", "", false, false, toString) - return v -} - -func GetCartesiContractsInputBoxAddress() string { - v := get("CARTESI_CONTRACTS_INPUT_BOX_ADDRESS", "", false, false, toString) - return v -} - -func GetCartesiExperimentalSunodoValidatorEnabled() bool { - v := get("CARTESI_EXPERIMENTAL_SUNODO_VALIDATOR_ENABLED", "false", true, false, toBool) - return v -} - -func GetCartesiExperimentalSunodoValidatorRedisEndpoint() string { - v := get("CARTESI_EXPERIMENTAL_SUNODO_VALIDATOR_REDIS_ENDPOINT", "", false, false, toString) - return v -} - -func GetCartesiFeatureDisableClaimer() bool { - v := get("CARTESI_FEATURE_DISABLE_CLAIMER", "false", true, false, toBool) - return v -} - -func GetCartesiFeatureDisableMachineHashCheck() bool { - v := get("CARTESI_FEATURE_DISABLE_MACHINE_HASH_CHECK", "false", true, false, toBool) - return v -} - -func GetCartesiFeatureHostMode() bool { - v := get("CARTESI_FEATURE_HOST_MODE", "false", true, false, toBool) - return v -} - -func GetCartesiHttpAddress() string { - v := get("CARTESI_HTTP_ADDRESS", "127.0.0.1", true, false, toString) - return v -} - -func GetCartesiHttpPort() int { - v := get("CARTESI_HTTP_PORT", "10000", true, false, toInt) - return v -} - -func GetCartesiLogLevel() LogLevel { - v := get("CARTESI_LOG_LEVEL", "info", true, false, toLogLevel) - return v -} - -func GetCartesiLogTimestamp() bool { - v := get("CARTESI_LOG_TIMESTAMP", "false", true, false, toBool) - return v -} - -func GetCartesiPostgresEndpoint() string { - v := get("CARTESI_POSTGRES_ENDPOINT", "", true, true, toString) - return v -} - -func GetCartesiEpochDuration() Duration { - v := get("CARTESI_EPOCH_DURATION", "86400", true, false, toDuration) - return v -} - -func GetCartesiSnapshotDir() string { - v := get("CARTESI_SNAPSHOT_DIR", "", false, false, toString) - return v -} diff --git a/internal/config/log.go b/internal/config/log.go index 57a3882f1..0171ebb25 100644 --- a/internal/config/log.go +++ b/internal/config/log.go @@ -23,7 +23,7 @@ func init() { func logInit() { var flags int - if GetCartesiLogTimestamp() { + if getCartesiLogTimestamp() { flags = log.LstdFlags } @@ -32,7 +32,7 @@ func logInit() { InfoLogger = log.New(os.Stdout, "INFO ", flags) DebugLogger = log.New(os.Stdout, "DEBUG ", flags) - switch GetCartesiLogLevel() { + switch getCartesiLogLevel() { case LogLevelError: WarningLogger.SetOutput(io.Discard) fallthrough diff --git a/internal/config/nodeconfig.go b/internal/config/nodeconfig.go new file mode 100644 index 000000000..65421fdf3 --- /dev/null +++ b/internal/config/nodeconfig.go @@ -0,0 +1,288 @@ +// Code generated by internal/config/generate. +// DO NOT EDIT. + +// (c) Cartesi and individual authors (see AUTHORS) +// SPDX-License-Identifier: Apache-2.0 (see LICENSE) + +package config + +import ( + "time" +) + +type ( + Duration = time.Duration +) + +func getCartesiAuthAwsKmsKeyId() (string, bool) { + v, ok := getOptional("CARTESI_AUTH_AWS_KMS_KEY_ID", "", false, true, toString) + return v, ok +} + +func getCartesiAuthAwsKmsRegion() (string, bool) { + v, ok := getOptional("CARTESI_AUTH_AWS_KMS_REGION", "", false, true, toString) + return v, ok +} + +func getCartesiAuthMnemonic() (string, bool) { + v, ok := getOptional("CARTESI_AUTH_MNEMONIC", "", false, true, toString) + return v, ok +} + +func getCartesiAuthMnemonicAccountIndex() (int, bool) { + v, ok := getOptional("CARTESI_AUTH_MNEMONIC_ACCOUNT_INDEX", "0", true, true, toInt) + return v, ok +} + +func getCartesiAuthMnemonicFile() (string, bool) { + v, ok := getOptional("CARTESI_AUTH_MNEMONIC_FILE", "", false, true, toString) + return v, ok +} + +func getCartesiBlockchainBlockTimeout() int { + v := get("CARTESI_BLOCKCHAIN_BLOCK_TIMEOUT", "60", true, false, toInt) + return v +} + +func getCartesiBlockchainFinalityOffset() int { + v := get("CARTESI_BLOCKCHAIN_FINALITY_OFFSET", "10", true, false, toInt) + return v +} + +func getCartesiBlockchainHttpEndpoint() string { + v := get("CARTESI_BLOCKCHAIN_HTTP_ENDPOINT", "", false, false, toString) + return v +} + +func getCartesiBlockchainId() int { + v := get("CARTESI_BLOCKCHAIN_ID", "", false, false, toInt) + return v +} + +func getCartesiBlockchainIsLegacy() bool { + v := get("CARTESI_BLOCKCHAIN_IS_LEGACY", "false", true, false, toBool) + return v +} + +func getCartesiBlockchainWsEndpoint() string { + v := get("CARTESI_BLOCKCHAIN_WS_ENDPOINT", "", false, false, toString) + return v +} + +func getCartesiContractsInputBoxDeploymentBlockNumber() int64 { + v := get("CARTESI_CONTRACTS_INPUT_BOX_DEPLOYMENT_BLOCK_NUMBER", "", false, false, toInt64) + return v +} + +func getCartesiContractsApplicationAddress() string { + v := get("CARTESI_CONTRACTS_APPLICATION_ADDRESS", "", false, false, toString) + return v +} + +func getCartesiContractsApplicationDeploymentBlockNumber() string { + v := get("CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER", "", false, false, toString) + return v +} + +func getCartesiContractsAuthorityAddress() string { + v := get("CARTESI_CONTRACTS_AUTHORITY_ADDRESS", "", false, false, toString) + return v +} + +func getCartesiContractsHistoryAddress() string { + v := get("CARTESI_CONTRACTS_HISTORY_ADDRESS", "", false, false, toString) + return v +} + +func getCartesiContractsInputBoxAddress() string { + v := get("CARTESI_CONTRACTS_INPUT_BOX_ADDRESS", "", false, false, toString) + return v +} + +func getCartesiExperimentalSunodoValidatorEnabled() bool { + v := get("CARTESI_EXPERIMENTAL_SUNODO_VALIDATOR_ENABLED", "false", true, false, toBool) + return v +} + +func getCartesiExperimentalSunodoValidatorRedisEndpoint() string { + v := get("CARTESI_EXPERIMENTAL_SUNODO_VALIDATOR_REDIS_ENDPOINT", "", true, false, toString) + return v +} + +func getCartesiFeatureDisableClaimer() bool { + v := get("CARTESI_FEATURE_DISABLE_CLAIMER", "false", true, false, toBool) + return v +} + +func getCartesiFeatureDisableMachineHashCheck() bool { + v := get("CARTESI_FEATURE_DISABLE_MACHINE_HASH_CHECK", "false", true, false, toBool) + return v +} + +func getCartesiFeatureHostMode() bool { + v := get("CARTESI_FEATURE_HOST_MODE", "false", true, false, toBool) + return v +} + +func getCartesiHttpAddress() string { + v := get("CARTESI_HTTP_ADDRESS", "127.0.0.1", true, false, toString) + return v +} + +func getCartesiHttpPort() int { + v := get("CARTESI_HTTP_PORT", "10000", true, false, toInt) + return v +} + +func getCartesiLogLevel() LogLevel { + v := get("CARTESI_LOG_LEVEL", "info", true, false, toLogLevel) + return v +} + +func getCartesiLogTimestamp() bool { + v := get("CARTESI_LOG_TIMESTAMP", "false", true, false, toBool) + return v +} + +func getCartesiPostgresEndpoint() string { + v := get("CARTESI_POSTGRES_ENDPOINT", "", true, true, toString) + return v +} + +func getCartesiEpochDuration() Duration { + v := get("CARTESI_EPOCH_DURATION", "86400", true, false, toDuration) + return v +} + +func getCartesiSnapshotDir() string { + v := get("CARTESI_SNAPSHOT_DIR", "", false, false, toString) + return v +} + +type NodeConfig struct { + CartesiBlockchainBlockTimeout int + CartesiBlockchainFinalityOffset int + CartesiBlockchainHttpEndpoint string + CartesiBlockchainId int + CartesiBlockchainIsLegacy bool + CartesiBlockchainWsEndpoint string + CartesiContractsInputBoxDeploymentBlockNumber int64 + CartesiContractsApplicationAddress string + CartesiContractsApplicationDeploymentBlockNumber string + CartesiContractsAuthorityAddress string + CartesiContractsHistoryAddress string + CartesiContractsInputBoxAddress string + CartesiExperimentalSunodoValidatorEnabled bool + CartesiExperimentalSunodoValidatorRedisEndpoint string + CartesiFeatureDisableClaimer bool + CartesiFeatureDisableMachineHashCheck bool + CartesiFeatureHostMode bool + CartesiHttpAddress string + CartesiHttpPort int + CartesiLogLevel LogLevel + CartesiLogTimestamp bool + CartesiPostgresEndpoint string + CartesiEpochDuration Duration + CartesiSnapshotDir string + CartesiAuth Auth +} + +func NewNodeConfigFromEnv() NodeConfig { + nodeConfig := NodeConfig{ + CartesiBlockchainBlockTimeout: getCartesiBlockchainBlockTimeout(), + CartesiBlockchainFinalityOffset: getCartesiBlockchainFinalityOffset(), + CartesiBlockchainHttpEndpoint: getCartesiBlockchainHttpEndpoint(), + CartesiBlockchainId: getCartesiBlockchainId(), + CartesiBlockchainIsLegacy: getCartesiBlockchainIsLegacy(), + CartesiBlockchainWsEndpoint: getCartesiBlockchainWsEndpoint(), + CartesiContractsInputBoxDeploymentBlockNumber: getCartesiContractsInputBoxDeploymentBlockNumber(), + CartesiContractsApplicationAddress: getCartesiContractsApplicationAddress(), + CartesiContractsApplicationDeploymentBlockNumber: getCartesiContractsApplicationDeploymentBlockNumber(), + CartesiContractsAuthorityAddress: getCartesiContractsAuthorityAddress(), + CartesiContractsHistoryAddress: getCartesiContractsHistoryAddress(), + CartesiContractsInputBoxAddress: getCartesiContractsInputBoxAddress(), + CartesiExperimentalSunodoValidatorEnabled: getCartesiExperimentalSunodoValidatorEnabled(), + CartesiExperimentalSunodoValidatorRedisEndpoint: getCartesiExperimentalSunodoValidatorRedisEndpoint(), + CartesiFeatureDisableClaimer: getCartesiFeatureDisableClaimer(), + CartesiFeatureDisableMachineHashCheck: getCartesiFeatureDisableMachineHashCheck(), + CartesiFeatureHostMode: getCartesiFeatureHostMode(), + CartesiHttpAddress: getCartesiHttpAddress(), + CartesiHttpPort: getCartesiHttpPort(), + CartesiLogLevel: getCartesiLogLevel(), + CartesiLogTimestamp: getCartesiLogTimestamp(), + CartesiPostgresEndpoint: getCartesiPostgresEndpoint(), + CartesiEpochDuration: getCartesiEpochDuration(), + CartesiSnapshotDir: getCartesiSnapshotDir(), + } + nodeConfig.CartesiAuth = getAuth() + return nodeConfig +} + +func NewNodeConfig() NodeConfig { + nodeConfig := NodeConfig{} + cartesiBlockchainBlockTimeout, err := toInt("60") + if err != nil { + panic(err) + } + nodeConfig.CartesiBlockchainBlockTimeout = cartesiBlockchainBlockTimeout + cartesiBlockchainFinalityOffset, err := toInt("10") + if err != nil { + panic(err) + } + nodeConfig.CartesiBlockchainFinalityOffset = cartesiBlockchainFinalityOffset + cartesiBlockchainIsLegacy, err := toBool("false") + if err != nil { + panic(err) + } + nodeConfig.CartesiBlockchainIsLegacy = cartesiBlockchainIsLegacy + cartesiExperimentalSunodoValidatorEnabled, err := toBool("false") + if err != nil { + panic(err) + } + nodeConfig.CartesiExperimentalSunodoValidatorEnabled = cartesiExperimentalSunodoValidatorEnabled + cartesiFeatureDisableClaimer, err := toBool("false") + if err != nil { + panic(err) + } + nodeConfig.CartesiFeatureDisableClaimer = cartesiFeatureDisableClaimer + cartesiFeatureDisableMachineHashCheck, err := toBool("false") + if err != nil { + panic(err) + } + nodeConfig.CartesiFeatureDisableMachineHashCheck = cartesiFeatureDisableMachineHashCheck + cartesiFeatureHostMode, err := toBool("false") + if err != nil { + panic(err) + } + nodeConfig.CartesiFeatureHostMode = cartesiFeatureHostMode + cartesiHttpAddress, err := toString("127.0.0.1") + if err != nil { + panic(err) + } + nodeConfig.CartesiHttpAddress = cartesiHttpAddress + cartesiHttpPort, err := toInt("10000") + if err != nil { + panic(err) + } + nodeConfig.CartesiHttpPort = cartesiHttpPort + cartesiLogLevel, err := toLogLevel("info") + if err != nil { + panic(err) + } + nodeConfig.CartesiLogLevel = cartesiLogLevel + cartesiLogTimestamp, err := toBool("false") + if err != nil { + panic(err) + } + nodeConfig.CartesiLogTimestamp = cartesiLogTimestamp + cartesiEpochDuration, err := toDuration("86400") + if err != nil { + panic(err) + } + nodeConfig.CartesiEpochDuration = cartesiEpochDuration + nodeConfig.CartesiAuth = AuthMnemonic{ + Mnemonic: "test test test test test test test test test test test junk", + AccountIndex: 0, + } + return nodeConfig +}