Skip to content

Commit

Permalink
Merge #4468
Browse files Browse the repository at this point in the history
4468: Separate validation and creation of transaction bodies r=Jimbo4350 a=Jimbo4350

Implement `createTransactionBody` and `createAndValidateTransactionBody`. We want to separate `TxBody` construction and validation to allow the creation of invalid transaction bodies.

Closes #3563 


Co-authored-by: Jordan Millar <jordan.millar@iohk.io>
  • Loading branch information
iohk-bors[bot] and Jimbo4350 authored Sep 29, 2022
2 parents 23b855d + c7dd93a commit 59b57bd
Show file tree
Hide file tree
Showing 10 changed files with 629 additions and 361 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ mkGenesisTransaction :: forall era .
-> [TxOut CtxTx era]
-> Tx era
mkGenesisTransaction key ttl fee txins txouts
= case makeTransactionBody txBodyContent of
= case createAndValidateTransactionBody txBodyContent of
Right b -> signShelleyTransaction b [WitnessGenesisUTxOKey key]
Left err -> error $ show err
where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ metadataSize :: forall era . IsShelleyBasedEra era => AsType era -> Maybe TxMeta
metadataSize p m = dummyTxSize p m - dummyTxSize p Nothing

dummyTxSizeInEra :: forall era . IsShelleyBasedEra era => TxMetadataInEra era -> Int
dummyTxSizeInEra metadata = case makeTransactionBody dummyTx of
dummyTxSizeInEra metadata = case createAndValidateTransactionBody dummyTx of
Right b -> BS.length $ serialiseToCBOR b
Left err -> error $ "metaDataSize " ++ show err
where
Expand Down
2 changes: 1 addition & 1 deletion bench/tx-generator/src/Cardano/TxGenerator/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ genTx :: forall era. IsShelleyBasedEra era =>
-> TxMetadataInEra era
-> TxGenerator era
genTx protocolParameters (collateral, collFunds) fee metadata inFunds outputs
= case makeTransactionBody txBodyContent of
= case createAndValidateTransactionBody txBodyContent of
Left err -> Left $ show err
Right b -> Right ( signShelleyTransaction b $ map WitnessPaymentKey allKeys
, getTxId b
Expand Down
2 changes: 1 addition & 1 deletion cardano-api/gen/Gen/Cardano/Api/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ genTxFee era =

genTxBody :: IsCardanoEra era => CardanoEra era -> Gen (TxBody era)
genTxBody era = do
res <- makeTransactionBody <$> genTxBodyContent era
res <- Api.createAndValidateTransactionBody <$> genTxBodyContent era
case res of
Left err -> fail (displayError err)
Right txBody -> pure txBody
Expand Down
3 changes: 2 additions & 1 deletion cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ module Cardano.Api (

-- ** Transaction bodies
TxBody(TxBody),
makeTransactionBody,
createAndValidateTransactionBody,
makeTransactionBody, -- TODO: Remove
TxBodyContent(..),
TxBodyError(..),
TxBodyScriptData(..),
Expand Down
8 changes: 4 additions & 4 deletions cardano-api/src/Cardano/Api/Fees.hs
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ makeTransactionBodyAutoBalance eraInMode systemstart history pparams
-- 3. update tx with fees
-- 4. balance the transaction and update tx change output
txbody0 <-
first TxBodyError $ makeTransactionBody txbodycontent
first TxBodyError $ createAndValidateTransactionBody txbodycontent
{ txOuts =
TxOut changeaddr (lovelaceToTxOutValue 0) TxOutDatumNone ReferenceScriptNone
: txOuts txbodycontent
Expand Down Expand Up @@ -974,7 +974,7 @@ makeTransactionBodyAutoBalance eraInMode systemstart history pparams

let (dummyCollRet, dummyTotColl) = maybeDummyTotalCollAndCollReturnOutput txbodycontent changeaddr
txbody1 <- first TxBodyError $ -- TODO: impossible to fail now
makeTransactionBody txbodycontent1 {
createAndValidateTransactionBody txbodycontent1 {
txFee = TxFeeExplicit explicitTxFees $ Lovelace (2^(32 :: Integer) - 1),
txOuts = TxOut changeaddr
(lovelaceToTxOutValue $ Lovelace (2^(64 :: Integer)) - 1)
Expand All @@ -998,7 +998,7 @@ makeTransactionBodyAutoBalance eraInMode systemstart history pparams
-- Here we do not want to start with any change output, since that's what
-- we need to calculate.
txbody2 <- first TxBodyError $ -- TODO: impossible to fail now
makeTransactionBody txbodycontent1 {
createAndValidateTransactionBody txbodycontent1 {
txFee = TxFeeExplicit explicitTxFees fee,
txReturnCollateral = retColl,
txTotalCollateral = reqCol
Expand Down Expand Up @@ -1027,7 +1027,7 @@ makeTransactionBodyAutoBalance eraInMode systemstart history pparams
-- would fit within 2^16-1. That's a possible optimisation.
txbody3 <-
first TxBodyError $ -- TODO: impossible to fail now
makeTransactionBody txbodycontent1 {
createAndValidateTransactionBody txbodycontent1 {
txFee = TxFeeExplicit explicitTxFees fee,
txOuts = accountForNoChange
(TxOut changeaddr balance TxOutDatumNone ReferenceScriptNone)
Expand Down
Loading

0 comments on commit 59b57bd

Please sign in to comment.