1
+ {-# LANGUAGE PatternSynonyms #-}
1
2
{-# LANGUAGE TypeFamilies #-}
2
3
3
4
-- |
8
9
module Cardano.Wallet.Delegation.Model
9
10
( Operation (.. )
10
11
, slotOf
12
+
13
+ , Transition (.. )
14
+ , applyTransition
15
+
11
16
, Status (.. )
17
+
12
18
, History
13
19
, status
20
+
21
+ , pattern Register
22
+ , pattern Delegate
23
+ , pattern Vote
24
+ , pattern Deregister'
25
+ , pattern DelegateAndVote
26
+ , pattern Registered
27
+ , pattern Delegating
28
+ , pattern Voting
29
+ , pattern DelegatingAndVoting
14
30
) where
15
31
16
32
import Prelude
@@ -27,18 +43,21 @@ import Data.Map.Strict
27
43
28
44
import qualified Data.Map.Strict as Map
29
45
46
+ data Transition drep pool
47
+ = VoteAndDelegate (Maybe drep ) (Maybe pool )
48
+ | Deregister
49
+ deriving (Eq , Show )
50
+
30
51
-- | Delta type for the delegation 'History'.
31
52
data Operation slot drep pool
32
- = VoteAndDelegate (Maybe drep ) (Maybe pool ) slot
33
- | Deregister slot
53
+ = ApplyTransition (Transition drep pool ) slot
34
54
| Rollback slot
35
- deriving (Show )
55
+ deriving (Eq , Show )
36
56
37
57
-- | Target slot of each 'Operation'.
38
58
slotOf :: Operation slot drep pool -> slot
39
- slotOf (Deregister x) = x
40
59
slotOf (Rollback x) = x
41
- slotOf (VoteAndDelegate _ _ x) = x
60
+ slotOf (ApplyTransition _ x) = x
42
61
43
62
-- | Valid state for the delegations, independent of time.
44
63
data Status drep pool
@@ -56,16 +75,17 @@ instance (Ord slot, Eq pool, Eq drep) => Delta (Operation slot drep pool) where
56
75
slot = slotOf r
57
76
hist' = cut (< slot) hist
58
77
miss = status slot hist'
59
- wanted = transition r $ status slot hist
78
+ wanted = case r of
79
+ ApplyTransition t _ -> applyTransition t $ status slot hist
80
+ Rollback _ -> status slot hist
60
81
61
- transition :: Operation slot drep pool -> Status drep pool -> Status drep pool
62
- transition ( Deregister _) _ = Inactive
63
- transition (VoteAndDelegate d p _ ) (Active d' p') = Active d'' p''
82
+ applyTransition :: Transition drep pool -> Status drep pool -> Status drep pool
83
+ applyTransition Deregister _ = Inactive
84
+ applyTransition (VoteAndDelegate d p) (Active d' p') = Active d'' p''
64
85
where
65
86
d'' = insertIfJust d d'
66
87
p'' = insertIfJust p p'
67
- transition (VoteAndDelegate d p _) _ = Active d p
68
- transition _ s = s
88
+ applyTransition (VoteAndDelegate d p) _ = Active d p
69
89
70
90
insertIfJust :: Maybe a -> Maybe a -> Maybe a
71
91
insertIfJust (Just y) _ = Just y
@@ -79,3 +99,31 @@ cut op = fst . Map.spanAntitone op
79
99
-- | Status of the delegation at a given slot.
80
100
status :: Ord slot => slot -> Map slot (Status drep pool ) -> Status drep pool
81
101
status x = maybe Inactive snd . Map. lookupMax . cut (<= x)
102
+
103
+ pattern Register :: slot -> Operation slot drep pool
104
+ pattern Register i = ApplyTransition (VoteAndDelegate Nothing Nothing ) i
105
+
106
+ pattern Delegate :: pool -> slot -> Operation slot drep pool
107
+ pattern Delegate p i = ApplyTransition (VoteAndDelegate Nothing (Just p)) i
108
+
109
+ pattern Vote :: drep -> slot -> Operation slot drep pool
110
+ pattern Vote v i = ApplyTransition (VoteAndDelegate (Just v) Nothing ) i
111
+
112
+ pattern Deregister' :: slot -> Operation slot drep pool
113
+ pattern Deregister' i = ApplyTransition Deregister i
114
+
115
+ pattern DelegateAndVote :: pool -> drep -> slot -> Operation slot drep pool
116
+ pattern DelegateAndVote p v i
117
+ = ApplyTransition (VoteAndDelegate (Just v) (Just p)) i
118
+
119
+ pattern Registered :: Status drep pool
120
+ pattern Registered = Active Nothing Nothing
121
+
122
+ pattern Delegating :: pool -> Status drep pool
123
+ pattern Delegating p = Active Nothing (Just p)
124
+
125
+ pattern Voting :: drep -> Status drep pool
126
+ pattern Voting v = Active (Just v) Nothing
127
+
128
+ pattern DelegatingAndVoting :: pool -> drep -> Status drep pool
129
+ pattern DelegatingAndVoting p v = Active (Just v) (Just p)
0 commit comments