Skip to content

Commit

Permalink
fix: not authorized issue by making handleMessage public (#202)
Browse files Browse the repository at this point in the history
* fix: not authorized issue by making handleMessage public

* feat: handleBTPMessage tests added

* fix: add fixes for handle Error

* fix: code order optmization in handle message

Signed-off-by: gcranju <[email protected]>

* fix:revert message added in handleRequest

* fix: versions openzeppelin fix

* fix: foundry upgrades deleted

---------

Signed-off-by: gcranju <[email protected]>
  • Loading branch information
gcranju authored Dec 15, 2023
1 parent d2995c1 commit a0de612
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
12 changes: 6 additions & 6 deletions contracts/evm/contracts/xcall/CallService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ contract CallService is IBSH, ICallService, IFeeManage, Initializable {
bytes calldata _msg
) external override {
checkService(_svc);
this.handleMessage(_from, _msg);
handleMessage(_from, _msg);
}

function handleBTPError(
Expand All @@ -292,16 +292,16 @@ contract CallService is IBSH, ICallService, IFeeManage, Initializable {
string calldata _msg
) external override {
checkService(_svc);
this.handleError(_sn);
handleError(_sn);
}
/* ========================================= */

function handleMessage(
string calldata _from,
bytes calldata _msg
) external override {
Types.CSMessage memory csMsg = _msg.decodeCSMessage();
) public override {
require(!_from.compareTo(nid), "Invalid Network ID");
Types.CSMessage memory csMsg = _msg.decodeCSMessage();
if (csMsg.msgType == Types.CS_REQUEST) {
handleRequest(_from, csMsg.payload);
} else if (csMsg.msgType == Types.CS_RESPONSE) {
Expand All @@ -316,7 +316,7 @@ contract CallService is IBSH, ICallService, IFeeManage, Initializable {

function handleError(
uint256 _sn
) external override {
) public override {
handleResponse(Types.CSMessageResponse(
_sn,
Types.CS_RESP_FAILURE
Expand Down Expand Up @@ -348,7 +348,7 @@ contract CallService is IBSH, ICallService, IFeeManage, Initializable {
) internal {
Types.CSMessageRequest memory req = msgPayload.decodeCSMessageRequest();
string memory fromNID = req.from.nid();
require(netFrom.compareTo(fromNID));
require(netFrom.compareTo(fromNID),"Invalid NID");

bytes32 dataHash = keccak256(req.data);
if (req.protocols.length > 1) {
Expand Down
28 changes: 28 additions & 0 deletions contracts/evm/test/xcall/CallService.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,34 @@ contract CallServiceTest is Test {
callService.handleMessage(iconNid, RLPEncodeStruct.encodeCSMessage(message));
}

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

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

Types.CSMessageRequest memory request = Types.CSMessageRequest(iconDapp, ParseAddress.toString(address(dapp)), 1, false, data, new string[](0));
Types.CSMessage memory message = Types.CSMessage(Types.CS_REQUEST,request.encodeCSMessageRequest());

vm.prank(address(baseConnection));
vm.expectRevert("InvalidServiceName");
callService.handleBTPMessage(iconNid, "btp", 1, RLPEncodeStruct.encodeCSMessage(message));
}

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

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

Types.CSMessageRequest memory request = Types.CSMessageRequest(iconDapp, ParseAddress.toString(address(dapp)), 1, false, data, new string[](0));
Types.CSMessage memory message = Types.CSMessage(Types.CS_REQUEST,request.encodeCSMessageRequest());

vm.expectEmit();
emit CallMessage(iconDapp, ParseAddress.toString(address(dapp)), 1, 1, data);

vm.prank(address(baseConnection));
callService.handleBTPMessage(iconNid, "xcallM", 1, RLPEncodeStruct.encodeCSMessage(message));
}

function testInvalidNid() public {
bytes memory data = bytes("test");
callService.setDefaultConnection(iconNid, address(baseConnection));
Expand Down

0 comments on commit a0de612

Please sign in to comment.