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: testcase for send call message #88

Merged
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7646a5e
remove msg encoding in encodeCSMessageResponse
sdpisreddevil Sep 14, 2023
192f77e
chore: forge init
sdpisreddevil Sep 14, 2023
7807af6
forge install: forge-std
sdpisreddevil Sep 14, 2023
4624d00
initial foundry setup
sdpisreddevil Sep 14, 2023
f459161
forge install: openzeppelin-contracts-upgradeable
sdpisreddevil Sep 14, 2023
ec65e07
forge install: openzeppelin-contracts
sdpisreddevil Sep 14, 2023
e3853a9
add contract setup script for Callservice for Xcall
sdpisreddevil Sep 14, 2023
2be2442
add initial test setup for Xcall
sdpisreddevil Sep 14, 2023
bcea8a0
add setAdmin testCases
sdpisreddevil Sep 14, 2023
5697381
add setProtocolFee and getProtocolFee testCases
sdpisreddevil Sep 14, 2023
f2743ba
add setProtocolFeeHandler and getProtocolFeeHandler testCases
sdpisreddevil Sep 14, 2023
bcaad1c
add sendCallMessage testCases
sdpisreddevil Sep 15, 2023
4ed42a4
add defaultConnection check on sendCallMessage
sdpisreddevil Sep 18, 2023
1a494b7
add defaultConnection setter for xCall
sdpisreddevil Sep 18, 2023
27e29e5
add revert messages on require()
sdpisreddevil Sep 18, 2023
7017bd9
add constant files for tests
sdpisreddevil Sep 18, 2023
8f7a65a
fix sendCallMessageSingleProtocol testCase
sdpisreddevil Sep 18, 2023
5fc5842
add `CallMessgeSent` eventLog for test
sdpisreddevil Sep 18, 2023
f32e1e4
add testCase for sendMessage using multiProtocol
sdpisreddevil Sep 18, 2023
335fb19
add testCase for sendMessage using defaultProtocol
sdpisreddevil Sep 18, 2023
fec8115
Merge branch 'fix/add-default-connection-setter' into feature/testcas…
sdpisreddevil Sep 18, 2023
d67d897
Merge branch 'feature/add-evm-multi-protocol-xcall' into feature/test…
AntonAndell Sep 19, 2023
58e1604
Fix expectCall on sendMessage TestCases
sdpisreddevil Sep 19, 2023
e6903ab
Merge branch 'feature/add-evm-multi-protocol-xcall' into feature/test…
sdpisreddevil Sep 25, 2023
d62ae02
Merge branch 'feature/add-evm-multi-protocol-xcall' into feature/test…
sdpisreddevil Sep 26, 2023
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
97 changes: 93 additions & 4 deletions contracts/evm/xcall/test/CallService.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,58 @@ 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/contracts/utils/NetworkAddress.sol";
import "@iconfoundation/btp2-solidity-library/contracts/utils/ParseAddress.sol";
import "@iconfoundation/btp2-solidity-library/contracts/utils/Integers.sol";
import "@iconfoundation/btp2-solidity-library/contracts/utils/Strings.sol";

import "@iconfoundation/btp2-solidity-library/contracts/interfaces/IConnection.sol";
import "@iconfoundation/btp2-solidity-library/contracts/interfaces/ICallServiceReceiver.sol";
import "@iconfoundation/btp2-solidity-library/contracts/interfaces/ICallService.sol";


import "../contracts/test/DAppProxySample.sol";


contract CallServiceTest is Test {
CallService public callService;
DAppProxySample public dapp;
IConnection public baseConnection;
IConnection public connection1;
IConnection public connection2;
ICallServiceReceiver public receiver;

address public owner = address(0x1111);
address public user = address(0x1234);

address public xcall;
// address public xcallSpy;
string public iconNid = "0x2.ICON";
string public ethNid = "0x1.ETH";
string public iconDapp = NetworkAddress.networkAddress(iconNid, "0xa");
string public netTo;
string public dstAccount;
string public ethDappAddress;

string public baseIconConnection = "0xb";

string[] _baseSource;
string[] _baseDestination;

string constant xcallMulti = "xcall-multi";

event CallMessageSent(
address indexed _from,
string indexed _to,
uint256 indexed _sn
);

function setUp() public {
dapp = new DAppProxySample();
ethDappAddress = NetworkAddress.networkAddress(ethNid, ParseAddress.toString(address(dapp)));

// mock when call getFee to return 0
baseConnection = IConnection(address(0x1234));
baseConnection = IConnection(address(0x0000000000000000000000000000000000000000));

_baseSource = new string[](1);
_baseSource[0] = ParseAddress.toString(address(baseConnection));
Expand Down Expand Up @@ -95,4 +108,80 @@ contract CallServiceTest is Test {
callService.setProtocolFeeHandler(user);
}

function testSendMessageSingleProtocol() public {
bytes memory data = bytes("test");
bytes memory rollbackData = bytes("");

vm.prank(address(dapp));
vm.expectEmit();
emit CallMessageSent(address(dapp), iconDapp, 1);

uint256 sn = callService.sendCallMessage{value: 0 ether}(iconDapp, data, rollbackData, _baseSource, _baseDestination);
assertEq(sn, 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.mockCall(address(dapp), abi.encodeWithSelector(baseConnection.sendMessage.selector, netTo, xcallMulti, 0, msg), abi.encode(1));
sdpisreddevil marked this conversation as resolved.
Show resolved Hide resolved

}

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);

vm.prank(address(dapp));
uint256 sn = callService.sendCallMessage{value: 0 ether}(iconDapp, data, rollbackData, sources, destinations);
assertEq(sn, 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.mockCall(address(connection1), abi.encodeWithSelector(connection1.sendMessage.selector, netTo, xcallMulti, 0, msg), abi.encode(1));
vm.mockCall(address(connection2), abi.encodeWithSelector(connection2.sendMessage.selector, netTo, xcallMulti, 0, msg), abi.encode(1));
}

function testSendMessageDefaultProtocol() public {
bytes memory data = bytes("test");
bytes memory rollbackData = bytes("");

callService.setDefaultConnection(netTo, address(baseConnection));

vm.expectEmit();
emit CallMessageSent(address(callService), iconDapp, 1);

uint256 sn = callService.sendCallMessage{value: 0 ether}(iconDapp, data, rollbackData);
assertEq(sn, 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.mockCall(address(dapp), abi.encodeWithSelector(baseConnection.sendMessage.selector, netTo, xcallMulti, 0, msg), abi.encode(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);
}

}