Skip to content

Commit 3d32b5a

Browse files
committed
Remove non-Byron TxOut CtxTx/CtxUTxO usage from cardano-cli
Eliminate the legacy 'TxOut CtxTx era' and 'TxOut CtxUTxO era' type signatures and pattern matches from cardano-cli's non-Byron code paths. Byron-era code uses a separate pre-Shelley tx output model and is left alone — 'Exp.TxOut' is not applicable there. * 'friendlyTxOut' in 'Compatible/Json/Friendly.hs' now takes 'Exp.TxOut (LedgerEra era)' and reads address, value, datum, and reference script directly via the ledger lenses ('addrTxOutL', 'valueTxOutL', 'datumTxOutL', 'referenceScriptTxOutL'). The two call sites ('basePairs', 'friendlyReturnCollateral') wrap the body's ledger outputs with 'Exp.TxOut' instead of going through 'fromShelleyTxOut → fromCtxUTxOTxOut'. The dead 'friendlyTxOutValue' helper is dropped. * 'filteredUTxOsToText' in 'EraBased/Query/Run.hs' now takes a 'ShelleyBasedEra era' witness, converts the api 'UTxO era' to the ledger UTxO once via 'toLedgerUTxO', then renders each entry from '(TxIn, Exp.TxOut (ShelleyLedgerEra era))' using ledger lenses. The pre-Babbage datum slot has no uniform ledger representation, so the renderer emits an empty placeholder there and shows the babbage+ ledger datum elsewhere — debug-style output, no golden tests touched. * Removed 'validateTxReturnCollateral' from 'Type/Error/TxValidationError.hs'. It was exported but never called outside its own module; the actual return-collateral construction in 'EraBased/Transaction/Run' builds 'Exp.TxReturnCollateral' directly. Remaining uses of legacy ctx-typed tx outputs in cardano-cli are confined to Byron-only modules.
1 parent 496fd98 commit 3d32b5a

3 files changed

Lines changed: 91 additions & 68 deletions

File tree

cardano-cli/src/Cardano/CLI/Compatible/Json/Friendly.hs

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ where
3333
import Cardano.Api as Api
3434
import Cardano.Api.Experimental (obtainCommonConstraints)
3535
import Cardano.Api.Experimental qualified as Exp
36+
import Cardano.Api.Experimental.Tx qualified as Exp
3637
import Cardano.Api.Ledger (ExUnits (..), extractHash, strictMaybeToMaybe)
3738
import Cardano.Api.Ledger qualified as Alonzo
3839
import Cardano.Api.Ledger qualified as L
@@ -219,8 +220,7 @@ basePairs
219220
-> [Aeson.Pair]
220221
basePairs era body mAuxData =
221222
Exp.obtainCommonConstraints era $
222-
let sbe = convert era :: ShelleyBasedEra era
223-
certs = toList (body ^. L.certsTxBodyL)
223+
let certs = toList (body ^. L.certsTxBodyL)
224224
in [ "auxiliary scripts" .= friendlyAuxScripts era mAuxData
225225
, "certificates"
226226
.= if null certs
@@ -232,7 +232,7 @@ basePairs era body mAuxData =
232232
, "metadata" .= friendlyMetadata mAuxData
233233
, "outputs"
234234
.= map
235-
(friendlyTxOut era . fromCtxUTxOTxOut . fromShelleyTxOut sbe)
235+
(friendlyTxOut era . Exp.TxOut)
236236
(toList (body ^. L.outputsTxBodyL))
237237
, "withdrawals" .= friendlyWithdrawals (body ^. L.withdrawalsTxBodyL)
238238
]
@@ -472,8 +472,7 @@ friendlyReturnCollateral era = \case
472472
L.SNothing -> Aeson.Null
473473
L.SJust collOut ->
474474
Exp.obtainCommonConstraints era $
475-
let sbe = convert era :: ShelleyBasedEra era
476-
in friendlyTxOut era (fromCtxUTxOTxOut (fromShelleyTxOut sbe collOut))
475+
friendlyTxOut era (Exp.TxOut collOut)
477476

