diff --git a/contracts/evm/contracts/adapters/ClusterConnection.sol b/contracts/evm/contracts/adapters/ClusterConnection.sol index 10a400f7..20f13234 100644 --- a/contracts/evm/contracts/adapters/ClusterConnection.sol +++ b/contracts/evm/contracts/adapters/ClusterConnection.sol @@ -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, diff --git a/contracts/evm/test/adapters/ClusterConnection.t.sol b/contracts/evm/test/adapters/ClusterConnection.t.sol index 52a7d4c6..ac38e16c 100644 --- a/contracts/evm/test/adapters/ClusterConnection.t.sol +++ b/contracts/evm/test/adapters/ClusterConnection.t.sol @@ -264,7 +264,6 @@ contract ClusterConnectionTest is Test { nidSource, 1, RLPEncodeStruct.encodeCSMessage(message), - nidTarget, signatures ); vm.stopPrank(); @@ -383,7 +382,6 @@ contract ClusterConnectionTest is Test { nidSource, 1, RLPEncodeStruct.encodeCSMessage(message), - nidTarget, signatures ); vm.stopPrank(); diff --git a/contracts/javascore/cluster-connection/src/main/java/xcall/adapter/cluster/ClusterConnection.java b/contracts/javascore/cluster-connection/src/main/java/xcall/adapter/cluster/ClusterConnection.java index c34fdf3a..b5aacbc7 100644 --- a/contracts/javascore/cluster-connection/src/main/java/xcall/adapter/cluster/ClusterConnection.java +++ b/contracts/javascore/cluster-connection/src/main/java/xcall/adapter/cluster/ClusterConnection.java @@ -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 uniqueValidators = new ArrayList<>(); for (byte[] signature : signatures) { diff --git a/contracts/javascore/cluster-connection/src/test/java/xcall/adapter/cluster/ClusterConnectionTest.java b/contracts/javascore/cluster-connection/src/test/java/xcall/adapter/cluster/ClusterConnectionTest.java index 1b100c5f..28c691e4 100644 --- a/contracts/javascore/cluster-connection/src/test/java/xcall/adapter/cluster/ClusterConnectionTest.java +++ b/contracts/javascore/cluster-connection/src/test/java/xcall/adapter/cluster/ClusterConnectionTest.java @@ -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())); } @@ -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())); } @@ -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); @@ -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) { diff --git a/contracts/javascore/xcall-lib/src/main/java/foundation/icon/xcall/CallService.java b/contracts/javascore/xcall-lib/src/main/java/foundation/icon/xcall/CallService.java index 338fb2b0..2a99a07a 100644 --- a/contracts/javascore/xcall-lib/src/main/java/foundation/icon/xcall/CallService.java +++ b/contracts/javascore/xcall-lib/src/main/java/foundation/icon/xcall/CallService.java @@ -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(); }