Skip to content

Commit

Permalink
feat: add rollupsmachine package
Browse files Browse the repository at this point in the history
  • Loading branch information
renan061 committed May 21, 2024
1 parent dd27f07 commit 03626e7
Show file tree
Hide file tree
Showing 11 changed files with 1,612 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package model

const HashSize = 32

type Hash = [HashSize]byte
40 changes: 40 additions & 0 deletions pkg/rollupsmachine/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package rollupsmachine

import (
"errors"
"fmt"

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

const unreachable = "internal error: entered unreacheable code"

var (
ErrMaxCycles = errors.New("reached limit cycles")
ErrMaxOutputs = fmt.Errorf("reached maximum number of emitted outputs (%d)", maxOutputs)
ErrHashSize = fmt.Errorf("hash does not have exactly %d bytes", HashSize)

ErrFailed = errors.New("machine failed")
ErrHalted = errors.New("machine halted")
ErrYieldedWithProgress = errors.New("machine yielded with progress")
ErrYieldedSoftly = errors.New("machine yielded softly")

// Load (and isReadyForRequests) errors
ErrNewRemoteMachineManager = errors.New("could not create the remote machine manager")
ErrRemoteLoadMachine = errors.New("remote server was not able to load the machine")
ErrNotReadyForRequests = errors.New("machine is not ready to receive requests")
ErrNotAtManualYield = errors.New("not at manual yield")
ErrLastInputWasRejected = errors.New("last input was rejected")
ErrLastInputYieldedAnException = errors.New("last input yielded an exception")

// Fork errors
ErrFork = errors.New("could not fork the machine")
ErrOrphanFork = errors.New("forked cartesi machine was left orphan")

// Destroy errors
ErrRemoteShutdown = errors.New("could not shut down the remote machine")
ErrMachineDestroy = errors.New("could not destroy the inner machine")
)
123 changes: 123 additions & 0 deletions pkg/rollupsmachine/io.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package rollupsmachine

import (
"fmt"
"math/big"
"strings"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
)

type Input struct {
ChainId uint64
AppContract [20]byte
Sender [20]byte
BlockNumber uint64
BlockTimestamp uint64
// PrevRandao uint64
Index uint64
Data []byte
}

type Query struct {
Data []byte
}

type Voucher struct {
Address [20]byte
Value *big.Int
Data []byte
}

type Notice struct {
Data []byte
}

func (input Input) Encode() ([]byte, error) {
chainId := new(big.Int).SetUint64(input.ChainId)
appContract := common.BytesToAddress(input.AppContract[:])
sender := common.BytesToAddress(input.Sender[:])
blockNumber := new(big.Int).SetUint64(input.BlockNumber)
blockTimestamp := new(big.Int).SetUint64(input.BlockTimestamp)
// prevRandao := new(big.Int).SetUint64(input.PrevRandao)
index := new(big.Int).SetUint64(input.Index)
return ioABI.Pack("EvmAdvance", chainId, appContract, sender, blockNumber, blockTimestamp,
index, input.Data)
}

func (query Query) Encode() ([]byte, error) {
return query.Data, nil
}

func decodeArguments(payload []byte) (arguments []any, _ error) {
method, err := ioABI.MethodById(payload)
if err != nil {
return nil, err
}

return method.Inputs.Unpack(payload[4:])
}

func DecodeOutput(payload []byte) (*Voucher, *Notice, error) {
arguments, err := decodeArguments(payload)
if err != nil {
return nil, nil, err
}

switch length := len(arguments); length {
case 1:
notice := &Notice{Data: arguments[0].([]byte)}
return nil, notice, nil
case 3:
voucher := &Voucher{
Address: [20]byte(arguments[0].(common.Address)),
Value: arguments[1].(*big.Int),
Data: arguments[2].([]byte),
}
return voucher, nil, nil
default:
return nil, nil, fmt.Errorf("not an output: len(arguments) == %d, should be 1 or 3", length)
}
}

var ioABI abi.ABI

func init() {
json := `[{
"type" : "function",
"name" : "EvmAdvance",
"inputs" : [
{ "type" : "uint256" },
{ "type" : "address" },
{ "type" : "address" },
{ "type" : "uint256" },
{ "type" : "uint256" },
{ "type" : "uint256" },
{ "type" : "bytes" }
]
}, {
"type" : "function",
"name" : "Voucher",
"inputs" : [
{ "type" : "address" },
{ "type" : "uint256" },
{ "type" : "bytes" }
]
}, {
"type" : "function",
"name" : "Notice",
"inputs" : [
{ "type" : "bytes" }
]
}]`

var err error
ioABI, err = abi.JSON(strings.NewReader(json))
if err != nil {
panic(err)
}
}
Loading

0 comments on commit 03626e7

Please sign in to comment.