Skip to content

Commit

Permalink
Remove UnwitnessedCliFormattedTxBody constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
smelc committed Apr 15, 2024
1 parent ad12f22 commit 51cf335
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 40 deletions.
42 changes: 13 additions & 29 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 <-
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
33 changes: 22 additions & 11 deletions cardano-cli/src/Cardano/CLI/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ module Cardano.CLI.Read
-- * Tx
, CddlError(..)
, CddlTx(..)
, IncompleteTx(..)
, ReadFileTxBody(..)
, getReadTxBody
, readFileTx
, readFileTxBody
, readCddlTx -- For testing purposes
Expand Down Expand Up @@ -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)

Check notice

Code scanning / HLint

Redundant bracket Note

cardano-cli/src/Cardano/CLI/Read.hs:530:36-64: Suggestion: Redundant bracket
  
Found:
  ReadFileTxBody -> (InAnyShelleyBasedEra TxBody)
  
Perhaps:
  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)
Expand Down

0 comments on commit 51cf335

Please sign in to comment.