22pragma solidity ^ 0.8.20 ;
33
44import "./helpers/IBCTestHelper.t.sol " ;
5- import {Vm} from "forge-std/Test.sol " ;
5+ import {Vm, console2 } from "forge-std/Test.sol " ;
66import {Strings} from "@openzeppelin/contracts/utils/Strings.sol " ;
77import {Upgrade, UpgradeFields, Timeout} from "../../../contracts/proto/Channel.sol " ;
88import {LocalhostClientLib} from "../../../contracts/clients/09-localhost/LocalhostClient.sol " ;
99import {LocalhostHelper} from "../../../contracts/clients/09-localhost/LocalhostHelper.sol " ;
1010import {IIBCChannelRecvPacket, IIBCChannelAcknowledgePacket} from "../../../contracts/core/04-channel/IIBCChannel.sol " ;
11- import {IIBCChannelUpgrade, IIBCChannelUpgradeBase} from "../../../contracts/core/04-channel/IIBCChannelUpgrade.sol " ;
11+ import {IIBCChannelUpgradeBase} from "../../../contracts/core/04-channel/IIBCChannelUpgrade.sol " ;
1212import {TestIBCChannelUpgradableMockApp} from "./helpers/TestIBCChannelUpgradableMockApp.t.sol " ;
1313import {ICS04UpgradeTestHelper} from "./helpers/ICS04UpgradeTestHelper.t.sol " ;
1414import {ICS04PacketEventTestHelper} from "./helpers/ICS04PacketTestHelper.t.sol " ;
@@ -412,8 +412,8 @@ contract TestICS04Upgrade is ICS04UpgradeTestHelper, ICS04PacketEventTestHelper
412412
413413 Timeout.Data[3 ] memory timeouts = [
414414 Timeout.Data ({height: H (block .number ), timestamp: 0 }),
415- Timeout.Data ({height: H (0 ), timestamp: uint64 ( block . timestamp )}),
416- Timeout.Data ({height: H (block .number ), timestamp: uint64 ( block . timestamp )})
415+ Timeout.Data ({height: H (0 ), timestamp: getTimestamp ( 0 )}),
416+ Timeout.Data ({height: H (block .number ), timestamp: getTimestamp ( )})
417417 ];
418418 HandshakeCallbacks memory callbacks = emptyCallbacks ();
419419 callbacks.openInitAndFlushing.callback = _testUpgradeTimeoutAbortAck;
@@ -457,8 +457,8 @@ contract TestICS04Upgrade is ICS04UpgradeTestHelper, ICS04PacketEventTestHelper
457457
458458 Timeout.Data[3 ] memory timeouts = [
459459 Timeout.Data ({height: H (block .number ), timestamp: 0 }),
460- Timeout.Data ({height: H (0 ), timestamp: uint64 ( block . timestamp )}),
461- Timeout.Data ({height: H (block .number ), timestamp: uint64 ( block . timestamp )})
460+ Timeout.Data ({height: H (0 ), timestamp: getTimestamp ( )}),
461+ Timeout.Data ({height: H (block .number ), timestamp: getTimestamp ( )})
462462 ];
463463 HandshakeCallbacks memory callbacks = emptyCallbacks ();
464464 callbacks.flushingAndFlushing.callback = _testUpgradeTimeoutAbortConfirm;
@@ -497,7 +497,7 @@ contract TestICS04Upgrade is ICS04UpgradeTestHelper, ICS04PacketEventTestHelper
497497 }
498498
499499 function testUpgradeTimeoutUpgrade () public {
500- CallbacksTimeout[] memory cases = new CallbacksTimeout [](10 );
500+ CallbacksTimeout[] memory cases = new CallbacksTimeout [](16 );
501501 for (uint256 i = 0 ; i < cases.length ; i++ ) {
502502 cases[i].callbacks = emptyCallbacks ();
503503 }
@@ -511,8 +511,8 @@ contract TestICS04Upgrade is ICS04UpgradeTestHelper, ICS04PacketEventTestHelper
511511 i++ ;
512512
513513 cases[i].callbacks.flushingAndFlushing.callback = _testUpgradeTimeoutUpgradeSuccess;
514- cases[i].t0 = Timeout.Data ({height: H (0 ), timestamp: uint64 ( block . timestamp + 1 )});
515- cases[i].t1 = Timeout.Data ({height: H (0 ), timestamp: uint64 ( block . timestamp + 1 )});
514+ cases[i].t0 = Timeout.Data ({height: H (0 ), timestamp: getTimestamp ( 1 )});
515+ cases[i].t1 = Timeout.Data ({height: H (0 ), timestamp: getTimestamp ( 1 )});
516516 i++ ;
517517
518518 cases[i].callbacks.openInitAndFlushing.callback = _testUpgradeTimeoutUpgradeSuccess;
@@ -521,50 +521,85 @@ contract TestICS04Upgrade is ICS04UpgradeTestHelper, ICS04PacketEventTestHelper
521521 cases[i].t1 = Timeout.Data ({height: H (block .number + 1 ), timestamp: 0 });
522522 i++ ;
523523
524+ cases[i].callbacks.openInitAndFlushing.callback = _testUpgradeTimeoutUpgradeSuccess;
525+ cases[i].callbacks.openInitAndFlushing.reverse = true ;
526+ cases[i].t0 = Timeout.Data ({height: H (0 ), timestamp: getTimestamp (1 )});
527+ cases[i].t1 = Timeout.Data ({height: H (0 ), timestamp: getTimestamp (1 )});
528+ i++ ;
529+
524530 cases[i].callbacks.flushingAndComplete.callback = _testUpgradeTimeoutUpgradeSuccess;
525531 cases[i].callbacks.flushingAndComplete.reverse = true ;
526532 cases[i].t0 = Timeout.Data ({height: H (block .number + 1 ), timestamp: 0 });
527533 cases[i].t1 = Timeout.Data ({height: H (block .number + 1 ), timestamp: 0 });
528534 i++ ;
529535
536+ cases[i].callbacks.flushingAndComplete.callback = _testUpgradeTimeoutUpgradeSuccess;
537+ cases[i].callbacks.flushingAndComplete.reverse = true ;
538+ cases[i].t0 = Timeout.Data ({height: H (0 ), timestamp: getTimestamp (1 )});
539+ cases[i].t1 = Timeout.Data ({height: H (0 ), timestamp: getTimestamp (1 )});
540+ i++ ;
541+
530542 // ------------------------------ Failure Cases ------------------------------ //
531543
532- cases[i].callbacks.flushingAndFlushing.callback = _testUpgradeTimeoutUpgradeFailNotReached ;
544+ cases[i].callbacks.flushingAndFlushing.callback = _testUpgradeTimeoutUpgradeFailTimeoutHeightNotReached ;
533545 cases[i].t0 = Timeout.Data ({height: H (block .number + 1 ), timestamp: 0 });
534546 cases[i].t1 = Timeout.Data ({height: H (block .number + 1 ), timestamp: 0 });
535547 i++ ;
536548
537- cases[i].callbacks.flushingAndFlushing.callback = _testUpgradeTimeoutUpgradeFailNotReached ;
538- cases[i].t0 = Timeout.Data ({height: H (0 ), timestamp: uint64 ( block . timestamp + 1 )});
539- cases[i].t1 = Timeout.Data ({height: H (0 ), timestamp: uint64 ( block . timestamp + 1 )});
549+ cases[i].callbacks.flushingAndFlushing.callback = _testUpgradeTimeoutUpgradeFailTimeoutTimestampNotReached ;
550+ cases[i].t0 = Timeout.Data ({height: H (0 ), timestamp: getTimestamp ( 1 )});
551+ cases[i].t1 = Timeout.Data ({height: H (0 ), timestamp: getTimestamp ( 1 )});
540552 i++ ;
541553
542554 cases[i].callbacks.flushingAndComplete.callback = _testUpgradeTimeoutUpgradeFailReached;
543555 cases[i].t0 = Timeout.Data ({height: H (block .number + 1 ), timestamp: 0 });
544556 cases[i].t1 = Timeout.Data ({height: H (block .number + 1 ), timestamp: 0 });
545557 i++ ;
546558
559+ cases[i].callbacks.flushingAndComplete.callback = _testUpgradeTimeoutUpgradeFailReached;
560+ cases[i].t0 = Timeout.Data ({height: H (0 ), timestamp: getTimestamp (1 )});
561+ cases[i].t1 = Timeout.Data ({height: H (0 ), timestamp: getTimestamp (1 )});
562+ i++ ;
563+
547564 cases[i].callbacks.openSucAndComplete.callback = _testUpgradeTimeoutUpgradeFailReachedAlreadyUpgraded;
548565 cases[i].callbacks.openSucAndComplete.reverse = true ;
549566 cases[i].t0 = Timeout.Data ({height: H (block .number + 1 ), timestamp: 0 });
550567 cases[i].t1 = Timeout.Data ({height: H (block .number + 1 ), timestamp: 0 });
551568 i++ ;
552569
570+ cases[i].callbacks.openSucAndComplete.callback = _testUpgradeTimeoutUpgradeFailReachedAlreadyUpgraded;
571+ cases[i].callbacks.openSucAndComplete.reverse = true ;
572+ cases[i].t0 = Timeout.Data ({height: H (0 ), timestamp: getTimestamp (1 )});
573+ cases[i].t1 = Timeout.Data ({height: H (0 ), timestamp: getTimestamp (1 )});
574+ i++ ;
575+
553576 cases[i].callbacks.completeAndComplete.callback = _testUpgradeTimeoutUpgradeFailReachedAlreadyCompleted;
554577 cases[i].t0 = Timeout.Data ({height: H (block .number + 1 ), timestamp: 0 });
555578 cases[i].t1 = Timeout.Data ({height: H (block .number + 1 ), timestamp: 0 });
556579 i++ ;
557580
581+ cases[i].callbacks.completeAndComplete.callback = _testUpgradeTimeoutUpgradeFailReachedAlreadyCompleted;
582+ cases[i].t0 = Timeout.Data ({height: H (0 ), timestamp: getTimestamp (1 )});
583+ cases[i].t1 = Timeout.Data ({height: H (0 ), timestamp: getTimestamp (1 )});
584+ i++ ;
585+
558586 cases[i].callbacks.completeAndComplete.callback = _testUpgradeTimeoutUpgradeFailReachedAlreadyCompleted;
559587 cases[i].callbacks.completeAndComplete.reverse = true ;
560588 cases[i].t0 = Timeout.Data ({height: H (block .number + 1 ), timestamp: 0 });
561589 cases[i].t1 = Timeout.Data ({height: H (block .number + 1 ), timestamp: 0 });
562590 i++ ;
563591
592+ cases[i].callbacks.completeAndComplete.callback = _testUpgradeTimeoutUpgradeFailReachedAlreadyCompleted;
593+ cases[i].callbacks.completeAndComplete.reverse = true ;
594+ cases[i].t0 = Timeout.Data ({height: H (0 ), timestamp: getTimestamp (1 )});
595+ cases[i].t1 = Timeout.Data ({height: H (0 ), timestamp: getTimestamp (1 )});
596+ i++ ;
597+
564598 require (i == cases.length , "invalid number of cases " );
565599
566600 for (uint256 i = 0 ; i < cases.length ; i++ ) {
567- (uint256 height , uint256 timestamp ) = (block .number , block .timestamp );
601+ console2.log ("case: " , i);
602+ (uint256 height , uint256 timestampSec ) = (block .number , block .timestamp );
568603 (ChannelInfo memory channel0 , ChannelInfo memory channel1 ) =
569604 createMockAppLocalhostChannel (Channel.Order.ORDER_UNORDERED);
570605 handshakeUpgradeWithCallbacks (
@@ -589,7 +624,7 @@ contract TestICS04Upgrade is ICS04UpgradeTestHelper, ICS04PacketEventTestHelper
589624 );
590625 // restore the block height and timestamp
591626 vm.roll (height);
592- vm.warp (timestamp );
627+ vm.warp (timestampSec );
593628 }
594629 }
595630
@@ -1822,7 +1857,7 @@ contract TestICS04Upgrade is ICS04UpgradeTestHelper, ICS04PacketEventTestHelper
18221857 vm.roll (uint256 (timeout.height.revision_height));
18231858 }
18241859 if (timeout.timestamp != 0 ) {
1825- vm.warp (uint256 (timeout.timestamp));
1860+ vm.warp (uint256 (timeout.timestamp / 1e9 ));
18261861 }
18271862 (Channel.Data memory counterpartyChannel ,) = handler.getChannel (channel1.portId, channel1.channelId);
18281863 handler.timeoutChannelUpgrade (
@@ -1848,7 +1883,7 @@ contract TestICS04Upgrade is ICS04UpgradeTestHelper, ICS04PacketEventTestHelper
18481883 return false ;
18491884 }
18501885
1851- function _testUpgradeTimeoutUpgradeFailNotReached (
1886+ function _testUpgradeTimeoutUpgradeFailTimeoutHeightNotReached (
18521887 IIBCHandler handler ,
18531888 ChannelInfo memory channel0 ,
18541889 ChannelInfo memory channel1
@@ -1861,8 +1896,25 @@ contract TestICS04Upgrade is ICS04UpgradeTestHelper, ICS04PacketEventTestHelper
18611896 proofChannel: LocalhostClientLib.sentinelProof (),
18621897 proofHeight: H (block .number )
18631898 });
1864- // IBCChannelUpgradeTimeoutHeightNotReached or IBCChannelUpgradeTimeoutTimestampNotReached
1865- vm.expectRevert ();
1899+ vm.expectRevert (IIBCChannelUpgradeErrors.IBCChannelUpgradeTimeoutHeightNotReached.selector );
1900+ handler.timeoutChannelUpgrade (msg_);
1901+ return true ;
1902+ }
1903+
1904+ function _testUpgradeTimeoutUpgradeFailTimeoutTimestampNotReached (
1905+ IIBCHandler handler ,
1906+ ChannelInfo memory channel0 ,
1907+ ChannelInfo memory channel1
1908+ ) internal returns (bool ) {
1909+ (Channel.Data memory counterpartyChannel ,) = handler.getChannel (channel1.portId, channel1.channelId);
1910+ IIBCChannelUpgradeBase.MsgTimeoutChannelUpgrade memory msg_ = IIBCChannelUpgradeBase.MsgTimeoutChannelUpgrade ({
1911+ portId: channel0.portId,
1912+ channelId: channel0.channelId,
1913+ counterpartyChannel: counterpartyChannel,
1914+ proofChannel: LocalhostClientLib.sentinelProof (),
1915+ proofHeight: H (block .number )
1916+ });
1917+ vm.expectRevert (IIBCChannelUpgradeErrors.IBCChannelUpgradeTimeoutTimestampNotReached.selector );
18661918 handler.timeoutChannelUpgrade (msg_);
18671919 return true ;
18681920 }
@@ -1877,7 +1929,7 @@ contract TestICS04Upgrade is ICS04UpgradeTestHelper, ICS04PacketEventTestHelper
18771929 vm.roll (uint256 (timeout.height.revision_height));
18781930 }
18791931 if (timeout.timestamp != 0 ) {
1880- vm.warp (uint256 (timeout.timestamp));
1932+ vm.warp (uint256 (timeout.timestamp / 1e9 ));
18811933 }
18821934 (Channel.Data memory counterpartyChannel ,) = handler.getChannel (channel1.portId, channel1.channelId);
18831935 IIBCChannelUpgradeBase.MsgTimeoutChannelUpgrade memory msg_ = IIBCChannelUpgradeBase.MsgTimeoutChannelUpgrade ({
@@ -1923,7 +1975,7 @@ contract TestICS04Upgrade is ICS04UpgradeTestHelper, ICS04PacketEventTestHelper
19231975 vm.roll (uint256 (timeout.height.revision_height));
19241976 }
19251977 if (timeout.timestamp != 0 ) {
1926- vm.warp (uint256 (timeout.timestamp));
1978+ vm.warp (uint256 (timeout.timestamp / 1e9 ));
19271979 }
19281980 (Channel.Data memory counterpartyChannel ,) = handler.getChannel (channel1.portId, channel1.channelId);
19291981 IIBCChannelUpgradeBase.MsgTimeoutChannelUpgrade memory msg_ = IIBCChannelUpgradeBase.MsgTimeoutChannelUpgrade ({
0 commit comments