Skip to content

Commit a71586c

Browse files
committed
PRT tool WIP
1 parent dfc9b3e commit a71586c

6 files changed

Lines changed: 126 additions & 78 deletions

File tree

internal/config/generate/Config.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ default = "true"
4040
go-type = "bool"
4141
description = """
4242
If set to false, the node will not start the inspect service."""
43-
used-by = ["advancer", "node", "prt"]
43+
used-by = ["advancer", "node"]
4444

4545
[features.CARTESI_FEATURE_JSONRPC_API_ENABLED]
4646
default = "true"

internal/config/generated.go

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/prt/prt.go

Lines changed: 18 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,81 +7,36 @@ import (
77
"context"
88
"errors"
99
"fmt"
10-
"net/http"
11-
"time"
1210

1311
"github.com/cartesi/rollups-node/internal/config"
14-
"github.com/cartesi/rollups-node/internal/inspect"
1512
"github.com/cartesi/rollups-node/internal/manager"
16-
. "github.com/cartesi/rollups-node/internal/model"
13+
"github.com/cartesi/rollups-node/internal/model"
1714
"github.com/cartesi/rollups-node/internal/repository"
1815
"github.com/cartesi/rollups-node/pkg/service"
1916
)
2017

2118
var (
2219
ErrInvalidMachines = errors.New("machines must not be nil")
2320
ErrInvalidRepository = errors.New("repository must not be nil")
24-
25-
ErrNoApp = errors.New("no machine for application")
26-
ErrNoTournaments = errors.New("no active tournaments")
27-
ErrInvalidStageConfig = errors.New("invalid stage configuration")
2821
)
2922

3023
type PRTRepository interface {
31-
ListInputs(ctx context.Context, nameOrAddress string, f repository.InputFilter, p repository.Pagination, descending bool) ([]*Input, uint64, error)
32-
GetLastInput(ctx context.Context, appAddress string, epochIndex uint64) (*Input, error)
33-
GetEpoch(ctx context.Context, nameOrAddress string, index uint64) (*Epoch, error)
34-
}
35-
36-
type Tournament struct {
37-
ID string
38-
ApplicationID int64
39-
StartEpoch uint64
40-
EndEpoch uint64
41-
Participants []string
42-
Teams map[string][]string
43-
Status TournamentStatus
44-
CreatedAt time.Time
45-
UpdatedAt time.Time
46-
}
47-
48-
type TournamentStatus int
49-
50-
const (
51-
TournamentStatus_Created TournamentStatus = iota
52-
TournamentStatus_Active
53-
TournamentStatus_Completed
54-
TournamentStatus_Failed
55-
)
56-
57-
type ComputationHash struct {
58-
ApplicationAddress string
59-
StageLevel int // 0 for complete, 1+ for sparse stages
60-
StartStep uint64
61-
EndStep uint64
62-
StateInterval uint64 // Steps between state hashes
63-
RootHash []byte
64-
CreatedAt time.Time
24+
ListApplications(ctx context.Context, f repository.ApplicationFilter, p repository.Pagination, descending bool) ([]*model.Application, uint64, error)
6525
}
6626

6727
type Service struct {
6828
service.Service
6929
config config.PrtConfig
7030
repository PRTRepository
7131
machineManager manager.MachineProvider
72-
inspector *inspect.Inspector
73-
HTTPServer *http.Server
74-
HTTPServerFunc func() error
7532
}
7633

77-
// CreateInfo contains the configuration for creating a PRT service
7834
type CreateInfo struct {
7935
service.CreateInfo
8036
Config config.PrtConfig
8137
Repository repository.Repository
8238
}
8339

