-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Byron update proposal and vote api integration
- Loading branch information
Showing
10 changed files
with
250 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
-- | Special Byron values that we can submit to a node to propose an update proposal | ||
-- or to vote on an update proposal. These are not transactions. | ||
-- | ||
module Cardano.Api.SpecialByron | ||
( ByronUpdateProposal(..), | ||
serialiseByronUpdateProposal, | ||
deserialiseByronUpdateProposal, | ||
updateProposalToGenTx, | ||
ParametersToUpdate(..), | ||
createProtocolParametersUpdate, | ||
ByronVote(..), | ||
createByronUpdateProposal, | ||
createByronVote, | ||
serialiseByronVote, | ||
deserialiseByronVote, | ||
voteToGenTx, | ||
) where | ||
|
||
import Cardano.Prelude (void) | ||
import Prelude | ||
|
||
import Data.ByteString (ByteString) | ||
import qualified Data.ByteString.Lazy as LB | ||
import qualified Data.Map.Strict as M | ||
import Data.Word | ||
import Numeric.Natural | ||
|
||
|
||
import Cardano.Api.Key | ||
import Cardano.Api.KeysByron | ||
import Cardano.Api.NetworkId (NetworkId, toByronProtocolMagicId) | ||
|
||
import qualified Cardano.Binary as CBOR | ||
import Cardano.Chain.Common (LovelacePortion, TxFeePolicy) | ||
import Cardano.Chain.Slotting | ||
import Cardano.Chain.Update | ||
import Cardano.Crypto (SafeSigner, noPassSafeSigner) | ||
import Ouroboros.Consensus.Byron.Ledger.Block (ByronBlock) | ||
import qualified Ouroboros.Consensus.Byron.Ledger.Mempool as Mempool | ||
|
||
-- | Byron era update proposal | ||
|
||
newtype ByronUpdateProposal a = ByronUpdateProposal { unByronUpdateProposal :: AProposal a} | ||
|
||
createByronUpdateProposal | ||
:: NetworkId | ||
-> ProtocolVersion | ||
-> SoftwareVersion | ||
-> SystemTag | ||
-> InstallerHash | ||
-> SigningKey ByronKey | ||
-> [ParametersToUpdate] | ||
-> ByronUpdateProposal () | ||
createByronUpdateProposal nId pVer sVer sysTag insHash (ByronSigningKey sKey) paramsToUpdate = | ||
ByronUpdateProposal $ signProposal (toByronProtocolMagicId nId) proposalBody noPassSigningKey | ||
where | ||
proposalBody :: ProposalBody | ||
proposalBody = ProposalBody pVer protocolParamsUpdate sVer metaData | ||
|
||
metaData :: M.Map SystemTag InstallerHash | ||
metaData = M.singleton sysTag insHash | ||
|
||
noPassSigningKey :: SafeSigner | ||
noPassSigningKey = noPassSafeSigner sKey | ||
|
||
protocolParamsUpdate :: ProtocolParametersUpdate | ||
protocolParamsUpdate = createProtocolParametersUpdate | ||
emptyProtocolParametersUpdate paramsToUpdate | ||
|
||
createProtocolParametersUpdate | ||
:: ProtocolParametersUpdate | ||
-> [ParametersToUpdate] | ||
-> ProtocolParametersUpdate | ||
createProtocolParametersUpdate = go | ||
where go i [] = i | ||
go i (paramToUpdate : rest) = | ||
case paramToUpdate of | ||
ScriptVersion val -> go i{ppuScriptVersion = Just val} rest | ||
SlotDuration val -> go i{ppuSlotDuration = Just val} rest | ||
MaxBlockSize val -> go i{ppuMaxBlockSize = Just val} rest | ||
MaxHeaderSize val -> go i{ppuMaxHeaderSize = Just val} rest | ||
MaxTxSize val -> go i{ppuMaxTxSize = Just val} rest | ||
MaxProposalSize val -> go i{ppuMaxProposalSize = Just val} rest | ||
MpcThd val -> go i{ppuMpcThd = Just val} rest | ||
HeavyDelThd val -> go i{ppuHeavyDelThd = Just val} rest | ||
UpdateVoteThd val -> go i{ppuUpdateVoteThd = Just val} rest | ||
UpdateProposalThd val -> go i{ppuUpdateProposalThd = Just val} rest | ||
UpdateProposalTTL val -> go i{ppuUpdateProposalTTL = Just val} rest | ||
SoftforkRuleParam val -> go i{ppuSoftforkRule = Just val} rest | ||
TxFeePolicy val -> go i{ppuTxFeePolicy = Just val} rest | ||
UnlockStakeEpoch val -> go i{ppuUnlockStakeEpoch = Just val} rest | ||
|
||
emptyProtocolParametersUpdate :: ProtocolParametersUpdate | ||
emptyProtocolParametersUpdate = | ||
ProtocolParametersUpdate | ||
{ ppuScriptVersion = Nothing | ||
, ppuSlotDuration = Nothing | ||
, ppuMaxBlockSize = Nothing | ||
, ppuMaxHeaderSize = Nothing | ||
, ppuMaxTxSize = Nothing | ||
, ppuMaxProposalSize = Nothing | ||
, ppuMpcThd = Nothing | ||
, ppuHeavyDelThd = Nothing | ||
, ppuUpdateVoteThd = Nothing | ||
, ppuUpdateProposalThd = Nothing | ||
, ppuUpdateProposalTTL = Nothing | ||
, ppuSoftforkRule = Nothing | ||
, ppuTxFeePolicy = Nothing | ||
, ppuUnlockStakeEpoch = Nothing | ||
} | ||
|
||
data ParametersToUpdate = | ||
ScriptVersion Word16 | ||
| SlotDuration Natural | ||
| MaxBlockSize Natural | ||
| MaxHeaderSize Natural | ||
| MaxTxSize Natural | ||
| MaxProposalSize Natural | ||
| MpcThd LovelacePortion | ||
| HeavyDelThd LovelacePortion | ||
| UpdateVoteThd LovelacePortion | ||
-- ^ UpdateVoteThd: This represents the minimum percentage of the total number of genesis | ||
-- keys that have to endorse a protocol version to be able to become adopted. | ||
| UpdateProposalThd LovelacePortion | ||
-- ^ UpdateProposalTTL: If after the number of slots specified the proposal | ||
-- does not reach majority of approvals, the proposal is simply discarded. | ||
| UpdateProposalTTL SlotNumber | ||
| SoftforkRuleParam SoftforkRule | ||
| TxFeePolicy TxFeePolicy | ||
| UnlockStakeEpoch EpochNumber | ||
deriving Show | ||
|
||
serialiseByronUpdateProposal :: ByronUpdateProposal () -> ByteString | ||
serialiseByronUpdateProposal (ByronUpdateProposal proposal) = CBOR.serialize' proposal | ||
|
||
deserialiseByronUpdateProposal :: ByteString -> Either CBOR.DecoderError (ByronUpdateProposal ByteString) | ||
deserialiseByronUpdateProposal bs = | ||
let lBs = LB.fromStrict bs | ||
in case CBOR.decodeFull lBs of | ||
Left deserFail -> Left deserFail | ||
Right proposal -> Right . ByronUpdateProposal $ annotateProposal proposal lBs | ||
where | ||
annotateProposal :: AProposal CBOR.ByteSpan -> LB.ByteString -> AProposal ByteString | ||
annotateProposal proposal bs' = CBOR.annotationBytes bs' proposal | ||
|
||
updateProposalToGenTx :: ByronUpdateProposal ByteString -> Mempool.GenTx ByronBlock | ||
updateProposalToGenTx (ByronUpdateProposal proposal) = | ||
Mempool.ByronUpdateProposal (recoverUpId proposal) proposal | ||
|
||
-- | Byron era votes | ||
|
||
data ByronVote a = ByronVote { unByronVote :: AVote a} | ||
|
||
createByronVote :: NetworkId -> SigningKey ByronKey -> ByronUpdateProposal ByteString -> Bool -> ByronVote () | ||
createByronVote nId (ByronSigningKey sKey) (ByronUpdateProposal proposal) yesOrNo = | ||
ByronVote (mkVote (toByronProtocolMagicId nId) sKey (recoverUpId proposal) yesOrNo) | ||
|
||
serialiseByronVote :: ByronVote () -> ByteString | ||
serialiseByronVote (ByronVote vote) = CBOR.serialize' (void vote) | ||
|
||
deserialiseByronVote :: ByteString -> Either CBOR.DecoderError (ByronVote ByteString) | ||
deserialiseByronVote bs = | ||
let lBs = LB.fromStrict bs | ||
in case CBOR.decodeFull lBs of | ||
Left deserFail -> Left deserFail | ||
Right vote -> Right . ByronVote $ annotateVote vote lBs | ||
where | ||
annotateVote :: AVote CBOR.ByteSpan -> LB.ByteString -> AVote ByteString | ||
annotateVote vote bs' = CBOR.annotationBytes bs' vote | ||
|
||
voteToGenTx :: ByronVote ByteString -> Mempool.GenTx ByronBlock | ||
voteToGenTx (ByronVote vote) = Mempool.ByronUpdateVote (recoverVoteId vote) vote | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.