Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameterize createAndValidateTransactionBody on ShelleyBasedEra era #378

7 changes: 4 additions & 3 deletions cardano-api/gen/Test/Gen/Cardano/Api/Byron.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Test.Gen.Cardano.Api.Byron
) where

import Cardano.Api hiding (txIns)
import Cardano.Api.Byron

import Data.Proxy

Expand All @@ -20,8 +21,8 @@ import Test.Tasty.Hedgehog
prop_byron_roundtrip_txbody_CBOR :: Property
prop_byron_roundtrip_txbody_CBOR = property $ do
let byron = ByronEra
x <- forAll $ makeSignedTransaction [] <$> genTxBodyByron
tripping x (serialiseTxLedgerCddl byron) (deserialiseTxLedgerCddl byron)
x <- forAll $ makeSignedByronTransaction [] <$> genTxBodyByron
tripping (ByronTx ByronEraOnlyByron x) (serialiseTxLedgerCddl byron) deserialiseByronTxCddl


prop_byron_roundtrip_tx_CBOR :: Property
Expand All @@ -42,7 +43,7 @@ prop_byron_roundtrip_Tx_Cddl :: Property
prop_byron_roundtrip_Tx_Cddl = property $ do
let byron = ByronEra
x <- forAll genTxByron
tripping x (serialiseTxLedgerCddl byron) (deserialiseTxLedgerCddl byron)
tripping x (serialiseTxLedgerCddl byron) deserialiseByronTxCddl

tests :: TestTree
tests = testGroup "Test.Gen.Cardano.Api.Byron"
Expand Down
28 changes: 13 additions & 15 deletions cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,13 @@ module Test.Gen.Cardano.Api.Typed

import Cardano.Api hiding (txIns)
import qualified Cardano.Api as Api
import Cardano.Api.Byron (KeyWitness (ByronKeyWitness),
import Cardano.Api.Byron (KeyWitness (ByronKeyWitness), Tx (ByronTx),
WitnessNetworkIdOrByronAddress (..))
import Cardano.Api.Eon.AllegraEraOnwards (allegraEraOnwardsToShelleyBasedEra)
import Cardano.Api.Pretty
import Cardano.Api.Error
import qualified Cardano.Api.Ledger as L
import qualified Cardano.Api.Ledger.Lens as A
import Cardano.Api.Pretty
import Cardano.Api.Script (scriptInEraToRefScript)
import Cardano.Api.Shelley
import qualified Cardano.Api.Shelley as ShelleyApi
Expand Down Expand Up @@ -314,7 +315,7 @@ genScriptInEra era =
[ ScriptInEra langInEra <$> genScript lang
| AnyScriptLanguage lang <- [minBound..maxBound]
-- TODO: scriptLanguageSupportedInEra should be parameterized on ShelleyBasedEra
, Just langInEra <- [scriptLanguageSupportedInEra (toCardanoEra era) lang] ]
, Just langInEra <- [scriptLanguageSupportedInEra era lang] ]

genScriptHash :: Gen ScriptHash
genScriptHash = do
Expand Down Expand Up @@ -718,10 +719,11 @@ genAddressInEraByron :: Gen (AddressInEra ByronEra)
genAddressInEraByron = byronAddressInEra <$> genAddressByron

genTxByron :: Gen (Tx ByronEra)
genTxByron =
makeSignedTransaction
<$> genWitnessesByron
<*> genTxBodyByron
genTxByron = do
tx <- makeSignedByronTransaction
<$> genWitnessesByron
<*> genTxBodyByron
return $ ByronTx ByronEraOnlyByron tx

genTxOutValueByron :: Gen (TxOutValue ByronEra)
genTxOutValueByron = TxOutValueByron <$> genPositiveLovelace
Expand All @@ -733,24 +735,20 @@ genTxOutByron =
<*> pure TxOutDatumNone
<*> pure ReferenceScriptNone

