Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/rebase services #611

Open
wants to merge 3 commits into
base: feature/new-build
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 34 additions & 87 deletions cmd/cartesi-rollups-advancer/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,104 +4,51 @@
package root

import (
"context"
"fmt"
"log/slog"
"net/http"
"os/signal"
"syscall"
"time"

"github.com/cartesi/rollups-node/internal/advancer"
"github.com/cartesi/rollups-node/internal/advancer/config"
"github.com/cartesi/rollups-node/internal/advancer/machines"
"github.com/cartesi/rollups-node/internal/inspect"
"github.com/cartesi/rollups-node/internal/repository"
"github.com/cartesi/rollups-node/internal/services"
"github.com/cartesi/rollups-node/internal/services/startup"

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

const CMD_NAME = "advancer"

var (
buildVersion = "devel"
Cmd = &cobra.Command{
Use: CMD_NAME,
Short: "Runs the Advancer",
Long: "Runs the Advancer in standalone mode",
RunE: run,
buildVersion = "devel"
advancerService = advancer.Service{}
createInfo = advancer.CreateInfo{
CreateInfo: service.CreateInfo{
Name: "advancer",
ProcOwner: true,
EnableSignalHandling: true,
TelemetryCreate: true,
TelemetryAddress: ":10001",
Impl: &advancerService,
},
MaxStartupTime: 10 * time.Second,
}
)

func getDatabase(ctx context.Context, endpoint string) (*repository.Database, error) {
database, err := repository.Connect(ctx, endpoint)
if err != nil {
return nil, fmt.Errorf("failed to connect to the database: %w", err)
}

return database, nil
var Cmd = &cobra.Command{
Use: createInfo.Name,
Short: "Runs " + createInfo.Name,
Long: "Runs " + createInfo.Name + " in standalone mode",
Run: run,
}

func healthcheckHandler(w http.ResponseWriter, r *http.Request) {
slog.Debug("Advancer received a healthcheck request")
w.WriteHeader(http.StatusOK)
func init() {
createInfo.LoadEnv()
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 {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()

c := config.GetAdvancerConfig()
startup.ConfigLogs(c.LogLevel, c.LogPrettyEnabled)

slog.Info("Starting the Cartesi Rollups Node Advancer", "version", buildVersion, "config", c)

database, err := getDatabase(ctx, c.PostgresEndpoint.Value)
if err != nil {
return err
}
defer database.Close()

repo := &repository.MachineRepository{Database: database}

machines, err := machines.Load(ctx, repo, c.MachineServerVerbosity)
if err != nil {
return fmt.Errorf("failed to load the machines: %w", err)
}
defer machines.Close()

inspector, err := inspect.New(machines)
if err != nil {
return fmt.Errorf("failed to create the inspector: %w", err)
}

advancer, err := advancer.New(machines, repo)
if err != nil {
return fmt.Errorf("failed to create the advancer: %w", err)
}

poller, err := advancer.Poller(c.AdvancerPollingInterval)
if err != nil {
return fmt.Errorf("failed to create the advancer service: %w", err)
}

serveMux := http.NewServeMux()
serveMux.Handle("/healthz", http.HandlerFunc(healthcheckHandler))
serveMux.Handle("/inspect/{dapp}", http.Handler(inspector))
serveMux.Handle("/inspect/{dapp}/{payload}", http.Handler(inspector))

httpServer := &http.Server{
Addr: fmt.Sprintf("%v:%v", c.HttpAddress, c.HttpPort),
Handler: services.CorsMiddleware(serveMux),
}

go func() {
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
slog.Error("Could not listen on %s: %v\n", httpServer.Addr, err)
stop()
}
}()

return poller.Start(ctx)
func run(cmd *cobra.Command, args []string) {
cobra.CheckErr(advancer.Create(&createInfo, &advancerService))
advancerService.CreateDefaultHandlers("/" + advancerService.Name)
cobra.CheckErr(advancerService.Serve())
}
34 changes: 13 additions & 21 deletions cmd/cartesi-rollups-claimer/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
package root

import (
"log/slog"
"time"

"github.com/cartesi/rollups-node/internal/claimer"
"github.com/cartesi/rollups-node/internal/config"
"github.com/cartesi/rollups-node/pkg/service"
"github.com/spf13/cobra"
)
Expand All @@ -23,10 +22,11 @@ var (
ProcOwner: true,
EnableSignalHandling: true,
TelemetryCreate: true,
TelemetryAddress: ":8081",
TelemetryAddress: ":10003",
Impl: &claimerService,
},
EnableSubmission: true,
MaxStartupTime: 10 * time.Second,
}
)

Expand All @@ -38,21 +38,7 @@ var Cmd = &cobra.Command{
}

func init() {
c := config.FromEnv()
createInfo.BlockchainHttpEndpoint = c.BlockchainHttpEndpoint
createInfo.PostgresEndpoint = c.PostgresEndpoint
createInfo.PollInterval = c.ClaimerPollingInterval
createInfo.LogLevel = map[slog.Level]string{
slog.LevelDebug: "debug",
slog.LevelInfo: "info",
slog.LevelWarn: "warn",
slog.LevelError: "error",
}[c.LogLevel]
createInfo.EnableSubmission = c.FeatureClaimSubmissionEnabled
if createInfo.EnableSubmission {
createInfo.Auth = c.Auth
}

createInfo.LoadEnv()
Cmd.Flags().StringVar(&createInfo.TelemetryAddress,
"telemetry-address", createInfo.TelemetryAddress,
"health check and metrics address and port")
Expand All @@ -62,16 +48,22 @@ func init() {
Cmd.Flags().DurationVar(&createInfo.PollInterval,
"poll-interval", createInfo.PollInterval,
"poll interval")
Cmd.Flags().StringVar(&createInfo.LogLevel,
"log-level", createInfo.LogLevel,
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)
}
Loading
Loading