Skip to content

Commit

Permalink
Propagate changes throughout the rest of cardano-api
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 committed Jul 17, 2023
1 parent 0a8a62f commit da9be77
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 67 deletions.
43 changes: 32 additions & 11 deletions cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -590,22 +590,43 @@ genTxCertificates :: CardanoEra era -> Gen (TxCertificates BuildTx era)
genTxCertificates era =
case certificatesSupportedInEra era of
Nothing -> pure TxCertificatesNone
Just supported -> do
certs <- Gen.list (Range.constant 0 3) genCertificate
Gen.choice
[ pure TxCertificatesNone
, pure (TxCertificates supported certs $ BuildTxWith mempty)
-- TODO: Generate certificates
]
Just supported ->
case cardanoEraStyle era of
LegacyByronEra -> pure TxCertificatesNone
ShelleyBasedEra sbe -> do
certs <- Gen.list (Range.constant 0 3) $ genCertificate sbe
Gen.choice
[ pure TxCertificatesNone
, pure (TxCertificates supported certs $ BuildTxWith mempty)
-- TODO: Generate certificates
]

-- TODO: Add remaining certificates
genCertificate :: Gen (Certificate era)
genCertificate =
-- TODO: This should be parameterised on ShelleyBasedEra
genCertificate :: ShelleyBasedEra era -> Gen (Certificate era)
genCertificate sbe =
Gen.choice
[ StakeAddressRegistrationCertificate <$> genStakeCredential
, StakeAddressDeregistrationCertificate <$> genStakeCredential
[ makeStakeAddressRegistrationCertificate <$> genStakeAddressRequirements sbe
, makeStakeAddressUnregistrationCertificate <$> genStakeAddressRequirements sbe
]

genStakeAddressRequirements :: ShelleyBasedEra era -> Gen (StakeAddressRequirements era)
genStakeAddressRequirements sbe =
case sbe of
ShelleyBasedEraShelley ->
StakeAddrRegistrationPreConway AtMostBabbageEraShelley <$> genStakeCredential
ShelleyBasedEraAllegra ->
StakeAddrRegistrationPreConway AtMostBabbageEraAllegra <$> genStakeCredential
ShelleyBasedEraMary ->
StakeAddrRegistrationPreConway AtMostBabbageEraMary <$> genStakeCredential
ShelleyBasedEraAlonzo ->
StakeAddrRegistrationPreConway AtMostBabbageEraAlonzo <$> genStakeCredential
ShelleyBasedEraBabbage ->
StakeAddrRegistrationPreConway AtMostBabbageEraBabbage <$> genStakeCredential
ShelleyBasedEraConway ->
StakeAddrRegistrationConway ConwayEraOnwardsConway <$> genLovelace <*> genStakeCredential


genTxUpdateProposal :: CardanoEra era -> Gen (TxUpdateProposal era)
genTxUpdateProposal era =
case updateProposalSupportedInEra era of
Expand Down
5 changes: 1 addition & 4 deletions cardano-api/internal/Cardano/Api/Convenience/Query.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}

Expand Down Expand Up @@ -88,9 +87,7 @@ queryStateForBalancedTx era allTxIns certs = runExceptT $ do
qeInMode <- pure (toEraInMode era CardanoMode)
& onNothing (left (EraConsensusModeMismatch (AnyConsensusMode CardanoMode) (getIsCardanoEraConstraint era $ AnyCardanoEra era)))

let stakeCreds = Set.fromList $ flip mapMaybe certs $ \case
StakeAddressDeregistrationCertificate cred -> Just cred
_ -> Nothing
let stakeCreds = Set.fromList $ mapMaybe (filterUnRegCreds sbe) certs

-- Query execution
utxo <- lift (queryUtxo qeInMode sbe (QueryUTxOByTxIn (Set.fromList allTxIns)))
Expand Down
29 changes: 13 additions & 16 deletions cardano-api/internal/Cardano/Api/Fees.hs
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ makeTransactionBodyAutoBalance systemstart history pparams poolids stakeDelegDep
failures
exUnitsMap'

txbodycontent1 <- substituteExecutionUnits exUnitsMap' txbodycontent
txbodycontent1 <- substituteExecutionUnits sbe exUnitsMap' txbodycontent

explicitTxFees <- first (const TxBodyErrorByronEraNotSupported) $
txFeesExplicitInEra era'
Expand Down Expand Up @@ -1174,11 +1174,12 @@ makeTransactionBodyAutoBalance systemstart history pparams poolids stakeDelegDep
(txOutInAnyEra txout)
minUTxO

