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

Add reference transaction inputs to cardano-api #3804

Merged
merged 4 commits into from
Apr 20, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ dummyTxSizeInEra metadata = case makeTransactionBody dummyTx of
txIns = [( TxIn "dbaff4e270cfb55612d9e2ac4658a27c79da4a5271c6f90853042d1403733810" (TxIx 0)
, BuildTxWith $ KeyWitness KeyWitnessForSpending )]
, txInsCollateral = TxInsCollateralNone
, txInsReference = TxInsReferenceNone
, txOuts = []
, txFee = mkFee 0
, txValidityRange = (TxValidityNoLowerBound, mkValidityUpperBound 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ mkGenesisTransaction key _payloadSize ttl fee txins txouts
txBodyContent = TxBodyContent {
txIns = zip txins $ repeat $ BuildTxWith $ KeyWitness KeyWitnessForSpending
, txInsCollateral = TxInsCollateralNone
, txInsReference = TxInsReferenceNone
, txOuts = txouts
, txFee = fees
, txValidityRange = (TxValidityNoLowerBound, validityUpperBound)
Expand Down
1 change: 1 addition & 0 deletions bench/tx-generator/src/Cardano/Benchmarking/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ genTx protocolParameters (collateral, collFunds) fee metadata witness inFunds ou
txBodyContent = TxBodyContent {
txIns = map (\f -> (getFundTxIn f, BuildTxWith witness)) inFunds
, txInsCollateral = collateral
, txInsReference = TxInsReferenceNone
, txOuts = outputs
, txFee = fee
, txValidityRange = (TxValidityNoLowerBound, upperBound)
Expand Down
19 changes: 16 additions & 3 deletions cardano-api/gen/Gen/Cardano/Api/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ import Cardano.Api.Byron (KeyWitness (ByronKeyWitness),
import Cardano.Api.Shelley (Hash (ScriptDataHash), KESPeriod (KESPeriod),
OperationalCertificateIssueCounter (OperationalCertificateIssueCounter),
PlutusScript (PlutusScriptSerialised), ProtocolParameters (ProtocolParameters),
ReferenceScript (..), StakeCredential (StakeCredentialByKey), StakePoolKey)
ReferenceScript (..), ReferenceTxInsScriptsInlineDatumsSupportedInEra (..),
StakeCredential (StakeCredentialByKey), StakePoolKey,
refInsScriptsAndInlineDatsSupportedInEra)

import Cardano.Prelude

Expand Down Expand Up @@ -524,6 +526,7 @@ genTxBodyContent :: CardanoEra era -> Gen (TxBodyContent BuildTx era)
genTxBodyContent era = do
txIns <- map (, BuildTxWith (KeyWitness KeyWitnessForSpending)) <$> Gen.list (Range.constant 1 10) genTxIn
txInsCollateral <- genTxInsCollateral era
txInsReference <- genTxInsReference era
txOuts <- Gen.list (Range.constant 1 10) (genTxOutTxContext era)
txTotalCollateral <- genTxTotalCollateral era
txReturnCollateral <- genTxReturnCollateral era
Expand All @@ -542,6 +545,7 @@ genTxBodyContent era = do
pure $ TxBodyContent
{ Api.txIns
, Api.txInsCollateral
, Api.txInsReference
, Api.txOuts
, Api.txTotalCollateral
, Api.txReturnCollateral
Expand All @@ -566,6 +570,15 @@ genTxInsCollateral era =
[ pure TxInsCollateralNone
, TxInsCollateral supported <$> Gen.list (Range.linear 0 10) genTxIn
]
genTxInsReference :: CardanoEra era -> Gen (TxInsReference era)
genTxInsReference era =
case refInsScriptsAndInlineDatsSupportedInEra era of
Nothing -> pure TxInsReferenceNone
Just supported -> Gen.choice
[ pure TxInsReferenceNone
, TxInsReference supported <$> Gen.list (Range.linear 0 10) genTxIn
]


genTxReturnCollateral :: CardanoEra era -> Gen (TxReturnCollateral CtxTx era)
genTxReturnCollateral era =
Expand Down Expand Up @@ -826,7 +839,7 @@ genTxOutDatumHashTxContext era = case era of
[ pure TxOutDatumNone
, TxOutDatumHash ScriptDataInBabbageEra <$> genHashScriptData
, TxOutDatumInTx ScriptDataInBabbageEra <$> genScriptData
, TxOutDatumInline InlineDatumSupportedInBabbageEra <$> genScriptData
, TxOutDatumInline ReferenceTxInsScriptsInlineDatumsInBabbageEra <$> genScriptData
]

genTxOutDatumHashUTxOContext :: CardanoEra era -> Gen (TxOutDatum CtxUTxO era)
Expand All @@ -842,7 +855,7 @@ genTxOutDatumHashUTxOContext era = case era of
BabbageEra -> Gen.choice
[ pure TxOutDatumNone
, TxOutDatumHash ScriptDataInBabbageEra <$> genHashScriptData
, TxOutDatumInline InlineDatumSupportedInBabbageEra <$> genScriptData
, TxOutDatumInline ReferenceTxInsScriptsInlineDatumsInBabbageEra <$> genScriptData
]

mkDummyHash :: forall h a. CRYPTO.HashAlgorithm h => Int -> CRYPTO.Hash h a
Expand Down
2 changes: 1 addition & 1 deletion cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ module Cardano.Api (

-- ** Other transaction body types
TxInsCollateral(..),
TxInsReference(..),
TxTotalCollateral(..),
TxReturnCollateral(..),
TxFee(..),
Expand Down Expand Up @@ -220,7 +221,6 @@ module Cardano.Api (
WithdrawalsSupportedInEra(..),
CertificatesSupportedInEra(..),
UpdateProposalSupportedInEra(..),
InlineDatumSupportedInEra(..),

-- ** Feature availability functions
collateralSupportedInEra,
Expand Down
31 changes: 16 additions & 15 deletions cardano-api/src/Cardano/Api/Script.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ module Cardano.Api.Script (

-- * Reference scripts
ReferenceScript(..),
ReferenceScriptsSupportedInEra(..),
referenceScriptsSupportedInEra,
ReferenceTxInsScriptsInlineDatumsSupportedInEra(..),
refInsScriptsAndInlineDatsSupportedInEra,
refScriptToShelleyScript,

-- * Use of a script in an era as a witness
Expand Down Expand Up @@ -1350,7 +1350,7 @@ parsePaymentKeyHash txt =
-- has to be added to the transaction, they can now be referenced via a transaction output.

data ReferenceScript era where
ReferenceScript :: ReferenceScriptsSupportedInEra era
ReferenceScript :: ReferenceTxInsScriptsInlineDatumsSupportedInEra era
-> ScriptInAnyLang
-> ReferenceScript era

Expand All @@ -1365,24 +1365,25 @@ instance IsCardanoEra era => ToJSON (ReferenceScript era) where

instance IsCardanoEra era => FromJSON (ReferenceScript era) where
parseJSON = Aeson.withObject "ReferenceScript" $ \o ->
case referenceScriptsSupportedInEra (cardanoEra :: CardanoEra era) of
case refInsScriptsAndInlineDatsSupportedInEra (cardanoEra :: CardanoEra era) of
Nothing -> pure ReferenceScriptNone
Just refSupInEra ->
ReferenceScript refSupInEra <$> o .: "referenceScript"

data ReferenceScriptsSupportedInEra era where
ReferenceScriptsInBabbageEra :: ReferenceScriptsSupportedInEra BabbageEra
data ReferenceTxInsScriptsInlineDatumsSupportedInEra era where
ReferenceTxInsScriptsInlineDatumsInBabbageEra :: ReferenceTxInsScriptsInlineDatumsSupportedInEra BabbageEra

deriving instance Eq (ReferenceScriptsSupportedInEra era)
deriving instance Show (ReferenceScriptsSupportedInEra era)
deriving instance Eq (ReferenceTxInsScriptsInlineDatumsSupportedInEra era)
deriving instance Show (ReferenceTxInsScriptsInlineDatumsSupportedInEra era)

referenceScriptsSupportedInEra :: CardanoEra era -> Maybe (ReferenceScriptsSupportedInEra era)
referenceScriptsSupportedInEra ByronEra = Nothing
referenceScriptsSupportedInEra ShelleyEra = Nothing
referenceScriptsSupportedInEra AllegraEra = Nothing
referenceScriptsSupportedInEra MaryEra = Nothing
referenceScriptsSupportedInEra AlonzoEra = Nothing
referenceScriptsSupportedInEra BabbageEra = Just ReferenceScriptsInBabbageEra
refInsScriptsAndInlineDatsSupportedInEra
:: CardanoEra era -> Maybe (ReferenceTxInsScriptsInlineDatumsSupportedInEra era)
refInsScriptsAndInlineDatsSupportedInEra ByronEra = Nothing
refInsScriptsAndInlineDatsSupportedInEra ShelleyEra = Nothing
refInsScriptsAndInlineDatsSupportedInEra AllegraEra = Nothing
refInsScriptsAndInlineDatsSupportedInEra MaryEra = Nothing
refInsScriptsAndInlineDatsSupportedInEra AlonzoEra = Nothing
refInsScriptsAndInlineDatsSupportedInEra BabbageEra = Just ReferenceTxInsScriptsInlineDatumsInBabbageEra

refScriptToShelleyScript
:: CardanoEra era
Expand Down
4 changes: 2 additions & 2 deletions cardano-api/src/Cardano/Api/Shelley.hs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ module Cardano.Api.Shelley

-- * Reference Scripts
ReferenceScript(..),
ReferenceScriptsSupportedInEra(..),
referenceScriptsSupportedInEra,
ReferenceTxInsScriptsInlineDatumsSupportedInEra(..),
refInsScriptsAndInlineDatsSupportedInEra,
refScriptToShelleyScript,

-- * Certificates
Expand Down
55 changes: 33 additions & 22 deletions cardano-api/src/Cardano/Api/TxBody.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ module Cardano.Api.TxBody (

-- * Other transaction body types
TxInsCollateral(..),
TxInsReference(..),
TxReturnCollateral(..),
TxTotalCollateral(..),
TxFee(..),
Expand Down Expand Up @@ -97,7 +98,6 @@ module Cardano.Api.TxBody (
WithdrawalsSupportedInEra(..),
CertificatesSupportedInEra(..),
UpdateProposalSupportedInEra(..),
InlineDatumSupportedInEra(..),
TxTotalAndReturnCollateralSupportedInEra(..),

-- ** Feature availability functions
Expand All @@ -116,7 +116,6 @@ module Cardano.Api.TxBody (
updateProposalSupportedInEra,
txScriptValiditySupportedInShelleyBasedEra,
txScriptValiditySupportedInCardanoEra,
inlineDatumSupportedInEra,
totalAndReturnCollateralSupportedInEra,

-- * Inspecting 'ScriptWitness'es
Expand Down Expand Up @@ -616,7 +615,7 @@ instance (IsShelleyBasedEra era, IsCardanoEra era)
Right sData ->
if hashScriptData sData /= h
then fail "Inline datum not equivalent to inline datum hash"
else return $ TxOutDatumInline InlineDatumSupportedInBabbageEra sData
else return $ TxOutDatumInline ReferenceTxInsScriptsInlineDatumsInBabbageEra sData
(Nothing, Nothing) -> return TxOutDatumNone
(_,_) -> fail "Should not be possible to create a tx output with either an inline datum hash or an inline datum"

Expand Down Expand Up @@ -706,7 +705,7 @@ instance (IsShelleyBasedEra era, IsCardanoEra era)
Right sData ->
if hashScriptData sData /= h
then fail "Inline datum not equivalent to inline datum hash"
else return $ TxOutDatumInline InlineDatumSupportedInBabbageEra sData
else return $ TxOutDatumInline ReferenceTxInsScriptsInlineDatumsInBabbageEra sData
(Nothing, Nothing) -> return TxOutDatumNone
(_,_) -> fail "Should not be possible to create a tx output with either an inline datum hash or an inline datum"

Expand Down Expand Up @@ -842,7 +841,7 @@ toAlonzoTxOutDataHash
toAlonzoTxOutDataHash TxOutDatumNone = SNothing
toAlonzoTxOutDataHash (TxOutDatumHash _ (ScriptDataHash dh)) = SJust dh
toAlonzoTxOutDataHash (TxOutDatumInline inlineDatumSupp _sd) =
case inlineDatumSupp :: InlineDatumSupportedInEra AlonzoEra of {}
case inlineDatumSupp :: ReferenceTxInsScriptsInlineDatumsSupportedInEra AlonzoEra of {}

fromAlonzoTxOutDataHash :: ScriptDataSupportedInEra era
-> StrictMaybe (Alonzo.DataHash StandardCrypto)
Expand Down Expand Up @@ -1275,6 +1274,16 @@ data TxInsCollateral era where
deriving instance Eq (TxInsCollateral era)
deriving instance Show (TxInsCollateral era)

data TxInsReference era where

TxInsReferenceNone :: TxInsReference era

TxInsReference :: ReferenceTxInsScriptsInlineDatumsSupportedInEra era
-> [TxIn]
-> TxInsReference era

deriving instance Eq (TxInsReference era)
deriving instance Show (TxInsReference era)

-- ----------------------------------------------------------------------------
-- Transaction output values (era-dependent)
Expand Down Expand Up @@ -1424,7 +1433,7 @@ data TxOutDatum ctx era where
-- datum hash. Note that the datum map will not be updated with this datum,
-- it only exists at the transaction output.
--
TxOutDatumInline :: InlineDatumSupportedInEra era
TxOutDatumInline :: ReferenceTxInsScriptsInlineDatumsSupportedInEra era
-> ScriptData
-> TxOutDatum ctx era

Expand All @@ -1443,21 +1452,6 @@ pattern TxOutDatumInTx s d <- TxOutDatumInTx' s _ d
{-# COMPLETE TxOutDatumNone, TxOutDatumHash, TxOutDatumInTx', TxOutDatumInline #-}
{-# COMPLETE TxOutDatumNone, TxOutDatumHash, TxOutDatumInTx , TxOutDatumInline #-}


data InlineDatumSupportedInEra era where
InlineDatumSupportedInBabbageEra :: InlineDatumSupportedInEra Babbage

deriving instance Eq (InlineDatumSupportedInEra era)
deriving instance Show (InlineDatumSupportedInEra era)

inlineDatumSupportedInEra :: CardanoEra era -> Maybe (InlineDatumSupportedInEra era)
inlineDatumSupportedInEra ByronEra = Nothing
inlineDatumSupportedInEra ShelleyEra = Nothing
inlineDatumSupportedInEra AllegraEra = Nothing
inlineDatumSupportedInEra MaryEra = Nothing
inlineDatumSupportedInEra AlonzoEra = Nothing
inlineDatumSupportedInEra BabbageEra = Just InlineDatumSupportedInBabbageEra

parseHash :: SerialiseAsRawBytes (Hash a) => AsType (Hash a) -> Parsec.Parser (Hash a)
parseHash asType = do
str <- Parsec.many1 Parsec.hexDigit Parsec.<?> "hash"
Expand Down Expand Up @@ -1633,6 +1627,7 @@ data TxBodyContent build era =
TxBodyContent {
txIns :: TxIns build era,
txInsCollateral :: TxInsCollateral era,
txInsReference :: TxInsReference era,
txOuts :: [TxOut CtxTx era],
txTotalCollateral :: TxTotalCollateral era,
txReturnCollateral :: TxReturnCollateral CtxTx era,
Expand Down Expand Up @@ -2118,6 +2113,7 @@ fromLedgerTxBody era scriptValidity body scriptdata mAux =
TxBodyContent
{ txIns = fromLedgerTxIns era body
, txInsCollateral = fromLedgerTxInsCollateral era body
, txInsReference = fromLedgerTxInsReference era body
, txOuts = fromLedgerTxOuts era body scriptdata
, txTotalCollateral = fromLedgerTxTotalCollateral era body
, txReturnCollateral = fromLedgerTxReturnCollateral era body
Expand Down Expand Up @@ -2175,6 +2171,20 @@ fromLedgerTxInsCollateral era body =
ShelleyBasedEraAlonzo -> toList $ Alonzo.collateral' body
ShelleyBasedEraBabbage -> toList $ Babbage.collateral body

fromLedgerTxInsReference
:: ShelleyBasedEra era -> Ledger.TxBody (ShelleyLedgerEra era) -> TxInsReference era
fromLedgerTxInsReference era txBody =
case refInsScriptsAndInlineDatsSupportedInEra $ shelleyBasedToCardanoEra era of
Nothing -> TxInsReferenceNone
Just suppInEra ->
let ledgerRefInputs = obtainReferenceInputsHasFieldConstraint suppInEra $ getField @"referenceInputs" txBody
in TxInsReference suppInEra $ map fromShelleyTxIn $ Set.toList ledgerRefInputs
where
obtainReferenceInputsHasFieldConstraint
:: ReferenceTxInsScriptsInlineDatumsSupportedInEra era
-> (HasField "referenceInputs" (Ledger.TxBody (ShelleyLedgerEra era)) (Set (Ledger.TxIn StandardCrypto)) => a)
-> a
obtainReferenceInputsHasFieldConstraint ReferenceTxInsScriptsInlineDatumsInBabbageEra f = f

fromLedgerTxOuts
:: forall era.
Expand Down Expand Up @@ -2670,6 +2680,7 @@ getByronTxBodyContent (Annotated Byron.UnsafeTx{txInputs, txOutputs} _) =
txIns = [ (fromByronTxIn input, ViewTx)
| input <- toList txInputs],
txInsCollateral = TxInsCollateralNone,
txInsReference = TxInsReferenceNone,
txOuts = fromByronTxOut <$> toList txOutputs,
txReturnCollateral = TxReturnCollateralNone,
txTotalCollateral = TxTotalCollateralNone,
Expand Down Expand Up @@ -3148,7 +3159,7 @@ toAlonzoTxOutDataHash' TxOutDatumNone = SNothing
toAlonzoTxOutDataHash' (TxOutDatumHash _ (ScriptDataHash dh)) = SJust dh
toAlonzoTxOutDataHash' (TxOutDatumInTx' _ (ScriptDataHash dh) _) = SJust dh
toAlonzoTxOutDataHash' (TxOutDatumInline inlineDatumSupp _sd) =
case inlineDatumSupp :: InlineDatumSupportedInEra AlonzoEra of {}
case inlineDatumSupp :: ReferenceTxInsScriptsInlineDatumsSupportedInEra AlonzoEra of {}

-- TODO: Consolidate with alonzo function and rename
toBabbageTxOutDatum'
Expand Down
2 changes: 2 additions & 0 deletions cardano-cli/src/Cardano/CLI/Byron/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ txSpendGenesisUTxOByronPBFT gc nId sk (ByronAddress bAddr) outs = do
, BuildTxWith (KeyWitness KeyWitnessForSpending))
]
TxInsCollateralNone
TxInsReferenceNone
outs
TxTotalCollateralNone
TxReturnCollateralNone
Expand Down Expand Up @@ -191,6 +192,7 @@ txSpendUTxOByronPBFT nId sk txIns outs = do
) | txIn <- txIns
]
TxInsCollateralNone
TxInsReferenceNone
outs
TxTotalCollateralNone
TxReturnCollateralNone
Expand Down
6 changes: 4 additions & 2 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,10 @@ runTxBuildRaw (AnyCardanoEra era)
<$> validateTxIns era inputsAndScripts
<*> validateTxInsCollateral
era inputsCollateral
<*> pure TxInsReferenceNone --TODO: Babbage era
<*> validateTxOuts era txouts
<*> pure TxTotalCollateralNone
<*> pure TxReturnCollateralNone
<*> pure TxTotalCollateralNone --TODO: Babbage era
<*> pure TxReturnCollateralNone --TODO: Babbage era
<*> validateTxFee era mFee
<*> ((,) <$> validateTxValidityLowerBound era mLowerBound
<*> validateTxValidityUpperBound era mUpperBound)
Expand Down Expand Up @@ -456,6 +457,7 @@ runTxBuild (AnyCardanoEra era) (AnyConsensusModeParams cModeParams) networkId mS
TxBodyContent
<$> validateTxIns era txins
<*> validateTxInsCollateral era txinsc
<*> pure TxInsReferenceNone -- TODO: Babbage era
<*> validateTxOuts era txouts
<*> pure TxTotalCollateralNone -- TODO: Babbage era
<*> pure TxReturnCollateralNone -- TODO: Babbage era
Expand Down