From cff25f73e7c19c995d61dd50c2de4c05c1d23ea0 Mon Sep 17 00:00:00 2001 From: John Ky Date: Mon, 3 Apr 2023 15:34:20 +1000 Subject: [PATCH] Move pConsensusModeParams to re-usable location --- cardano-cli/src/Cardano/CLI/Common/Parsers.hs | 60 ++++++++++++++++++- cardano-cli/src/Cardano/CLI/Parsers.hs | 3 +- .../src/Cardano/CLI/Shelley/Parsers.hs | 40 +------------ .../src/Cardano/TxSubmit/CLI/Parsers.hs | 59 ++---------------- 4 files changed, 66 insertions(+), 96 deletions(-) diff --git a/cardano-cli/src/Cardano/CLI/Common/Parsers.hs b/cardano-cli/src/Cardano/CLI/Common/Parsers.hs index 219a8ada402..ffe047f17bf 100644 --- a/cardano-cli/src/Cardano/CLI/Common/Parsers.hs +++ b/cardano-cli/src/Cardano/CLI/Common/Parsers.hs @@ -1,10 +1,13 @@ module Cardano.CLI.Common.Parsers ( pNetworkId + , pConsensusModeParams ) where -import Cardano.Api (NetworkId (..), NetworkMagic (..), bounded) +import Cardano.Api (AnyConsensusModeParams (..), ConsensusModeParams (..), + EpochSlots (..), NetworkId (..), NetworkMagic (..), bounded) import Data.Foldable +import Data.Word (Word64) import Options.Applicative (Parser) import qualified Options.Applicative as Opt @@ -20,3 +23,58 @@ pNetworkId = asum , Opt.help "Specify a testnet magic id." ] ] + +pConsensusModeParams :: Parser AnyConsensusModeParams +pConsensusModeParams = asum + [ pShelleyMode *> pShelleyConsensusMode + , pByronMode *> pByronConsensusMode + , pCardanoMode *> pCardanoConsensusMode + , pDefaultConsensusMode + ] + where + pShelleyMode :: Parser () + pShelleyMode = + Opt.flag' () $ mconcat + [ Opt.long "shelley-mode" + , Opt.help "For talking to a node running in Shelley-only mode." + ] + + pByronMode :: Parser () + pByronMode = + Opt.flag' () $ mconcat + [ Opt.long "byron-mode" + , Opt.help "For talking to a node running in Byron-only mode." + ] + + pCardanoMode :: Parser () + pCardanoMode = + Opt.flag' () $ mconcat + [ Opt.long "cardano-mode" + , Opt.help "For talking to a node running in full Cardano mode (default)." + ] + + pCardanoConsensusMode :: Parser AnyConsensusModeParams + pCardanoConsensusMode = AnyConsensusModeParams . CardanoModeParams <$> pEpochSlots + + pByronConsensusMode :: Parser AnyConsensusModeParams + pByronConsensusMode = AnyConsensusModeParams . ByronModeParams <$> pEpochSlots + + pShelleyConsensusMode :: Parser AnyConsensusModeParams + pShelleyConsensusMode = pure (AnyConsensusModeParams ShelleyModeParams) + + pDefaultConsensusMode :: Parser AnyConsensusModeParams + pDefaultConsensusMode = + pure . AnyConsensusModeParams . CardanoModeParams $ EpochSlots defaultByronEpochSlots + +defaultByronEpochSlots :: Word64 +defaultByronEpochSlots = 21600 + +pEpochSlots :: Parser EpochSlots +pEpochSlots = + fmap EpochSlots $ Opt.option (bounded "SLOTS") $ mconcat + [ Opt.long "epoch-slots" + , Opt.metavar "SLOTS" + , Opt.help "The number of slots per epoch for the Byron era." + , Opt.value defaultByronEpochSlots -- Default to the mainnet value. + , Opt.showDefault + ] diff --git a/cardano-cli/src/Cardano/CLI/Parsers.hs b/cardano-cli/src/Cardano/CLI/Parsers.hs index a3ba4b5e077..67dc394e112 100644 --- a/cardano-cli/src/Cardano/CLI/Parsers.hs +++ b/cardano-cli/src/Cardano/CLI/Parsers.hs @@ -6,11 +6,12 @@ module Cardano.CLI.Parsers ( opts , pref + , pConsensusModeParams , pNetworkId ) where import Cardano.CLI.Byron.Parsers (backwardsCompatibilityCommands, parseByronCommands) -import Cardano.CLI.Common.Parsers (pNetworkId) +import Cardano.CLI.Common.Parsers (pConsensusModeParams, pNetworkId) import Cardano.CLI.Ping (parsePingCmd) import Cardano.CLI.Render (customRenderHelp) import Cardano.CLI.Run (ClientCommand (..)) diff --git a/cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs b/cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs index 98131821c96..fdeea267a41 100644 --- a/cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs +++ b/cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs @@ -58,7 +58,7 @@ import Cardano.Api.Shelley import Cardano.Chain.Common (BlockCount (BlockCount)) -import Cardano.CLI.Common.Parsers (pNetworkId) +import Cardano.CLI.Common.Parsers (pConsensusModeParams, pNetworkId) import Cardano.CLI.Shelley.Commands import Cardano.CLI.Shelley.Key (PaymentVerifier (..), StakeVerifier (..), VerificationKeyOrFile (..), VerificationKeyOrHashOrFile (..), @@ -3200,44 +3200,6 @@ pMaxCollateralInputs = ] ] -pConsensusModeParams :: Parser AnyConsensusModeParams -pConsensusModeParams = asum - [ Opt.flag' (AnyConsensusModeParams ShelleyModeParams) - ( Opt.long "shelley-mode" - <> Opt.help "For talking to a node running in Shelley-only mode." - ) - , Opt.flag' () - ( Opt.long "byron-mode" - <> Opt.help "For talking to a node running in Byron-only mode." - ) - *> pByronConsensusMode - , Opt.flag' () - ( Opt.long "cardano-mode" - <> Opt.help "For talking to a node running in full Cardano mode (default)." - ) - *> pCardanoConsensusMode - , -- Default to the Cardano consensus mode. - pure . AnyConsensusModeParams . CardanoModeParams $ EpochSlots defaultByronEpochSlots - ] - where - pCardanoConsensusMode :: Parser AnyConsensusModeParams - pCardanoConsensusMode = AnyConsensusModeParams . CardanoModeParams <$> pEpochSlots - pByronConsensusMode :: Parser AnyConsensusModeParams - pByronConsensusMode = AnyConsensusModeParams . ByronModeParams <$> pEpochSlots - -defaultByronEpochSlots :: Word64 -defaultByronEpochSlots = 21600 - -pEpochSlots :: Parser EpochSlots -pEpochSlots = - fmap EpochSlots $ Opt.option (bounded "SLOTS") $ mconcat - [ Opt.long "epoch-slots" - , Opt.metavar "SLOTS" - , Opt.help "The number of slots per epoch for the Byron era." - , Opt.value defaultByronEpochSlots -- Default to the mainnet value. - , Opt.showDefault - ] - pProtocolVersion :: Parser (Natural, Natural) pProtocolVersion = (,) <$> pProtocolMajorVersion <*> pProtocolMinorVersion diff --git a/cardano-submit-api/src/Cardano/TxSubmit/CLI/Parsers.hs b/cardano-submit-api/src/Cardano/TxSubmit/CLI/Parsers.hs index f294d5cf43c..112ac3b6592 100644 --- a/cardano-submit-api/src/Cardano/TxSubmit/CLI/Parsers.hs +++ b/cardano-submit-api/src/Cardano/TxSubmit/CLI/Parsers.hs @@ -4,20 +4,17 @@ module Cardano.TxSubmit.CLI.Parsers ( opts , pTxSubmitNodeParams , pConfigFile - , pProtocol , pSocketPath ) where -import Cardano.Api (AnyConsensusModeParams (..), ConsensusModeParams (..), - EpochSlots (..), SocketPath (..)) +import Cardano.Api (SocketPath (..)) -import Cardano.CLI.Parsers (pNetworkId) +import Cardano.CLI.Parsers (pConsensusModeParams, pNetworkId) import Cardano.TxSubmit.CLI.Types (ConfigFile (..), TxSubmitNodeParams (..)) import Cardano.TxSubmit.Rest.Parsers (pWebserverConfig) -import Control.Applicative (Alternative (..), (<**>)) -import Data.Word (Word64) +import Control.Applicative ((<**>)) import Options.Applicative (Parser, ParserInfo) import qualified Options.Applicative as Opt @@ -31,7 +28,7 @@ opts = Opt.info (pTxSubmitNodeParams <**> Opt.helper) pTxSubmitNodeParams :: Parser TxSubmitNodeParams pTxSubmitNodeParams = TxSubmitNodeParams <$> pConfigFile - <*> pProtocol + <*> pConsensusModeParams <*> pNetworkId <*> pSocketPath <*> pWebserverConfig 8090 @@ -45,54 +42,6 @@ pConfigFile = ConfigFile <$> Opt.strOption <> Opt.metavar "FILEPATH" ) --- TODO: This was ripped from `cardano-cli` because, unfortunately, it's not --- exported. Once we export this parser from the appropriate module and update --- our `cardano-cli` dependency, we should remove this and import the parser --- from there. -pProtocol :: Parser AnyConsensusModeParams -pProtocol = - ( Opt.flag' () - ( Opt.long "shelley-mode" - <> Opt.help "For talking to a node running in Shelley-only mode." - ) - *> pShelley - ) - <|> ( Opt.flag' () - ( Opt.long "byron-mode" - <> Opt.help "For talking to a node running in Byron-only mode." - ) - *> pByron - ) - <|> ( Opt.flag' () - ( Opt.long "cardano-mode" - <> Opt.help "For talking to a node running in full Cardano mode (default)." - ) - *> pCardano - ) - <|> -- Default to the Cardano protocol. - pure (AnyConsensusModeParams (CardanoModeParams (EpochSlots defaultByronEpochSlots))) - where - pByron :: Parser AnyConsensusModeParams - pByron = AnyConsensusModeParams . ByronModeParams <$> pEpochSlots - - pShelley :: Parser AnyConsensusModeParams - pShelley = pure (AnyConsensusModeParams ShelleyModeParams) - - pCardano :: Parser AnyConsensusModeParams - pCardano = AnyConsensusModeParams . CardanoModeParams <$> pEpochSlots - - pEpochSlots :: Parser EpochSlots - pEpochSlots = EpochSlots <$> Opt.option Opt.auto - ( Opt.long "epoch-slots" - <> Opt.metavar "NATURAL" - <> Opt.help "The number of slots per epoch for the Byron era." - <> Opt.value defaultByronEpochSlots -- Default to the mainnet value. - <> Opt.showDefault - ) - - defaultByronEpochSlots :: Word64 - defaultByronEpochSlots = 21600 - pSocketPath :: Parser SocketPath pSocketPath = SocketPath <$> Opt.strOption ( Opt.long "socket-path"