Skip to content

Commit

Permalink
fix: create packet id from the hash of entire packet && update unit t…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
sherpalden committed Dec 4, 2024
1 parent 1cea9f3 commit 9dfbb74
Show file tree
Hide file tree
Showing 3 changed files with 320 additions and 319 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.math.BigInteger;

import score.ByteArrayObjectWriter;
import score.Context;
import score.ObjectReader;
import score.ObjectWriter;
Expand Down Expand Up @@ -75,12 +76,8 @@ public Packet(String srcNetwork, String srcContractAddress, BigInteger srcSn, Bi
this.data = data;
}

public String getId() {
return createId(this.srcNetwork, this.srcContractAddress, this.srcSn);
}

public static String createId(String srcNetwork, String contractAddress, BigInteger srcSn) {
return srcNetwork + "/" + contractAddress + "/" + srcSn.toString();
public byte[] getId() {
return Context.hash("sha-256", this.toBytes());
}

/**
Expand Down Expand Up @@ -171,4 +168,10 @@ public static Packet readObject(ObjectReader r) {
r.end();
return p;
}

public byte[] toBytes() {
ByteArrayObjectWriter writer = Context.newByteArrayObjectWriter("RLPn");
Packet.writeObject(writer, this);
return writer.toByteArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public class RelayAggregator {
private final ArrayDB<Address> relayers = Context.newArrayDB("relayers", Address.class);
private final DictDB<Address, Boolean> relayersLookup = Context.newDictDB("relayersLookup", Boolean.class);

private final DictDB<String, Packet> packets = Context.newDictDB("packets", Packet.class);
private final DictDB<byte[], Packet> packets = Context.newDictDB("packets", Packet.class);

private final BranchDB<String, DictDB<Address, byte[]>> signatures = Context.newBranchDB("signatures",
private final BranchDB<byte[], DictDB<Address, byte[]>> signatures = Context.newBranchDB("signatures",
byte[].class);

public RelayAggregator(Address _admin) {
Expand Down Expand Up @@ -119,8 +119,13 @@ public boolean packetSubmitted(
Address relayer,
String srcNetwork,
String srcContractAddress,
BigInteger srcSn) {
String pktID = Packet.createId(srcNetwork, srcContractAddress, srcSn);
BigInteger srcSn,
BigInteger srcHeight,
String dstNetwork,
String dstContractAddress,
byte[] data) {
Packet pkt = new Packet(srcNetwork, srcContractAddress, srcSn, srcHeight, dstNetwork, dstContractAddress, data);
byte[] pktID = pkt.getId();
byte[] existingSign = signatures.at(pktID).get(relayer);
return existingSign != null;
}
Expand All @@ -139,7 +144,7 @@ public void submitPacket(
relayersOnly();

Packet pkt = new Packet(srcNetwork, srcContractAddress, srcSn, srcHeight, dstNetwork, dstContractAddress, data);
String pktID = pkt.getId();
byte[] pktID = pkt.getId();

if (packets.get(pktID) == null) {
packets.set(pktID, pkt);
Expand All @@ -162,7 +167,7 @@ public void submitPacket(
setSignature(pktID, Context.getCaller(), signature);

if (signatureThresholdReached(pktID)) {
byte[][] sigs = getSignatures(srcNetwork, srcContractAddress, srcSn);
byte[][] sigs = getSignatures(pktID);
byte[] encodedSigs = serializeSignatures(sigs);
PacketAcknowledged(
pkt.getSrcNetwork(),
Expand All @@ -177,8 +182,7 @@ public void submitPacket(
}
}

private byte[][] getSignatures(String srcNetwork, String srcContractAddress, BigInteger srcSn) {
String pktID = Packet.createId(srcNetwork, srcContractAddress, srcSn);
private byte[][] getSignatures(byte[] pktID) {
DictDB<Address, byte[]> signDict = signatures.at(pktID);
ArrayList<byte[]> signatureList = new ArrayList<byte[]>();

Expand All @@ -197,7 +201,7 @@ private byte[][] getSignatures(String srcNetwork, String srcContractAddress, Big
return sigs;
}

protected void setSignature(String pktID, Address addr, byte[] sign) {
protected void setSignature(byte[] pktID, Address addr, byte[] sign) {
signatures.at(pktID).set(addr, sign);
}

Expand Down Expand Up @@ -233,7 +237,7 @@ protected static byte[][] deserializeSignatures(byte[] encodedSigs) {
}

private void adminOnly() {
Context.require(Context.getCaller().equals(admin.get()), "Unauthorized: caller is not the leader relayer");
Context.require(Context.getCaller().equals(admin.get()), "Unauthorized: caller is not the admin");
}

private void relayersOnly() {
Expand Down Expand Up @@ -262,7 +266,7 @@ private void removeRelayer(Address oldRelayer) {
}
}

private Boolean signatureThresholdReached(String pktID) {
private Boolean signatureThresholdReached(byte[] pktID) {
int noOfSignatures = 0;
for (int i = 0; i < relayers.size(); i++) {
Address relayer = relayers.get(i);
Expand All @@ -274,7 +278,7 @@ private Boolean signatureThresholdReached(String pktID) {
return noOfSignatures >= signatureThreshold.get();
}

private void removePacket(String pktID) {
private void removePacket(byte[] pktID) {
packets.set(pktID, null);
DictDB<Address, byte[]> signDict = signatures.at(pktID);

Expand Down
Loading

0 comments on commit 9dfbb74

Please sign in to comment.