@@ -1515,7 +1515,10 @@ canRecoverDeposit tracer workDir backend hydraScriptsTxId =
1515
1515
where
1516
1516
hydraTracer = contramap FromHydraNode tracer
1517
1517
1518
- -- | Open a single participant head, deposit, Close and then recover it.
1518
+ -- | Open a single-participant head, perform 3 deposits, and then:
1519
+ -- 1. Close the head and recover deposit #1
1520
+ -- 2. Fanout the head and recover deposit #2
1521
+ -- 3. Open a new head and recover deposit #3
1519
1522
canRecoverDepositInAnyState :: ChainBackend backend => Tracer IO EndToEndLog -> FilePath -> backend -> [TxId ] -> IO ()
1520
1523
canRecoverDepositInAnyState tracer workDir backend hydraScriptsTxId =
1521
1524
(`finally` returnFundsToFaucet tracer backend Alice ) $ do
@@ -1529,6 +1532,7 @@ canRecoverDepositInAnyState tracer workDir backend hydraScriptsTxId =
1529
1532
chainConfigFor Alice workDir backend hydraScriptsTxId [] contestationPeriod
1530
1533
<&> setNetworkId networkId . modifyConfig (\ c -> c{depositPeriod})
1531
1534
withHydraNode hydraTracer aliceChainConfig workDir 1 aliceSk [] [1 ] $ \ n1 -> do
1535
+ -- Init the head
1532
1536
send n1 $ input " Init" []
1533
1537
headId <- waitMatch 10 n1 $ headIsInitializingWith (Set. fromList [alice])
1534
1538
@@ -1545,116 +1549,44 @@ canRecoverDepositInAnyState tracer workDir backend hydraScriptsTxId =
1545
1549
commitUTxO2 <- seedFromFaucet backend walletVk commitAmount (contramap FromFaucet tracer)
1546
1550
commitUTxO3 <- seedFromFaucet backend walletVk commitAmount (contramap FromFaucet tracer)
1547
1551
1548
- (balance <$> Backend. queryUTxOFor backend QueryTip walletVk)
1549
- `shouldReturn` lovelaceToValue (commitAmount * 3 )
1552
+ queryWalletBalance walletVk `shouldReturn` lovelaceToValue (commitAmount * 3 )
1550
1553
1551
1554
-- Increment commit #1
1552
- depositTransaction1 <-
1553
- parseUrlThrow (" POST " <> hydraNodeBaseUrl n1 <> " /commit" )
1554
- <&> setRequestBodyJSON commitUTxO1
1555
- >>= httpJSON
1556
- <&> getResponseBody
1557
-
1558
- let tx1 = signTx walletSk depositTransaction1
1559
- Backend. submitTransaction backend tx1
1560
-
1561
- deadline1 <- waitMatch 10 n1 $ \ v -> do
1562
- guard $ v ^? key " tag" == Just " CommitRecorded"
1563
- v ^? key " deadline" >>= parseMaybe parseJSON
1564
-
1565
- (selectLovelace . balance <$> Backend. queryUTxOFor backend QueryTip walletVk)
1566
- `shouldReturn` (commitAmount * 2 )
1555
+ depositReceipt1 <- increment n1 walletSk commitUTxO1
1556
+ queryWalletBalance walletVk `shouldReturn` lovelaceToValue (commitAmount * 2 )
1567
1557
1568
1558
-- Increment commit #2
1569
- depositTransaction2 <-
1570
- parseUrlThrow (" POST " <> hydraNodeBaseUrl n1 <> " /commit" )
1571
- <&> setRequestBodyJSON commitUTxO2
1572
- >>= httpJSON
1573
- <&> getResponseBody
1574
-
1575
- let tx2 = signTx walletSk depositTransaction2
1576
- Backend. submitTransaction backend tx2
1577
-
1578
- deadline2 <- waitMatch 10 n1 $ \ v -> do
1579
- guard $ v ^? key " tag" == Just " CommitRecorded"
1580
- v ^? key " deadline" >>= parseMaybe parseJSON
1581
-
1582
- (selectLovelace . balance <$> Backend. queryUTxOFor backend QueryTip walletVk)
1583
- `shouldReturn` commitAmount
1559
+ depositReceipt2 <- increment n1 walletSk commitUTxO2
1560
+ queryWalletBalance walletVk `shouldReturn` lovelaceToValue commitAmount
1584
1561
1585
1562
-- Increment commit #3
1586
- depositTransaction3 <-
1587
- parseUrlThrow (" POST " <> hydraNodeBaseUrl n1 <> " /commit" )
1588
- <&> setRequestBodyJSON commitUTxO3
1589
- >>= httpJSON
1590
- <&> getResponseBody
1591
-
1592
- let tx3 = signTx walletSk depositTransaction3
1593
- Backend. submitTransaction backend tx3
1594
-
1595
- deadline3 <- waitMatch 10 n1 $ \ v -> do
1596
- guard $ v ^? key " tag" == Just " CommitRecorded"
1597
- v ^? key " deadline" >>= parseMaybe parseJSON
1598
-
1599
- (selectLovelace . balance <$> Backend. queryUTxOFor backend QueryTip walletVk)
1600
- `shouldReturn` 0
1563
+ depositReceipt3 <- increment n1 walletSk commitUTxO3
1564
+ selectLovelace <$> queryWalletBalance walletVk `shouldReturn` 0
1601
1565
1602
- -- Close the head
1566
+ -- 1. Close the head
1603
1567
send n1 $ input " Close" []
1604
1568
1605
- deadline1' <- waitMatch (10 * blockTime) n1 $ \ v -> do
1569
+ contestationDeadline <- waitMatch (10 * blockTime) n1 $ \ v -> do
1606
1570
guard $ v ^? key " tag" == Just " HeadIsClosed"
1607
1571
v ^? key " contestationDeadline" . _JSON
1608
1572
1609
1573
-- Recover deposit #1
1610
- let path1 = BSC. unpack $ urlEncode False $ encodeUtf8 $ T. pack $ show (getTxId $ getTxBody tx1)
1611
- -- NOTE: we need to wait for the deadline to pass before we can recover the deposit
1612
- diff1 <- realToFrac . diffUTCTime deadline1 <$> getCurrentTime
1613
- threadDelay $ diff1 + 1
1574
+ recover n1 depositReceipt1 commitUTxO1
1575
+ queryWalletBalance walletVk `shouldReturn` balance commitUTxO1
1614
1576
1615
- (`shouldReturn` " OK" ) $
1616
- parseUrlThrow (" DELETE " <> hydraNodeBaseUrl n1 <> " /commits/" <> path1)
1617
- >>= httpJSON
1618
- <&> getResponseBody @ String
1619
-
1620
- waitMatch 20 n1 $ \ v -> do
1621
- guard $ v ^? key " tag" == Just " CommitRecovered"
1622
- guard $ v ^? key " recoveredUTxO" == Just (toJSON commitUTxO1)
1623
-
1624
- (balance <$> Backend. queryUTxOFor backend QueryTip walletVk)
1625
- `shouldReturn` lovelaceToValue commitAmount
1626
-
1627
- -- Fanout the head
1628
- remainingTime <- diffUTCTime deadline1' <$> getCurrentTime
1577
+ -- 2. Fanout the head
1578
+ remainingTime <- diffUTCTime contestationDeadline <$> getCurrentTime
1629
1579
waitFor hydraTracer (remainingTime + 3 * blockTime) [n1] $
1630
1580
output " ReadyToFanout" [" headId" .= headId]
1631
1581
send n1 $ input " Fanout" []
1632
1582
waitMatch (20 * blockTime) n1 $ \ v ->
1633
1583
guard $ v ^? key " tag" == Just " HeadIsFinalized"
1634
1584
1635
1585
-- Recover deposit #2
1636
- let path2 = BSC. unpack $ urlEncode False $ encodeUtf8 $ T. pack $ show (getTxId $ getTxBody tx2)
1637
- -- NOTE: we need to wait for the deadline to pass before we can recover the deposit
1638
- diff2 <- realToFrac . diffUTCTime deadline2 <$> getCurrentTime
1639
- threadDelay $ diff2 + 1
1640
-
1641
- (`shouldReturn` " OK" ) $
1642
- parseUrlThrow (" DELETE " <> hydraNodeBaseUrl n1 <> " /commits/" <> path2)
1643
- >>= httpJSON
1644
- <&> getResponseBody @ String
1645
-
1646
- waitMatch 20 n1 $ \ v -> do
1647
- guard $ v ^? key " tag" == Just " CommitRecovered"
1648
- guard $ v ^? key " recoveredUTxO" == Just (toJSON commitUTxO2)
1649
-
1650
- (balance <$> Backend. queryUTxOFor backend QueryTip walletVk)
1651
- `shouldReturn` lovelaceToValue (commitAmount * 2 )
1586
+ recover n1 depositReceipt2 commitUTxO2
1587
+ queryWalletBalance walletVk `shouldReturn` balance (commitUTxO1 <> commitUTxO2)
1652
1588
1653
- -- Assert final wallet balance
1654
- (balance <$> Backend. queryUTxOFor backend QueryTip walletVk)
1655
- `shouldReturn` balance (commitUTxO1 <> commitUTxO2)
1656
-
1657
- -- Open a new head
1589
+ -- 3. Open a new head
1658
1590
send n1 $ input " Init" []
1659
1591
headId2 <- waitMatch 10 n1 $ headIsInitializingWith (Set. fromList [alice])
1660
1592
@@ -1665,28 +1597,46 @@ canRecoverDepositInAnyState tracer workDir backend hydraScriptsTxId =
1665
1597
output " HeadIsOpen" [" utxo" .= object mempty , " headId" .= headId2]
1666
1598
1667
1599
-- Recover deposit #3
1668
- let path3 = BSC. unpack $ urlEncode False $ encodeUtf8 $ T. pack $ show (getTxId $ getTxBody tx3)
1669
- -- NOTE: we need to wait for the deadline to pass before we can recover the deposit
1670
- diff3 <- realToFrac . diffUTCTime deadline3 <$> getCurrentTime
1671
- threadDelay $ diff3 + 1
1600
+ recover n1 depositReceipt3 commitUTxO3
1601
+ queryWalletBalance walletVk `shouldReturn` balance (commitUTxO1 <> commitUTxO2 <> commitUTxO3)
1602
+ where
1603
+ hydraTracer = contramap FromHydraNode tracer
1604
+
1605
+ queryWalletBalance walletVk =
1606
+ balance <$> Backend. queryUTxOFor backend QueryTip walletVk
1672
1607
1673
- (`shouldReturn` " OK" ) $
1674
- parseUrlThrow (" DELETE " <> hydraNodeBaseUrl n1 <> " /commits/" <> path3)
1608
+ increment :: HydraClient -> SigningKey PaymentKey -> UTxO -> IO (TxId , UTCTime )
1609
+ increment n walletSk commitUTxO = do
1610
+ depositTransaction <-
1611
+ parseUrlThrow (" POST " <> hydraNodeBaseUrl n <> " /commit" )
1612
+ <&> setRequestBodyJSON commitUTxO
1675
1613
>>= httpJSON
1676
- <&> getResponseBody @ String
1614
+ <&> getResponseBody
1677
1615
1678
- waitMatch 20 n1 $ \ v -> do
1679
- guard $ v ^? key " tag" == Just " CommitRecovered"
1680
- guard $ v ^? key " recoveredUTxO" == Just (toJSON commitUTxO3)
1616
+ let tx = signTx walletSk depositTransaction
1617
+ Backend. submitTransaction backend tx
1681
1618
1682
- (balance <$> Backend. queryUTxOFor backend QueryTip walletVk)
1683
- `shouldReturn` lovelaceToValue (commitAmount * 3 )
1619
+ deadline <- waitMatch 10 n $ \ v -> do
1620
+ guard $ v ^? key " tag" == Just " CommitRecorded"
1621
+ v ^? key " deadline" >>= parseMaybe parseJSON
1684
1622
1685
- -- Assert final wallet balance
1686
- (balance <$> Backend. queryUTxOFor backend QueryTip walletVk)
1687
- `shouldReturn` balance (commitUTxO1 <> commitUTxO2 <> commitUTxO3)
1688
- where
1689
- hydraTracer = contramap FromHydraNode tracer
1623
+ pure (getTxId $ getTxBody tx, deadline)
1624
+
1625
+ recover :: HydraClient -> (TxId , UTCTime ) -> UTxO -> IO ()
1626
+ recover n (depositId, deadline) commitUTxO = do
1627
+ -- NOTE: we need to wait for the deadline to pass before we can recover the deposit
1628
+ diff <- realToFrac . diffUTCTime deadline <$> getCurrentTime
1629
+ threadDelay $ diff + 1
1630
+
1631
+ let path = BSC. unpack $ urlEncode False $ encodeUtf8 $ T. pack $ show depositId
1632
+ (`shouldReturn` " OK" ) $
1633
+ parseUrlThrow (" DELETE " <> hydraNodeBaseUrl n <> " /commits/" <> path)
1634
+ >>= httpJSON
1635
+ <&> getResponseBody @ String
1636
+
1637
+ waitMatch 20 n $ \ v -> do
1638
+ guard $ v ^? key " tag" == Just " CommitRecovered"
1639
+ guard $ v ^? key " recoveredUTxO" == Just (toJSON commitUTxO)
1690
1640
1691
1641
-- | Make sure to be able to see pending deposits.
1692
1642
canSeePendingDeposits :: ChainBackend backend => Tracer IO EndToEndLog -> FilePath -> NominalDiffTime -> backend -> [TxId ] -> IO ()
0 commit comments