diff --git a/cardano-tx-generator/app/cardano-tx-generator.hs b/cardano-tx-generator/app/cardano-tx-generator.hs index 16f9e6dc6fc..e1d68fc3dd4 100644 --- a/cardano-tx-generator/app/cardano-tx-generator.hs +++ b/cardano-tx-generator/app/cardano-tx-generator.hs @@ -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.") diff --git a/cardano-tx-generator/cardano-tx-generator.cabal b/cardano-tx-generator/cardano-tx-generator.cabal index 857235afa83..14f01126c55 100644 --- a/cardano-tx-generator/cardano-tx-generator.cabal +++ b/cardano-tx-generator/cardano-tx-generator.cabal @@ -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 diff --git a/cardano-tx-generator/src/Cardano/Benchmarking/GeneratorTx/CLI/Parsers.hs b/cardano-tx-generator/src/Cardano/Benchmarking/GeneratorTx/CLI/Parsers.hs new file mode 100644 index 00000000000..f21f55d51f1 --- /dev/null +++ b/cardano-tx-generator/src/Cardano/Benchmarking/GeneratorTx/CLI/Parsers.hs @@ -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 diff --git a/cardano-tx-generator/src/Cardano/Benchmarking/GeneratorTx/CLI/Run.hs b/cardano-tx-generator/src/Cardano/Benchmarking/GeneratorTx/CLI/Run.hs new file mode 100644 index 00000000000..9798fb33398 --- /dev/null +++ b/cardano-tx-generator/src/Cardano/Benchmarking/GeneratorTx/CLI/Run.hs @@ -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] +-}