Skip to content

Commit

Permalink
Merge pull request #5976 from IntersectMBO/smelc/separate-cardano-tes…
Browse files Browse the repository at this point in the history
…tnet-options-stages

cardano-testnet: separate in types the options encoded in genesis files, from other options
  • Loading branch information
smelc authored Sep 13, 2024
2 parents 82dfb84 + a25c6c1 commit e6a9c24
Show file tree
Hide file tree
Showing 42 changed files with 379 additions and 323 deletions.
1 change: 1 addition & 0 deletions cardano-node-chairman/cardano-node-chairman.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ test-suite chairman-tests

build-depends: cardano-testnet
, cardano-crypto-class ^>= 2.1.2
, data-default-class
, filepath
, hedgehog
, hedgehog-extras ^>= 0.6.4
Expand Down
5 changes: 3 additions & 2 deletions cardano-node-chairman/test/Spec/Chairman/Cardano.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

module Spec.Chairman.Cardano where

import Cardano.Testnet (NodeRuntime (nodeName), allNodes, cardanoDefaultTestnetOptions,
import Cardano.Testnet (NodeRuntime (nodeName), allNodes,
cardanoTestnetDefault, mkConf)

import Data.Default.Class
import Testnet.Property.Util (integrationRetryWorkspace)

import qualified Hedgehog as H
Expand All @@ -18,6 +19,6 @@ hprop_chairman :: H.Property
hprop_chairman = integrationRetryWorkspace 2 "cardano-chairman" $ \tempAbsPath' -> do
conf <- mkConf tempAbsPath'

allNodes' <- fmap nodeName . allNodes <$> cardanoTestnetDefault cardanoDefaultTestnetOptions conf
allNodes' <- fmap nodeName . allNodes <$> cardanoTestnetDefault def def conf

chairmanOver 120 50 conf allNodes'
1 change: 1 addition & 0 deletions cardano-testnet/cardano-testnet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ test-suite cardano-testnet-test
, cardano-strict-containers ^>= 0.1
, cardano-testnet
, containers
, data-default-class
, directory
, exceptions
, filepath
Expand Down
1 change: 0 additions & 1 deletion cardano-testnet/src/Cardano/Testnet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ module Cardano.Testnet (
-- ** Testnet options
CardanoTestnetOptions(..),
TestnetNodeOptions(..),
cardanoDefaultTestnetOptions,
cardanoDefaultTestnetNodeOptions,
getDefaultAlonzoGenesis,
getDefaultShelleyGenesis,
Expand Down
77 changes: 46 additions & 31 deletions cardano-testnet/src/Parsers/Cardano.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Cardano.CLI.EraBased.Options.Common hiding (pNetworkId)

import Prelude

import Data.Default.Class
import Data.Functor
import qualified Data.List as L
import Data.Word (Word64)
Expand All @@ -20,46 +21,29 @@ import Testnet.Start.Types
import Testnet.Types (readNodeLoggingFormat)


optsTestnet :: EnvCli -> Parser CardanoTestnetOptions
optsTestnet envCli = CardanoTestnetOptions
optsTestnet :: EnvCli -> Parser CardanoTestnetCliOptions
optsTestnet envCli = CardanoTestnetCliOptions
<$> pCardanoTestnetCliOptions envCli
<*> pShelleyTestnetOptions

pCardanoTestnetCliOptions :: EnvCli -> Parser CardanoTestnetOptions
pCardanoTestnetCliOptions envCli = CardanoTestnetOptions
<$> pNumSpoNodes
<*> pAnyShelleyBasedEra'
<*> OA.option auto
( OA.long "epoch-length"
<> OA.help "Epoch length, in number of slots"
<> OA.metavar "SLOTS"
<> OA.showDefault
<> OA.value (cardanoEpochLength cardanoDefaultTestnetOptions)
)
<*> OA.option auto
( OA.long "slot-length"
<> OA.help "Slot length"
<> OA.metavar "SECONDS"
<> OA.showDefault
<> OA.value (cardanoSlotLength cardanoDefaultTestnetOptions)
)
<*> pNetworkId
<*> OA.option auto
( OA.long "active-slots-coeff"
<> OA.help "Active slots co-efficient"
<> OA.metavar "DOUBLE"
<> OA.showDefault
<> OA.value (cardanoActiveSlotsCoeff cardanoDefaultTestnetOptions)
)
<*> pMaxLovelaceSupply
<*> OA.option auto
( OA.long "enable-p2p"
<> OA.help "Enable P2P"
<> OA.metavar "BOOL"
<> OA.showDefault
<> OA.value (cardanoEnableP2P cardanoDefaultTestnetOptions)
<> OA.value (cardanoEnableP2P def)
)
<*> OA.option (OA.eitherReader readNodeLoggingFormat)
( OA.long "nodeLoggingFormat"
<> OA.help "Node logging format (json|text)"
<> OA.metavar "LOGGING_FORMAT"
<> OA.showDefault
<> OA.value (cardanoNodeLoggingFormat cardanoDefaultTestnetOptions)
<> OA.value (cardanoNodeLoggingFormat def)
)
<*> OA.option auto
( OA.long "num-dreps"
Expand All @@ -86,10 +70,9 @@ pNumSpoNodes =
<> OA.help "Number of pool nodes. Note this uses a default node configuration for all nodes."
<> OA.metavar "COUNT"
<> OA.showDefault
<> OA.value (cardanoNodes cardanoDefaultTestnetOptions)
<> OA.value (cardanoNodes def)
)


_pSpo :: Parser TestnetNodeOptions
_pSpo =
SpoTestnetNodeOptions . Just
Expand All @@ -113,8 +96,40 @@ parseNodeConfigFile = NodeConfigurationYaml <$>
, "Or use num-pool-nodes to use cardano-testnet's default configuration."
]


cmdCardano :: EnvCli -> Mod CommandFields CardanoTestnetOptions
pShelleyTestnetOptions :: Parser ShelleyTestnetOptions
pShelleyTestnetOptions =
ShelleyTestnetOptions
<$> pNetworkId
<*> pEpochLength
<*> pSlotLength
<*> pActiveSlotCoeffs
where
pEpochLength =
OA.option auto
( OA.long "epoch-length"
<> OA.help "Epoch length, in number of slots"
<> OA.metavar "SLOTS"
<> OA.showDefault
<> OA.value (shelleyEpochLength def)
)
pSlotLength =
OA.option auto
( OA.long "slot-length"
<> OA.help "Slot length"
<> OA.metavar "SECONDS"
<> OA.showDefault
<> OA.value (shelleySlotLength def)
)
pActiveSlotCoeffs =
OA.option auto
( OA.long "active-slots-coeff"
<> OA.help "Active slots co-efficient"
<> OA.metavar "DOUBLE"
<> OA.showDefault
<> OA.value (shelleyActiveSlotsCoeff def)
)

cmdCardano :: EnvCli -> Mod CommandFields CardanoTestnetCliOptions
cmdCardano envCli = command' "cardano" "Start a testnet in any era" (optsTestnet envCli)

pNetworkId :: Parser Int
Expand All @@ -132,6 +147,6 @@ pMaxLovelaceSupply =
<> help "Max lovelace supply that your testnet starts with."
<> metavar "WORD64"
<> showDefault
<> value (cardanoMaxSupply cardanoDefaultTestnetOptions)
<> value (cardanoMaxSupply def)
)

