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

Introduce handshake command + tighter ping UI #468

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ library
Cardano.CLI.Read
Cardano.CLI.Render
Cardano.CLI.Run
Cardano.CLI.Run.HandShake
Cardano.CLI.Run.Ping
Cardano.CLI.TopHandler
Cardano.CLI.Types.Common
Expand Down
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 @@ -23,6 +23,7 @@ import Data.Foldable
import Options.Applicative
import qualified Options.Applicative as Opt
import qualified Prettyprinter as PP
import Cardano.CLI.Run.HandShake (parseHandShakeCmd)

opts :: EnvCli -> ParserInfo ClientCommand
opts envCli =
Expand Down Expand Up @@ -54,6 +55,7 @@ parseClientCommand envCli =
-- , parseTopLevelLatest envCli -- TODO restore this when the governance command group is fully operational
, parseTopLevelLegacy envCli
, parseByron envCli
, parseHandShake
, parsePing
, backwardsCompatibilityCommands envCli
, parseDisplayVersion (opts envCli)
Expand All @@ -68,6 +70,9 @@ parseByron mNetworkId =
, command' "byron" "Byron specific commands" $ parseByronCommands mNetworkId
]

parseHandShake :: Parser ClientCommand
parseHandShake = CliHandShakeCommand <$> parseHandShakeCmd

parsePing :: Parser ClientCommand
parsePing = CliPingCommand <$> parsePingCmd

Expand Down
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,8 @@ import Cardano.CLI.IO.GitRev (gitRev)
import Cardano.CLI.Legacy.Commands
import Cardano.CLI.Legacy.Run (runLegacyCmds)
import Cardano.CLI.Render (customRenderHelp)
import Cardano.CLI.Run.HandShake (HandShakeCmd (..))
import qualified Cardano.CLI.Run.HandShake as HandShake
import Cardano.CLI.Run.Ping (PingClientCmdError (..), PingCmd (..),
renderPingClientCmdError, runPingCmd)
import Cardano.CLI.Types.Errors.CmdError
Expand Down Expand Up @@ -50,6 +52,8 @@ data ClientCommand =
-- | Legacy shelley-based Commands
| LegacyCmds LegacyCmds

| CliHandShakeCommand HandShakeCmd

| CliPingCommand PingCmd

| forall a. Help ParserPrefs (ParserInfo a)
Expand All @@ -68,6 +72,8 @@ runClientCommand = \case
firstExceptT ByronClientError $ runByronClientCommand cmds
LegacyCmds cmds ->
firstExceptT (CmdError (renderLegacyCommand cmds)) $ runLegacyCmds cmds
CliHandShakeCommand cmds ->
firstExceptT PingClientError $ runPingCmd $ HandShake.toPingCmd cmds
CliPingCommand cmds ->
firstExceptT PingClientError $ runPingCmd cmds
Help pprefs allParserInfo ->
Expand Down
109 changes: 109 additions & 0 deletions cardano-cli/src/Cardano/CLI/Run/HandShake.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@