genTxBodyByron :: Gen (TxBody ByronEra)
genTxBodyByron :: Gen (L.Annotated L.Tx ByteString)
genTxBodyByron = do
txIns <- map (, BuildTxWith (KeyWitness KeyWitnessForSpending)) <$> Gen.list (Range.constant 1 10) genTxIn
txOuts <- Gen.list (Range.constant 1 10) genTxOutByron
let byronTxBodyContent = (defaultTxBodyContent ByronEra)
{ Api.txIns
, Api.txOuts
}
case Api.createAndValidateTransactionBody ByronEra byronTxBodyContent of
Left err -> fail $ prettyToString $ prettyError err
case Api.makeByronTransactionBody txIns txOuts of
Left err -> fail (displayError err)
Right txBody -> pure txBody

genWitnessesByron :: Gen [KeyWitness ByronEra]
genWitnessesByron = Gen.list (Range.constant 1 10) genByronKeyWitness

genTxBody :: ShelleyBasedEra era -> Gen (TxBody era)
genTxBody era = do
res <- Api.createAndValidateTransactionBody (toCardanoEra era) <$> genTxBodyContent era
res <- Api.createAndValidateTransactionBody era <$> genTxBodyContent era
case res of
Left err -> fail (prettyToString (prettyError err))
Right txBody -> pure txBody
Expand Down
12 changes: 4 additions & 8 deletions cardano-api/internal/Cardano/Api/Fees.hs
Original file line number Diff line number Diff line change
Expand Up @@ -579,10 +579,6 @@ evaluateTransactionBalance :: forall era. ()
-> UTxO era
-> TxBody era
-> TxOutValue era
evaluateTransactionBalance sbe _ _ _ _ _ (ByronTxBody ByronEraOnlyByron _) =
-- TODO: we could actually support Byron here, it'd be different but simpler
case sbe of {}

