Skip to content

Commit

Permalink
Remove _createMessageID in favor of mock value
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkaplan13 committed Dec 22, 2023
1 parent 8c3598a commit 7defce5
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 42 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ contract ERC20BridgeTest is Test {
0xf9FA4a0c696b659328DDaaBCB46Ae4eBFC9e68e4;
address public constant WARP_PRECOMPILE_ADDRESS =
address(0x0200000000000000000000000000000000000005);
bytes32 internal constant _MOCK_MESSAGE_ID =
bytes32(hex"1111111111111111111111111111111111111111111111111111111111111111");
bytes32 private constant _MOCK_BLOCKCHAIN_ID = bytes32(uint256(123456));
bytes32 private constant _DEFAULT_OTHER_CHAIN_ID =
bytes32(hex"abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcd");
Expand Down Expand Up @@ -192,11 +194,10 @@ contract ERC20BridgeTest is Test {
)
});

bytes32 mockMessageID = _createMessageID(42);
vm.mockCall(
MOCK_TELEPORTER_MESSENGER_ADDRESS,
abi.encodeCall(ITeleporterMessenger.sendCrossChainMessage, (expectedMessageInput)),
abi.encode(mockMessageID)
abi.encode(_MOCK_MESSAGE_ID)
);
vm.expectCall(
MOCK_TELEPORTER_MESSENGER_ADDRESS,
Expand All @@ -207,7 +208,7 @@ contract ERC20BridgeTest is Test {
emit BridgeTokens({
tokenContractAddress: address(mockERC20),
destinationBlockchainID: _DEFAULT_OTHER_CHAIN_ID,
teleporterMessageID: mockMessageID,
teleporterMessageID: _MOCK_MESSAGE_ID,
destinationBridgeAddress: _DEFAULT_OTHER_BRIDGE_ADDRESS,
recipient: _DEFAULT_RECIPIENT,
amount: totalAmount
Expand Down Expand Up @@ -301,11 +302,10 @@ contract ERC20BridgeTest is Test {
abi.encodeCall(IERC20.approve, (MOCK_TELEPORTER_MESSENGER_ADDRESS, feeAmount))
);

bytes32 mockMessageID = _createMessageID(42);
vm.mockCall(
MOCK_TELEPORTER_MESSENGER_ADDRESS,
abi.encodeCall(ITeleporterMessenger.sendCrossChainMessage, (expectedMessageInput)),
abi.encode(mockMessageID)
abi.encode(_MOCK_MESSAGE_ID)
);
vm.expectCall(
MOCK_TELEPORTER_MESSENGER_ADDRESS,
Expand All @@ -316,7 +316,7 @@ contract ERC20BridgeTest is Test {
emit BridgeTokens({
tokenContractAddress: address(mockERC20),
destinationBlockchainID: _DEFAULT_OTHER_CHAIN_ID,
teleporterMessageID: mockMessageID,
teleporterMessageID: _MOCK_MESSAGE_ID,
destinationBridgeAddress: _DEFAULT_OTHER_BRIDGE_ADDRESS,
recipient: _DEFAULT_RECIPIENT,
amount: bridgeAmount
Expand Down Expand Up @@ -378,11 +378,10 @@ contract ERC20BridgeTest is Test {
abi.encodeCall(IERC20.approve, (MOCK_TELEPORTER_MESSENGER_ADDRESS, bridgeFeeAmount))
);

bytes32 mockMessageID = _createMessageID(42);
vm.mockCall(
MOCK_TELEPORTER_MESSENGER_ADDRESS,
abi.encodeCall(ITeleporterMessenger.sendCrossChainMessage, (expectedMessageInput)),
abi.encode(mockMessageID)
abi.encode(_MOCK_MESSAGE_ID)
);
vm.expectCall(
MOCK_TELEPORTER_MESSENGER_ADDRESS,
Expand All @@ -393,7 +392,7 @@ contract ERC20BridgeTest is Test {
emit BridgeTokens({
tokenContractAddress: address(mockERC20),
destinationBlockchainID: _DEFAULT_OTHER_CHAIN_ID,
teleporterMessageID: mockMessageID,
teleporterMessageID: _MOCK_MESSAGE_ID,
destinationBridgeAddress: _DEFAULT_OTHER_BRIDGE_ADDRESS,
recipient: _DEFAULT_RECIPIENT,
amount: expectedBridgeAmount
Expand Down Expand Up @@ -586,11 +585,10 @@ contract ERC20BridgeTest is Test {
)
});

bytes32 mockMessageID = _createMessageID(456);
vm.mockCall(
MOCK_TELEPORTER_MESSENGER_ADDRESS,
abi.encodeCall(ITeleporterMessenger.sendCrossChainMessage, (expectedMessageInput)),
abi.encode(mockMessageID)
abi.encode(_MOCK_MESSAGE_ID)
);
vm.expectCall(
MOCK_TELEPORTER_MESSENGER_ADDRESS,
Expand All @@ -599,7 +597,10 @@ contract ERC20BridgeTest is Test {

vm.expectEmit(true, true, true, true, address(erc20Bridge));
emit SubmitCreateBridgeToken(
destinationBlockchainID, destinationBridgeAddress, nativeContractAddress, mockMessageID
destinationBlockchainID,
destinationBridgeAddress,
nativeContractAddress,
_MOCK_MESSAGE_ID
);

erc20Bridge.submitCreateBridgeToken(
Expand Down Expand Up @@ -645,11 +646,6 @@ contract ERC20BridgeTest is Test {
vm.expectCall(tokenContract, abi.encodeCall(token.decimals, ()));
}

function _createMessageID(uint256 messageNonce) private pure returns (bytes32) {
return
sha256(abi.encode(MOCK_TELEPORTER_MESSENGER_ADDRESS, _MOCK_BLOCKCHAIN_ID, messageNonce));
}

function _deriveExpectedContractAddress(
address creator,
uint8 nonce
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ contract ERC20TokenSourceTest is NativeTokenBridgeTest {
vm.mockCall(
MOCK_TELEPORTER_MESSENGER_ADDRESS,
abi.encodeWithSelector(ITeleporterMessenger.sendCrossChainMessage.selector),
abi.encode(_createMessageID(1))
abi.encode(_MOCK_MESSAGE_ID)
);

vm.expectCall(
Expand Down Expand Up @@ -68,7 +68,7 @@ contract ERC20TokenSourceTest is NativeTokenBridgeTest {
sender: address(this),
recipient: _DEFAULT_RECIPIENT,
amount: _DEFAULT_TRANSFER_AMOUNT,
teleporterMessageID: _createMessageID(1)
teleporterMessageID: _MOCK_MESSAGE_ID
});

TeleporterMessageInput memory expectedMessageInput = TeleporterMessageInput({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ contract NativeTokenBridgeTest is Test {
address internal constant _DEFAULT_RECIPIENT = 0xa4CEE7d1aF6aDdDD33E3b1cC680AB84fdf1b6d1d;
uint256 internal constant _DEFAULT_TRANSFER_AMOUNT = 1e18;
uint256 internal constant _DEFAULT_FEE_AMOUNT = 123456;

function _createMessageID(uint256 messageNonce) internal pure returns (bytes32) {
return
sha256(abi.encode(MOCK_TELEPORTER_MESSENGER_ADDRESS, _MOCK_BLOCKCHAIN_ID, messageNonce));
}
bytes32 internal constant _MOCK_MESSAGE_ID =
bytes32(hex"1111111111111111111111111111111111111111111111111111111111111111");
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ contract NativeTokenDestinationTest is NativeTokenBridgeTest {
vm.mockCall(
MOCK_TELEPORTER_MESSENGER_ADDRESS,
abi.encodeWithSelector(ITeleporterMessenger.sendCrossChainMessage.selector),
abi.encode(_createMessageID(1))
abi.encode(_MOCK_MESSAGE_ID)
);

vm.expectCall(
Expand Down Expand Up @@ -96,7 +96,7 @@ contract NativeTokenDestinationTest is NativeTokenBridgeTest {
sender: address(this),
recipient: _DEFAULT_RECIPIENT,
amount: _DEFAULT_TRANSFER_AMOUNT,
teleporterMessageID: _createMessageID(1)
teleporterMessageID: _MOCK_MESSAGE_ID
});

TeleporterMessageInput memory expectedMessageInput = TeleporterMessageInput({
Expand Down Expand Up @@ -182,7 +182,7 @@ contract NativeTokenDestinationTest is NativeTokenBridgeTest {
vm.expectEmit(true, true, true, true, address(nativeTokenDestination));
emit ReportTotalBurnedTxFees({
burnAddressBalance: burnedFees,
teleporterMessageID: _createMessageID(1)
teleporterMessageID: _MOCK_MESSAGE_ID
});

TeleporterMessageInput memory expectedMessageInput = TeleporterMessageInput({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ contract NativeTokenSourceTest is NativeTokenBridgeTest {
vm.mockCall(
MOCK_TELEPORTER_MESSENGER_ADDRESS,
abi.encodeWithSelector(ITeleporterMessenger.sendCrossChainMessage.selector),
abi.encode(_createMessageID(1))
abi.encode(_MOCK_MESSAGE_ID)
);

vm.expectCall(
Expand Down Expand Up @@ -67,7 +67,7 @@ contract NativeTokenSourceTest is NativeTokenBridgeTest {
sender: address(this),
recipient: _DEFAULT_RECIPIENT,
amount: _DEFAULT_TRANSFER_AMOUNT,
teleporterMessageID: _createMessageID(1)
teleporterMessageID: _MOCK_MESSAGE_ID
});

TeleporterMessageInput memory expectedMessageInput = TeleporterMessageInput({
Expand Down
24 changes: 12 additions & 12 deletions contracts/src/Teleporter/TeleporterMessenger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -525,18 +525,6 @@ contract TeleporterMessenger is ITeleporterMessenger, ReentrancyGuards {
return receiptQueues[originBlockchainID].getReceiptAtIndex(index);
}

/**
* @dev Calculates the message ID for the given source blockchain ID and message nonce.
*/
function calculateMessageID(
bytes32 sourceBlockchainID,
bytes32 destinationBlockchainID,
uint256 nonce
) public view returns (bytes32) {
return
keccak256(abi.encode(address(this), sourceBlockchainID, destinationBlockchainID, nonce));
}

/**
* @dev If not already set, initializes blockchainID by getting the current
* blockchain ID value from the Warp precompile.
Expand All @@ -551,6 +539,18 @@ contract TeleporterMessenger is ITeleporterMessenger, ReentrancyGuards {
return blockchainID_;
}

/**
* @dev Calculates the message ID for the given source blockchain ID and message nonce.
*/
function calculateMessageID(
bytes32 sourceBlockchainID,
bytes32 destinationBlockchainID,
uint256 nonce
) public view returns (bytes32) {
return
keccak256(abi.encode(address(this), sourceBlockchainID, destinationBlockchainID, nonce));
}

/**
* @dev Checks if a given message has been received.
* @return A boolean representing if the given message has been received or not.
Expand Down

0 comments on commit 7defce5

Please sign in to comment.