Skip to content

Commit

Permalink
feat: Add initialization timeout
Browse files Browse the repository at this point in the history
refactor: code format
refactor: reduce namespace polution by removing service examples
refactor: use service logger
  • Loading branch information
mpolitzer committed Dec 12, 2024
1 parent 3db4a7e commit 32d7968
Show file tree
Hide file tree
Showing 69 changed files with 1,186 additions and 2,056 deletions.
9 changes: 9 additions & 0 deletions cmd/cartesi-rollups-advancer/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package root

import (
"time"

"github.com/cartesi/rollups-node/internal/advancer"
"github.com/cartesi/rollups-node/pkg/service"
"github.com/spf13/cobra"
Expand All @@ -21,6 +23,7 @@ var (
TelemetryAddress: ":10001",
Impl: &advancerService,
},
MaxStartupTime: 10 * time.Second,
}
)

Expand All @@ -36,6 +39,12 @@ func init() {
Cmd.Flags().Var(&createInfo.LogLevel,
"log-level",
"log level: debug, info, warn or error")
Cmd.Flags().BoolVar(&createInfo.LogPretty,
"log-color", createInfo.LogPretty,
"tint the logs (colored output)")
Cmd.Flags().DurationVar(&createInfo.MaxStartupTime,
"max-startup-time", createInfo.MaxStartupTime,
"maximum startup time in seconds")
}

func run(cmd *cobra.Command, args []string) {
Expand Down
11 changes: 10 additions & 1 deletion cmd/cartesi-rollups-claimer/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package root

import (
"time"

"github.com/cartesi/rollups-node/internal/claimer"
"github.com/cartesi/rollups-node/pkg/service"
"github.com/spf13/cobra"
Expand All @@ -24,6 +26,7 @@ var (
Impl: &claimerService,
},
EnableSubmission: true,
MaxStartupTime: 10 * time.Second,
}
)

Expand All @@ -48,13 +51,19 @@ func init() {
Cmd.Flags().Var(&createInfo.LogLevel,
"log-level",
"log level: debug, info, warn or error")
Cmd.Flags().BoolVar(&createInfo.LogPretty,
"log-color", createInfo.LogPretty,
"tint the logs (colored output)")
Cmd.Flags().DurationVar(&createInfo.MaxStartupTime,
"max-startup-time", createInfo.MaxStartupTime,
"maximum startup time in seconds")
Cmd.Flags().BoolVar(&createInfo.EnableSubmission,
"claim-submission", createInfo.EnableSubmission,
"enable or disable claim submission (reader mode)")
}

func run(cmd *cobra.Command, args []string) {
cobra.CheckErr(claimer.Create(createInfo, &claimerService))
cobra.CheckErr(claimer.Create(&createInfo, &claimerService))
claimerService.CreateDefaultHandlers("/" + claimerService.Name)
cobra.CheckErr(claimerService.Serve())
}
4 changes: 3 additions & 1 deletion cmd/cartesi-rollups-cli/root/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package common

import (
"log/slog"

"github.com/cartesi/rollups-node/internal/repository"
"github.com/spf13/cobra"
)
Expand All @@ -19,6 +21,6 @@ func Setup(cmd *cobra.Command, args []string) {
ctx := cmd.Context()

var err error
Database, err = repository.Connect(ctx, PostgresEndpoint)
Database, err = repository.Connect(ctx, PostgresEndpoint, slog.Default())
cobra.CheckErr(err)
}
42 changes: 32 additions & 10 deletions cmd/cartesi-rollups-evm-reader/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
package root

import (
"time"

"github.com/cartesi/rollups-node/internal/config"
"github.com/cartesi/rollups-node/internal/evmreader"
"github.com/cartesi/rollups-node/internal/model"
"github.com/cartesi/rollups-node/pkg/service"
"github.com/ethereum/go-ethereum/common"

"github.com/spf13/cobra"
)
Expand All @@ -24,8 +29,13 @@ var (
TelemetryAddress: ":10000",
Impl: &readerService,
},
DefaultBlockString: "safe",
EvmReaderPersistentConfig: model.EvmReaderPersistentConfig{
DefaultBlock: model.DefaultBlockStatusSafe,
},
MaxStartupTime: 10 * time.Second,
}
inputBoxAddress service.EthAddress
DefaultBlockString = "safe"
)

