@@ -150,7 +150,65 @@ tests =
150150 -- Test tree fails
151151 (\ _ -> pure () )
152152 )
153- , testCase " spend pingPong an output succeeds" (mockchainSucceeds $ failOnError (pingPongScriptTest Scripts. Pong ))
153+ , testGroup
154+ " ping-pong"
155+ [ testCase
156+ " Ping and Pong should succeed"
157+ ( mockchainSucceeds $
158+ failOnError
159+ (pingPongMultipleRounds Scripts. Pinged [Scripts. Pong ])
160+ )
161+ , testCase
162+ " Pong and Ping should succeed"
163+ ( mockchainSucceeds $
164+ failOnError (pingPongMultipleRounds Scripts. Ponged [Scripts. Ping ])
165+ )
166+ , testCase
167+ " Ping and Ping should fail"
168+ ( mockchainFails
169+ (failOnError (pingPongMultipleRounds Scripts. Pinged [Scripts. Ping ]))
170+ -- Test tree fails
171+ (\ _ -> pure () )
172+ )
173+ , testCase
174+ " Pong and Pong should fail"
175+ ( mockchainFails
176+ (failOnError (pingPongMultipleRounds Scripts. Ponged [Scripts. Pong ]))
177+ -- Test tree fails
178+ (\ _ -> pure () )
179+ )
180+ , testCase
181+ " Stop after Ping should succeed"
182+ ( mockchainSucceeds $
183+ failOnError (pingPongMultipleRounds Scripts. Ponged [Scripts. Ping , Scripts. Stop ])
184+ )
185+ , testCase
186+ " Stop after Pong should succeed"
187+ ( mockchainSucceeds $
188+ failOnError (pingPongMultipleRounds Scripts. Pinged [Scripts. Pong , Scripts. Stop ])
189+ )
190+ , testCase
191+ " Stop after Stop should fail"
192+ ( mockchainFails
193+ (failOnError (pingPongMultipleRounds Scripts. Stopped [Scripts. Stop ]))
194+ -- Test tree fails
195+ (\ _ -> pure () )
196+ )
197+ , testCase
198+ " Ping after Stop should fail"
199+ ( mockchainFails
200+ (failOnError (pingPongMultipleRounds Scripts. Stopped [Scripts. Ping ]))
201+ -- Test tree fails
202+ (\ _ -> pure () )
203+ )
204+ , testCase
205+ " Pong after Stop should fail"
206+ ( mockchainFails
207+ (failOnError (pingPongMultipleRounds Scripts. Stopped [Scripts. Pong ]))
208+ -- Test tree fails
209+ (\ _ -> pure () )
210+ )
211+ ]
154212 ]
155213 , testGroup
156214 " mockchain"
@@ -451,7 +509,7 @@ sampleScriptTest
451509 )
452510 => Scripts. SampleRedeemer
453511 -> m ()
454- sampleScriptTest redemer = inBabbage @ era $ do
512+ sampleScriptTest redeemer = inBabbage @ era $ do
455513 let txBody =
456514 execBuildTx
457515 ( BuildTx. payToScriptDatumHash
@@ -465,55 +523,51 @@ sampleScriptTest redemer = inBabbage @era $ do
465523 input <- C. TxIn . C. getTxId . C. getTxBody <$> tryBalanceAndSubmit mempty Wallet. w1 txBody TrailingChange [] <*> pure (C. TxIx 0 )
466524
467525 -- Spend!! the outputs in a single transaction
468- _tx <- tryBalanceAndSubmit mempty Wallet. w1 (execBuildTx $ Scripts. spendSample redemer input) TrailingChange []
526+ _tx <- tryBalanceAndSubmit mempty Wallet. w1 (execBuildTx $ Scripts. spendSample redeemer input) TrailingChange []
469527 pure ()
470528
471- pingPongScriptTest
529+ pingPongMultipleRounds
472530 :: forall era m
473531 . ( MonadMockchain era m
474532 , MonadError (BalanceTxError era ) m
475533 , MonadFail m
476534 , C. IsBabbageBasedEra era
477535 , C. HasScriptLanguageInEra C. PlutusScriptV3 era
478536 )
479- => Scripts. PingPongRedeemer
537+ => Scripts. PingPongState
538+ -> [Scripts. PingPongRedeemer ]
480539 -> m ()
481- pingPongScriptTest redemer = inBabbage @ era $ do
540+ pingPongMultipleRounds fstState redeemers = inBabbage @ era $ do
541+ let value = 10_000_000
542+ -- this is the inital state and will not be validated
543+ -- we should prepare the state based on what we are about to play
482544 let txBody =
483545 execBuildTx
484546 ( BuildTx. payToScriptInlineDatum
485547 Defaults. networkId
486548 (C. hashScript (plutusScript Scripts. pingPongValidatorScript))
487- Scripts. Pinged
549+ -- we should start with Pinged if redeemer is Pong
550+ -- and Ponged if redeemer is Ping
551+ fstState
488552 C. NoStakeAddress
489- (C. lovelaceToValue 10_000_000 )
553+ (C. lovelaceToValue value )
490554 )
491- -- here is the locking !!!
492555 tx <- tryBalanceAndSubmit mempty Wallet. w1 txBody TrailingChange []
493- -- error $ show tx
494-
495- let input = C. TxIn (C. getTxId $ C. getTxBody tx) (C. TxIx 0 )
496- -- input <- C.TxBody . C.getTxId . C.getTxBody <$> tryBalanceAndSubmit mempty Wallet.w1 txBody TrailingChange [] <*> pure (C.TxIx 0)
497-
498- -- Spend!! the outputs in a single transaction
499- _tx <-
500- tryBalanceAndSubmit
501- mempty
502- Wallet. w1
503- ( execBuildTx $ do
504- Scripts. spendPingPong redemer input
505- --
506- -- here we are going to payToScript
507- BuildTx. payToScriptInlineDatum
508- Defaults. networkId
509- (C. hashScript (plutusScript Scripts. pingPongValidatorScript))
510- Scripts. Ponged
511- C. NoStakeAddress
512- (C. lovelaceToValue 10_000_000 ) -- add to the witness the datum
513- )
514- TrailingChange
515- []
556+ _ <- play value tx redeemers
516557 pure ()
558+ where
559+ play _ tx [] = pure tx
560+ play value tx (redeemer : xs) = do
561+ newTx <-
562+ tryBalanceAndSubmit
563+ mempty
564+ Wallet. w1
565+ (execBuildTx $ Scripts. playPingPongRound Defaults. networkId value redeemer (getTxIn tx))
566+ TrailingChange
567+ []
568+ play value newTx xs
569+
570+ getTxIn tx = C. TxIn (C. getTxId $ C. getTxBody tx) (C. TxIx 0 )
517571
518572scriptStakingCredential :: C. StakeCredential
519573scriptStakingCredential = C. StakeCredentialByScript $ C. hashScript (C. PlutusScript C. PlutusScriptV2 Scripts. v2StakingScript)
0 commit comments