{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE TypeApplications #-}

{- HLINT ignore "Move brackets to avoid $" -}

module Cardano.CLI.Run.HandShake
carbolymer marked this conversation as resolved.
Show resolved Hide resolved
( HandShakeCmd(..)
, parseHandShakeCmd
, toPingCmd
) where


import Cardano.CLI.Run.Ping (PingCmd (..))
import qualified Cardano.CLI.Run.Ping as Ping
import qualified Cardano.Network.Ping as CNP

import Control.Applicative ((<|>))
import Data.Word (Word32)
import qualified Options.Applicative as Opt
import qualified Prettyprinter as PP

data HandShakeCmd = HandShakeCmd
{ endPoint :: !Ping.EndPoint
Copy link
Contributor

Choose a reason for hiding this comment

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

This shouldn't be a separate command. It should live under the ping command as something like handshake-only.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We've settled for cardano-cli ping --handshake-only here on Slack.

, port :: !String
, magic :: !Word32
, json :: !Bool
, quiet :: !Bool
, query :: !Bool
} deriving (Eq, Show)

toPingCmd :: HandShakeCmd -> PingCmd
toPingCmd (HandShakeCmd {endPoint, port, magic, json, quiet, query}) =
PingCmd {
pingCmdCount = 0
, pingCmdEndPoint = endPoint
, pingCmdPort = port
, pingCmdMagic = magic
, pingCmdJson = json
, pingCmdQuiet = quiet
, pingOptsHandshakeQuery = query
}

parseHandShakeCmd :: Opt.Parser HandShakeCmd
parseHandShakeCmd = Opt.hsubparser $ mconcat
[ Opt.metavar "handshake"
, Opt.command "handshake"
$ Opt.info pHandShake $ Opt.progDescDoc
$ Just $ PP.pretty @String "Negotiates a handshake and prints the negotiated version"
]

pHost :: Opt.Parser String
pHost =
Opt.strOption $ mconcat
[ Opt.long "host"
, Opt.short 'h'
, Opt.metavar "HOST"
, Opt.help "Hostname/IP, e.g. relay.iohk.example."
]

pUnixSocket :: Opt.Parser String
pUnixSocket =
Opt.strOption $ mconcat
[ Opt.long "unixsock"
, Opt.short 'u'
, Opt.metavar "SOCKET"
, Opt.help "Unix socket, e.g. file.socket."
]

pEndPoint :: Opt.Parser Ping.EndPoint
pEndPoint = fmap Ping.HostEndPoint pHost <|> fmap Ping.UnixSockEndPoint pUnixSocket

pHandShake :: Opt.Parser HandShakeCmd
pHandShake = HandShakeCmd
<$> pEndPoint
<*> ( Opt.strOption $ mconcat
[ Opt.long "port"
, Opt.short 'p'
, Opt.metavar "PORT"
, Opt.help "Port number, e.g. 1234."
, Opt.value "3001"
]
)
<*> ( Opt.option Opt.auto $ mconcat
[ Opt.long "magic"
, Opt.short 'm'
, Opt.metavar "MAGIC"
, Opt.help "Network magic."
, Opt.value CNP.mainnetMagic
]
)
<*> ( Opt.switch $ mconcat
[ Opt.long "json"
, Opt.short 'j'
, Opt.help "JSON output flag."
]
)
<*> ( Opt.switch $ mconcat
[ Opt.long "quiet"
, Opt.short 'q'
, Opt.help "Don't print network statistics, only print negotiated version (default) or supported versions (-Q)."
]
)
<*> ( Opt.switch $ mconcat
[ Opt.long "query-versions"
, Opt.short 'Q'
, Opt.help "Instead of printing the negotiated version, print all queried versions."
]
)
33 changes: 12 additions & 21 deletions cardano-cli/src/Cardano/CLI/Run/Ping.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
{- HLINT ignore "Move brackets to avoid $" -}

module Cardano.CLI.Run.Ping
( PingCmd(..)
( EndPoint(..),
PingCmd(..)
, PingClientCmdError(..)
, renderPingClientCmdError
, runPingCmd
Expand All @@ -15,7 +16,6 @@ import Cardano.Api.Pretty

import qualified Cardano.Network.Ping as CNP

import Control.Applicative ((<|>))
import Control.Concurrent.Class.MonadSTM.Strict (StrictTMVar)
import qualified Control.Concurrent.Class.MonadSTM.Strict as STM
import Control.Exception (SomeException)
Expand All @@ -38,6 +38,8 @@ import qualified System.IO as IO

newtype PingClientCmdError = PingClientCmdError [(AddrInfo, SomeException)]

-- | Note that the @ping@ command only supports @HostEndPoint@. It's 'HandShake'
-- that support both @HostEndPoint@ and @UnixSockEndPoint@
data EndPoint = HostEndPoint String | UnixSockEndPoint String deriving (Eq, Show)

maybeHostEndPoint :: EndPoint -> Maybe String
Expand Down Expand Up @@ -130,10 +132,9 @@ renderPingClientCmdError = \case
parsePingCmd :: Opt.Parser PingCmd
parsePingCmd = Opt.hsubparser $ mconcat
[ Opt.metavar "ping"
, Opt.command "ping" $ Opt.info pPing $ Opt.progDescDoc $ Just $ mconcat
[ PP.pretty @String "Ping a cardano node either using node-to-node or node-to-client protocol. "
, PP.pretty @String "It negotiates a handshake and keeps sending keep alive messages."
]
, Opt.command "ping" $ Opt.info pPing $ Opt.progDescDoc $ Just $
PP.pretty @String "Ping a cardano node. It negotiates a handshake and keeps sending keep alive messages."

]

pHost :: Opt.Parser String
Expand All @@ -145,18 +146,6 @@ pHost =
, Opt.help "Hostname/IP, e.g. relay.iohk.example."
]

pUnixSocket :: Opt.Parser String
pUnixSocket =
Opt.strOption $ mconcat
[ Opt.long "unixsock"
, Opt.short 'u'
, Opt.metavar "SOCKET"
, Opt.help "Unix socket, e.g. file.socket."
]

pEndPoint :: Opt.Parser EndPoint
pEndPoint = fmap HostEndPoint pHost <|> fmap UnixSockEndPoint pUnixSocket

pPing :: Opt.Parser PingCmd
pPing = PingCmd
<$> ( Opt.option Opt.auto $ mconcat
Expand All @@ -170,7 +159,7 @@ pPing = PingCmd
, Opt.value maxBound
]
)
<*> pEndPoint
<*> fmap HostEndPoint pHost
<*> ( Opt.strOption $ mconcat
[ Opt.long "port"
, Opt.short 'p'
Expand All @@ -196,12 +185,14 @@ pPing = PingCmd
<*> ( Opt.switch $ mconcat
[ Opt.long "quiet"
, Opt.short 'q'
, Opt.help "Quiet flag, CSV/JSON only output"
, Opt.help "Quiet flag: don't print network statistics."
]
)
-- TODO remove this option at some point (TODO introduced in 8.15)
<*> ( Opt.switch $ mconcat
[ Opt.long "query-versions"
, Opt.hidden
, Opt.short 'Q'
, Opt.help "Query the supported protocol versions using the handshake protocol and terminate the connection."
, Opt.help "Query the supported protocol versions using the handshake protocol and terminate the connection (deprecated, use the \"cardano-cli handshake\" command instead)."
]
)
15 changes: 12 additions & 3 deletions cardano-cli/test/cardano-cli-golden/files/golden/help.cli
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Usage: cardano-cli
| legacy
| Legacy commands
| byron
| handshake
| ping
| version
)
Expand Down Expand Up @@ -10961,15 +10962,23 @@ Usage: cardano-cli byron create-update-proposal

Create an update proposal.

Usage: cardano-cli handshake ((-h|--host HOST) | (-u|--unixsock SOCKET))
[-p|--port PORT]
[-m|--magic MAGIC]
[-j|--json]
[-q|--quiet]
[-Q|--query-versions]

Negotiates a handshake and prints the negotiated version

Usage: cardano-cli ping [-c|--count COUNT]
((-h|--host HOST) | (-u|--unixsock SOCKET))
(-h|--host HOST)
[-p|--port PORT]
[-m|--magic MAGIC]
[-j|--json]
[-q|--quiet]
[-Q|--query-versions]

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

Usage: cardano-cli genesis --genesis-output-dir FILEPATH
--start-time POSIXSECONDS
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Usage: cardano-cli handshake ((-h|--host HOST) | (-u|--unixsock SOCKET))
[-p|--port PORT]
[-m|--magic MAGIC]
[-j|--json]
[-q|--quiet]
[-Q|--query-versions]

Negotiates a handshake and prints the negotiated version

Available options:
-h,--host HOST Hostname/IP, e.g. relay.iohk.example.
-u,--unixsock SOCKET Unix socket, e.g. file.socket.
-p,--port PORT Port number, e.g. 1234.
-m,--magic MAGIC Network magic.
-j,--json JSON output flag.
-q,--quiet Don't print network statistics, only print negotiated
version (default) or supported versions (-Q).
-Q,--query-versions Instead of printing the negotiated version, print all
queried versions.
-h,--help Show this help text
12 changes: 6 additions & 6 deletions cardano-cli/test/cardano-cli-golden/files/golden/help/ping.cli
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
Usage: cardano-cli ping [-c|--count COUNT]
((-h|--host HOST) | (-u|--unixsock SOCKET))
(-h|--host HOST)
[-p|--port PORT]
[-m|--magic MAGIC]
[-j|--json]
[-q|--quiet]
[-Q|--query-versions]

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

Available options:
-c,--count COUNT Stop after sending count requests and receiving count
responses. If this option is not specified, ping will
operate until interrupted.
-h,--host HOST Hostname/IP, e.g. relay.iohk.example.
-u,--unixsock SOCKET Unix socket, e.g. file.socket.
-p,--port PORT Port number, e.g. 1234.
-m,--magic MAGIC Network magic.
-j,--json JSON output flag.
-q,--quiet Quiet flag, CSV/JSON only output
-q,--quiet Quiet flag: don't print network statistics.
-Q,--query-versions Query the supported protocol versions using the
handshake protocol and terminate the connection.
handshake protocol and terminate the connection
(deprecated, use the "cardano-cli handshake" command
instead).
-h,--help Show this help text