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 Jul 19, 2024
1 parent 370cb46 commit f749688
Show file tree
Hide file tree
Showing 6 changed files with 1,202 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/node/model/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
)

const HashLength = common.HashLength

type (
Hash = common.Hash
Address = common.Address
Expand Down
42 changes: 42 additions & 0 deletions pkg/rollupsmachine/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// (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/internal/node/model"
)

const unreachable = "internal error: entered unreacheable code"

var (
ErrCartesiMachine = errors.New("cartesi machine internal error")

// Misc.
ErrException = errors.New("last request yielded an exception")
ErrHalted = errors.New("machine halted")
ErrProgress = errors.New("machine yielded progress")
ErrSoftYield = errors.New("machine yielded softly")
ErrCycleLimitExceeded = errors.New("cycle limit exceeded")
ErrOutputsLimitExceeded = errors.New("outputs length limit exceeded")
// ErrPayloadLengthLimitExceeded = errors.New("payload length limit exceeded")

ErrOrphanServer = errors.New("cartesi machine server was left orphan")

// Load
ErrNotAtManualYield = errors.New("not at manual yield")

// Advance
ErrHashLength = fmt.Errorf("hash does not have exactly %d bytes", model.HashLength)
)

func errOrphanServerWithAddress(address string) error {
return fmt.Errorf("%w at address %s", ErrOrphanServer, address)
}

func errCartesiMachine(err error) error {
return errors.Join(ErrCartesiMachine, err)
}
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: //nolint:mnd
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 f749688

Please sign in to comment.