evaluateTransactionBalance sbe pp poolids stakeDelegDeposits drepDelegDeposits utxo (ShelleyTxBody _ txbody _ _ _ _) =
shelleyBasedEraConstraints sbe
$ TxOutValueShelleyBased sbe
Expand Down Expand Up @@ -803,7 +799,7 @@ makeTransactionBodyAutoBalance sbe systemstart history lpp@(LedgerProtocolParame
-- 3. update tx with fees
-- 4. balance the transaction and update tx change output
txbody0 <-
first TxBodyError $ createAndValidateTransactionBody era txbodycontent
first TxBodyError $ createAndValidateTransactionBody sbe txbodycontent
{ txOuts = txOuts txbodycontent ++
[TxOut changeaddr (lovelaceToTxOutValue sbe 0) TxOutDatumNone ReferenceScriptNone]
--TODO: think about the size of the change output
Expand Down Expand Up @@ -854,7 +850,7 @@ makeTransactionBodyAutoBalance sbe systemstart history lpp@(LedgerProtocolParame

let (dummyCollRet, dummyTotColl) = maybeDummyTotalCollAndCollReturnOutput txbodycontent changeaddr
txbody1 <- first TxBodyError $ -- TODO: impossible to fail now
createAndValidateTransactionBody era txbodycontent1 {
createAndValidateTransactionBody sbe txbodycontent1 {
txFee = TxFeeExplicit sbe maxLovelaceFee,
txOuts = TxOut changeaddr changeTxOut TxOutDatumNone ReferenceScriptNone
: txOuts txbodycontent,
Expand All @@ -880,7 +876,7 @@ makeTransactionBodyAutoBalance sbe systemstart history lpp@(LedgerProtocolParame
-- Here we do not want to start with any change output, since that's what
-- we need to calculate.
txbody2 <- first TxBodyError $ -- TODO: impossible to fail now
createAndValidateTransactionBody era txbodycontent1 {
createAndValidateTransactionBody sbe txbodycontent1 {
txFee = TxFeeExplicit sbe fee,
txReturnCollateral = retColl,
txTotalCollateral = reqCol
Expand Down Expand Up @@ -913,7 +909,7 @@ makeTransactionBodyAutoBalance sbe systemstart history lpp@(LedgerProtocolParame
first TxBodyError $ -- TODO: impossible to fail now. We need to implement a function
-- that simply creates a transaction body because we have already
-- validated the transaction body earlier within makeTransactionBodyAutoBalance
createAndValidateTransactionBody era finalTxBodyContent
createAndValidateTransactionBody sbe finalTxBodyContent
return (BalancedTxBody finalTxBodyContent txbody3 (TxOut changeaddr balance TxOutDatumNone ReferenceScriptNone) fee)
where
-- Essentially we check for the existence of collateral inputs. If they exist we
Expand Down
5 changes: 3 additions & 2 deletions cardano-api/internal/Cardano/Api/Governance/Poll.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module Cardano.Api.Governance.Poll(
verifyPollAnswer,
) where

import Cardano.Api.Eon.ShelleyBasedEra
import Cardano.Api.Eras
import Cardano.Api.Hash
import Cardano.Api.HasTypeProxy
Expand Down Expand Up @@ -326,9 +327,9 @@ renderGovernancePollError err =
-- (the existence of the transaction in the ledger provides this guarantee).
verifyPollAnswer
:: GovernancePoll
-> InAnyCardanoEra Tx
-> InAnyShelleyBasedEra Tx
-> Either GovernancePollError [Hash PaymentKey]
verifyPollAnswer poll (InAnyCardanoEra _era (getTxBody -> TxBody body)) = do
verifyPollAnswer poll (InAnyShelleyBasedEra _era (getTxBody -> TxBody body)) = do
answer <- extractPollAnswer (txMetadata body)
answer `hasMatchingHash` hashGovernancePoll poll
answer `isAmongAcceptableChoices` govPollAnswers poll
Expand Down
9 changes: 7 additions & 2 deletions cardano-api/internal/Cardano/Api/IPC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ module Cardano.Api.IPC (
) where

import Cardano.Api.Block
import Cardano.Api.Eras
import Cardano.Api.HasTypeProxy
import Cardano.Api.InMode
import Cardano.Api.IO
Expand All @@ -85,7 +86,7 @@ import Cardano.Api.Modes
import Cardano.Api.NetworkId
import Cardano.Api.Protocol
import Cardano.Api.Query
import Cardano.Api.Tx (getTxBody)
import Cardano.Api.Tx
import Cardano.Api.TxBody

import qualified Cardano.Ledger.Api as L
Expand Down Expand Up @@ -630,7 +631,11 @@ instance ToJSON LocalTxMonitoringResult where
]
where
txId = case txInMode of
Just (TxInMode _ tx) -> Just $ getTxId $ getTxBody tx
Just (TxInMode e tx) ->
case e of
-- NB: Local tx protocol is not possible in the Byron era
ByronEra -> error "ToJSON LocalTxMonitoringResult: Byron era not supported"
_ -> Just $ getTxId $ getTxBody tx
-- TODO: support fetching the ID of a Byron Era transaction
_ -> Nothing
LocalTxMonitoringMempoolSizeAndCapacity mempool slot ->
Expand Down
6 changes: 6 additions & 0 deletions cardano-api/internal/Cardano/Api/ReexposeLedger.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ module Cardano.Api.ReexposeLedger
, drepDepositL
, csCommitteeCredsL

-- Byron
, Annotated
, Byron.Tx(..)

-- Babbage
, CoinPerByte (..)

Expand Down Expand Up @@ -103,6 +107,7 @@ module Cardano.Api.ReexposeLedger
, EpochNo(..)
) where

import qualified Cardano.Chain.UTxO as Byron
import Cardano.Crypto.Hash.Class (hashFromBytes, hashToBytes)
import Cardano.Ledger.Alonzo.Core (CoinPerWord (..), PParamsUpdate (..))
import Cardano.Ledger.Alonzo.Scripts (Prices (..))
Expand All @@ -116,6 +121,7 @@ import Cardano.Ledger.Babbage.Core (CoinPerByte (..))
import Cardano.Ledger.BaseTypes (DnsName, Network (..), StrictMaybe (..), Url,
boundRational, dnsToText, maybeToStrictMaybe, portToWord16, strictMaybeToMaybe,
textToDns, textToUrl, unboundRational, urlToText)
import Cardano.Ledger.Binary (Annotated (..))
import Cardano.Ledger.CertState (DRepState, csCommitteeCredsL)
import Cardano.Ledger.Coin (Coin (..), addDeltaCoin, toDeltaCoin)
import Cardano.Ledger.Conway.Core (DRepVotingThresholds (..), PoolVotingThresholds (..),
Expand Down
30 changes: 15 additions & 15 deletions cardano-api/internal/Cardano/Api/Script.hs
Original file line number Diff line number Diff line change
Expand Up @@ -578,45 +578,45 @@ instance HasTypeProxy era => HasTypeProxy (ScriptInEra era) where
-- | Check if a given script language is supported in a given era, and if so
-- return the evidence.
--
scriptLanguageSupportedInEra :: CardanoEra era
scriptLanguageSupportedInEra :: ShelleyBasedEra era
-> ScriptLanguage lang
-> Maybe (ScriptLanguageInEra lang era)
scriptLanguageSupportedInEra era lang =
case (era, lang) of
(ShelleyEra, SimpleScriptLanguage) ->
(ShelleyBasedEraShelley, SimpleScriptLanguage) ->
Just SimpleScriptInShelley

(AllegraEra, SimpleScriptLanguage) ->
(ShelleyBasedEraAllegra, SimpleScriptLanguage) ->
Just SimpleScriptInAllegra

(MaryEra, SimpleScriptLanguage) ->
(ShelleyBasedEraMary, SimpleScriptLanguage) ->
Just SimpleScriptInMary

(AlonzoEra, SimpleScriptLanguage) ->
(ShelleyBasedEraAlonzo, SimpleScriptLanguage) ->
Just SimpleScriptInAlonzo

(BabbageEra, SimpleScriptLanguage) ->
(ShelleyBasedEraBabbage, SimpleScriptLanguage) ->
Just SimpleScriptInBabbage

(ConwayEra, SimpleScriptLanguage) ->
(ShelleyBasedEraConway, SimpleScriptLanguage) ->
Just SimpleScriptInConway

(AlonzoEra, PlutusScriptLanguage PlutusScriptV1) ->
(ShelleyBasedEraAlonzo, PlutusScriptLanguage PlutusScriptV1) ->
Just PlutusScriptV1InAlonzo

(BabbageEra, PlutusScriptLanguage PlutusScriptV1) ->
(ShelleyBasedEraBabbage, PlutusScriptLanguage PlutusScriptV1) ->
Just PlutusScriptV1InBabbage

(BabbageEra, PlutusScriptLanguage PlutusScriptV2) ->
(ShelleyBasedEraBabbage, PlutusScriptLanguage PlutusScriptV2) ->
Just PlutusScriptV2InBabbage

(ConwayEra, PlutusScriptLanguage PlutusScriptV1) ->
(ShelleyBasedEraConway, PlutusScriptLanguage PlutusScriptV1) ->
Just PlutusScriptV1InConway

(ConwayEra, PlutusScriptLanguage PlutusScriptV2) ->
(ShelleyBasedEraConway, PlutusScriptLanguage PlutusScriptV2) ->
Just PlutusScriptV2InConway

(ConwayEra, PlutusScriptLanguage PlutusScriptV3) ->
(ShelleyBasedEraConway, PlutusScriptLanguage PlutusScriptV3) ->
Just PlutusScriptV3InConway

_ -> Nothing
Expand Down Expand Up @@ -664,7 +664,7 @@ eraOfScriptLanguageInEra langInEra =
-- | Given a target era and a script in some language, check if the language is
-- supported in that era, and if so return a 'ScriptInEra'.
--
toScriptInEra :: CardanoEra era -> ScriptInAnyLang -> Maybe (ScriptInEra era)
toScriptInEra :: ShelleyBasedEra era -> ScriptInAnyLang -> Maybe (ScriptInEra era)
toScriptInEra era (ScriptInAnyLang lang s) = do
lang' <- scriptLanguageSupportedInEra era lang
return (ScriptInEra lang' s)
Expand Down Expand Up @@ -1405,7 +1405,7 @@ instance IsCardanoEra era => FromJSON (ReferenceScript era) where
(cardanoEra :: CardanoEra era)

refScriptToShelleyScript
:: CardanoEra era
:: ShelleyBasedEra era
-> ReferenceScript era
-> StrictMaybe (Ledger.Script (ShelleyLedgerEra era))
refScriptToShelleyScript era (ReferenceScript _ s) =
Expand Down
Loading