@@ -2,23 +2,26 @@ module Spec.BotPlutusInterface.TxStatusChange (tests) where
22
33import BotPlutusInterface.Types (
44 ContractEnvironment (cePABConfig ),
5- PABConfig (pcOwnPubKeyHash ),
5+ PABConfig (pcOwnPubKeyHash , pcTxStatusPolling ),
6+ TxStatusPolling (spBlocksTimeOut ),
67 )
7- import Control.Lens ((&) , (.~) )
8- import Control.Monad (void )
8+ import Control.Lens ((&) , (.~) , (^.) )
99import Data.Default (def )
1010import Data.Text (Text )
1111import Data.Text qualified as Text
1212import Ledger (PaymentPubKeyHash (unPaymentPubKeyHash ), getCardanoTxId )
1313import Ledger.Ada qualified as Ada
1414import Ledger.Constraints qualified as Constraints
1515import Ledger.Tx (TxOut (TxOut ), TxOutRef (TxOutRef ))
16- import Plutus.ChainIndex (RollbackState (Unknown ), TxStatus )
16+ import Plutus.ChainIndex (RollbackState (Unknown ), Tip (TipAtGenesis ), TxStatus )
17+ import Plutus.ChainIndex.Types (Tip (Tip ))
1718import Plutus.Contract (
1819 Contract (.. ),
1920 Endpoint ,
2021 awaitTxStatusChange ,
22+ getTip ,
2123 submitTx ,
24+ throwError ,
2225 )
2326import Spec.MockContract (
2427 contractEnv ,
@@ -27,10 +30,11 @@ import Spec.MockContract (
2730 paymentPkh2 ,
2831 pkhAddr1 ,
2932 runContractPure ,
33+ tip ,
3034 utxos ,
3135 )
3236import Test.Tasty (TestTree , testGroup )
33- import Test.Tasty.HUnit (Assertion , assertFailure , testCase , (@?=) )
37+ import Test.Tasty.HUnit (Assertion , assertBool , assertFailure , testCase , (@?=) )
3438import Prelude
3539
3640tests :: TestTree
@@ -51,28 +55,45 @@ testTxFoundAndConfirmed = do
5155 pabConf = def {pcOwnPubKeyHash = unPaymentPubKeyHash paymentPkh1}
5256 contractEnv' = def {cePABConfig = pabConf}
5357
54- contract :: Contract () (Endpoint " SendAda" () ) Text ()
58+ contract :: Contract () (Endpoint " SendAda" () ) Text TxStatus
5559 contract = do
5660 let constraints =
5761 Constraints. mustPayToPubKey paymentPkh2 (Ada. lovelaceValueOf 1000 )
5862 tx <- submitTx constraints
59- void $ awaitTxStatusChange $ getCardanoTxId tx
63+ awaitTxStatusChange $ getCardanoTxId tx
6064
6165 case runContractPure contract initState of
6266 (Left err, _) -> assertFailure $ Text. unpack err
67+ (Right Unknown , _) -> assertFailure " State should not be Unknown"
6368 (Right _, _) -> pure ()
6469
6570testStopWaitingByTimeout :: Assertion
6671testStopWaitingByTimeout = do
6772 let initState =
6873 def & contractEnv .~ contractEnv'
6974 pabConf = def {pcOwnPubKeyHash = unPaymentPubKeyHash paymentPkh1}
75+ timeoutBlocks = fromIntegral . spBlocksTimeOut . pcTxStatusPolling $ pabConf
7076 contractEnv' = def {cePABConfig = pabConf}
7177
72- contract :: Contract () (Endpoint " SendAda" () ) Text TxStatus
73- contract =
74- awaitTxStatusChange nonExistingTxId
78+ contract :: Contract () (Endpoint " SendAda" () ) Text (Tip , TxStatus )
79+ contract = do
80+ awaitStartBlock <- getTip
81+ case awaitStartBlock of
82+ TipAtGenesis -> throwError " Should not happen: TipAtGenesis"
83+ tip' -> do
84+ txStatus <- awaitTxStatusChange nonExistingTxId
85+ return (tip', txStatus)
7586
7687 case runContractPure contract initState of
7788 (Left err, _) -> assertFailure $ Text. unpack err
78- (Right txStatus, _) -> txStatus @?= Unknown
89+ (Right (startTip, txStatus), state) -> do
90+ startAwaitingBlockNo <- getBlock startTip
91+ endAwaitingBlockNo <- getBlock $ state ^. tip
92+ assertBool
93+ " Current block should be GT than start + timeout"
94+ (endAwaitingBlockNo > startAwaitingBlockNo + timeoutBlocks)
95+ txStatus @?= Unknown
96+ where
97+ getBlock = \ case
98+ TipAtGenesis -> assertFailure " Should not happen: TipAtGenesis"
99+ Tip _ _ blockNo -> pure blockNo
0 commit comments