Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cardano-api/cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ library
Cardano.Api.Experimental.Tx.Internal.AnyWitness
Cardano.Api.Experimental.Tx.Internal.Body
Cardano.Api.Experimental.Tx.Internal.Certificate
Cardano.Api.Experimental.Tx.Internal.Compatible
Cardano.Api.Experimental.Tx.Internal.Fee
Cardano.Api.Experimental.Tx.Internal.TxScriptWitnessRequirements
Cardano.Api.Genesis.Internal
Expand Down
20 changes: 16 additions & 4 deletions cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -792,21 +792,33 @@ genTxCertificates =
certs <- Gen.list (Range.constant 0 3) $ genCertificate w
Gen.choice
[ pure TxCertificatesNone
, pure (TxCertificates w $ fromList ((,BuildTxWith Nothing) <$> certs))
-- TODO: Generate certificates
, pure
( TxCertificates w $
fromList ((,BuildTxWith Nothing) <$> map extractCertificate certs)
)
-- TODO: Generate certificates
]
)

extractCertificate
:: Api.Certificate era
-> Exp.Certificate (ShelleyLedgerEra era)
extractCertificate (Api.ShelleyRelatedCertificate w c) =
shelleyToBabbageEraConstraints w $ Exp.Certificate c
extractCertificate (ConwayCertificate w c) =
conwayEraOnwardsConstraints w $ Exp.Certificate c

genScriptWitnessedTxCertificates :: Typeable era => Exp.Era era -> Gen (TxCertificates BuildTx era)
genScriptWitnessedTxCertificates era = do
let w = convert era
num <- Gen.integral (Range.linear 0 3)
certs <- Gen.list (Range.singleton num) $ genCertificate w
plutusScriptWits <- Gen.list (Range.singleton num) $ genApiPlutusScriptWitness WitCtxStake era
plutusScriptWits <-
Gen.list (Range.singleton num) $ genApiPlutusScriptWitness WitCtxStake era
let certsAndWits =
zipWith
(\c p -> (c, Just p))
certs
(map extractCertificate certs)
plutusScriptWits

pure $ mkTxCertificates (convert era) certsAndWits
Expand Down
70 changes: 18 additions & 52 deletions cardano-api/src/Cardano/Api/Certificate/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ module Cardano.Api.Certificate.Internal
, certificateToTxCert
, filterUnRegCreds
, filterUnRegDRepCreds
, getTxCertWitness
, isDRepRegOrUpdateCert
)
where
Expand All @@ -85,6 +86,7 @@ import Cardano.Api.Certificate.Internal.DRepMetadata
import Cardano.Api.Certificate.Internal.StakePoolMetadata
import Cardano.Api.Era
import Cardano.Api.Error (Error (..))
import Cardano.Api.Experimental.Tx.Internal.Certificate qualified as Exp
import Cardano.Api.Governance.Internal.Action.VotingProcedure
import Cardano.Api.HasTypeProxy
import Cardano.Api.Internal.Utils (noInlineMaybeToStrictMaybe)
Expand All @@ -103,6 +105,7 @@ import Cardano.Ledger.Coin qualified as L
import Cardano.Ledger.Keys qualified as Ledger
import Cardano.Ledger.State qualified as Ledger

import Control.Monad
import Control.Monad.Except (MonadError (..))
import Data.ByteString (ByteString)
import Data.ByteString qualified as BS
Expand All @@ -120,6 +123,8 @@ import Network.Socket (PortNumber)
-- Certificates embedded in transactions
--

