Skip to content

Commit 500bb48

Browse files
Update QuickCheck properties to match Delegation.agda
1 parent 8a4a4a3 commit 500bb48

File tree

3 files changed

+42
-68
lines changed

3 files changed

+42
-68
lines changed

lib/wallet/src/Cardano/Wallet/Delegation/Model.hs

+8-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88
-- Data types that represents a history of delegations and its changes.
99
module Cardano.Wallet.Delegation.Model
1010
( Operation (..)
11-
, Transition (..)
1211
, slotOf
12+
13+
, Transition (..)
14+
, applyTransition
15+
1316
, Status (..)
17+
1418
, History
1519
, status
20+
1621
, pattern Register
1722
, pattern Delegate
1823
, pattern Vote
@@ -41,13 +46,13 @@ import qualified Data.Map.Strict as Map
4146
data Transition drep pool
4247
= VoteAndDelegate (Maybe drep) (Maybe pool)
4348
| Deregister
44-
deriving (Show)
49+
deriving (Eq, Show)
4550

4651
-- | Delta type for the delegation 'History'.
4752
data Operation slot drep pool
4853
= ApplyTransition (Transition drep pool) slot
4954
| Rollback slot
50-
deriving (Show)
55+
deriving (Eq, Show)
5156

5257
-- | Target slot of each 'Operation'.
5358
slotOf :: Operation slot drep pool -> slot
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
{-# LANGUAGE FlexibleContexts #-}
2-
{-# LANGUAGE LambdaCase #-}
32
{-# LANGUAGE MonoLocalBinds #-}
3+
{-# LANGUAGE NamedFieldPuns #-}
44

55
-- |
66
-- Copyright: © 2022–2023 IOHK
77
-- License: Apache-2.0
88
--
99
-- Properties of the delegations-history model.
1010
module Cardano.Wallet.Delegation.Properties
11-
( GenSlot
12-
, Step (..)
11+
( Step (..)
1312
, properties
1413
)
1514
where
@@ -20,16 +19,14 @@ import Cardano.Wallet.Delegation.Model
2019
( History
2120
, Operation (..)
2221
, Status (..)
23-
, Transition (..)
22+
, applyTransition
2423
, slotOf
2524
, status
2625
)
27-
import Control.Applicative
28-
( (<|>)
29-
)
3026
import Test.QuickCheck
3127
( Gen
3228
, Property
29+
, conjoin
3330
, counterexample
3431
, forAll
3532
, (===)
@@ -44,70 +41,45 @@ data Step slot drep pool = Step
4441
}
4542
deriving (Show)
4643

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)
5550
-> 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'
6158
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+
]
7762

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'.
8065
properties
8166
:: (Show slot, Show drep, Show pool, Ord slot, Eq drep, Eq pool)
8267
=> (History slot drep pool -> Gen slot)
8368
-> Step slot drep pool
8469
-> Property
8570
properties genSlot step =
8671
let that msg = counterexample ("falsified: " <> msg)
87-
prop cond target =
72+
setsTheFuture' op =
8873
counterexample (show step)
89-
$ property' genSlot step
90-
$ precond cond target
74+
. setsTheFuture genSlot step op
9175
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)
11181
Rollback _ ->
112-
that "rollback invariant is respected"
113-
$ property' genSlot step (===)
82+
that "Rollback invariant is respected"
83+
$ setsTheFuture'
84+
Rollback
85+
id

specifications/Cardano/Wallet/Delegation.agda

+1-4
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,11 @@ record HistoryLaws (api : HistoryApi) : Set₁ where
7777
field
7878
prop-transitions
7979
: (t : Transition)
80-
(history : History api)
8180
setsTheFuture api
8281
(ApplyTransition t)
8382
(applyTransition t)
8483

8584
prop-rollback
86-
: (slot : Slot)
87-
(history : History api)
88-
setsTheFuture api
85+
: setsTheFuture api
8986
Rollback
9087
(λ old old)

0 commit comments

Comments
 (0)