8 changes: 4 additions & 4 deletions cardano-testnet/src/Parsers/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ opts envCli = Opt.info (commands envCli <**> helper) idm
-- by allowing the user to start testnets in any era (excluding Byron)
-- via StartCardanoTestnet
data CardanoTestnetCommands
= StartCardanoTestnet CardanoTestnetOptions
= StartCardanoTestnet CardanoTestnetCliOptions
| GetVersion VersionOptions
| Help ParserPrefs (ParserInfo CardanoTestnetCommands) HelpOptions

Expand All @@ -51,6 +51,6 @@ runTestnetCmd = \case
Help pPrefs pInfo cmdOpts -> runHelpOptions pPrefs pInfo cmdOpts


runCardanoOptions :: CardanoTestnetOptions -> IO ()
runCardanoOptions options =
runTestnet $ cardanoTestnetDefault options
runCardanoOptions :: CardanoTestnetCliOptions -> IO ()
runCardanoOptions (CardanoTestnetCliOptions testnetOptions shelleyOptions) =
runTestnet $ cardanoTestnetDefault testnetOptions shelleyOptions
31 changes: 16 additions & 15 deletions cardano-testnet/src/Testnet/Defaults.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import Data.Text (Text)
import qualified Data.Text as Text
import Data.Time (UTCTime)
import qualified Data.Vector as Vector
import Data.Word (Word64)
import Lens.Micro
import Numeric.Natural
import System.FilePath ((</>))
Expand Down Expand Up @@ -416,31 +417,31 @@ defaultByronProtocolParamsJsonValue =
]

