Skip to content

Commit

Permalink
Merge pull request #4764 from IntersectMBO/ldan/drep-distr-query-opti…
Browse files Browse the repository at this point in the history
…misation

Add registered DRep stake distribution query

Resolves #4664
  • Loading branch information
Lucsanszky authored Nov 21, 2024
2 parents 0f668a3 + 18a08e4 commit 242547e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 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.10.0.0

* Added `queryRegisteredDRepStakeDistr` state query
* Remove deprecated `RewardAcnt`, `mkRwdAcnt`, `serialiseRewardAcnt` and `deserialiseRewardAcnt`.
* Added `queryStakePoolDefaultVote` state query and its return type `DefaultVote`

Expand Down
34 changes: 32 additions & 2 deletions libs/cardano-ledger-api/src/Cardano/Ledger/Api/State/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ module Cardano.Ledger.Api.State.Query (
-- * @GetDRepStakeDistr@
queryDRepStakeDistr,

-- * @GetRegisteredDRepStakeDistr@
queryRegisteredDRepStakeDistr,

-- * @GetSPOStakeDistr@
querySPOStakeDistr,

Expand Down Expand Up @@ -69,7 +72,7 @@ import Cardano.Ledger.Api.State.Query.CommitteeMembersState (
)
import Cardano.Ledger.BaseTypes (EpochNo, strictMaybeToMaybe)
import Cardano.Ledger.CertState
import Cardano.Ledger.Coin (Coin)
import Cardano.Ledger.Coin (Coin (..), CompactForm (..))
import Cardano.Ledger.Compactible (fromCompact)
import Cardano.Ledger.Conway.Governance (
Committee (committeeMembers),
Expand All @@ -86,6 +89,7 @@ import Cardano.Ledger.Conway.Governance (
defaultStakePoolVote,
ensCommitteeL,
finishDRepPulser,
proposalsDeposits,
psDRepDistr,
psPoolDistr,
psProposalsL,
Expand All @@ -108,7 +112,7 @@ 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.Maybe (fromMaybe, isJust)
import Data.Sequence (Seq (..))
import qualified Data.Sequence as Seq
import Data.Sequence.Strict (StrictSeq (..))
Expand Down Expand Up @@ -185,6 +189,32 @@ queryDRepStakeDistr nes creds
where
distr = psDRepDistr . fst $ finishedPulserState nes

-- | Query the stake distribution of the registered DReps. This does not
-- include the @AlwaysAbstain@ and @NoConfidence@ DReps.
queryRegisteredDRepStakeDistr ::
ConwayEraGov era =>
NewEpochState era ->
-- | Specify DRep Ids whose stake distribution should be returned. When this set is
-- empty, distributions for all of the registered DReps will be returned.
Set (Credential 'DRepRole (EraCrypto era)) ->
Map (Credential 'DRepRole (EraCrypto era)) Coin
queryRegisteredDRepStakeDistr nes creds =
Map.foldlWithKey' computeDistr mempty selectedDReps
where
selectedDReps
| null creds = registeredDReps
| otherwise = registeredDReps `Map.restrictKeys` creds
registeredDReps = nes ^. nesEsL . esLStateL . lsCertStateL . certVStateL . vsDRepsL
computeDistr distrAcc dRepCred (DRepState {..}) =
Map.insert dRepCred (totalDelegations drepDelegs) distrAcc
totalDelegations =
fromCompact . foldMap stakeAndDeposits
stakeDistr = nes ^. nesEsL . epochStateIncrStakeDistrL
proposalDeposits = proposalsDeposits $ nes ^. newEpochStateGovStateL . proposalsGovStateL
stakeAndDeposits stakeCred =
fromMaybe (CompactCoin 0) $
Map.lookup stakeCred stakeDistr <> Map.lookup stakeCred proposalDeposits

-- | Query pool stake distribution.
querySPOStakeDistr ::
ConwayEraGov era =>
Expand Down

0 comments on commit 242547e

Please sign in to comment.