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

Change the CLI's MIR cert command to take stake addresses #2282

Merged
merged 1 commit into from Mar 3, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Shelley/Commands.hs
Expand Up @@ -307,7 +307,7 @@ renderQueryCmd cmd =
QueryProtocolState' {} -> "query protocol-state"

data GovernanceCmd
= GovernanceMIRCertificate MIRPot [VerificationKeyFile] [Lovelace] OutputFile
= GovernanceMIRCertificate MIRPot [StakeAddress] [Lovelace] OutputFile
| GovernanceGenesisKeyDelegationCertificate
(VerificationKeyOrHashOrFile GenesisKey)
(VerificationKeyOrHashOrFile GenesisDelegateKey)
Expand Down
16 changes: 12 additions & 4 deletions cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs
Expand Up @@ -65,7 +65,7 @@ parseShelleyCommands =
, Opt.command "address"
(Opt.info (AddressCmd <$> pAddressCmd) $ Opt.progDesc "Payment address commands")
, Opt.command "stake-address"
(Opt.info (StakeAddressCmd <$> pStakeAddress) $ Opt.progDesc "Stake address commands")
(Opt.info (StakeAddressCmd <$> pStakeAddressCmd) $ Opt.progDesc "Stake address commands")
, Opt.command "key"
(Opt.info (KeyCmd <$> pKeyCmd) $ Opt.progDesc "Key utility commands")
, Opt.command "transaction"
Expand Down Expand Up @@ -199,8 +199,8 @@ pScript = ScriptFile <$> Opt.strOption
<> Opt.completer (Opt.bashCompleter "file")
)

pStakeAddress :: Parser StakeAddressCmd
pStakeAddress =
pStakeAddressCmd :: Parser StakeAddressCmd
pStakeAddressCmd =
asum
[ subParser "key-gen"
(Opt.info pStakeAddressKeyGen $ Opt.progDesc "Create a stake address key pair")
Expand Down Expand Up @@ -737,7 +737,7 @@ pGovernanceCmd =
pMIRCertificate :: Parser GovernanceCmd
pMIRCertificate = GovernanceMIRCertificate
<$> pMIRPot
<*> some pStakeVerificationKeyFile
<*> some pStakeAddress
<*> some pRewardAmt
<*> pOutputFile

Expand Down Expand Up @@ -1779,6 +1779,14 @@ pAddress =
<> Opt.help "A Cardano address"
)

pStakeAddress :: Parser StakeAddress
pStakeAddress =
Opt.option (readerFromAttoParser parseStakeAddress)
( Opt.long "stake-address"
<> Opt.metavar "ADDRESS"
<> Opt.help "Target stake address (bech32 format)."
)

pStakeVerificationKeyOrFile :: Parser (VerificationKeyOrFile StakeKey)
pStakeVerificationKeyOrFile =
VerificationKeyValue <$> pStakeVerificationKey
Expand Down
35 changes: 13 additions & 22 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Governance.hs
Expand Up @@ -8,7 +8,7 @@ import Cardano.Prelude

import qualified Data.Text as Text

import Control.Monad.Trans.Except.Extra (firstExceptT, left, newExceptT, right)
import Control.Monad.Trans.Except.Extra (firstExceptT, left, newExceptT)

import Cardano.Api
import Cardano.Api.Shelley
Expand Down Expand Up @@ -62,18 +62,18 @@ runGovernanceCmd (GovernanceUpdateProposal out eNo genVKeys ppUp) =

runGovernanceMIRCertificate
:: Shelley.MIRPot
-> [VerificationKeyFile]
-- ^ Stake verification keys
-> [Lovelace]
-- ^ Reward amounts
-> [StakeAddress] -- ^ Stake addresses
-> [Lovelace] -- ^ Corresponding reward amounts (same length)
-> OutputFile
-> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceMIRCertificate mirPot vKeys rwdAmts (OutputFile oFp) = do
sCreds <- mapM readStakeKeyToCred vKeys
runGovernanceMIRCertificate mirPot sAddrs rwdAmts (OutputFile oFp) = do

checkEqualKeyRewards vKeys rwdAmts
unless (length sAddrs == length rwdAmts) $
left $ ShelleyGovernanceCmdMIRCertificateKeyRewardMistmach
oFp (length sAddrs) (length rwdAmts)

let mirCert = makeMIRCertificate mirPot (zip sCreds rwdAmts)
let sCreds = map stakeAddrToStakeCredential sAddrs
mirCert = makeMIRCertificate mirPot (zip sCreds rwdAmts)

firstExceptT ShelleyGovernanceCmdTextEnvWriteError
. newExceptT
Expand All @@ -82,19 +82,10 @@ runGovernanceMIRCertificate mirPot vKeys rwdAmts (OutputFile oFp) = do
mirCertDesc :: TextEnvelopeDescr
mirCertDesc = "Move Instantaneous Rewards Certificate"

checkEqualKeyRewards :: [VerificationKeyFile] -> [Lovelace] -> ExceptT ShelleyGovernanceCmdError IO ()
checkEqualKeyRewards keys rwds = do
let numVKeys = length keys
numRwdAmts = length rwds
if numVKeys == numRwdAmts
then return () else left $ ShelleyGovernanceCmdMIRCertificateKeyRewardMistmach oFp numVKeys numRwdAmts

readStakeKeyToCred :: VerificationKeyFile -> ExceptT ShelleyGovernanceCmdError IO StakeCredential
readStakeKeyToCred (VerificationKeyFile stVKey) = do
stakeVkey <- firstExceptT ShelleyGovernanceCmdTextEnvReadError
. newExceptT
$ readFileTextEnvelope (AsVerificationKey AsStakeKey) stVKey
right . StakeCredentialByKey $ verificationKeyHash stakeVkey
--TODO: expose a pattern for StakeAddress that give us the StakeCredential
stakeAddrToStakeCredential :: StakeAddress -> StakeCredential
stakeAddrToStakeCredential (StakeAddress _ scred) =
fromShelleyStakeCredential scred

runGovernanceGenesisKeyDelegationCertificate
:: VerificationKeyOrHashOrFile GenesisKey
Expand Down
Expand Up @@ -36,11 +36,12 @@ golden_shelleyMIRCertificate = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir

H.assertFilesExist [verKey, signKey]

let testAddr = "stake1u9j6axhcpd0exvrthn5dqzqt54g85akqvkn4uqmccm70qsc5hpv9w"
-- Create MIR certificate
void $ execCardanoCLI
[ "governance","create-mir-certificate"
, "--reserves" --TODO: Should also do "--reserves"
, "--stake-verification-key-file", verKey
, "--stake-address", testAddr
, "--reward", "1000"
, "--out-file", mirCertificate
]
Expand Down