Skip to content
This repository was archived by the owner on Dec 2, 2024. It is now read-only.

Commit b80116b

Browse files
author
Evgenii Akentev
authored
Drop Tx and EmulatorTx types (#973)
* Drop EmulatorTx * Make CardanoTx newtype * Remove Tx type * Drop unnecessary CardanoTx newtype layer
1 parent f54eca8 commit b80116b

File tree

52 files changed

+591
-1324
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+591
-1324
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Removed
2+
3+
- Remove `estimateTransactionFee`, `signTx`, `fromPlutusTx`, `fromPlutusTxSigned`, `fromPlutusTxSigned'` as the `Tx` was removed from `plutus-ledger`.

cardano-node-emulator/src/Cardano/Node/Emulator/Fee.hs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
{-# LANGUAGE TypeFamilies #-}
88
-- | Calculating transaction fees in the emulator.
99
module Cardano.Node.Emulator.Fee(
10-
estimateTransactionFee,
1110
estimateCardanoBuildTxFee,
1211
makeAutoBalancedTransaction,
1312
makeAutoBalancedTransactionWithUtxoProvider,
@@ -23,7 +22,7 @@ import Cardano.Api.Shelley qualified as C.Api
2322
import Cardano.Ledger.BaseTypes (Globals (systemStart))
2423
import Cardano.Ledger.Core qualified as C.Ledger (Tx)
2524
import Cardano.Ledger.Shelley.API qualified as C.Ledger hiding (Tx)
26-
import Cardano.Node.Emulator.Params (EmulatorEra, PParams, Params (emulatorPParams, pNetworkId), emulatorEraHistory,
25+
import Cardano.Node.Emulator.Params (EmulatorEra, PParams, Params (emulatorPParams), emulatorEraHistory,
2726
emulatorGlobals, pProtocolParams)
2827
import Cardano.Node.Emulator.Validation (CardanoLedgerError, UTxO (..), makeTransactionBody)
2928
import Control.Arrow ((&&&))
@@ -36,26 +35,16 @@ import Data.Map qualified as Map
3635
import Data.Maybe (isNothing, listToMaybe)
3736
import Data.Ord (Down (Down))
3837
import GHC.Generics (Generic)
39-
import Ledger.Address (CardanoAddress, PaymentPubKeyHash)
38+
import Ledger.Address (CardanoAddress)
4039
import Ledger.Index (UtxoIndex (UtxoIndex), ValidationError (..), ValidationPhase (Phase1), adjustCardanoTxOut,
4140
minAdaTxOutEstimated)
42-
import Ledger.Tx (ToCardanoError (TxBodyError), Tx, TxOut, TxOutRef)
41+
import Ledger.Tx (ToCardanoError (TxBodyError), TxOut, TxOutRef)
4342
import Ledger.Tx qualified as Tx
4443
import Ledger.Tx.CardanoAPI (CardanoBuildTx (..), fromPlutusIndex, getCardanoBuildTx, toCardanoFee,
45-
toCardanoReturnCollateral, toCardanoTotalCollateral, toCardanoTxBodyContent)
44+
toCardanoReturnCollateral, toCardanoTotalCollateral)
4645
import Ledger.Tx.CardanoAPI qualified as CardanoAPI
4746
import Ledger.Value.CardanoAPI (isZero, lovelaceToValue, split, valueGeq)
4847

49-
estimateTransactionFee
50-
:: Params
51-
-> UTxO EmulatorEra
52-
-> [PaymentPubKeyHash]
53-
-> Tx
54-
-> Either CardanoLedgerError C.Lovelace
55-
estimateTransactionFee params utxo requiredSigners tx = do
56-
txBodyContent <- first Right $ toCardanoTxBodyContent (pNetworkId params) (emulatorPParams params) requiredSigners tx
57-
estimateCardanoBuildTxFee params utxo txBodyContent
58-
5948
estimateCardanoBuildTxFee
6049
:: Params
6150
-> UTxO EmulatorEra

cardano-node-emulator/src/Cardano/Node/Emulator/Generators.hs

Lines changed: 37 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ module Cardano.Node.Emulator.Generators(
4949
splitVal,
5050
validateMockchain,
5151
signAll,
52-
signTx,
5352
CW.knownAddresses,
5453
CW.knownPaymentPublicKeys,
5554
CW.knownPaymentPrivateKeys,
5655
CW.knownPaymentKeys,
5756
knownXPrvs,
5857
alwaysSucceedPolicy,
5958
alwaysSucceedPolicyId,
60-
someTokenValue
59+
someTokenValue,
60+
emptyTxBodyContent
6161
) where
6262

6363
import Control.Monad (guard, replicateM)
@@ -86,15 +86,14 @@ import Cardano.Crypto.Wallet qualified as Crypto
8686
import Cardano.Node.Emulator.Params (Params (pSlotConfig))
8787
import Cardano.Node.Emulator.TimeSlot (SlotConfig)
8888
import Cardano.Node.Emulator.TimeSlot qualified as TimeSlot
89-
import Cardano.Node.Emulator.Validation (fromPlutusTxSigned, validateCardanoTx)
89+
import Cardano.Node.Emulator.Validation (validateCardanoTx)
9090
import Control.Lens.Lens ((<&>))
9191
import Data.Functor (($>))
9292
import Data.String (fromString)
9393
import Gen.Cardano.Api.Typed (genTxIn)
94-
import Ledger (CardanoTx (CardanoApiTx), Interval, MintingPolicy (getMintingPolicy),
94+
import Ledger (CardanoTx (CardanoEmulatorEraTx), Interval, MintingPolicy (getMintingPolicy),
9595
POSIXTime (POSIXTime, getPOSIXTime), POSIXTimeRange, Passphrase (Passphrase),
9696
PaymentPrivateKey (unPaymentPrivateKey), PaymentPubKey, Slot (Slot), SlotRange,
97-
SomeCardanoApiTx (CardanoApiEmulatorEraTx),
9897
TxInType (ConsumePublicKeyAddress, ConsumeSimpleScriptAddress, ScriptAddress), TxInput, TxInputType,
9998
TxOut, TxOutRef (TxOutRef), ValidationErrorInPhase, addCardanoTxSignature, maxFee, minAdaTxOutEstimated,
10099
minLovelaceTxOutEstimated, pubKeyTxOut, txOutValue, validatorHash)
@@ -163,11 +162,10 @@ genMockchain' gm = do
163162
slotCfg <- genSlotConfig
164163
(txn, ot) <- genInitialTransaction gm
165164
let params = def { pSlotConfig = slotCfg }
166-
signedTx = signTx params mempty txn
167165
-- There is a problem that txId of emulator tx and tx of cardano tx are different.
168166
-- We convert the emulator tx to cardano tx here to get the correct transaction id
169167
-- because later we anyway will use the converted cardano tx so the utxo should match it.
170-
tid = Tx.getCardanoTxId signedTx
168+
tid = Tx.getCardanoTxId txn
171169
pure Mockchain {
172170
mockchainInitialTxPool = [txn],
173171
mockchainUtxo = Map.fromList $ first (TxOutRef tid) <$> zip [0..] ot,
@@ -188,6 +186,27 @@ genInitialTransaction g = do
188186
(body, o) <- initialTxBody g
189187
(,o) <$> makeTx body
190188

189+
emptyTxBodyContent :: C.TxBodyContent C.BuildTx C.BabbageEra
190+
emptyTxBodyContent = C.TxBodyContent
191+
{ txIns = []
192+
, txInsCollateral = C.TxInsCollateralNone
193+
, txMintValue = C.TxMintNone
194+
, txFee = C.toCardanoFee 0
195+
, txOuts = []
196+
, txProtocolParams = C.BuildTxWith $ Just $ C.fromLedgerPParams C.ShelleyBasedEraBabbage def
197+
, txInsReference = C.TxInsReferenceNone
198+
, txTotalCollateral = C.TxTotalCollateralNone
199+
, txReturnCollateral = C.TxReturnCollateralNone
200+
, txValidityRange = ( C.TxValidityNoLowerBound
201+
, C.TxValidityNoUpperBound C.ValidityNoUpperBoundInBabbageEra)
202+
, txScriptValidity = C.TxScriptValidityNone
203+
, txExtraKeyWits = C.TxExtraKeyWitnessesNone
204+
, txMetadata = C.TxMetadataNone
205+
, txAuxScripts = C.TxAuxScriptsNone
206+
, txWithdrawals = C.TxWithdrawalsNone
207+
, txCertificates = C.TxCertificatesNone
208+
, txUpdateProposal = C.TxUpdateProposalNone
209+
}
191210

192211
initialTxBody ::
193212
GeneratorModel
@@ -198,27 +217,9 @@ initialTxBody GeneratorModel{..} = do
198217
-- we use a generated tx in input it's unbalanced but it's "fine" as we don't validate this tx
199218
txIns <- map (, C.BuildTxWith (C.KeyWitness C.KeyWitnessForSpending))
200219
<$> Gen.list (Range.constant 1 10) genTxIn
201-
pure (C.TxBodyContent
202-
{ txIns
203-
, txInsCollateral = C.TxInsCollateralNone
204-
, txMintValue = C.TxMintNone
205-
, txFee = C.toCardanoFee 0
206-
, txOuts = Tx.getTxOut <$> o
207-
-- unused:
208-
, txProtocolParams = C.BuildTxWith $ Just $ C.fromLedgerPParams C.ShelleyBasedEraBabbage def
209-
, txInsReference = C.TxInsReferenceNone
210-
, txTotalCollateral = C.TxTotalCollateralNone
211-
, txReturnCollateral = C.TxReturnCollateralNone
212-
, txValidityRange = ( C.TxValidityNoLowerBound
213-
, C.TxValidityNoUpperBound C.ValidityNoUpperBoundInBabbageEra)
214-
, txScriptValidity = C.TxScriptValidityNone
215-
, txExtraKeyWits = C.TxExtraKeyWitnessesNone
216-
, txMetadata = C.TxMetadataNone
217-
, txAuxScripts = C.TxAuxScriptsNone
218-
, txWithdrawals = C.TxWithdrawalsNone
219-
220-
, txCertificates = C.TxCertificatesNone
221-
, txUpdateProposal = C.TxUpdateProposalNone
220+
pure (emptyTxBodyContent
221+
{ C.txIns
222+
, C.txOuts = Tx.getTxOut <$> o
222223
}, o)
223224

224225
-- | Generate a valid transaction, using the unspent outputs provided.
@@ -269,7 +270,7 @@ makeTx
269270
-> m CardanoTx
270271
makeTx bodyContent = do
271272
txBody <- either (fail . ("Can't create TxBody" <>) . show) pure $ C.makeTransactionBody bodyContent
272-
pure $ signAll $ CardanoApiTx $ CardanoApiEmulatorEraTx $ C.Tx txBody []
273+
pure $ signAll $ CardanoEmulatorEraTx $ C.Tx txBody []
273274

274275
-- | Generate a valid transaction, using the unspent outputs provided.
275276
-- Fails if the there are no unspent outputs, or if the total value
@@ -333,27 +334,12 @@ genValidTransactionBodySpending' g ins totalVal = do
333334
(fail "Cannot gen collateral")
334335
(failOnCardanoError . (C.toCardanoTxInsCollateral . map toTxInput . flip take ins . fromIntegral))
335336
(gmMaxCollateralInputs g)
336-
pure $ C.TxBodyContent
337-
{ txIns
338-
, txInsCollateral
339-
, txMintValue
340-
, txFee = C.toCardanoFee fee'
341-
, txOuts = Tx.getTxOut <$> txOutputs
342-
-- unused:
343-
, txProtocolParams = C.BuildTxWith $ Just $ C.fromLedgerPParams C.ShelleyBasedEraBabbage def
344-
, txInsReference = C.TxInsReferenceNone
345-
, txTotalCollateral = C.TxTotalCollateralNone
346-
, txReturnCollateral = C.TxReturnCollateralNone
347-
, txValidityRange = ( C.TxValidityNoLowerBound
348-
, C.TxValidityNoUpperBound C.ValidityNoUpperBoundInBabbageEra)
349-
, txScriptValidity = C.TxScriptValidityNone
350-
, txExtraKeyWits = C.TxExtraKeyWitnessesNone
351-
, txMetadata = C.TxMetadataNone
352-
, txAuxScripts = C.TxAuxScriptsNone
353-
, txWithdrawals = C.TxWithdrawalsNone
354-
355-
, txCertificates = C.TxCertificatesNone
356-
, txUpdateProposal = C.TxUpdateProposalNone
337+
pure $ emptyTxBodyContent
338+
{ C.txIns
339+
, C.txInsCollateral
340+
, C.txMintValue
341+
, C.txFee = C.toCardanoFee fee'
342+
, C.txOuts = Tx.getTxOut <$> txOutputs
357343
}
358344
where
359345
-- | Translate TxIn to TxInput taking out data witnesses if present.
@@ -379,18 +365,11 @@ genValidTransactionBodySpending' g ins totalVal = do
379365
toTxInType Tx.ConsumePublicKeyAddress = Tx.TxConsumePublicKeyAddress
380366
toTxInType (Tx.ScriptAddress valOrRef rd dat) = Tx.TxScriptAddress rd (first validatorHash valOrRef) $ fmap datumHash dat
381367

382-
signTx :: Params -> Map TxOutRef TxOut -> CardanoTx -> CardanoTx
383-
signTx params utxo = let
384-
cUtxoIndex = either (error . show) id $ fromPlutusIndex (Index.UtxoIndex utxo)
385-
in Tx.onCardanoTx
386-
(\t -> fromPlutusTxSigned params cUtxoIndex t CW.knownPaymentKeys)
387-
Tx.CardanoApiTx
388-
389368
-- | Validate a transaction in a mockchain.
390369
validateMockchain :: Mockchain -> CardanoTx -> Maybe Ledger.ValidationErrorInPhase
391370
validateMockchain (Mockchain _ utxo params) tx = result where
392371
cUtxoIndex = either (error . show) id $ fromPlutusIndex (Index.UtxoIndex utxo)
393-
result = leftToMaybe $ validateCardanoTx params 1 cUtxoIndex (signTx params utxo tx)
372+
result = leftToMaybe $ validateCardanoTx params 1 cUtxoIndex tx
394373

395374
-- | Generate an 'Interval where the lower bound if less or equal than the
396375
-- upper bound.

cardano-node-emulator/src/Cardano/Node/Emulator/Validation.hs

Lines changed: 7 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
{-# LANGUAGE TemplateHaskell #-}
77
{-# LANGUAGE TupleSections #-}
88
{-# LANGUAGE TypeApplications #-}
9+
{-# LANGUAGE ViewPatterns #-}
10+
911
{-| Transaction validation using 'cardano-ledger-specs'
1012
-}
1113
module Cardano.Node.Emulator.Validation(
@@ -19,9 +21,6 @@ module Cardano.Node.Emulator.Validation(
1921
hasValidationErrors,
2022
makeTransactionBody,
2123
validateCardanoTx,
22-
fromPlutusTx,
23-
fromPlutusTxSigned,
24-
fromPlutusTxSigned',
2524
-- * Modifying the state
2625
makeBlock,
2726
setSlot,
@@ -37,7 +36,6 @@ module Cardano.Node.Emulator.Validation(
3736
emulatorGlobals,
3837
) where
3938

40-
import Cardano.Api qualified as C
4139
import Cardano.Api.Shelley qualified as C.Api
4240
import Cardano.Ledger.Alonzo.PlutusScriptApi (collectTwoPhaseScriptInputs, evalScripts)
4341
import Cardano.Ledger.Alonzo.Rules.Utxos (UtxosPredicateFailure (CollectErrors))
@@ -58,29 +56,22 @@ import Cardano.Ledger.Shelley.LedgerState (LedgerState (..), UTxOState (..), sma
5856
import Cardano.Ledger.Shelley.Rules.Utxo (UtxoEnv (..))
5957
import Cardano.Ledger.Shelley.TxBody (DCert, Wdrl)
6058
import Cardano.Ledger.ShelleyMA.Timelocks (ValidityInterval)
61-
import Cardano.Node.Emulator.Params (EmulatorEra, Params (emulatorPParams, pNetworkId), emulatorGlobals,
62-
emulatorPParams)
59+
import Cardano.Node.Emulator.Params (EmulatorEra, Params (emulatorPParams), emulatorGlobals, emulatorPParams)
6360
import Cardano.Slotting.Slot (SlotNo (..))
6461
import Control.Lens (makeLenses, over, (&), (.~), (^.))
6562
import Control.Monad.Except (MonadError (throwError))
6663
import Data.Array (array)
6764
import Data.Bifunctor (Bifunctor (..))
6865
import Data.Default (def)
69-
import Data.Foldable (foldl')
7066
import Data.Map qualified as Map
71-
import Data.Maybe (mapMaybe)
7267
import Data.Sequence.Strict (StrictSeq)
7368
import Data.Set (Set)
7469
import Data.Text qualified as Text
7570
import GHC.Records (HasField (..))
76-
import Ledger.Address qualified as P
77-
import Ledger.Crypto qualified as Crypto
7871
import Ledger.Index.Internal qualified as P
7972
import Ledger.Slot (Slot)
80-
import Ledger.Tx (CardanoTx (CardanoApiTx), SomeCardanoApiTx (CardanoApiEmulatorEraTx, SomeTx), addCardanoTxSignature,
81-
onCardanoTx)
73+
import Ledger.Tx (CardanoTx (CardanoEmulatorEraTx))
8274
import Ledger.Tx.CardanoAPI qualified as P
83-
import Ledger.Tx.Internal qualified as P
8475
import Plutus.V1.Ledger.Api qualified as V1 hiding (TxOut (..))
8576
import Plutus.V1.Ledger.Scripts qualified as P
8677

@@ -261,11 +252,9 @@ validateCardanoTx
261252
-> UTxO EmulatorEra
262253
-> CardanoTx
263254
-> Either P.ValidationErrorInPhase P.ValidationSuccess
264-
validateCardanoTx params slot utxo@(UTxO utxoMap) =
265-
onCardanoTx
266-
(\_ -> error "validateCardanoTx: EmulatorTx is not supported")
267-
(\(CardanoApiEmulatorEraTx tx) -> if Map.null utxoMap then Right Map.empty else
268-
hasValidationErrors params (fromIntegral slot) utxo tx)
255+
validateCardanoTx params slot utxo@(UTxO utxoMap) (CardanoEmulatorEraTx tx) =
256+
if Map.null utxoMap then Right Map.empty else
257+
hasValidationErrors params (fromIntegral slot) utxo tx
269258

270259
getTxExUnitsWithLogs :: Params -> UTxO EmulatorEra -> C.Api.Tx C.Api.BabbageEra -> Either P.ValidationErrorInPhase P.ValidationSuccess
271260
getTxExUnitsWithLogs params utxo (C.Api.ShelleyTx _ tx) =
@@ -292,41 +281,3 @@ makeTransactionBody params utxo txBodyContent = do
292281
txTmp <- bimap Right (C.Api.makeSignedTransaction []) $ P.makeTransactionBody (Just $ emulatorPParams params) mempty txBodyContent
293282
exUnits <- bimap Left (Map.map snd) $ getTxExUnitsWithLogs params utxo txTmp
294283
first Right $ P.makeTransactionBody (Just $ emulatorPParams params) exUnits txBodyContent
295-
296-
fromPlutusTx
297-
:: Params
298-
-> UTxO EmulatorEra
299-
-> [P.PaymentPubKeyHash]
300-
-> P.Tx
301-
-> Either CardanoLedgerError (C.Tx C.BabbageEra)
302-
fromPlutusTx params utxo requiredSigners tx = do
303-
txBodyContent <- first Right $ P.toCardanoTxBodyContent (pNetworkId params) (emulatorPParams params) requiredSigners tx
304-
C.Api.makeSignedTransaction [] <$> makeTransactionBody params utxo txBodyContent
305-
306-
fromPlutusTxSigned
307-
:: Params
308-
-> UTxO EmulatorEra
309-
-> P.Tx
310-
-> Map.Map P.PaymentPubKey P.PaymentPrivateKey
311-
-> CardanoTx
312-
fromPlutusTxSigned params utxo tx knownPaymentKeys = case fromPlutusTxSigned' params utxo tx knownPaymentKeys of
313-
Left e -> error ("fromPlutusTxSigned: failed to convert " ++ show e)
314-
Right t -> t
315-
316-
fromPlutusTxSigned'
317-
:: Params
318-
-> UTxO EmulatorEra
319-
-> P.Tx
320-
-> Map.Map P.PaymentPubKey P.PaymentPrivateKey
321-
-> Either CardanoLedgerError CardanoTx
322-
fromPlutusTxSigned' params utxo tx knownPaymentKeys =
323-
let
324-
getPrivateKey = fmap P.unPaymentPrivateKey . flip Map.lookup knownPaymentKeys . P.PaymentPubKey
325-
getPublicKeys = Map.keys . P.txSignatures
326-
privateKeys = mapMaybe getPrivateKey $ getPublicKeys tx
327-
signTx txn = foldl' (flip addCardanoTxSignature) txn privateKeys
328-
convertTx t =
329-
flip SomeTx C.BabbageEraInCardanoMode
330-
<$> fromPlutusTx params utxo (P.PaymentPubKeyHash . Crypto.pubKeyHash <$> getPublicKeys t) t
331-
in
332-
signTx . CardanoApiTx <$> convertTx tx

cardano-streaming/examples/Example2.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ main = do
2626
flip map txs $ \tx@(Cardano.Api.Tx txBody _) ->
2727
let scriptData = Ledger.Tx.CardanoAPI.scriptDataFromCardanoTxBody txBody
2828
txId = Ledger.Tx.CardanoAPI.fromCardanoTxId $ Cardano.Api.getTxId txBody
29-
txOutRefs = Ledger.Tx.CardanoAPI.txOutRefs (workaround (Ledger.Tx.CardanoAPI.SomeTx tx) eim)
29+
txOutRefs = Ledger.Tx.CardanoAPI.txOutRefs (workaround (Ledger.Tx.CardanoAPI.CardanoTx tx) eim)
3030
in Aeson.object
3131
[ "txId" .= txId,
3232
"txOutRefs" .= txOutRefs,

0 commit comments

Comments
 (0)