Skip to content

Commit

Permalink
feat: Add message types to xCall
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonAndell committed Nov 14, 2023
1 parent 055416f commit 146d710
Show file tree
Hide file tree
Showing 11 changed files with 512 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public class CSMessage {
public static final int REQUEST = 1;
public static final int RESPONSE = 2;
public static final int RESULT = 2;

private final int type;
private final byte[] data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ public class CSMessageRequest {
private final String from;
private final String to;
private final BigInteger sn;
private final boolean rollback;
private final int type;
private byte[] data;
private final String[] protocols;


public CSMessageRequest(String from, String to, BigInteger sn, boolean rollback, byte[] data, String[] protocols) {
public CSMessageRequest(String from, String to, BigInteger sn, int type, byte[] data, String[] protocols) {
this.from = from;
this.to = to;
this.sn = sn;
this.rollback = rollback;
this.type = type;
this.data = data;
if (protocols == null) {
protocols = new String[]{};
Expand All @@ -63,8 +63,8 @@ public BigInteger getSn() {
return sn;
}

public boolean needRollback() {
return rollback;
public int getType() {
return type;
}

public byte[] getData() {
Expand All @@ -81,7 +81,7 @@ public static void writeObject(ObjectWriter w, CSMessageRequest m) {
w.write(m.to);

w.write(m.sn);
w.write(m.rollback);
w.write(m.type);
w.writeNullable(m.data);
w.beginList(m.protocols.length);
for(String protocol : m.protocols) {
Expand All @@ -97,7 +97,7 @@ public static CSMessageRequest readObject(ObjectReader r) {
r.readString(),
r.readString(),
r.readBigInteger(),
r.readBoolean(),
r.readInt(),
r.readNullable(byte[].class),
readProtocols(r)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@

import java.math.BigInteger;

public class CSMessageResponse {
public class CSMessageResult {
public static final int SUCCESS = 1;
public static final int FAILURE = 0;

private final BigInteger sn;
private final int code;

public CSMessageResponse(BigInteger sn, int code) {
public CSMessageResult(BigInteger sn, int code) {
this.sn = sn;
this.code = code;
}
Expand All @@ -43,16 +43,16 @@ public int getCode() {
return code;
}

public static void writeObject(ObjectWriter w, CSMessageResponse m) {
public static void writeObject(ObjectWriter w, CSMessageResult m) {
w.beginList(2);
w.write(m.sn);
w.write(m.code);
w.end();
}

public static CSMessageResponse readObject(ObjectReader r) {
public static CSMessageResult readObject(ObjectReader r) {
r.beginList();
CSMessageResponse m = new CSMessageResponse(
CSMessageResult m = new CSMessageResult(
r.readBigInteger(),
r.readInt()
);
Expand All @@ -62,11 +62,11 @@ public static CSMessageResponse readObject(ObjectReader r) {

public byte[] toBytes() {
ByteArrayObjectWriter writer = Context.newByteArrayObjectWriter("RLPn");
CSMessageResponse.writeObject(writer, this);
CSMessageResult.writeObject(writer, this);
return writer.toByteArray();
}

public static CSMessageResponse fromBytes(byte[] bytes) {
public static CSMessageResult fromBytes(byte[] bytes) {
ObjectReader reader = Context.newByteArrayObjectReader("RLPn", bytes);
return readObject(reader);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
import score.ObjectWriter;
import scorex.util.ArrayList;

public class CallRequest {
public class RollbackData {
private final Address from;
private final String to;
private final String[] protocols;
private final byte[] rollback;
private boolean enabled;

public CallRequest(Address from, String to, String[] protocols, byte[] rollback) {
public RollbackData(Address from, String to, String[] protocols, byte[] rollback) {
this.from = from;
this.to = to;
if (protocols == null) {
Expand All @@ -57,7 +57,7 @@ public byte[] getRollback() {
return rollback;
}

public static void writeObject(ObjectWriter w, CallRequest req) {
public static void writeObject(ObjectWriter w, RollbackData req) {
w.beginList(5);
w.write(req.from);
w.write(req.to);
Expand All @@ -71,9 +71,9 @@ public static void writeObject(ObjectWriter w, CallRequest req) {
w.end();
}

public static CallRequest readObject(ObjectReader r) {
public static RollbackData readObject(ObjectReader r) {
r.beginList();
CallRequest req = new CallRequest(
RollbackData req = new RollbackData(
r.readAddress(),
r.readString(),
readProtocols(r),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

package foundation.icon.xcall.messages;

public class CallMessage extends Message {
public static final int TYPE = 1;
private byte[] data;

public CallMessage(byte[] data) {
this.data = data;
}

public int getType() {
return TYPE;
}

public byte[] getData() {
return data;
}

public byte[] toBytes() {
return data;
}

public static CallMessage fromBytes(byte[] bytes) {
return new CallMessage(bytes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package foundation.icon.xcall.messages;

import score.ByteArrayObjectWriter;
import score.Context;
import score.ObjectReader;
import score.ObjectWriter;

public class CallMessageWithRollback extends Message {
public static final int TYPE = 2;
private byte[] data;
private byte[] rollback;
public CallMessageWithRollback(byte[] data, byte[] rollback) {
this.data = data;
this.rollback = rollback;
}

public int getType() {
return TYPE;
}

public byte[] getData() {
return data;
}

public byte[] getRollback() {
return rollback;
}

public static void writeObject(ObjectWriter w, CallMessageWithRollback call) {
w.beginList(2);
w.write(call.data);
w.write(call.rollback);
w.end();
}

public static CallMessageWithRollback readObject(ObjectReader r) {
r.beginList();
CallMessageWithRollback call = new CallMessageWithRollback(
r.readByteArray(),
r.readByteArray()
);
return call;
}

public byte[] toBytes() {
ByteArrayObjectWriter writer = Context.newByteArrayObjectWriter("RLPn");
CallMessageWithRollback.writeObject(writer, this);
return writer.toByteArray();
}

public static CallMessageWithRollback fromBytes(byte[] bytes) {
ObjectReader reader = Context.newByteArrayObjectReader("RLPn", bytes);
return readObject(reader);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package foundation.icon.xcall.messages;

public abstract class Message {
public abstract int getType();

public abstract byte[] toBytes();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package foundation.icon.xcall.messages;

import java.util.List;

import score.ByteArrayObjectWriter;
import score.Context;
import score.ObjectReader;
import score.ObjectWriter;
import scorex.util.ArrayList;

public class XCallEnvelope {
public int type;
public byte[] message;
public String[] sources = new String[]{};
public String[] destinations = new String[]{};;


public XCallEnvelope(int type, byte[] message, String[] sources, String[] destinations) {
this.type = type;
this.message = message;
this.sources = sources;
this.destinations = destinations;
}

public XCallEnvelope(Message message, String[] sources, String[] destinations) {
this.type = message.getType();
this.message = message.toBytes();
this.sources = sources;
this.destinations = destinations;
}

public XCallEnvelope(Message message) {
this.type = message.getType();
this.message = message.toBytes();
}

public int getType() {
return type;
}

public byte[] getMessage() {
return message;
}

public String[] getSources() {
return sources;
}

public String[] getDestinations() {
return destinations;
}

public static void writeObject(ObjectWriter w, XCallEnvelope envelope) {
w.beginList(3);
w.write(envelope.type);
w.write(envelope.message);
w.beginList(envelope.sources.length);
for(String protocol : envelope.sources) {
w.write(protocol);
}
w.end();
w.beginList(envelope.destinations.length);
for(String protocol : envelope.destinations) {
w.write(protocol);
}
w.end();
w.end();
}

public static XCallEnvelope readObject(ObjectReader r) {
r.beginList();
XCallEnvelope call = new XCallEnvelope(
r.readInt(),
r.readByteArray(),
readProtocols(r),
readProtocols(r)
);
return call;
}

private static String[] readProtocols(ObjectReader r) {
r.beginList();
List<String> protocolsList = new ArrayList<>();
while(r.hasNext()) {
protocolsList.add(r.readString());
}
int size = protocolsList.size();
String[] protocols = new String[size];
for(int i=0; i < size; i++) {
protocols[i] = protocolsList.get(i);
}
r.end();
return protocols;
}

public byte[] toBytes() {
ByteArrayObjectWriter writer = Context.newByteArrayObjectWriter("RLPn");
XCallEnvelope.writeObject(writer, this);
return writer.toByteArray();
}

public static XCallEnvelope fromBytes(byte[] bytes) {
ObjectReader reader = Context.newByteArrayObjectReader("RLPn", bytes);
return readObject(reader);
}

}
Loading

0 comments on commit 146d710

Please sign in to comment.