Skip to content

Commit

Permalink
Move PingCmd to Cardano.CLI.Commands.Ping
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed May 25, 2024
1 parent e1a2748 commit 2a9e2de
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ library
Cardano.CLI.Byron.Vote
Cardano.CLI.Commands
Cardano.CLI.Commands.Ping
Cardano.CLI.Commands.Debug
Cardano.CLI.Commands.Debug.LogEpochState
Cardano.CLI.Environment
Cardano.CLI.EraBased.Commands
Cardano.CLI.EraBased.Commands.Address
Expand Down Expand Up @@ -140,13 +142,17 @@ library
Cardano.CLI.Legacy.Run.TextView
Cardano.CLI.Legacy.Run.Transaction
Cardano.CLI.Options
Cardano.CLI.Options.Debug
Cardano.CLI.Options.Ping
Cardano.CLI.Orphans
Cardano.CLI.OS.Posix
Cardano.CLI.Parser
Cardano.CLI.Pretty
Cardano.CLI.Read
Cardano.CLI.Render
Cardano.CLI.Run
Cardano.CLI.Run.Debug
Cardano.CLI.Run.Debug.LogEpochState
Cardano.CLI.Run.Ping
Cardano.CLI.TopHandler
Cardano.CLI.Types.Common
Expand Down Expand Up @@ -206,7 +212,9 @@ library
, cardano-crypto-wrapper ^>= 1.5.1
, cardano-data >= 1.1
, cardano-git-rev ^>= 0.2.2
, cardano-ledger-api
, cardano-ledger-byron >= 1.0.1.0
, cardano-ledger-shelley
, cardano-ping ^>= 0.2.0.13
, cardano-prelude
, cardano-slotting ^>= 0.2.0.0
Expand Down
2 changes: 2 additions & 0 deletions cardano-cli/src/Cardano/CLI/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Cardano.CLI.Commands
) where

import Cardano.CLI.Byron.Commands (ByronCommand)
import Cardano.CLI.Commands.Debug
import Cardano.CLI.Commands.Ping (PingCmd (..))
import Cardano.CLI.EraBased.Commands
import Cardano.CLI.Legacy.Commands
Expand All @@ -22,6 +23,7 @@ data ClientCommand =
| LegacyCmds LegacyCmds

| CliPingCommand PingCmd
| CliDebugCmds DebugCmds

| forall a. Help ParserPrefs (ParserInfo a)
| DisplayVersion
8 changes: 8 additions & 0 deletions cardano-cli/src/Cardano/CLI/Commands/Debug.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Cardano.CLI.Commands.Debug
( DebugCmds (..)
) where

import Cardano.CLI.Commands.Debug.LogEpochState

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

module Cardano.CLI.Commands.Debug.LogEpochState
( LogEpochStateCmdArgs(..)
, Configuration
) where

import Cardano.Api

import Cardano.CLI.Orphans ()

data Configuration

data LogEpochStateCmdArgs = LogEpochStateCmdArgs
{ nodeSocketPath :: !SocketPath
, configurationFile :: !(NodeConfigFile 'In)
, outputFilePath :: !(File Configuration 'Out)
} deriving Show
5 changes: 5 additions & 0 deletions cardano-cli/src/Cardano/CLI/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Cardano.CLI.Environment (EnvCli)
import Cardano.CLI.EraBased.Commands
import Cardano.CLI.EraBased.Options.Common
import Cardano.CLI.Legacy.Options (parseLegacyCmds)
import Cardano.CLI.Options.Debug
import Cardano.CLI.Options.Ping (parsePingCmd)
import Cardano.CLI.Render (customRenderHelp)
import Cardano.CLI.Run (ClientCommand (..))
Expand Down Expand Up @@ -55,6 +56,7 @@ parseClientCommand envCli =
, parseTopLevelLegacy envCli
, parseByron envCli
, parsePing
, parseDebug envCli
, backwardsCompatibilityCommands envCli
, parseDisplayVersion (opts envCli)
]
Expand All @@ -71,6 +73,9 @@ parseByron mNetworkId =
parsePing :: Parser ClientCommand
parsePing = CliPingCommand <$> parsePingCmd

parseDebug :: EnvCli -> Parser ClientCommand
parseDebug envCli = CliDebugCmds <$> parseDebugCmds envCli

parseAnyEra :: EnvCli -> Parser ClientCommand
parseAnyEra envCli = AnyEraCommand <$> pAnyEraCommand envCli

Expand Down
56 changes: 56 additions & 0 deletions cardano-cli/src/Cardano/CLI/Options/Debug.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}

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

module Cardano.CLI.Options.Debug
( 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 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 commands"
, Opt.commandGroup "Debug commands"
, Opt.command "debug"
$ Opt.info (pDebugCmds envCli)
$ Opt.progDesc "Debug commands"
]

pDebugCmds :: EnvCli -> Parser DebugCmds
pDebugCmds envCli =
asum
[ subParser "log-epoch-state"
(Opt.info pLogEpochStateCmdArgs $ Opt.progDesc "Log epoch state.")
]
where
pLogEpochStateCmdArgs :: Parser DebugCmds
pLogEpochStateCmdArgs =
fmap DebugLogEpochStateCmd $
LogEpochStateCmdArgs
<$> pSocketPath envCli
<*> pNodeConfigurationFileIn
<*> pFileOutDirection "out-file" "Output filepath of the log file."

