Skip to content

Commit

Permalink
Augment spo-stake-distribution to include DRep delegation choice of p…
Browse files Browse the repository at this point in the history
…ool's reward account
  • Loading branch information
smelc authored and CarlosLopezDeLara committed Dec 20, 2024
1 parent f477420 commit ee57bc6
Showing 1 changed file with 58 additions and 5 deletions.
63 changes: 58 additions & 5 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}

Expand Down Expand Up @@ -57,6 +58,7 @@ import Cardano.CLI.Types.Output (QueryDRepStateOutput (..))
import qualified Cardano.CLI.Types.Output as O
import Cardano.Crypto.Hash (hashToBytesAsHex)
import qualified Cardano.Crypto.Hash.Blake2b as Blake2b
import Cardano.Prelude (catMaybes)
import Cardano.Slotting.EpochInfo (EpochInfo (..), epochInfoSlotToUTCTime, hoistEpochInfo)
import Cardano.Slotting.Time (RelativeTime (..), toRelativeTime)

Expand Down Expand Up @@ -917,7 +919,6 @@ callQueryStakeAddressInfoCmd
, Cmd.target
}
, Cmd.addr = StakeAddress _ addr
, Cmd.mOutFile
} = do
let localNodeConnInfo = LocalNodeConnectInfo consensusModeParams networkId nodeSocketPath

Expand Down Expand Up @@ -1344,6 +1345,7 @@ runQueryStakeDistributionCmd
& onLeft (left . QueryCmdAcquireFailure)
& onLeft left


writeStakeDistribution
:: OutputFormatJsonOrText
-> Maybe (File () Out)
Expand Down Expand Up @@ -1670,7 +1672,7 @@ runQuerySPOStakeDistribution
Cmd.QuerySPOStakeDistributionCmdArgs
{ Cmd.eon
, Cmd.commons =
Cmd.QueryCommons
commons@Cmd.QueryCommons
{ Cmd.nodeSocketPath
, Cmd.consensusModeParams
, Cmd.networkId
Expand All @@ -1687,9 +1689,60 @@ runQuerySPOStakeDistribution

spos <- fromList <$> mapM spoFromSource spoHashSources

spoStakeDistribution <- runQuery localNodeConnInfo target $ querySPOStakeDistribution eon spos
writeOutput mOutFile $
Map.assocs spoStakeDistribution
let beo = convert eon

spoStakeDistribution :: Map (L.KeyHash L.StakePool StandardCrypto) L.Coin <-
runQuery localNodeConnInfo target $ querySPOStakeDistribution eon spos
let poolIds :: Maybe (Set (Hash StakePoolKey)) = Just $ Set.fromList $ map StakePoolKeyHash $ Map.keys spoStakeDistribution

serialisedPoolState :: SerialisedPoolState era <-
runQuery localNodeConnInfo target $ queryPoolState beo poolIds

PoolState (poolState :: (L.PState (ShelleyLedgerEra era))) <-
pure (decodePoolState serialisedPoolState)
& onLeft (left . QueryCmdPoolStateDecodeError)

let spoToPoolParams
:: Map
(L.KeyHash L.StakePool StandardCrypto)
(L.PoolParams StandardCrypto) = L.psStakePoolParams poolState
rewardsAccounts
:: Map
(L.KeyHash L.StakePool StandardCrypto)
StakeCredential = Map.map (fromShelleyStakeCredential . L.raCredential . L.ppRewardAccount) spoToPoolParams
rewardsAddresses
:: Map
(L.KeyHash L.StakePool StandardCrypto)
StakeAddress = Map.map (makeStakeAddress networkId) rewardsAccounts
addressesAndRewards
:: Map
StakeAddress
(L.KeyHash L.StakePool StandardCrypto) = Map.fromList [(addr, keyHash) | (keyHash, addr) <- Map.toList rewardsAddresses]
mkQueryStakeAddressInfoCmdArgs addr =
Cmd.QueryStakeAddressInfoCmdArgs
{ Cmd.commons = commons
, addr
, mOutFile -- unused anyway. TODO tighten this by removing the field.
}
infos <-
mapM (callQueryStakeAddressInfoCmd . mkQueryStakeAddressInfoCmdArgs) $ Map.elems rewardsAddresses
let spoToDelegatee :: Map (L.KeyHash L.StakePool StandardCrypto) (L.DRep StandardCrypto) =
Map.fromList $
catMaybes $
[ fmap (,delegatee) mSpo
| info <- infos
, (addr, delegatee) <- Map.toList $ delegatees info
, let mSpo = Map.lookup addr addressesAndRewards
]
toWrite =
[ ( spo
, coin
, Map.lookup spo spoToDelegatee
)
| (spo, coin) <- Map.assocs spoStakeDistribution
]

writeOutput mOutFile toWrite

runQueryCommitteeMembersState
:: Cmd.QueryCommitteeMembersStateCmdArgs era
Expand Down

0 comments on commit ee57bc6

Please sign in to comment.