diff --git a/pkg/libcmt/gollup.go b/pkg/libcmt/gollup.go index 650e5cb27..4f0dbf89f 100644 --- a/pkg/libcmt/gollup.go +++ b/pkg/libcmt/gollup.go @@ -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 @@ -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 diff --git a/pkg/libcmt/rollup.go b/pkg/libcmt/rollup.go index 90aa732c4..024e863b5 100644 --- a/pkg/libcmt/rollup.go +++ b/pkg/libcmt/rollup.go @@ -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 diff --git a/pkg/machine-runner/tests/.gitignore b/pkg/machine-runner/tests/.gitignore new file mode 100644 index 000000000..5d03801c8 --- /dev/null +++ b/pkg/machine-runner/tests/.gitignore @@ -0,0 +1 @@ +*/snapshot diff --git a/pkg/machine-runner/tests/report/main.go b/pkg/machine-runner/tests/advance-inspect/main.go similarity index 67% rename from pkg/machine-runner/tests/report/main.go rename to pkg/machine-runner/tests/advance-inspect/main.go index 16e851fcb..fc7e9fe17 100644 --- a/pkg/machine-runner/tests/report/main.go +++ b/pkg/machine-runner/tests/advance-inspect/main.go @@ -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) + } } diff --git a/pkg/machine-runner/tests/advance/.gitignore b/pkg/machine-runner/tests/advance/.gitignore deleted file mode 100644 index 8ab67f2e7..000000000 --- a/pkg/machine-runner/tests/advance/.gitignore +++ /dev/null @@ -1 +0,0 @@ -snapshot diff --git a/pkg/machine-runner/tests/advance/main.go b/pkg/machine-runner/tests/advance/main.go deleted file mode 100644 index 938c4b685..000000000 --- a/pkg/machine-runner/tests/advance/main.go +++ /dev/null @@ -1,29 +0,0 @@ -// (c) Cartesi and individual authors (see AUTHORS) -// SPDX-License-Identifier: Apache-2.0 (see LICENSE) - -package main - -import ( - "fmt" - - "github.com/cartesi/rollups-node/pkg/libcmt" -) - -func advance(emitter libcmt.OutputEmitter, input *libcmt.Input) bool { - s := fmt.Sprintf("advance: %s", input.Data) - emitter.SendNotice([]byte(s)) - return true -} - -func inspect(emitter libcmt.ReportEmitter, query *libcmt.Query) bool { - return false -} - -func main() { - gollup, err := libcmt.NewGollup(advance, inspect) - if err != nil { - panic(err) - } - defer gollup.Destroy() - gollup.Run() -} diff --git a/pkg/machine-runner/tests/commands.go b/pkg/machine-runner/tests/commands.go index c61d28b8c..5ae9f17cc 100644 --- a/pkg/machine-runner/tests/commands.go +++ b/pkg/machine-runner/tests/commands.go @@ -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 @@ -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()) diff --git a/pkg/machine-runner/tests/input-exception/.gitignore b/pkg/machine-runner/tests/input-exception/.gitignore deleted file mode 100644 index 8ab67f2e7..000000000 --- a/pkg/machine-runner/tests/input-exception/.gitignore +++ /dev/null @@ -1 +0,0 @@ -snapshot diff --git a/pkg/machine-runner/tests/input-exception/main.go b/pkg/machine-runner/tests/input-exception/main.go deleted file mode 100644 index 8f55b9b6e..000000000 --- a/pkg/machine-runner/tests/input-exception/main.go +++ /dev/null @@ -1,16 +0,0 @@ -// (c) Cartesi and individual authors (see AUTHORS) -// SPDX-License-Identifier: Apache-2.0 (see LICENSE) - -package main - -import "github.com/cartesi/rollups-node/pkg/libcmt" - -func main() { - rollup, err := libcmt.NewRollup() - if err != nil { - panic(err) - } - defer rollup.Destroy() - quote := "There is no escape--we pay for the violence of our ancestors. -- Frank Herbert, Dune" - rollup.EmitException([]byte(quote)) -} diff --git a/pkg/machine-runner/tests/input-rejected/.gitignore b/pkg/machine-runner/tests/input-rejected/.gitignore deleted file mode 100644 index 8ab67f2e7..000000000 --- a/pkg/machine-runner/tests/input-rejected/.gitignore +++ /dev/null @@ -1 +0,0 @@ -snapshot diff --git a/pkg/machine-runner/tests/input-rejected/main.go b/pkg/machine-runner/tests/input-rejected/main.go deleted file mode 100644 index a3c5ff124..000000000 --- a/pkg/machine-runner/tests/input-rejected/main.go +++ /dev/null @@ -1,15 +0,0 @@ -// (c) Cartesi and individual authors (see AUTHORS) -// SPDX-License-Identifier: Apache-2.0 (see LICENSE) - -package main - -import "github.com/cartesi/rollups-node/pkg/libcmt" - -func main() { - rollup, err := libcmt.NewRollup() - if err != nil { - panic(err) - } - defer rollup.Destroy() - rollup.Finish(false) -} diff --git a/pkg/machine-runner/tests/machine_test.go b/pkg/machine-runner/tests/machine_test.go index d954de272..806e1dfe0 100644 --- a/pkg/machine-runner/tests/machine_test.go +++ b/pkg/machine-runner/tests/machine_test.go @@ -4,6 +4,7 @@ package tests import ( + "fmt" "os" "testing" @@ -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) } // ------------------------------------------------------------------------------------------------ @@ -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) @@ -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). @@ -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)) +} + // ------------------------------------------------------------------------------------------------ /* @@ -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) +} diff --git a/pkg/machine-runner/tests/not-at-manual-yield/.gitignore b/pkg/machine-runner/tests/not-at-manual-yield/.gitignore deleted file mode 100644 index 8ab67f2e7..000000000 --- a/pkg/machine-runner/tests/not-at-manual-yield/.gitignore +++ /dev/null @@ -1 +0,0 @@ -snapshot diff --git a/pkg/machine-runner/tests/not-at-manual-yield/main.go b/pkg/machine-runner/tests/not-at-manual-yield/main.go deleted file mode 100644 index 163dfdb05..000000000 --- a/pkg/machine-runner/tests/not-at-manual-yield/main.go +++ /dev/null @@ -1,16 +0,0 @@ -// (c) Cartesi and individual authors (see AUTHORS) -// SPDX-License-Identifier: Apache-2.0 (see LICENSE) - -package main - -import "github.com/cartesi/rollups-node/pkg/libcmt" - -func main() { - rollup, err := libcmt.NewRollup() - if err != nil { - panic(err) - } - defer rollup.Destroy() - quote := "I must not fear. Fear is the mind-killer. -- Frank Herbert, Dune" - rollup.EmitNotice([]byte(quote)) -} diff --git a/pkg/machine-runner/tests/report/.gitignore b/pkg/machine-runner/tests/report/.gitignore deleted file mode 100644 index 8ab67f2e7..000000000 --- a/pkg/machine-runner/tests/report/.gitignore +++ /dev/null @@ -1 +0,0 @@ -snapshot