diff --git a/contracts/evm/xcall/test/CallService.t.sol b/contracts/evm/xcall/test/CallService.t.sol index 9ca76c5c..ef7e0d5e 100644 --- a/contracts/evm/xcall/test/CallService.t.sol +++ b/contracts/evm/xcall/test/CallService.t.sol @@ -3,6 +3,8 @@ pragma solidity ^0.8.13; import "forge-std/Test.sol"; import "../contracts/CallService.sol"; +import "../contracts/libraries/Types.sol"; +import "../contracts/test/DAppProxySample.sol"; import "@iconfoundation/btp2-solidity-library/utils/NetworkAddress.sol"; import "@iconfoundation/btp2-solidity-library/utils/ParseAddress.sol"; @@ -149,6 +151,83 @@ contract CallServiceTest is Test { callService.setProtocolFeeHandler(user); } + function testSendMessageSingleProtocol() public { + bytes memory data = bytes("test"); + bytes memory rollbackData = bytes(""); + receiver = ICallServiceReceiver(address(0x02)); + + vm.prank(address(dapp)); + vm.expectEmit(); + emit CallMessageSent(address(dapp), iconDapp, 1); + + Types.CSMessageRequest memory request = Types.CSMessageRequest(ethDappAddress, dstAccount, 1, false, data, _baseSource); + Types.CSMessage memory msg = Types.CSMessage(Types.CS_REQUEST, RLPEncodeStruct.encodeCSMessageRequest(request)); + + vm.expectCall(address(baseConnection), abi.encodeWithSelector(baseConnection.sendMessage.selector)); + + uint256 sn = callService.sendCallMessage{value: 0 ether}(iconDapp, data, rollbackData, _baseSource, _baseDestination); + assertEq(sn, 1); + + } + + function testSendMessageMultiProtocol() public { + bytes memory data = bytes("test"); + bytes memory rollbackData = bytes(""); + + connection1 = IConnection(address(0x0000000000000000000000000000000000000011)); + connection2 = IConnection(address(0x0000000000000000000000000000000000000012)); + + vm.mockCall(address(connection1), abi.encodeWithSelector(connection1.getFee.selector), abi.encode(0)); + vm.mockCall(address(connection2), abi.encodeWithSelector(connection2.getFee.selector), abi.encode(0)); + + string[] memory destinations = new string[](2); + destinations[0] = "0x1icon"; + destinations[1] = "0x2icon"; + + string[] memory sources = new string[](2); + sources[0] = ParseAddress.toString(address(connection1)); + sources[1] = ParseAddress.toString(address(connection2)); + + vm.expectEmit(); + emit CallMessageSent(address(dapp), iconDapp, 1); + + Types.CSMessageRequest memory request = Types.CSMessageRequest(ethDappAddress, dstAccount, 1, false, data, destinations); + Types.CSMessage memory msg = Types.CSMessage(Types.CS_REQUEST, RLPEncodeStruct.encodeCSMessageRequest(request)); + + vm.expectCall(address(connection1), abi.encodeWithSelector(connection1.sendMessage.selector)); + vm.expectCall(address(connection2), abi.encodeWithSelector(connection2.sendMessage.selector)); + + vm.prank(address(dapp)); + uint256 sn = callService.sendCallMessage{value: 0 ether}(iconDapp, data, rollbackData, sources, destinations); + assertEq(sn, 1); + } + + function testSendMessageDefaultProtocol() public { + bytes memory data = bytes("test"); + bytes memory rollbackData = bytes(""); + + callService.setDefaultConnection(iconNid, address(baseConnection)); + + vm.expectEmit(); + emit CallMessageSent(address(callService), iconDapp, 1); + + Types.CSMessageRequest memory request = Types.CSMessageRequest(ethDappAddress, dstAccount, 1, false, data, new string[](0)); + Types.CSMessage memory msg = Types.CSMessage(Types.CS_REQUEST, RLPEncodeStruct.encodeCSMessageRequest(request)); + + vm.expectCall(address(baseConnection), abi.encodeWithSelector(baseConnection.sendMessage.selector)); + + uint256 sn = callService.sendCallMessage{value: 0 ether}(iconDapp, data, rollbackData); + assertEq(sn, 1); + } + + function testSendMessageDefaultProtocolNotSet() public { + bytes memory data = bytes("test"); + bytes memory rollbackData = bytes(""); + + vm.expectRevert("NoDefaultConnection"); + uint256 sn = callService.sendCallMessage{value: 0 ether}(iconDapp, data, rollbackData); + } + function testHandleResponseDefaultProtocol() public { bytes memory data = bytes("test");