Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:add forge tests for TestSendReceiver #3055

Merged
merged 5 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions solidity/contracts/test/TestSendReceiver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ contract TestSendReceiver is IMessageRecipient {

uint256 public constant HANDLE_GAS_AMOUNT = 50_000;

event Handled(bytes32 blockHash);
event Handled(uint256 randao);

function dispatchToSelf(
IMailbox _mailbox,
Expand Down Expand Up @@ -47,14 +47,12 @@ contract TestSendReceiver is IMessageRecipient {
);
}

// for testing random failure of handle call for the agents
// if randao ends in 0, fail
function handle(uint32, bytes32, bytes calldata) external payable override {
bytes32 blockHash = previousBlockHash();
bool isBlockHashEndIn0 = uint256(blockHash) % 16 == 0;
require(!isBlockHashEndIn0, "block hash ends in 0");
emit Handled(blockHash);
}

function previousBlockHash() internal view returns (bytes32) {
return blockhash(block.number - 1);
uint256 randao = block.prevrandao;
bool doesRandaoEndIn0 = randao % 16 == 0;
require(!doesRandaoEndIn0, "randao ends in 0");
emit Handled(randao);
}
}
110 changes: 110 additions & 0 deletions solidity/test/test/TestSendReceiver.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.8.0;

import {Test} from "forge-std/Test.sol";

import {TypeCasts} from "../../contracts/libs/TypeCasts.sol";
import {TestSendReceiver} from "../../contracts/test/TestSendReceiver.sol";
import {TestMailbox} from "../../contracts/test/TestMailbox.sol";
import {TestIsm} from "../../contracts/test/TestIsm.sol";
import {TestInterchainGasPaymaster} from "../../contracts/test/TestInterchainGasPaymaster.sol";
import {TestMerkleTreeHook} from "../../contracts/test/TestMerkleTreeHook.sol";

contract TestSendReceiverTest is Test {
using TypeCasts for address;

uint32 internal constant TEST_ORIGIN_DOMAIN = 1;
uint32 internal constant TEST_DESTINATION_DOMAIN = 2;
bytes internal constant TEST_MESSAGE_CONTENT = bytes("Bonjour");
TestSendReceiver internal testSendReceiver;
TestInterchainGasPaymaster internal igp;
TestMailbox internal mailbox;
uint256 internal gasPayment;
bytes internal testMessage;

function setUp() public {
mailbox = new TestMailbox(TEST_ORIGIN_DOMAIN);
TestIsm ism = new TestIsm();
igp = new TestInterchainGasPaymaster();
TestMerkleTreeHook requiredHook = new TestMerkleTreeHook(
address(mailbox)
);
mailbox.initialize(
address(this),
address(ism),
address(igp),
address(requiredHook)
);
testSendReceiver = new TestSendReceiver();

gasPayment = mailbox.quoteDispatch(
TEST_DESTINATION_DOMAIN,
address(testSendReceiver).addressToBytes32(),
TEST_MESSAGE_CONTENT
);
testMessage = mailbox.buildOutboundMessage(
TEST_DESTINATION_DOMAIN,
address(testSendReceiver).addressToBytes32(),
TEST_MESSAGE_CONTENT
);
}

event Dispatch(
address indexed sender,
uint32 indexed destination,
bytes32 indexed recipient,
bytes message
);

function testDispatchToSelf() public {
vm.expectEmit(true, true, true, false, address(mailbox));
// sender address is the testSendReceiver which doesn't match sender
// in event (not in scope for test here - tested in Mailbox.t.sol)
emit Dispatch(
address(testSendReceiver),
TEST_DESTINATION_DOMAIN,
address(testSendReceiver).addressToBytes32(),
testMessage
);
testSendReceiver.dispatchToSelf{value: gasPayment}(
mailbox,
TEST_DESTINATION_DOMAIN,
TEST_MESSAGE_CONTENT
);
}

function testDispatchToSelf_withHook() public {
vm.expectEmit(true, true, true, false, address(mailbox));
// sender address is the testSendReceiver which doesn't match sender
// in event (not in scope for test here - tested in Mailbox.t.sol)
emit Dispatch(
address(testSendReceiver),
TEST_DESTINATION_DOMAIN,
address(testSendReceiver).addressToBytes32(),
testMessage
);
testSendReceiver.dispatchToSelf{value: gasPayment}(
mailbox,
TEST_DESTINATION_DOMAIN,
TEST_MESSAGE_CONTENT,
igp
);
}

event Handled(uint256 randao);

function testHandle(bytes32 randao) public {
vm.prevrandao(randao);
if (block.prevrandao % 16 == 0) {
vm.expectRevert("randao ends in 0");
} else {
vm.expectEmit(true, true, true, false, address(testSendReceiver));
emit Handled(block.prevrandao);
}
testSendReceiver.handle(
0,
address(testSendReceiver).addressToBytes32(),
"0x1234"
);
}
}
Loading