Skip to content

Commit

Permalink
feat: javascore dapp new message type (#248)
Browse files Browse the repository at this point in the history
* feat: new Message type added in javascore

* feat: new Message type added in javascore dapp

* fix: unallowed method removed

* feat: sendMessageAny added in javascore

* fix: message type added in dapp

* feat: add response ability

* fix: set response value sent zero
  • Loading branch information
gcranju authored Jan 24, 2024
1 parent 16a3a5a commit 7b50c81
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
import score.annotation.Payable;
import foundation.icon.xcall.CallServiceReceiver;
import foundation.icon.xcall.NetworkAddress;
import foundation.icon.xcall.messages.Message;
import foundation.icon.xcall.messages.CallMessage;
import foundation.icon.xcall.messages.CallMessageWithRollback;
import foundation.icon.xcall.messages.XCallEnvelope;
import foundation.icon.xcall.messages.PersistentMessage;

public class MultiProtocolSampleDapp implements CallServiceReceiver {
private final Address callSvc;
Expand Down Expand Up @@ -58,7 +63,6 @@ public String[] getDestinations(String nid) {
return toArray(this.destinations.at(nid));
}


public String[] toArray(ArrayDB<String> db) {
int size = db.size();
String[] arr = new String[size];
Expand All @@ -69,6 +73,40 @@ public String[] toArray(ArrayDB<String> db) {
return arr;
}

@Payable
@External
public void sendNewMessage(String _to, byte[] _data, int messageType, @Optional byte[] _rollback) {
String net = NetworkAddress.valueOf(_to).net();

Message msg;
XCallEnvelope envelope;
if (messageType == PersistentMessage.TYPE) {
msg = new PersistentMessage(_data);
envelope = new XCallEnvelope(msg, getSources(net), getDestinations(net));
_sendCall(Context.getValue(), _to, envelope.toBytes());
} else if (messageType == CallMessage.TYPE) {
msg = new CallMessage(_data);
envelope = new XCallEnvelope(msg, getSources(net), getDestinations(net));
_sendCall(Context.getValue(), _to, envelope.toBytes());
} else if (messageType == CallMessageWithRollback.TYPE) {
msg = new CallMessageWithRollback(_data, _rollback);
envelope = new XCallEnvelope(msg, getSources(net), getDestinations(net));
_sendCall(Context.getValue(), _to, envelope.toBytes());
} else {
Context.revert("invalid message type");
}
}

@Payable
@External
public void sendMessageAny(String _to, byte[] _data) {
_sendCall(Context.getValue(), _to, _data);
}

private BigInteger _sendCall(BigInteger value, String to, byte[] envelope) {
return Context.call(BigInteger.class, value, this.callSvc, "sendCall", to, envelope);
}

@Payable
@External
public void sendMessage(String _to, byte[] _data, @Optional byte[] _rollback) {
Expand All @@ -77,7 +115,8 @@ public void sendMessage(String _to, byte[] _data, @Optional byte[] _rollback) {

private BigInteger _sendCallMessage(BigInteger value, String to, byte[] data, byte[] rollback) {
String net = NetworkAddress.valueOf(to).net();
return Context.call(BigInteger.class, value, this.callSvc, "sendCallMessage", to, data, rollback, getSources(net), getDestinations(net));
return Context.call(BigInteger.class, value, this.callSvc, "sendCallMessage", to, data, rollback,
getSources(net), getDestinations(net));
}

@External
Expand All @@ -92,12 +131,15 @@ public void handleCallMessage(String _from, byte[] _data, String[] protocols) {
Context.require(equals(protocols, getSources(from.net())), "invalid protocols");

Context.require(!new String(_data).equals("rollback"), "failed");
// normal message delivery

if (new String(_data).equals("reply-response")) {
// response message
_sendCallMessage(BigInteger.ZERO, _from, new byte[] { 1, 2, 3 }, null);
}
MessageReceived(_from, _data);
}
}


@EventLog
public void MessageReceived(String _from, byte[] _data) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
import foundation.icon.xcall.DefaultCallServiceReceiver;
import foundation.icon.xcall.NetworkAddress;

import foundation.icon.xcall.messages.Message;
import foundation.icon.xcall.messages.CallMessage;
import foundation.icon.xcall.messages.CallMessageWithRollback;
import foundation.icon.xcall.messages.XCallEnvelope;
import foundation.icon.xcall.messages.PersistentMessage;

public class SimpleDapp implements DefaultCallServiceReceiver {
private final Address callSvc;

Expand All @@ -40,6 +46,27 @@ private void onlyCallService() {
Context.require(Context.getCaller().equals(this.callSvc), "onlyCallService");
}

@Payable
@External
public void sendNewMessage(String _to, byte[] _data, @Optional byte[] _rollback, @Optional boolean isPersistent) {
Message msg;
if (isPersistent) {
msg = new PersistentMessage(_data);
} else if (_rollback == null || _rollback.length == 0) {
msg = new CallMessage(_data);
} else {
msg = new CallMessageWithRollback(_data, _rollback);
}
String[] sources = new String[0];
String[] destinations = new String[0];
XCallEnvelope envelope = new XCallEnvelope(msg, sources, destinations);
_sendCall(Context.getValue(), _to, envelope.toBytes());
}

private BigInteger _sendCall(BigInteger value, String to, byte[] envelope) {
return Context.call(BigInteger.class, value, this.callSvc, "sendCall", to, envelope);
}

@Payable
@External
public void sendMessage(String _to, byte[] _data, @Optional byte[] _rollback) {
Expand Down

0 comments on commit 7b50c81

Please sign in to comment.