pNodeConfigurationFileIn :: Parser (NodeConfigFile In)
pNodeConfigurationFileIn =
fmap File $ Opt.strOption $ mconcat
[ Opt.long "verification-key-file"
, Opt.metavar "FILE"
, Opt.help "Input filepath of the node configuration file."
, Opt.completer (Opt.bashCompleter "file")
]
23 changes: 23 additions & 0 deletions cardano-cli/src/Cardano/CLI/Orphans.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{-# OPTIONS_GHC -Wno-orphans #-}

module Cardano.CLI.Orphans
(
) where

import Cardano.Api

import qualified Cardano.Ledger.Api as L
import qualified Cardano.Ledger.Shelley.LedgerState as L

import Data.Aeson

instance (L.EraTxOut ledgerera, L.EraGov ledgerera) => ToJSON (L.NewEpochState ledgerera) where
toJSON (L.NewEpochState nesEL nesBprev nesBCur nesEs nesRu nesPd _stashedAvvm)=
object
[ "currentEpoch" .= nesEL
, "priorBlocks" .= nesBprev
, "currentEpochBlocks" .= nesBCur
, "currentEpochState" .= nesEs
, "rewardUpdate" .= nesRu
, "currentStakeDistribution" .= nesPd
]
6 changes: 6 additions & 0 deletions cardano-cli/src/Cardano/CLI/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Cardano.CLI.EraBased.Run
import Cardano.CLI.Legacy.Commands
import Cardano.CLI.Legacy.Run (runLegacyCmds)
import Cardano.CLI.Render (customRenderHelp)
import Cardano.CLI.Run.Debug
import Cardano.CLI.Run.Ping (PingClientCmdError (..), renderPingClientCmdError,
runPingCmd)
import Cardano.CLI.Types.Errors.CmdError
Expand Down Expand Up @@ -45,6 +46,7 @@ data ClientCommandErrors
= ByronClientError ByronClientCmdError
| CmdError Text CmdError
| PingClientError PingClientCmdError
| DebugCmdError DebugCmdError

runClientCommand :: ClientCommand -> ExceptT ClientCommandErrors IO ()
runClientCommand = \case
Expand All @@ -56,6 +58,8 @@ runClientCommand = \case
firstExceptT (CmdError (renderLegacyCommand cmds)) $ runLegacyCmds cmds
CliPingCommand cmds ->
firstExceptT PingClientError $ runPingCmd cmds
CliDebugCmds cmds ->
firstExceptT DebugCmdError $ runDebugCmds cmds
Help pprefs allParserInfo ->
runHelp pprefs allParserInfo
DisplayVersion ->
Expand All @@ -69,6 +73,8 @@ renderClientCommandError = \case
renderByronClientCmdError err
PingClientError err ->
renderPingClientCmdError err
DebugCmdError err ->
renderDebugCmdError err

runDisplayVersion :: ExceptT ClientCommandErrors IO ()
runDisplayVersion = do
Expand Down
24 changes: 24 additions & 0 deletions cardano-cli/src/Cardano/CLI/Run/Debug.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{-# LANGUAGE LambdaCase #-}

module Cardano.CLI.Run.Debug
( DebugCmdError(..)
, runLogEpochStateCmd
, runDebugCmds
, renderDebugCmdError
) where

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

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

renderDebugCmdError :: DebugCmdError -> Doc ann
renderDebugCmdError DebugCmdFailed = "Debug command failed"
39 changes: 39 additions & 0 deletions cardano-cli/src/Cardano/CLI/Run/Debug/LogEpochState.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE NamedFieldPuns #-}

module Cardano.CLI.Run.Debug.LogEpochState
( runLogEpochStateCmd
) where

import Cardano.Api
import qualified Cardano.Api as Api

import Cardano.CLI.Commands.Debug.LogEpochState
import Cardano.CLI.Orphans ()

import Control.Monad
import Data.Aeson
import qualified Data.ByteString.Lazy.Char8 as BSC

runLogEpochStateCmd
:: LogEpochStateCmdArgs
-> IO ()
runLogEpochStateCmd
LogEpochStateCmdArgs
{ nodeSocketPath
, configurationFile
, outputFilePath = File outputFilePath
} = do
appendFile outputFilePath ""

-- TODO error handling
void $ runExceptT $ foldEpochState
configurationFile
nodeSocketPath
Api.QuickValidation
(EpochNo maxBound)
()
(\(AnyNewEpochState sbe nes) _ _ -> do
liftIO $ appendFile outputFilePath $ BSC.unpack (shelleyBasedEraConstraints sbe $ encode nes) <> "\n"
pure ConditionNotMet
)
11 changes: 11 additions & 0 deletions cardano-cli/test/cardano-cli-golden/files/golden/help.cli
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Usage: cardano-cli
| Legacy commands
| byron
| ping
| Debug commands
| version
)

Expand Down Expand Up @@ -12369,6 +12370,16 @@ Usage: cardano-cli ping [-c|--count COUNT]

Ping a cardano node either using node-to-node or node-to-client protocol. It negotiates a handshake and keeps sending keep alive messages.

Usage: cardano-cli debug log-epoch-state

Debug commands

Usage: cardano-cli debug log-epoch-state --socket-path SOCKET_PATH
--verification-key-file FILE
--out-file FILE

Log epoch state.

Usage: cardano-cli genesis --genesis-output-dir FILEPATH
--start-time POSIXSECONDS
--protocol-parameters-file FILEPATH
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Usage: cardano-cli debug log-epoch-state --socket-path SOCKET_PATH
--verification-key-file FILE
--out-file FILE

Log epoch state.

Available options:
--socket-path SOCKET_PATH
Path to the node socket. This overrides the
CARDANO_NODE_SOCKET_PATH environment variable. The
argument is optional if CARDANO_NODE_SOCKET_PATH is
defined and mandatory otherwise.
--verification-key-file FILE
Input filepath of the node configuration file.
--out-file FILE Output filepath of the log file.
-h,--help Show this help text

0 comments on commit 2a9e2de

Please sign in to comment.