-
Notifications
You must be signed in to change notification settings - Fork 155
/
Query.hs
316 lines (279 loc) · 11.2 KB
/
Query.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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Cardano.Ledger.Api.State.Query (
-- * @GetFilteredDelegationsAndRewardAccounts@
filterStakePoolDelegsAndRewards,
queryStakePoolDelegsAndRewards,
-- * @GetGovState@
queryGovState,
-- * @GetConstitution@
queryConstitution,
-- * @GetConstitutionHash@
queryConstitutionHash,
-- * @GetDRepState@
queryDRepState,
-- * @GetDRepStakeDistr@
queryDRepStakeDistr,
-- * @GetSPOStakeDistr@
querySPOStakeDistr,
-- * @GetCommitteeState@
queryCommitteeState,
-- * @GetCommitteeMembersState@
queryCommitteeMembersState,
-- * @GetAccountState@
queryAccountState,
CommitteeMemberState (..),
CommitteeMembersState (..),
HotCredAuthStatus (..),
MemberStatus (..),
NextEpochChange (..),
-- * @GetCurrentPParams@
queryCurrentPParams,
-- * @GetFuturePParams@
queryFuturePParams,
-- * For testing
getNextEpochCommitteeMembers,
) where
import Cardano.Ledger.Api.State.Query.CommitteeMembersState (
CommitteeMemberState (..),
CommitteeMembersState (..),
HotCredAuthStatus (..),
MemberStatus (..),
NextEpochChange (..),
)
import Cardano.Ledger.BaseTypes (EpochNo, strictMaybeToMaybe)
import Cardano.Ledger.CertState
import Cardano.Ledger.Coin (Coin)
import Cardano.Ledger.Compactible (fromCompact)
import Cardano.Ledger.Conway.Governance (
Committee (committeeMembers),
Constitution (constitutionAnchor),
ConwayEraGov (..),
PulsingSnapshot,
RatifyState,
committeeThresholdL,
ensCommitteeL,
finishDRepPulser,
psDRepDistr,
psPoolDistr,
rsEnactStateL,
)
import Cardano.Ledger.Conway.Rules (updateDormantDRepExpiry)
import Cardano.Ledger.Core
import Cardano.Ledger.Credential (Credential)
import Cardano.Ledger.Keys (KeyHash, KeyRole (..))
import Cardano.Ledger.SafeHash (SafeHash)
import Cardano.Ledger.Shelley.Governance (EraGov (..), FuturePParams (..))
import Cardano.Ledger.Shelley.LedgerState
import Cardano.Ledger.UMap (
StakeCredentials (scRewards, scSPools),
UMap,
domRestrictedStakeCredentials,
)
import Control.Monad (guard)
import Data.Foldable (foldMap')
import Data.Map (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe (isJust)
import Data.Set (Set)
import qualified Data.Set as Set
import Lens.Micro
import Lens.Micro.Extras (view)
-- | Filter out stake pool delegations and rewards for a set of stake credentials
filterStakePoolDelegsAndRewards ::
UMap c ->
Set (Credential 'Staking c) ->
(Map (Credential 'Staking c) (KeyHash 'StakePool c), Map (Credential 'Staking c) Coin)
filterStakePoolDelegsAndRewards umap creds =
(scSPools stakeCredentials, scRewards stakeCredentials)
where
stakeCredentials = domRestrictedStakeCredentials creds umap
-- | Uses `filterStakePoolDelegsAndRewards` to get the same information from the `NewEpochState`
--
-- Implementation for @GetFilteredDelegationsAndRewardAccounts@ query.
queryStakePoolDelegsAndRewards ::
NewEpochState era ->
Set (Credential 'Staking (EraCrypto era)) ->
( Map (Credential 'Staking (EraCrypto era)) (KeyHash 'StakePool (EraCrypto era))
, Map (Credential 'Staking (EraCrypto era)) Coin
)
queryStakePoolDelegsAndRewards nes = filterStakePoolDelegsAndRewards (dsUnified (getDState nes))
getDState :: NewEpochState era -> DState era
getDState = certDState . lsCertState . esLState . nesEs
queryConstitution :: ConwayEraGov era => NewEpochState era -> Constitution era
queryConstitution = (^. constitutionGovStateL) . queryGovState
queryConstitutionHash ::
ConwayEraGov era =>
NewEpochState era ->
SafeHash (EraCrypto era) AnchorData
queryConstitutionHash nes =
anchorDataHash . constitutionAnchor $ queryConstitution nes
-- | This query returns all of the state related to governance
queryGovState :: NewEpochState era -> GovState era
queryGovState nes = nes ^. nesEpochStateL . epochStateGovStateL
-- | Query DRep state.
queryDRepState ::
NewEpochState era ->
-- | Specify a set of DRep credentials whose state should be returned. When this set is
-- empty, states for all of the DReps will be returned.
Set (Credential 'DRepRole (EraCrypto era)) ->
Map (Credential 'DRepRole (EraCrypto era)) (DRepState (EraCrypto era))
queryDRepState nes creds
| null creds = updateDormantDRepExpiry' vState ^. vsDRepsL
| otherwise = updateDormantDRepExpiry' vStateFiltered ^. vsDRepsL
where
vStateFiltered = vState & vsDRepsL %~ (`Map.restrictKeys` creds)
vState = certVState $ lsCertState $ esLState $ nesEs nes
updateDormantDRepExpiry' = updateDormantDRepExpiry (nes ^. nesELL)
-- | Query DRep stake distribution. Note that this can be an expensive query because there
-- is a chance that current distribution has not been fully computed yet.
queryDRepStakeDistr ::
ConwayEraGov era =>
NewEpochState era ->
-- | Specify DRep Ids whose stake distribution should be returned. When this set is
-- empty, distributions for all of the DReps will be returned.
Set (DRep (EraCrypto era)) ->
Map (DRep (EraCrypto era)) Coin
queryDRepStakeDistr nes creds
| null creds = Map.map fromCompact distr
| otherwise = Map.map fromCompact $ distr `Map.restrictKeys` creds
where
distr = psDRepDistr . fst $ finishedPulserState nes
-- | Query pool stake distribution.
querySPOStakeDistr ::
ConwayEraGov era =>
NewEpochState era ->
Set (KeyHash 'StakePool (EraCrypto era)) ->
-- | Specify pool key hashes whose stake distribution should be returned. When this set is
-- empty, distributions for all of the pools will be returned.
Map (KeyHash 'StakePool (EraCrypto era)) Coin
querySPOStakeDistr nes keys
| null keys = Map.map fromCompact distr
| otherwise = Map.map fromCompact $ distr `Map.restrictKeys` keys
where
distr = psPoolDistr . fst $ finishedPulserState nes
-- | Query committee members
queryCommitteeState :: NewEpochState era -> CommitteeState era
queryCommitteeState nes =
vsCommitteeState $ certVState $ lsCertState $ esLState $ nesEs nes
{-# DEPRECATED queryCommitteeState "In favor of `queryCommitteeMembersState`" #-}
-- | Query committee members. Whenever the system is in No Confidence mode this query will
-- return `Nothing`.
queryCommitteeMembersState ::
forall era.
ConwayEraGov era =>
-- | filter by cold credentials (don't filter when empty)
Set (Credential 'ColdCommitteeRole (EraCrypto era)) ->
-- | filter by hot credentials (don't filter when empty)
Set (Credential 'HotCommitteeRole (EraCrypto era)) ->
-- | filter by status (don't filter when empty)
-- (useful, for discovering, for example, only active members)
Set MemberStatus ->
NewEpochState era ->
CommitteeMembersState (EraCrypto era)
queryCommitteeMembersState coldCredsFilter hotCredsFilter statusFilter nes =
let
committee = queryGovState nes ^. committeeGovStateL
comMembers = foldMap' committeeMembers committee
nextComMembers = getNextEpochCommitteeMembers nes
comStateMembers =
csCommitteeCreds $
nes ^. nesEpochStateL . esLStateL . lsCertStateL . certVStateL . vsCommitteeStateL
withFilteredColdCreds s
| Set.null coldCredsFilter = s
| otherwise = s `Set.intersection` coldCredsFilter
relevantColdKeys
| Set.null statusFilter || Set.member Unrecognized statusFilter =
withFilteredColdCreds $
Set.unions
[ Map.keysSet comMembers
, Map.keysSet comStateMembers
, Map.keysSet nextComMembers
]
| otherwise = withFilteredColdCreds $ Map.keysSet comMembers
relevantHotKeys =
Set.fromList
[ ck
| (ck, CommitteeHotCredential hk) <- Map.toList comStateMembers
, hk `Set.member` hotCredsFilter
]
relevant
| Set.null hotCredsFilter = relevantColdKeys
| otherwise = relevantColdKeys `Set.intersection` relevantHotKeys
cms = Map.mapMaybe id $ Map.fromSet mkMaybeMemberState relevant
currentEpoch = nes ^. nesELL
mkMaybeMemberState ::
Credential 'ColdCommitteeRole (EraCrypto era) ->
Maybe (CommitteeMemberState (EraCrypto era))
mkMaybeMemberState coldCred = do
let mbExpiry = Map.lookup coldCred comMembers
let status = case mbExpiry of
Nothing -> Unrecognized
Just expiry
| currentEpoch > expiry -> Expired
| otherwise -> Active
guard (null statusFilter || status `Set.member` statusFilter)
let hkStatus =
case Map.lookup coldCred comStateMembers of
Nothing -> MemberNotAuthorized
Just (CommitteeMemberResigned anchor) -> MemberResigned (strictMaybeToMaybe anchor)
Just (CommitteeHotCredential hk) -> MemberAuthorized hk
pure $ CommitteeMemberState hkStatus status mbExpiry (nextEpochChange coldCred)
nextEpochChange :: Credential 'ColdCommitteeRole (EraCrypto era) -> NextEpochChange
nextEpochChange ck
| not inCurrent && inNext = ToBeEnacted
| not inNext = ToBeRemoved
| Just curTerm <- lookupCurrent
, Just nextTerm <- lookupNext
, curTerm /= nextTerm
, -- if the term is adjusted such that it expires in the next epoch,
-- we set it to ToBeExpired instead of TermAdjusted
not expiringNext =
TermAdjusted nextTerm
| expiringCurrent || expiringNext = ToBeExpired
| otherwise = NoChangeExpected
where
lookupCurrent = Map.lookup ck comMembers
lookupNext = Map.lookup ck nextComMembers
inCurrent = isJust lookupCurrent
inNext = isJust lookupNext
expiringCurrent = lookupCurrent == Just currentEpoch
expiringNext = lookupNext == Just currentEpoch
in
CommitteeMembersState
{ csCommittee = cms
, csThreshold = strictMaybeToMaybe $ (^. committeeThresholdL) <$> committee
, csEpochNo = currentEpoch
}
queryAccountState ::
NewEpochState era ->
AccountState
queryAccountState = view $ nesEsL . esAccountStateL
getNextEpochCommitteeMembers ::
ConwayEraGov era =>
NewEpochState era ->
Map (Credential 'ColdCommitteeRole (EraCrypto era)) EpochNo
getNextEpochCommitteeMembers nes =
let ratifyState = snd $ finishedPulserState nes
committee = ratifyState ^. rsEnactStateL . ensCommitteeL
in foldMap' committeeMembers committee
-- | This is a simple lookup into the state for the values of current protocol
-- parameters. These values can change on the epoch boundary. Use `queryFuturePParams` to
-- see if we are aware of any upcoming changes.
queryCurrentPParams :: EraGov era => NewEpochState era -> PParams era
queryCurrentPParams nes = queryGovState nes ^. curPParamsGovStateL
-- | This query will return values for protocol parameters that are likely to be adopted
-- at the next epoch boundary. It is only when we passed 2 stability windows before the
-- end of the epoch that users can rely on this query to produce stable results.
queryFuturePParams :: EraGov era => NewEpochState era -> Maybe (PParams era)
queryFuturePParams nes =
case queryGovState nes ^. futurePParamsGovStateL of
NoPParamsUpdate -> Nothing
PotentialPParamsUpdate mpp -> mpp
DefinitePParamsUpdate pp -> Just pp
finishedPulserState ::
ConwayEraGov era =>
NewEpochState era ->
(PulsingSnapshot era, RatifyState era)
finishedPulserState nes = finishDRepPulser (nes ^. newEpochStateGovStateL . drepPulsingStateGovStateL)