Skip to content

Commit

Permalink
Add queryGovState, queryDRepState, queryDRepStakeDistr and `que…
Browse files Browse the repository at this point in the history
…ryCommitteeState`
  • Loading branch information
lehins committed Aug 14, 2023
1 parent 913c0a6 commit a2cafff
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
1 change: 1 addition & 0 deletions libs/cardano-ledger-api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 1.4.0.0

* Add `queryGovState`, `queryDRepState`, `queryDRepStakeDistr` and `queryCommitteeState`
* Add `queryConstitution`.
* Replace `constitutionHash` with `constitutionAnchor`
* Add `eraName`
Expand Down
63 changes: 59 additions & 4 deletions libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,43 @@ module Cardano.Ledger.Api.State.Query (
filterStakePoolDelegsAndRewards,
queryStakePoolDelegsAndRewards,

-- * @GetGovState@
queryGovState,

-- * @GetConstitution@
queryConstitution,

-- * @GetConstitutionHash@
queryConstitutionHash,

-- * @GetDRepState@
queryDRepState,

-- * @GetDRepStakeDistr@
queryDRepStakeDistr,

-- * @GetCommitteeState@
queryCommitteeState,
) where

import Cardano.Ledger.Allegra.Core (Constitution (constitutionAnchor))
import Cardano.Ledger.CertState
import Cardano.Ledger.Coin (Coin)
import Cardano.Ledger.Conway.Governance (Anchor (..), AnchorData, EraGov (..))
import Cardano.Ledger.Compactible (fromCompact)
import Cardano.Ledger.Core
import Cardano.Ledger.Credential (Credential)
import Cardano.Ledger.Keys (KeyHash, KeyRole (StakePool, Staking))
import Cardano.Ledger.DRepDistr (extractDRepDistr)
import Cardano.Ledger.Keys (KeyHash, KeyRole (..))
import Cardano.Ledger.SafeHash (SafeHash)
import Cardano.Ledger.Shelley.Governance (EraGov (GovState, getConstitution))
import Cardano.Ledger.Shelley.LedgerState
import Cardano.Ledger.UMap (
StakeCredentials (scRewards, scSPools),
UMap,
domRestrictedStakeCredentials,
)
import Data.Map (Map)
import qualified Data.Map.Strict as Map
import Data.Set (Set)
import Lens.Micro

Expand Down Expand Up @@ -52,12 +71,48 @@ getDState :: NewEpochState era -> DState era
getDState = certDState . lsCertState . esLState . nesEs

queryConstitution :: EraGov era => NewEpochState era -> Maybe (Constitution era)
queryConstitution nes =
getConstitution (nes ^. nesEpochStateL . esLStateL . lsUTxOStateL . utxosGovStateL)
queryConstitution = getConstitution . queryGovState

queryConstitutionHash ::
EraGov era =>
NewEpochState era ->
Maybe (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 . esLStateL . lsUTxOStateL . utxosGovStateL

-- | Query DRep state.
queryDRepState ::
NewEpochState era ->
-- | Specify state of which DReps 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 = drepsState
| otherwise = drepsState `Map.restrictKeys` creds
where
drepsState = vsDReps $ certVState $ lsCertState $ esLState $ nesEs nes

-- | 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 ::
NewEpochState era ->
-- | Specify for which DReps distribution should be returned. When this set is empty,
-- distribution 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 = extractDRepDistr distrPulser
distrPulser = vsDRepDistr $ certVState $ lsCertState $ esLState $ nesEs nes

-- | Query committee members
queryCommitteeState :: NewEpochState era -> CommitteeState era
queryCommitteeState nes =
vsCommitteeState $ certVState $ lsCertState $ esLState $ nesEs nes

0 comments on commit a2cafff

Please sign in to comment.