Skip to content

Commit

Permalink
reafactor: use service logger
Browse files Browse the repository at this point in the history
  • Loading branch information
mpolitzer committed Dec 6, 2024
1 parent f64ef31 commit dc9e0ff
Show file tree
Hide file tree
Showing 19 changed files with 441 additions and 508 deletions.
17 changes: 14 additions & 3 deletions cmd/cartesi-rollups-evm-reader/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -24,8 +26,11 @@ var (
TelemetryAddress: ":10000",
Impl: &readerService,
},
DefaultBlockString: "safe",
EvmReaderPersistentConfig: model.EvmReaderPersistentConfig{
DefaultBlock: model.DefaultBlockStatusSafe,
},
}
DefaultBlockString = "safe"
)

var Cmd = &cobra.Command{
Expand All @@ -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'`)

Expand Down Expand Up @@ -78,6 +83,12 @@ func init() {
}

func run(cmd *cobra.Command, args []string) {
if cmd.Flags().Changed("default-block") {
var err error
createInfo.DefaultBlock, err = config.ToDefaultBlockFromString(DefaultBlockString)
cobra.CheckErr(err)
}

ready := make(chan struct{}, 1)
cobra.CheckErr(evmreader.Create(&createInfo, &readerService))
readerService.CreateDefaultHandlers("/" + readerService.Name)
Expand Down
73 changes: 35 additions & 38 deletions internal/advancer/advancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"time"

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
56 changes: 16 additions & 40 deletions internal/advancer/advancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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() {
Expand Down Expand Up @@ -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
}

Expand Down
Loading

0 comments on commit dc9e0ff

Please sign in to comment.