defaultShelleyGenesis
:: UTCTime
-> CardanoTestnetOptions
:: AnyShelleyBasedEra
-> UTCTime
-> Word64
-> ShelleyTestnetOptions
-> Api.ShelleyGenesis StandardCrypto
defaultShelleyGenesis startTime testnetOptions = do
let CardanoTestnetOptions
{ cardanoTestnetMagic = testnetMagic
, cardanoSlotLength = slotLength
, cardanoEpochLength = epochLength
, cardanoMaxSupply = sgMaxLovelaceSupply
, cardanoActiveSlotsCoeff
, cardanoNodeEra
} = testnetOptions
defaultShelleyGenesis asbe startTime maxSupply options = do
let ShelleyTestnetOptions
{ shelleyTestnetMagic = magic
, shelleySlotLength = slotLength
, shelleyEpochLength = epochLength
, shelleyActiveSlotsCoeff
} = options
-- f
activeSlotsCoeff = round (cardanoActiveSlotsCoeff * 100) % 100
activeSlotsCoeff = round (shelleyActiveSlotsCoeff * 100) % 100
-- make security param k satisfy: epochLength = 10 * k / f
-- TODO: find out why this actually degrates network stability - turned off for now
-- securityParam = ceiling $ fromIntegral epochLength * cardanoActiveSlotsCoeff / 10
pVer = eraToProtocolVersion cardanoNodeEra
pVer = eraToProtocolVersion asbe
protocolParams = Api.sgProtocolParams Api.shelleyGenesisDefaults
protocolParamsWithPVer = protocolParams & ppProtocolVersionL' .~ pVer
Api.shelleyGenesisDefaults
{ Api.sgActiveSlotsCoeff = unsafeBoundedRational activeSlotsCoeff
, Api.sgEpochLength = EpochSize $ fromIntegral epochLength
, Api.sgMaxLovelaceSupply
, Api.sgNetworkMagic = fromIntegral testnetMagic
, Api.sgMaxLovelaceSupply = maxSupply
, Api.sgNetworkMagic = fromIntegral magic
, Api.sgProtocolParams = protocolParamsWithPVer
-- using default from shelley genesis k = 2160
-- , Api.sgSecurityParam = securityParam
Expand Down
10 changes: 4 additions & 6 deletions cardano-testnet/src/Testnet/Process/Cli/SPO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,13 @@ createStakeKeyDeregistrationCertificate tempAbsP sbe stakeVerKey deposit outputF
-- | Related documentation: https://github.com/input-output-hk/cardano-node-wiki/blob/main/docs/stake-pool-operations/8_register_stakepool.md
registerSingleSpo
:: (MonadTest m, MonadCatch m, MonadIO m, HasCallStack)
=> Int -- ^ Identifier for stake pool
=> AnyShelleyBasedEra
-> Int -- ^ Identifier for stake pool
-> TmpAbsolutePath
-> NodeConfigFile 'In
-> SocketPath
-> EpochNo -- ^ Termination epoch
-> CardanoTestnetOptions
-> Int -- ^ Testnet magic
-> ExecConfig
-> (TxIn, FilePath, String)
-> m ( String
Expand All @@ -260,10 +261,8 @@ registerSingleSpo
-- 3. FilePath: Stake pool cold verification key
-- 4. FilePath: Stake pool VRF signing key
-- 5. FilePath: Stake pool VRF verification key
registerSingleSpo identifier tap@(TmpAbsolutePath tempAbsPath') nodeConfigFile socketPath termEpoch cTestnetOptions execConfig
registerSingleSpo asbe identifier tap@(TmpAbsolutePath tempAbsPath') nodeConfigFile socketPath termEpoch testnetMag execConfig
(fundingInput, fundingSigninKey, changeAddr) = GHC.withFrozenCallStack $ do
let testnetMag = cardanoTestnetMagic cTestnetOptions

workDir <- H.note tempAbsPath'

-- In order to register a stake pool we need two certificates:
Expand Down Expand Up @@ -322,7 +321,6 @@ registerSingleSpo identifier tap@(TmpAbsolutePath tempAbsPath') nodeConfigFile s

-- 5. Create registration certificate
let poolRegCertFp = spoReqDir </> "registration.cert"
let asbe = cardanoNodeEra cTestnetOptions

-- The pledge, pool cost and pool margin can all be 0
execCli_
Expand Down
8 changes: 3 additions & 5 deletions cardano-testnet/src/Testnet/Property/Assert.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import Data.Type.Equality
import Data.Word (Word8)
import GHC.Stack as GHC

import Testnet.Components.Configuration (NumPools(..), numPools)
import Testnet.Components.Configuration (NumPools(..))
import Testnet.Process.Run
import Testnet.Start.Types

Expand Down Expand Up @@ -84,12 +84,10 @@ assertByDeadlineIOCustom str deadline f = withFrozenCallStack $ do
assertExpectedSposInLedgerState
:: (MonadTest m, MonadCatch m, MonadIO m, HasCallStack)
=> FilePath -- ^ Stake pools query output filepath
-> CardanoTestnetOptions
-> NumPools
-> ExecConfig
-> m ()
assertExpectedSposInLedgerState output tNetOptions execConfig = withFrozenCallStack $ do
let NumPools numExpectedPools = numPools tNetOptions

assertExpectedSposInLedgerState output (NumPools numExpectedPools) execConfig = withFrozenCallStack $ do
void $ execCli' execConfig
[ "query", "stake-pools"
, "--out-file", output
Expand Down
Loading

0 comments on commit e6a9c24

Please sign in to comment.