var Cmd = &cobra.Command{
Expand All @@ -38,8 +48,8 @@ var Cmd = &cobra.Command{
func init() {
createInfo.LoadEnv()

Cmd.Flags().StringVarP(&createInfo.DefaultBlockString,
"default-block", "d", createInfo.DefaultBlockString,
Cmd.Flags().StringVarP(&DefaultBlockString,
"default-block", "d", DefaultBlockString,
`Default block to be used when fetching new blocks.
One of 'latest', 'safe', 'pending', 'finalized'`)

Expand All @@ -61,11 +71,9 @@ func init() {
createInfo.BlockchainWsEndpoint.Value,
"Blockchain WS Endpoint")

// Cmd.Flags().StringVarP(&inputBoxAddress,
// "inputbox-address",
// "i",
// "",
// "Input Box contract address")
Cmd.Flags().Var(&inputBoxAddress,
"inputbox-address",
"Input Box contract address")

Cmd.Flags().Uint64VarP(&createInfo.InputBoxDeploymentBlock,
"inputbox-block-number",
Expand All @@ -75,11 +83,25 @@ func init() {
Cmd.Flags().Var(&createInfo.LogLevel,
"log-level",
"log level: debug, info, warn or error")
Cmd.Flags().BoolVar(&createInfo.LogPretty,
"log-color", createInfo.LogPretty,
"tint the logs (colored output)")
Cmd.Flags().DurationVar(&createInfo.MaxStartupTime,
"max-startup-time", createInfo.MaxStartupTime,
"maximum startup time in seconds")
}

func run(cmd *cobra.Command, args []string) {
ready := make(chan struct{}, 1)
if cmd.Flags().Changed("default-block") {
var err error
createInfo.DefaultBlock, err = config.ToDefaultBlockFromString(DefaultBlockString)
cobra.CheckErr(err)
}
if cmd.Flags().Changed("inputbox-address") {
createInfo.InputBoxAddress = common.Address(inputBoxAddress)
}

cobra.CheckErr(evmreader.Create(&createInfo, &readerService))
readerService.CreateDefaultHandlers("/" + readerService.Name)
cobra.CheckErr(readerService.Start(nil, ready))
cobra.CheckErr(readerService.Serve())
}
98 changes: 35 additions & 63 deletions cmd/cartesi-rollups-node/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,84 +4,56 @@
package root

import (
"context"
"log/slog"
"os"
"os/signal"
"syscall"
"time"

"github.com/cartesi/rollups-node/internal/config"
"github.com/cartesi/rollups-node/internal/node"
"github.com/cartesi/rollups-node/internal/repository"
"github.com/cartesi/rollups-node/pkg/service"
"github.com/spf13/cobra"
)

const CMD_NAME = "node"

var (
// Should be overridden during the final release build with ldflags
// to contain the actual version number
buildVersion = "devel"
Cmd = &cobra.Command{
Use: CMD_NAME,
Short: "Runs the Cartesi Rollups Node",
Long: "Runs the Cartesi Rollups Node as a single process",
RunE: run,
nodeService = node.Service{}
createInfo = node.CreateInfo{
CreateInfo: service.CreateInfo{
Name: "cartesi-rollups-node",
ProcOwner: true,
EnableSignalHandling: true,
TelemetryCreate: true,
TelemetryAddress: ":10001",
Impl: &nodeService,
},
MaxStartupTime: 10 * time.Second,
}
enableClaimSubmission bool
)

var Cmd = &cobra.Command{
Use: createInfo.Name,
Short: "Runs " + createInfo.Name,
Long: "Runs " + createInfo.Name + " as a single process",
Run: run,
}

func init() {
Cmd.Flags().BoolVar(&enableClaimSubmission,
"claim-submission", true,
createInfo.LoadEnv()
Cmd.Flags().BoolVar(&createInfo.EnableClaimSubmission,
"claim-submission", createInfo.EnableClaimSubmission,
"enable or disable claim submission (reader mode)")
Cmd.Flags().Var(&createInfo.LogLevel,
"log-level",
"log level: debug, info, warn or error")
Cmd.Flags().BoolVar(&createInfo.LogPretty,
"log-color", createInfo.LogPretty,
"tint the logs (colored output)")
Cmd.Flags().DurationVar(&createInfo.MaxStartupTime,
"max-startup-time", createInfo.MaxStartupTime,
"maximum startup time in seconds")
}

func run(cmd *cobra.Command, args []string) error {
startTime := time.Now()

ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()

cfg := config.FromEnv()
if cmd.Flags().Lookup("claim-submission").Changed {
cfg.FeatureClaimSubmissionEnabled = enableClaimSubmission
if enableClaimSubmission && cfg.Auth == nil {
cfg.Auth = config.AuthFromEnv()
}
}

database, err := repository.Connect(ctx, cfg.PostgresEndpoint.Value)
if err != nil {
slog.Error("Node couldn't connect to the database", "error", err)
os.Exit(1)
}
defer database.Close()

// create the node supervisor
supervisor, err := node.Setup(ctx, cfg, database)
if err != nil {
slog.Error("Node exited with an error", "error", err)
os.Exit(1)
}

// logs startup time
ready := make(chan struct{}, 1)
go func() {
select {
case <-ready:
duration := time.Since(startTime)
slog.Info("Node is ready", "after", duration)
case <-ctx.Done():
}
}()

// start supervisor
if err := supervisor.Start(ctx, ready); err != nil {
slog.Error("Node exited with an error", "error", err)
os.Exit(1)
}

return err
func run(cmd *cobra.Command, args []string) {
cobra.CheckErr(node.Create(&createInfo, &nodeService))
nodeService.CreateDefaultHandlers("")
cobra.CheckErr(nodeService.Serve())
}
5 changes: 4 additions & 1 deletion cmd/cartesi-rollups-validator/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ func init() {
Cmd.Flags().Var(&createInfo.LogLevel,
"log-level",
"log level: debug, info, warn or error")
Cmd.Flags().BoolVar(&createInfo.LogPretty,
"log-color", createInfo.LogPretty,
"tint the logs (colored output)")
Cmd.Flags().StringVar(&createInfo.PostgresEndpoint.Value,
"postgres-endpoint", createInfo.PostgresEndpoint.Value,
"Postgres endpoint")
}

func run(cmd *cobra.Command, args []string) {
cobra.CheckErr(validator.Create(createInfo, &validatorService))
cobra.CheckErr(validator.Create(&createInfo, &validatorService))
validatorService.CreateDefaultHandlers("/" + validatorService.Name)
cobra.CheckErr(validatorService.Serve())
}
12 changes: 1 addition & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@ require (
github.com/aws/aws-sdk-go-v2 v1.32.2
github.com/aws/aws-sdk-go-v2/config v1.18.45
github.com/aws/aws-sdk-go-v2/service/kms v1.37.2
github.com/davecgh/go-spew v1.1.1
github.com/deepmap/oapi-codegen/v2 v2.1.0
github.com/golang-migrate/migrate/v4 v4.18.1
github.com/jackc/pgconn v1.14.3
github.com/jackc/pgx v3.6.2+incompatible
github.com/jackc/pgx/v5 v5.7.1
github.com/lmittmann/tint v1.0.5
github.com/mattn/go-isatty v0.0.20
github.com/oapi-codegen/runtime v1.1.1
golang.org/x/sync v0.8.0
golang.org/x/text v0.19.0
Expand All @@ -46,12 +42,12 @@ require (
github.com/aws/smithy-go v1.22.0 // indirect
github.com/bits-and-blooms/bitset v1.14.3 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
github.com/cockroachdb/apd v1.1.0 // indirect
github.com/consensys/bavard v0.1.22 // indirect
github.com/consensys/gnark-crypto v0.14.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
github.com/crate-crypto/go-ipa v0.0.0-20240724233137-53bbb0ceb27a // indirect
github.com/crate-crypto/go-kzg-4844 v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/ethereum/c-kzg-4844 v1.0.3 // indirect
Expand All @@ -61,19 +57,14 @@ require (
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/holiman/uint256 v1.3.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
Expand All @@ -86,7 +77,6 @@ require (
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
Expand Down
Loading

0 comments on commit 32d7968

Please sign in to comment.