-
Notifications
You must be signed in to change notification settings - Fork 155
/
Deleg.hs
294 lines (273 loc) · 10.1 KB
/
Deleg.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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
module Shelley.Spec.Ledger.STS.Deleg
( DELEG,
DelegEnv (..),
PredicateFailure,
DelegPredicateFailure (..),
)
where
import Cardano.Binary
( FromCBOR (..),
ToCBOR (..),
encodeListLen,
)
import Cardano.Ledger.Era (Era)
import Cardano.Prelude (NoUnexpectedThunks (..))
import Control.Iterate.SetAlgebra (dom, eval, range, setSingleton, singleton, (∈), (∉), (∪), (⋪), (⋫), (⨃))
import Control.Monad.Trans.Reader (asks)
import Control.State.Transition
import Data.Foldable (fold)
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
import Data.Typeable (Typeable)
import Data.Word (Word8)
import GHC.Generics (Generic)
import Shelley.Spec.Ledger.BaseTypes
( Globals (..),
ShelleyBase,
invalidKey,
)
import Shelley.Spec.Ledger.Coin (Coin (..))
import Shelley.Spec.Ledger.Credential (Credential)
import Shelley.Spec.Ledger.Keys
( GenDelegPair (..),
GenDelegs (..),
Hash,
KeyHash,
KeyRole (..),
VerKeyVRF,
)
import Shelley.Spec.Ledger.LedgerState
( AccountState (..),
DState,
FutureGenDeleg (..),
InstantaneousRewards (..),
emptyDState,
_delegations,
_fGenDelegs,
_genDelegs,
_irwd,
_ptrs,
_rewards,
)
import Shelley.Spec.Ledger.Serialization (decodeRecordSum)
import Shelley.Spec.Ledger.Slot
( Duration (..),
EpochNo (..),
SlotNo,
epochInfoEpoch,
epochInfoFirst,
(*-),
(+*),
)
import Shelley.Spec.Ledger.TxBody
( DCert (..),
DelegCert (..),
Delegation (..),
GenesisDelegCert (..),
MIRCert (..),
MIRPot (..),
Ptr,
)
data DELEG era
data DelegEnv = DelegEnv
{ slotNo :: SlotNo,
ptr_ :: Ptr,
acnt_ :: AccountState
}
deriving (Show, Eq)
data DelegPredicateFailure era
= StakeKeyAlreadyRegisteredDELEG
!(Credential 'Staking era) -- Credential which is already registered
| -- | Indicates that the stake key is somehow already in the rewards map.
-- This error is now redundant with StakeKeyAlreadyRegisteredDELEG.
-- We should remove it and replace its one use with StakeKeyAlreadyRegisteredDELEG.
StakeKeyInRewardsDELEG
!(Credential 'Staking era) -- DEPRECATED, now redundant with StakeKeyAlreadyRegisteredDELEG
| StakeKeyNotRegisteredDELEG
!(Credential 'Staking era) -- Credential which is not registered
| StakeKeyNonZeroAccountBalanceDELEG
!(Maybe Coin) -- The remaining reward account balance, if it exists
| StakeDelegationImpossibleDELEG
!(Credential 'Staking era) -- Credential that is not registered
| WrongCertificateTypeDELEG -- The DCertPool constructor should not be used by this transition
| GenesisKeyNotInpMappingDELEG
!(KeyHash 'Genesis era) -- Unknown Genesis KeyHash
| DuplicateGenesisDelegateDELEG
!(KeyHash 'GenesisDelegate era) -- Keyhash which is already delegated to
| InsufficientForInstantaneousRewardsDELEG
!MIRPot -- which pot the rewards are to be drawn from, treasury or reserves
!Coin -- amount of rewards to be given out
!Coin -- size of the pot from which the lovelace is drawn
| MIRCertificateTooLateinEpochDELEG
!SlotNo -- current slot
!SlotNo -- MIR must be submitted before this slot
| DuplicateGenesisVRFDELEG
!(Hash era (VerKeyVRF era)) --VRF KeyHash which is already delegated to
deriving (Show, Eq, Generic)
instance Typeable era => STS (DELEG era) where
type State (DELEG era) = DState era
type Signal (DELEG era) = DCert era
type Environment (DELEG era) = DelegEnv
type BaseM (DELEG era) = ShelleyBase
type PredicateFailure (DELEG era) = DelegPredicateFailure era
initialRules = [pure emptyDState]
transitionRules = [delegationTransition]
instance NoUnexpectedThunks (DelegPredicateFailure era)
instance
(Typeable era, Era era) =>
ToCBOR (DelegPredicateFailure era)
where
toCBOR = \case
StakeKeyAlreadyRegisteredDELEG cred ->
encodeListLen 2 <> toCBOR (0 :: Word8) <> toCBOR cred
StakeKeyInRewardsDELEG cred ->
encodeListLen 2 <> toCBOR (10 :: Word8) <> toCBOR cred
StakeKeyNotRegisteredDELEG cred ->
encodeListLen 2 <> toCBOR (1 :: Word8) <> toCBOR cred
StakeKeyNonZeroAccountBalanceDELEG rewardBalance ->
encodeListLen 2 <> toCBOR (2 :: Word8) <> toCBOR rewardBalance
StakeDelegationImpossibleDELEG cred ->
encodeListLen 2 <> toCBOR (3 :: Word8) <> toCBOR cred
WrongCertificateTypeDELEG ->
encodeListLen 1 <> toCBOR (4 :: Word8)
GenesisKeyNotInpMappingDELEG gkh ->
encodeListLen 2 <> toCBOR (5 :: Word8) <> toCBOR gkh
DuplicateGenesisDelegateDELEG kh ->
encodeListLen 2 <> toCBOR (6 :: Word8) <> toCBOR kh
InsufficientForInstantaneousRewardsDELEG pot needed potAmount ->
encodeListLen 4 <> toCBOR (7 :: Word8)
<> toCBOR pot
<> toCBOR needed
<> toCBOR potAmount
MIRCertificateTooLateinEpochDELEG sNow sTooLate ->
encodeListLen 3 <> toCBOR (8 :: Word8) <> toCBOR sNow <> toCBOR sTooLate
DuplicateGenesisVRFDELEG vrf ->
encodeListLen 2 <> toCBOR (9 :: Word8) <> toCBOR vrf
instance
(Era era) =>
FromCBOR (DelegPredicateFailure era)
where
fromCBOR = decodeRecordSum "PredicateFailure (DELEG era)" $
\case
0 -> do
kh <- fromCBOR
pure (2, StakeKeyAlreadyRegisteredDELEG kh)
10 -> do
kh <- fromCBOR
pure (2, StakeKeyInRewardsDELEG kh)
1 -> do
kh <- fromCBOR
pure (2, StakeKeyNotRegisteredDELEG kh)
2 -> do
b <- fromCBOR
pure (2, StakeKeyNonZeroAccountBalanceDELEG b)
3 -> do
kh <- fromCBOR
pure (2, StakeDelegationImpossibleDELEG kh)
4 -> do
pure (1, WrongCertificateTypeDELEG)
5 -> do
gkh <- fromCBOR
pure (2, GenesisKeyNotInpMappingDELEG gkh)
6 -> do
kh <- fromCBOR
pure (2, DuplicateGenesisDelegateDELEG kh)
7 -> do
pot <- fromCBOR
needed <- fromCBOR
potAmount <- fromCBOR
pure (4, InsufficientForInstantaneousRewardsDELEG pot needed potAmount)
8 -> do
sNow <- fromCBOR
sTooLate <- fromCBOR
pure (3, MIRCertificateTooLateinEpochDELEG sNow sTooLate)
9 -> do
vrf <- fromCBOR
pure (2, DuplicateGenesisVRFDELEG vrf)
k -> invalidKey k
delegationTransition ::
Typeable era =>
TransitionRule (DELEG era)
delegationTransition = do
TRC (DelegEnv slot ptr acnt, ds, c) <- judgmentContext
case c of
DCertDeleg (RegKey hk) -> do
eval (hk ∉ dom (_rewards ds)) ?! StakeKeyAlreadyRegisteredDELEG hk
pure $
ds
{ _rewards = eval (_rewards ds ∪ (singleton hk mempty)),
_ptrs = eval (_ptrs ds ∪ (singleton ptr hk))
}
DCertDeleg (DeRegKey hk) -> do
-- note that pattern match is used instead of cwitness, as in the spec
eval (hk ∈ dom (_rewards ds)) ?! StakeKeyNotRegisteredDELEG hk
let rewardCoin = Map.lookup hk (_rewards ds)
rewardCoin == Just mempty ?! StakeKeyNonZeroAccountBalanceDELEG rewardCoin
pure $
ds
{ _rewards = eval (setSingleton hk ⋪ _rewards ds),
_delegations = eval (setSingleton hk ⋪ _delegations ds),
_ptrs = eval (_ptrs ds ⋫ setSingleton hk)
}
DCertDeleg (Delegate (Delegation hk dpool)) -> do
-- note that pattern match is used instead of cwitness and dpool, as in the spec
eval (hk ∈ dom (_rewards ds)) ?! StakeDelegationImpossibleDELEG hk
pure $
ds
{ _delegations = eval (_delegations ds ⨃ (singleton hk dpool))
}
DCertGenesis (GenesisDelegCert gkh vkh vrf) -> do
sp <- liftSTS $ asks stabilityWindow
-- note that pattern match is used instead of genesisDeleg, as in the spec
let s' = slot +* Duration sp
(GenDelegs genDelegs) = _genDelegs ds
-- gkh ∈ dom genDelegs ?! GenesisKeyNotInpMappingDELEG gkh
(case Map.lookup gkh genDelegs of Just _ -> True; Nothing -> False) ?! GenesisKeyNotInpMappingDELEG gkh
let cod =
range $
Map.filterWithKey (\g _ -> g /= gkh) genDelegs
fod =
range $
Map.filterWithKey (\(FutureGenDeleg _ g) _ -> g /= gkh) (_fGenDelegs ds)
currentOtherColdKeyHashes = Set.map genDelegKeyHash cod
currentOtherVrfKeyHashes = Set.map genDelegVrfHash cod
futureOtherColdKeyHashes = Set.map genDelegKeyHash fod
futureOtherVrfKeyHashes = Set.map genDelegVrfHash fod
eval (vkh ∉ (currentOtherColdKeyHashes `Set.union` futureOtherColdKeyHashes))
?! DuplicateGenesisDelegateDELEG vkh
eval (vrf ∉ (currentOtherVrfKeyHashes `Set.union` futureOtherVrfKeyHashes))
?! DuplicateGenesisVRFDELEG vrf
pure $
ds
{ _fGenDelegs = eval ((_fGenDelegs ds) ⨃ (singleton (FutureGenDeleg s' gkh) (GenDelegPair vkh vrf)))
}
DCertMir (MIRCert targetPot credCoinMap) -> do
sp <- liftSTS $ asks stabilityWindow
firstSlot <- liftSTS $ do
ei <- asks epochInfo
EpochNo currEpoch <- epochInfoEpoch ei slot
epochInfoFirst ei $ EpochNo (currEpoch + 1)
let tooLate = firstSlot *- Duration sp
slot < tooLate
?! MIRCertificateTooLateinEpochDELEG slot tooLate
let (potAmount, instantaneousRewards) =
case targetPot of
ReservesMIR -> (_reserves acnt, iRReserves $ _irwd ds)
TreasuryMIR -> (_treasury acnt, iRTreasury $ _irwd ds)
let combinedMap = Map.union credCoinMap instantaneousRewards
requiredForRewards = fold combinedMap
requiredForRewards <= potAmount
?! InsufficientForInstantaneousRewardsDELEG targetPot requiredForRewards potAmount
case targetPot of
ReservesMIR -> pure $ ds {_irwd = (_irwd ds) {iRReserves = combinedMap}}
TreasuryMIR -> pure $ ds {_irwd = (_irwd ds) {iRTreasury = combinedMap}}
DCertPool _ -> do
failBecause WrongCertificateTypeDELEG -- this always fails
pure ds