Skip to content

Commit

Permalink
fix: remove dstNID in recv_message for cluster evm and icon
Browse files Browse the repository at this point in the history
  • Loading branch information
sherpalden committed Dec 6, 2024
1 parent 1a16784 commit fddd175
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 124 deletions.
4 changes: 3 additions & 1 deletion contracts/evm/contracts/adapters/ClusterConnection.sol
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,15 @@ contract ClusterConnection is Initializable, IConnection {
string memory srcNetwork,
uint256 _connSn,
bytes calldata _msg,
string memory dstNetwork,
bytes[] calldata _signedMessages
) public onlyRelayer {
require(
_signedMessages.length >= validatorsThreshold,
"Not enough signatures passed"
);

string memory dstNetwork = ICallService(xCall).getNetworkId();

bytes32 messageHash = getMessageHash(
srcNetwork,
_connSn,
Expand Down
2 changes: 0 additions & 2 deletions contracts/evm/test/adapters/ClusterConnection.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ contract ClusterConnectionTest is Test {
nidSource,
1,
RLPEncodeStruct.encodeCSMessage(message),
nidTarget,
signatures
);
vm.stopPrank();
Expand Down Expand Up @@ -383,7 +382,6 @@ contract ClusterConnectionTest is Test {
nidSource,
1,
RLPEncodeStruct.encodeCSMessage(message),
nidTarget,
signatures
);
vm.stopPrank();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,12 @@ public void recvMessageWithSignatures(
String srcNetwork,
BigInteger _connSn,
byte[] msg,
String dstNetwork,
byte[][] signatures) {
OnlyRelayer();
Context.require(signatures.length >= validatorsThreshold.get().intValue(), "Not enough signatures");

String dstNetwork = Context.call(String.class, xCall.get(), "getNetworkId");

byte[] messageHash = getMessageHash(srcNetwork, _connSn, msg, dstNetwork);
List<String> uniqueValidators = new ArrayList<>();
for (byte[] signature : signatures) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,11 @@ public void testRecvMessageWithSignatures() throws Exception {
byte[][] validators = new byte[][] {
wallet.getPublicKey().toByteArray(),
};

connection.invoke(owner, "updateValidators", validators, BigInteger.ONE);
connection.invoke(source_relayer, "recvMessageWithSignatures", nidSource, BigInteger.ONE, data, nidTarget,

when(callservice.mock.getNetworkId()).thenReturn(nidTarget);
connection.invoke(source_relayer, "recvMessageWithSignatures", nidSource, BigInteger.ONE, data,
byteArray);
verify(callservice.mock).handleMessage(eq(nidSource), eq("test".getBytes()));
}
Expand All @@ -186,7 +189,8 @@ public void testRecvMessageWithMultiSignatures() throws Exception {
wallet2.getPublicKey().toByteArray(),
};
connection.invoke(owner, "updateValidators", validators, BigInteger.TWO);
connection.invoke(source_relayer, "recvMessageWithSignatures", nidSource, BigInteger.ONE, data, nidTarget,
when(callservice.mock.getNetworkId()).thenReturn(nidTarget);
connection.invoke(source_relayer, "recvMessageWithSignatures", nidSource, BigInteger.ONE, data,
byteArray);
verify(callservice.mock).handleMessage(eq(nidSource), eq("test".getBytes()));
}
Expand All @@ -204,9 +208,9 @@ public void testRecvMessageWithSignaturesNotEnoughSignatures() throws Exception
wallet2.getPublicKey().toByteArray(),
};
connection.invoke(owner, "updateValidators", validators, BigInteger.TWO);
when(callservice.mock.getNetworkId()).thenReturn(nidTarget);
UserRevertedException e = assertThrows(UserRevertedException.class,
() -> connection.invoke(source_relayer, "recvMessageWithSignatures", nidSource, BigInteger.ONE, data,
nidTarget,
byteArray));
assertEquals("Reverted(0): Not enough signatures", e.getMessage());
verifyNoInteractions(callservice.mock);
Expand All @@ -226,12 +230,12 @@ public void testRecvMessageWithSignaturesNotEnoughValidSignatures() throws Excep
wallet2.getPublicKey().toByteArray(),
};
connection.invoke(owner, "updateValidators", validators, BigInteger.TWO);

when(callservice.mock.getNetworkId()).thenReturn(nidTarget);
UserRevertedException e = assertThrows(UserRevertedException.class,
() -> connection.invoke(source_relayer, "recvMessageWithSignatures", nidSource, BigInteger.ONE, data,
nidTarget,
byteArray));
assertEquals("Reverted(0): Not enough valid signatures", e.getMessage());
verifyNoInteractions(callservice.mock);
}

public static byte[] getMessageHash(String srcNetwork, BigInteger _connSn, byte[] msg, String dstNetwork) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,119 +29,130 @@
@ScoreClient
@ScoreInterface
public interface CallService {
/**
* The name of CallService.
*/
String NAME = "xcallM";

/*======== At the source CALL_BSH ========*/
/**
* Sends a call message to the contract on the destination chain.
*
* @param _to The BTP address of the callee on the destination chain
* @param _data The calldata specific to the target contract
* @param _rollback (Optional) The data for restoring the caller state when an error occurred
* @return The serial number of the request
*/
@Payable
@External
BigInteger sendCallMessage(String _to,
byte[] _data,
@Optional byte[] _rollback,
@Optional String[] _sources,
@Optional String[] _destinations);

/**
* Handles incoming Messages.
*
* @param _from String ( Network id of source network )
* @param _msg Bytes ( serialized bytes of CallMessage )
*/
@External
void handleMessage(String _from, byte[] _msg);

/**
* Handle the error on delivering the message.
*
* @param _sn Integer ( serial number of the original message )
*/

@External
void handleError(BigInteger _sn);

/**
* Notifies that the requested call message has been sent.
*
* @param _from The chain-specific address of the caller
* @param _to The BTP address of the callee on the destination chain
* @param _sn The serial number of the request
*/
@EventLog(indexed=3)
void CallMessageSent(Address _from, String _to, BigInteger _sn);

/**
* Notifies that a response message has arrived for the `_sn` if the request was a two-way message.
*
* @param _sn The serial number of the previous request
* @param _code The response code
* {@code (0: Success, -1: Unknown generic failure, >=1: User defined error code)}
*/
@EventLog(indexed=1)
void ResponseMessage(BigInteger _sn, int _code);

/**
* Notifies the user that a rollback operation is required for the request '_sn'.
*
* @param _sn The serial number of the previous request
*/
@EventLog(indexed=1)
void RollbackMessage(BigInteger _sn);

/**
* Rollbacks the caller state of the request '_sn'.
*
* @param _sn The serial number of the previous request
*/
@External
void executeRollback(BigInteger _sn);

/**
* Notifies that the rollback has been executed.
*
* @param _sn The serial number for the rollback
*/
@EventLog(indexed=1)
void RollbackExecuted(BigInteger _sn);

/*======== At the destination CALL_BSH ========*/
/**
* Notifies the user that a new call message has arrived.
*
* @param _from The BTP address of the caller on the source chain
* @param _to A string representation of the callee address
* @param _sn The serial number of the request from the source
* @param _reqId The request id of the destination chain
* @param _data The calldata
*/
@EventLog(indexed=3)
void CallMessage(String _from, String _to, BigInteger _sn, BigInteger _reqId, byte[] _data);

/**
* Executes the requested call message.
*
* @param _reqId The request id
*/
@External
void executeCall(BigInteger _reqId, byte[] _data);

/**
* Notifies that the call message has been executed.
*
* @param _reqId The request id for the call message
* @param _code The execution result code
* {@code (0: Success, -1: Unknown generic failure, >=1: User defined error code)}
* @param _msg The result message if any
*/
@EventLog(indexed=1)
void CallExecuted(BigInteger _reqId, int _code, String _msg);
/**
* The name of CallService.
*/
String NAME = "xcallM";

/* ======== At the source CALL_BSH ======== */
/**
* Sends a call message to the contract on the destination chain.
*
* @param _to The BTP address of the callee on the destination chain
* @param _data The calldata specific to the target contract
* @param _rollback (Optional) The data for restoring the caller state when an
* error occurred
* @return The serial number of the request
*/
@Payable
@External
BigInteger sendCallMessage(String _to,
byte[] _data,
@Optional byte[] _rollback,
@Optional String[] _sources,
@Optional String[] _destinations);

/**
* Handles incoming Messages.
*
* @param _from String ( Network id of source network )
* @param _msg Bytes ( serialized bytes of CallMessage )
*/
@External
void handleMessage(String _from, byte[] _msg);

/**
* Handle the error on delivering the message.
*
* @param _sn Integer ( serial number of the original message )
*/

@External
void handleError(BigInteger _sn);

/**
* Notifies that the requested call message has been sent.
*
* @param _from The chain-specific address of the caller
* @param _to The BTP address of the callee on the destination chain
* @param _sn The serial number of the request
*/
@EventLog(indexed = 3)
void CallMessageSent(Address _from, String _to, BigInteger _sn);

/**
* Notifies that a response message has arrived for the `_sn` if the request was
* a two-way message.
*
* @param _sn The serial number of the previous request
* @param _code The response code
* {@code (0: Success, -1: Unknown generic failure, >=1: User defined error code)}
*/
@EventLog(indexed = 1)
void ResponseMessage(BigInteger _sn, int _code);

/**
* Notifies the user that a rollback operation is required for the request
* '_sn'.
*
* @param _sn The serial number of the previous request
*/
@EventLog(indexed = 1)
void RollbackMessage(BigInteger _sn);

/**
* Rollbacks the caller state of the request '_sn'.
*
* @param _sn The serial number of the previous request
*/
@External
void executeRollback(BigInteger _sn);

/**
* Notifies that the rollback has been executed.
*
* @param _sn The serial number for the rollback
*/
@EventLog(indexed = 1)
void RollbackExecuted(BigInteger _sn);

/* ======== At the destination CALL_BSH ======== */
/**
* Notifies the user that a new call message has arrived.
*
* @param _from The BTP address of the caller on the source chain
* @param _to A string representation of the callee address
* @param _sn The serial number of the request from the source
* @param _reqId The request id of the destination chain
* @param _data The calldata
*/
@EventLog(indexed = 3)
void CallMessage(String _from, String _to, BigInteger _sn, BigInteger _reqId, byte[] _data);

/**
* Executes the requested call message.
*
* @param _reqId The request id
*/
@External
void executeCall(BigInteger _reqId, byte[] _data);

/**
* Notifies that the call message has been executed.
*
* @param _reqId The request id for the call message
* @param _code The execution result code
* {@code (0: Success, -1: Unknown generic failure, >=1: User defined error code)}
* @param _msg The result message if any
*/
@EventLog(indexed = 1)
void CallExecuted(BigInteger _reqId, int _code, String _msg);

/**
* Returns the network id of the chain
*
* @return nid network id of the chain
*/
@External(readonly = true)
public String getNetworkId();
}

0 comments on commit fddd175

Please sign in to comment.