Skip to content

Commit

Permalink
Move pConsensusModeParams to re-usable location
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Apr 12, 2023
1 parent 5555f68 commit ce152f6
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 96 deletions.
60 changes: 59 additions & 1 deletion cardano-cli/src/Cardano/CLI/Common/Parsers.hs
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
]
3 changes: 2 additions & 1 deletion cardano-cli/src/Cardano/CLI/Parsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 (..))
Expand Down
40 changes: 1 addition & 39 deletions cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 (..), StakeIdentifier (..),
StakeVerifier (..), VerificationKeyOrFile (..), VerificationKeyOrHashOrFile (..),
Expand Down Expand Up @@ -3209,44 +3209,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
Expand Down
59 changes: 4 additions & 55 deletions cardano-submit-api/src/Cardano/TxSubmit/CLI/Parsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,7 +28,7 @@ opts = Opt.info (pTxSubmitNodeParams <**> Opt.helper)
pTxSubmitNodeParams :: Parser TxSubmitNodeParams
pTxSubmitNodeParams = TxSubmitNodeParams
<$> pConfigFile
<*> pProtocol
<*> pConsensusModeParams
<*> pNetworkId
<*> pSocketPath
<*> pWebserverConfig 8090
Expand All @@ -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"
Expand Down

0 comments on commit ce152f6

Please sign in to comment.