-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
055416f
commit 146d710
Showing
11 changed files
with
512 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
contracts/javascore/xcall-lib/src/main/java/foundation/icon/xcall/messages/CallMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...score/xcall-lib/src/main/java/foundation/icon/xcall/messages/CallMessageWithRollback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
contracts/javascore/xcall-lib/src/main/java/foundation/icon/xcall/messages/Message.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
107 changes: 107 additions & 0 deletions
107
...racts/javascore/xcall-lib/src/main/java/foundation/icon/xcall/messages/XCallEnvelope.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.