From 51cf335cc0e6951568ff4fd8c8706e1cdd756e4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Hurlin?= Date: Mon, 8 Apr 2024 15:41:15 +0200 Subject: [PATCH] Remove UnwitnessedCliFormattedTxBody constructor --- .../Cardano/CLI/EraBased/Run/Transaction.hs | 42 ++++++------------- cardano-cli/src/Cardano/CLI/Read.hs | 33 ++++++++++----- 2 files changed, 35 insertions(+), 40 deletions(-) diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs b/cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs index 1a61700753..7bebc97ccb 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs @@ -942,10 +942,9 @@ runTransactionSignCmd $ readFileTxBody txbodyFile case unwitnessed of - IncompleteCddlFormattedTx anyTx -> do - InAnyShelleyBasedEra sbe unwitTx <- pure anyTx + IncompleteCddlFormattedTx anyTxBody -> do + InAnyShelleyBasedEra sbe txbody <- pure anyTxBody - let txbody = getTxBody unwitTx -- Byron witnesses require the network ID. This can either be provided -- directly or derived from a provided Byron address. byronWitnesses <- firstExceptT TxCmdBootstrapWitnessError @@ -958,7 +957,7 @@ runTransactionSignCmd lift (writeTxFileTextEnvelopeCddl sbe outTxFile tx) & onLeft (left . TxCmdWriteFileError) - UnwitnessedCliFormattedTxBody anyTxbody -> do + ReadTxBody anyTxbody -> do InAnyShelleyBasedEra sbe txbody <- pure anyTxbody -- Byron witnesses require the network ID. This can either be provided -- directly or derived from a provided Byron address. @@ -1031,12 +1030,7 @@ runTransactionCalculateMinFeeCmd let nShelleyKeyWitW32 = fromIntegral nShelleyKeyWitnesses - InAnyShelleyBasedEra sbe txbody <- case unwitnessed of - IncompleteCddlFormattedTx (InAnyShelleyBasedEra sbe unwitTx) -> do - pure $ InAnyShelleyBasedEra sbe $ getTxBody unwitTx - - UnwitnessedCliFormattedTxBody (InAnyShelleyBasedEra sbe txbody) -> do - pure $ InAnyShelleyBasedEra sbe txbody + InAnyShelleyBasedEra sbe txbody <- pure $ getReadTxBody unwitnessed lpparams <- getLedgerPParams sbe pparams @@ -1199,11 +1193,8 @@ runTransactionTxIdCmd InputTxBodyFile (File txbodyFilePath) -> do txbodyFile <- liftIO $ fileOrPipe txbodyFilePath unwitnessed <- firstExceptT TxCmdCddlError . newExceptT - $ readFileTxBody txbodyFile - case unwitnessed of - UnwitnessedCliFormattedTxBody anyTxBody -> return anyTxBody - IncompleteCddlFormattedTx (InAnyShelleyBasedEra era tx) -> - return (InAnyShelleyBasedEra era (getTxBody tx)) + $ readFileTxBody txbodyFile + return $ getReadTxBody unwitnessed InputTxFile (File txFilePath) -> do txFile <- liftIO $ fileOrPipe txFilePath @@ -1226,12 +1217,8 @@ runTransactionViewCmd txbodyFile <- liftIO $ fileOrPipe txbodyFilePath unwitnessed <- firstExceptT TxCmdCddlError . newExceptT $ readFileTxBody txbodyFile - InAnyShelleyBasedEra era txbody <- - case unwitnessed of - UnwitnessedCliFormattedTxBody anyTxBody -> pure anyTxBody - IncompleteCddlFormattedTx (InAnyShelleyBasedEra era tx) -> - pure $ InAnyShelleyBasedEra era (getTxBody tx) - -- Why are we differentiating between a transaction body and a transaction? + InAnyShelleyBasedEra era txbody <- pure $ getReadTxBody unwitnessed + -- Why are we differentiating between a transaction body and a. newExceptT transaction? -- In the case of a transaction body, we /could/ simply call @makeSignedTransaction []@ -- to get a transaction which would allow us to reuse friendlyTxBS. However, -- this would mean that we'd have an empty list of witnesses mentioned in the output, which @@ -1266,10 +1253,8 @@ runTransactionWitnessCmd unwitnessed <- firstExceptT TxCmdCddlError . newExceptT $ readFileTxBody txbodyFile case unwitnessed of - IncompleteCddlFormattedTx anyTx -> do - InAnyShelleyBasedEra sbe cddlTx <- pure anyTx - - let txbody = getTxBody cddlTx + IncompleteCddlFormattedTx anyTxBody -> do + InAnyShelleyBasedEra sbe txbody <- pure anyTxBody someWit <- firstExceptT TxCmdReadWitnessSigningDataError . newExceptT $ readWitnessSigningData witnessSigningData witness <- @@ -1286,7 +1271,7 @@ runTransactionWitnessCmd firstExceptT TxCmdWriteFileError . newExceptT $ writeTxWitnessFileTextEnvelopeCddl sbe outFile witness - UnwitnessedCliFormattedTxBody anyTxbody -> do + ReadTxBody anyTxbody -> do InAnyShelleyBasedEra sbe txbody <- pure anyTxbody someWit <- firstExceptT TxCmdReadWitnessSigningDataError @@ -1320,7 +1305,7 @@ runTransactionSignWitnessCmd txbodyFile <- liftIO $ fileOrPipe txbodyFilePath unwitnessed <- lift (readFileTxBody txbodyFile) & onLeft (left . TxCmdCddlError) case unwitnessed of - UnwitnessedCliFormattedTxBody (InAnyShelleyBasedEra era txbody) -> do + ReadTxBody (InAnyShelleyBasedEra era txbody) -> do witnesses <- sequence [ do @@ -1339,8 +1324,7 @@ runTransactionSignWitnessCmd lift (writeLazyByteStringFile outFile $ shelleyBasedEraConstraints era $ textEnvelopeToJSON Nothing tx) & onLeft (left . TxCmdWriteFileError) - IncompleteCddlFormattedTx (InAnyShelleyBasedEra era anyTx) -> do - let txbody = getTxBody anyTx + IncompleteCddlFormattedTx (InAnyShelleyBasedEra era txbody) -> do -- TODO: Left off here. Remember we were never reading byron key witnesses anyways! witnesses <- sequence diff --git a/cardano-cli/src/Cardano/CLI/Read.hs b/cardano-cli/src/Cardano/CLI/Read.hs index 4c45f43e75..d81c60a635 100644 --- a/cardano-cli/src/Cardano/CLI/Read.hs +++ b/cardano-cli/src/Cardano/CLI/Read.hs @@ -33,7 +33,8 @@ module Cardano.CLI.Read -- * Tx , CddlError(..) , CddlTx(..) - , IncompleteTx(..) + , ReadFileTxBody(..) + , getReadTxBody , readFileTx , readFileTxBody , readCddlTx -- For testing purposes @@ -518,21 +519,31 @@ readFileTx file = do Left e -> fmap unCddlTx <$> acceptTxCDDLSerialisation file e Right tx -> return $ Right tx --- IncompleteCddlFormattedTx is an CDDL formatted tx or partial tx --- (respectively needs additional witnesses or totally unwitnessed) --- while UnwitnessedCliFormattedTxBody is CLI formatted TxBody and --- needs to be key witnessed. +-- | The return type of 'readFileTxBody'. Use 'getReadTxBody' when you don't need +-- to distinguish the constructors. +data ReadFileTxBody = + ReadTxBody (InAnyShelleyBasedEra TxBody) + | -- | @IncompleteCddlFormattedTx@ is an CDDL formatted tx or partial tx + -- (respectively needs additional witnesses or totally unwitnessed) + IncompleteCddlFormattedTx (InAnyShelleyBasedEra TxBody) -data IncompleteTx - = UnwitnessedCliFormattedTxBody (InAnyShelleyBasedEra TxBody) - | IncompleteCddlFormattedTx (InAnyShelleyBasedEra Tx) +getReadTxBody :: ReadFileTxBody -> (InAnyShelleyBasedEra TxBody) +getReadTxBody = \case + ReadTxBody anyTxBody -> anyTxBody + IncompleteCddlFormattedTx anyTxBody -> anyTxBody -readFileTxBody :: FileOrPipe -> IO (Either CddlError IncompleteTx) +readFileTxBody :: FileOrPipe -> IO (Either CddlError ReadFileTxBody) readFileTxBody file = do eTxBody <- readFileInAnyShelleyBasedEra AsTxBody file case eTxBody of - Left e -> fmap (IncompleteCddlFormattedTx . unCddlTx) <$> acceptTxCDDLSerialisation file e - Right txBody -> return $ Right $ UnwitnessedCliFormattedTxBody txBody + Left e -> do + cddlErrOrTx <- acceptTxCDDLSerialisation file e + case cddlErrOrTx of + Left cddlErr -> return $ Left cddlErr + Right cddlTx -> do + InAnyShelleyBasedEra sbe tx <- pure $ unCddlTx cddlTx + return $ Right $ IncompleteCddlFormattedTx $ inAnyShelleyBasedEra sbe $ getTxBody tx + Right txBody -> return $ Right $ ReadTxBody txBody data CddlError = CddlErrorTextEnv !(FileError TextEnvelopeError)