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

Sketch a way to filter DRep queries #580

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ data QueryDRepStateCmdArgs era = QueryDRepStateCmdArgs
, nodeSocketPath :: !SocketPath
, consensusModeParams :: !ConsensusModeParams
, networkId :: !NetworkId
, drepKeys :: !(AllOrOnly (VerificationKeyOrHashOrFile DRepKey))
, drepSources :: !(AllOrOnly DRepSource)
, mOutFile :: !(Maybe (File () Out))
} deriving Show

Expand All @@ -196,7 +196,7 @@ data QueryDRepStakeDistributionCmdArgs era = QueryDRepStakeDistributionCmdArgs
, nodeSocketPath :: !SocketPath
, consensusModeParams :: !ConsensusModeParams
, networkId :: !NetworkId
, drepKeys :: !(AllOrOnly (VerificationKeyOrHashOrFile DRepKey))
, drepSources :: !(AllOrOnly DRepSource)
, mOutFile :: !(Maybe (File () Out))
} deriving Show

Expand Down
23 changes: 19 additions & 4 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3046,6 +3046,22 @@ pDRepHashSource =
, DRepHashSourceVerificationKey <$> pDRepVerificationKeyOrHashOrFile
]

pDRepSource :: Parser DRepSource
pDRepSource =
asum
[ FromHash <$> pDRepHashSource
, Opt.flag' AlwaysAbstain $
mconcat
[ Opt.long "always-abstain"
, Opt.help "The trivial DRep that always abstains."
]
, Opt.flag' AlwaysNoConfidence $
mconcat
[ Opt.long "always-no-confidence"
, Opt.help "The trivial DRep that always votes no-confidence."
]
]

pDRepScriptHash :: Parser ScriptHash
pDRepScriptHash =
Opt.option scriptHashReader $ mconcat
Expand All @@ -3062,10 +3078,9 @@ pDRepVerificationKeyOrHashOrFile =
, VerificationKeyHash <$> pDRepVerificationKeyHash
]

pAllOrOnlyDRepVerificationKeyOrHashOrFile
:: Parser (AllOrOnly (VerificationKeyOrHashOrFile DRepKey))
pAllOrOnlyDRepVerificationKeyOrHashOrFile = pAll <|> pOnly
where pOnly = Only <$> some pDRepVerificationKeyOrHashOrFile
pAllOrOnlyDRepSource :: Parser (AllOrOnly DRepSource)
pAllOrOnlyDRepSource = pAll <|> pOnly
where pOnly = Only <$> some pDRepSource
pAll = Opt.flag' All $ mconcat
[ Opt.long "all-dreps"
, Opt.help "Query for all DReps."
Expand Down
4 changes: 2 additions & 2 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ pQueryDRepStateCmd era envCli = do
<$> pSocketPath envCli
<*> pConsensusModeParams
<*> pNetworkId envCli
<*> pAllOrOnlyDRepVerificationKeyOrHashOrFile
<*> pAllOrOnlyDRepSource
<*> optional pOutputFile

pQueryDRepStakeDistributionCmd :: ()
Expand All @@ -341,7 +341,7 @@ pQueryDRepStakeDistributionCmd era envCli = do
<$> pSocketPath envCli
<*> pConsensusModeParams
<*> pNetworkId envCli
<*> pAllOrOnlyDRepVerificationKeyOrHashOrFile
<*> pAllOrOnlyDRepSource
<*> optional pOutputFile

pQueryGetCommitteeStateCmd :: ()
Expand Down
30 changes: 21 additions & 9 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import Data.Functor ((<&>))
import qualified Data.List as List
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe (mapMaybe)
import Data.Proxy (Proxy (..))
import Data.Set (Set)
import qualified Data.Set as Set
Expand Down Expand Up @@ -1393,16 +1394,20 @@ runQueryDRepState
, Cmd.nodeSocketPath
, Cmd.consensusModeParams
, Cmd.networkId
, Cmd.drepKeys = drepKeys'
, Cmd.drepSources
, Cmd.mOutFile
} = conwayEraOnwardsConstraints eon $ do
let localNodeConnInfo = LocalNodeConnectInfo consensusModeParams networkId nodeSocketPath

