Skip to content

Commit

Permalink
CAD-701: CLI parsers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Shevchenko committed Mar 4, 2020
1 parent 2468b73 commit bff1ecd
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 11 deletions.
31 changes: 20 additions & 11 deletions cardano-tx-generator/app/cardano-tx-generator.hs
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
{-# LANGUAGE OverloadedStrings #-}

import Cardano.Prelude hiding (option)
import Prelude (String)

--import Control.Monad.Trans.Except.Extra (runExceptT)
--import qualified Options.Applicative as Opt
--import Options.Applicative (Parser, ParserInfo, ParserPrefs,
-- showHelpOnEmpty)
--import System.Exit (exitFailure)

--import Cardano.CLI.Parsers
--import Cardano.CLI.Run
--import Cardano.Common.TopHandler
import Control.Monad.Trans.Except.Extra (runExceptT)
import qualified Options.Applicative as Opt
import Options.Applicative (ParserInfo, ParserPrefs,
showHelpOnEmpty)
import System.Exit (exitFailure)

import Cardano.Benchmarking.GeneratorTx.CLI.Parsers (GenerateTxs, parseCommand)
import Cardano.Benchmarking.GeneratorTx.CLI.Run (runCommand)

main :: IO ()
main = do
putStrLn ("Hi!" :: String)
generateTxs <- Opt.customExecParser prefs info
result <- runExceptT $ runCommand generateTxs
case result of
Right _ -> pure ()
Left err -> print err >> exitFailure
where
prefs :: ParserPrefs
prefs = Opt.prefs showHelpOnEmpty

info :: ParserInfo GenerateTxs
info = Opt.info
(parseCommand <**> Opt.helper)
(Opt.fullDesc <> Opt.header "cardano-tx-generator - the transaction generator for cardano node.")
2 changes: 2 additions & 0 deletions cardano-tx-generator/cardano-tx-generator.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ library
Cardano.Benchmarking.GeneratorTx.NodeToNode
Cardano.Benchmarking.GeneratorTx.Tx
Cardano.Benchmarking.GeneratorTx.Submission
Cardano.Benchmarking.GeneratorTx.CLI.Parsers
Cardano.Benchmarking.GeneratorTx.CLI.Run

other-modules: Paths_cardano_tx_generator

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
module Cardano.Benchmarking.GeneratorTx.CLI.Parsers
( GenerateTxs (..)
, parseCommand
) where

import Cardano.Prelude hiding (option)
import qualified Data.List.NonEmpty as NE
import Prelude (String)
import Options.Applicative
( Parser
, bashCompleter, completer, help, long, metavar
, auto, option, strOption
)
import qualified Control.Arrow as Arr
import Network.Socket (PortNumber)

import Cardano.Config.Types
( SigningKeyFile(..)
, DelegationCertFile(..)
, GenesisFile(..)
, SocketPath(..)
, NodeAddress(..)
, NodeHostAddress(..)
)
import Cardano.Config.CommonCLI
( parseDelegationCert
, parseGenesisPath
)

import Cardano.Benchmarking.GeneratorTx
( NumberOfTxs(..)
, NumberOfInputsPerTx(..)
, NumberOfOutputsPerTx(..)
, FeePerTx(..)
, TPSRate(..)
, TxAdditionalSize(..)
, ExplorerAPIEnpoint(..)
)

data GenerateTxs =
GenerateTxs FilePath
SigningKeyFile
DelegationCertFile
GenesisFile
SocketPath
(NonEmpty NodeAddress)
NumberOfTxs
NumberOfInputsPerTx
NumberOfOutputsPerTx
FeePerTx
TPSRate
(Maybe TxAdditionalSize)
(Maybe ExplorerAPIEnpoint)
[SigningKeyFile]

parseCommand :: Parser GenerateTxs
parseCommand =
GenerateTxs
<$> parseConfigFile
"config"
"Configuration file for the cardano-node"
<*> parseSigningKeyFile
"signing-key"
"Signing key file."
<*> (DelegationCertFile <$> parseDelegationCert)
<*> (GenesisFile <$> parseGenesisPath)
<*> parseSocketPath
"socket-path"
"Path to a cardano-node socket"
<*> (NE.fromList <$> some (
parseTargetNodeAddress
"target-node"
"host and port of the node transactions will be sent to."
)
)
<*> parseNumberOfTxs
"num-of-txs"
"Number of transactions generator will create."
<*> parseNumberOfInputsPerTx
"inputs-per-tx"
"Number of inputs in each of transactions."
<*> parseNumberOfOutputsPerTx
"outputs-per-tx"
"Number of outputs in each of transactions."
<*> parseFeePerTx
"tx-fee"
"Fee per transaction, in Lovelaces."
<*> parseTPSRate
"tps"
"TPS (transaction per second) rate."
<*> optional (
parseTxAdditionalSize
"add-tx-size"
"Additional size of transaction, in bytes."
)
<*> optional (
parseExplorerAPIEndpoint
"submit-to-api"
"Explorer's API endpoint to submit transaction."
)
<*> parseSigningKeysFiles
"sig-key"
"Path to signing key file, for genesis UTxO using by generator."

----------------------------------------------------------------

parseTargetNodeAddress :: String -> String -> Parser NodeAddress
parseTargetNodeAddress optname desc =
option
( uncurry NodeAddress
. Arr.first parseHostAddress
. Arr.second parsePort
<$> auto
)
$ long optname
<> metavar "(HOST,PORT)"
<> help desc

parseHostAddress :: String -> NodeHostAddress
parseHostAddress = NodeHostAddress . Just .
maybe (panic "Bad host of target node") identity . readMaybe

parsePort :: Word16 -> PortNumber
parsePort = fromIntegral

parseNumberOfTxs :: String -> String -> Parser NumberOfTxs
parseNumberOfTxs opt desc = NumberOfTxs <$> parseIntegral opt desc

parseNumberOfInputsPerTx :: String -> String -> Parser NumberOfInputsPerTx
parseNumberOfInputsPerTx opt desc = NumberOfInputsPerTx <$> parseIntegral opt desc

parseNumberOfOutputsPerTx :: String -> String -> Parser NumberOfOutputsPerTx
parseNumberOfOutputsPerTx opt desc = NumberOfOutputsPerTx <$> parseIntegral opt desc

parseFeePerTx :: String -> String -> Parser FeePerTx
parseFeePerTx opt desc = FeePerTx <$> parseIntegral opt desc

parseTPSRate :: String -> String -> Parser TPSRate
parseTPSRate opt desc = TPSRate <$> parseIntegral opt desc

parseTxAdditionalSize :: String -> String -> Parser TxAdditionalSize
parseTxAdditionalSize opt desc = TxAdditionalSize <$> parseIntegral opt desc

parseExplorerAPIEndpoint :: String -> String -> Parser ExplorerAPIEnpoint
parseExplorerAPIEndpoint opt desc = ExplorerAPIEnpoint <$> parseUrl opt desc

parseSigningKeyFile :: String -> String -> Parser SigningKeyFile
parseSigningKeyFile opt desc = SigningKeyFile <$> parseFilePath opt desc

parseSigningKeysFiles :: String -> String -> Parser [SigningKeyFile]
parseSigningKeysFiles opt desc = some $ SigningKeyFile <$> parseFilePath opt desc

------------------------------------------------------------------

parseIntegral :: Integral a => String -> String -> Parser a
parseIntegral optname desc = option (fromInteger <$> auto)
$ long optname <> metavar "INT" <> help desc

parseUrl :: String -> String -> Parser String
parseUrl optname desc =
strOption $ long optname <> metavar "URL" <> help desc

parseFilePath :: String -> String -> Parser FilePath
parseFilePath optname desc =
strOption
$ long optname
<> metavar "FILEPATH"
<> help desc
<> completer (bashCompleter "file")

parseSocketPath :: String -> String -> Parser SocketPath
parseSocketPath optname desc =
SocketFile <$> parseFilePath optname desc

parseConfigFile :: String -> String -> Parser FilePath
parseConfigFile = parseFilePath
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module Cardano.Benchmarking.GeneratorTx.CLI.Run
( runCommand
) where

import Prelude (error)
import Cardano.Prelude hiding (option)

import Cardano.Benchmarking.GeneratorTx.CLI.Parsers (GenerateTxs (..))

data CliError = CliError Int deriving Show

runCommand :: GenerateTxs -> ExceptT CliError IO ()
runCommand _ = error ""

{-
runCommand (GenerateTxs
logConfigFp
signingKey
delegCert
genFile
socketFp
targetNodeAddresses
numOfTxs
numOfInsPerTx
numOfOutsPerTx
feePerTx
tps
txAdditionalSize
explorerAPIEndpoint
sigKeysFiles) = withIOManagerE $ \iocp -> do
-- Default update value
let update = Update (ApplicationName "cardano-sl") 1 $ LastKnownBlockVersion 0 2 0
nc <- liftIO $ parseNodeConfigurationFP logConfigFp
-- Logging layer
(loggingLayer, _) <- liftIO $ createLoggingFeatureCLI
(pack $ showVersion version)
NoEnvironment
(Just logConfigFp)
(ncLogMetrics nc)
genHash <- getGenesisHash genFile
firstExceptT
GenerateTxsError
$ withRealPBFT
genHash
genFile
(ncReqNetworkMagic nc)
Nothing
(Just delegCert)
(Just signingKey)
update
(ncProtocol nc) $ \protocol@(Consensus.ProtocolRealPBFT _ _ _ _ _) ->
firstExceptT GenesisBenchmarkRunnerError
$ genesisBenchmarkRunner
loggingLayer
iocp
socketFp
protocol
targetNodeAddresses
numOfTxs
numOfInsPerTx
numOfOutsPerTx
feePerTx
tps
txAdditionalSize
explorerAPIEndpoint
[fp | SigningKeyFile fp <- sigKeysFiles]
-}

0 comments on commit bff1ecd

Please sign in to comment.