Skip to content

Commit

Permalink
refactor: rename input related terms
Browse files Browse the repository at this point in the history
  • Loading branch information
ZzzzHui committed Nov 15, 2023
1 parent 8e4c677 commit e8523ea
Show file tree
Hide file tree
Showing 18 changed files with 177 additions and 108 deletions.
7 changes: 7 additions & 0 deletions onchain/rollups/.changeset/cuddly-ears-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@cartesi/rollups": major
---

`inputIndex` is renamed as `index`
`input` is renamed as `payload` and `input` now refers to the whole input blob
event `InputAdded` now contains `dapp`, `index` and `input`
4 changes: 2 additions & 2 deletions onchain/rollups/contracts/common/Inputs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ pragma solidity ^0.8.8;
/// @title Inputs
/// @notice Defines the signatures of inputs.
interface Inputs {
/// @notice An EVM input.
/// @notice An EvmAdvance input.
/// @param sender `msg.sender`
/// @param blockNumber `block.number`
/// @param blockTimestamp `block.timestamp`
/// @param inputIndex The index of the input in the input box
/// @param input The input payload
function EvmInput(
function EvmAdvance(
address sender,
uint256 blockNumber,
uint256 blockTimestamp,
Expand Down
18 changes: 6 additions & 12 deletions onchain/rollups/contracts/inputs/IInputBox.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,20 @@ pragma solidity ^0.8.8;
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 sender The address that sent the input
/// @param input The input payload
/// @param index The index of the input in the 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,
address sender,
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
16 changes: 7 additions & 9 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 (and input hash) is composed of the address of the input sender,
/// the block number and timestamp, the index of the input, and the input 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 @@ -35,14 +35,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.EvmInput,
(msg.sender, block.number, block.timestamp, inputIndex, _input)
Inputs.EvmAdvance,
(msg.sender, block.number, block.timestamp, index, _payload)
);

if (input.length > CanonicalMachine.INPUT_MAX_SIZE) {
Expand All @@ -51,11 +51,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, msg.sender, _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 @@ -29,13 +29,13 @@ contract ERC20Portal is InputRelay, IERC20Portal {

require(success, "Portal: ERC20 deposit failed");

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);
}
}
48 changes: 24 additions & 24 deletions onchain/rollups/test/foundry/inputs/InputBox.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
pragma solidity ^0.8.8;

import {Test} from "forge-std/Test.sol";
import {TestBase} from "../util/TestBase.sol";
import {InputBox} from "contracts/inputs/InputBox.sol";
import {IInputBox} from "contracts/inputs/IInputBox.sol";
import {CanonicalMachine} from "contracts/common/CanonicalMachine.sol";
Expand Down Expand Up @@ -48,7 +49,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 +63,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 @@ -100,8 +101,8 @@ contract InputBoxHandler is Test {
// Compute the input hash from the arguments passed to `addInput`
bytes32 computedInputHash = keccak256(
abi.encodeCall(
Inputs.EvmInput,
(msg.sender, block.number, block.timestamp, index, _input)
Inputs.EvmAdvance,
(msg.sender, block.number, block.timestamp, index, _payload)
)
);

Expand Down Expand Up @@ -130,18 +131,13 @@ contract InputBoxHandler is Test {
}
}

contract InputBoxTest is Test {
contract InputBoxTest is TestBase {
using CanonicalMachine for CanonicalMachine.Log2Size;

InputBox inputBox;
InputBoxHandler handler;

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

function setUp() public {
inputBox = new InputBox();
Expand Down Expand Up @@ -169,18 +165,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 @@ -189,26 +185,30 @@ contract InputBoxTest is Test {
vm.expectEmit(true, true, false, true, address(inputBox));

// The event we expect
emit InputAdded(_dapp, i, address(this), _inputs[i]);
emit InputAdded(
_dapp,
i,
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(
Inputs.EvmInput,
Inputs.EvmAdvance,
(
address(this),
i, // block.number
i + year2022, // block.timestamp
i, // inputBox.length
_inputs[i]
_payloads[i]
)
)
);
Expand Down Expand Up @@ -262,7 +262,7 @@ contract InputBoxTest is Test {

function getMaxInputPayloadLength() internal pure returns (uint256) {
bytes memory blob = abi.encodeCall(
Inputs.EvmInput,
Inputs.EvmAdvance,
(address(0), 0, 0, 0, new bytes(32))
);
// number of bytes in input blob excluding input payload
Expand Down
Loading

0 comments on commit e8523ea

Please sign in to comment.