Skip to content

Commit

Permalink
feat!: rename input related terms
Browse files Browse the repository at this point in the history
  • Loading branch information
ZzzzHui committed Nov 21, 2023
1 parent 4861c4e commit 4471b6e
Show file tree
Hide file tree
Showing 18 changed files with 174 additions and 77 deletions.
9 changes: 9 additions & 0 deletions onchain/rollups/.changeset/cuddly-ears-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@cartesi/rollups": major
---

Renamed `inputIndex` as `index`, and `input` as `payload`.
This change was made to draw a clear distinction between input and payload, in the context of the `InputBox` contract.
When a user submits a payload, it is encoded along with metadata to form an input blob, which is then forwarded to the Cartesi Machine.
The `InputAdded` event now emits the input blob instead of the payload.
As a result, you can now feed the machine with the input blob as-is, and let it be decoded in user space.
8 changes: 4 additions & 4 deletions onchain/rollups/contracts/common/Inputs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ interface Inputs {
/// @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 inputIndex The index of the input in the DApp's input box
/// @param input The payload provided by the sender
/// @param index The index of the input in the DApp's input box
/// @param payload The payload provided by the sender
function EvmAdvance(
address sender,
uint256 blockNumber,
uint256 blockTimestamp,
uint256 inputIndex,
bytes calldata input
uint256 index,
bytes calldata payload
) external;

/// @notice An inspect request from an EVM-compatible blockchain to a Cartesi Machine.
Expand Down
12 changes: 6 additions & 6 deletions onchain/rollups/contracts/inputs/IInputBox.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ interface IInputBox {

/// @notice Emitted when an input is added to a DApp's input box.
/// @param dapp The address of the DApp
/// @param inputIndex The index of the input in the input box
/// @param input The input payload
/// @param index The index of the input in the DApp's input box
/// @param input The input blob
/// @dev MUST be triggered on a successful call to `addInput`.
event InputAdded(address indexed dapp, uint256 indexed inputIndex, bytes input);
event InputAdded(address indexed dapp, uint256 indexed index, bytes input);

/// @notice Add an input to a DApp's input box.
/// @param _dapp The address of the DApp
/// @param _input The input payload
/// @return The hash of the input plus some extra metadata
/// @param _payload The input payload
/// @return The hash of the input blob
/// @dev MUST fire an `InputAdded` event accordingly.
/// Input larger than machine limit will raise `InputSizeExceedsLimit` error.
function addInput(
address _dapp,
bytes calldata _input
bytes calldata _payload
) external returns (bytes32);

/// @notice Get the number of inputs in a DApp's input box.
Expand Down
14 changes: 6 additions & 8 deletions onchain/rollups/contracts/inputs/InputBox.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {Inputs} from "../common/Inputs.sol";
/// data from anyone and adds a compound hash to an append-only list
/// (called "input box"). Each DApp has its own input box.
///
/// The input hash is composed by the input payload, the block number and timestamp,
/// the address of the input sender, and the index of the input.
/// The input blob is composed of the address of the input sender,
/// the block number and timestamp, the input index and payload.
///
/// Data availability is guaranteed by the emission of `InputAdded` events
/// on every successful call to `addInput`. This ensures that inputs can be
Expand All @@ -32,14 +32,14 @@ contract InputBox is IInputBox {

function addInput(
address _dapp,
bytes calldata _input
bytes calldata _payload
) external override returns (bytes32) {
bytes32[] storage inputBox = inputBoxes[_dapp];
uint256 inputIndex = inputBox.length;
uint256 index = inputBox.length;

bytes memory input = abi.encodeCall(
Inputs.EvmAdvance,
(msg.sender, block.number, block.timestamp, inputIndex, _input)
(msg.sender, block.number, block.timestamp, index, _payload)
);

if (input.length > CanonicalMachine.INPUT_MAX_SIZE) {
Expand All @@ -48,11 +48,9 @@ contract InputBox is IInputBox {

bytes32 inputHash = keccak256(input);

// add input to the input box
inputBox.push(inputHash);

// block.number and timestamp can be retrieved by the event metadata itself
emit InputAdded(_dapp, inputIndex, input);
emit InputAdded(_dapp, index, input);

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

bytes memory input = InputEncoding.encodeBatchERC1155Deposit(
bytes memory payload = InputEncoding.encodeBatchERC1155Deposit(
_token,
msg.sender,
_tokenIds,
Expand All @@ -44,6 +44,6 @@ contract ERC1155BatchPortal is InputRelay, IERC1155BatchPortal {
_execLayerData
);

inputBox.addInput(_dapp, input);
inputBox.addInput(_dapp, payload);
}
}
4 changes: 2 additions & 2 deletions onchain/rollups/contracts/portals/ERC1155SinglePortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ contract ERC1155SinglePortal is InputRelay, IERC1155SinglePortal {
_baseLayerData
);

bytes memory input = InputEncoding.encodeSingleERC1155Deposit(
bytes memory payload = InputEncoding.encodeSingleERC1155Deposit(
_token,
msg.sender,
_tokenId,
Expand All @@ -44,6 +44,6 @@ contract ERC1155SinglePortal is InputRelay, IERC1155SinglePortal {
_execLayerData
);

inputBox.addInput(_dapp, input);
inputBox.addInput(_dapp, payload);
}
}
4 changes: 2 additions & 2 deletions onchain/rollups/contracts/portals/ERC20Portal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ contract ERC20Portal is InputRelay, IERC20Portal {
) external override {
_token.safeTransferFrom(msg.sender, _dapp, _amount);

bytes memory input = InputEncoding.encodeERC20Deposit(
bytes memory payload = InputEncoding.encodeERC20Deposit(
_token,
msg.sender,
_amount,
_execLayerData
);

inputBox.addInput(_dapp, input);
inputBox.addInput(_dapp, payload);
}
}
4 changes: 2 additions & 2 deletions onchain/rollups/contracts/portals/ERC721Portal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ contract ERC721Portal is InputRelay, IERC721Portal {
) external override {
_token.safeTransferFrom(msg.sender, _dapp, _tokenId, _baseLayerData);

bytes memory input = InputEncoding.encodeERC721Deposit(
bytes memory payload = InputEncoding.encodeERC721Deposit(
_token,
msg.sender,
_tokenId,
_baseLayerData,
_execLayerData
);

inputBox.addInput(_dapp, input);
inputBox.addInput(_dapp, payload);
}
}
4 changes: 2 additions & 2 deletions onchain/rollups/contracts/portals/EtherPortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ contract EtherPortal is InputRelay, IEtherPortal {
revert EtherTransferFailed();
}

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

inputBox.addInput(_dapp, input);
inputBox.addInput(_dapp, payload);
}
}
4 changes: 2 additions & 2 deletions onchain/rollups/contracts/relays/DAppAddressRelay.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ contract DAppAddressRelay is InputRelay, IDAppAddressRelay {
constructor(IInputBox _inputBox) InputRelay(_inputBox) {}

function relayDAppAddress(address _dapp) external override {
bytes memory input = InputEncoding.encodeDAppAddressRelay(_dapp);
inputBox.addInput(_dapp, input);
bytes memory payload = InputEncoding.encodeDAppAddressRelay(_dapp);
inputBox.addInput(_dapp, payload);
}
}
40 changes: 25 additions & 15 deletions onchain/rollups/test/foundry/inputs/InputBox.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {IInputBox} from "contracts/inputs/IInputBox.sol";
import {CanonicalMachine} from "contracts/common/CanonicalMachine.sol";
import {Inputs} from "contracts/common/Inputs.sol";

import {EncodeEvmAdvance} from "../util/EncodeEvmAdvance.sol";

contract InputBoxHandler is Test {
IInputBox immutable inputBox;

Expand Down Expand Up @@ -48,7 +50,7 @@ contract InputBoxHandler is Test {
vm.roll(blockNumber);
}

function addInput(address _dapp, bytes calldata _input) external {
function addInput(address _dapp, bytes calldata _payload) external {
// For some reason, the invariant testing framework doesn't
// record changes made to block properties, so we have to
// set them in the beginning of every call
Expand All @@ -62,7 +64,7 @@ contract InputBoxHandler is Test {

// Make the sender add the input to the DApp's input box
vm.prank(msg.sender);
bytes32 inputHash = inputBox.addInput(_dapp, _input);
bytes32 inputHash = inputBox.addInput(_dapp, _payload);

// If this is the first input being added to the DApp's input box,
// then push the dapp to the array of dapps
Expand Down Expand Up @@ -101,7 +103,7 @@ contract InputBoxHandler is Test {
bytes32 computedInputHash = keccak256(
abi.encodeCall(
Inputs.EvmAdvance,
(msg.sender, block.number, block.timestamp, index, _input)
(msg.sender, block.number, block.timestamp, index, _payload)
)
);

Expand Down Expand Up @@ -136,7 +138,7 @@ contract InputBoxTest is Test {
InputBox inputBox;
InputBoxHandler handler;

event InputAdded(address indexed dapp, uint256 indexed inputIndex, bytes input);
event InputAdded(address indexed dapp, uint256 indexed index, bytes input);

function setUp() public {
inputBox = new InputBox();
Expand Down Expand Up @@ -164,18 +166,18 @@ contract InputBoxTest is Test {
}

// fuzz testing with multiple inputs
function testAddInput(address _dapp, bytes[] calldata _inputs) public {
uint256 numInputs = _inputs.length;
bytes32[] memory returnedValues = new bytes32[](numInputs);
function testAddInput(address _dapp, bytes[] calldata _payloads) public {
uint256 numPayloads = _payloads.length;
bytes32[] memory returnedValues = new bytes32[](numPayloads);
uint256 year2022 = 1641070800; // Unix Timestamp for 2022

// assume #bytes for each input is within bounds
for (uint256 i; i < numInputs; ++i) {
vm.assume(_inputs[i].length <= getMaxInputPayloadLength());
// assume #bytes for each payload is within bounds
for (uint256 i; i < numPayloads; ++i) {
vm.assume(_payloads[i].length <= getMaxInputPayloadLength());
}

// adding inputs
for (uint256 i; i < numInputs; ++i) {
for (uint256 i; i < numPayloads; ++i) {
// test for different block number and timestamp
vm.roll(i);
vm.warp(i + year2022); // year 2022
Expand All @@ -184,16 +186,24 @@ contract InputBoxTest is Test {
vm.expectEmit(true, true, false, true, address(inputBox));

// The event we expect
emit InputAdded(_dapp, i, _inputs[i]);
emit InputAdded(
_dapp,
i,
EncodeEvmAdvance.encodeEvmAdvance(
address(this),
i,
_payloads[i]
)
);

returnedValues[i] = inputBox.addInput(_dapp, _inputs[i]);
returnedValues[i] = inputBox.addInput(_dapp, _payloads[i]);

// test whether the number of inputs has increased
assertEq(i + 1, inputBox.getNumberOfInputs(_dapp));
}

// testing added inputs
for (uint256 i; i < numInputs; ++i) {
for (uint256 i; i < numPayloads; ++i) {
// compute input hash for each input
bytes32 inputHash = keccak256(
abi.encodeCall(
Expand All @@ -203,7 +213,7 @@ contract InputBoxTest is Test {
i, // block.number
i + year2022, // block.timestamp
i, // inputBox.length
_inputs[i]
_payloads[i]
)
)
);
Expand Down
21 changes: 16 additions & 5 deletions onchain/rollups/test/foundry/portals/ERC1155BatchPortal.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import {IERC1155} from "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import {IERC1155Receiver} from "@openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol";
import {IInputBox} from "contracts/inputs/IInputBox.sol";
import {InputBox} from "contracts/inputs/InputBox.sol";
import {InputEncoding} from "contracts/common/InputEncoding.sol";

import {EncodeEvmAdvance} from "../util/EncodeEvmAdvance.sol";

contract BatchToken is ERC1155 {
constructor(
Expand Down Expand Up @@ -71,7 +74,7 @@ contract ERC1155BatchPortalTest is Test {
uint256[] ids,
uint256[] values
);
event InputAdded(address indexed dapp, uint256 indexed inputIndex, bytes input);
event InputAdded(address indexed dapp, uint256 indexed index, bytes input);

function setUp() public {
inputBox = new InputBox();
Expand Down Expand Up @@ -107,7 +110,7 @@ contract ERC1155BatchPortalTest is Test {
vm.expectEmit(true, true, true, true);
emit TransferBatch(address(portal), alice, dapp, tokenIds, values);
// Expect InputAdded to be emitted with the right arguments
bytes memory input = this.encodeBatchERC1155Deposit(
bytes memory payload = this.encodeBatchERC1155Deposit(
token,
alice,
tokenIds,
Expand All @@ -116,7 +119,11 @@ contract ERC1155BatchPortalTest is Test {
execLayerData
);
vm.expectEmit(true, true, false, true, address(inputBox));
emit InputAdded(dapp, 0, input);
emit InputAdded(
dapp,
0,
EncodeEvmAdvance.encodeEvmAdvance(address(portal), 0, payload)
);

portal.depositBatchERC1155Token(
token,
Expand Down Expand Up @@ -193,7 +200,7 @@ contract ERC1155BatchPortalTest is Test {
vm.expectEmit(true, true, true, true);
emit TransferBatch(address(portal), alice, dapp, tokenIds, values);
// Expect InputAdded to be emitted with the right arguments
bytes memory input = this.encodeBatchERC1155Deposit(
bytes memory payload = this.encodeBatchERC1155Deposit(
token,
alice,
tokenIds,
Expand All @@ -202,7 +209,11 @@ contract ERC1155BatchPortalTest is Test {
execLayerData
);
vm.expectEmit(true, true, false, true, address(inputBox));
emit InputAdded(dapp, 0, input);
emit InputAdded(
dapp,
0,
EncodeEvmAdvance.encodeEvmAdvance(address(portal), 0, payload)
);

portal.depositBatchERC1155Token(
token,
Expand Down
Loading

0 comments on commit 4471b6e

Please sign in to comment.