Skip to content

Commit 496fd98

Browse files
committed
Migrate to experimental TxOut in createCompatibleTx
Adapt to cardano-api PR #1209, which removes the legacy 'TxOut CtxTx era' from the Compatible and Experimental APIs: * 'createCompatibleTx' now takes '[Exp.TxOut (ShelleyLedgerEra era)]' plus a new 'Map L.DataHash (L.Data ...)' argument carrying any supplemental datum bodies. The legacy 'TxOut CtxTx era' bundled supplemental datums inside outputs; 'Exp.TxOut' only carries the datum hash, so callers thread the full datum bodies in explicitly. * The bridge helpers 'fromLegacyTxOut', 'legacyDatumToDatum', 'supplementalDatumFromLegacy', 'toLedgerDatum', and the 'DatumDecodingError' type are deleted from 'Cardano.Api.Experimental.Tx'. Migration: * 'mkTxOut' and 'toTxOutInAnyEra' now return '(Exp.TxOut (ShelleyLedgerEra era), Map DataHash (L.Data ...))' directly, building the legacy 'TxOut CtxTx era' internally only as a stepping stone for 'toShelleyTxOutAny'. * 'createCompatibleTx' call site in 'Compatible/Transaction/Run.hs' folds the per-output supplemental datums via 'Map.unions' and passes them as the new argument. * 'toTxOutInEra' and 'toTxOutInShelleyBasedEra' in 'EraBased/Transaction/Run.hs' delegate directly to 'mkTxOut' (the deleted 'fromLegacyTxOut' is gone). * 'TxCmdDatumDecodingError' is removed since the underlying 'Exp.DatumDecodingError' is gone and the new conversion is total. Temporary 'cabal.project' additions (to be removed once #1209 is merged and the next cardano-api is published to CHaP): * 'source-repository-package' pointing at the PR branch so CI can build against the unpublished API. * Per-package '-Wwarn=deprecations -Wwarn=unused-imports' for cardano-cli. The PR branch is several commits ahead of cardano-api-11.1.0.0 and surfaces unrelated TxBody/TxBodyContent deprecations (cardano-api PR #1200) plus a redundant 'Cardano.Ledger.Core' import in 'Cardano.CLI.Read' that became visible after upstream re-exports widened. Both are separate cleanups out of scope for this PR.
1 parent 9ca9c9b commit 496fd98

5 files changed

Lines changed: 72 additions & 16 deletions

File tree

cabal.project

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,29 @@ packages:
2626
program-options
2727
ghc-options: -Werror
2828

29+
-- TEMPORARY: build against cardano-api PR #1209
30+
-- (jordan/remove-legacy-txout-from-compat-and-experimental).
31+
-- The PR removes legacy TxOut from the Compatible and Experimental APIs,
32+
-- which this branch migrates cardano-cli to. Remove this stanza and the
33+
-- per-package warning downgrades below once #1209 is merged and the next
34+
-- cardano-api release is published to CHaP.
35+
source-repository-package
36+
type: git
37+
location: https://github.com/IntersectMBO/cardano-api
38+
tag: d848cec1ba4769b9daf4ac5ac9ed61a3f76c8042
39+
subdir:
40+
cardano-api
41+
cardano-api-gen
42+
43+
-- TEMPORARY: the PR branch is several commits ahead of cardano-api-11.1.0.0
44+
-- and surfaces unrelated TxBody/TxBodyContent deprecations (cardano-api PR
45+
-- #1200) plus a redundant Cardano.Ledger.Core import in Cardano.CLI.Read
46+
-- that became visible after upstream re-exports widened. Both are separate
47+
-- cardano-cli cleanups; downgrade to warnings here so the migration build
48+
-- is not blocked by them. Remove once the underlying cleanups land.
49+
package cardano-cli
50+
ghc-options: -Wwarn=deprecations -Wwarn=unused-imports
51+
2952
package crypton
3053
-- Using RDRAND instead of /dev/urandom as an entropy source for key
3154
-- generation is dubious. Set the flag so we use /dev/urandom by default.

cardano-cli/src/Cardano/CLI/Compatible/Transaction/Run.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import Cardano.CLI.Type.Common
3838

3939
import Control.Monad
4040
import Data.Map.Ordered.Strict qualified as OMap
41+
import Data.Map.Strict qualified as Map
4142
import Lens.Micro
4243

4344
runCompatibleTransactionCmd
@@ -60,7 +61,9 @@ runCompatibleTransactionCmd
6061
) = shelleyBasedEraConstraints sbe $ do
6162
sks <- mapM (fromEitherIOCli . readWitnessSigningData) witnesses
6263

