Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add era-independent "debug transaction view" command #840

Merged
merged 7 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ library
Cardano.CLI.Commands
Cardano.CLI.Commands.Debug
Cardano.CLI.Commands.Debug.LogEpochState
Cardano.CLI.Commands.Debug.TransactionView
Cardano.CLI.Commands.Hash
Cardano.CLI.Commands.Ping
Cardano.CLI.Environment
Expand Down Expand Up @@ -155,6 +156,7 @@ library
Cardano.CLI.Run
Cardano.CLI.Run.Debug
Cardano.CLI.Run.Debug.LogEpochState
Cardano.CLI.Run.Debug.TransactionView
Cardano.CLI.Run.Hash
Cardano.CLI.Run.Ping
Cardano.CLI.TopHandler
Expand All @@ -164,6 +166,7 @@ library
Cardano.CLI.Types.Errors.BootstrapWitnessError
Cardano.CLI.Types.Errors.CardanoAddressSigningKeyConversionError
Cardano.CLI.Types.Errors.CmdError
Cardano.CLI.Types.Errors.DebugCmdError
Cardano.CLI.Types.Errors.DelegationError
Cardano.CLI.Types.Errors.GenesisCmdError
Cardano.CLI.Types.Errors.GovernanceActionsError
Expand Down
4 changes: 3 additions & 1 deletion cardano-cli/src/Cardano/CLI/Commands/Debug.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Cardano.CLI.Commands.Debug
where

import Cardano.CLI.Commands.Debug.LogEpochState
import Cardano.CLI.Commands.Debug.TransactionView

newtype DebugCmds
data DebugCmds
= DebugLogEpochStateCmd LogEpochStateCmdArgs
| DebugTransactionViewCmd TransactionViewCmdArgs
12 changes: 12 additions & 0 deletions cardano-cli/src/Cardano/CLI/Commands/Debug/TransactionView.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{-# LANGUAGE DataKinds #-}

module Cardano.CLI.Commands.Debug.TransactionView where

import Cardano.CLI.Types.Common

data TransactionViewCmdArgs = TransactionViewCmdArgs
{ outputFormat :: !ViewOutputFormat
, mOutFile :: !(Maybe (File () Out))
, inputTxBodyOrTxFile :: !InputTxBodyOrTxFile
}
deriving Show
15 changes: 15 additions & 0 deletions cardano-cli/src/Cardano/CLI/Options/Debug.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Cardano.Api.Shelley hiding (QueryInShelleyBasedEra (..))

import Cardano.CLI.Commands.Debug
import Cardano.CLI.Commands.Debug.LogEpochState
import Cardano.CLI.Commands.Debug.TransactionView
import Cardano.CLI.Environment
import Cardano.CLI.EraBased.Options.Common

Expand Down Expand Up @@ -45,6 +46,13 @@ pDebugCmds envCli =
, " The log file format is line delimited JSON."
, " The command will not terminate."
]
, subParser "transaction" $
Opt.info
( asum
[ subParser "view" (Opt.info pTransactionView $ Opt.progDesc "Print a transaction.")
]
)
(Opt.progDesc "Transaction commands")
]
where
pLogEpochStateCmdArgs :: Parser DebugCmds
Expand All @@ -56,6 +64,13 @@ pDebugCmds envCli =
<*> pFileOutDirection
"out-file"
"Output filepath of the log file. The log file format is line delimited JSON."
pTransactionView :: Parser DebugCmds
pTransactionView =
fmap DebugTransactionViewCmd $
TransactionViewCmdArgs
<$> pTxViewOutputFormat
<*> pMaybeOutputFile
<*> pInputTxOrTxBodyFile

pNodeConfigurationFileIn :: Parser (NodeConfigFile In)
pNodeConfigurationFileIn =
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ renderClientCommandError = \case
PingClientError err ->
renderPingClientCmdError err
DebugCmdError err ->
renderDebugCmdError err
prettyError err

runDisplayVersion :: ExceptT ClientCommandErrors IO ()
runDisplayVersion = do
Expand Down
15 changes: 5 additions & 10 deletions cardano-cli/src/Cardano/CLI/Run/Debug.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,17 @@ module Cardano.CLI.Run.Debug
( DebugCmdError (..)
, runLogEpochStateCmd
, runDebugCmds
, renderDebugCmdError
)
where

