1
1
{-# LANGUAGE FlexibleContexts #-}
2
- {-# LANGUAGE LambdaCase #-}
3
2
{-# LANGUAGE MonoLocalBinds #-}
3
+ {-# LANGUAGE NamedFieldPuns #-}
4
4
5
5
-- |
6
6
-- Copyright: © 2022–2023 IOHK
7
7
-- License: Apache-2.0
8
8
--
9
9
-- Properties of the delegations-history model.
10
10
module Cardano.Wallet.Delegation.Properties
11
- ( GenSlot
12
- , Step (.. )
11
+ ( Step (.. )
13
12
, properties
14
13
)
15
14
where
@@ -20,16 +19,14 @@ import Cardano.Wallet.Delegation.Model
20
19
( History
21
20
, Operation (.. )
22
21
, Status (.. )
23
- , Transition ( .. )
22
+ , applyTransition
24
23
, slotOf
25
24
, status
26
25
)
27
- import Control.Applicative
28
- ( (<|>)
29
- )
30
26
import Test.QuickCheck
31
27
( Gen
32
28
, Property
29
+ , conjoin
33
30
, counterexample
34
31
, forAll
35
32
, (===)
@@ -44,70 +41,45 @@ data Step slot drep pool = Step
44
41
}
45
42
deriving (Show )
46
43
47
- -- | Compute a not so random slot from a 'History' of delegations.
48
- type GenSlot slot drep pool = History slot drep pool -> Gen slot
49
-
50
- property'
51
- :: (Ord a , Show a , Show drep , Show pool , Eq drep , Eq pool )
52
- => (History a drep pool -> Gen a )
53
- -> Step a drep pool
54
- -> (Status drep pool -> Status drep pool -> Property )
44
+ setsTheFuture
45
+ :: (Ord slot , Show slot , Show drep , Show pool , Eq drep , Eq pool )
46
+ => (History slot drep pool -> Gen slot )
47
+ -> Step slot drep pool
48
+ -> (slot -> Operation slot drep pool )
49
+ -> (Status drep pool -> Status drep pool )
55
50
-> Property
56
- property' genSlot Step {old_ = xs, new_ = xs', delta_ = diff} change =
57
- let x = slotOf diff
58
- old = status x xs
59
- in forAll (genSlot xs') $ \ y ->
60
- let new = status y xs'
51
+ setsTheFuture genSlot Step {old_= history, new_= history', delta_} op transition =
52
+ let x = slotOf delta_
53
+ old = status x history
54
+ in conjoin
55
+ [ delta_ === op x
56
+ , forAll (genSlot history') $ \ y ->
57
+ let new = status y history'
61
58
in case compare y x of
62
- LT -> new === status y xs
63
- _ -> change old new
64
-
65
- precond
66
- :: (Eq drep , Eq pool , Show drep , Show pool )
67
- => (Status drep pool -> (Bool , Maybe x ))
68
- -> (Maybe x -> Status drep pool )
69
- -> Status drep pool
70
- -> Status drep pool
71
- -> Property
72
- precond check target old new
73
- | (fst $ check old) = counterexample " new target"
74
- $ new === (target (snd $ check old))
75
- | otherwise = counterexample " no changes"
76
- $ new === old
59
+ LT -> new === status y history
60
+ _ -> new === transition old
61
+ ]
77
62
78
- -- | Properties replicated verbatim from specifications. See
79
- -- 'specifications/Cardano/Wallet/delegation.lean'
63
+ -- | Properties replicated verbatim from specifications.
64
+ -- See 'specifications/Cardano/Wallet/Delegation.agda'.
80
65
properties
81
66
:: (Show slot , Show drep , Show pool , Ord slot , Eq drep , Eq pool )
82
67
=> (History slot drep pool -> Gen slot )
83
68
-> Step slot drep pool
84
69
-> Property
85
70
properties genSlot step =
86
71
let that msg = counterexample (" falsified: " <> msg)
87
- prop cond target =
72
+ setsTheFuture' op =
88
73
counterexample (show step)
89
- $ property' genSlot step
90
- $ precond cond target
74
+ . setsTheFuture genSlot step op
91
75
in case delta_ step of
92
- ApplyTransition Deregister _ ->
93
- that " deregister invariant is respected"
94
- $ prop
95
- ( \ case
96
- Active _ _ -> (True , Nothing )
97
- _ -> (False , Nothing )
98
- )
99
- (const Inactive )
100
- ApplyTransition (VoteAndDelegate v p) _ -> do
101
- that " delegate and/or vote invariant is respected"
102
- $ prop
103
- ( \ case
104
- Inactive -> (True , Just Inactive )
105
- Active v' p'-> (True , Just (Active v' p'))
106
- )
107
- $ \ case
108
- Just (Active v' p') -> Active (v <|> v') (p <|> p')
109
- Just Inactive -> Active v p
110
- _ -> error " VoteAndDelegate branch broke"
76
+ ApplyTransition t _ ->
77
+ that " ApplyTransition invariant is respected"
78
+ $ setsTheFuture'
79
+ (ApplyTransition t)
80
+ (applyTransition t)
111
81
Rollback _ ->
112
- that " rollback invariant is respected"
113
- $ property' genSlot step (===)
82
+ that " Rollback invariant is respected"
83
+ $ setsTheFuture'
84
+ Rollback
85
+ id
0 commit comments