Skip to content

Commit

Permalink
Move transaction echo command to under 'debug' command group.
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Jun 18, 2024
1 parent b0f4e01 commit e264596
Show file tree
Hide file tree
Showing 27 changed files with 223 additions and 188 deletions.
9 changes: 8 additions & 1 deletion cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ library
Cardano.CLI.Commands.Ping
Cardano.CLI.Commands.Debug
Cardano.CLI.Commands.Debug.LogEpochState
Cardano.CLI.Commands.Debug.Transaction
Cardano.CLI.Commands.Debug.Transaction.Echo
Cardano.CLI.Environment
Cardano.CLI.EraBased.Commands
Cardano.CLI.EraBased.Commands.Address
Expand Down Expand Up @@ -143,6 +145,8 @@ library
Cardano.CLI.Legacy.Run.Transaction
Cardano.CLI.Options
Cardano.CLI.Options.Debug
Cardano.CLI.Options.Debug.Transaction
Cardano.CLI.Options.Debug.Transaction.Echo
Cardano.CLI.Options.Ping
Cardano.CLI.Orphans
Cardano.CLI.OS.Posix
Expand All @@ -153,15 +157,17 @@ library
Cardano.CLI.Run
Cardano.CLI.Run.Debug
Cardano.CLI.Run.Debug.LogEpochState
Cardano.CLI.Run.Debug.Transaction
Cardano.CLI.Run.Debug.Transaction.Echo
Cardano.CLI.Run.Ping
Cardano.CLI.TopHandler
Cardano.CLI.Types.Common
Cardano.CLI.Types.MonadWarning
Cardano.CLI.Types.Errors.AddressCmdError
Cardano.CLI.Types.Errors.AddressInfoError
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 All @@ -187,6 +193,7 @@ library
Cardano.CLI.Types.Errors.TextViewFileError
Cardano.CLI.Types.Errors.TxCmdError
Cardano.CLI.Types.Errors.TxValidationError
Cardano.CLI.Types.MonadWarning
Cardano.CLI.Types.Governance
Cardano.CLI.Types.Key
Cardano.CLI.Types.Key.VerificationKey
Expand Down
6 changes: 4 additions & 2 deletions cardano-cli/src/Cardano/CLI/Commands/Debug.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module Cardano.CLI.Commands.Debug
) where

import Cardano.CLI.Commands.Debug.LogEpochState
import Cardano.CLI.Commands.Debug.Transaction

newtype DebugCmds =
DebugLogEpochStateCmd LogEpochStateCmdArgs
data DebugCmds =
DebugLogEpochStateCmd LogEpochStateCmdArgs
| DebugTransactionCmds DebugTransactionCmds
8 changes: 8 additions & 0 deletions cardano-cli/src/Cardano/CLI/Commands/Debug/Transaction.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Cardano.CLI.Commands.Debug.Transaction
( DebugTransactionCmds (..)
) where

import Cardano.CLI.Commands.Debug.Transaction.Echo

newtype DebugTransactionCmds =
DebugTransactionEchoCmd TransactionEchoCmdArgs
15 changes: 15 additions & 0 deletions cardano-cli/src/Cardano/CLI/Commands/Debug/Transaction/Echo.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}

module Cardano.CLI.Commands.Debug.Transaction.Echo
( TransactionEchoCmdArgs(..)
) where

import Cardano.Api.Shelley

import Cardano.CLI.Types.Common

data TransactionEchoCmdArgs = TransactionEchoCmdArgs
{ txOrTxBodyFile :: !InputTxBodyOrTxFile
, outTxFile :: !(TxFile Out)
} deriving Show
6 changes: 1 addition & 5 deletions cardano-cli/src/Cardano/CLI/EraBased/Commands/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module Cardano.CLI.EraBased.Commands.Transaction
import Cardano.Api.Ledger (Coin)
import Cardano.Api.Shelley

import Cardano.CLI.Commands.Debug.Transaction.Echo
import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Governance

Expand Down Expand Up @@ -179,11 +180,6 @@ data TransactionBuildEstimateCmdArgs era = TransactionBuildEstimateCmdArgs
, txBodyOutFile :: !(TxBodyFile Out)
}

