Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
renan061 committed Jul 19, 2024
1 parent 484789f commit 06bcd9c
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 127 deletions.
16 changes: 7 additions & 9 deletions internal/node/machineadvancer/advancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
package machineadvancer

import (
"context"
"errors"
"fmt"
"time"

. "github.com/cartesi/rollups-node/internal/node/model"
"github.com/cartesi/rollups-node/internal/node/model"
"github.com/cartesi/rollups-node/internal/node/nodemachine"
)

type Machine interface {
Advance(input []byte) (*nodemachine.AdvanceResponse, error)
Advance(_ context.Context, input []byte) (*nodemachine.AdvanceResponse, error)
}

type MachineAdvancer struct {
machines map[Address]Machine
machines map[model.Address]Machine
repository Repository
ticker *time.Ticker
}
Expand All @@ -32,7 +33,7 @@ var (

// Duration must be greater than 0.
func New(
machines map[Address]Machine,
machines map[model.Address]Machine,
repository Repository,
pollingInterval time.Duration,
) (*MachineAdvancer, error) {
Expand Down Expand Up @@ -69,15 +70,12 @@ func (advancer *MachineAdvancer) Start() error {

// Processes all inputs sequentially.
for _, input := range inputs {
res, err := machine.Advance(input.Payload)
res, err := machine.Advance(context.Background(), input.RawData)
if err != nil {
return err
}
inputStatus, err := inputStatusFromResponse(res)
fmt.Println(inputStatus)

err = advancer.repository.Store(
input, res.Outputs, res.Reports, res.OutputsHash, res.MachineHash, res.Status)
err = advancer.repository.Store(input, res)
if err != nil {
return err
}
Expand Down
67 changes: 29 additions & 38 deletions internal/node/machineadvancer/advancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
package machineadvancer

import (
"context"
crand "crypto/rand"
"errors"
mrand "math/rand"
"testing"
"time"

. "github.com/cartesi/rollups-node/internal/node/model"
"github.com/cartesi/rollups-node/internal/node/model"
"github.com/cartesi/rollups-node/internal/node/nodemachine"

"github.com/cartesi/rollups-node/pkg/rollupsmachine"
"github.com/stretchr/testify/suite"
)

Expand All @@ -26,7 +26,7 @@ type MachineAdvancerSuite struct{ suite.Suite }
func (s *MachineAdvancerSuite) TestNew() {
s.Run("Ok", func() {
require := s.Require()
machines := map[Address]Machine{randomAddress(): newMockMachine()}
machines := map[model.Address]Machine{randomAddress(): newMockMachine()}
repository := newMockRepository()
machineAdvancer, err := New(machines, repository, time.Nanosecond)
require.NotNil(machineAdvancer)
Expand All @@ -43,15 +43,15 @@ func (s *MachineAdvancerSuite) TestNew() {

s.Run("InvalidRepository", func() {
require := s.Require()
machines := map[Address]Machine{randomAddress(): newMockMachine()}
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[Address]Machine{randomAddress(): newMockMachine()}
machines := map[model.Address]Machine{randomAddress(): newMockMachine()}
repository := newMockRepository()
machineAdvancer, err := New(machines, repository, time.Duration(0))
require.Nil(machineAdvancer)
Expand All @@ -67,16 +67,16 @@ func (s *MachineAdvancerSuite) TestStart() {

type StartSuite struct {
suite.Suite
machines map[Address]Machine
machines map[model.Address]Machine
repository *MockRepository
}

func (s *StartSuite) SetupTest() {
s.machines = map[Address]Machine{}
s.machines = map[model.Address]Machine{}
s.repository = newMockRepository()
}

// TODO: this test is absurdly basic. We need more tests.
// NOTE: This test is very basic! We need more tests!
func (s *StartSuite) TestBasic() {
require := s.Require()

Expand All @@ -87,7 +87,7 @@ func (s *StartSuite) TestBasic() {
machine.add(advanceResponse, nil)
s.machines[appAddress] = machine

s.repository.add(map[Address][]MachineInput{appAddress: randomInputs(1)}, nil, nil)
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)
Expand Down Expand Up @@ -121,7 +121,10 @@ func (m *MockMachine) add(result *nodemachine.AdvanceResponse, err error) {
m.errors = append(m.errors, err)
}

func (m *MockMachine) Advance(input []byte) (*nodemachine.AdvanceResponse, error) {
func (m *MockMachine) Advance(
_ context.Context,
input []byte,
) (*nodemachine.AdvanceResponse, error) {
result, err := m.results[m.index], m.errors[m.index]
m.index += 1
return result, err
Expand All @@ -131,7 +134,7 @@ func (m *MockMachine) Advance(input []byte) (*nodemachine.AdvanceResponse, error

type MockRepository struct {
getInputsIndex uint8
getInputsResults []map[Address][]MachineInput
getInputsResults []map[model.Address][]model.Input
getInputsErrors []error

storeIndex uint8
Expand All @@ -142,7 +145,7 @@ type MockRepository struct {
func newMockRepository() *MockRepository {
return &MockRepository{
getInputsIndex: 0,
getInputsResults: []map[Address][]MachineInput{},
getInputsResults: []map[model.Address][]model.Input{},
getInputsErrors: []error{},
storeIndex: 0,
storeErrors: []error{},
Expand All @@ -151,7 +154,7 @@ func newMockRepository() *MockRepository {
}

func (r *MockRepository) add(
getInputsResult map[Address][]MachineInput,
getInputsResult map[model.Address][]model.Input,
getInputsError error,
storeError error,
) {
Expand All @@ -162,7 +165,9 @@ func (r *MockRepository) add(

var testFinished = errors.New("test finished")

func (r *MockRepository) GetInputs(appAddresses []Address) (map[Address][]MachineInput, error) {
func (r *MockRepository) GetInputs(
appAddresses []model.Address,
) (map[model.Address][]model.Input, error) {
if int(r.getInputsIndex) == len(r.getInputsResults) {
return nil, testFinished
}
Expand All @@ -171,45 +176,31 @@ func (r *MockRepository) GetInputs(appAddresses []Address) (map[Address][]Machin
return result, err
}

func (r *MockRepository) Store(
input MachineInput,
outputs []MachineOutput,
reports []MachineReport,
outputsHash Hash,
machineHash Hash,
status rollupsmachine.Status) error {

func (r *MockRepository) Store(input model.Input, res *nodemachine.AdvanceResponse) error {
err := r.storeErrors[r.storeIndex]
r.storeIndex += 1
r.stored = append(r.stored, &nodemachine.AdvanceResponse{
Outputs: outputs,
Reports: reports,
OutputsHash: outputsHash,
MachineHash: machineHash,
Status: status,
})

r.stored = append(r.stored, res)
return err
}

// ------------------------------------------------------------------------------------------------

func randomAddress() Address {
func randomAddress() model.Address {
address := make([]byte, 20)
_, err := crand.Read(address)
if err != nil {
panic(err)
}
return Address(address)
return model.Address(address)
}

func randomHash() Hash {
func randomHash() model.Hash {
hash := make([]byte, 32)
_, err := crand.Read(hash)
if err != nil {
panic(err)
}
return Hash(hash)
return model.Hash(hash)
}

func randomBytes() []byte {
Expand All @@ -231,21 +222,21 @@ func randomSliceOfBytes() [][]byte {
return slice
}

func randomInputs(size int) []MachineInput {
slice := make([]MachineInput, size)
func randomInputs(size int) []model.Input {
slice := make([]model.Input, size)
for i := 0; i < size; i++ {
slice[i] = MachineInput{ID: uint64(i), Payload: randomBytes()}
slice[i] = model.Input{Id: uint64(i), RawData: randomBytes()}
}
return slice

}

func randomAdvanceResponse() *nodemachine.AdvanceResponse {
return &nodemachine.AdvanceResponse{
Status: model.InputStatusAccepted,
Outputs: randomSliceOfBytes(),
Reports: randomSliceOfBytes(),
OutputsHash: randomHash(),
MachineHash: randomHash(),
Status: rollupsmachine.RequestAccepted,
}
}
38 changes: 4 additions & 34 deletions internal/node/machineadvancer/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,13 @@
package machineadvancer

import (
"errors"

. "github.com/cartesi/rollups-node/internal/node/model"
"github.com/cartesi/rollups-node/internal/node/model"
"github.com/cartesi/rollups-node/internal/node/nodemachine"
"github.com/cartesi/rollups-node/pkg/rollupsmachine"
)

type Repository interface {
GetInputs(appAddresses []Address) (map[Address][]MachineInput, error)

Store(input MachineInput,
outputs []MachineOutput,
reports []MachineReport,
outputsHash Hash,
machineHash Hash,
status rollupsmachine.Status) error
}
// Only needs Id and RawData fields from model.Input.
GetInputs(appAddresses []model.Address) (map[model.Address][]model.Input, error)

func inputStatusFromResponse(res *nodemachine.AdvanceResponse) (InputCompletionStatus, error) {
switch res.Status {
case rollupsmachine.RequestAccepted:
return InputStatusAccepted, nil
case rollupsmachine.RequestRejected:
return InputStatusRejected, nil
case rollupsmachine.RequestException:
return InputStatusException, nil
case rollupsmachine.MachineHalted:
return InputStatusMachineHalted, nil
case rollupsmachine.CycleLimitExceeded:
return InputStatusCycleLimitExceeded, nil
case rollupsmachine.TimeLimitExceeded:
return InputStatusTimeLimitExceeded, nil
case rollupsmachine.PayloadLengthLimitExceeded:
return InputStatusPayloadLengthLimitExceeded, nil
case rollupsmachine.OutputsLimitExceeded:
return "TODO", errors.New("TODO")
default:
return "TODO", errors.New("TODO")
}
Store(model.Input, *nodemachine.AdvanceResponse) error
}
14 changes: 0 additions & 14 deletions internal/node/model/machine.go

This file was deleted.

Loading

0 comments on commit 06bcd9c

Please sign in to comment.