Skip to content

Commit

Permalink
fix: cluster connection fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Itshyphen committed Oct 25, 2024
1 parent 0b14023 commit b133c29
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 142 deletions.
17 changes: 6 additions & 11 deletions contracts/evm/contracts/adapters/ClusterConnection.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ contract ClusterConnection is Initializable, IConnection {
return validators;
}

function setValidators(address[] memory _validators, uint8 _threshold) external onlyAdmin {
function updateValidators(address[] memory _validators, uint8 _threshold) external onlyAdmin {
delete validators;
for (uint i = 0; i < _validators.length; i++) {
if(!isValidator(_validators[i]) && _validators[i] != address(0)) {
Expand Down Expand Up @@ -143,6 +143,7 @@ contract ClusterConnection is Initializable, IConnection {
bytes32 messageHash = getMessageHash(srcNetwork, _connSn, _msg);
uint signerCount = 0;
address[] memory collectedSigners = new address[](_signedMessages.length);

for (uint i = 0; i < _signedMessages.length; i++) {
address signer = recoverSigner(messageHash, _signedMessages[i]);
require(signer != address(0), "Invalid signature");
Expand Down Expand Up @@ -186,17 +187,11 @@ contract ClusterConnection is Initializable, IConnection {
}


/**
@notice Sends the message to a xCall.
@param srcNetwork String ( Network Id )
@param _connSn Integer ( connection message sn )
@param _msg Bytes ( serialized bytes of Service Message )
*/
function recvMessage(
string memory srcNetwork,
uint256 _connSn,
bytes calldata _msg
) public onlyRelayer {
) internal {
require(!receipts[srcNetwork][_connSn], "Duplicate Message");
receipts[srcNetwork][_connSn] = true;
ICallService(xCall).handleMessage(srcNetwork, _msg);
Expand Down Expand Up @@ -269,9 +264,9 @@ contract ClusterConnection is Initializable, IConnection {

function getMessageHash(string memory srcNetwork, uint256 _connSn, bytes calldata _msg) internal pure returns (bytes32) {
bytes memory rlp = abi.encodePacked(
srcNetwork.encodeString(),
_connSn.encodeUint(),
_msg.encodeBytes()
srcNetwork,
_connSn,
_msg
);
return keccak256(rlp);
}
Expand Down
138 changes: 7 additions & 131 deletions contracts/evm/test/adapters/ClusterConnection.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -133,98 +133,6 @@ contract ClusterConnectionTest is Test {
vm.stopPrank();
}

function testRecvMessage() public {
bytes memory data = bytes("test");
string memory iconDapp = NetworkAddress.networkAddress(
nidSource,
"0xa"
);
Types.CSMessageRequestV2 memory request = Types.CSMessageRequestV2(
iconDapp,
ParseAddress.toString(address(dappSource)),
1,
Types.CALL_MESSAGE_TYPE,
data,
new string[](0)
);
Types.CSMessage memory message = Types.CSMessage(
Types.CS_REQUEST,
request.encodeCSMessageRequestV2()
);

vm.startPrank(destination_relayer);
adapterTarget.recvMessage(
nidSource,
1,
RLPEncodeStruct.encodeCSMessage(message)
);
vm.stopPrank();
}

function testRecvMessageUnAuthorized() public {
bytes memory data = bytes("test");
string memory iconDapp = NetworkAddress.networkAddress(
nidSource,
"0xa"
);
Types.CSMessageRequestV2 memory request = Types.CSMessageRequestV2(
iconDapp,
ParseAddress.toString(address(dappSource)),
1,
Types.CALL_MESSAGE_TYPE,
data,
new string[](0)
);
Types.CSMessage memory message = Types.CSMessage(
Types.CS_REQUEST,
request.encodeCSMessageRequestV2()
);

vm.startPrank(user);
vm.expectRevert("OnlyRelayer");
adapterTarget.recvMessage(
nidSource,
1,
RLPEncodeStruct.encodeCSMessage(message)
);
vm.stopPrank();
}

function testRecvMessageDuplicateMsg() public {
bytes memory data = bytes("test");
string memory iconDapp = NetworkAddress.networkAddress(
nidSource,
"0xa"
);
Types.CSMessageRequestV2 memory request = Types.CSMessageRequestV2(
iconDapp,
ParseAddress.toString(address(dappSource)),
1,
Types.CALL_MESSAGE_TYPE,
data,
new string[](0)
);
Types.CSMessage memory message = Types.CSMessage(
Types.CS_REQUEST,
request.encodeCSMessageRequestV2()
);

vm.startPrank(destination_relayer);
adapterTarget.recvMessage(
nidSource,
1,
RLPEncodeStruct.encodeCSMessage(message)
);

vm.expectRevert("Duplicate Message");
adapterTarget.recvMessage(
nidSource,
1,
RLPEncodeStruct.encodeCSMessage(message)
);
vm.stopPrank();
}

function testRevertMessage() public {
vm.startPrank(destination_relayer);
vm.expectRevert("CallRequestNotFound");
Expand Down Expand Up @@ -284,39 +192,7 @@ contract ClusterConnectionTest is Test {
vm.stopPrank();

assert(source_relayer.balance == 10 ether);
}

function testGetReceipt() public {
bytes memory data = bytes("test");
string memory iconDapp = NetworkAddress.networkAddress(
nidSource,
"0xa"
);
Types.CSMessageRequestV2 memory request = Types.CSMessageRequestV2(
iconDapp,
ParseAddress.toString(address(dappSource)),
1,
Types.CALL_MESSAGE_TYPE,
data,
new string[](0)
);
Types.CSMessage memory message = Types.CSMessage(
Types.CS_REQUEST,
request.encodeCSMessageRequestV2()
);

assert(adapterTarget.getReceipt(nidSource, 1) == false);

vm.startPrank(destination_relayer);
adapterTarget.recvMessage(
nidSource,
1,
RLPEncodeStruct.encodeCSMessage(message)
);
vm.stopPrank();

assert(adapterTarget.getReceipt(nidSource, 1) == true);
}
}

function testRecvMessageWithMultiSignature() public {
bytes memory data = bytes("test");
Expand Down Expand Up @@ -347,7 +223,7 @@ contract ClusterConnectionTest is Test {
validators[1] = address(0x15d34AAf54267DB7D7c367839AAf71A00a2C6A65);
validators[2] = address(0x70997970C51812dc3A010C7d01b50e0d17dc79C8);
validators[3] = address(0xa0Ee7A142d267C1f36714E4a8F75612F20a79720);
adapterTarget.setValidators(validators, 4);
adapterTarget.updateValidators(validators, 4);
vm.stopPrank();

vm.startPrank(destination_relayer);
Expand Down Expand Up @@ -405,7 +281,7 @@ contract ClusterConnectionTest is Test {
address[] memory validators = new address[](2);
validators[0] = address(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266);
validators[1] = address(0x976EA74026E726554dB657fA54763abd0C3a0aa9);
adapterTarget.setValidators(validators, 2);
adapterTarget.updateValidators(validators, 2);
assertEq(2, adapterTarget.listValidators().length);
vm.stopPrank();
}
Expand Down Expand Up @@ -442,7 +318,7 @@ contract ClusterConnectionTest is Test {
address[] memory validators = new address[](2);
validators[0] = address(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266);
validators[1] = address(0x976EA74026E726554dB657fA54763abd0C3a0aa9);
adapterTarget.setValidators(validators, 2);
adapterTarget.updateValidators(validators, 2);
vm.stopPrank();
vm.startPrank(destination_relayer);
vm.expectRevert("Not enough valid signatures passed");
Expand All @@ -460,9 +336,9 @@ contract ClusterConnectionTest is Test {

function getMessageHash(string memory srcNetwork, uint256 _connSn, bytes memory _msg) internal pure returns (bytes32) {
bytes memory rlp = abi.encodePacked(
srcNetwork.encodeString(),
_connSn.encodeUint(),
_msg.encodeBytes()
srcNetwork,
_connSn,
_msg
);
return keccak256(rlp);
}
Expand Down

0 comments on commit b133c29

Please sign in to comment.