63-
allOuts <- mapM (toTxOutInAnyEra sbe) outs
64+
outsAndDatums <- mapM (toTxOutInAnyEra sbe) outs
65+
let allOuts = map fst outsAndDatums
66+
extraDatums = Map.unions (map snd outsAndDatums)
6467

6568
certFilesAndMaybeScriptWits <-
6669
readCertificateScriptWitnesses' sbe certificates
@@ -107,7 +110,7 @@ runCompatibleTransactionCmd
107110

108111
transaction@(ShelleyTx _ ledgerTx) <-
109112
fromEitherCli $
110-
createCompatibleTx sbe ins allOuts fee protocolUpdates votes txCerts
113+
createCompatibleTx sbe ins allOuts extraDatums fee protocolUpdates votes txCerts
111114

112115
let txBody = ledgerTx ^. L.bodyTxL
113116

cardano-cli/src/Cardano/CLI/Compatible/Transaction/TxOut.hs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# LANGUAGE FlexibleContexts #-}
2+
{-# LANGUAGE GADTs #-}
13
{-# LANGUAGE LambdaCase #-}
24
{-# LANGUAGE RankNTypes #-}
35

@@ -8,28 +10,44 @@ module Cardano.CLI.Compatible.Transaction.TxOut
810
where
911

1012
import Cardano.Api
13+
import Cardano.Api.Experimental.Tx qualified as Exp
14+
import Cardano.Api.Ledger qualified as L
1115

1216
import Cardano.CLI.Compatible.Exception
1317
import Cardano.CLI.EraBased.Script.Read.Common
1418
import Cardano.CLI.Orphan ()
1519
import Cardano.CLI.Read
1620
import Cardano.CLI.Type.Common
21+
import Cardano.Ledger.Hashes (DataHash)
22+
import Cardano.Ledger.Plutus.Data qualified as L
23+
24+
import Data.Map.Strict (Map)
25+
import Data.Map.Strict qualified as Map
1726

1827
toTxOutInAnyEra
1928
:: ShelleyBasedEra era
2029
-> TxOutAnyEra
21-
-> CIO e (TxOut CtxTx era)
30+
-> CIO e (Exp.TxOut (ShelleyLedgerEra era), Map DataHash (L.Data (ShelleyLedgerEra era)))
2231
toTxOutInAnyEra era (TxOutAnyEra addr' val' mDatumHash refScriptFp) = do
2332
let addr = anyAddressInShelleyBasedEra era addr'
2433
mkTxOut era addr val' mDatumHash refScriptFp
2534

35+
-- | Build an output for a transaction body. Produces the experimental
36+
-- 'Exp.TxOut' plus any supplemental datum bodies that the caller-supplied
37+
-- datum carries. The legacy 'TxOut CtxTx era' bundled supplemental datums
38+
-- inside outputs; 'Exp.TxOut' only carries the datum hash, so callers thread
39+
-- the full datum bodies in separately (e.g. via 'createCompatibleTx').
40+
--
41+
-- The legacy 'TxOut CtxTx era' is used internally as a stepping stone to
42+
-- reuse the api's 'toShelleyTxOutAny' field-level conversion logic; it is
43+
-- not exposed.
2644
mkTxOut
2745
:: ShelleyBasedEra era
2846
-> AddressInEra era
2947
-> Value
3048
-> TxOutDatumAnyEra
3149
-> ReferenceScriptAnyEra
32-
-> CIO e (TxOut CtxTx era)
50+
-> CIO e (Exp.TxOut (ShelleyLedgerEra era), Map DataHash (L.Data (ShelleyLedgerEra era)))
3351
mkTxOut sbe addr val' mDatumHash refScriptFp = do
3452
let era = toCardanoEra sbe
3553
val <- toTxOutValueInShelleyBasedEra sbe val'
@@ -46,7 +64,19 @@ mkTxOut sbe addr val' mDatumHash refScriptFp = do
4664
(`getReferenceScript` refScriptFp)
4765
era
4866

49-
pure $ TxOut addr val datum refScript
67+
let legacyTxOut = TxOut addr val datum refScript
68+
pure $
69+
shelleyBasedEraConstraints sbe $
70+
(Exp.TxOut (toShelleyTxOutAny sbe legacyTxOut), supplementalsOf datum)
71+
where
72+
supplementalsOf
73+
:: L.Era (ShelleyLedgerEra era)
74+
=> TxOutDatum CtxTx era
75+
-> Map DataHash (L.Data (ShelleyLedgerEra era))
76+
supplementalsOf (TxOutSupplementalDatum _ h) =
77+
let ld = toAlonzoData h
78+
in Map.singleton (L.hashData ld) ld
79+
supplementalsOf _ = mempty
5080

5181
toTxOutValueInShelleyBasedEra
5282
:: ShelleyBasedEra era

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,15 @@ runTransactionBuildCmd
366366
else writeTxFileTextEnvelope eon fpath noWitTx
367367

368368
toTxOutInEra
369-
:: Exp.IsEra era
369+
:: forall era e
370+
. Exp.IsEra era
370371
=> TxOutAnyEra
371372
-> CIO e (Exp.TxOut (Exp.LedgerEra era), Map.Map DataHash (L.Data (Exp.LedgerEra era)))
372373
toTxOutInEra (TxOutAnyEra addr' val' mDatumHash refScriptFp) = do
373-
let addr = anyAddressInShelleyBasedEra (convert Exp.useEra) addr'
374-
o <- mkTxOut (convert Exp.useEra) addr val' mDatumHash refScriptFp
375-
fromEitherCli $ Exp.fromLegacyTxOut o
374+
let sbe = convert (Exp.useEra @era)
375+
addr = anyAddressInShelleyBasedEra sbe addr'
376+
obtainCommonConstraints (Exp.useEra @era) $
377+
mkTxOut sbe addr val' mDatumHash refScriptFp
376378

377379
runTransactionBuildEstimateCmd
378380
:: forall era e
@@ -1176,14 +1178,15 @@ getAllReferenceInputs
11761178
]
11771179

11781180
toTxOutInShelleyBasedEra
1179-
:: Exp.IsEra era
1181+
:: forall era e
1182+
. Exp.IsEra era
11801183
=> TxOutShelleyBasedEra
11811184
-> CIO e (Exp.TxOut (Exp.LedgerEra era), Map.Map DataHash (L.Data (Exp.LedgerEra era)))
11821185
toTxOutInShelleyBasedEra (TxOutShelleyBasedEra addr' val' mDatumHash refScriptFp) = do
1183-
let sbe = convert Exp.useEra
1186+
let sbe = convert (Exp.useEra @era)
11841187
addr = shelleyAddressInEra sbe addr'
1185-
o <- mkTxOut sbe addr val' mDatumHash refScriptFp
1186-
fromEitherCli $ Exp.fromLegacyTxOut o
1188+
obtainCommonConstraints (Exp.useEra @era) $
1189+
mkTxOut sbe addr val' mDatumHash refScriptFp
11871190

11881191
-- TODO: Currently we specify the policyId with the '--mint' option on the cli
11891192
-- and we added a separate '--policy-id' parser that parses the policy id for the

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ data AnyTxBodyErrorAutoBalance where
4242

4343
data TxCmdError
4444
= TxCmdCBORDecodeError !CBOR.DecoderError
45-
| TxCmdDatumDecodingError Exp.DatumDecodingError
4645
| TxCmdProtocolParamsError ProtocolParamsError
4746
| forall era. LostScriptWitnesses
4847
[Exp.AnyIndexedPlutusScriptWitness (Exp.LedgerEra era)]
@@ -199,8 +198,6 @@ renderTxCmdError = \case
199198
, pretty (length after)
200199
, "."
201200
]
202-
TxCmdDatumDecodingError err ->
203-
"Error decoding datum: " <> pshow err
204201

205202
prettyPolicyIdList :: [PolicyId] -> Doc ann
206203
prettyPolicyIdList =

0 commit comments

Comments
 (0)