Skip to content

Commit

Permalink
Add constraints to Proposal and Vote types, Add test generators for them
Browse files Browse the repository at this point in the history
  • Loading branch information
carbolymer committed Jul 28, 2023
1 parent 71fcd1e commit 81e2f5b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 43 deletions.
4 changes: 3 additions & 1 deletion cardano-api/cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,14 @@ library gen
, cardano-ledger-alonzo >= 1.3.1.1
, cardano-ledger-alonzo-test
, cardano-ledger-byron-test >= 1.5
, cardano-ledger-core >= 1.4
, cardano-ledger-core:{cardano-ledger-core, testlib} >= 1.4
, cardano-ledger-shelley >= 1.4.1.0
, cardano-ledger-conway:testlib >= 1.5
, containers
, filepath
, hedgehog >= 1.1
, hedgehog-extras
, hedgehog-quickcheck
, tasty
, tasty-hedgehog
, text
Expand Down
43 changes: 34 additions & 9 deletions cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
Expand Down Expand Up @@ -126,13 +128,7 @@ import qualified Cardano.Api as Api
import Cardano.Api.Byron (KeyWitness (ByronKeyWitness),
WitnessNetworkIdOrByronAddress (..))
import Cardano.Api.Script (scriptInEraToRefScript)
import Cardano.Api.Shelley (GovernancePoll (..), GovernancePollAnswer (..), Hash (..),
KESPeriod (KESPeriod),
OperationalCertificateIssueCounter (OperationalCertificateIssueCounter),
PlutusScript (PlutusScriptSerialised), ProtocolParameters (..),
ReferenceScript (..), ReferenceTxInsScriptsInlineDatumsSupportedInEra (..),
StakeCredential (StakeCredentialByKey), StakePoolKey,
refInsScriptsAndInlineDatsSupportedInEra)
import Cardano.Api.Shelley

import qualified Cardano.Binary as CBOR
import qualified Cardano.Crypto.Hash as Crypto
Expand All @@ -144,12 +140,14 @@ import Cardano.Ledger.SafeHash (unsafeMakeSafeHash)
import qualified Cardano.Ledger.Shelley.TxBody as Ledger (EraIndependentTxBody)

import Control.Applicative (Alternative (..), optional)
import Control.Monad
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Short as SBS
import Data.Coerce
import Data.Int (Int64)
import Data.Map.Strict (Map)
import Data.Maybe
import Data.Ratio (Ratio, (%))
import Data.String
import Data.Word (Word64)
Expand All @@ -160,9 +158,12 @@ import Test.Gen.Cardano.Api.Metadata (genTxMetadata)
import Test.Cardano.Chain.UTxO.Gen (genVKWitness)
import Test.Cardano.Crypto.Gen (genProtocolMagicId)
import qualified Test.Cardano.Ledger.Alonzo.PlutusScripts as Plutus
import Test.Cardano.Ledger.Conway.Arbitrary ()
import Test.Cardano.Ledger.Core.Arbitrary ()

import Hedgehog (Gen, Range)
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Gen.QuickCheck as Q
import qualified Hedgehog.Range as Range

