-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
1,202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.