84-
// Create initializes a new PRT service
8540
func Create(ctx context.Context, c *CreateInfo) (*Service, error) {
8641
var err error
8742
if err = ctx.Err(); err != nil {
@@ -112,38 +67,35 @@ func Create(ctx context.Context, c *CreateInfo) (*Service, error) {
11267
)
11368
s.machineManager = manager
11469

115-
// Initialize the inspect service if enabled
116-
if c.Config.FeatureInspectEnabled {
117-
s.inspector, s.HTTPServer, s.HTTPServerFunc = inspect.NewInspector(
118-
c.Repository,
119-
manager,
120-
c.Config.InspectAddress,
121-
c.LogLevel,
122-
c.LogColor,
123-
)
124-
}
125-
12670
return s, nil
12771
}
12872

12973
// Service interface implementation
13074
func (s *Service) Alive() bool { return true }
13175
func (s *Service) Ready() bool { return true }
13276
func (s *Service) Reload() []error { return nil }
133-
134-
func (s *Service) Tick() []error {
135-
s.Logger.Info("PRT service tick... (TODO)")
136-
return []error{}
137-
}
138-
13977
func (s *Service) Stop(b bool) []error {
14078
return nil
14179
}
142-
14380
func (s *Service) Serve() error {
14481
return s.Service.Serve()
14582
}
146-
14783
func (s *Service) String() string {
14884
return s.Name
14985
}
86+
func (s *Service) Tick() []error {
87+
s.Logger.Info("PRT service tick...")
88+
apps, _, err := s.repository.ListApplications(context.Background(),
89+
repository.ApplicationFilter{
90+
State: model.Pointer(model.ApplicationState_Enabled),
91+
ConsensusType: model.Pointer(model.ConsensusType_Prt),
92+
}, repository.Pagination{}, false)
93+
if err != nil {
94+
s.Logger.Error("Failed to list applications", "error", err)
95+
return []error{err}
96+
}
97+
for _, app := range apps {
98+
s.Logger.Info("Application found", "name", app.Name, "consensus type:", app.ConsensusType)
99+
}
100+
return []error{}
101+
}

internal/prt/prt_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// (c) Cartesi and individual authors (see AUTHORS)
2+
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)
3+
4+
package prt
5+
6+
import (
7+
"context"
8+
"log/slog"
9+
"os"
10+
"testing"
11+
12+
"github.com/cartesi/rollups-node/internal/model"
13+
"github.com/cartesi/rollups-node/internal/repository"
14+
"github.com/cartesi/rollups-node/pkg/service"
15+
"github.com/lmittmann/tint"
16+
17+
"github.com/ethereum/go-ethereum/common"
18+
19+
"github.com/stretchr/testify/assert"
20+
"github.com/stretchr/testify/mock"
21+
)
22+
23+
type prtRepositoryMock struct {
24+
mock.Mock
25+
}
26+
27+
func (m *prtRepositoryMock) ListApplications(
28+
ctx context.Context,
29+
f repository.ApplicationFilter,
30+
pagination repository.Pagination,
31+
descending bool,
32+
) ([]*model.Application, uint64, error) {
33+
args := m.Called(ctx, f, pagination, descending)
34+
return args.Get(0).([]*model.Application), args.Get(1).(uint64), args.Error(2)
35+
}
36+
37+
func newServiceMock() (*Service, *prtRepositoryMock) {
38+
opts := &tint.Options{
39+
Level: slog.LevelDebug,
40+
AddSource: true,
41+
// RFC3339 with milliseconds and without timezone
42+
TimeFormat: "2006-01-02T15:04:05.000",
43+
}
44+
handler := tint.NewHandler(os.Stdout, opts)
45+
repository := &prtRepositoryMock{}
46+
47+
prt := &Service{
48+
Service: service.Service{
49+
Name: "prt",
50+
Logger: slog.New(handler),
51+
},
52+
repository: repository,
53+
}
54+
return prt, repository
55+
}
56+
57+
func makeApplication(id int64) *model.Application {
58+
return &model.Application{
59+
ID: id,
60+
IApplicationAddress: common.HexToAddress("0x01"),
61+
IConsensusAddress: common.HexToAddress("0x01"),
62+
IInputBoxAddress: common.HexToAddress("0x02"),
63+
}
64+
}
65+
66+
// //////////////////////////////////////////////////////////////////////////////
67+
// Basic Service Tests
68+
// //////////////////////////////////////////////////////////////////////////////
69+
func TestServiceMethods(t *testing.T) {
70+
s, _ := newServiceMock()
71+
72+
assert.True(t, s.Alive())
73+
assert.True(t, s.Ready())
74+
assert.Empty(t, s.Reload())
75+
assert.Empty(t, s.Stop(false))
76+
assert.NotEmpty(t, s.String())
77+
}
78+
79+
func TestTick_NoApplications(t *testing.T) {
80+
s, r := newServiceMock()
81+
defer r.AssertExpectations(t)
82+
83+
r.On("ListApplications", mock.Anything, mock.Anything, mock.Anything, mock.Anything).
84+
Return([]*model.Application{}, uint64(0), nil).Once()
85+
86+
errs := s.Tick()
87+
assert.Empty(t, errs)
88+
}
89+
90+
func TestTick_WithApplications(t *testing.T) {
91+
s, r := newServiceMock()
92+
defer r.AssertExpectations(t)
93+
94+
app := makeApplication(1)
95+
apps := []*model.Application{app}
96+
97+
r.On("ListApplications", mock.Anything, mock.Anything, mock.Anything, mock.Anything).
98+
Return(apps, uint64(1), nil).Once()
99+
100+
errs := s.Tick()
101+
assert.Empty(t, errs)
102+
}

