Skip to content

Commit

Permalink
feat!: include chain ID in EvmAdvance input
Browse files Browse the repository at this point in the history
  • Loading branch information
ZzzzHui committed Jan 25, 2024
1 parent 4e2533e commit 8892a88
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
5 changes: 5 additions & 0 deletions onchain/rollups/.changeset/shaggy-queens-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cartesi/rollups": major
---

Include chain ID in `EvmAdvance` input.
2 changes: 2 additions & 0 deletions onchain/rollups/contracts/common/Inputs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ pragma solidity ^0.8.8;
/// @notice Defines the signatures of inputs.
interface Inputs {
/// @notice An advance request from an EVM-compatible blockchain to a Cartesi Machine.
/// @param chainId The chain ID
/// @param app The application address
/// @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(
uint256 chainId,
address app,
address sender,
uint256 blockNumber,
Expand Down
10 changes: 9 additions & 1 deletion onchain/rollups/contracts/inputs/InputBox.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ contract InputBox is IInputBox {

bytes memory input = abi.encodeCall(
Inputs.EvmAdvance,
(app, msg.sender, block.number, block.timestamp, index, payload)
(
block.chainid,
app,
msg.sender,
block.number,
block.timestamp,
index,
payload
)
);

if (input.length > CanonicalMachine.INPUT_MAX_SIZE) {
Expand Down
16 changes: 12 additions & 4 deletions onchain/rollups/test/foundry/inputs/InputBox.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ contract InputBoxTest is Test {
assertEq(_inputBox.getNumberOfInputs(app), 0);
}

function testAddLargeInput() public {
function testAddLargeInput(uint64 chainId) public {
address app = vm.addr(1);
uint256 max = _getMaxInputPayloadLength();

_inputBox.addInput(app, new bytes(max));

bytes memory largePayload = new bytes(max + 1);
uint256 largeLength = EvmAdvanceEncoder
.encode(app, address(this), 1, largePayload)
.encode(chainId, app, address(this), 1, largePayload)
.length;
vm.expectRevert(
abi.encodeWithSelector(
Expand All @@ -43,7 +43,13 @@ contract InputBoxTest is Test {
_inputBox.addInput(app, largePayload);
}

function testAddInput(address app, bytes[] calldata payloads) public {
function testAddInput(
uint64 chainId,
address app,
bytes[] calldata payloads
) public {
vm.chainId(chainId); // foundry limits chain id to be less than 2^64 - 1

uint256 numPayloads = payloads.length;
bytes32[] memory returnedValues = new bytes32[](numPayloads);
uint256 year2022 = 1641070800; // Unix Timestamp for 2022
Expand All @@ -61,6 +67,7 @@ contract InputBoxTest is Test {

vm.expectEmit(true, true, false, true, address(_inputBox));
bytes memory input = EvmAdvanceEncoder.encode(
chainId,
app,
address(this),
i,
Expand All @@ -79,6 +86,7 @@ contract InputBoxTest is Test {
abi.encodeCall(
Inputs.EvmAdvance,
(
chainId,
app,
address(this),
i, // block.number
Expand All @@ -98,7 +106,7 @@ contract InputBoxTest is Test {
function _getMaxInputPayloadLength() internal pure returns (uint256) {
bytes memory blob = abi.encodeCall(
Inputs.EvmAdvance,
(address(0), address(0), 0, 0, 0, new bytes(32))
(0, address(0), address(0), 0, 0, 0, new bytes(32))
);
// number of bytes in input blob excluding input payload
uint256 extraBytes = blob.length - 32;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ contract ApplicationAddressRelayTest is Test {
assertEq(address(_relay.getInputBox()), address(_inputBox));
}

function testRelayApplicationAddress(address app) public {
function testRelayApplicationAddress(uint64 chainId, address app) public {
vm.chainId(chainId); // foundry limits chain id to be less than 2^64 - 1

// Check the application's input box before
assertEq(_inputBox.getNumberOfInputs(app), 0);

// Construct the application address relay input
bytes memory payload = abi.encodePacked(app);
bytes memory input = EvmAdvanceEncoder.encode(
chainId,
app,
address(_relay),
0,
Expand Down
11 changes: 10 additions & 1 deletion onchain/rollups/test/foundry/util/EvmAdvanceEncoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {Inputs} from "contracts/common/Inputs.sol";

library EvmAdvanceEncoder {
function encode(
uint256 chainId,
address app,
address sender,
uint256 index,
Expand All @@ -16,7 +17,15 @@ library EvmAdvanceEncoder {
return
abi.encodeCall(
Inputs.EvmAdvance,
(app, sender, block.number, block.timestamp, index, payload)
(
chainId,
app,
sender,
block.number,
block.timestamp,
index,
payload
)
);
}
}

0 comments on commit 8892a88

Please sign in to comment.