data TransactionEchoCmdArgs = TransactionEchoCmdArgs
{ txOrTxBodyFile :: !InputTxBodyOrTxFile
, outTxFile :: !(TxFile Out)
} deriving Show

data TransactionSignCmdArgs = TransactionSignCmdArgs
{ txOrTxBodyFile :: !InputTxBodyOrTxFile
, witnessSigningData :: ![WitnessSigningData]
Expand Down
11 changes: 0 additions & 11 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ pTransactionCmds era envCli =
$ subParser "sign"
$ Opt.info (pTransactionSign envCli)
$ Opt.progDesc "Sign a transaction"
, Just
$ subParser "echo"
$ Opt.info pTransactionEcho
$ Opt.progDesc "Echo a transaction"
, Just
$ subParser "witness"
$ Opt.info (pTransactionCreateWitness envCli)
Expand Down Expand Up @@ -287,13 +283,6 @@ pTransactionSign envCli =
<*> optional (pNetworkId envCli)
<*> pTxFileOut

pTransactionEcho :: Parser (TransactionCmds era)
pTransactionEcho =
fmap TransactionEchoCmd $
TransactionEchoCmdArgs
<$> pInputTxOrTxBodyFile
<*> pTxFileOut

pTransactionCreateWitness :: EnvCli -> Parser (TransactionCmds era)
pTransactionCreateWitness envCli =
fmap TransactionWitnessCmd $
Expand Down
67 changes: 67 additions & 0 deletions cardano-cli/src/Cardano/CLI/Options/Debug/Transaction.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}

{- HLINT ignore "Use <$>" -}
{- HLINT ignore "Move brackets to avoid $" -}

module Cardano.CLI.Options.Debug.Transaction
( parseDebugCmds
) where

import Cardano.Api.Shelley hiding (QueryInShelleyBasedEra (..))

import Cardano.CLI.Commands.Debug
import Cardano.CLI.Commands.Debug.LogEpochState
import Cardano.CLI.Environment
import Cardano.CLI.EraBased.Options.Common
import Cardano.CLI.Options.Debug.Transaction.Echo

import Data.Foldable
import Options.Applicative hiding (help, str)
import qualified Options.Applicative as Opt

parseDebugCmds :: EnvCli -> Parser DebugCmds
parseDebugCmds envCli =
Opt.hsubparser $ mconcat
[ Opt.metavar "debug transaction commands"
, Opt.commandGroup "debug commands"
, Opt.command "transaction"
$ Opt.info (pDebugCmds envCli)
$ Opt.progDesc "Debug transaction commands"
]

pDebugCmds :: EnvCli -> Parser DebugCmds
pDebugCmds envCli =
asum
[ subParser "log-epoch-state"
$ Opt.info pLogEpochStateCmdArgs
$ Opt.progDesc
$ mconcat
[ "Log epoch state of a running node."
, " This command will connect to a local node and log the epoch state to a file."
, " The log file format is line delimited JSON."
, " The command will not terminate."
]
, subParser "echo"
$ Opt.info (DebugTransactionCmds <$> pDebugTransactionEcho)
$ Opt.progDesc "Echo a transaction"
]
where
pLogEpochStateCmdArgs :: Parser DebugCmds
pLogEpochStateCmdArgs =
fmap DebugLogEpochStateCmd $
LogEpochStateCmdArgs
<$> pSocketPath envCli
<*> pNodeConfigurationFileIn
<*> pFileOutDirection "out-file" "Output filepath of the log file. The log file format is line delimited JSON."

pNodeConfigurationFileIn :: Parser (NodeConfigFile In)
pNodeConfigurationFileIn =
fmap File $ Opt.strOption $ mconcat
[ Opt.long "node-configuration-file"
, Opt.metavar "FILE"
, Opt.help "Input filepath of the node configuration file."
, Opt.completer (Opt.bashCompleter "file")
]
21 changes: 21 additions & 0 deletions cardano-cli/src/Cardano/CLI/Options/Debug/Transaction/Echo.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Cardano.CLI.Options.Debug.Transaction.Echo
( pDebugTransactionEcho
) where

import Cardano.CLI.Commands.Debug.Transaction
import Cardano.CLI.Commands.Debug.Transaction.Echo
import Cardano.CLI.EraBased.Options.Common

