Skip to content

Commit

Permalink
feat: move Node Services creation from cmd to internal Library
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoura committed Mar 4, 2024
1 parent 8bb2717 commit c036dfa
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 52 deletions.
38 changes: 7 additions & 31 deletions cmd/cartesi-rollups-node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"time"

"github.com/cartesi/rollups-node/internal/config"
"github.com/cartesi/rollups-node/internal/node"
"github.com/cartesi/rollups-node/internal/services"
)

func main() {
startTime := time.Now()
var s []services.Service

ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
Expand All @@ -24,35 +24,7 @@ func main() {

nodeConfig.Validate()

sunodoValidatorEnabled := nodeConfig.CartesiExperimentalSunodoValidatorEnabled()
if !sunodoValidatorEnabled {
// add Redis first
s = append(s, newRedis(nodeConfig))
}

// add services without dependencies
s = append(s, newGraphQLServer(nodeConfig))
s = append(s, newIndexer(nodeConfig))
s = append(s, newStateServer(nodeConfig))

// start either the server manager or host runner
if nodeConfig.CartesiFeatureHostMode() {
s = append(s, newHostRunner(nodeConfig))
} else {
s = append(s, newServerManager(nodeConfig))
}

// enable claimer if reader mode and sunodo validator mode are disabled
if !nodeConfig.CartesiFeatureDisableClaimer() && !sunodoValidatorEnabled {
s = append(s, newAuthorityClaimer(nodeConfig))
}

// add services with dependencies
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(nodeConfig))
s := node.NewNodeServices(nodeConfig)

ready := make(chan struct{}, 1)
// logs startup time
Expand All @@ -66,7 +38,11 @@ func main() {
}()

// start supervisor
supervisor := newSupervisorService(s)
supervisor := services.SupervisorService{
Name: "rollups-node",
Services: s,
}

if err := supervisor.Start(ctx, ready); err != nil {
config.ErrorLogger.Print(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/config/generate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func writeCode(envs []Env) {
//Getter
addLine(&code, "func (nodeConfig* NodeConfig) "+name+"() "+env.GoType+" {")
addLine(&code, "if nodeConfig."+varName+" == nil {")
addLine(&code, `panic("Variable `+env.Name+` is not set")`)
addLine(&code, `fail("missing required %s env var","`+env.Name+`")`)
addLine(&code, "}")
addLine(&code, "return *nodeConfig."+varName)
addLine(&code, "}")
Expand Down
32 changes: 32 additions & 0 deletions internal/config/get.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions internal/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func (nodeConfig *NodeConfig) Validate() {
if nodeConfig.cartesiExperimentalSunodoValidatorEnabled != nil &&
*nodeConfig.cartesiExperimentalSunodoValidatorEnabled {
if nodeConfig.cartesiExperimentalSunodoValidatorRedisEndpoint == nil {
panic("Missing CARTESI_EXPERIMENTAL_SUNODO_VALIDATOR_REDIS_ENDPOINT config")
fail("Missing required %s env var", "CARTESI_EXPERIMENTAL_SUNODO_VALIDATOR_REDIS_ENDPOINT")
}
}

Expand All @@ -19,43 +19,43 @@ func (nodeConfig *NodeConfig) Validate() {
}

if nodeConfig.cartesiBlockchainHttpEndpoint == nil {
panic("Missing CARTESI_BLOCKCHAIN_HTTP_ENDPOINT")
fail("Missing required %s env var", "CARTESI_BLOCKCHAIN_HTTP_ENDPOINT")
}

if nodeConfig.cartesiBlockchainId == nil {
panic("Missing CARTESI_BLOCKCHAIN_ID config")
fail("Missing required %s env var", "CARTESI_BLOCKCHAIN_ID")
}

if nodeConfig.cartesiBlockchainWsEndpoint == nil {
panic("Missing CARTESI_BLOCKCHAIN_WS_ENDPOINT config")
fail("Missing required %s env var", "CARTESI_BLOCKCHAIN_WS_ENDPOINT")
}

if nodeConfig.cartesiContractsInputBoxDeploymentBlockNumber == nil {
panic("Missing CARTESI_CONTRACTS_INPUT_BOX_DEPLOYMENT_BLOCK_NUMBER config")
fail("Missing required %s env var", "CARTESI_CONTRACTS_INPUT_BOX_DEPLOYMENT_BLOCK_NUMBER")
}

if nodeConfig.cartesiContractsApplicationAddress == nil {
panic("Missing CARTESI_CONTRACTS_APPLICATION_ADDRESS config")
fail("Missing required %s env var", "CARTESI_CONTRACTS_APPLICATION_ADDRESS")
}

if nodeConfig.cartesiContractsApplicationDeploymentBlockNumber == nil {
panic("Missing CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER config")
fail("Missing required %s env var", "CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER")
}

if nodeConfig.cartesiContractsAuthorityAddress == nil {
panic("Missing CARTESI_CONTRACTS_AUTHORITY_ADDRESS config")
fail("Missing required %s env var", "CARTESI_CONTRACTS_AUTHORITY_ADDRESS")
}

if nodeConfig.cartesiContractsHistoryAddress == nil {
panic("Missing CARTESI_CONTRACTS_HISTORY_ADDRESS config")
fail("Missing required %s env var", "CARTESI_CONTRACTS_HISTORY_ADDRESS")
}

if nodeConfig.cartesiContractsInputBoxAddress == nil {
panic("Missing CARTESI_CONTRACTS_INPUT_BOX_ADDRESS config")
fail("Missing required %s env var", "CARTESI_CONTRACTS_INPUT_BOX_ADDRESS")
}

if nodeConfig.cartesiSnapshotDir == nil {
panic("Missing CARTESI_SNAPSHOT_DIR config")
fail("Missing required %s env var", "CARTESI_SNAPSHOT_DIR")
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package main
package node

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package main
package node

import (
"fmt"
Expand Down Expand Up @@ -387,13 +387,6 @@ func newStateServer(nodeConfig config.NodeConfig) services.CommandService {
return s
}

func newSupervisorService(s []services.Service) services.SupervisorService {
return services.SupervisorService{
Name: "rollups-node",
Services: s,
}
}

func newHttpService(nodeConfig config.NodeConfig) services.HttpService {
addr := fmt.Sprintf(
"%v:%v",
Expand All @@ -407,3 +400,38 @@ func newHttpService(nodeConfig config.NodeConfig) services.HttpService {
Handler: handler,
}
}

func NewNodeServices(nodeConfig config.NodeConfig) []services.Service {
var s []services.Service
sunodoValidatorEnabled := nodeConfig.CartesiExperimentalSunodoValidatorEnabled()
if !sunodoValidatorEnabled {
// add Redis first
s = append(s, newRedis(nodeConfig))
}

// add services without dependencies
s = append(s, newGraphQLServer(nodeConfig))
s = append(s, newIndexer(nodeConfig))
s = append(s, newStateServer(nodeConfig))

// start either the server manager or host runner
if nodeConfig.CartesiFeatureHostMode() {
s = append(s, newHostRunner(nodeConfig))
} else {
s = append(s, newServerManager(nodeConfig))
}

// enable claimer if reader mode and sunodo validator mode are disabled
if !nodeConfig.CartesiFeatureDisableClaimer() && !sunodoValidatorEnabled {
s = append(s, newAuthorityClaimer(nodeConfig))
}

// add services with dependencies
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(nodeConfig))

return s
}

0 comments on commit c036dfa

Please sign in to comment.