478477
friendlyExtraKeyWits :: Set.Set (L.KeyHash L.Guard) -> Aeson.Value
479478
friendlyExtraKeyWits keyhashes
@@ -499,35 +498,59 @@ friendlyStakeAddress (StakeAddress net cred) =
499498
, friendlyStakeCredential cred
500499
]
501500

502-
friendlyTxOut :: Exp.Era era -> TxOut CtxTx era -> Aeson.Value
503-
friendlyTxOut era (TxOut addr amount mdatum script) =
501+
friendlyTxOut
502+
:: forall era
503+
. Exp.Era era
504+
-> Exp.TxOut (Exp.LedgerEra era)
505+
-> Aeson.Value
506+
friendlyTxOut era (Exp.TxOut ledgerTxOut) =
504507
Exp.obtainCommonConstraints era $
505-
object $
506-
case addr of
507-
AddressInEra ByronAddressInAnyEra byronAdr ->
508-
[ "address era" .= String "Byron"
509-
, "address" .= serialiseAddress byronAdr
510-
, "amount" .= friendlyTxOutValue era amount
511-
]
512-
AddressInEra (ShelleyAddressInEra _) saddr@(ShelleyAddress net cred stake) ->
513-
let preAlonzo =
514-
friendlyPaymentCredential (fromShelleyPaymentCredential cred)
515-
: [ "address era" .= Aeson.String "Shelley"
516-
, "network" .= net
517-
, "address" .= serialiseAddress saddr
518-
, "amount" .= friendlyTxOutValue era amount
519-
, "stake reference" .= friendlyStakeReference (fromShelleyStakeReference stake)
520-
]
521-
datum = ["datum" .= d | d <- maybeToList $ renderDatum mdatum]
522-
sinceAlonzo = ["reference script" .= script]
523-
in preAlonzo ++ datum ++ sinceAlonzo
508+
babbageEraOnwardsConstraints beo $
509+
let addr = fromShelleyAddr sbe (ledgerTxOut ^. L.addrTxOutL)
510+
ledgerValue = ledgerTxOut ^. L.valueTxOutL
511+
refScript = case ledgerTxOut ^. L.referenceScriptTxOutL of
512+
L.SNothing -> ReferenceScriptNone
513+
L.SJust s -> fromShelleyScriptToReferenceScript sbe s
514+
in object $
515+
case addr of
516+
AddressInEra ByronAddressInAnyEra byronAdr ->
517+
[ "address era" .= String "Byron"
518+
, "address" .= serialiseAddress byronAdr
519+
, "amount" .= friendlyLedgerValue era ledgerValue
520+
]
521+
AddressInEra (ShelleyAddressInEra _) saddr@(ShelleyAddress net cred stake) ->
522+
let preAlonzo =
523+
friendlyPaymentCredential (fromShelleyPaymentCredential cred)
524+
: [ "address era" .= Aeson.String "Shelley"
525+
, "network" .= net
526+
, "address" .= serialiseAddress saddr
527+
, "amount" .= friendlyLedgerValue era ledgerValue
528+
, "stake reference"
529+
.= friendlyStakeReference (fromShelleyStakeReference stake)
530+
]
531+
datumField =
532+
[ "datum" .= d
533+
| d <- maybeToList $ renderLedgerDatum beo (ledgerTxOut ^. L.datumTxOutL)
534+
]
535+
sinceAlonzo = ["reference script" .= refScript]
536+
in preAlonzo ++ datumField ++ sinceAlonzo
524537
where
525-
renderDatum :: TxOutDatum CtxTx era -> Maybe Aeson.Value
526-
renderDatum = \case
527-
TxOutDatumNone -> Nothing
528-
TxOutDatumHash _ h -> Just $ toJSON h
529-
TxOutSupplementalDatum _ sData -> Just $ scriptDataToJson ScriptDataJsonDetailedSchema sData
530-
TxOutDatumInline _ sData -> Just $ scriptDataToJson ScriptDataJsonDetailedSchema sData
538+
beo = convert era :: BabbageEraOnwards era
539+
sbe = convert era :: ShelleyBasedEra era
540+
541+
renderLedgerDatum
542+
:: BabbageEraOnwards era
543+
-> L.Datum (Exp.LedgerEra era)
544+
-> Maybe Aeson.Value
545+
renderLedgerDatum w = \case
546+
L.NoDatum -> Nothing
547+
L.DatumHash dh -> Just $ toJSON (ScriptDataHash dh)
548+
L.Datum binData ->
549+
babbageEraOnwardsConstraints w $
550+
Just $
551+
scriptDataToJson
552+
ScriptDataJsonDetailedSchema
553+
(fromAlonzoData (L.binaryDataToData binData))
531554