import Options.Applicative hiding (help, str)

pDebugTransactionEcho :: Parser DebugTransactionCmds
pDebugTransactionEcho =
fmap DebugTransactionEchoCmd $
TransactionEchoCmdArgs
<$> pInputTxOrTxBodyFile
<*> pTxFileOut
22 changes: 15 additions & 7 deletions cardano-cli/src/Cardano/CLI/Run/Debug.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@ module Cardano.CLI.Run.Debug
, 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.Transaction
import Cardano.CLI.Types.Errors.DebugCmdError

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

renderDebugCmdError :: DebugCmdError -> Doc ann
renderDebugCmdError DebugCmdFailed = "Debug command failed"
renderDebugCmdError = \case
DebugCmdFailed ->
"Debug command failed"
DebugCmdWriteFileError fileErr ->
prettyError fileErr
DebugCmdTextEnvCddlError cddlErr ->
mconcat
[ "Failed to decode the ledger's CDDL serialisation format. "
, "TextEnvelopeCddl error: " <> prettyError cddlErr
]
17 changes: 17 additions & 0 deletions cardano-cli/src/Cardano/CLI/Run/Debug/Transaction.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{-# LANGUAGE LambdaCase #-}

module Cardano.CLI.Run.Debug.Transaction
( runDebugTransactionCmds
) where

import Cardano.Api

import Cardano.CLI.Commands.Debug.Transaction
import Cardano.CLI.Run.Debug.Transaction.Echo
import Cardano.CLI.Types.Errors.DebugCmdError

runDebugTransactionCmds
:: DebugTransactionCmds
-> ExceptT DebugCmdError IO ()
runDebugTransactionCmds = \case
DebugTransactionEchoCmd cmd -> runDebugTransactionEchoCmd cmd
51 changes: 51 additions & 0 deletions cardano-cli/src/Cardano/CLI/Run/Debug/Transaction/Echo.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Cardano.CLI.Run.Debug.Transaction.Echo
( runDebugTransactionEchoCmd
) where

import Cardano.Api

import qualified Cardano.CLI.Commands.Debug.Transaction.Echo as Cmd
import Cardano.CLI.Orphans ()
import Cardano.CLI.Read
import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Errors.DebugCmdError

import Data.Function ((&))

runDebugTransactionEchoCmd :: ()
=> Cmd.TransactionEchoCmdArgs
-> ExceptT DebugCmdError IO ()
runDebugTransactionEchoCmd
Cmd.TransactionEchoCmdArgs
{ Cmd.txOrTxBodyFile = txOrTxBody
, Cmd.outTxFile = outTxFile
} = do

case txOrTxBody of
InputTxFile (File inputTxFilePath) -> do
inputTxFile <- liftIO $ fileOrPipe inputTxFilePath
anyTx <- lift (readFileTx inputTxFile) & onLeft (left . DebugCmdTextEnvCddlError)

InAnyShelleyBasedEra sbe tx <- pure anyTx

lift (writeTxFileTextEnvelopeCddl sbe outTxFile tx)
& onLeft (left . DebugCmdWriteFileError)

InputTxBodyFile (File txbodyFilePath) -> do
txbodyFile <- liftIO $ fileOrPipe txbodyFilePath
unwitnessed <- firstExceptT DebugCmdTextEnvCddlError . newExceptT $ readFileTxBody txbodyFile

case unwitnessed of
IncompleteCddlTxBody anyTxBody -> do
InAnyShelleyBasedEra sbe txbody <- pure anyTxBody

let tx = makeSignedTransaction [] txbody

firstExceptT DebugCmdWriteFileError . newExceptT
$ writeLazyByteStringFile outTxFile
$ shelleyBasedEraConstraints sbe
$ textEnvelopeToJSON Nothing tx
16 changes: 16 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,16 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GADTs #-}

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

import Cardano.Api

import GHC.Generics (Generic)

data DebugCmdError =
DebugCmdFailed
| DebugCmdTextEnvCddlError !(FileError TextEnvelopeCddlError)
| DebugCmdWriteFileError !(FileError ())
deriving (Show, Generic)
Loading

0 comments on commit e264596

Please sign in to comment.