Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use service logger
Browse files Browse the repository at this point in the history
mpolitzer committed Dec 11, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 5a3e8eb commit b4586cf
Showing 27 changed files with 742 additions and 945 deletions.
2 changes: 1 addition & 1 deletion cmd/cartesi-rollups-claimer/root/root.go
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ func init() {
}

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())
}
20 changes: 15 additions & 5 deletions cmd/cartesi-rollups-evm-reader/root/root.go
Original file line number Diff line number Diff line change
@@ -4,7 +4,9 @@
package root

import (
"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/spf13/cobra"
@@ -24,8 +26,11 @@ var (
TelemetryAddress: ":10000",
Impl: &readerService,
},
DefaultBlockString: "safe",
EvmReaderPersistentConfig: model.EvmReaderPersistentConfig{
DefaultBlock: model.DefaultBlockStatusSafe,
},
}
DefaultBlockString = "safe"
)

var Cmd = &cobra.Command{
@@ -38,8 +43,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'`)

@@ -78,8 +83,13 @@ func init() {
}

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)
}

cobra.CheckErr(evmreader.Create(&createInfo, &readerService))
readerService.CreateDefaultHandlers("/" + readerService.Name)
cobra.CheckErr(readerService.Start(nil, ready))
cobra.CheckErr(readerService.Serve())
}
90 changes: 25 additions & 65 deletions cmd/cartesi-rollups-node/root/root.go
Original file line number Diff line number Diff line change
@@ -4,84 +4,44 @@
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: "supervisor",
ProcOwner: true,
EnableSignalHandling: true,
TelemetryCreate: true,
TelemetryAddress: ":10001",
Impl: &nodeService,
},
}
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)")
}

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())
}
73 changes: 35 additions & 38 deletions internal/advancer/advancer.go
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"time"

@@ -43,25 +42,11 @@ type IAdvancerMachines interface {
Apps() []Address
}

type Advancer struct {
repository IAdvancerRepository
machines IAdvancerMachines
}

type Service struct {
service.Service
Advancer
inspector *inspect.Inspector
}

func New(machines IAdvancerMachines, repository IAdvancerRepository) (*Advancer, error) {
if machines == nil {
return nil, ErrInvalidMachines
}
if repository == nil {
return nil, ErrInvalidRepository
}
return &Advancer{machines: machines, repository: repository}, nil
repository IAdvancerRepository
machines IAdvancerMachines
inspector inspect.Inspector
}