532555
friendlyStakeReference :: StakeAddressReference -> Aeson.Value
533556
friendlyStakeReference = \case
@@ -690,12 +713,6 @@ friendlyPaymentCredential = \case
690713
friendlyLovelace :: Lovelace -> Aeson.Value
691714
friendlyLovelace value = String $ docToText (pretty value)
692715

693-
friendlyTxOutValue :: Exp.Era era -> TxOutValue era -> Aeson.Value
694-
friendlyTxOutValue era = \case
695-
TxOutValueByron lovelace -> friendlyLovelace lovelace
696-
TxOutValueShelleyBased _ v ->
697-
Exp.obtainCommonConstraints era $ friendlyLedgerValue era v
698-
699716
friendlyLedgerValue
700717
:: ()
701718
=> Exp.Era era

cardano-cli/src/Cardano/CLI/EraBased/Query/Run.hs

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import Cardano.Api qualified as Api
4141
import Cardano.Api.Consensus qualified as Consensus
4242
import Cardano.Api.Experimental (obtainCommonConstraints)
4343
import Cardano.Api.Experimental qualified as Exp
44+
import Cardano.Api.Experimental.Tx qualified as Exp
4445
import Cardano.Api.Ledger (strictMaybeToMaybe)
4546
import Cardano.Api.Ledger qualified as L
4647
import Cardano.Api.Network qualified as Consensus
@@ -67,6 +68,7 @@ import Cardano.CLI.Type.Output qualified as O
6768
import Cardano.Crypto.Hash (hashToBytesAsHex)
6869
import Cardano.Ledger.Address qualified as L
6970
import Cardano.Ledger.Api.State.Query qualified as L
71+
import Cardano.Ledger.Api.Tx qualified as L
7072
import Cardano.Ledger.Conway.State (ChainAccountState (..))
7173
import Cardano.Slotting.EpochInfo (EpochInfo (..), epochInfoSlotToUTCTime, hoistEpochInfo)
7274
import Cardano.Slotting.Time (RelativeTime (..), toRelativeTime)
@@ -1191,7 +1193,7 @@ writeFilteredUTxOs era format mOutFile utxo = do
11911193
. Vary.on (\FormatCborBin -> CBOR.serialize $ toLedgerUTxO era utxo)
11921194
. Vary.on (\FormatCborHex -> Base16.encode . CBOR.serialize $ toLedgerUTxO era utxo)
11931195
. Vary.on (\FormatJson -> Json.encodeJson utxo)
1194-
. Vary.on (\FormatText -> strictTextToLazyBytestring $ filteredUTxOsToText utxo)
1196+
. Vary.on (\FormatText -> strictTextToLazyBytestring $ filteredUTxOsToText era utxo)
11951197
. Vary.on (\FormatYaml -> Json.encodeYaml utxo)
11961198
$ Vary.exhaustiveCase
11971199
)
@@ -1200,40 +1202,53 @@ writeFilteredUTxOs era format mOutFile utxo = do
12001202
. newExceptT
12011203
$ writeLazyByteStringOutput mOutFile output
12021204

1203-
filteredUTxOsToText :: UTxO era -> Text
1204-
filteredUTxOsToText (UTxO utxo) = do
1205-
mconcat
1206-
[ Text.unlines [title, Text.replicate (Text.length title + 2) "-"]
1207-
, Text.unlines $
1208-
map utxoToText $
1209-
toList utxo
1210-
]
1205+
filteredUTxOsToText :: ShelleyBasedEra era -> UTxO era -> Text
1206+
filteredUTxOsToText sbe utxo =
1207+
shelleyBasedEraConstraints sbe $
1208+
let entries =
1209+
[ (fromShelleyTxIn ledgerTxIn, Exp.TxOut ledgerTxOut)
1210+
| (ledgerTxIn, ledgerTxOut) <- Map.toList . L.unUTxO $ toLedgerUTxO sbe utxo
1211+
]
1212+
in mconcat
1213+
[ Text.unlines [title, Text.replicate (Text.length title + 2) "-"]
1214+
, Text.unlines $ map (utxoToText sbe) entries
1215+
]
12111216
where
12121217
title :: Text
12131218
title =
12141219
" TxHash TxIx Amount"
12151220

