Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: javascore dapp new message type #248

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(Context.getValue(), _from, new byte[] { 1, 2, 3 }, null);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User BigInteger.ZERO instead of Context.getValue. Just so it clear that it is supposed to be free

}
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