Skip to content

Commit 4d4f884

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 ...)' of 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 in 'Cardano.Api.Experimental.Tx' (fromLegacyTxOut, legacyDatumToDatum, supplementalDatumFromLegacy, toLedgerDatum, DatumDecodingError) are deleted. Changes here: * Add a 'convertLegacyTxOut' helper in 'Cardano.CLI.Compatible.Transaction.TxOut' that maps each legacy 'TxOut CtxTx era' to '(Exp.TxOut, Map DataHash (L.Data ...))' using 'toShelleyTxOutAny' (which already strips supplemental datums to their hash in the ledger TxOut) and pulls the supplemental datum bodies into the map. * Update the 'createCompatibleTx' call in 'Cardano.CLI.Compatible.Transaction.Run' to convert outs at the boundary and pass the folded 'extraDatums' map. * Replace 'Exp.fromLegacyTxOut' in 'toTxOutInEra' and 'toTxOutInShelleyBasedEra' with 'convertLegacyTxOut', using 'obtainCommonConstraints' to bridge 'ShelleyLedgerEra ~ LedgerEra'. * Drop 'TxCmdDatumDecodingError' from 'TxCmdError' 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 cardano-cli cleanups out of scope for this PR.
1 parent 9ca9c9b commit 4d4f884

5 files changed

Lines changed: 71 additions & 12 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: 6 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,10 @@ runCompatibleTransactionCmd
6061
) = shelleyBasedEraConstraints sbe $ do
6162
sks <- mapM (fromEitherIOCli . readWitnessSigningData) witnesses
6263

63-
allOuts <- mapM (toTxOutInAnyEra sbe) outs
64+
legacyOuts <- mapM (toTxOutInAnyEra sbe) outs
65+
let convertedOuts = map (convertLegacyTxOut sbe) legacyOuts
66+
allOuts = map fst convertedOuts
67+
extraDatums = Map.unions (map snd convertedOuts)
6468

6569
certFilesAndMaybeScriptWits <-
6670
readCertificateScriptWitnesses' sbe certificates
@@ -107,7 +111,7 @@ runCompatibleTransactionCmd
107111

108112
transaction@(ShelleyTx _ ledgerTx) <-
109113
fromEitherCli $
110-
createCompatibleTx sbe ins allOuts fee protocolUpdates votes txCerts
114+
createCompatibleTx sbe ins allOuts extraDatums fee protocolUpdates votes txCerts
111115

112116
let txBody = ledgerTx ^. L.bodyTxL
113117

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

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

46
module Cardano.CLI.Compatible.Transaction.TxOut
57
( mkTxOut
68
, toTxOutInAnyEra
9+
, convertLegacyTxOut
710
)
811
where
912

1013
import Cardano.Api
14+
import Cardano.Api.Experimental.Tx qualified as Exp
15+
import Cardano.Api.Ledger qualified as L
1116

1217
import Cardano.CLI.Compatible.Exception
1318
import Cardano.CLI.EraBased.Script.Read.Common
1419
import Cardano.CLI.Orphan ()
1520
import Cardano.CLI.Read
1621
import Cardano.CLI.Type.Common
22+
import Cardano.Ledger.Hashes (DataHash)
23+
import Cardano.Ledger.Plutus.Data qualified as L
24+
25+
import Data.Map.Strict (Map)
26+
import Data.Map.Strict qualified as Map
1727

1828
toTxOutInAnyEra
1929
:: ShelleyBasedEra era
@@ -23,6 +33,28 @@ toTxOutInAnyEra era (TxOutAnyEra addr' val' mDatumHash refScriptFp) = do
2333
let addr = anyAddressInShelleyBasedEra era addr'
2434
mkTxOut era addr val' mDatumHash refScriptFp
2535

36+
-- | Convert a legacy 'TxOut CtxTx era' into the experimental 'Exp.TxOut' plus
37+
-- the supplemental datums it carried. The legacy 'TxOut CtxTx era' bundled
38+
-- supplemental datum bodies inside 'TxOutSupplementalDatum'; 'Exp.TxOut' only
39+
-- carries the datum hash, so the body is returned separately in the map for
40+
-- callers to thread into the witness set (e.g. via 'createCompatibleTx').
41+
convertLegacyTxOut
42+
:: ShelleyBasedEra era
43+
-> TxOut CtxTx era
44+
-> (Exp.TxOut (ShelleyLedgerEra era), Map DataHash (L.Data (ShelleyLedgerEra era)))
45+
convertLegacyTxOut sbe tOut@(TxOut _ _ d _) =
46+
shelleyBasedEraConstraints sbe $
47+
(Exp.TxOut (toShelleyTxOutAny sbe tOut), supplementalsOf d)
48+
where
49+
supplementalsOf
50+
:: L.Era (ShelleyLedgerEra era)
51+
=> TxOutDatum CtxTx era
52+
-> Map DataHash (L.Data (ShelleyLedgerEra era))
53+
supplementalsOf (TxOutSupplementalDatum _ h) =
54+
let ld = toAlonzoData h
55+
in Map.singleton (L.hashData ld) ld
56+
supplementalsOf _ = mempty
57+
2658
mkTxOut
2759
:: ShelleyBasedEra era
2860
-> AddressInEra era

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

Lines changed: 10 additions & 7 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+
o <- mkTxOut sbe addr val' mDatumHash refScriptFp
377+
pure $ obtainCommonConstraints (Exp.useEra @era) $ convertLegacyTxOut sbe o
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'
11851188
o <- mkTxOut sbe addr val' mDatumHash refScriptFp
1186-
fromEitherCli $ Exp.fromLegacyTxOut o
1189+
pure $ obtainCommonConstraints (Exp.useEra @era) $ convertLegacyTxOut sbe o
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)