type CreateInfo struct {
@@ -92,29 +77,41 @@ func Create(c *CreateInfo, s *Service) error {
return err
}

if c.Repository == nil {
c.Repository, err = repository.Connect(s.Context, c.PostgresEndpoint.Value)
if err != nil {
return err
if s.repository == nil {
if c.Repository == nil {
c.Repository, err = repository.Connect(s.Context, c.PostgresEndpoint.Value)
if err != nil {
return err
}
}
s.repository = c.Repository
}
s.repository = c.Repository

if c.Machines == nil {
c.Machines, err = machines.Load(s.Context, c.Repository, c.MachineServerVerbosity.Value)
if err != nil {
return err
if s.machines == nil {
if c.Machines == nil {
c.Machines, err = machines.Load(s.Context,
c.Repository, c.MachineServerVerbosity.Value, s.Logger)
if err != nil {
return err
}
}
s.machines = c.Machines
}
s.machines = c.Machines

if s.Service.ServeMux == nil {
if c.CreateInfo.ServeMux == nil {
c.ServeMux = http.NewServeMux()
// allow partial construction for testing
if c.Machines != nil {
s.inspector = inspect.Inspector{
IInspectMachines: c.Machines,
}
if s.Service.ServeMux == nil {
if c.CreateInfo.ServeMux == nil {
c.ServeMux = http.NewServeMux()
}
s.ServeMux = c.ServeMux
}
s.ServeMux.Handle("/inspect/{dapp}", http.Handler(&s.inspector))
s.ServeMux.Handle("/inspect/{dapp}/{payload}", http.Handler(&s.inspector))
}
s.Service.ServeMux.Handle("/inspect/{dapp}", http.Handler(s.inspector))
s.Service.ServeMux.Handle("/inspect/{dapp}/{payload}", http.Handler(s.inspector))

return nil
}
@@ -144,7 +141,7 @@ func (v *Service) String() string {
// It gets unprocessed inputs from the repository,
// runs them through the cartesi machine,
// and updates the repository with the outputs.
func (advancer *Advancer) Step(ctx context.Context) error {
func (advancer *Service) Step(ctx context.Context) error {
// Dynamically updates the list of machines
err := advancer.machines.UpdateMachines(ctx)
if err != nil {
@@ -154,15 +151,15 @@ func (advancer *Advancer) Step(ctx context.Context) error {
apps := advancer.machines.Apps()

// Gets the unprocessed inputs (of all apps) from the repository.
slog.Debug("advancer: querying for unprocessed inputs")
advancer.Logger.Debug("querying for unprocessed inputs")
inputs, err := advancer.repository.GetUnprocessedInputs(ctx, apps)
if err != nil {
return err
}

// Processes each set of inputs.
for app, inputs := range inputs {
slog.Debug(fmt.Sprintf("advancer: processing %d input(s) from %v", len(inputs), app))
advancer.Logger.Debug(fmt.Sprintf("processing %d input(s) from %v", len(inputs), app))
err := advancer.process(ctx, app, inputs)
if err != nil {
return err
@@ -181,7 +178,7 @@ func (advancer *Advancer) Step(ctx context.Context) error {
}

// process sequentially processes inputs from the the application.
func (advancer *Advancer) process(ctx context.Context, app Address, inputs []*Input) error {
func (advancer *Service) process(ctx context.Context, app Address, inputs []*Input) error {
// Asserts that the app has an associated machine.
machine, exists := advancer.machines.GetAdvanceMachine(app)
if !exists {
@@ -195,7 +192,7 @@ func (advancer *Advancer) process(ctx context.Context, app Address, inputs []*In

// FIXME if theres a change in epoch id call update epochs
for _, input := range inputs {
slog.Info("advancer: Processing input", "app", app, "id", input.Id, "index", input.Index)
advancer.Logger.Info("Processing input", "app", app, "id", input.Id, "index", input.Index)

// Sends the input to the cartesi machine.
res, err := machine.Advance(ctx, input.RawData, input.Index)
56 changes: 16 additions & 40 deletions internal/advancer/advancer_test.go
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ import (
"github.com/cartesi/rollups-node/internal/advancer/machines"
. "github.com/cartesi/rollups-node/internal/model"
"github.com/cartesi/rollups-node/internal/nodemachine"
"github.com/cartesi/rollups-node/pkg/service"

"github.com/stretchr/testify/suite"
)
@@ -25,41 +26,16 @@ func TestAdvancer(t *testing.T) {

type AdvancerSuite struct{ suite.Suite }

func (s *AdvancerSuite) TestNew() {
s.Run("Ok", func() {
require := s.Require()
machines := newMockMachines()
machines.Map[randomAddress()] = &MockMachine{}
var repository IAdvancerRepository = &MockRepository{}
advancer, err := New(machines, repository)
require.NotNil(advancer)
require.Nil(err)
})

s.Run("InvalidMachines", func() {
require := s.Require()
var machines IAdvancerMachines = nil
var repository IAdvancerRepository = &MockRepository{}
advancer, err := New(machines, repository)
require.Nil(advancer)
require.Error(err)
require.Equal(ErrInvalidMachines, err)
})

s.Run("InvalidRepository", func() {
require := s.Require()
machines := newMockMachines()
machines.Map[randomAddress()] = &MockMachine{}
var repository IAdvancerRepository = nil
advancer, err := New(machines, repository)
require.Nil(advancer)
require.Error(err)
require.Equal(ErrInvalidRepository, err)
})
}

func (s *AdvancerSuite) TestPoller() {
s.T().Skip("TODO")
func New(m IAdvancerMachines, r IAdvancerRepository) (*Service, error) {
s := &Service{
machines: m,
repository: r,
}
return s, Create(&CreateInfo{
CreateInfo: service.CreateInfo{
Name: "advancer",
},
}, s)
}

func (s *AdvancerSuite) TestRun() {
@@ -105,15 +81,15 @@ func (s *AdvancerSuite) TestRun() {
}

func (s *AdvancerSuite) TestProcess() {
setup := func() (IAdvancerMachines, *MockRepository, *Advancer, Address) {
setup := func() (IAdvancerMachines, *MockRepository, *Service, Address) {
require := s.Require()

app := randomAddress()
machines := newMockMachines()
machines.Map[app] = &MockMachine{}
repository := &MockRepository{}
advancer := &Advancer{
machines: machines,
repository: repository,
}
advancer, err := New(machines, repository)
require.Nil(err)
return machines, repository, advancer, app
}

Loading

0 comments on commit b4586cf

Please sign in to comment.