Skip to content

Commit

Permalink
fix: track acknowledged packet state (#415)
Browse files Browse the repository at this point in the history
* fix: track acknowledged packet state

* fix: return already acknowledged package

* fix: update unit tests

* fix: add comma

---------

Co-authored-by: sherpalden <[email protected]>
Co-authored-by: bcsainju <[email protected]>
Co-authored-by: Palden Sherpa <[email protected]>
  • Loading branch information
4 people authored Dec 16, 2024
1 parent a81c141 commit 8d50787
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ 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<byte[], Boolean> acknowledgedPackets = Context.newDictDB("acknowledgedPackets", Boolean.class);
private final DictDB<byte[], Packet> packets = Context.newDictDB("packets", Packet.class);

private final BranchDB<byte[], DictDB<Address, byte[]>> signatures = Context.newBranchDB("signatures",
Expand Down Expand Up @@ -130,6 +131,20 @@ public boolean packetSubmitted(
return existingSign != null;
}

@External(readonly = true)
public boolean packetAcknowledged(
String srcNetwork,
String srcContractAddress,
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();
return acknowledgedPackets.get(pktID) != null && acknowledgedPackets.get(pktID) == true;
}

@External
public void submitPacket(
String srcNetwork,
Expand All @@ -146,6 +161,10 @@ public void submitPacket(
Packet pkt = new Packet(srcNetwork, srcContractAddress, srcSn, srcHeight, dstNetwork, dstContractAddress, data);
byte[] pktID = pkt.getId();

if (acknowledgedPackets.get(pktID) != null && acknowledgedPackets.get(pktID) == true) {
return;
}

if (packets.get(pktID) == null) {
packets.set(pktID, pkt);
if (signatureThreshold.get() > 1) {
Expand Down Expand Up @@ -178,6 +197,7 @@ public void submitPacket(
pkt.getDstContractAddress(),
pkt.getData(),
encodedSigs);
acknowledgedPackets.set(pktID, true);
removePacket(pktID);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public void testPacketSubmitted_false() throws Exception {
}

@Test
public void testSubmitPacket() throws Exception {
public void testPacketAcknowledged_true() throws Exception {
String srcNetwork = "0x2.icon";
String dstNetwork = "sui";
BigInteger srcSn = BigInteger.ONE;
Expand All @@ -248,20 +248,40 @@ public void testSubmitPacket() throws Exception {
aggregator.invoke(adminAc, "setSignatureThreshold", 2);

byte[] dataHash = Context.hash("sha-256", data);
byte[] sign = relayerOne.sign(dataHash);

byte[] signOne = relayerOne.sign(dataHash);
aggregator.invoke(relayerOneAc, "submitPacket", srcNetwork,
srcContractAddress, srcSn, srcHeight, dstNetwork,
dstContractAddress,
data,
signOne);

byte[] signTwo = relayerTwo.sign(dataHash);
aggregator.invoke(relayerTwoAc, "submitPacket", srcNetwork,
srcContractAddress, srcSn, srcHeight, dstNetwork,
dstContractAddress,
data,
signTwo);

byte[][] sigs = new byte[2][];
sigs[0] = signOne;
sigs[1] = signTwo;

byte[] encodedSigs = RelayAggregator.serializeSignatures(sigs);
byte[][] decodedSigs = RelayAggregator.deserializeSignatures(encodedSigs);

assertArrayEquals(signOne, decodedSigs[0]);
assertArrayEquals(signTwo, decodedSigs[1]);

verify(aggregatorSpy).PacketAcknowledged(srcNetwork, srcContractAddress,
srcSn, srcHeight, dstNetwork,
dstContractAddress, data,
sign);
encodedSigs);

Packet pkt = new Packet(srcNetwork, srcContractAddress, srcSn, srcHeight, dstNetwork,
dstContractAddress, data);
byte[] pktID = pkt.getId();
verify(aggregatorSpy).PacketRegistered(srcNetwork, srcContractAddress, srcSn,
srcHeight, dstNetwork,
dstContractAddress, data);
verify(aggregatorSpy).setSignature(pktID, relayerOneAc.getAddress(), sign);
boolean acknowledged = (boolean) aggregator.call("packetAcknowledged",
srcNetwork,
srcContractAddress, srcSn, srcHeight, dstNetwork, dstContractAddress, data);
assertEquals(acknowledged, true);
}

@Test
Expand Down

0 comments on commit 8d50787

Please sign in to comment.