Skip to content

Commit

Permalink
Merge branch 'main' into bsc-plato
Browse files Browse the repository at this point in the history
  • Loading branch information
bunnie307 committed Jul 10, 2023
2 parents 86eddb1 + 152fc93 commit 20aba4d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void executeRollback(BigInteger _sn) {
}

@Override
public void executeCall(BigInteger _reqId) {
Context.call(this.address, "executeCall", _reqId);
public void executeCall(BigInteger _reqId, byte[] _data) {
Context.call(this.address, "executeCall", _reqId, _data);
}
}
2 changes: 1 addition & 1 deletion xcall/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = '0.6.1'
version = '0.6.2'

dependencies {
compileOnly("foundation.icon:javaee-api:$javaeeVersion")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public interface CallService {
* Executes the requested call message.
*
* @param _reqId The request id
* @param _data The calldata
*/
@External
void executeCall(BigInteger _reqId);
void executeCall(BigInteger _reqId, byte[] _data);
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ public interface CallServiceEvent {
* @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);
void CallMessage(String _from, String _to, BigInteger _sn, BigInteger _reqId, byte[] _data);

/**
* Notifies that the call message has been executed.
Expand Down
18 changes: 13 additions & 5 deletions xcall/src/main/java/foundation/icon/btp/xcall/CallServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import score.annotation.Payable;

import java.math.BigInteger;
import java.util.Arrays;

public class CallServiceImpl implements BSH, CallService, CallServiceEvent, FeeManage {
public static final int MAX_DATA_SIZE = 2048;
Expand Down Expand Up @@ -99,6 +100,10 @@ private void cleanupCallRequest(BigInteger sn) {
requests.set(sn, null);
}

private byte[] getDataHash(byte[] data) {
return Context.hash("keccak-256", data);
}

@Override
@Payable
@External
Expand Down Expand Up @@ -141,17 +146,19 @@ public BigInteger sendCallMessage(String _to, byte[] _data, @Optional byte[] _ro

@Override
@External
public void executeCall(BigInteger _reqId) {
public void executeCall(BigInteger _reqId, byte[] _data) {
CSMessageRequest req = proxyReqs.get(_reqId);
Context.require(req != null, "InvalidRequestId");
// compare the given data hash with the saved one
Context.require(Arrays.equals(getDataHash(_data), req.getData()), "DataHashMismatch");
// cleanup
proxyReqs.set(_reqId, null);

BTPAddress from = BTPAddress.valueOf(req.getFrom());
CSMessageResponse msgRes = null;
try {
DAppProxy proxy = new DAppProxy(Address.fromString(req.getTo()));
proxy.handleCallMessage(req.getFrom(), req.getData());
proxy.handleCallMessage(req.getFrom(), _data);
msgRes = new CSMessageResponse(req.getSn(), CSMessageResponse.SUCCESS, "");
} catch (UserRevertedException e) {
int code = e.getCode();
Expand Down Expand Up @@ -201,7 +208,7 @@ public void executeRollback(BigInteger _sn) {

@Override
@EventLog(indexed=3)
public void CallMessage(String _from, String _to, BigInteger _sn, BigInteger _reqId) {}
public void CallMessage(String _from, String _to, BigInteger _sn, BigInteger _reqId, byte[] _data) {}

@Override
@EventLog(indexed=1)
Expand Down Expand Up @@ -267,11 +274,12 @@ private void handleRequest(String netFrom, BigInteger sn, byte[] data) {
String to = msgReq.getTo();

BigInteger reqId = getNextReqId();
CSMessageRequest req = new CSMessageRequest(from.toString(), to, msgReq.getSn(), msgReq.needRollback(), msgReq.getData());
CSMessageRequest req = new CSMessageRequest(from.toString(), to, msgReq.getSn(), msgReq.needRollback(),
getDataHash(msgReq.getData()));
proxyReqs.set(reqId, req);

// emit event to notify the user
CallMessage(from.toString(), to, msgReq.getSn(), reqId);
CallMessage(from.toString(), to, msgReq.getSn(), reqId, msgReq.getData());
}

private void handleResponse(String netFrom, BigInteger sn, byte[] data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ void handleBTPMessageShouldEmitCallMessage() {
assertEquals(to.account(), el.get_to());
assertEquals(srcSn, el.get_sn());
assertEquals(reqId, el.get_reqId());
assertArrayEquals(data, el.get_data());
});
MockBMCIntegrationTest.mockBMC.handleBTPMessage(
checker, csAddress,
Expand All @@ -159,15 +160,22 @@ void handleBTPMessageShouldEmitCallMessage() {
@Test
void executeCallWithoutSuccessResponse() {
var from = new BTPAddress(linkNet, sampleAddress.toString());
byte[] data = requestMap.get(srcSn).getData();

// should fail if data is not the expected one
AssertRevertedException.assertUserReverted(0, () ->
callSvc.executeCall(reqId, "fakeData".getBytes())
);

var checker = CSIntegrationTest.messageReceivedEvent((el) -> {
assertEquals(from.toString(), el.get_from());
assertArrayEquals(requestMap.get(srcSn).getData(), el.get_data());
assertArrayEquals(data, el.get_data());
}).andThen(CSIntegrationTest.callExecutedEvent((el) -> {
assertEquals(reqId, el.get_reqId());
assertEquals(CSMessageResponse.SUCCESS, el.get_code());
assertEquals("", el.get_msg());
})).andThen(MockBMCIntegrationTest.sendMessageEventShouldNotExists());
callSvc.executeCall(checker, reqId);
callSvc.executeCall(checker, reqId, data);
}

@Order(3)
Expand Down Expand Up @@ -332,7 +340,7 @@ void executeCallWithFailureResponse() {
assertEquals(CSMessageResponse.FAILURE, el.get_code());
assertEquals(response.getMsg(), el.get_msg());
}));
callSvc.executeCall(checker, reqId);
callSvc.executeCall(checker, reqId, data);
}

@Order(12)
Expand Down

0 comments on commit 20aba4d

Please sign in to comment.