/
Governance.hs
109 lines (90 loc) · 4.11 KB
/
Governance.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
module Cardano.CLI.Shelley.Run.Governance
( ShelleyGovernanceError
, renderShelleyGovernanceError
, runGovernanceCmd
) where
import Cardano.Prelude
import qualified Data.Text as Text
import Control.Monad.Trans.Except (ExceptT)
import Control.Monad.Trans.Except.Extra (firstExceptT, left, newExceptT, right)
import Cardano.Api.TextView (TextViewDescription (..), textShow)
import Cardano.Api.Typed
import Cardano.CLI.Shelley.Parsers
import Cardano.CLI.Types
import qualified Shelley.Spec.Ledger.TxData as Shelley
data ShelleyGovernanceError
= TextEnvReadError !(FileError TextEnvelopeError)
| TextEnvWriteError !(FileError ())
| GovernanceEmptyUpdateProposalError
| GovernanceMIRCertificateKeyRewardMistmach
!FilePath
!Int
-- ^ Number of stake verification keys
!Int
-- ^ Number of reward amounts
deriving Show
renderShelleyGovernanceError :: ShelleyGovernanceError -> Text
renderShelleyGovernanceError err =
case err of
TextEnvReadError fileErr -> Text.pack (displayError fileErr)
TextEnvWriteError fileErr -> Text.pack (displayError fileErr)
-- TODO: The equality check is still not working for empty update proposals.
GovernanceEmptyUpdateProposalError ->
"Empty update proposals are not allowed"
GovernanceMIRCertificateKeyRewardMistmach fp numVKeys numRwdAmts ->
"Error creating the MIR certificate at: " <> textShow fp
<> " The number of staking keys: " <> textShow numVKeys
<> " and the number of reward amounts: " <> textShow numRwdAmts
<> " are not equivalent."
runGovernanceCmd :: GovernanceCmd -> ExceptT ShelleyGovernanceError IO ()
runGovernanceCmd (GovernanceMIRCertificate mirpot vKeys rewards out) = runGovernanceMIRCertificate mirpot vKeys rewards out
runGovernanceCmd (GovernanceUpdateProposal out eNo genVKeys ppUp) = runGovernanceUpdateProposal out eNo genVKeys ppUp
runGovernanceMIRCertificate
:: Shelley.MIRPot
-> [VerificationKeyFile]
-- ^ Stake verification keys
-> [Lovelace]
-- ^ Reward amounts
-> OutputFile
-> ExceptT ShelleyGovernanceError IO ()
runGovernanceMIRCertificate mirPot vKeys rwdAmts (OutputFile oFp) = do
sCreds <- mapM readStakeKeyToCred vKeys
checkEqualKeyRewards vKeys rwdAmts
let mirCert = makeMIRCertificate mirPot (zip sCreds rwdAmts)
firstExceptT TextEnvWriteError
. newExceptT
$ writeFileTextEnvelope oFp (Just mirCertDesc) mirCert
where
mirCertDesc :: TextViewDescription
mirCertDesc = TextViewDescription "Move Instantaneous Rewards Certificate"
checkEqualKeyRewards :: [VerificationKeyFile] -> [Lovelace] -> ExceptT ShelleyGovernanceError IO ()
checkEqualKeyRewards keys rwds = do
let numVKeys = length keys
numRwdAmts = length rwds
if numVKeys == numRwdAmts
then return () else left $ GovernanceMIRCertificateKeyRewardMistmach oFp numVKeys numRwdAmts
readStakeKeyToCred :: VerificationKeyFile -> ExceptT ShelleyGovernanceError IO StakeCredential
readStakeKeyToCred (VerificationKeyFile stVKey) = do
stakeVkey <- firstExceptT TextEnvReadError
. newExceptT
$ readFileTextEnvelope (AsVerificationKey AsStakeKey) stVKey
right . StakeCredentialByKey $ verificationKeyHash stakeVkey
runGovernanceUpdateProposal
:: OutputFile
-> EpochNo
-> [VerificationKeyFile]
-- ^ Genesis verification keys
-> ProtocolParametersUpdate
-> ExceptT ShelleyGovernanceError IO ()
runGovernanceUpdateProposal (OutputFile upFile) eNo genVerKeyFiles upPprams = do
when (upPprams == mempty) $ left GovernanceEmptyUpdateProposalError
genVKeys <- sequence
[ firstExceptT TextEnvReadError . newExceptT $
readFileTextEnvelope
(AsVerificationKey AsGenesisKey)
vkeyFile
| VerificationKeyFile vkeyFile <- genVerKeyFiles ]
let genKeyHashes = map verificationKeyHash genVKeys
upProp = makeShelleyUpdateProposal upPprams genKeyHashes eNo
firstExceptT TextEnvWriteError . newExceptT $
writeFileTextEnvelope upFile Nothing upProp