From 0a133e18100348895b1275e04195a49f6da1f8fb Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 16 Feb 2024 19:16:11 +0100 Subject: [PATCH] test: check eth refunds --- .../feature/SendMessagePayment.t.sol | 2 +- .../processMessage/Reentry.call.t.sol | 2 +- .../RequirePaymentFromRelayer.t.sol | 131 ++++++++++++++++++ 3 files changed, 133 insertions(+), 2 deletions(-) diff --git a/test/IncentivizedMessageEscrow/feature/SendMessagePayment.t.sol b/test/IncentivizedMessageEscrow/feature/SendMessagePayment.t.sol index f15bc43..d2609b2 100644 --- a/test/IncentivizedMessageEscrow/feature/SendMessagePayment.t.sol +++ b/test/IncentivizedMessageEscrow/feature/SendMessagePayment.t.sol @@ -27,7 +27,7 @@ contract sendPacketPaymentTest is TestCommon { _REFUND_GAS_TO = makeAddr("Alice"); BOB = makeAddr("Bob"); escrow = new IncentivizedMockEscrow(sendLostGasTo, _DESTINATION_IDENTIFIER, SIGNER, SEND_MESSAGE_PAYMENT_COST, 0); - + application = ICrossChainReceiver(address(new MockApplication(address(escrow)))); // Set implementations to the escrow address. diff --git a/test/IncentivizedMessageEscrow/processMessage/Reentry.call.t.sol b/test/IncentivizedMessageEscrow/processMessage/Reentry.call.t.sol index f19503e..79fa2a0 100644 --- a/test/IncentivizedMessageEscrow/processMessage/Reentry.call.t.sol +++ b/test/IncentivizedMessageEscrow/processMessage/Reentry.call.t.sol @@ -61,7 +61,7 @@ contract CallReentryTest is TestCommon, ICrossChainReceiver { messageIdentifier, _DESTINATION_ADDRESS_APPLICATION, feeRecipient, - uint48(0xf8f3), // Gas used + uint48(0xf909), // Gas used uint64(1), uint8(1) ) diff --git a/test/IncentivizedMessageEscrow/processMessage/RequirePaymentFromRelayer.t.sol b/test/IncentivizedMessageEscrow/processMessage/RequirePaymentFromRelayer.t.sol index a32f3ce..e6cd2aa 100644 --- a/test/IncentivizedMessageEscrow/processMessage/RequirePaymentFromRelayer.t.sol +++ b/test/IncentivizedMessageEscrow/processMessage/RequirePaymentFromRelayer.t.sol @@ -90,5 +90,136 @@ contract sendPacketPaymentTest is TestCommon { newMessage, bytes32(abi.encodePacked(address(this))) ); + + // This doesn't fail + escrow.processPacket{value: SEND_MESSAGE_PAYMENT_COST}( + _metadata, + newMessage, + bytes32(abi.encodePacked(address(this))) + ); + } + + function test_process_message_refund_with_cost(uint32 excess) external { + IncentiveDescription storage incentive = _INCENTIVE; + + vm.recordLogs(); + (, bytes32 messageIdentifier) = escrow.submitMessage{value: _getTotalIncentive(_INCENTIVE) + SEND_MESSAGE_PAYMENT_COST}( + bytes32(uint256(0x123123) + uint256(2**255)), + _DESTINATION_ADDRESS_THIS, + _MESSAGE, + incentive, + 0 + ); + + Vm.Log[] memory entries = vm.getRecordedLogs(); + + (, , bytes memory messageWithContext) = abi.decode(entries[1].data, (bytes32, bytes, bytes)); + + + (bytes memory _metadata, bytes memory newMessage) = getVerifiedMessage(address(escrow), messageWithContext); + + // This doesn't fail + escrow.processPacket{value: SEND_MESSAGE_PAYMENT_COST + excess}( + _metadata, + newMessage, + bytes32(abi.encodePacked(address(this))) + ); + + assertEq(_receive, excess, "Didn't receive excess."); + } + + function test_process_message_reemit_with_cost(uint64 excess) external { + IncentiveDescription storage incentive = _INCENTIVE; + + vm.recordLogs(); + (, bytes32 messageIdentifier) = escrow.submitMessage{value: _getTotalIncentive(_INCENTIVE) + SEND_MESSAGE_PAYMENT_COST}( + bytes32(uint256(0x123123) + uint256(2**255)), + _DESTINATION_ADDRESS_THIS, + _MESSAGE, + incentive, + 0 + ); + + Vm.Log[] memory entries = vm.getRecordedLogs(); + + (, , bytes memory messageWithContext) = abi.decode(entries[1].data, (bytes32, bytes, bytes)); + + + (bytes memory _metadata, bytes memory newMessage) = getVerifiedMessage(address(escrow), messageWithContext); + + vm.recordLogs(); + + escrow.processPacket{value: SEND_MESSAGE_PAYMENT_COST}( + _metadata, + newMessage, + bytes32(abi.encodePacked(address(this))) + ); + + entries = vm.getRecordedLogs(); + + (, , bytes memory ackMessageWithContext) = abi.decode(entries[1].data, (bytes32, bytes, bytes)); + + bytes memory ackMessage = this.sliceMemory(ackMessageWithContext, 64); + + vm.expectRevert( + abi.encodeWithSignature( + "NotEnoughGasProvided(uint128,uint128)", + uint128(0), + uint128(SEND_MESSAGE_PAYMENT_COST) + ) + ); + escrow.reemitAckMessage(_DESTINATION_IDENTIFIER, abi.encode(address(escrow)), ackMessage); + + escrow.reemitAckMessage{value:SEND_MESSAGE_PAYMENT_COST + excess}(_DESTINATION_IDENTIFIER, abi.encode(address(escrow)), this.sliceMemory(ackMessageWithContext, 64)); + + assertEq(_receive, excess, "Didn't receive excess."); + } + + function test_process_message_timeout_with_cost(uint64 excess) external { + uint256 currentTime = 1000000; + vm.warp(1000000); + IncentiveDescription storage incentive = _INCENTIVE; + + (, bytes32 messageIdentifier) = escrow.submitMessage{value: _getTotalIncentive(_INCENTIVE) + SEND_MESSAGE_PAYMENT_COST}( + bytes32(uint256(0x123123) + uint256(2**255)), + _DESTINATION_ADDRESS_THIS, + _MESSAGE, + incentive, + 0 + ); + + bytes memory FROM_APPLICATION_START = new bytes(65); + + bytes memory mockMessage = abi.encodePacked( + bytes1(0), + bytes32(0), + FROM_APPLICATION_START, + FROM_APPLICATION_START, + uint64(currentTime), + bytes32(0), + bytes32(0), + bytes32(0) + ); + + vm.expectRevert( + abi.encodeWithSignature( + "NotEnoughGasProvided(uint128,uint128)", + uint128(0), + uint128(SEND_MESSAGE_PAYMENT_COST) + ) + ); + escrow.timeoutMessage(_DESTINATION_IDENTIFIER, abi.encode(address(escrow)), 100, mockMessage); + + escrow.timeoutMessage{value:SEND_MESSAGE_PAYMENT_COST + excess}(_DESTINATION_IDENTIFIER, abi.encode(address(escrow)), 100, mockMessage); + + assertEq(_receive, excess, "Didn't receive excess."); + } + + function sliceMemory(bytes calldata b, uint256 startSlice) external returns(bytes memory) { + return b[startSlice: ]; + } + + receive() external payable { + _receive = msg.value; } } \ No newline at end of file