substituteExecutionUnits :: Map ScriptWitnessIndex ExecutionUnits
substituteExecutionUnits :: ShelleyBasedEra era
-> Map ScriptWitnessIndex ExecutionUnits
-> TxBodyContent BuildTx era
-> Either TxBodyErrorAutoBalance (TxBodyContent BuildTx era)
substituteExecutionUnits exUnitsMap =
mapTxScriptWitnesses f
substituteExecutionUnits sbe exUnitsMap =
mapTxScriptWitnesses f sbe
where
f :: ScriptWitnessIndex
-> ScriptWitness witctx era
Expand All @@ -1195,9 +1196,10 @@ mapTxScriptWitnesses
(forall witctx. ScriptWitnessIndex
-> ScriptWitness witctx era
-> Either TxBodyErrorAutoBalance (ScriptWitness witctx era))
-> ShelleyBasedEra era
-> TxBodyContent BuildTx era
-> Either TxBodyErrorAutoBalance (TxBodyContent BuildTx era)
mapTxScriptWitnesses f txbodycontent@TxBodyContent {
mapTxScriptWitnesses f sbe txbodycontent@TxBodyContent {
txIns,
txWithdrawals,
txCertificates,
Expand All @@ -1206,7 +1208,7 @@ mapTxScriptWitnesses f txbodycontent@TxBodyContent {
mappedTxIns <- mapScriptWitnessesTxIns txIns
mappedWithdrawals <- mapScriptWitnessesWithdrawals txWithdrawals
mappedMintedVals <- mapScriptWitnessesMinting txMintValue
mappedTxCertificates <- mapScriptWitnessesCertificates txCertificates
mappedTxCertificates <- mapScriptWitnessesCertificates sbe txCertificates

Right $ txbodycontent
& setTxIns mappedTxIns
Expand Down Expand Up @@ -1271,18 +1273,19 @@ mapTxScriptWitnesses f txbodycontent@TxBodyContent {
adjustWitness g (ScriptWitness ctx witness') = ScriptWitness ctx <$> g witness'

mapScriptWitnessesCertificates
:: TxCertificates BuildTx era
:: ShelleyBasedEra era
-> TxCertificates BuildTx era
-> Either TxBodyErrorAutoBalance (TxCertificates BuildTx era)
mapScriptWitnessesCertificates TxCertificatesNone = Right TxCertificatesNone
mapScriptWitnessesCertificates (TxCertificates supported certs
mapScriptWitnessesCertificates _ TxCertificatesNone = Right TxCertificatesNone
mapScriptWitnessesCertificates sbe' (TxCertificates supported certs
(BuildTxWith witnesses)) =
let mappedScriptWitnesses
:: [(StakeCredential, Either TxBodyErrorAutoBalance (Witness WitCtxStake era))]
mappedScriptWitnesses =
[ (stakecred, ScriptWitness ctx <$> witness')
-- The certs are indexed in list order
| (ix, cert) <- zip [0..] certs
, stakecred <- maybeToList (selectStakeCredential cert)
, stakecred <- maybeToList (selectStakeCredential sbe' cert)
, ScriptWitness ctx witness
<- maybeToList (Map.lookup stakecred witnesses)
, let witness' = f (ScriptWitnessIndexCertificate ix) witness
Expand All @@ -1294,12 +1297,6 @@ mapTxScriptWitnesses f txbodycontent@TxBodyContent {
Right wit -> Right (sCred, wit)
) mappedScriptWitnesses

selectStakeCredential cert =
case cert of
StakeAddressDeregistrationCertificate stakecred -> Just stakecred
StakeAddressPoolDelegationCertificate stakecred _ -> Just stakecred
_ -> Nothing

mapScriptWitnessesMinting
:: TxMintValue BuildTx era
-> Either TxBodyErrorAutoBalance (TxMintValue BuildTx era)
Expand Down
29 changes: 12 additions & 17 deletions cardano-api/internal/Cardano/Api/TxBody.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2359,7 +2359,7 @@ createTransactionBody
-> Either TxBodyError (TxBody era)
createTransactionBody sbe txBodyContent =
let apiTxOuts = txOuts txBodyContent
apiScriptWitnesses = collectTxBodyScriptWitnesses txBodyContent
apiScriptWitnesses = collectTxBodyScriptWitnesses sbe txBodyContent
apiScriptValidity = txScriptValidity txBodyContent
apiMintValue = txMintValue txBodyContent
apiProtocolParameters = txProtocolParams txBodyContent
Expand Down Expand Up @@ -2553,7 +2553,7 @@ validateTxBodyContent sbe txBodContent@TxBodyContent {
txProtocolParams,
txMintValue,
txMetadata} =
let witnesses = collectTxBodyScriptWitnesses txBodContent
let witnesses = collectTxBodyScriptWitnesses sbe txBodContent
languages = Set.fromList
[ toAlonzoLanguage (AnyPlutusScriptVersion v)
| (_, AnyScriptWitness (PlutusScriptWitness _ v _ _ _ _)) <- witnesses
Expand Down Expand Up @@ -3679,7 +3679,7 @@ makeShelleyTransactionBody sbe@ShelleyBasedEraShelley
scripts_ = catMaybes
[ toShelleyScript <$> scriptWitnessScript scriptwitness
| (_, AnyScriptWitness scriptwitness)
<- collectTxBodyScriptWitnesses txbodycontent
<- collectTxBodyScriptWitnesses sbe txbodycontent
]

txAuxData :: Maybe (L.TxAuxData StandardShelley)
Expand Down Expand Up @@ -3716,7 +3716,7 @@ makeShelleyTransactionBody sbe@ShelleyBasedEraAllegra
scripts_ = catMaybes
[ toShelleyScript <$> scriptWitnessScript scriptwitness
| (_, AnyScriptWitness scriptwitness)
<- collectTxBodyScriptWitnesses txbodycontent
<- collectTxBodyScriptWitnesses sbe txbodycontent
]

txAuxData :: Maybe (L.TxAuxData StandardAllegra)
Expand Down Expand Up @@ -3755,7 +3755,7 @@ makeShelleyTransactionBody sbe@ShelleyBasedEraMary
scripts = List.nub $ catMaybes
[ toShelleyScript <$> scriptWitnessScript scriptwitness
| (_, AnyScriptWitness scriptwitness)
<- collectTxBodyScriptWitnesses txbodycontent
<- collectTxBodyScriptWitnesses sbe txbodycontent
]

txAuxData :: Maybe (L.TxAuxData StandardMary)
Expand Down Expand Up @@ -3801,7 +3801,7 @@ makeShelleyTransactionBody sbe@ShelleyBasedEraAlonzo
txScriptValidity
where
witnesses :: [(ScriptWitnessIndex, AnyScriptWitness AlonzoEra)]
witnesses = collectTxBodyScriptWitnesses txbodycontent
witnesses = collectTxBodyScriptWitnesses sbe txbodycontent

scripts :: [Ledger.Script StandardAlonzo]
scripts = List.nub $ catMaybes
Expand Down Expand Up @@ -3894,7 +3894,7 @@ makeShelleyTransactionBody sbe@ShelleyBasedEraBabbage
txScriptValidity
where
witnesses :: [(ScriptWitnessIndex, AnyScriptWitness BabbageEra)]
witnesses = collectTxBodyScriptWitnesses txbodycontent
witnesses = collectTxBodyScriptWitnesses sbe txbodycontent

scripts :: [Ledger.Script StandardBabbage]
scripts = List.nub $ catMaybes
Expand Down Expand Up @@ -3995,7 +3995,7 @@ makeShelleyTransactionBody sbe@ShelleyBasedEraConway
txScriptValidity
where
witnesses :: [(ScriptWitnessIndex, AnyScriptWitness ConwayEra)]
witnesses = collectTxBodyScriptWitnesses txbodycontent
witnesses = collectTxBodyScriptWitnesses sbe txbodycontent

scripts :: [Ledger.Script StandardConway]
scripts = catMaybes
Expand Down Expand Up @@ -4181,10 +4181,10 @@ fromAlonzoRdmrPtr (Alonzo.RdmrPtr tag n) =
Alonzo.Cert -> ScriptWitnessIndexCertificate (fromIntegral n)
Alonzo.Rewrd -> ScriptWitnessIndexWithdrawal (fromIntegral n)

collectTxBodyScriptWitnesses :: forall era.
TxBodyContent BuildTx era
collectTxBodyScriptWitnesses :: forall era. ShelleyBasedEra era
-> TxBodyContent BuildTx era
-> [(ScriptWitnessIndex, AnyScriptWitness era)]
collectTxBodyScriptWitnesses TxBodyContent {
collectTxBodyScriptWitnesses sbe TxBodyContent {
txIns,
txWithdrawals,
txCertificates,
Expand Down Expand Up @@ -4227,15 +4227,10 @@ collectTxBodyScriptWitnesses TxBodyContent {
-- The certs are indexed in list order
| (ix, cert) <- zip [0..] certs
, ScriptWitness _ witness <- maybeToList $ do
stakecred <- selectStakeCredential cert
stakecred <- obtainEraCryptoConstraints sbe $ selectStakeCredential sbe cert
Map.lookup stakecred witnesses
]

selectStakeCredential cert =
case cert of
StakeAddressDeregistrationCertificate stakecred -> Just stakecred
StakeAddressPoolDelegationCertificate stakecred _ -> Just stakecred
_ -> Nothing

scriptWitnessesMinting
:: TxMintValue BuildTx era
Expand Down
15 changes: 1 addition & 14 deletions cardano-api/internal/Cardano/Api/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ module Cardano.Api.Utils
, bounded

-- ** Constraint solvers
, obtainCertificateConstraints
, obtainCryptoConstraints
, obtainEraConstraints
, obtainEraPParamsConstraint
Expand All @@ -47,7 +46,6 @@ import qualified Cardano.Ledger.Core as Ledger
import Cardano.Ledger.Crypto (Crypto, StandardCrypto)
import qualified Cardano.Ledger.Crypto as Ledger
import Cardano.Ledger.Shelley ()
import qualified Cardano.Ledger.Shelley.TxCert as Shelley

import Control.Exception (bracket)
import Control.Monad (when)
Expand Down Expand Up @@ -204,16 +202,5 @@ obtainSafeToHashConstraint ShelleyBasedEraMary f = f
obtainSafeToHashConstraint ShelleyBasedEraAlonzo f = f
obtainSafeToHashConstraint ShelleyBasedEraBabbage f = f
obtainSafeToHashConstraint ShelleyBasedEraConway f = f
obtainCertificateConstraints

:: ShelleyBasedEra era
-> (( Shelley.ShelleyEraTxCert (ShelleyLedgerEra era)
, EraCrypto (ShelleyLedgerEra era) ~ StandardCrypto
) => a)
-> a
obtainCertificateConstraints ShelleyBasedEraShelley f = f
obtainCertificateConstraints ShelleyBasedEraAllegra f = f
obtainCertificateConstraints ShelleyBasedEraMary f = f
obtainCertificateConstraints ShelleyBasedEraAlonzo f = f
obtainCertificateConstraints ShelleyBasedEraBabbage f = f
obtainCertificateConstraints ShelleyBasedEraConway f = f

17 changes: 12 additions & 5 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,9 @@ module Cardano.Api (
-- | Certificates that are embedded in transactions for registering and
-- unregistering stake address, and for setting the stake pool delegation
-- choice for a stake address.
StakeAddressRequirements(..),
makeStakeAddressRegistrationCertificate,
makeStakeAddressDeregistrationCertificate,
makeStakeAddressUnregistrationCertificate,
makeStakeAddressPoolDelegationCertificate,

-- ** Registering stake pools
Expand Down Expand Up @@ -792,6 +793,7 @@ module Cardano.Api (
makeMIRCertificate,
makeGenesisKeyDelegationCertificate,
MIRTarget (..),
MIRPot(..),

-- * Protocol parameter updates
UpdateProposal(..),
Expand Down Expand Up @@ -894,7 +896,10 @@ module Cardano.Api (
querySystemStart,
queryUtxo,
determineEraExpr,
-- * Governance

-- ** Conway related
AtMostBabbageEra(..),
ConwayEraOnwards(..),

-- ** DReps
DRepKey,
Expand All @@ -903,9 +908,11 @@ module Cardano.Api (
DRepMetadataValidationError,
validateAndHashDRepMetadata,

-- ** Governance Committee
makeCommitteeDelegationCertificate,
makeCommitteeHotKeyUnregistrationCertificate,
-- ** Governance related certificates
makeCommitteeColdkeyResignationCertificate,
makeCommitteeHotKeyAuthorizationCertificate,
makeDrepRegistrationCertificate,
makeDrepUnregistrationCertificate,

ResolvablePointers(..),
) where
Expand Down

0 comments on commit da9be77

Please sign in to comment.