let drepKeys = case drepKeys' of
let dreps' = case drepSources of
All -> []
Only l -> l
drepCreds <- Set.fromList <$> mapM (firstExceptT QueryCmdDRepKeyError . getDRepCredentialFromVerKeyHashOrFile) drepKeys
dreps <- mapM dRepFromSource dreps'

-- TODO: The queryDRepState expects credentials, but not all DReps are given
-- by a credential (namely, not the "always abstain" and "always no
-- confidence" one.) So, we filter so that only the credential DReps are left.
let drepCreds = Set.fromList $ mapMaybe ( \case Ledger.DRepCredential cred -> Just cred; _ -> Nothing) dreps
drepState <- runQuery localNodeConnInfo $ queryDRepState eon drepCreds
writeOutput mOutFile $
second drepStateToJson <$> Map.assocs drepState
Expand All @@ -1422,23 +1427,30 @@ runQueryDRepStakeDistribution
, Cmd.nodeSocketPath
, Cmd.consensusModeParams
, Cmd.networkId
, Cmd.drepKeys = drepKeys'
, Cmd.drepSources
, Cmd.mOutFile
} = conwayEraOnwardsConstraints eon $ do
let localNodeConnInfo = LocalNodeConnectInfo consensusModeParams networkId nodeSocketPath

let drepFromVrfKey = fmap Ledger.DRepCredential
. firstExceptT QueryCmdDRepKeyError
. getDRepCredentialFromVerKeyHashOrFile
drepKeys = case drepKeys' of
let dreps' = case drepSources of
All -> []
Only l -> l
dreps <- Set.fromList <$> mapM drepFromVrfKey drepKeys
dreps <- Set.fromList <$> mapM dRepFromSource dreps'

drepStakeDistribution <- runQuery localNodeConnInfo $ queryDRepStakeDistribution eon dreps
writeOutput mOutFile $
Map.assocs drepStakeDistribution

dRepFromSource :: DRepSource -> ExceptT QueryCmdError IO (Ledger.DRep StandardCrypto)
dRepFromSource (FromHash (DRepHashSourceVerificationKey vk)) = drepFromVrfKey vk
where
drepFromVrfKey = fmap Ledger.DRepCredential
. firstExceptT QueryCmdDRepKeyError
. getDRepCredentialFromVerKeyHashOrFile
dRepFromSource (FromHash (DRepHashSourceScript sh)) = return . Ledger.DRepCredential . Ledger.ScriptHashObj $ sh -- TODO: what's the problem here?
dRepFromSource AlwaysNoConfidence = return Ledger.DRepAlwaysNoConfidence
dRepFromSource AlwaysAbstain = return Ledger.DRepAlwaysAbstain

runQueryCommitteeMembersState
:: Cmd.QueryCommitteeMembersStateCmdArgs era
-> ExceptT QueryCmdError IO ()
Expand Down
3 changes: 3 additions & 0 deletions cardano-cli/src/Cardano/CLI/Types/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ module Cardano.CLI.Types.Key

, readDRepCredential

, DRepSource(..)

, SomeSigningKey(..)
, withSomeSigningKey
, readSigningKeyFile
Expand Down Expand Up @@ -339,6 +341,7 @@ readDRepCredential = \case
& onLeft (left . DelegationDRepReadError)
pure $ L.KeyHashObj drepKeyHash

data DRepSource = FromHash DRepHashSource | AlwaysAbstain | AlwaysNoConfidence deriving (Eq, Show)

data SomeSigningKey
= AByronSigningKey (SigningKey ByronKey)
Expand Down
Loading