Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
renan061 committed Mar 15, 2024
1 parent 3065f3b commit f9f5da8
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 110 deletions.
4 changes: 4 additions & 0 deletions pkg/libcmt/gollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package libcmt

import "log/slog"

type OutputEmitter interface {
SendVoucher(address [20]byte, value []byte, data []byte) error
SendNotice(data []byte) error
Expand Down Expand Up @@ -46,12 +48,14 @@ func (gollup *Gollup) Run() error {

switch finish.NextRequestType {
case AdvanceStateRequest:
slog.Info("Received advance-state request nugget.")
input, err := gollup.rollup.ReadAdvanceState()
if err != nil {
return err
}
accept = gollup.advanceHandler(gollup, input)
case InspectStateRequest:
slog.Info("Received inspect-state request.")
query, err := gollup.rollup.ReadInspectState()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/libcmt/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (rollup *Rollup) SaveMerkle(path string) error {
func toError(errno C.int) error {
if errno < 0 {
s := C.strerror(-errno)
// TODO defer C.free(unsafe.Pointer(cs))
defer C.free(unsafe.Pointer(s))
return fmt.Errorf("%s (%d)", C.GoString(s), errno)
} else {
return nil
Expand Down
1 change: 1 addition & 0 deletions pkg/machine-runner/tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*/snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,31 @@
package main

import (
"fmt"
"log/slog"

"github.com/cartesi/rollups-node/pkg/libcmt"
)

func advance(emitter libcmt.OutputEmitter, input *libcmt.Input) bool {
return false
emitter.SendNotice(input.Data)
return true
}

func inspect(emitter libcmt.ReportEmitter, query *libcmt.Query) bool {
s := fmt.Sprintf("report: %s", query.Data)
emitter.SendReport([]byte(s))
emitter.SendReport(query.Data)
return true
}

func main() {
slog.Info("=============== Start app.")
defer slog.Info("=============== End app.")
gollup, err := libcmt.NewGollup(advance, inspect)
if err != nil {
panic(err)
}
defer gollup.Destroy()
gollup.Run()
err = gollup.Run()
if err != nil {
panic(err)
}
}
1 change: 0 additions & 1 deletion pkg/machine-runner/tests/advance/.gitignore

This file was deleted.

29 changes: 0 additions & 29 deletions pkg/machine-runner/tests/advance/main.go

This file was deleted.

13 changes: 11 additions & 2 deletions pkg/machine-runner/tests/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (

var RiscVCC = "riscv64-linux-gnu-gcc-12"

// CreateSnapshot creates a cartesi machine snapshot
// CreateGollupSnapshot creates a cartesi machine snapshot
// with a block size of 1024*kblock
// from the Go program at name.
func CreateSnapshot(name string, kblocks uint64) {
func CreateGollupSnapshot(name string, kblocks uint64) {
slog.Info("----- Creating Gollup snapshot", "name", name)

folder := name + "/"
main := folder + "main.go"
binary := "temp/" + name
Expand Down Expand Up @@ -46,6 +48,13 @@ func CreateSnapshot(name string, kblocks uint64) {
"--", "CMT_DEBUG=yes", fmt.Sprintf("/mnt/%s/%s", name, name)))
}

func CreateSimpleSnapshot(name, bash string) {
slog.Info("----- Creating simple snapshot", "name", name)
run(exec.Command("rm", "-rf", name),
exec.Command("mkdir", "-p", name),
exec.Command("cartesi-machine", fmt.Sprintf("--store=%s", name+"/snapshot"), "--", bash))
}

func run(cmds ...*exec.Cmd) {
for _, cmd := range cmds {
slog.Info(cmd.String())
Expand Down
1 change: 0 additions & 1 deletion pkg/machine-runner/tests/input-exception/.gitignore

This file was deleted.

16 changes: 0 additions & 16 deletions pkg/machine-runner/tests/input-exception/main.go

This file was deleted.

1 change: 0 additions & 1 deletion pkg/machine-runner/tests/input-rejected/.gitignore

This file was deleted.

15 changes: 0 additions & 15 deletions pkg/machine-runner/tests/input-rejected/main.go

This file was deleted.

62 changes: 41 additions & 21 deletions pkg/machine-runner/tests/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package tests

import (
"fmt"
"os"
"testing"

Expand All @@ -14,16 +15,12 @@ import (
)

func init() {
snapshots := []string{
"advance",
"input-exception",
"input-rejected",
"not-at-manual-yield",
"report",
}
for _, snapshot := range snapshots {
CreateSnapshot(snapshot, 2)
}
CreateSimpleSnapshot("rollup-accept", "rollup accept")
CreateSimpleSnapshot("rollup-reject", "rollup reject")
CreateSimpleSnapshot("rollup-notice", payload("Hari Seldon")+" | rollup notice")
CreateSimpleSnapshot("rollup-exception", payload("Paul Atreides")+" | rollup exception")

CreateGollupSnapshot("advance-inspect", 4)
}

// ------------------------------------------------------------------------------------------------
Expand All @@ -47,42 +44,42 @@ func (suite *LoadSuite) TearDownTest() {
}

func (suite *LoadSuite) TestLoad() {
m, err := machine.Load(suite.address, "advance/snapshot", suite.config)
m, err := machine.Load(suite.address, "rollup-accept/snapshot", suite.config)
suite.Nil(err)
suite.NotNil(m)
}

func (suite *LoadSuite) TestInvalidAddress() {
// NOTE: This test does not require an initialized server; the setup is incidental.
m, err := machine.Load("invalid address", "advance/snapshot", suite.config)
m, err := machine.Load("invalid address", "rollup-accept/snapshot", suite.config)
suite.NotNil(err)
suite.Nil(m)
suite.Equal(binding.ErrorRuntime, err.(binding.Error).Code)
}

func (suite *LoadSuite) TestNonExistingSnapshot() {
m, err := machine.Load(suite.address, "non-existing snapshot", suite.config)
m, err := machine.Load(suite.address, "non-existing/snapshot", suite.config)
suite.NotNil(err)
suite.Nil(m)
suite.Equal(binding.ErrorRuntime, err.(binding.Error).Code)
}

func (suite *LoadSuite) TestNotPrimedNotAtManualYield() {
m, err := machine.Load(suite.address, "not-at-manual-yield/snapshot", suite.config)
m, err := machine.Load(suite.address, "rollup-notice/snapshot", suite.config)
suite.NotNil(err)
suite.ErrorIs(err, machine.ErrNotAtManualYield)
suite.Nil(m)
}

func (suite *LoadSuite) TestNotPrimedInputRejected() {
m, err := machine.Load(suite.address, "input-rejected/snapshot", suite.config)
m, err := machine.Load(suite.address, "rollup-reject/snapshot", suite.config)
suite.NotNil(err)
suite.ErrorIs(err, machine.ErrLastInputWasRejected)
suite.Nil(m)
}

func (suite *LoadSuite) TestNotPrimedInputException() {
m, err := machine.Load(suite.address, "input-exception/snapshot", suite.config)
m, err := machine.Load(suite.address, "rollup-exception/snapshot", suite.config)
suite.NotNil(err)
suite.ErrorIs(err, machine.ErrLastInputYieldedAnException)
suite.Nil(m)
Expand Down Expand Up @@ -138,34 +135,47 @@ func (suite *ForkSuite) TearDownTest() {

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

/*
type AdvanceInspectSuite struct {
suite.Suite
machine *machine.Machine
}

func (suite *AdvanceInspectSuite) SetupTest() {
address, err := machine.StartServer(binary, machine.ServerLogLevelInfo, 0, os.Stdout, os.Stderr)
address, err := machine.StartServer(machine.ServerLogLevelInfo, 0, os.Stdout, os.Stderr)
suite.Nil(err)

suite.machine, err = machine.Load(address, snapshotAdvanceInspect, binding.RuntimeConfig{})
machine, err := machine.Load(address, "advance-inspect/snapshot", binding.RuntimeConfig{})
suite.Nil(err)
suite.NotNil(suite.machine)
suite.NotNil(machine)

suite.machine = machine
}

func (suite *AdvanceInspectSuite) TearDownTest() {
err := suite.machine.Destroy()
suite.Nil(err)
suite.machine = nil
}

func (suite *AdvanceInspectSuite) TestSingleOutput() {
input := machine.Input{Data: []byte("renan")}
fmt.Println("Start")
defer fmt.Println("End")

// s := `Any fool can tell a crisis when it arrives.
// The real service to the state is to detect it in embryo.
// -- Isaac Asimov, Foundation`
s := "nugget"
input := machine.Input{Data: []byte(s)}
request, err := input.Encode()
suite.Nil(err)

fmt.Println("Input", input, s)

outputs, err := suite.machine.Advance(request)
suite.Nil(err)
suite.Len(outputs, 1)

fmt.Println("Output", string(outputs[0]))
}

// Advance/Inspect ok (single output).
Expand All @@ -175,6 +185,10 @@ func (suite *AdvanceInspectSuite) TestSingleOutput() {
// Advance/Inspect input rejected.
// Advance/Inspect input exception.

func TestAdvanceInspect(t *testing.T) {
suite.Run(t, new(AdvanceInspectSuite))
}

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

/*
Expand Down Expand Up @@ -291,3 +305,9 @@ func inspect(t *testing.T, machine *Machine, data string, counter, length int) {
}
}
*/

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

func payload(s string) string {
return fmt.Sprintf("echo '{ \"payload\": \"%s\" }'", s)
}
1 change: 0 additions & 1 deletion pkg/machine-runner/tests/not-at-manual-yield/.gitignore

This file was deleted.

16 changes: 0 additions & 16 deletions pkg/machine-runner/tests/not-at-manual-yield/main.go

This file was deleted.

1 change: 0 additions & 1 deletion pkg/machine-runner/tests/report/.gitignore

This file was deleted.

0 comments on commit f9f5da8

Please sign in to comment.