Skip to content

Commit

Permalink
feat: new message type in solidity dapp (#245)
Browse files Browse the repository at this point in the history
* feat: new message type in solidity dapp

* feat: sendMessageAny added in solidity

* fix: else case added for invalid message type

* feat: add response ability

* fix: similarity in data
  • Loading branch information
gcranju authored Jan 24, 2024
1 parent 7b50c81 commit eaa0e8f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "@iconfoundation/btp2-solidity-library/utils/ParseAddress.sol";
import "@iconfoundation/btp2-solidity-library/utils/Strings.sol";
import "@iconfoundation/btp2-solidity-library/interfaces/ICallService.sol";
import "@iconfoundation/btp2-solidity-library/interfaces/ICallServiceReceiver.sol";
import "@xcall/utils/Types.sol";

import "openzeppelin-contracts-upgradeable/contracts/proxy/utils/Initializable.sol";

Expand Down Expand Up @@ -49,6 +50,30 @@ contract MultiProtocolSampleDapp is Initializable, ICallServiceReceiver {
_sendCallMessage(msg.value, to, data, rollback);
}

function sendNewMessage(string memory to, bytes memory data, int256 messageType, bytes memory rollback) external payable {

bytes memory message;
(string memory net,) = to.parseNetworkAddress();
string[] memory _sources = getSources(net);
string[] memory _destinations = getDestinations(net);

Check warning on line 58 in contracts/evm/contracts/mocks/multi-protocol-dapp/MultiProtocolSampleDapp.sol

View check run for this annotation

Codecov / codecov/patch

contracts/evm/contracts/mocks/multi-protocol-dapp/MultiProtocolSampleDapp.sol#L55-L58

Added lines #L55 - L58 were not covered by tests

if (messageType == Types.PERSISTENT_MESSAGE_TYPE) {
message = Types.createPersistentMessage(data, _sources, _destinations);
} else if(messageType == Types.CALL_MESSAGE_TYPE) {
message = Types.createCallMessage(data, _sources, _destinations);
} else if(messageType == Types.CALL_MESSAGE_ROLLBACK_TYPE) {
require(rollback.length > 0, "InvalidRollback");
message = Types.createCallMessageWithRollback(data, rollback, _sources, _destinations);

Check warning on line 66 in contracts/evm/contracts/mocks/multi-protocol-dapp/MultiProtocolSampleDapp.sol

View check run for this annotation

Codecov / codecov/patch

contracts/evm/contracts/mocks/multi-protocol-dapp/MultiProtocolSampleDapp.sol#L60-L66

Added lines #L60 - L66 were not covered by tests
} else {
revert("InvalidMessageType");

Check warning on line 68 in contracts/evm/contracts/mocks/multi-protocol-dapp/MultiProtocolSampleDapp.sol

View check run for this annotation

Codecov / codecov/patch

contracts/evm/contracts/mocks/multi-protocol-dapp/MultiProtocolSampleDapp.sol#L68

Added line #L68 was not covered by tests
}
_sendCall(msg.value, to, message);

Check warning on line 70 in contracts/evm/contracts/mocks/multi-protocol-dapp/MultiProtocolSampleDapp.sol

View check run for this annotation

Codecov / codecov/patch

contracts/evm/contracts/mocks/multi-protocol-dapp/MultiProtocolSampleDapp.sol#L70

Added line #L70 was not covered by tests
}

function sendMessageAny(string memory to, bytes memory data) external payable {
_sendCall(msg.value, to, data);

Check warning on line 74 in contracts/evm/contracts/mocks/multi-protocol-dapp/MultiProtocolSampleDapp.sol

View check run for this annotation

Codecov / codecov/patch

contracts/evm/contracts/mocks/multi-protocol-dapp/MultiProtocolSampleDapp.sol#L74

Added line #L74 was not covered by tests
}

function _sendCallMessage(
uint256 value,
string memory to,
Expand All @@ -59,6 +84,14 @@ contract MultiProtocolSampleDapp is Initializable, ICallServiceReceiver {
ICallService(callSvc).sendCallMessage{value: value}(to, data, rollback, getSources(net), getDestinations(net));
}

function _sendCall(
uint256 value,
string memory to,
bytes memory message
) private {
ICallService(callSvc).sendCall{value: value}(to, message);

Check warning on line 92 in contracts/evm/contracts/mocks/multi-protocol-dapp/MultiProtocolSampleDapp.sol

View check run for this annotation

Codecov / codecov/patch

contracts/evm/contracts/mocks/multi-protocol-dapp/MultiProtocolSampleDapp.sol#L92

Added line #L92 was not covered by tests
}


function handleCallMessage(string memory from, bytes memory data, string[] memory protocols) external onlyCallService {
(string memory netFrom,) = from.parseNetworkAddress();
Expand All @@ -69,11 +102,14 @@ contract MultiProtocolSampleDapp is Initializable, ICallServiceReceiver {
} else {
require(protocolsEqual(protocols, getSources(netFrom)), "invalid protocols");
require(keccak256(data) != keccak256(abi.encodePacked("rollback")), "rollback");

if(keccak256(data) == keccak256(abi.encodePacked("reply-reponse"))) {
_sendCallMessage(0, from, '010203', bytes(""));

Check warning on line 107 in contracts/evm/contracts/mocks/multi-protocol-dapp/MultiProtocolSampleDapp.sol

View check run for this annotation

Codecov / codecov/patch

contracts/evm/contracts/mocks/multi-protocol-dapp/MultiProtocolSampleDapp.sol#L107

Added line #L107 was not covered by tests
}
emit MessageReceived(from, data);
}
}


function protocolsEqual(string[] memory a, string[] memory b) private pure returns (bool) {
if (a.length != b.length) {
return false;
Expand Down
1 change: 0 additions & 1 deletion contracts/evm/contracts/xcall/CallService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ contract CallService is IBSH, ICallService, IFeeManage, Initializable {
using RLPEncodeStruct for Types.CSMessage;
using RLPEncodeStruct for Types.CSMessageRequest;
using RLPEncodeStruct for Types.CSMessageResult;
using RLPEncodeStruct for Types.CallMessage;
using RLPEncodeStruct for Types.CallMessageWithRollback;
using RLPEncodeStruct for Types.XCallEnvelope;
using RLPDecodeStruct for bytes;
Expand Down
44 changes: 37 additions & 7 deletions contracts/evm/library/utils/Types.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity >=0.8.0;
import "@xcall/utils/RLPEncodeStruct.sol";
import "@xcall/utils/RLPEncodeStruct.sol";

/**
* @notice List of ALL Struct being used to Encode and Decode RLP Messages
*/
library Types {
using RLPEncodeStruct for Types.CallMessageWithRollback;
using RLPEncodeStruct for Types.XCallEnvelope;

// The name of CallService.
string constant NAME = "xcallM";

Expand Down Expand Up @@ -81,13 +86,38 @@ library Types {
bytes data;
}

function createPersistentMessageEnvelope(bytes memory data) internal pure returns (XCallEnvelope memory) {
return XCallEnvelope(
PERSISTENT_MESSAGE_TYPE,
data,
new string[](0),
new string[](0)
);
function createPersistentMessage(
bytes memory data,
string[] memory sources,
string[] memory destinations
) internal pure returns (bytes memory) {
return
XCallEnvelope(PERSISTENT_MESSAGE_TYPE, data, sources, destinations).encodeXCallEnvelope();
}

function createCallMessage(
bytes memory data,
string[] memory sources,
string[] memory destinations
) internal pure returns (bytes memory) {
return XCallEnvelope(CALL_MESSAGE_TYPE, data, sources, destinations).encodeXCallEnvelope();
}

function createCallMessageWithRollback(
bytes memory data,
bytes memory rollback,
string[] memory sources,
string[] memory destinations
) internal pure returns (bytes memory) {
Types.CallMessageWithRollback memory _msg = Types
.CallMessageWithRollback(data, rollback);

return
XCallEnvelope(
CALL_MESSAGE_ROLLBACK_TYPE,
_msg.encodeCallMessageWithRollback(),
sources,
destinations
).encodeXCallEnvelope();
}
}
6 changes: 2 additions & 4 deletions contracts/evm/test/xcall/CallService.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,8 @@ contract CallServiceTest is Test {

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

Types.XCallEnvelope memory _msg = Types.createPersistentMessageEnvelope(data);
console2.log(_msg.messageType);
bytes memory _msg = Types.createPersistentMessage(data, new string[](0), new string[](0));

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

Expand All @@ -289,7 +287,7 @@ contract CallServiceTest is Test {
vm.expectCall(address(baseConnection), abi.encodeCall(baseConnection.sendMessage, (iconNid, Types.NAME, 0, message.encodeCSMessage())));

vm.prank(address(dapp));
uint256 sn = callService.sendCall{value: 0 ether}(iconDapp, _msg.encodeXCallEnvelope());
uint256 sn = callService.sendCall{value: 0 ether}(iconDapp, _msg);
assertEq(sn, 1);
}

Expand Down

0 comments on commit eaa0e8f

Please sign in to comment.