{- HLINT ignore "Reduce duplication" -}
Expand Down Expand Up @@ -668,8 +669,8 @@ genTxBodyContent era = do
txUpdateProposal <- genTxUpdateProposal era
txMintValue <- genTxMintValue era
txScriptValidity <- genTxScriptValidity era
txGovernanceActions <- return TxGovernanceActionsNone -- TODO: Conway era
txVotes <- return TxVotesNone -- TODO: Conway era
txGovernanceActions <- genTxGovernanceActions era
txVotes <- genTxVotes era
pure $ TxBodyContent
{ Api.txIns
, Api.txInsCollateral
Expand Down Expand Up @@ -1107,3 +1108,27 @@ genGovernancePollAnswer =
where
genGovernancePollHash =
GovernancePollHash . mkDummyHash <$> Gen.int (Range.linear 0 10)

genTxGovernanceActions :: CardanoEra era -> Gen (TxGovernanceActions era)
genTxGovernanceActions era = fromMaybe (pure TxGovernanceActionsNone) $ do
sbe <- join $ requireShelleyBasedEra era
supported <- governanceActionsSupportedInEra sbe
let proposals = Gen.list (Range.constant 0 10) $ genProposal sbe supported
pure $ TxGovernanceActions supported <$> proposals
where
genProposal :: ShelleyBasedEra era
-> TxGovernanceActionSupportedInEra era
-> Gen (Proposal era)
genProposal sbe = shelleyBasedEraConstraints sbe $ fmap Proposal . \case
GovernanceActionsSupportedInConwayEra -> Q.arbitrary

genTxVotes :: CardanoEra era -> Gen (TxVotes era)
genTxVotes era = fromMaybe (pure TxVotesNone) $ do
sbe <- join $ requireShelleyBasedEra era
supported <- votesSupportedInEra sbe
let votes = Gen.list (Range.constant 0 10) $ genVote sbe
pure $ TxVotes supported <$> votes
where
genVote :: ShelleyBasedEra era -> Gen (VotingProcedure era)
genVote sbe = obtainEraConstraints sbe $ VotingProcedure <$> Q.arbitrary

2 changes: 0 additions & 2 deletions cardano-api/internal/Cardano/Api/Fees.hs
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,6 @@ estimateTransactionKeyWitnessCount TxBodyContent {

type PlutusScriptBytes = ShortByteString

-- TODO: Conway era - We don't really want to parameterise error
-- types on the error for now. As a stop gap we should implement a rendering function
data ResolvablePointers where
ResolvablePointers ::
( Ledger.Era (ShelleyLedgerEra era)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ data GovernanceAction


toSafeHash :: ByteString -> SafeHash StandardCrypto ByteString
toSafeHash = makeHashWithExplicitProxys (Proxy :: Proxy StandardCrypto) (Proxy :: Proxy ByteString)
toSafeHash = makeHashWithExplicitProxys (Proxy @StandardCrypto) (Proxy @ByteString)

toGovernanceAction
:: EraCrypto ledgerera ~ StandardCrypto
Expand Down Expand Up @@ -169,8 +169,8 @@ createProposalProcedure
-> GovernanceAction
-> Proposal era
createProposalProcedure sbe dep (StakeKeyHash retAddrh) govAct =
Proposal $ obtainEraCryptoConstraints sbe $
Gov.ProposalProcedure
obtainEraConstraints sbe $ obtainEraCryptoConstraints sbe $
Proposal Gov.ProposalProcedure
{ Gov.pProcDeposit = toShelleyLovelace dep
, Gov.pProcReturnAddr = retAddrh
, Gov.pProcGovernanceAction = toGovernanceAction sbe govAct
Expand All @@ -186,3 +186,4 @@ fromProposalProcedure sbe (Proposal pp) =
, StakeKeyHash (obtainEraCryptoConstraints sbe (Gov.pProcReturnAddr pp))
, obtainEraCryptoConstraints sbe $ fromGovernanceAction sbe (Gov.pProcGovernanceAction pp)
)

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
Expand All @@ -28,7 +29,6 @@ import Cardano.Api.Utils
import qualified Cardano.Binary as CBOR
import qualified Cardano.Ledger.BaseTypes as Ledger
import qualified Cardano.Ledger.Binary.Plain as Plain
import qualified Cardano.Ledger.Conway as Conway
import qualified Cardano.Ledger.Conway.Governance as Ledger
import Cardano.Ledger.Core (EraCrypto)
import qualified Cardano.Ledger.Core as Shelley
Expand Down Expand Up @@ -61,7 +61,6 @@ deriving instance Eq (TxVotes era)
-- The Conway and subsequent eras support governance actions.
--
data TxVotesSupportedInEra era where

VotesSupportedInConwayEra :: TxVotesSupportedInEra ConwayEra

deriving instance Show (TxVotesSupportedInEra era)
Expand Down Expand Up @@ -171,7 +170,7 @@ createVotingProcedure
-> GovernanceActionId (ShelleyLedgerEra era)
-> VotingProcedure era
createVotingProcedure sbe vChoice vt (GovernanceActionId govActId) =
obtainEraCryptoConstraints sbe
obtainEraConstraints sbe $ obtainEraCryptoConstraints sbe
$ VotingProcedure $ Ledger.VotingProcedure
{ Ledger.vProcGovActionId = govActId
, Ledger.vProcVoter = toVoterRole sbe vt
Expand All @@ -184,33 +183,21 @@ newtype VotingProcedure era = VotingProcedure
}
deriving (Show, Eq)

-- TODO: Conway - convert newtype VotingProcedure to a GADT with a ShelleyBasedEra era value
instance
(Shelley.Era (ShelleyLedgerEra era)
, IsShelleyBasedEra era
) => ToCBOR (VotingProcedure era) where
toCBOR (VotingProcedure vp) = Shelley.toEraCBOR @Conway.Conway vp

instance
( IsShelleyBasedEra era
, Shelley.Era (ShelleyLedgerEra era)
) => FromCBOR (VotingProcedure era) where
fromCBOR = VotingProcedure <$> Shelley.fromEraCBOR @Conway.Conway

instance
( IsShelleyBasedEra era
, Shelley.Era (ShelleyLedgerEra era)
) => SerialiseAsCBOR (VotingProcedure era) where
serialiseToCBOR = CBOR.serialize'
deserialiseFromCBOR _proxy = CBOR.decodeFull'


instance
( IsShelleyBasedEra era
, Shelley.Era (ShelleyLedgerEra era)
) => HasTextEnvelope (VotingProcedure era) where
instance IsShelleyBasedEra era => ToCBOR (VotingProcedure era) where
toCBOR (VotingProcedure vp) = obtainEraConstraints sbe $ Shelley.toEraCBOR @(ShelleyLedgerEra era) vp
where sbe = shelleyBasedEra @era

instance IsShelleyBasedEra era => FromCBOR (VotingProcedure era) where
fromCBOR = obtainEraConstraints (shelleyBasedEra @era) $ VotingProcedure <$> Shelley.fromEraCBOR @(ShelleyLedgerEra era)

instance IsShelleyBasedEra era => SerialiseAsCBOR (VotingProcedure era) where
serialiseToCBOR = obtainEraConstraints (shelleyBasedEra @era) CBOR.serialize'
deserialiseFromCBOR _proxy = obtainEraConstraints (shelleyBasedEra @era) CBOR.decodeFull'

instance IsShelleyBasedEra era => HasTextEnvelope (VotingProcedure era) where
textEnvelopeType _ = "Governance vote"

instance HasTypeProxy era => HasTypeProxy (VotingProcedure era) where
data AsType (VotingProcedure era) = AsVote
proxyToAsType _ = AsVote

0 comments on commit 81e2f5b

Please sign in to comment.