Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Augment of query spo-stake-distribution to include the DRep delegation choices of the Pool's rewards accounts #990

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,9 @@ write-ghc-environment-files: always
-- IMPORTANT
-- Do NOT add more source-repository-package stanzas here unless they are strictly
-- temporary! Please read the section in CONTRIBUTING about updating dependencies.

source-repository-package
type: git
location: https://github.com/IntersectMBO/cardano-api
subdir: cardano-api
tag: 359017665e9dbdb3b48a03ad79a64a5ab2b03584
127 changes: 106 additions & 21 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 @@ -58,6 +59,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 @@ -918,6 +920,40 @@ runQueryStakeAddressInfoCmd
=> Cmd.QueryStakeAddressInfoCmdArgs
-> ExceptT QueryCmdError IO ()
runQueryStakeAddressInfoCmd
[email protected]
{ Cmd.commons =
Cmd.QueryCommons
{ Cmd.nodeSocketPath
, Cmd.consensusModeParams
, Cmd.networkId
, Cmd.target
}
, Cmd.mOutFile
} = do
let localNodeConnInfo = LocalNodeConnectInfo consensusModeParams networkId nodeSocketPath
AnyCardanoEra era <-
firstExceptT
QueryCmdAcquireFailure
(newExceptT $ executeLocalStateQueryExpr localNodeConnInfo target queryCurrentEra)
& onLeft (left . QueryCmdUnsupportedNtcVersion)
sbe <- requireShelleyBasedEra era & onNothing (left QueryCmdByronEra)

said <- callQueryStakeAddressInfoCmd cmd

writeStakeAddressInfo sbe said mOutFile

-- | Container for data returned by 'callQueryStakeAddressInfoCmd'
data StakeAddressInfoData = StakeAddressInfoData
{ rewards :: DelegationsAndRewards
, deposits :: Map StakeAddress Lovelace
, delegatees :: Map StakeAddress (L.DRep L.StandardCrypto)
}

callQueryStakeAddressInfoCmd
:: ()
=> Cmd.QueryStakeAddressInfoCmdArgs
-> ExceptT QueryCmdError IO StakeAddressInfoData
callQueryStakeAddressInfoCmd
Cmd.QueryStakeAddressInfoCmdArgs
{ Cmd.commons =
Cmd.QueryCommons
Expand All @@ -927,7 +963,6 @@ runQueryStakeAddressInfoCmd
, Cmd.target
}
, Cmd.addr = StakeAddress _ addr
, Cmd.mOutFile
} = do
let localNodeConnInfo = LocalNodeConnectInfo consensusModeParams networkId nodeSocketPath

Expand Down Expand Up @@ -961,13 +996,12 @@ runQueryStakeAddressInfoCmd
& onLeft (left . QueryCmdUnsupportedNtcVersion)
& onLeft (left . QueryCmdLocalStateQueryError . EraMismatchError)

return $ do
writeStakeAddressInfo
sbe
mOutFile
(DelegationsAndRewards (stakeRewardAccountBalances, stakePools))
(Map.mapKeys (makeStakeAddress networkId) stakeDelegDeposits)
(Map.mapKeys (makeStakeAddress networkId) stakeVoteDelegatees)
return $
return $
StakeAddressInfoData
(DelegationsAndRewards (stakeRewardAccountBalances, stakePools))
(Map.mapKeys (makeStakeAddress networkId) stakeDelegDeposits)
(Map.mapKeys (makeStakeAddress networkId) stakeVoteDelegatees)
)
& onLeft (left . QueryCmdAcquireFailure)
& onLeft left
Expand All @@ -976,19 +1010,18 @@ runQueryStakeAddressInfoCmd

writeStakeAddressInfo
:: ShelleyBasedEra era
-> StakeAddressInfoData
-> Maybe (File () Out)
-> DelegationsAndRewards
-> Map StakeAddress Lovelace
-- ^ deposits
-> Map StakeAddress (L.DRep L.StandardCrypto)
-- ^ vote delegatees
-> ExceptT QueryCmdError IO ()
writeStakeAddressInfo
sbe
mOutFile
(DelegationsAndRewards (stakeAccountBalances, stakePools))
stakeDelegDeposits
voteDelegatees =
( StakeAddressInfoData
{ rewards = DelegationsAndRewards (stakeAccountBalances, stakePools)
, deposits = stakeDelegDeposits
, delegatees = voteDelegatees
}
)
mOutFile =
firstExceptT QueryCmdWriteFileError . newExceptT $
writeLazyByteStringOutput mOutFile (encodePretty $ jsonInfo sbe)
where
Expand Down Expand Up @@ -1373,6 +1406,7 @@ runQueryStakeDistributionCmd
& onLeft (left . QueryCmdAcquireFailure)
& onLeft left


writeStakeDistribution
:: OutputFormatJsonOrText
-> Maybe (File () Out)
Expand Down Expand Up @@ -1720,7 +1754,7 @@ runQuerySPOStakeDistribution
Cmd.QuerySPOStakeDistributionCmdArgs
{ Cmd.eon
, Cmd.commons =
Cmd.QueryCommons
commons@Cmd.QueryCommons
{ Cmd.nodeSocketPath
, Cmd.consensusModeParams
, Cmd.networkId
Expand All @@ -1737,9 +1771,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
Loading