import Cardano.Api

import Cardano.CLI.Commands.Debug
import Cardano.CLI.Run.Debug.LogEpochState

import Control.Monad.IO.Class
import Control.Monad.Trans.Except
import Prettyprinter

data DebugCmdError = DebugCmdFailed
import Cardano.CLI.Run.Debug.TransactionView (runTransactionViewCmd)
import Cardano.CLI.Types.Errors.DebugCmdError

runDebugCmds :: DebugCmds -> ExceptT DebugCmdError IO ()
runDebugCmds = \case
DebugLogEpochStateCmd cmd -> liftIO $ runLogEpochStateCmd cmd

renderDebugCmdError :: DebugCmdError -> Doc ann
renderDebugCmdError DebugCmdFailed = "Debug command failed"
DebugTransactionViewCmd cmd -> firstExceptT DebugTxCmdError $ runTransactionViewCmd cmd
50 changes: 50 additions & 0 deletions cardano-cli/src/Cardano/CLI/Run/Debug/TransactionView.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{-# LANGUAGE NamedFieldPuns #-}

module Cardano.CLI.Run.Debug.TransactionView
( runTransactionViewCmd
)
where

import Cardano.Api

import Cardano.CLI.Commands.Debug.TransactionView
import Cardano.CLI.Json.Friendly (FriendlyFormat (..), friendlyTx, friendlyTxBody)
import Cardano.CLI.Read
import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Errors.TxCmdError

import Data.Function ((&))

runTransactionViewCmd
:: ()
=> TransactionViewCmdArgs
-> ExceptT TxCmdError IO ()
runTransactionViewCmd
TransactionViewCmdArgs
{ outputFormat
, mOutFile
, inputTxBodyOrTxFile
} =
case inputTxBodyOrTxFile of
InputTxBodyFile (File txbodyFilePath) -> do
txbodyFile <- liftIO $ fileOrPipe txbodyFilePath
unwitnessed <-
firstExceptT TxCmdTextEnvCddlError . newExceptT $
readFileTxBody txbodyFile
InAnyShelleyBasedEra era txbody <- pure $ unIncompleteCddlTxBody unwitnessed
-- Why are we differentiating between a transaction body and a 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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something the new api would fix because by default we would either produce an unsigned tx or a signed tx. No need to think about transaction bodies that are missing witnesses.

-- this would mean that we'd have an empty list of witnesses mentioned in the output, which
-- is arguably not part of the transaction body.
firstExceptT TxCmdWriteFileError . newExceptT $
case outputFormat of
ViewOutputFormatYaml -> friendlyTxBody FriendlyYaml mOutFile (toCardanoEra era) txbody
ViewOutputFormatJson -> friendlyTxBody FriendlyJson mOutFile (toCardanoEra era) txbody
InputTxFile (File txFilePath) -> do
txFile <- liftIO $ fileOrPipe txFilePath
InAnyShelleyBasedEra era tx <- lift (readFileTx txFile) & onLeft (left . TxCmdTextEnvCddlError)
firstExceptT TxCmdWriteFileError . newExceptT $
case outputFormat of
ViewOutputFormatYaml -> friendlyTx FriendlyYaml mOutFile (toCardanoEra era) tx
ViewOutputFormatJson -> friendlyTx FriendlyJson mOutFile (toCardanoEra era) tx
19 changes: 19 additions & 0 deletions cardano-cli/src/Cardano/CLI/Types/Errors/DebugCmdError.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{-# LANGUAGE LambdaCase #-}

module Cardano.CLI.Types.Errors.DebugCmdError
( DebugCmdError (..)
)
where

import Cardano.Api

import Cardano.CLI.Types.Errors.TxCmdError

data DebugCmdError
= DebugCmdFailed
| DebugTxCmdError !TxCmdError

instance Error DebugCmdError where
prettyError = \case
DebugCmdFailed -> "Debug command failed"
DebugTxCmdError err -> renderTxCmdError err