Skip to content

Commit 3f0c1e5

Browse files
committedJan 23, 2024
fix: Fix bug in request timeout where timestamp criteria is reversed
1 parent 2a6b7d7 commit 3f0c1e5

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed
 

‎contracts/javascore/ibc/src/main/java/ibc/ics04/channel/IBCPacket.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,10 @@ public void _requestTimeout(MsgRequestTimeoutPacket msg) {
230230
boolean heightTimeout = revisionHeight.compareTo(BigInteger.ZERO) > 0
231231
&& BigInteger.valueOf(Context.getBlockHeight())
232232
.compareTo(revisionHeight) >= 0;
233-
boolean timeTimeout = packet.getTimeoutTimestamp().compareTo(BigInteger.ZERO) > 0
233+
BigInteger timeoutTimestamp = packet.getTimeoutTimestamp();
234+
boolean timeTimeout = timeoutTimestamp.compareTo(BigInteger.ZERO) > 0
234235
&& BigInteger.valueOf(Context.getBlockTimestamp())
235-
.compareTo(packet.getTimeoutTimestamp()) < 0;
236+
.compareTo(timeoutTimestamp) >= 0;
236237
Context.require(heightTimeout || timeTimeout, "Packet has not yet timed out");
237238

238239
byte[] commitmentPath = IBCCommitment.packetCommitmentPath(packet.getSourcePort(),

‎contracts/javascore/ibc/src/test/java/ibc/ics04/channel/PacketTest.java

+63-2
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ void requestTimeout_UnOrdered() {
541541
packet.invoke(owner, "setChannel", portId, channelId, baseChannel);
542542
Height timeoutHeight = new Height();
543543
timeoutHeight.setRevisionHeight(BigInteger.valueOf(sm.getBlock().getHeight()));
544-
MsgRequestTimeoutPacket timeoutPacket=new MsgRequestTimeoutPacket();
544+
MsgRequestTimeoutPacket timeoutPacket = new MsgRequestTimeoutPacket();
545545
basePacket.setTimeoutHeight(timeoutHeight);
546546
basePacket.setTimeoutTimestamp(BigInteger.valueOf(sm.getBlock().getTimestamp()));
547547
byte[] commitmentPath = IBCCommitment.packetReceiptCommitmentKey(basePacket.getSourcePort(),
@@ -563,7 +563,7 @@ void requestTimeout_Ordered() {
563563
packet.invoke(owner, "setChannel", portId, channelId, baseChannel);
564564
Height timeoutHeight = new Height();
565565
timeoutHeight.setRevisionHeight(BigInteger.valueOf(sm.getBlock().getHeight()));
566-
MsgRequestTimeoutPacket timeoutPacket=new MsgRequestTimeoutPacket();
566+
MsgRequestTimeoutPacket timeoutPacket = new MsgRequestTimeoutPacket();
567567
basePacket.setTimeoutHeight(timeoutHeight);
568568
basePacket.setTimeoutTimestamp(BigInteger.valueOf(sm.getBlock().getTimestamp()));
569569
byte[] commitmentPath = IBCCommitment.nextSequenceRecvCommitmentKey(basePacket.getSourcePort(),
@@ -579,6 +579,67 @@ void requestTimeout_Ordered() {
579579
ByteUtil.join(commitmentPath, Proto.encodeFixed64(basePacket.getSequence(), false)));
580580
}
581581

582+
@Test
583+
void requestTimeout_timestamp() {
584+
// Arrange
585+
baseChannel.setOrdering(Channel.Order.ORDER_UNORDERED);
586+
packet.invoke(owner, "setChannel", portId, channelId, baseChannel);
587+
MsgRequestTimeoutPacket timeoutPacket = new MsgRequestTimeoutPacket();
588+
basePacket.setTimeoutTimestamp(BigInteger.valueOf(sm.getBlock().getTimestamp()));
589+
byte[] commitmentPath = IBCCommitment.packetReceiptCommitmentKey(basePacket.getSourcePort(),
590+
basePacket.getSourceChannel(), basePacket.getSequence());
591+
timeoutPacket.setPacket(basePacket.encode());
592+
timeoutPacket.setProofHeight(new byte[0]);
593+
timeoutPacket.setProof(new byte[0]);
594+
// Act
595+
packet.invoke(owner, "_requestTimeout", timeoutPacket);
596+
597+
// Assert
598+
verify(packetSpy).sendBTPMessage(clientId, commitmentPath);
599+
}
600+
601+
@Test
602+
void requestTimeout_NotYetTimedOut_Timestamp() {
603+
// Arrange
604+
baseChannel.setOrdering(Channel.Order.ORDER_UNORDERED);
605+
packet.invoke(owner, "setChannel", portId, channelId, baseChannel);
606+
MsgRequestTimeoutPacket timeoutPacket = new MsgRequestTimeoutPacket();
607+
basePacket.setTimeoutTimestamp(BigInteger.valueOf(sm.getBlock().getTimestamp()).multiply(BigInteger.TWO));
608+
byte[] commitmentPath = IBCCommitment.packetReceiptCommitmentKey(basePacket.getSourcePort(),
609+
basePacket.getSourceChannel(), basePacket.getSequence());
610+
timeoutPacket.setPacket(basePacket.encode());
611+
timeoutPacket.setProofHeight(new byte[0]);
612+
timeoutPacket.setProof(new byte[0]);
613+
String expectedErrorMessage = "Packet has not yet timed out";
614+
615+
// Act
616+
Executable beforeTimeout = () -> packet.invoke(owner, "_requestTimeout", timeoutPacket);
617+
AssertionError e = assertThrows(AssertionError.class, beforeTimeout);
618+
assertTrue(e.getMessage().contains(expectedErrorMessage));
619+
}
620+
621+
@Test
622+
void requestTimeout_NotYetTimedOut_Height() {
623+
// Arrange
624+
baseChannel.setOrdering(Channel.Order.ORDER_UNORDERED);
625+
packet.invoke(owner, "setChannel", portId, channelId, baseChannel);
626+
Height timeoutHeight = new Height();
627+
timeoutHeight.setRevisionHeight(BigInteger.valueOf(sm.getBlock().getHeight()).multiply(BigInteger.TWO));
628+
MsgRequestTimeoutPacket timeoutPacket = new MsgRequestTimeoutPacket();
629+
basePacket.setTimeoutHeight(timeoutHeight);
630+
byte[] commitmentPath = IBCCommitment.packetReceiptCommitmentKey(basePacket.getSourcePort(),
631+
basePacket.getSourceChannel(), basePacket.getSequence());
632+
timeoutPacket.setPacket(basePacket.encode());
633+
timeoutPacket.setProofHeight(new byte[0]);
634+
timeoutPacket.setProof(new byte[0]);
635+
String expectedErrorMessage = "Packet has not yet timed out";
636+
637+
// Act
638+
Executable beforeTimeout = () -> packet.invoke(owner, "_requestTimeout", timeoutPacket);
639+
AssertionError e = assertThrows(AssertionError.class, beforeTimeout);
640+
assertTrue(e.getMessage().contains(expectedErrorMessage));
641+
}
642+
582643
@Test
583644
void timeoutPacket_unOrdered() {
584645
// Arrange

0 commit comments

Comments
 (0)