12161221
utxoToText
1217-
:: (TxIn, TxOut CtxUTxO era)
1222+
:: ShelleyBasedEra era
1223+
-> (TxIn, Exp.TxOut (ShelleyLedgerEra era))
12181224
-> Text
1219-
utxoToText txInOutTuple =
1220-
let (TxIn (TxId txhash) (TxIx index), TxOut _ value mDatum _) = txInOutTuple
1221-
in mconcat
1222-
[ Text.decodeLatin1 (hashToBytesAsHex txhash)
1223-
, textShowN 6 index
1224-
, " " <> printableValue value <> " + " <> Text.pack (show mDatum)
1225-
]
1225+
utxoToText sbe (TxIn (TxId txhash) (TxIx index), Exp.TxOut ledgerTxOut) =
1226+
shelleyBasedEraConstraints sbe $
1227+
mconcat
1228+
[ Text.decodeLatin1 (hashToBytesAsHex txhash)
1229+
, textShowN 6 index
1230+
, " " <> printableValue <> " + " <> printableDatum
1231+
]
12261232
where
12271233
textShowN :: Show a => Int -> a -> Text
12281234
textShowN len x =
12291235
let str = show x
12301236
slen = length str
12311237
in Text.pack $ replicate (max 1 (len - slen)) ' ' ++ str
12321238

1233-
printableValue :: TxOutValue era -> Text
1234-
printableValue = \case
1235-
TxOutValueByron (L.Coin i) -> Text.pack $ show i
1236-
TxOutValueShelleyBased sbe2 val -> renderValue $ Api.fromLedgerValue sbe2 val
1239+
printableValue :: Text
1240+
printableValue =
1241+
renderValue $ Api.fromLedgerValue sbe (ledgerTxOut ^. L.valueTxOutL)
1242+
1243+
-- Debug-style datum rendering — pre-Babbage outputs have no datum
1244+
-- representation we can read uniformly, so show the babbage+ ledger
1245+
-- datum where it exists and an empty placeholder otherwise.
1246+
printableDatum :: Text
1247+
printableDatum =
1248+
caseShelleyToAlonzoOrBabbageEraOnwards
1249+
(const "")
1250+
(\beo -> babbageEraOnwardsConstraints beo $ Text.pack $ show (ledgerTxOut ^. L.datumTxOutL))
1251+
sbe
12371252

12381253
runQueryStakePoolsCmd
12391254
:: ()

cardano-cli/src/Cardano/CLI/Type/Error/TxValidationError.hs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ module Cardano.CLI.Type.Error.TxValidationError
1313
, validateScriptSupportedInEra
1414
, validateTxAuxScripts
1515
, validateRequiredSigners
16-
, validateTxReturnCollateral
1716
, validateTxScriptValidity
1817
, validateTxTotalCollateral
1918
, validateTxValidityLowerBound
@@ -88,14 +87,6 @@ validateTxTreasuryDonation mTreasuryDonation = do
8887

8988
Exp.obtainCommonConstraints (Exp.useEra @era) $ mkFeatured unTxTreasuryDonation
9089

91-
validateTxReturnCollateral
92-
:: IsEra era
93-
=> Maybe (TxOut CtxTx era)
94-
-> TxReturnCollateral CtxTx era
95-
validateTxReturnCollateral Nothing = TxReturnCollateralNone
96-
validateTxReturnCollateral (Just retColTxOut) = do
97-
TxReturnCollateral (convert useEra) retColTxOut
98-
9990
validateTxValidityLowerBound
10091
:: IsEra era
10192
=> Maybe SlotNo

0 commit comments

Comments
 (0)