-
Notifications
You must be signed in to change notification settings - Fork 86
/
StateSpec.hs
675 lines (609 loc) · 24.2 KB
/
StateSpec.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
{-# LANGUAGE DuplicateRecordFields #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Hydra.Chain.Direct.StateSpec where
import Hydra.Prelude hiding (label)
import Test.Hydra.Prelude
import Cardano.Api.UTxO qualified as UTxO
import Cardano.Binary (serialize)
import Data.ByteString.Lazy qualified as LBS
import Data.Set qualified as Set
import Hydra.Cardano.Api (
NetworkId (Mainnet),
PlutusScriptV2,
Tx,
TxIn,
UTxO,
findRedeemerSpending,
fromPlutusScript,
genTxIn,
hashScript,
isScriptTxOut,
lovelaceToValue,
mkScriptAddress,
modifyTxOutAddress,
modifyTxOutValue,
scriptPolicyId,
toPlutusCurrencySymbol,
toScriptData,
txInputSet,
txIns',
txOutValue,
txOuts',
utxoFromTx,
valueSize,
pattern PlutusScript,
pattern PlutusScriptSerialised,
)
import Hydra.Cardano.Api.Pretty (renderTx)
import Hydra.Chain (OnChainTx (..), PostTxError (..), maxMainnetLovelace, maximumNumberOfParties)
import Hydra.Chain.Direct.Contract.Mutation (
Mutation (..),
applyMutation,
modifyInlineDatum,
propTransactionEvaluates,
propTransactionFailsEvaluation,
replaceHeadId,
replacePolicyIdWith,
)
import Hydra.Chain.Direct.Fixture (slotLength, systemStart, testNetworkId)
import Hydra.Chain.Direct.State (
ChainContext (..),
ChainState (..),
ClosedState (..),
HasKnownUTxO (getKnownUTxO),
HydraContext (..),
InitialState (..),
OpenState (..),
abort,
closedThreadOutput,
commit,
ctxHeadParameters,
ctxParticipants,
ctxParties,
genChainStateWithTx,
genCloseTx,
genCollectComTx,
genCommitFor,
genCommits,
genCommits',
genContestTx,
genFanoutTx,
genHydraContext,
genInitTx,
genStInitial,
getContestationDeadline,
getKnownUTxO,
initialize,
observeClose,
observeCollect,
observeCommit,
pickChainContext,
unsafeAbort,
unsafeClose,
unsafeCollect,
unsafeCommit,
unsafeFanout,
unsafeObserveInitAndCommits,
)
import Hydra.Chain.Direct.State qualified as Transition
import Hydra.Chain.Direct.Tx (
AbortObservation (..),
CloseObservation (..),
ClosedThreadOutput (closedContesters),
CollectComObservation (..),
CommitObservation (..),
ContestObservation (..),
FanoutObservation (..),
HeadObservation (..),
NotAnInitReason (..),
observeCommitTx,
observeHeadTx,
observeInitTx,
)
import Hydra.ContestationPeriod (toNominalDiffTime)
import Hydra.Contract.HeadTokens qualified as HeadTokens
import Hydra.Contract.Initial qualified as Initial
import Hydra.Ledger.Cardano (
genOutput,
genTxOut,
genTxOutAdaOnly,
genTxOutByron,
genTxOutWithReferenceScript,
genUTxO1,
genUTxOSized,
)
import Hydra.Ledger.Cardano.Evaluate (
evaluateTx,
genValidityBoundsFromContestationPeriod,
maxTxSize,
)
import Hydra.Ledger.Cardano.Time (slotNoFromUTCTime)
import Hydra.Snapshot (ConfirmedSnapshot (InitialSnapshot, initialUTxO))
import Hydra.Snapshot qualified as Snapshot
import PlutusLedgerApi.Test.Examples qualified as Plutus
import PlutusLedgerApi.V2 qualified as Plutus
import Test.Aeson.GenericSpecs (roundtripAndGoldenSpecs)
import Test.QuickCheck (
Property,
Testable (property),
checkCoverage,
classify,
conjoin,
counterexample,
cover,
forAll,
forAllBlind,
forAllShow,
getPositive,
label,
sized,
sublistOf,
tabulate,
(.&&.),
(.||.),
(===),
)
import Test.QuickCheck.Monadic (monadicIO, monadicST, pick)
import Prelude qualified
spec :: Spec
spec = parallel $ do
describe "ChainState" $
roundtripAndGoldenSpecs (Proxy @ChainState)
describe "Plutus.PubKeyHash" $
roundtripAndGoldenSpecs (Proxy @Plutus.PubKeyHash)
describe "observeTx" $ do
prop "All valid transitions for all possible states can be observed." prop_observeAnyTx
describe "init" $ do
propBelowSizeLimit maxTxSize forAllInit
propIsValid forAllInit
-- XXX: This is testing observeInitTx (we will get rid of 'observeInit')
it "only proper head is observed" $
monadicIO $ do
ctx <- pickBlind (genHydraContext maximumNumberOfParties)
cctx <- pickBlind $ pickChainContext ctx
seedInput <- pickBlind arbitrary
vk <- pickBlind arbitrary
seedTxOut <- pickBlind $ genTxOutAdaOnly vk
let tx = initialize cctx seedInput (ctxParticipants ctx) (ctxHeadParameters ctx)
(mutation, cex, expected) <- pickBlind $ genInitTxMutation seedInput tx
let utxo = UTxO.singleton (seedInput, seedTxOut)
let (tx', utxo') = applyMutation mutation (tx, utxo)
originalIsObserved = property $ isRight (observeInitTx tx)
-- We expected mutated transaction to still be valid, but not observed.
mutatedIsValid = property $
case evaluateTx tx' utxo' of
Left _ -> False
Right ok
| all isRight ok -> True
| otherwise -> False
mutatedIsNotObserved =
observeInitTx tx' === Left expected
pure $
conjoin
[ originalIsObserved
& counterexample (renderTx tx)
& counterexample "Original transaction is not observed."
, mutatedIsValid
& counterexample (renderTx tx')
& counterexample "Mutated transaction is not valid."
, mutatedIsNotObserved
& counterexample (renderTx tx')
& counterexample "Should not observe mutated transaction"
]
& counterexample cex
& label (show expected)
describe "commit" $ do
propBelowSizeLimit maxTxSize forAllCommit
propIsValid forAllCommit
-- XXX: This is testing observeCommitTx. Eventually we will get rid of the
-- state-ful layer anyways.
it "only proper head is observed" $
forAllCommit' $ \ctx st committedUtxo tx ->
monadicIO $ do
let utxo = getKnownUTxO ctx <> getKnownUTxO st <> committedUtxo
mutation <- pick $ genCommitTxMutation utxo tx
let (tx', utxo') = applyMutation mutation (tx, utxo)
originalIsObserved = property $ isJust $ observeCommitTx testNetworkId utxo tx
-- We expected mutated transaction to still be valid, but not observed.
mutatedIsValid =
case evaluateTx tx' utxo' of
Left err -> property False & counterexample (show err)
Right ok
| all isRight ok -> property True
| otherwise -> property False & counterexample (show ok)
mutatedIsNotObserved =
isNothing $ observeCommitTx testNetworkId utxo' tx'
pure $
conjoin
[ originalIsObserved
& counterexample (renderTx tx)
& counterexample "Original transaction is not observed."
, mutatedIsValid
& counterexample (renderTx tx')
& counterexample "Mutated transaction is not valid."
, mutatedIsNotObserved
& counterexample (renderTx tx')
& counterexample "Should not observe mutated transaction"
]
prop "consumes all inputs that are committed" $
forAllCommit' $ \ctx st _ tx ->
case observeCommit ctx st tx of
Just (_, st') ->
let knownInputs = UTxO.inputSet (getKnownUTxO st')
in knownInputs `Set.disjoint` txInputSet tx
Nothing ->
False
prop "can only be applied / observed once" $
forAllCommit' $ \ctx st _ tx ->
case observeCommit ctx st tx of
Just (_, st') ->
case observeCommit ctx st' tx of
Just{} -> False
Nothing -> True
Nothing ->
False
prop "reject committing outputs with byron addresses" $
monadicST $ do
hctx <- pickBlind $ genHydraContext maximumNumberOfParties
(ctx, stInitial@InitialState{headId}) <- pickBlind $ genStInitial hctx
utxo <- pick $ genUTxO1 genTxOutByron
pure $
case commit ctx headId (getKnownUTxO stInitial) utxo of
Left UnsupportedLegacyOutput{} -> property True
_ -> property False
prop "reject Commits with more than maxMainnetLovelace Lovelace" $
monadicST $ do
hctx <- pickBlind $ genHydraContext maximumNumberOfParties
(ctx, stInitial@InitialState{headId}) <- pickBlind $ genStInitial hctx
utxo <- pickBlind genAdaOnlyUTxOOnMainnetWithAmountBiggerThanOutLimit
let mainnetChainContext = ctx{networkId = Mainnet}
pure $
case commit mainnetChainContext headId (getKnownUTxO stInitial) utxo of
Left CommittedTooMuchADAForMainnet{userCommittedLovelace, mainnetLimitLovelace} ->
-- check that user committed more than our limit but also use 'maxMainnetLovelace'
-- to be sure we didn't construct 'CommittedTooMuchADAForMainnet' wrongly
property $ userCommittedLovelace > mainnetLimitLovelace && userCommittedLovelace > maxMainnetLovelace
_ -> property False
describe "abort" $ do
propBelowSizeLimit maxTxSize forAllAbort
propIsValid forAllAbort
-- XXX: This is something we should test for all tx creation functions.
-- Maybe extend the forAllXXX generators to work on artificially duplicated,
-- compatible UTxOs.
prop "can create valid abort transactions for any observed head" $
monadicST $ do
hctx <- pickBlind $ genHydraContext maximumNumberOfParties
ctx <- pickBlind $ pickChainContext hctx
-- Generate a head in initialized state
(initTx1, seed1) <- pickBlind $ genInitTxWithSeed hctx
-- Generate another head in initialized state
(initTx2, seed2) <- pickBlind $ genInitTxWithSeed hctx
-- Expect to create abort transactions for either head
let utxo = getKnownUTxO ctx <> utxoFromTx initTx1 <> utxoFromTx initTx2
let propIsValidAbortTx res =
case res of
Left err -> property False & counterexample ("Failed to create abort: " <> show err)
Right tx -> propTransactionEvaluates (tx, utxo)
pure $
conjoin
[ propIsValidAbortTx (abort ctx seed1 utxo mempty)
& counterexample "AbortTx of head 1"
, propIsValidAbortTx (abort ctx seed2 utxo mempty)
& counterexample "AbortTx of head 2"
]
describe "collectCom" $ do
propBelowSizeLimit maxTxSize forAllCollectCom
propIsValid forAllCollectCom
describe "close" $ do
propBelowSizeLimit maxTxSize forAllClose
propIsValid forAllClose
describe "contest" $ do
propBelowSizeLimit maxTxSize forAllContest
propIsValid forAllContest
describe "fanout" $ do
propBelowSizeLimit maxTxSize forAllFanout
propIsValid forAllFanout
describe "acceptance" $ do
it "can close & fanout every collected head" $ do
prop_canCloseFanoutEveryCollect
genInitTxMutation :: TxIn -> Tx -> Gen (Mutation, String, NotAnInitReason)
genInitTxMutation seedInput tx =
genChangeMintingPolicy
where
genChangeMintingPolicy =
pure
( Changes $
ChangeMintingPolicy alwaysSucceedsV2
: fmap changeMintingPolicy (zip changedOutputsValue [0 ..])
, "new minting policy: " <> show (hashScript $ PlutusScript alwaysSucceedsV2)
, NotAHeadPolicy
)
-- We do replace the minting policy of all tokens and datum of a head output to
-- simulate a faked init transaction.
alwaysSucceedsV2 = PlutusScriptSerialised $ Plutus.alwaysSucceedingNAryFunction 2
originalPolicyId = HeadTokens.headPolicyId seedInput
fakePolicyId = scriptPolicyId $ PlutusScript alwaysSucceedsV2
changeMintingPolicy (out, idx)
| idx == 0 = ChangeOutput idx $ modifyInlineDatum (replaceHeadId $ toPlutusCurrencySymbol fakePolicyId) out
| otherwise = ChangeOutput idx out
changedOutputsValue = replacePolicyIdWith originalPolicyId fakePolicyId <$> txOuts' tx
genCommitTxMutation :: UTxO -> Tx -> Gen Mutation
genCommitTxMutation utxo tx =
mutateInitialAddress
where
mutateInitialAddress = do
let mutatedTxOut = modifyTxOutAddress (const fakeScriptAddress) initialTxOut
pure $
Changes
[ ChangeInput initialTxIn mutatedTxOut (Just $ toScriptData initialRedeemer)
, AddScript fakeScript
]
(initialTxIn, initialTxOut) =
fromMaybe (error "not found initial script") $
UTxO.find (isScriptTxOut @PlutusScriptV2 initialScript) resolvedInputs
resolvedInputs =
UTxO.fromPairs $
mapMaybe (\txIn -> (txIn,) <$> UTxO.resolve txIn utxo) (txIns' tx)
initialRedeemer =
fromMaybe (error "not found redeemer") $
findRedeemerSpending @Initial.RedeemerType tx initialTxIn
initialScript = fromPlutusScript Initial.validatorScript
fakeScriptAddress = mkScriptAddress @PlutusScriptV2 testNetworkId fakeScript
fakeScript = fromPlutusScript $ Plutus.alwaysSucceedingNAryFunction 3
genAdaOnlyUTxOOnMainnetWithAmountBiggerThanOutLimit :: Gen UTxO
genAdaOnlyUTxOOnMainnetWithAmountBiggerThanOutLimit = do
adaAmount <- (+ maxMainnetLovelace) . getPositive <$> arbitrary
genUTxO1 (modifyTxOutValue (const $ lovelaceToValue adaAmount) <$> genTxOut)
-- * Properties
-- | Given any Head protocol state and the transaction corresponding a protocol
-- transition we should be able to observe this transition correctly even in
-- presence of other valid Hydra Head protocol states in the used lookup utxo.
prop_observeAnyTx :: Property
prop_observeAnyTx =
checkCoverage $ do
forAllShow genChainStateWithTx showTransition $ \(ctx, st, tx, transition) ->
forAllShow genChainStateWithTx showTransition $ \(_, otherSt, _, _) ->
genericCoverTable [transition] $ do
let expectedHeadId = chainStateHeadId st
case observeHeadTx (networkId ctx) (getKnownUTxO st <> getKnownUTxO otherSt) tx of
NoHeadTx ->
False & counterexample ("observeHeadTx ignored transaction: " <> show tx)
-- NOTE: we don't have the generated headId easily accessible in the initial state
Init{} -> transition === Transition.Init
Commit CommitObservation{headId} -> transition === Transition.Commit .&&. Just headId === expectedHeadId
Abort AbortObservation{headId} -> transition === Transition.Abort .&&. Just headId === expectedHeadId
CollectCom CollectComObservation{headId} -> transition === Transition.Collect .&&. Just headId === expectedHeadId
Close CloseObservation{headId} -> transition === Transition.Close .&&. Just headId === expectedHeadId
Contest ContestObservation{headId} -> transition === Transition.Contest .&&. Just headId === expectedHeadId
Fanout FanoutObservation{headId} -> transition === Transition.Fanout .&&. Just headId === expectedHeadId
where
showTransition (_, _, _, t) = show t
chainStateHeadId = \case
Idle{} -> Nothing
Initial InitialState{headId} -> Just headId
Open OpenState{headId} -> Just headId
Closed ClosedState{headId} -> Just headId
prop_canCloseFanoutEveryCollect :: Property
prop_canCloseFanoutEveryCollect = monadicST $ do
let maxParties = 10
ctx@HydraContext{ctxContestationPeriod} <- pickBlind $ genHydraContext maxParties
cctx <- pickBlind $ pickChainContext ctx
-- Init
txInit <- pickBlind $ genInitTx ctx
-- Commits
commits <- pickBlind $ genCommits' (genUTxOSized 1) ctx txInit
let (committed, stInitial) = unsafeObserveInitAndCommits cctx (ctxVerificationKeys ctx) txInit commits
let InitialState{headId = initialHeadId} = stInitial
-- Collect
let initialUTxO = fold committed
let spendableUTxO = getKnownUTxO stInitial
let txCollect = unsafeCollect cctx initialHeadId (ctxHeadParameters ctx) initialUTxO spendableUTxO
stOpen@OpenState{seedTxIn, headId} <- mfail $ snd <$> observeCollect stInitial txCollect
-- Close
(closeLower, closeUpper) <- pickBlind $ genValidityBoundsFromContestationPeriod ctxContestationPeriod
let closeUTxO = getKnownUTxO stOpen
txClose = unsafeClose cctx closeUTxO headId (ctxHeadParameters ctx) InitialSnapshot{headId, initialUTxO} closeLower closeUpper
(deadline, stClosed) <- case observeClose stOpen txClose of
Just (OnCloseTx{contestationDeadline}, st) -> pure (contestationDeadline, st)
_ -> fail "not observed close"
-- Fanout
let fanoutUTxO = getKnownUTxO stClosed
let txFanout = unsafeFanout cctx fanoutUTxO seedTxIn initialUTxO (slotNoFromUTCTime systemStart slotLength deadline)
-- Properties
let collectFails =
propTransactionFailsEvaluation (txCollect, getKnownUTxO cctx <> getKnownUTxO stInitial)
& counterexample "collect passed, but others failed?"
& cover 10 True "collect failed already"
let collectCloseAndFanoutPass =
conjoin
[ propTransactionEvaluates (txCollect, getKnownUTxO cctx <> getKnownUTxO stInitial)
& counterexample "collect failed"
, propTransactionEvaluates (txClose, getKnownUTxO cctx <> getKnownUTxO stOpen)
& counterexample "close failed"
, propTransactionEvaluates (txFanout, getKnownUTxO cctx <> getKnownUTxO stClosed)
& counterexample "fanout failed"
]
& cover 10 True "collect, close and fanout passed"
pure $
-- XXX: Coverage does not work if we only collectFails
checkCoverage
(collectFails .||. collectCloseAndFanoutPass)
--
-- Generic Properties
--
propBelowSizeLimit ::
Natural ->
((UTxO -> Tx -> Property) -> Property) ->
SpecWith ()
propBelowSizeLimit txSizeLimit forAllTx =
prop ("transaction size is below " <> showKB txSizeLimit) $
forAllTx $ \_ tx ->
let cbor = serialize tx
len = LBS.length cbor
in len < fromIntegral txSizeLimit
& label (showKB len)
& counterexample (renderTx tx)
& counterexample ("Actual size: " <> show len)
where
showKB nb = show (nb `div` 1024) <> "kB"
propIsValid ::
((UTxO -> Tx -> Property) -> Property) ->
SpecWith ()
propIsValid forAllTx =
prop "validates within maxTxExecutionUnits" $
forAllTx $
\utxo tx -> propTransactionEvaluates (tx, utxo)
-- * Generators
-- TODO: These forAllXX functions are hard to use and understand. Maybe simple
-- 'Gen' or functions in 'PropertyM' are better combinable?
forAllInit ::
Testable property =>
(UTxO -> Tx -> property) ->
Property
forAllInit action =
forAllBlind (genHydraContext maximumNumberOfParties) $ \ctx ->
forAll (pickChainContext ctx) $ \cctx -> do
forAll ((,) <$> genTxIn <*> genOutput (ownVerificationKey cctx)) $ \(seedIn, seedOut) -> do
let tx = initialize cctx seedIn (ctxParticipants ctx) (ctxHeadParameters ctx)
utxo = UTxO.singleton (seedIn, seedOut) <> getKnownUTxO cctx
in action utxo tx
& classify
(null (ctxVerificationKeys ctx))
"1 party"
& classify
(not (null (ctxVerificationKeys ctx)))
"2+ parties"
forAllCommit ::
Testable property =>
(UTxO -> Tx -> property) ->
Property
forAllCommit action =
forAllCommit' $ \ctx st toCommit tx ->
let utxo = getKnownUTxO st <> toCommit <> getKnownUTxO ctx
in action utxo tx
forAllCommit' ::
Testable property =>
(ChainContext -> InitialState -> UTxO -> Tx -> property) ->
Property
forAllCommit' action = do
forAllBlind (genHydraContext maximumNumberOfParties) $ \hctx ->
forAllBlind (genStInitial hctx) $ \(ctx, stInitial) ->
forAllBlind (genCommitFor $ ownVerificationKey ctx) $ \toCommit ->
-- TODO: generate script inputs here? <- SB: what script inputs?
let InitialState{headId} = stInitial
tx = unsafeCommit ctx headId (getKnownUTxO ctx <> getKnownUTxO stInitial) toCommit
in action ctx stInitial toCommit tx
& classify
(null toCommit)
"Empty commit"
& classify
(not (null toCommit))
"Non-empty commit"
forAllAbort ::
Testable property =>
(UTxO -> Tx -> property) ->
Property
forAllAbort action = do
forAll (genHydraContext maximumNumberOfParties) $ \ctx ->
forAll (pickChainContext ctx) $ \cctx ->
forAllBlind (genInitTx ctx) $ \initTx -> do
forAllBlind (sublistOf =<< genCommits ctx initTx) $ \commits ->
let (committed, stInitialized) = unsafeObserveInitAndCommits cctx (ctxVerificationKeys ctx) initTx commits
utxo = getKnownUTxO stInitialized <> getKnownUTxO cctx
InitialState{seedTxIn} = stInitialized
in action utxo (unsafeAbort cctx seedTxIn utxo (fold committed))
& classify
(null commits)
"Abort immediately, after 0 commits"
& classify
(not (null commits) && length commits < length (ctxParties ctx))
"Abort after some (but not all) commits"
& classify
(length commits == length (ctxParties ctx))
"Abort after all commits"
forAllCollectCom ::
Testable property =>
(UTxO -> Tx -> property) ->
Property
forAllCollectCom action =
forAllBlind genCollectComTx $ \(ctx, committedUTxO, stInitialized, tx) ->
let utxo = getKnownUTxO stInitialized <> getKnownUTxO ctx
in action utxo tx
& counterexample ("Committed UTxO: " <> show committedUTxO)
forAllClose ::
Testable property =>
(UTxO -> Tx -> property) ->
Property
forAllClose action = do
-- FIXME: we should not hardcode number of parties but generate it within bounds
forAll (genCloseTx maximumNumberOfParties) $ \(ctx, st, tx, sn) ->
let utxo = getKnownUTxO st <> getKnownUTxO ctx
in action utxo tx
& label (Prelude.head . Prelude.words . show $ sn)
forAllContest ::
Testable property =>
(UTxO -> Tx -> property) ->
Property
forAllContest action =
forAllBlind genContestTx $ \(hctx@HydraContext{ctxContestationPeriod}, closePointInTime, stClosed, tx) ->
-- XXX: Pick an arbitrary context to contest. We will stumble over this when
-- we make contests only possible once per party.
forAllBlind (pickChainContext hctx) $ \ctx ->
let utxo = getKnownUTxO stClosed <> getKnownUTxO ctx
in action utxo tx
& counterexample ("Contestation deadline: " <> show (getContestationDeadline stClosed))
& counterexample ("Contestation period: " <> show ctxContestationPeriod)
& counterexample ("Close point: " <> show closePointInTime)
& counterexample ("Closed contesters: " <> show (getClosedContesters stClosed))
& tabulate "Contestation period" (tabulateContestationPeriod ctxContestationPeriod)
& tabulate "Close point (slot)" (tabulateNum $ fst closePointInTime)
where
tabulateNum x
| x > 0 = ["> 0"]
| x < 0 = ["< 0"]
| otherwise = ["== 0"]
tabulateContestationPeriod (toNominalDiffTime -> cp)
| cp == confirmedHorizon = ["k blocks on mainnet"]
| cp == oneDay = ["one day"]
| cp == oneWeek = ["one week"]
| cp == oneMonth = ["one month"]
| cp == oneYear = ["one year"]
| cp < confirmedHorizon = ["< k blocks"]
| otherwise = ["> k blocks"]
confirmedHorizon = 2160 * 20 -- k blocks on mainnet
oneDay = 3600 * 24
oneWeek = oneDay * 7
oneMonth = oneDay * 30
oneYear = oneDay * 365
getClosedContesters = closedContesters . closedThreadOutput
forAllFanout ::
Testable property =>
(UTxO -> Tx -> property) ->
Property
forAllFanout action =
-- TODO: The utxo to fanout should be more arbitrary to have better test coverage
forAll (sized $ \n -> genFanoutTx maximumNumberOfParties (n `min` maxSupported)) $ \(hctx, stClosed, tx) ->
forAllBlind (pickChainContext hctx) $ \ctx ->
let utxo = getKnownUTxO stClosed <> getKnownUTxO ctx
in action utxo tx
& label ("Fanout size: " <> prettyLength (countAssets $ txOuts' tx))
where
maxSupported = 58
countAssets = getSum . foldMap (Sum . valueSize . txOutValue)
prettyLength len
| len > maxSupported = "> " <> show maxSupported <> " ???"
| len >= 40 = "40-" <> show maxSupported
| len >= 10 = "10-40"
| len >= 1 = "1-10"
| otherwise = "0"
-- | Generate an init tx with the used seed TxIn.
genInitTxWithSeed :: HydraContext -> Gen (Tx, TxIn)
genInitTxWithSeed ctx = do
cctx <- pickChainContext ctx
seedTxIn <- genTxIn
pure (initialize cctx seedTxIn (ctxParticipants ctx) (ctxHeadParameters ctx), seedTxIn)
-- * Helpers
mfail :: MonadFail m => Maybe a -> m a
mfail = \case
Nothing -> fail "encountered Nothing"
Just a -> pure a