internal/repository/postgres/schema/migrations/000001_create_initial_schema.down.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ DROP TYPE IF EXISTS "SnapshotPolicy";
3939
DROP TYPE IF EXISTS "EpochStatus";
4040
DROP TYPE IF EXISTS "DefaultBlock";
4141
DROP TYPE IF EXISTS "InputCompletionStatus";
42+
DROP TYPE IF EXISTS "ConsensusType";
4243
DROP TYPE IF EXISTS "ApplicationState";
4344
DROP DOMAIN IF EXISTS "data_availability";
4445
DROP DOMAIN IF EXISTS "hash";

test/validator/validator_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func (s *ValidatorRepositoryIntegrationSuite) TestItReturnsPristineClaim() {
8484
s.Run("WhenThereAreNoOutputsAndNoPreviousEpoch", func() {
8585
app := &model.Application{
8686
Name: "test-app",
87+
ConsensusType: model.ConsensusType_Authority,
8788
IApplicationAddress: common.BytesToAddress([]byte("deadbeef")),
8889
IConsensusAddress: common.BytesToAddress([]byte("beadbeef")),
8990
TemplateHash: common.BytesToHash([]byte("template")),
@@ -150,6 +151,7 @@ func (s *ValidatorRepositoryIntegrationSuite) TestItReturnsPreviousClaim() {
150151
s.Run("WhenThereAreNoOutputsAndThereIsAPreviousEpoch", func() {
151152
app := &model.Application{
152153
Name: "test-app",
154+
ConsensusType: model.ConsensusType_Authority,
153155
IApplicationAddress: common.BytesToAddress([]byte("deadbeef")),
154156
IConsensusAddress: common.BytesToAddress([]byte("beadbeef")),
155157
TemplateHash: common.BytesToHash([]byte("template")),
@@ -253,6 +255,7 @@ func (s *ValidatorRepositoryIntegrationSuite) TestItReturnsANewClaimAndProofs()
253255
s.Run("WhenThereAreOutputsAndNoPreviousEpoch", func() {
254256
app := &model.Application{
255257
Name: "test-app",
258+
ConsensusType: model.ConsensusType_Authority,
256259
IApplicationAddress: common.BytesToAddress([]byte("deadbeef")),
257260
IConsensusAddress: common.BytesToAddress([]byte("beadbeef")),
258261
TemplateHash: common.BytesToHash([]byte("template")),
@@ -340,6 +343,7 @@ func (s *ValidatorRepositoryIntegrationSuite) TestItReturnsANewClaimAndProofs()
340343
s.Run("WhenThereAreOutputsAndAPreviousEpoch", func() {
341344
app := &model.Application{
342345
Name: "test-app",
346+
ConsensusType: model.ConsensusType_Authority,
343347
IApplicationAddress: common.BytesToAddress([]byte("deadbeef")),
344348
IConsensusAddress: common.BytesToAddress([]byte("beadbeef")),
345349
TemplateHash: common.BytesToHash([]byte("template")),

0 commit comments

Comments
 (0)