{-# DEPRECATED Certificate "Use `Certificate` type from Cardano.Api.Experimental.Tx.Internal.Certificate instead" #-}

data Certificate era where
-- Pre-Conway
-- 1. Stake registration
Expand Down Expand Up @@ -567,60 +572,21 @@ selectStakeCredentialWitness = \case
getTxCertWitness (convert cEra) conwayCert

filterUnRegCreds
:: Certificate era -> Maybe StakeCredential
filterUnRegCreds =
fmap fromShelleyStakeCredential . \case
ShelleyRelatedCertificate stbEra shelleyCert -> shelleyToBabbageEraConstraints stbEra $
case shelleyCert of
Ledger.RegTxCert _ -> Nothing
Ledger.UnRegTxCert cred -> Just cred
Ledger.DelegStakeTxCert _ _ -> Nothing
Ledger.RegPoolTxCert _ -> Nothing
Ledger.RetirePoolTxCert _ _ -> Nothing
Ledger.MirTxCert _ -> Nothing
Ledger.GenesisDelegTxCert{} -> Nothing
_ -> error "dijkstra"
ConwayCertificate cEra conwayCert -> conwayEraOnwardsConstraints cEra $
case conwayCert of
Ledger.RegPoolTxCert _ -> Nothing
Ledger.RetirePoolTxCert _ _ -> Nothing
Ledger.RegDepositTxCert _ _ -> Nothing
Ledger.UnRegDepositTxCert cred _ -> Just cred
Ledger.DelegTxCert _ _ -> Nothing
Ledger.RegDepositDelegTxCert{} -> Nothing
Ledger.AuthCommitteeHotKeyTxCert{} -> Nothing
Ledger.ResignCommitteeColdTxCert{} -> Nothing
Ledger.RegDRepTxCert{} -> Nothing
Ledger.UnRegDRepTxCert{} -> Nothing
Ledger.UpdateDRepTxCert{} -> Nothing
-- those are old shelley patterns
Ledger.RegTxCert _ -> Nothing
-- stake cred deregistration w/o deposit
Ledger.UnRegTxCert cred -> Just cred
_ -> error "dijkstra"
:: ShelleyBasedEra era -> Exp.Certificate (ShelleyLedgerEra era) -> Maybe StakeCredential
filterUnRegCreds sbe (Exp.Certificate cert) =
fmap fromShelleyStakeCredential $
shelleyBasedEraConstraints sbe $
Ledger.lookupUnRegStakeTxCert cert
Copy link
Contributor

@carbolymer carbolymer Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's cool, so many hours were wasted here on figuring out the correct implementation here.


filterUnRegDRepCreds
:: Certificate era -> Maybe (Ledger.Credential Ledger.DRepRole)
filterUnRegDRepCreds = \case
ShelleyRelatedCertificate _ _ -> Nothing
ConwayCertificate cEra conwayCert -> conwayEraOnwardsConstraints cEra $
case conwayCert of
Ledger.RegPoolTxCert _ -> Nothing
Ledger.RetirePoolTxCert _ _ -> Nothing
Ledger.RegDepositTxCert _ _ -> Nothing
Ledger.UnRegDepositTxCert _ _ -> Nothing
Ledger.DelegTxCert _ _ -> Nothing
Ledger.RegDepositDelegTxCert{} -> Nothing
Ledger.AuthCommitteeHotKeyTxCert{} -> Nothing
Ledger.ResignCommitteeColdTxCert{} -> Nothing
Ledger.RegDRepTxCert{} -> Nothing
Ledger.UnRegDRepTxCert cred _ -> Just cred
Ledger.UpdateDRepTxCert{} -> Nothing
-- those are old shelley patterns
Ledger.RegTxCert _ -> Nothing
-- stake cred deregistration w/o deposit
Ledger.UnRegTxCert _ -> Nothing
_ -> error "dijkstra"
:: ShelleyBasedEra era
-> Exp.Certificate (ShelleyLedgerEra era)
-> Maybe (Ledger.Credential Ledger.DRepRole)
filterUnRegDRepCreds sbe (Exp.Certificate cert) =
join $ forEraInEonMaybe (toCardanoEra sbe) $ \w ->
conwayEraOnwardsConstraints w $
fst
<$> Ledger.getUnRegDRepTxCert cert

-- ----------------------------------------------------------------------------
-- Internal conversion functions
Expand Down
9 changes: 7 additions & 2 deletions cardano-api/src/Cardano/Api/Compatible/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ module Cardano.Api.Compatible.Tx
where

import Cardano.Api.Address (StakeCredential)
import Cardano.Api.Certificate.Internal (Certificate)
import Cardano.Api.Era
import Cardano.Api.Experimental.Tx.Internal.Certificate qualified as Exp
import Cardano.Api.Plutus.Internal.Script
import Cardano.Api.ProtocolParameters
import Cardano.Api.Tx.Internal.Body
Expand Down Expand Up @@ -155,7 +155,12 @@ createCompatibleTx sbe ins outs txFee' anyProtocolUpdate anyVote txCertificates'
(L.bodyTxL . L.votingProceduresTxBodyL) .~ votingProcedures

indexedTxCerts
:: [(ScriptWitnessIndex, Certificate era, StakeCredential, Witness WitCtxStake era)]
:: [ ( ScriptWitnessIndex
, Exp.Certificate (ShelleyLedgerEra era)
, StakeCredential
, Witness WitCtxStake era
)
]
indexedTxCerts = indexTxCertificates txCertificates'

setScriptWitnesses
Expand Down
5 changes: 3 additions & 2 deletions cardano-api/src/Cardano/Api/Experimental.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ module Cardano.Api.Experimental
, hashTxBody
, evaluateTransactionExecutionUnitsShelley
, Certificate (..)
, convertToNewCertificate
, convertToOldApiCertificate
, AnchorDataFromCertificateError (..)
, getAnchorDataFromCertificate
, mkTxCertificates

-- ** Transaction fee related
Expand Down Expand Up @@ -78,5 +78,6 @@ import Cardano.Api.Experimental.Plutus.Internal.Shim.LegacyScripts
import Cardano.Api.Experimental.Simple.Script
import Cardano.Api.Experimental.Tx
import Cardano.Api.Experimental.Tx.Internal.Certificate
import Cardano.Api.Experimental.Tx.Internal.Compatible
import Cardano.Api.Experimental.Tx.Internal.Fee
import Cardano.Api.Tx.Internal.Fee (evaluateTransactionExecutionUnitsShelley)
3 changes: 2 additions & 1 deletion cardano-api/src/Cardano/Api/Experimental/Era.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ module Cardano.Api.Experimental.Era
)
where

import Cardano.Api.Consensus
import Cardano.Api.Consensus.Internal.Mode
import Cardano.Api.Consensus.Internal.Reexport
import Cardano.Api.Era qualified as Api
import Cardano.Api.Era.Internal.Core (BabbageEra, ConwayEra, DijkstraEra, Eon (..))
import Cardano.Api.Era.Internal.Eon.AlonzoEraOnwards
Expand Down
Loading
Loading