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

Merged
merged 5 commits into from
Dec 20, 2024
Merged
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
2 changes: 1 addition & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repository cardano-haskell-packages
-- you need to run if you change them
index-state:
, hackage.haskell.org 2024-10-11T15:49:11Z
, cardano-haskell-packages 2024-12-05T13:51:16Z
, cardano-haskell-packages 2024-12-19T20:16:27Z

packages:
cardano-cli
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ library
binary,
bytestring,
canonical-json,
cardano-api ^>=10.4,
cardano-api ^>=10.5,
cardano-binary,
cardano-crypto,
cardano-crypto-class ^>=2.1.2,
Expand Down
162 changes: 121 additions & 41 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 #-}
Dismissed Show dismissed Hide dismissed
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}

Expand Down Expand Up @@ -565,7 +566,7 @@
case Map.lookup (coerce blockIssuerHash) opCertCounterMap of
-- Operational certificate exists in the protocol state
-- so our ondisk op cert counter must be greater than or
-- equal to what is in the node state
-- equal to what is in the node state.
Just ptclStateCounter -> return (OpCertOnDiskCounter onDiskOpCertCount, Just $ OpCertNodeStateCounter ptclStateCounter)
Nothing -> return (OpCertOnDiskCounter onDiskOpCertCount, Nothing)

Expand Down Expand Up @@ -874,68 +875,96 @@
=> Cmd.QueryStakeAddressInfoCmdArgs
-> ExceptT QueryCmdError IO ()
runQueryStakeAddressInfoCmd
Cmd.QueryStakeAddressInfoCmdArgs
cmd@Cmd.QueryStakeAddressInfoCmdArgs
{ Cmd.commons =
Cmd.QueryCommons
{ Cmd.nodeSocketPath
, Cmd.consensusModeParams
, Cmd.networkId
, Cmd.target
}
, Cmd.addr = StakeAddress _ addr
, 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)

join $
lift
( executeLocalStateQueryExpr localNodeConnInfo target $ runExceptT $ do
AnyCardanoEra era <- easyRunQueryCurrentEra
said <- callQueryStakeAddressInfoCmd cmd

sbe <-
requireShelleyBasedEra era
& onNothing (left QueryCmdByronEra)
writeStakeAddressInfo sbe said mOutFile

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

(stakeRewardAccountBalances, stakePools) <-
easyRunQuery (queryStakeAddresses sbe stakeAddr networkId)
callQueryStakeAddressInfoCmd
:: ()
=> Cmd.QueryStakeAddressInfoCmdArgs
-> ExceptT QueryCmdError IO StakeAddressInfoData
callQueryStakeAddressInfoCmd
Cmd.QueryStakeAddressInfoCmdArgs
{ Cmd.commons =
Cmd.QueryCommons
{ Cmd.nodeSocketPath
, Cmd.consensusModeParams
, Cmd.networkId
, Cmd.target
}
, Cmd.addr = StakeAddress _ addr
} =
do
let localNodeConnInfo = LocalNodeConnectInfo consensusModeParams networkId nodeSocketPath

beo <- requireEon BabbageEra era
lift $ executeLocalStateQueryExpr localNodeConnInfo target $ runExceptT $ do
AnyCardanoEra era <- easyRunQueryCurrentEra

stakeDelegDeposits <- easyRunQuery (queryStakeDelegDeposits beo stakeAddr)
sbe <-
requireShelleyBasedEra era
& onNothing (left QueryCmdByronEra)

stakeVoteDelegatees <- monoidForEraInEonA era $ \ceo ->
easyRunQuery (queryStakeVoteDelegatees ceo stakeAddr)
let stakeAddr = Set.singleton $ fromShelleyStakeCredential addr

return $ do
writeStakeAddressInfo
sbe
mOutFile
(DelegationsAndRewards (stakeRewardAccountBalances, stakePools))
(Map.mapKeys (makeStakeAddress networkId) stakeDelegDeposits)
(Map.mapKeys (makeStakeAddress networkId) stakeVoteDelegatees)
)
& onLeft (left . QueryCmdAcquireFailure)
& onLeft left
(stakeRewardAccountBalances, stakePools) <-
easyRunQuery (queryStakeAddresses sbe stakeAddr networkId)

beo <- requireEon BabbageEra era

stakeDelegDeposits <- easyRunQuery (queryStakeDelegDeposits beo stakeAddr)

stakeVoteDelegatees <- monoidForEraInEonA era $ \ceo ->
easyRunQuery (queryStakeVoteDelegatees ceo stakeAddr)

pure $
StakeAddressInfoData
(DelegationsAndRewards (stakeRewardAccountBalances, stakePools))
(Map.mapKeys (makeStakeAddress networkId) stakeDelegDeposits)
(Map.mapKeys (makeStakeAddress networkId) stakeVoteDelegatees)
& onLeft (left . QueryCmdAcquireFailure)
& onLeft left

-- -------------------------------------------------------------------------------------------------

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 @@ -1638,7 +1667,7 @@
Cmd.QuerySPOStakeDistributionCmdArgs
{ Cmd.eon
, Cmd.commons =
Cmd.QueryCommons
commons@Cmd.QueryCommons
{ Cmd.nodeSocketPath
, Cmd.consensusModeParams
, Cmd.networkId
Expand All @@ -1655,9 +1684,59 @@

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 :: Set (Hash StakePoolKey) = Set.fromList $ map StakePoolKeyHash $ Map.keys spoStakeDistribution

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

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

let addressesAndRewards
:: Map
StakeAddress
(L.KeyHash L.StakePool StandardCrypto) =
Map.fromList
[ ( makeStakeAddress networkId . fromShelleyStakeCredential . L.raCredential . L.ppRewardAccount $ addr
, keyHash
)
| (keyHash, addr) <- Map.toList $ L.psStakePoolParams poolState
]

mkQueryStakeAddressInfoCmdArgs addr =
Cmd.QueryStakeAddressInfoCmdArgs
{ Cmd.commons = commons
, addr
, mOutFile -- unused anyway. TODO tighten this by removing the field.
}

spoToDelegatee <-
Map.fromList . concat
<$> traverse
( \stakeAddr -> do
info <- callQueryStakeAddressInfoCmd $ mkQueryStakeAddressInfoCmdArgs stakeAddr
return $
[ (spo, delegatee)
| (Just spo, delegatee) <-
map (first (`Map.lookup` addressesAndRewards)) $ Map.toList $ delegatees info
]
)
(Map.keys addressesAndRewards)

let toWrite =
[ ( spo
, coin
, Map.lookup spo spoToDelegatee
)
| (spo, coin) <- Map.assocs spoStakeDistribution
]

writeOutput mOutFile toWrite

runQueryCommitteeMembersState
:: Cmd.QueryCommitteeMembersStateCmdArgs era
Expand Down Expand Up @@ -1870,7 +1949,8 @@
easyRunQuery
:: ()
=> Monad m
=> m (Either UnsupportedNtcVersionError (Either Consensus.EraMismatch a)) -> ExceptT QueryCmdError m a
=> m (Either UnsupportedNtcVersionError (Either Consensus.EraMismatch a))
-> ExceptT QueryCmdError m a
easyRunQuery q =
lift q
& onLeft (left . QueryCmdUnsupportedNtcVersion)
Expand Down
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading