From e8523eaebc4788f30b3952fca9f54fc946bf29e4 Mon Sep 17 00:00:00 2001 From: Zehui Zheng Date: Wed, 15 Nov 2023 18:02:57 +0800 Subject: [PATCH] refactor: rename input related terms --- .../rollups/.changeset/cuddly-ears-obey.md | 7 +++ onchain/rollups/contracts/common/Inputs.sol | 4 +- .../rollups/contracts/inputs/IInputBox.sol | 18 +++---- onchain/rollups/contracts/inputs/InputBox.sol | 16 +++---- .../contracts/portals/ERC1155BatchPortal.sol | 4 +- .../contracts/portals/ERC1155SinglePortal.sol | 4 +- .../rollups/contracts/portals/ERC20Portal.sol | 4 +- .../contracts/portals/ERC721Portal.sol | 4 +- .../rollups/contracts/portals/EtherPortal.sol | 4 +- .../contracts/relays/DAppAddressRelay.sol | 4 +- .../test/foundry/inputs/InputBox.t.sol | 48 +++++++++---------- .../foundry/portals/ERC1155BatchPortal.t.sol | 46 +++++++++++++++++- .../foundry/portals/ERC1155SinglePortal.t.sol | 27 ++++++++++- .../test/foundry/portals/ERC20Portal.t.sol | 16 +++---- .../test/foundry/portals/ERC721Portal.t.sol | 28 +++++------ .../test/foundry/portals/EtherPortal.t.sol | 20 ++++---- .../foundry/relays/DAppAddressRelay.t.sol | 17 +++---- .../rollups/test/foundry/util/TestBase.sol | 14 ++++++ 18 files changed, 177 insertions(+), 108 deletions(-) create mode 100644 onchain/rollups/.changeset/cuddly-ears-obey.md diff --git a/onchain/rollups/.changeset/cuddly-ears-obey.md b/onchain/rollups/.changeset/cuddly-ears-obey.md new file mode 100644 index 00000000..2d9b0a5a --- /dev/null +++ b/onchain/rollups/.changeset/cuddly-ears-obey.md @@ -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` diff --git a/onchain/rollups/contracts/common/Inputs.sol b/onchain/rollups/contracts/common/Inputs.sol index 6f125c58..71068873 100644 --- a/onchain/rollups/contracts/common/Inputs.sol +++ b/onchain/rollups/contracts/common/Inputs.sol @@ -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, diff --git a/onchain/rollups/contracts/inputs/IInputBox.sol b/onchain/rollups/contracts/inputs/IInputBox.sol index c6a38469..02aad696 100644 --- a/onchain/rollups/contracts/inputs/IInputBox.sol +++ b/onchain/rollups/contracts/inputs/IInputBox.sol @@ -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. diff --git a/onchain/rollups/contracts/inputs/InputBox.sol b/onchain/rollups/contracts/inputs/InputBox.sol index 3ae74004..5e4bb898 100644 --- a/onchain/rollups/contracts/inputs/InputBox.sol +++ b/onchain/rollups/contracts/inputs/InputBox.sol @@ -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 @@ -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) { @@ -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; } diff --git a/onchain/rollups/contracts/portals/ERC1155BatchPortal.sol b/onchain/rollups/contracts/portals/ERC1155BatchPortal.sol index 1aeee7c7..4a4d5627 100644 --- a/onchain/rollups/contracts/portals/ERC1155BatchPortal.sol +++ b/onchain/rollups/contracts/portals/ERC1155BatchPortal.sol @@ -35,7 +35,7 @@ contract ERC1155BatchPortal is InputRelay, IERC1155BatchPortal { _baseLayerData ); - bytes memory input = InputEncoding.encodeBatchERC1155Deposit( + bytes memory payload = InputEncoding.encodeBatchERC1155Deposit( _token, msg.sender, _tokenIds, @@ -44,6 +44,6 @@ contract ERC1155BatchPortal is InputRelay, IERC1155BatchPortal { _execLayerData ); - inputBox.addInput(_dapp, input); + inputBox.addInput(_dapp, payload); } } diff --git a/onchain/rollups/contracts/portals/ERC1155SinglePortal.sol b/onchain/rollups/contracts/portals/ERC1155SinglePortal.sol index 747a2b62..f9c60266 100644 --- a/onchain/rollups/contracts/portals/ERC1155SinglePortal.sol +++ b/onchain/rollups/contracts/portals/ERC1155SinglePortal.sol @@ -35,7 +35,7 @@ contract ERC1155SinglePortal is InputRelay, IERC1155SinglePortal { _baseLayerData ); - bytes memory input = InputEncoding.encodeSingleERC1155Deposit( + bytes memory payload = InputEncoding.encodeSingleERC1155Deposit( _token, msg.sender, _tokenId, @@ -44,6 +44,6 @@ contract ERC1155SinglePortal is InputRelay, IERC1155SinglePortal { _execLayerData ); - inputBox.addInput(_dapp, input); + inputBox.addInput(_dapp, payload); } } diff --git a/onchain/rollups/contracts/portals/ERC20Portal.sol b/onchain/rollups/contracts/portals/ERC20Portal.sol index 32fdc8b5..6a910cf6 100644 --- a/onchain/rollups/contracts/portals/ERC20Portal.sol +++ b/onchain/rollups/contracts/portals/ERC20Portal.sol @@ -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); } } diff --git a/onchain/rollups/contracts/portals/ERC721Portal.sol b/onchain/rollups/contracts/portals/ERC721Portal.sol index 0e36bbe5..5d560c00 100644 --- a/onchain/rollups/contracts/portals/ERC721Portal.sol +++ b/onchain/rollups/contracts/portals/ERC721Portal.sol @@ -28,7 +28,7 @@ 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, @@ -36,6 +36,6 @@ contract ERC721Portal is InputRelay, IERC721Portal { _execLayerData ); - inputBox.addInput(_dapp, input); + inputBox.addInput(_dapp, payload); } } diff --git a/onchain/rollups/contracts/portals/EtherPortal.sol b/onchain/rollups/contracts/portals/EtherPortal.sol index 46a9a9ba..fa772513 100644 --- a/onchain/rollups/contracts/portals/EtherPortal.sol +++ b/onchain/rollups/contracts/portals/EtherPortal.sol @@ -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); } } diff --git a/onchain/rollups/contracts/relays/DAppAddressRelay.sol b/onchain/rollups/contracts/relays/DAppAddressRelay.sol index ff3520e3..4188043f 100644 --- a/onchain/rollups/contracts/relays/DAppAddressRelay.sol +++ b/onchain/rollups/contracts/relays/DAppAddressRelay.sol @@ -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); } } diff --git a/onchain/rollups/test/foundry/inputs/InputBox.t.sol b/onchain/rollups/test/foundry/inputs/InputBox.t.sol index d54700b3..ee02e1d1 100644 --- a/onchain/rollups/test/foundry/inputs/InputBox.t.sol +++ b/onchain/rollups/test/foundry/inputs/InputBox.t.sol @@ -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"; @@ -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 @@ -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 @@ -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) ) ); @@ -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(); @@ -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 @@ -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] ) ) ); @@ -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 diff --git a/onchain/rollups/test/foundry/portals/ERC1155BatchPortal.t.sol b/onchain/rollups/test/foundry/portals/ERC1155BatchPortal.t.sol index c16bd3bd..afee41b5 100644 --- a/onchain/rollups/test/foundry/portals/ERC1155BatchPortal.t.sol +++ b/onchain/rollups/test/foundry/portals/ERC1155BatchPortal.t.sol @@ -5,6 +5,7 @@ pragma solidity ^0.8.8; import {Test} from "forge-std/Test.sol"; +import {TestBase} from "../util/TestBase.sol"; import {ERC1155BatchPortal} from "contracts/portals/ERC1155BatchPortal.sol"; import {IERC1155BatchPortal} from "contracts/portals/IERC1155BatchPortal.sol"; import {ERC1155} from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol"; @@ -12,6 +13,7 @@ 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"; contract BatchToken is ERC1155 { constructor( @@ -56,7 +58,7 @@ contract ERC1155Receiver is IERC1155Receiver { } } -contract ERC1155BatchPortalTest is Test { +contract ERC1155BatchPortalTest is TestBase { IInputBox inputBox; IERC1155BatchPortal portal; IERC1155 token; @@ -71,6 +73,7 @@ contract ERC1155BatchPortalTest is Test { uint256[] ids, uint256[] values ); + event InputAdded(address indexed dapp, uint256 indexed index, bytes input); function setUp() public { inputBox = new InputBox(); @@ -105,6 +108,17 @@ contract ERC1155BatchPortalTest is Test { // Expect TransferBatch to be emitted with the right arguments 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 payload = this.encodeBatchERC1155Deposit( + token, + alice, + tokenIds, + values, + baseLayerData, + execLayerData + ); + vm.expectEmit(true, true, false, true, address(inputBox)); + emit InputAdded(dapp, 0, encodeEvmAdvance(address(portal), 0, payload)); portal.depositBatchERC1155Token( token, @@ -180,6 +194,17 @@ contract ERC1155BatchPortalTest is Test { // Expect TransferBatch to be emitted with the right arguments 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 payload = this.encodeBatchERC1155Deposit( + token, + alice, + tokenIds, + values, + baseLayerData, + execLayerData + ); + vm.expectEmit(true, true, false, true, address(inputBox)); + emit InputAdded(dapp, 0, encodeEvmAdvance(address(portal), 0, payload)); portal.depositBatchERC1155Token( token, @@ -262,6 +287,25 @@ contract ERC1155BatchPortalTest is Test { } return values; } + + function encodeBatchERC1155Deposit( + IERC1155 _token, + address _sender, + uint256[] calldata _tokenIds, + uint256[] calldata _values, + bytes calldata _baseLayerData, + bytes calldata _execLayerData + ) external pure returns (bytes memory) { + return + InputEncoding.encodeBatchERC1155Deposit( + _token, + _sender, + _tokenIds, + _values, + _baseLayerData, + _execLayerData + ); + } } contract InvariantSettings { diff --git a/onchain/rollups/test/foundry/portals/ERC1155SinglePortal.t.sol b/onchain/rollups/test/foundry/portals/ERC1155SinglePortal.t.sol index 08318bdf..05db94f6 100644 --- a/onchain/rollups/test/foundry/portals/ERC1155SinglePortal.t.sol +++ b/onchain/rollups/test/foundry/portals/ERC1155SinglePortal.t.sol @@ -5,6 +5,7 @@ pragma solidity ^0.8.8; import {Test} from "forge-std/Test.sol"; +import {TestBase} from "../util/TestBase.sol"; import {ERC1155SinglePortal} from "contracts/portals/ERC1155SinglePortal.sol"; import {IERC1155SinglePortal} from "contracts/portals/IERC1155SinglePortal.sol"; import {ERC1155} from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol"; @@ -12,6 +13,7 @@ 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"; contract NormalToken is ERC1155 { constructor( @@ -66,7 +68,7 @@ contract ERC1155Receiver is IERC1155Receiver { } } -contract ERC1155SinglePortalTest is Test { +contract ERC1155SinglePortalTest is TestBase { IInputBox inputBox; IERC1155SinglePortal portal; IERC1155 token; @@ -81,6 +83,7 @@ contract ERC1155SinglePortalTest is Test { uint256 id, uint256 value ); + event InputAdded(address indexed dapp, uint256 indexed index, bytes input); function setUp() public { inputBox = new InputBox(); @@ -112,6 +115,17 @@ contract ERC1155SinglePortalTest is Test { // Expect TransferSingle to be emitted with the right arguments vm.expectEmit(true, true, true, true); emit TransferSingle(address(portal), alice, dapp, tokenId, value); + // Expect InputAdded to be emitted with the right arguments + bytes memory payload = InputEncoding.encodeSingleERC1155Deposit( + token, + alice, + tokenId, + value, + baseLayerData, + execLayerData + ); + vm.expectEmit(true, true, false, true, address(inputBox)); + emit InputAdded(dapp, 0, encodeEvmAdvance(address(portal), 0, payload)); portal.depositSingleERC1155Token( token, @@ -184,6 +198,17 @@ contract ERC1155SinglePortalTest is Test { // Expect TransferSingle to be emitted with the right arguments vm.expectEmit(true, true, true, true); emit TransferSingle(address(portal), alice, dapp, tokenId, value); + // Expect InputAdded to be emitted with the right arguments + bytes memory payload = InputEncoding.encodeSingleERC1155Deposit( + token, + alice, + tokenId, + value, + baseLayerData, + execLayerData + ); + vm.expectEmit(true, true, false, true, address(inputBox)); + emit InputAdded(dapp, 0, encodeEvmAdvance(address(portal), 0, payload)); portal.depositSingleERC1155Token( token, diff --git a/onchain/rollups/test/foundry/portals/ERC20Portal.t.sol b/onchain/rollups/test/foundry/portals/ERC20Portal.t.sol index bec3965e..3e556cfe 100644 --- a/onchain/rollups/test/foundry/portals/ERC20Portal.t.sol +++ b/onchain/rollups/test/foundry/portals/ERC20Portal.t.sol @@ -5,6 +5,7 @@ pragma solidity ^0.8.8; import {Test} from "forge-std/Test.sol"; +import {TestBase} from "../util/TestBase.sol"; import {ERC20Portal} from "contracts/portals/ERC20Portal.sol"; import {IERC20Portal} from "contracts/portals/IERC20Portal.sol"; import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; @@ -83,19 +84,14 @@ contract WatcherToken is ERC20 { } } -contract ERC20PortalTest is Test { +contract ERC20PortalTest is TestBase { IInputBox inputBox; IERC20Portal portal; IERC20 token; address alice; address dapp; - event InputAdded( - address indexed dapp, - uint256 indexed inputIndex, - address sender, - bytes input - ); + event InputAdded(address indexed dapp, uint256 indexed index, bytes input); event WatchedTransfer( address from, address to, @@ -121,8 +117,8 @@ contract ERC20PortalTest is Test { // Create a normal token token = new NormalToken(_amount); - // Construct the ERC-20 deposit input - bytes memory input = InputEncoding.encodeERC20Deposit( + // Construct the ERC-20 deposit input payload + bytes memory payload = InputEncoding.encodeERC20Deposit( token, alice, _amount, @@ -143,7 +139,7 @@ contract ERC20PortalTest is Test { // Expect InputAdded to be emitted with the right arguments vm.expectEmit(true, true, false, true, address(inputBox)); - emit InputAdded(dapp, 0, address(portal), input); + emit InputAdded(dapp, 0, encodeEvmAdvance(address(portal), 0, payload)); // Transfer ERC-20 tokens to the DApp via the portal portal.depositERC20Tokens(token, dapp, _amount, _data); diff --git a/onchain/rollups/test/foundry/portals/ERC721Portal.t.sol b/onchain/rollups/test/foundry/portals/ERC721Portal.t.sol index 3cd25216..28a0b12e 100644 --- a/onchain/rollups/test/foundry/portals/ERC721Portal.t.sol +++ b/onchain/rollups/test/foundry/portals/ERC721Portal.t.sol @@ -5,6 +5,7 @@ pragma solidity ^0.8.8; import {Test} from "forge-std/Test.sol"; +import {TestBase} from "../util/TestBase.sol"; import {ERC721Portal} from "contracts/portals/ERC721Portal.sol"; import {IERC721Portal} from "contracts/portals/IERC721Portal.sol"; import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol"; @@ -82,19 +83,14 @@ contract WatcherERC721Receiver is IERC721Receiver { } } -contract ERC721PortalTest is Test { +contract ERC721PortalTest is TestBase { IInputBox inputBox; IERC721Portal portal; IERC721 token; address alice; address dapp; - event InputAdded( - address indexed dapp, - uint256 indexed inputIndex, - address sender, - bytes input - ); + event InputAdded(address indexed dapp, uint256 indexed index, bytes input); event Transfer( address indexed from, address indexed to, @@ -129,8 +125,8 @@ contract ERC721PortalTest is Test { // Create a normal token with one NFT token = new NormalToken(alice, _tokenId); - // Construct the ERC-721 deposit input - bytes memory input = abi.encodePacked( + // Construct the ERC-721 deposit input payload + bytes memory payload = abi.encodePacked( token, alice, _tokenId, @@ -152,7 +148,7 @@ contract ERC721PortalTest is Test { // Expect InputAdded to be emitted with the right arguments vm.expectEmit(true, true, false, true, address(inputBox)); - emit InputAdded(dapp, 0, address(portal), input); + emit InputAdded(dapp, 0, encodeEvmAdvance(address(portal), 0, payload)); // Transfer ERC-721 tokens to the DApp via the portal portal.depositERC721Token( @@ -183,8 +179,8 @@ contract ERC721PortalTest is Test { // Create a normal token with one NFT token = new NormalToken(alice, _tokenId); - // Construct the ERC-721 deposit input - bytes memory input = abi.encodePacked( + // Construct the ERC-721 deposit input payload + bytes memory payload = abi.encodePacked( token, alice, _tokenId, @@ -206,7 +202,7 @@ contract ERC721PortalTest is Test { // Expect InputAdded to be emitted with the right arguments vm.expectEmit(true, true, false, true, address(inputBox)); - emit InputAdded(dapp, 0, address(portal), input); + emit InputAdded(dapp, 0, encodeEvmAdvance(address(portal), 0, payload)); // Transfer ERC-721 tokens to the DApp via the portal portal.depositERC721Token( @@ -300,8 +296,8 @@ contract ERC721PortalTest is Test { // Create a normal token with one NFT token = new NormalToken(alice, _tokenId); - // Construct the ERC-721 deposit input - bytes memory input = abi.encodePacked( + // Construct the ERC-721 deposit input payload + bytes memory payload = abi.encodePacked( token, alice, _tokenId, @@ -333,7 +329,7 @@ contract ERC721PortalTest is Test { // Expect InputAdded to be emitted with the right arguments vm.expectEmit(true, true, false, true, address(inputBox)); - emit InputAdded(dapp, 0, address(portal), input); + emit InputAdded(dapp, 0, encodeEvmAdvance(address(portal), 0, payload)); // Deposit token in DApp's account portal.depositERC721Token( diff --git a/onchain/rollups/test/foundry/portals/EtherPortal.t.sol b/onchain/rollups/test/foundry/portals/EtherPortal.t.sol index 90816aba..12011c3e 100644 --- a/onchain/rollups/test/foundry/portals/EtherPortal.t.sol +++ b/onchain/rollups/test/foundry/portals/EtherPortal.t.sol @@ -5,6 +5,7 @@ pragma solidity ^0.8.8; import {Test} from "forge-std/Test.sol"; +import {TestBase} from "../util/TestBase.sol"; import {EtherPortal} from "contracts/portals/EtherPortal.sol"; import {IEtherPortal} from "contracts/portals/IEtherPortal.sol"; import {IInputBox} from "contracts/inputs/IInputBox.sol"; @@ -40,18 +41,13 @@ contract InputBoxWatcher { } } -contract EtherPortalTest is Test { +contract EtherPortalTest is TestBase { IInputBox inputBox; IEtherPortal etherPortal; address alice; address dapp; - event InputAdded( - address indexed dapp, - uint256 indexed inputIndex, - address sender, - bytes input - ); + event InputAdded(address indexed dapp, uint256 indexed index, bytes input); event WatchedFallback( address sender, uint256 value, @@ -70,8 +66,8 @@ contract EtherPortalTest is Test { } function testEtherDeposit(uint256 value, bytes calldata data) public { - // Construct the Ether deposit input - bytes memory input = abi.encodePacked(alice, value, data); + // Construct the Ether deposit input payload + bytes memory payload = abi.encodePacked(alice, value, data); // Transfer Ether to Alice and start impersonating her startHoax(alice, value); @@ -83,7 +79,11 @@ contract EtherPortalTest is Test { // Expect InputAdded to be emitted with the right arguments vm.expectEmit(true, true, false, true, address(inputBox)); - emit InputAdded(dapp, 0, address(etherPortal), input); + emit InputAdded( + dapp, + 0, + encodeEvmAdvance(address(etherPortal), 0, payload) + ); // Deposit Ether in the DApp via the portal etherPortal.depositEther{value: value}(dapp, data); diff --git a/onchain/rollups/test/foundry/relays/DAppAddressRelay.t.sol b/onchain/rollups/test/foundry/relays/DAppAddressRelay.t.sol index b2f70209..9fc3fb82 100644 --- a/onchain/rollups/test/foundry/relays/DAppAddressRelay.t.sol +++ b/onchain/rollups/test/foundry/relays/DAppAddressRelay.t.sol @@ -4,23 +4,18 @@ /// @title DApp Address Relay Test pragma solidity ^0.8.8; -import {Test} from "forge-std/Test.sol"; +import {TestBase} from "../util/TestBase.sol"; import {IDAppAddressRelay} from "contracts/relays/IDAppAddressRelay.sol"; import {DAppAddressRelay} from "contracts/relays/DAppAddressRelay.sol"; import {IInputBox} from "contracts/inputs/IInputBox.sol"; import {InputBox} from "contracts/inputs/InputBox.sol"; import {InputEncoding} from "contracts/common/InputEncoding.sol"; -contract DAppAddressRelayTest is Test { +contract DAppAddressRelayTest is TestBase { IInputBox inputBox; IDAppAddressRelay relay; - 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(); @@ -35,12 +30,12 @@ contract DAppAddressRelayTest is Test { // Check the DApp's input box before assertEq(inputBox.getNumberOfInputs(_dapp), 0); - // Construct the DApp address relay input - bytes memory input = abi.encodePacked(_dapp); + // Construct the DApp address relay input payload + bytes memory payload = abi.encodePacked(_dapp); // Expect InputAdded to be emitted with the right arguments vm.expectEmit(true, true, false, true, address(inputBox)); - emit InputAdded(_dapp, 0, address(relay), input); + emit InputAdded(_dapp, 0, encodeEvmAdvance(address(relay), 0, payload)); // Relay the DApp's address relay.relayDAppAddress(_dapp); diff --git a/onchain/rollups/test/foundry/util/TestBase.sol b/onchain/rollups/test/foundry/util/TestBase.sol index fd539ba9..53e1f3e7 100644 --- a/onchain/rollups/test/foundry/util/TestBase.sol +++ b/onchain/rollups/test/foundry/util/TestBase.sol @@ -6,6 +6,8 @@ pragma solidity ^0.8.8; import {Test} from "forge-std/Test.sol"; +import {Inputs} from "contracts/common/Inputs.sol"; + contract TestBase is Test { /// @notice Guarantess `addr` is an address that can be mocked /// @dev Some addresses are reserved by Forge and should not be mocked @@ -17,4 +19,16 @@ contract TestBase is Test { vm.assume(addr != MULTICALL3_ADDRESS); _; } + + function encodeEvmAdvance( + address _sender, + uint256 _index, + bytes memory _payload + ) internal view returns (bytes memory) { + return + abi.encodeCall( + Inputs.EvmAdvance, + (_sender, block.number, block.timestamp, _index, _payload) + ); + } }