Skip to content

Commit

Permalink
light weight baby
Browse files Browse the repository at this point in the history
  • Loading branch information
renan061 committed Mar 15, 2024
1 parent e23683b commit adca281
Show file tree
Hide file tree
Showing 17 changed files with 103 additions and 86 deletions.
14 changes: 0 additions & 14 deletions pkg/machine-runner/machine/Makefile

This file was deleted.

6 changes: 3 additions & 3 deletions pkg/machine-runner/machine/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ var (
ErrYieldedSoftly = errors.New("machine yielded softly")

// isPrimed() errors
ErrNotAtManualYield = errors.New("not at manual yield")
ErrInputRejected = errors.New("last input was rejected")
ErrInputException = errors.New("last input yielded an exception")
ErrNotAtManualYield = errors.New("not at manual yield")
ErrLastInputWasRejected = errors.New("last input was rejected")
ErrLastInputYieldedAnException = errors.New("last input yielded an exception")

// Destroy() errors
ErrRemoteShutdown = errors.New("could not shut down the machine")
Expand Down
4 changes: 2 additions & 2 deletions pkg/machine-runner/machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ func (machine Machine) lastInputWasAccepted() error {
case binding.YieldReasonRxAccepted:
return nil
case binding.YieldReasonRxRejected:
return ErrInputRejected
return ErrLastInputWasRejected
case binding.YieldReasonTxException:
return ErrInputException
return ErrLastInputYieldedAnException
default:
panic(unreachable)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/machine-runner/machine/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const (
//
// It returns the server's address.
func StartServer(
binary string,
verbosity ServerLogLevel,
port uint32,
stdout io.Writer,
Expand All @@ -52,7 +51,7 @@ func StartServer(
}

// Creates the command.
cmd := exec.Command(binary, args...)
cmd := exec.Command("jsonrpc-remote-cartesi-machine", args...)
slog.Info(cmd.String())

// Redirects stdout and stderr.
Expand Down
14 changes: 0 additions & 14 deletions pkg/machine-runner/machine/tests/cmd_test.go

This file was deleted.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package snapshots
package tests

import (
"fmt"
Expand All @@ -8,13 +8,12 @@ import (
"strconv"
)

var CartesiMachine = "/home/renan/Local/install/bin/cartesi-machine"
var RiscVCC = "riscv64-linux-gnu-gcc-12"

// CreateSnapshot creates a cartesi machine snapshot
// with a block size of 1024*kblock
// from the Go program at name.
func CreateSnapshot(name string, kblocks uint64) error {
func CreateSnapshot(name string, kblocks uint64) {
folder := name + "/"
main := folder + "main.go"
binary := "temp/" + name
Expand All @@ -23,13 +22,9 @@ func CreateSnapshot(name string, kblocks uint64) error {

defer func() {
folder := name + "/"
err := run(
exec.Command("rm", "-f", folder+name),
run(exec.Command("rm", "-f", folder+name),
exec.Command("rm", "-f", name+".ext2"),
exec.Command("rm", "-rf", "temp"))
if err != nil {
panic(err)
}
}()

goCmd := exec.Command("go", "build", "-o", binary, main)
Expand All @@ -42,26 +37,24 @@ func CreateSnapshot(name string, kblocks uint64) error {

blocks := strconv.FormatUint(kblocks*1024, 10)

return run(
exec.Command("rm", "-rf", snapshot),
run(exec.Command("rm", "-rf", snapshot),
goCmd,
exec.Command("xgenext2fs", "-f", "-b", blocks, "-d", "temp", ext2),
exec.Command(CartesiMachine,
exec.Command("cartesi-machine",
fmt.Sprintf("--flash-drive=label:%s,filename:%s", name, ext2),
fmt.Sprintf("--store=%s", snapshot),
"--", "CMT_DEBUG=yes", fmt.Sprintf("/mnt/%s/%s", name, name)))
}

func run(cmds ...*exec.Cmd) error {
func run(cmds ...*exec.Cmd) {
for _, cmd := range cmds {
slog.Info(cmd.String())
output, err := cmd.CombinedOutput()
if s := string(output); s != "" {
slog.Info(s)
}
if err != nil {
return err
slog.Error(err.Error())
}
}
return nil
}
16 changes: 16 additions & 0 deletions pkg/machine-runner/tests/input-exception/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// (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))
}
1 change: 1 addition & 0 deletions pkg/machine-runner/tests/input-rejected/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
snapshot
15 changes: 15 additions & 0 deletions pkg/machine-runner/tests/input-rejected/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// (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)
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package machine
package tests

import (
"errors"
"os"
"testing"

"github.com/cartesi/rollups-node/pkg/machine-runner/machine"
"github.com/cartesi/rollups-node/pkg/machine-runner/machine/binding"

"github.com/stretchr/testify/suite"
)

var (
binary = os.Getenv("SERVER_PATH")
snapshot = os.Getenv("SNAPSHOT_PATH")
snapshotNotAtManualYield = os.Getenv("SNAPSHOT_NOT_PRIMED_PATH")
snapshotInputRejected = os.Getenv("SNAPSHOT_INPUT_REJECTED_PATH")
snapshotInputException = os.Getenv("SNAPSHOT_INPUT_EXCEPTION_PATH")
snapshotAdvanceInspect = os.Getenv("SNAPSHOT_ADVANCE_INSPECT_PATH")
)
func init() {
snapshots := []string{
"advance",
"input-exception",
"input-rejected",
"not-at-manual-yield",
"report",
}
for _, snapshot := range snapshots {
CreateSnapshot(snapshot, 2)
}
}

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

Expand All @@ -32,56 +36,60 @@ type LoadSuite struct {

func (suite *LoadSuite) SetupTest() {
var err error
suite.address, err = StartServer(binary, ServerLogLevelInfo, 0, os.Stdout, os.Stderr)
suite.address, err = machine.StartServer(machine.ServerLogLevelInfo, 0, os.Stdout, os.Stderr)
suite.Nil(err)
suite.config = binding.RuntimeConfig{}
}

func (suite *LoadSuite) TearDownTest() {
err := StopServer(suite.address)
err := machine.StopServer(suite.address)
suite.Nil(err)
}

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

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

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

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

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

func (suite *LoadSuite) TestNotPrimedInputException() {
machine, err := Load(suite.address, snapshotInputException, suite.config)
m, err := machine.Load(suite.address, "input-exception/snapshot", suite.config)
suite.NotNil(err)
suite.True(errors.Is(err, ErrInputException))
suite.Nil(machine)
suite.ErrorIs(err, machine.ErrLastInputYieldedAnException)
suite.Nil(m)
}

func TestLoad(t *testing.T) {
suite.Run(t, new(LoadSuite))
}

// ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -130,16 +138,17 @@ func (suite *ForkSuite) TearDownTest() {

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

/*
type AdvanceInspectSuite struct {
suite.Suite
machine *Machine
machine *machine.Machine
}
func (suite *AdvanceInspectSuite) SetupTest() {
address, err := StartServer(binary, ServerLogLevelInfo, 0, os.Stdout, os.Stderr)
address, err := machine.StartServer(binary, machine.ServerLogLevelInfo, 0, os.Stdout, os.Stderr)
suite.Nil(err)
suite.machine, err = Load(address, snapshotAdvanceInspect, binding.RuntimeConfig{})
suite.machine, err = machine.Load(address, snapshotAdvanceInspect, binding.RuntimeConfig{})
suite.Nil(err)
suite.NotNil(suite.machine)
}
Expand All @@ -150,7 +159,7 @@ func (suite *AdvanceInspectSuite) TearDownTest() {
}
func (suite *AdvanceInspectSuite) TestSingleOutput() {
input := Input{Data: []byte("renan")}
input := machine.Input{Data: []byte("renan")}
request, err := input.Encode()
suite.Nil(err)
Expand All @@ -168,12 +177,6 @@ func (suite *AdvanceInspectSuite) TestSingleOutput() {
// ------------------------------------------------------------------------------------------------
func TestAll(t *testing.T) {
// suite.Run(t, new(LoadSuite))
// suite.Run(t, new(ForkSuite))
suite.Run(t, new(AdvanceInspectSuite))
}

/*
- Fork ok.
Expand Down
1 change: 1 addition & 0 deletions pkg/machine-runner/tests/not-at-manual-yield/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
snapshot
16 changes: 16 additions & 0 deletions pkg/machine-runner/tests/not-at-manual-yield/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// (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))
}
1 change: 1 addition & 0 deletions pkg/machine-runner/tests/report/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
snapshot
File renamed without changes.

0 comments on commit adca281

Please sign in to comment.