diff --git a/internal/node/machine/advancer/advancer.go b/internal/node/machine/advancer/advancer.go new file mode 100644 index 000000000..4921e525e --- /dev/null +++ b/internal/node/machine/advancer/advancer.go @@ -0,0 +1,118 @@ +// (c) Cartesi and individual authors (see AUTHORS) +// SPDX-License-Identifier: Apache-2.0 (see LICENSE) + +package advancer + +import ( + "context" + "errors" + "fmt" + "log/slog" + "sync" + "time" + + "github.com/cartesi/rollups-node/internal/node/machine/nodemachine" + . "github.com/cartesi/rollups-node/internal/node/model" +) + +type Repository interface { + // Only needs Id and RawData fields from model.Input. + GetInputs(_ context.Context, appAddresses []Address) (map[Address][]*Input, error) + + StoreResults(context.Context, *Input, *nodemachine.AdvanceResult) error +} + +type Machine interface { + Advance(_ context.Context, input []byte) (*nodemachine.AdvanceResult, error) +} + +type MachineAdvancer struct { + machines *sync.Map // map[Address]Machine + repository Repository + ticker *time.Ticker +} + +var ( + ErrInvalidMachines = errors.New("machines must not be nil") + ErrInvalidRepository = errors.New("repository must not be nil") + ErrInvalidPollingInterval = errors.New("polling interval must be greater than zero") + + ErrInvalidAddress = errors.New("no machine for address") +) + +func New( + machines *sync.Map, + repository Repository, + pollingInterval time.Duration, +) (*MachineAdvancer, error) { + if machines == nil { + return nil, ErrInvalidMachines + } + if repository == nil { + return nil, ErrInvalidRepository + } + if pollingInterval <= 0 { + return nil, ErrInvalidPollingInterval + } + return &MachineAdvancer{ + machines: machines, + repository: repository, + ticker: time.NewTicker(pollingInterval), + }, nil +} + +func (advancer *MachineAdvancer) Start(ctx context.Context) error { + for { + appAddresses := keysToSlice(advancer.machines) + slog.Info("advancer: getting unprocessed inputs from the database.", + "appAddresses", appAddresses) + + // Gets the unprocessed inputs (of all apps) from the repository. + inputs, err := advancer.repository.GetInputs(ctx, appAddresses) + if err != nil { + return err + } + + for appAddress, inputs := range inputs { + slog.Info("advancer: processing inputs.", "appAddress", appAddress) + value, ok := advancer.machines.Load(appAddress) + if !ok { + return fmt.Errorf("%w %s", ErrInvalidAddress, appAddress.String()) + } + machine := value.(Machine) + + // Processes all inputs sequentially. + for _, input := range inputs { + slog.Info("advancer: processing input", "id", input.Id) + slog.Debug("--->", "input", input) + res, err := machine.Advance(ctx, input.RawData) + if err != nil { + return err + } + + slog.Info("advancer: storing result") + slog.Debug("--->", "result", res) + err = advancer.repository.StoreResults(ctx, input, res) + if err != nil { + return err + } + } + } + + // Waits for the current polling interval to elapse. + slog.Info("advancer: waiting.") + <-advancer.ticker.C + } +} + +// ------------------------------------------------------------------------------------------------ + +// keysToSlice returns a slice with the keys of a sync.Map. +func keysToSlice(m *sync.Map) []Address { + keys := []Address{} + m.Range(func(key, _ any) bool { + keys = append(keys, key.(Address)) + return true + }) + return keys +} diff --git a/internal/node/machine/advancer/advancer_test.go b/internal/node/machine/advancer/advancer_test.go new file mode 100644 index 000000000..1c88faf56 --- /dev/null +++ b/internal/node/machine/advancer/advancer_test.go @@ -0,0 +1,242 @@ +// (c) Cartesi and individual authors (see AUTHORS) +// SPDX-License-Identifier: Apache-2.0 (see LICENSE) + +package advancer + +import ( + "context" + crand "crypto/rand" + "errors" + mrand "math/rand" + "testing" + "time" + + "github.com/cartesi/rollups-node/internal/node/machine/nodemachine" + "github.com/cartesi/rollups-node/internal/node/model" + + "github.com/stretchr/testify/suite" +) + +func TestMachineAdvancer(t *testing.T) { + suite.Run(t, new(MachineAdvancerSuite)) +} + +type MachineAdvancerSuite struct{ suite.Suite } + +func (s *MachineAdvancerSuite) TestNew() { + s.Run("Ok", func() { + require := s.Require() + machines := map[model.Address]Machine{randomAddress(): newMockMachine()} + repository := newMockRepository() + machineAdvancer, err := New(machines, repository, time.Nanosecond) + require.NotNil(machineAdvancer) + require.Nil(err) + }) + + s.Run("InvalidMachines", func() { + require := s.Require() + repository := newMockRepository() + machineAdvancer, err := New(nil, repository, time.Nanosecond) + require.Nil(machineAdvancer) + require.Equal(ErrInvalidMachines, err) + }) + + s.Run("InvalidRepository", func() { + require := s.Require() + machines := map[model.Address]Machine{randomAddress(): newMockMachine()} + machineAdvancer, err := New(machines, nil, time.Nanosecond) + require.Nil(machineAdvancer) + require.Equal(ErrInvalidRepository, err) + }) + + s.Run("InvalidPollingInterval", func() { + require := s.Require() + machines := map[model.Address]Machine{randomAddress(): newMockMachine()} + repository := newMockRepository() + machineAdvancer, err := New(machines, repository, time.Duration(0)) + require.Nil(machineAdvancer) + require.Equal(ErrInvalidPollingInterval, err) + }) +} + +func (s *MachineAdvancerSuite) TestStart() { + suite.Run(s.T(), new(StartSuite)) +} + +// ------------------------------------------------------------------------------------------------ + +type StartSuite struct { + suite.Suite + machines map[model.Address]Machine + repository *MockRepository +} + +func (s *StartSuite) SetupTest() { + s.machines = map[model.Address]Machine{} + s.repository = newMockRepository() +} + +// NOTE: This test is very basic! We need more tests! +func (s *StartSuite) TestBasic() { + require := s.Require() + + appAddress := randomAddress() + + machine := newMockMachine() + advanceResponse := randomAdvanceResponse() + machine.add(advanceResponse, nil) + s.machines[appAddress] = machine + + s.repository.add(map[model.Address][]model.Input{appAddress: randomInputs(1)}, nil, nil) + + machineAdvancer, err := New(s.machines, s.repository, time.Nanosecond) + require.NotNil(machineAdvancer) + require.Nil(err) + + err = machineAdvancer.Start() + require.Equal(testFinished, err) + + require.Len(s.repository.stored, 1) + require.Equal(advanceResponse, s.repository.stored[0]) +} + +// ------------------------------------------------------------------------------------------------ + +type MockMachine struct { + index uint8 + results []*nodemachine.AdvanceResult + errors []error +} + +func newMockMachine() *MockMachine { + return &MockMachine{ + index: 0, + results: []*nodemachine.AdvanceResult{}, + errors: []error{}, + } +} + +func (m *MockMachine) add(result *nodemachine.AdvanceResult, err error) { + m.results = append(m.results, result) + m.errors = append(m.errors, err) +} + +func (m *MockMachine) Advance( + _ context.Context, + input []byte, +) (*nodemachine.AdvanceResult, error) { + result, err := m.results[m.index], m.errors[m.index] + m.index += 1 + return result, err +} + +// ------------------------------------------------------------------------------------------------ + +type MockRepository struct { + getInputsIndex uint8 + getInputsResults []map[model.Address][]model.Input + getInputsErrors []error + + storeIndex uint8 + storeErrors []error + stored []*nodemachine.AdvanceResult +} + +func newMockRepository() *MockRepository { + return &MockRepository{ + getInputsIndex: 0, + getInputsResults: []map[model.Address][]model.Input{}, + getInputsErrors: []error{}, + storeIndex: 0, + storeErrors: []error{}, + stored: []*nodemachine.AdvanceResult{}, + } +} + +func (r *MockRepository) add( + getInputsResult map[model.Address][]model.Input, + getInputsError error, + storeError error, +) { + r.getInputsResults = append(r.getInputsResults, getInputsResult) + r.getInputsErrors = append(r.getInputsErrors, getInputsError) + r.storeErrors = append(r.storeErrors, storeError) +} + +var testFinished = errors.New("test finished") + +func (r *MockRepository) GetUnprocessedInputs( + appAddresses []model.Address, +) (map[model.Address][]model.Input, error) { + if int(r.getInputsIndex) == len(r.getInputsResults) { + return nil, testFinished + } + result, err := r.getInputsResults[r.getInputsIndex], r.getInputsErrors[r.getInputsIndex] + r.getInputsIndex += 1 + return result, err +} + +func (r *MockRepository) Store(input model.Input, res *nodemachine.AdvanceResult) error { + err := r.storeErrors[r.storeIndex] + r.storeIndex += 1 + r.stored = append(r.stored, res) + return err +} + +// ------------------------------------------------------------------------------------------------ + +func randomAddress() model.Address { + address := make([]byte, 20) + _, err := crand.Read(address) + if err != nil { + panic(err) + } + return model.Address(address) +} + +func randomHash() model.Hash { + hash := make([]byte, 32) + _, err := crand.Read(hash) + if err != nil { + panic(err) + } + return model.Hash(hash) +} + +func randomBytes() []byte { + size := mrand.Intn(100) + 1 + bytes := make([]byte, size) + _, err := crand.Read(bytes) + if err != nil { + panic(err) + } + return bytes +} + +func randomSliceOfBytes() [][]byte { + size := mrand.Intn(10) + 1 + slice := make([][]byte, size) + for i := 0; i < size; i++ { + slice[i] = randomBytes() + } + return slice +} + +func randomInputs(size int) []model.Input { + slice := make([]model.Input, size) + for i := 0; i < size; i++ { + slice[i] = model.Input{Id: uint64(i), RawData: randomBytes()} + } + return slice + +} + +func randomAdvanceResponse() *nodemachine.AdvanceResult { + return &nodemachine.AdvanceResult{ + Status: model.InputStatusAccepted, + Outputs: randomSliceOfBytes(), + Reports: randomSliceOfBytes(), + OutputsHash: randomHash(), + MachineHash: randomHash(), + } +} diff --git a/internal/node/machine/nodemachine/machine.go b/internal/node/machine/nodemachine/machine.go new file mode 100644 index 000000000..9b328e7e4 --- /dev/null +++ b/internal/node/machine/nodemachine/machine.go @@ -0,0 +1,213 @@ +// (c) Cartesi and individual authors (see AUTHORS) +// SPDX-License-Identifier: Apache-2.0 (see LICENSE) + +package nodemachine + +import ( + "context" + "errors" + "time" + + "github.com/cartesi/rollups-node/internal/node/machine/nodemachine/pmutex" + "github.com/cartesi/rollups-node/internal/node/model" + "github.com/cartesi/rollups-node/pkg/rollupsmachine" + + "golang.org/x/sync/semaphore" +) + +var ErrTimeLimitExceeded = errors.New("time limit exceeded") + +type AdvanceResult struct { + Status model.InputCompletionStatus + Outputs [][]byte + Reports [][]byte + OutputsHash model.Hash + MachineHash model.Hash +} + +func (res AdvanceResult) StatusOk() bool { + return res.Status == model.InputStatusAccepted || res.Status == model.InputStatusRejected +} + +type InspectResult struct { + Accepted bool + Reports [][]byte + Err error +} + +type RollupsMachine interface { + Fork() (*rollupsmachine.RollupsMachine, string, error) // NOTE: returns the concrete type + Close() error + + Hash() (model.Hash, error) + + Advance([]byte) (bool, [][]byte, [][]byte, model.Hash, error) + Inspect([]byte) (bool, [][]byte, error) +} + +type NodeMachine struct { + RollupsMachine + + // Timeout in seconds. + timeout time.Duration + + // Ensures advance/inspect mutual exclusion when accessing the inner RollupsMachine. + // Advances have a higher priority than Inspects to acquire the lock. + mutex *pmutex.PMutex + + // Controls how many inspects can be concurrently active. + inspects *semaphore.Weighted +} + +func New( + rollupsMachine RollupsMachine, + timeout time.Duration, + maxConcurrentInspects int8, +) *NodeMachine { + return &NodeMachine{ + RollupsMachine: rollupsMachine, + timeout: timeout, + mutex: pmutex.New(), + inspects: semaphore.NewWeighted(int64(maxConcurrentInspects)), + } +} + +func (machine *NodeMachine) Advance(ctx context.Context, input []byte) (*AdvanceResult, error) { + var fork RollupsMachine + var err error + + // Forks the machine. + machine.mutex.HLock() + fork, _, err = machine.Fork() + machine.mutex.Unlock() + if err != nil { + return nil, err + } + + // Sends the advance-state request to the forked machine. + accepted, outputs, reports, outputsHash, err := fork.Advance(input) + status, err := toInputStatus(accepted, err) + if err != nil { + return nil, errors.Join(err, fork.Close()) + } + + res := &AdvanceResult{ + Status: status, + Outputs: outputs, + Reports: reports, + OutputsHash: outputsHash, + } + + // Only gets the post-advance machine hash if the request was accepted. + if status == model.InputStatusAccepted { + res.MachineHash, err = fork.Hash() + if err != nil { + return nil, errors.Join(err, fork.Close()) + } + } + + // If the forked machine is in a valid state: + if res.StatusOk() { + // Closes the current machine. + err = machine.RollupsMachine.Close() + // Replaces the current machine with the fork. + machine.mutex.HLock() + machine.RollupsMachine = fork + machine.mutex.Unlock() + } else { + // Closes the forked machine. + err = fork.Close() + } + + return res, err +} + +func (machine *NodeMachine) Inspect(ctx context.Context, query []byte) (*InspectResult, error) { + // Controls how many inspects can be concurrently active. + err := machine.inspects.Acquire(ctx, 1) + if err != nil { + return nil, err + } + defer machine.inspects.Release(1) + + var fork RollupsMachine + + // Forks the machine. + machine.mutex.LLock() + fork, _, err = machine.RollupsMachine.Fork() + machine.mutex.Unlock() + if err != nil { + return nil, err + } + + // Sends the inspect-state request to the forked machine. + res, _, timedOut := runWithTimeout(ctx, machine.timeout, func() (*InspectResult, error) { + accepted, reports, err := fork.Inspect(query) + return &InspectResult{Accepted: accepted, Reports: reports, Err: err}, nil + }) + if timedOut { + res = &InspectResult{Err: ErrTimeLimitExceeded} + } + + return res, fork.Close() +} + +// ------------------------------------------------------------------------------------------------ + +func toInputStatus(accepted bool, err error) (status model.InputCompletionStatus, _ error) { + switch err { + case nil: + if accepted { + return model.InputStatusAccepted, nil + } else { + return model.InputStatusRejected, nil + } + case rollupsmachine.ErrException: + return model.InputStatusException, nil + case rollupsmachine.ErrHalted: + return model.InputStatusMachineHalted, nil + case rollupsmachine.ErrCycleLimitExceeded: + return model.InputStatusCycleLimitExceeded, nil + case rollupsmachine.ErrOutputsLimitExceeded: + panic("TODO") + case rollupsmachine.ErrCartesiMachine, + rollupsmachine.ErrProgress, + rollupsmachine.ErrSoftYield: + return status, err + default: + return status, err + } + + // ErrPayloadLengthLimitExceeded + // InputStatusPayloadLengthLimitExceeded +} + +// Unused. +func runWithTimeout[T any]( + ctx context.Context, + timeout time.Duration, + f func() (*T, error), +) (_ *T, _ error, timedOut bool) { + ctx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() + + success := make(chan *T, 1) + failure := make(chan error, 1) + go func() { + t, err := f() + if err != nil { + failure <- err + } else { + success <- t + } + }() + + select { + case <-ctx.Done(): + return nil, nil, true + case t := <-success: + return t, nil, false + case err := <-failure: + return nil, err, false + } +} diff --git a/internal/node/machine/nodemachine/pmutex/pmutex.go b/internal/node/machine/nodemachine/pmutex/pmutex.go new file mode 100644 index 000000000..b494c9c0a --- /dev/null +++ b/internal/node/machine/nodemachine/pmutex/pmutex.go @@ -0,0 +1,56 @@ +// (c) Cartesi and individual authors (see AUTHORS) +// SPDX-License-Identifier: Apache-2.0 (see LICENSE) + +package pmutex + +import ( + "sync" + "sync/atomic" +) + +// A PMutex is a mutual exclusion lock with priority capabilities. +// A call to HLock always acquires the mutex before LLock. +type PMutex struct { + // Main mutex. + mutex *sync.Mutex + + // Condition variable for the waiting low-priority threads. + waitingLow *sync.Cond + + // Quantity of high-priority threads waiting to acquire the lock. + waitingHigh *atomic.Int32 +} + +// New creates a new PMutex. +func New() *PMutex { + mutex := &sync.Mutex{} + return &PMutex{ + mutex: mutex, + waitingLow: sync.NewCond(mutex), + waitingHigh: &atomic.Int32{}, + } +} + +// HLock acquires the mutex for high-priority threads. +func (pmutex *PMutex) HLock() { + pmutex.waitingHigh.Add(1) + pmutex.mutex.Lock() + pmutex.waitingHigh.Add(-1) +} + +// LLock acquires the mutex for low-priority threads. +// (It waits until there are no high-priority threads trying to acquire the lock.) +func (pmutex *PMutex) LLock() { + pmutex.mutex.Lock() + for pmutex.waitingHigh.Load() != 0 { + // NOTE: a cond.Wait() releases the lock uppon being called + // and tries to acquire it after being awakened. + pmutex.waitingLow.Wait() + } +} + +// Unlock releases the mutex for both types of threads. +func (pmutex *PMutex) Unlock() { + pmutex.waitingLow.Broadcast() + pmutex.mutex.Unlock() +} diff --git a/internal/repository/advancer.go b/internal/repository/advancer.go new file mode 100644 index 000000000..0e70cfff5 --- /dev/null +++ b/internal/repository/advancer.go @@ -0,0 +1,191 @@ +// (c) Cartesi and individual authors (see AUTHORS) +// SPDX-License-Identifier: Apache-2.0 (see LICENSE) + +package repository + +import ( + "context" + "errors" + "fmt" + "strings" + + "github.com/cartesi/rollups-node/internal/node/machine/nodemachine" + . "github.com/cartesi/rollups-node/internal/node/model" + "github.com/jackc/pgx/v5" +) + +var ErrAdvancerRepository = errors.New("advancer repository error") + +type AdvancerRepository struct{ *Database } + +func (repository *AdvancerRepository) GetInputs( + ctx context.Context, + appAddresses []Address, +) (map[Address][]*Input, error) { + result := map[Address][]*Input{} + if len(appAddresses) == 0 { + return result, nil + } + + query := fmt.Sprintf(` + SELECT id, application_address, index, raw_data, block_number, status + FROM input + WHERE status = 'NONE' + AND application_address IN (%s) + ORDER BY index ASC, application_address + `, toIN(appAddresses)) // TODO: not sanitized + rows, err := repository.db.Query(ctx, query) + if err != nil { + return nil, fmt.Errorf("%w (failed querying inputs): %w", ErrAdvancerRepository, err) + } + + var input Input + scans := []any{ + &input.Id, + &input.AppAddress, + &input.Index, + &input.RawData, + &input.BlockNumber, + &input.CompletionStatus, + } + + _, err = pgx.ForEachRow(rows, scans, func() error { + input := input + if _, ok := result[input.AppAddress]; ok { + result[input.AppAddress] = append(result[input.AppAddress], &input) + } else { + result[input.AppAddress] = []*Input{&input} + } + return nil + }) + if err != nil { + return nil, fmt.Errorf("%w (failed reading input rows): %w", ErrAdvancerRepository, err) + } + + return result, nil +} + +func (repository *AdvancerRepository) StoreResults( + ctx context.Context, + input *Input, + res *nodemachine.AdvanceResult, +) error { + tx, err := repository.db.Begin(ctx) + if err != nil { + return errors.Join(ErrBeginTx, err) + } + + nextOutputIndex, err := repository.getNextOutputIndex(ctx, tx, input.AppAddress) + if err != nil { + return err + } + + err = repository.insertOutputs(ctx, tx, res.Outputs, input.Id, nextOutputIndex) + if err != nil { + return err + } + + err = repository.updateInput(ctx, tx, input.Id, res.Status, res.OutputsHash, res.MachineHash) + if err != nil { + return err + } + + err = tx.Commit(ctx) + if err != nil { + return errors.Join(ErrCommitTx, err, tx.Rollback(ctx)) + } + + return nil +} + +// ------------------------------------------------------------------------------------------------ + +func (_ *AdvancerRepository) getNextOutputIndex( + ctx context.Context, + tx pgx.Tx, + appAddress Address, +) (uint64, error) { + var nextOutputIndex uint64 + query := ` + SELECT COALESCE(MAX(output.index) + 1, 0) + FROM input INNER JOIN output ON input.id = output.input_id + WHERE input.status = 'ACCEPTED' + AND input.application_address = $1 + ` + err := tx.QueryRow(ctx, query, appAddress).Scan(&nextOutputIndex) + if err != nil { + err = fmt.Errorf("failed to get the next output index: %w", err) + return 0, errors.Join(err, tx.Rollback(ctx)) + } + return nextOutputIndex, nil +} + +func (_ *AdvancerRepository) insertOutputs( + ctx context.Context, + tx pgx.Tx, + outputs [][]byte, + inputId uint64, + nextOutputIndex uint64, +) error { + lenOutputs := int64(len(outputs)) + if lenOutputs < 1 { + return nil + } + + rows := [][]any{} + for i, output := range outputs { + rows = append(rows, []any{inputId, nextOutputIndex + uint64(i), output}) + } + + count, err := tx.CopyFrom( + context.Background(), + pgx.Identifier{"output"}, + []string{"input_id", "index", "raw_data"}, + pgx.CopyFromRows(rows), + ) + if err != nil { + return errors.Join(ErrCopyFrom, err, tx.Rollback(ctx)) + } + if lenOutputs != count { + err := fmt.Errorf("not all outputs were inserted (%d != %d)", lenOutputs, count) + return errors.Join(err, tx.Rollback(ctx)) + } + + return nil +} + +func (_ *AdvancerRepository) updateInput( + ctx context.Context, + tx pgx.Tx, + inputId uint64, + status InputCompletionStatus, + outputsHash Hash, + machineHash Hash, +) error { + query := ` + UPDATE input + SET (status, outputs_hash, machine_hash) = (@status, @outputsHash, @machineHash) + WHERE id = @id + ` + args := pgx.NamedArgs{ + "status": status, + "outputsHash": outputsHash, + "machineHash": machineHash, + "id": inputId, + } + _, err := tx.Exec(ctx, query, args) + if err != nil { + return errors.Join(ErrUpdateRow, err, tx.Rollback(ctx)) + } + return nil +} + +// ------------------------------------------------------------------------------------------------ + +func toIN[T fmt.Stringer](a []T) string { + s := []string{} + for _, x := range a { + s = append(s, fmt.Sprintf("'\\x%s'", x.String()[2:])) + } + return fmt.Sprintf("(%s)", strings.Join(s, ", ")) +} diff --git a/internal/repository/base.go b/internal/repository/base.go index 9bfc6dac4..ed92ff81d 100644 --- a/internal/repository/base.go +++ b/internal/repository/base.go @@ -19,7 +19,14 @@ type Database struct { db *pgxpool.Pool } -var ErrInsertRow = errors.New("unable to insert row") +var ( + ErrInsertRow = errors.New("unable to insert row") + ErrUpdateRow = errors.New("unable to update row") + ErrCopyFrom = errors.New("unable to COPY FROM") + + ErrBeginTx = errors.New("unable to begin transaction") + ErrCommitTx = errors.New("unable to commit transaction") +) func Connect( ctx context.Context, @@ -141,8 +148,10 @@ func (pg *Database) InsertInput( @blockNumber, @machineHash, @outputsHash, - @applicationAddress)` - + @applicationAddress) + RETURNING + id + ` args := pgx.NamedArgs{ "index": input.Index, "status": input.CompletionStatus, @@ -153,7 +162,7 @@ func (pg *Database) InsertInput( "applicationAddress": input.AppAddress, } - _, err := pg.db.Exec(ctx, query, args) + err := pg.db.QueryRow(ctx, query, args).Scan(&input.Id) if err != nil { return fmt.Errorf("%w: %w", ErrInsertRow, err) } diff --git a/internal/repository/migrations/000001_create_application_input_claim_output_report_nodeconfig.up.sql b/internal/repository/migrations/000001_create_application_input_claim_output_report_nodeconfig.up.sql index 849c0e786..ec69aac12 100644 --- a/internal/repository/migrations/000001_create_application_input_claim_output_report_nodeconfig.up.sql +++ b/internal/repository/migrations/000001_create_application_input_claim_output_report_nodeconfig.up.sql @@ -64,8 +64,6 @@ CREATE TABLE "output" CONSTRAINT "output_input_id_fkey" FOREIGN KEY ("input_id") REFERENCES "input"("id") ); -CREATE UNIQUE INDEX "output_idx" ON "output"("index"); - CREATE TABLE "report" ( "id" BIGSERIAL, diff --git a/internal/repository/schemamanager.go b/internal/repository/schemamanager.go index c0b2d2242..397c8fe75 100644 --- a/internal/repository/schemamanager.go +++ b/internal/repository/schemamanager.go @@ -79,6 +79,10 @@ func (s *SchemaManager) Upgrade() error { return nil } +func (s *SchemaManager) DeleteAll() error { + return s.migrate.Down() +} + func (s *SchemaManager) Close() { source, db := s.migrate.Close() if source != nil { diff --git a/test/advancer/advancer_test.go b/test/advancer/advancer_test.go new file mode 100644 index 000000000..649eb8ca1 --- /dev/null +++ b/test/advancer/advancer_test.go @@ -0,0 +1,216 @@ +// (c) Cartesi and individual authors (see AUTHORS) +// SPDX-License-Identifier: Apache-2.0 (see LICENSE) + +package advancer + +import ( + "context" + "log" + "log/slog" + "os" + "sync" + "testing" + "time" + + "github.com/cartesi/rollups-node/internal/node/machine/advancer" + "github.com/cartesi/rollups-node/internal/node/machine/nodemachine" + "github.com/cartesi/rollups-node/internal/node/model" + "github.com/cartesi/rollups-node/internal/repository" + "github.com/cartesi/rollups-node/pkg/emulator" + "github.com/cartesi/rollups-node/pkg/rollupsmachine" + "github.com/cartesi/rollups-node/test/snapshot" + "github.com/ethereum/go-ethereum/common" + "github.com/stretchr/testify/require" + "github.com/testcontainers/testcontainers-go" + "github.com/testcontainers/testcontainers-go/modules/postgres" + "github.com/testcontainers/testcontainers-go/wait" +) + +func init() { + log.SetFlags(log.Ltime) + slog.SetLogLoggerLevel(slog.LevelDebug) +} + +var appAddress model.Address + +func TestAdvancer(t *testing.T) { + require := require.New(t) + + // Creates the snapshot. + script := "ioctl-echo-loop --vouchers=1 --notices=3 --reports=5 --verbose=1" + snapshot, err := snapshot.FromScript(script, uint64(1_000_000_000)) + require.Nil(err) + defer func() { require.Nil(snapshot.Close()) }() + + // Starts the server. + verbosity := rollupsmachine.ServerVerbosityInfo + address, err := rollupsmachine.StartServer(verbosity, 0, os.Stdout, os.Stderr) + require.Nil(err) + + // Loads the rollupsmachine. + config := &emulator.MachineRuntimeConfig{} + rollupsMachine, err := rollupsmachine.Load(snapshot.Dir, address, config) + require.Nil(err) + require.NotNil(rollupsMachine) + + // Wraps the rollupsmachine with nodemachine. + nodeMachine := nodemachine.New(rollupsMachine, time.Minute, 10) + require.Nil(err) + require.NotNil(nodeMachine) + defer func() { require.Nil(nodeMachine.Close()) }() + + // Creates the machine pool. + appAddress = common.HexToAddress("deadbeef") + machine := new(sync.Map) + machine.Store(appAddress, nodeMachine) + + // Creates the background context. + ctx := context.Background() + + // // Create the database container. + // databaseContainer, err := newDatabaseContainer(ctx) + // require.Nil(err) + // defer func() { require.Nil(databaseContainer.Terminate(ctx)) }() + + // Setups the database. + database, err := newLocalDatabase(ctx) + require.Nil(err) + err = populateDatabase(ctx, database) + require.Nil(err, "%v", err) + defer database.Close() + + // Creates the advancer's repository. + repository := &repository.AdvancerRepository{Database: database} + + // Creates the advancer. + advancer, err := advancer.New(machine, repository, 10*time.Second) + require.Nil(err) + require.NotNil(advancer) + + // Starts the advancer. + err = advancer.Start(ctx) + require.Nil(err, "%v", err) +} + +func newDatabaseContainer(ctx context.Context) (*postgres.PostgresContainer, error) { + dbName := "cartesinode" + dbUser := "admin" + dbPassword := "password" + + // Start the postgres container + container, err := postgres.Run( + ctx, + "postgres:16-alpine", + postgres.WithDatabase(dbName), + postgres.WithUsername(dbUser), + postgres.WithPassword(dbPassword), + testcontainers.WithWaitStrategy( + wait.ForLog("database system is ready to accept connections"). + WithOccurrence(2). + WithStartupTimeout(10*time.Second)), + ) + + return container, err +} + +func newLocalDatabase(ctx context.Context) (*repository.Database, error) { + endpoint := "postgres://renan:renan@localhost:5432/renan?sslmode=disable" + + schemaManager, err := repository.NewSchemaManager(endpoint) + if err != nil { + return nil, err + } + + err = schemaManager.DeleteAll() + if err != nil { + return nil, err + } + + err = schemaManager.Upgrade() + if err != nil { + return nil, err + } + + database, err := repository.Connect(ctx, endpoint) + if err != nil { + return nil, err + } + + return database, nil +} + +func newDatabase( + ctx context.Context, + container *postgres.PostgresContainer, +) (*repository.Database, error) { + endpoint, err := container.ConnectionString(ctx, "sslmode=disable") + if err != nil { + return nil, err + } + + schemaManager, err := repository.NewSchemaManager(endpoint) + if err != nil { + return nil, err + } + + err = schemaManager.Upgrade() + if err != nil { + return nil, err + } + + database, err := repository.Connect(ctx, endpoint) + if err != nil { + return nil, err + } + + return database, nil +} + +func populateDatabase(ctx context.Context, database *repository.Database) (err error) { + application := &model.Application{ + ContractAddress: appAddress, + TemplateHash: [32]byte{}, + SnapshotURI: "invalid", + LastProcessedBlock: 0, + EpochLength: 0, + Status: "RUNNING", + } + err = database.InsertApplication(ctx, application) + if err != nil { + return + } + + inputs := []*model.Input{{ + Index: 0, + CompletionStatus: model.InputStatusAccepted, + RawData: []byte("first input"), + BlockNumber: 0, + AppAddress: appAddress, + }, { + Index: 1, + CompletionStatus: model.InputStatusNone, + RawData: []byte("second input"), + BlockNumber: 1, + AppAddress: appAddress, + }, { + Index: 2, + CompletionStatus: model.InputStatusNone, + RawData: []byte("third input"), + BlockNumber: 2, + AppAddress: appAddress, + }} + + for _, input := range inputs { + input.RawData, err = rollupsmachine.Input{Data: input.RawData}.Encode() + if err != nil { + return + } + + err = database.InsertInput(ctx, input) + if err != nil { + return + } + } + + return +}