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

Merged
merged 4 commits into from
Dec 19, 2024
Merged
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
127 changes: 40 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,57 @@
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",
EnableSignalHandling: true,
TelemetryCreate: true,
TelemetryAddress: ":10002",
Impl: &advancerService,
},
MaxStartupTime: 10 * time.Second,
InspectAddress: ":10012",
}
)

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().StringVar(&createInfo.TelemetryAddress,
"telemetry-address", createInfo.TelemetryAddress,
"telemetry address")
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().StringVar(&createInfo.InspectAddress,
"inspect-address", createInfo.InspectAddress,
"inspect address")
}

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("")
cobra.CheckErr(advancerService.Serve())
}
39 changes: 15 additions & 24 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 @@ -20,13 +19,13 @@ var (
createInfo = claimer.CreateInfo{
CreateInfo: service.CreateInfo{
Name: "claimer",
ProcOwner: true,
EnableSignalHandling: true,
TelemetryCreate: true,
TelemetryAddress: ":8081",
TelemetryAddress: ":10004",
Impl: &claimerService,
},
EnableSubmission: true,
MaxStartupTime: 10 * time.Second,
}
)

Expand All @@ -38,40 +37,32 @@ 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")
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.BlockchainHttpEndpoint.Value,
"blockchain-http-endpoint", createInfo.BlockchainHttpEndpoint.Value,
"blockchain http endpoint")
Cmd.Flags().DurationVar(&createInfo.PollInterval,
"poll-interval", createInfo.PollInterval,
"poll interval")
Cmd.Flags().StringVar(&createInfo.LogLevel,
"log-level", createInfo.LogLevel,
"log level: debug, info, warn or error")
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))
claimerService.CreateDefaultHandlers("/" + claimerService.Name)
cobra.CheckErr(claimer.Create(&createInfo, &claimerService))
claimerService.CreateDefaultHandlers("")
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