Skip to content

Commit

Permalink
feat!: blockchain agnostic inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
ZzzzHui committed Jan 25, 2024
1 parent d8561fe commit 0f88032
Show file tree
Hide file tree
Showing 23 changed files with 204 additions and 161 deletions.
5 changes: 5 additions & 0 deletions onchain/rollups/.changeset/grumpy-coins-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cartesi/rollups": major
---

Inputs are now blockchain-agnostic and self-contained blobs.
1 change: 1 addition & 0 deletions onchain/rollups/.changeset/seven-teachers-behave.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Removed:
- the `AuthorityHistoryPairFactory` contract.
- the `IAuthorityHistoryPairFactory` interface.
- the `OutputEncoding` library.
- the `LibInput` library.
12 changes: 12 additions & 0 deletions onchain/rollups/.changeset/six-doors-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"@cartesi/rollups": major
---

Modified the `IInputBox` interface:

- Modified the `InputAdded` event:

- Removed the `sender` parameter.
- Changed the semantics of the `input` parameter.

- Added an `InputTooLarge` error.
4 changes: 2 additions & 2 deletions onchain/rollups/contracts/common/CanonicalMachine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ library CanonicalMachine {
/// @notice Keccak-256 output size (32 bytes).
Log2Size constant KECCAK_LOG2_SIZE = Log2Size.wrap(5);

/// @notice Maximum input payload size (~2 megabytes).
/// @notice Maximum input size (~2 megabytes).
/// @dev The offset and size fields use up the extra 64 bytes.
uint256 constant INPUT_PAYLOAD_MAX_SIZE = (1 << 21) - 64;
uint256 constant INPUT_MAX_SIZE = (1 << 21) - 64;

/// @notice Maximum output metadata memory range (2 megabytes).
Log2Size constant OUTPUT_METADATA_LOG2_SIZE = Log2Size.wrap(21);
Expand Down
12 changes: 6 additions & 6 deletions onchain/rollups/contracts/common/InputEncoding.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ library InputEncoding {
/// @param sender The Ether sender
/// @param value The amount of Ether being sent in Wei
/// @param execLayerData Additional data to be interpreted by the execution layer
/// @return The encoded input
/// @return The encoded input payload
function encodeEtherDeposit(
address sender,
uint256 value,
Expand All @@ -35,7 +35,7 @@ library InputEncoding {
/// @param sender The token sender
/// @param amount The amount of tokens being sent
/// @param execLayerData Additional data to be interpreted by the execution layer
/// @return The encoded input
/// @return The encoded input payload
function encodeERC20Deposit(
IERC20 token,
address sender,
Expand All @@ -57,7 +57,7 @@ library InputEncoding {
/// @param tokenId The token identifier
/// @param baseLayerData Additional data to be interpreted by the base layer
/// @param execLayerData Additional data to be interpreted by the execution layer
/// @return The encoded input
/// @return The encoded input payload
/// @dev `baseLayerData` should be forwarded to `token`.
function encodeERC721Deposit(
IERC721 token,
Expand All @@ -83,7 +83,7 @@ library InputEncoding {
/// @param value Transfer amount
/// @param baseLayerData Additional data to be interpreted by the base layer
/// @param execLayerData Additional data to be interpreted by the execution layer
/// @return The encoded input
/// @return The encoded input payload
/// @dev `baseLayerData` should be forwarded to `token`.
function encodeSingleERC1155Deposit(
IERC1155 token,
Expand Down Expand Up @@ -111,7 +111,7 @@ library InputEncoding {
/// @param values Transfer amounts per token type
/// @param baseLayerData Additional data to be interpreted by the base layer
/// @param execLayerData Additional data to be interpreted by the execution layer
/// @return The encoded input
/// @return The encoded input payload
/// @dev `baseLayerData` should be forwarded to `token`.
function encodeBatchERC1155Deposit(
IERC1155 token,
Expand All @@ -137,7 +137,7 @@ library InputEncoding {

/// @notice Encode an application address relay.
/// @param app The application address
/// @return The encoded input
/// @return The encoded input payload
function encodeApplicationAddressRelay(
address app
) internal pure returns (bytes memory) {
Expand Down
26 changes: 26 additions & 0 deletions onchain/rollups/contracts/common/Inputs.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

pragma solidity ^0.8.8;

/// @title Inputs
/// @notice Defines the signatures of inputs.
interface Inputs {
/// @notice An advance request from an EVM-compatible blockchain to a Cartesi Machine.
/// @param sender The address of whoever sent the input
/// @param blockNumber The number of the block in which the input was added
/// @param blockTimestamp The timestamp of the block in which the input was added
/// @param index The input index
/// @param payload The payload provided by the sender
function EvmAdvance(
address sender,
uint256 blockNumber,
uint256 blockTimestamp,
uint256 index,
bytes calldata payload
) external;

/// @notice An inspect request to a Cartesi Machine.
/// @param payload The inspect payload
function EvmInspect(bytes calldata payload) external;
}
24 changes: 9 additions & 15 deletions onchain/rollups/contracts/inputs/IInputBox.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,23 @@ interface IInputBox {
/// @notice MUST trigger when an input is added.
/// @param app The application address
/// @param index The input index
/// @param sender The input sender address
/// @param payload The input payload
event InputAdded(
address indexed app,
uint256 indexed index,
address sender,
bytes payload
);
/// @param input The input blob
event InputAdded(address indexed app, uint256 indexed index, bytes input);

/// @notice Payload is too large.
/// @notice Input is too large.
/// @param app The application address
/// @param payloadLength The payload length
/// @param maxPayloadLength The maximum payload length
error PayloadTooLarge(
/// @param inputLength The input length
/// @param maxInputLength The maximum input length
error InputTooLarge(
address app,
uint256 payloadLength,
uint256 maxPayloadLength
uint256 inputLength,
uint256 maxInputLength
);

/// @notice Send an input to an application.
/// @param app The application address
/// @param payload The input payload
/// @return The input hash
/// @return The hash of the input blob
/// @dev MUST fire an `InputAdded` event.
function addInput(
address app,
Expand Down
31 changes: 15 additions & 16 deletions onchain/rollups/contracts/inputs/InputBox.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
pragma solidity ^0.8.8;

import {IInputBox} from "./IInputBox.sol";
import {LibInput} from "../library/LibInput.sol";
import {CanonicalMachine} from "../common/CanonicalMachine.sol";
import {Inputs} from "../common/Inputs.sol";

contract InputBox is IInputBox {
/// @notice Mapping of application addresses to arrays of input hashes.
Expand All @@ -16,29 +16,28 @@ contract InputBox is IInputBox {
address app,
bytes calldata payload
) external override returns (bytes32) {
if (payload.length > CanonicalMachine.INPUT_PAYLOAD_MAX_SIZE) {
revert PayloadTooLarge(
app,
payload.length,
CanonicalMachine.INPUT_PAYLOAD_MAX_SIZE
);
}

bytes32[] storage inputBox = _inputBoxes[app];

uint256 index = inputBox.length;

bytes32 inputHash = LibInput.computeInputHash(
msg.sender,
block.number,
block.timestamp,
index,
payload
bytes memory input = abi.encodeCall(
Inputs.EvmAdvance,
(msg.sender, block.number, block.timestamp, index, payload)
);

if (input.length > CanonicalMachine.INPUT_MAX_SIZE) {
revert InputTooLarge(
app,
input.length,
CanonicalMachine.INPUT_MAX_SIZE
);
}

bytes32 inputHash = keccak256(input);

inputBox.push(inputHash);

emit InputAdded(app, index, msg.sender, payload);
emit InputAdded(app, index, input);

return inputHash;
}
Expand Down
45 changes: 0 additions & 45 deletions onchain/rollups/contracts/library/LibInput.sol

This file was deleted.

4 changes: 2 additions & 2 deletions onchain/rollups/contracts/portals/ERC1155BatchPortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ contract ERC1155BatchPortal is IERC1155BatchPortal, InputRelay {
baseLayerData
);

bytes memory input = InputEncoding.encodeBatchERC1155Deposit(
bytes memory payload = InputEncoding.encodeBatchERC1155Deposit(
token,
msg.sender,
tokenIds,
Expand All @@ -45,7 +45,7 @@ contract ERC1155BatchPortal is IERC1155BatchPortal, InputRelay {
execLayerData
);

_inputBox.addInput(app, input);
_inputBox.addInput(app, payload);
}

function supportsInterface(
Expand Down
4 changes: 2 additions & 2 deletions onchain/rollups/contracts/portals/ERC1155SinglePortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ contract ERC1155SinglePortal is IERC1155SinglePortal, InputRelay {
) external override {
token.safeTransferFrom(msg.sender, app, tokenId, value, baseLayerData);

bytes memory input = InputEncoding.encodeSingleERC1155Deposit(
bytes memory payload = InputEncoding.encodeSingleERC1155Deposit(
token,
msg.sender,
tokenId,
Expand All @@ -39,7 +39,7 @@ contract ERC1155SinglePortal is IERC1155SinglePortal, InputRelay {
execLayerData
);

_inputBox.addInput(app, input);
_inputBox.addInput(app, payload);
}

function supportsInterface(
Expand Down
4 changes: 2 additions & 2 deletions onchain/rollups/contracts/portals/ERC20Portal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ contract ERC20Portal is IERC20Portal, InputRelay {
revert ERC20TransferFailed();
}

bytes memory input = InputEncoding.encodeERC20Deposit(
bytes memory payload = InputEncoding.encodeERC20Deposit(
token,
msg.sender,
amount,
execLayerData
);

_inputBox.addInput(app, input);
_inputBox.addInput(app, payload);
}

function supportsInterface(
Expand Down
4 changes: 2 additions & 2 deletions onchain/rollups/contracts/portals/ERC721Portal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ contract ERC721Portal is IERC721Portal, InputRelay {
) external override {
token.safeTransferFrom(msg.sender, app, tokenId, baseLayerData);

bytes memory input = InputEncoding.encodeERC721Deposit(
bytes memory payload = InputEncoding.encodeERC721Deposit(
token,
msg.sender,
tokenId,
baseLayerData,
execLayerData
);

_inputBox.addInput(app, input);
_inputBox.addInput(app, payload);
}

function supportsInterface(
Expand Down
4 changes: 2 additions & 2 deletions onchain/rollups/contracts/portals/EtherPortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ contract EtherPortal is IEtherPortal, InputRelay {
revert EtherTransferFailed();
}

bytes memory input = InputEncoding.encodeEtherDeposit(
bytes memory payload = InputEncoding.encodeEtherDeposit(
msg.sender,
msg.value,
execLayerData
);

_inputBox.addInput(app, input);
_inputBox.addInput(app, payload);
}

function supportsInterface(
Expand Down
4 changes: 2 additions & 2 deletions onchain/rollups/contracts/relays/ApplicationAddressRelay.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ contract ApplicationAddressRelay is IApplicationAddressRelay, InputRelay {
constructor(IInputBox inputBox) InputRelay(inputBox) {}

function relayApplicationAddress(address app) external override {
bytes memory input = InputEncoding.encodeApplicationAddressRelay(app);
_inputBox.addInput(app, input);
bytes memory payload = InputEncoding.encodeApplicationAddressRelay(app);
_inputBox.addInput(app, payload);
}

function supportsInterface(
Expand Down
Loading

0 comments on commit 0f88032

Please sign in to comment.