Skip to content

Commit

Permalink
Change the CLI's MIR cert command to take stake addresses
Browse files Browse the repository at this point in the history
Instead of taking stake verification key files. Stake addresses is the
more sensible choice since MIR certs really contain a stake credential
and a stake address is basically a wrapper for a stake credential.

There's no need to keep both options (key files and stake addrs) since
we can already convert from a stake vkey file to a stake address.

It's strictly more general, since this now support script-flavour stake
addresses. And sometimes we only have the stake address, not the vkey.
  • Loading branch information
dcoutts authored and Jimbo4350 committed Mar 3, 2021
1 parent 8b1c229 commit 9b6e8e8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
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

0 comments on commit 9b6e8e8

Please sign in to comment.