Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cl/testnetdata #595

Merged
merged 8 commits into from
Feb 8, 2024
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Environment.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ getCardanoEra = do
"conway" -> pure $ Just $ AnyCardanoEra ConwayEra
unknown -> error $ "Unknown era: " <> unknown -- TODO improve error handling

Nothing -> pure Nothing
Nothing -> pure Nothing
4 changes: 2 additions & 2 deletions cardano-cli/src/Cardano/CLI/EraBased/Commands/Genesis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ data GenesisCreateTestNetDataCmdArgs = GenesisCreateTestNetDataCmdArgs
, numUtxoKeys :: !Word -- ^ The number of UTxO credentials to create and write to disk.
, totalSupply :: !(Maybe Lovelace) -- ^ The total number of Lovelace
, delegatedSupply :: !(Maybe Lovelace) -- ^ The number of Lovelace being delegated
, networkId :: !NetworkId -- ^ The network ID to use.
, networkId :: !(Maybe NetworkId) -- ^ The network ID to use. Overrides the network id supplied in the spec file.
, systemStart :: !(Maybe SystemStart) -- ^ The genesis start time.
, outputDir :: !FilePath -- ^ Directory where to write credentials and files.
} deriving Show
Expand Down Expand Up @@ -156,4 +156,4 @@ renderGenesisCmds = \case
GenesisAddr {} ->
"genesis initial-addr"
GenesisHashFile {} ->
"genesis hash"
"genesis hash"
19 changes: 13 additions & 6 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Genesis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pGenesisCmds envCli =
]
, Just
$ subParser "create-testnet-data"
$ Opt.info (pGenesisCreateTestNetData envCli)
$ Opt.info pGenesisCreateTestNetData
$ Opt.progDesc
$ mconcat
[ "Create data to use for starting a testnet."
Expand Down Expand Up @@ -199,8 +199,8 @@ pGenesisCreateStaked envCli =
<*> pStuffedUtxoCount
<*> Opt.optional pRelayJsonFp

pGenesisCreateTestNetData :: EnvCli -> Parser (GenesisCmds era)
pGenesisCreateTestNetData envCli =
pGenesisCreateTestNetData :: Parser (GenesisCmds era)
CarlosLopezDeLara marked this conversation as resolved.
Show resolved Hide resolved
pGenesisCreateTestNetData =
fmap GenesisCreateTestNetData $ GenesisCreateTestNetDataCmdArgs
<$> (optional $ pSpecFile "shelley")
<*> pNumGenesisKeys
Expand All @@ -211,10 +211,18 @@ pGenesisCreateTestNetData envCli =
<*> pNumUtxoKeys
<*> pSupply
<*> pSupplyDelegated
<*> pNetworkId envCli
<*> optional pNetworkIdForTestnetData
<*> pMaybeSystemStart
<*> pOutputDir
where
pNetworkIdForTestnetData :: Parser NetworkId
pNetworkIdForTestnetData = Testnet . NetworkMagic <$> testnetMagicParser
where
testnetMagicParser = Opt.option (bounded "TESTNET_MAGIC")
( Opt.long "testnet-magic"
<> Opt.metavar "NATURAL"
<> Opt.help "Specify a testnet magic id for the cluster. Overrides the network id supplied in the spec file."
)
pSpecFile era = Opt.strOption $ mconcat
[ Opt.long $ "spec-" <> era
, Opt.metavar "FILE"
Expand Down Expand Up @@ -409,7 +417,6 @@ pSlotLength =
, Opt.value 1_000
]


pSlotCoefficient :: Parser Rational
pSlotCoefficient =
Opt.option readRationalUnitInterval $ mconcat
Expand All @@ -435,4 +442,4 @@ pBulkPoolsPerFile =
, Opt.metavar "INT"
, Opt.help "Each bulk pool to contain this many pool credential sets [default is 0]."
, Opt.value 0
]
]
28 changes: 17 additions & 11 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/CreateTestnetData.hs
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,21 @@ runGenesisCreateTestNetDataCmd Cmd.GenesisCreateTestNetDataCmdArgs
, outputDir }
= do
liftIO $ createDirectoryIfMissing False outputDir
shelleyGenesis <-
shelleyGenesisInit <-
case specShelley of
Just shelleyPath ->
newExceptT $ readAndDecodeShelleyGenesis shelleyPath
Nothing -> do
Nothing ->
-- No template given: a default file is created
pure $ shelleyGenesisDefaults { sgNetworkMagic = unNetworkMagic (toNetworkMagic networkId) }

let -- {0 -> genesis-keys/genesis0/key.vkey, 1 -> genesis-keys/genesis1/key.vkey, ...}
pure shelleyGenesisDefaults

-- Read NetworkId either from file or from the flag. Flag overrides template file.
let actualNetworkId =
CarlosLopezDeLara marked this conversation as resolved.
Show resolved Hide resolved
case networkId of
Just networkFromFlag -> networkFromFlag
Nothing -> fromNetworkMagic (NetworkMagic $ sgNetworkMagic shelleyGenesisInit)
shelleyGenesis = shelleyGenesisInit { sgNetworkMagic = unNetworkMagic (toNetworkMagic actualNetworkId) }
-- {0 -> genesis-keys/genesis0/key.vkey, 1 -> genesis-keys/genesis1/key.vkey, ...}
genesisVKeysPaths = mkPaths numGenesisKeys genesisDir "genesis" "key.vkey"
-- {0 -> delegate-keys/delegate0/key.vkey, 1 -> delegate-keys/delegate1/key.vkey, ...}
delegateKeys = mkPaths numGenesisKeys delegateDir "delegate" "key.vkey"
Expand Down Expand Up @@ -237,7 +243,7 @@ runGenesisCreateTestNetDataCmd Cmd.GenesisCreateTestNetDataCmdArgs
let poolDir = poolsDir </> ("pool" <> show index)

createPoolCredentials desiredKeyOutputFormat poolDir
buildPoolParams networkId poolDir Nothing (fromMaybe mempty mayStakePoolRelays)
buildPoolParams actualNetworkId poolDir Nothing (fromMaybe mempty mayStakePoolRelays)

when (0 < numPools) $ writeREADME poolsDir poolsREADME

Expand Down Expand Up @@ -283,15 +289,15 @@ runGenesisCreateTestNetDataCmd Cmd.GenesisCreateTestNetDataCmdArgs
-- We don't need to be attentive to laziness here, because anyway this
-- doesn't scale really well (because we're generating legit credentials,
-- as opposed to the Transient case).
zipWithM (computeDelegation networkId) delegates distribution
zipWithM (computeDelegation actualNetworkId) delegates distribution
Transient _ ->
liftIO $ Lazy.forStateM g distribution $ flip computeInsecureDelegation networkId
liftIO $ Lazy.forStateM g distribution $ flip computeInsecureDelegation actualNetworkId

genDlgs <- readGenDelegsMap genesisVKeysPaths delegateKeys delegateVrfKeys
nonDelegAddrs <- readInitialFundAddresses utxoKeys networkId
nonDelegAddrs <- readInitialFundAddresses utxoKeys actualNetworkId
start <- maybe (SystemStart <$> getCurrentTimePlus30) pure systemStart

let network = toShelleyNetwork networkId
let network = toShelleyNetwork actualNetworkId
stuffedUtxoAddrs <- liftIO $ Lazy.replicateM (fromIntegral numStuffedUtxo) $ genStuffedAddress network

let stake = second Ledger.ppId . mkDelegationMapEntry <$> delegations
Expand Down Expand Up @@ -723,4 +729,4 @@ readInitialFundAddresses utxoKeys nw = do
, let vkh = verificationKeyHash (castVerificationKey vkey)
addr = makeShelleyAddressInEra ShelleyBasedEraShelley nw (PaymentCredentialByKey vkh)
NoStakeAddress
]
]
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import qualified Hedgehog.Extras.Test.Golden as H
{- HLINT ignore "Use camelCase" -}

networkMagic :: Word32
networkMagic = 42
networkMagic = 623

numDReps :: Int
numDReps = 5
Expand Down Expand Up @@ -62,16 +62,33 @@ tree root = do
subTrees <- mapM tree subs
return $ files ++ concat subTrees

-- | This test tests the non-transient case, i.e. it maximizes the files
-- that can be written to disk. Execute this test with:
-- @cabal test cardano-cli-golden --test-options '-p "/golden create testnet data/'@
-- Execute this test with:
-- @cabal test cardano-cli-golden --test-options '-p "/golden create testnet data/"'@
hprop_golden_create_testnet_data :: Property
hprop_golden_create_testnet_data =
golden_create_testnet_data Nothing

-- Execute this test with:
-- @cabal test cardano-cli-golden --test-options '-p "/golden create testnet data with template/"'@
hprop_golden_create_testnet_data_with_template :: Property
hprop_golden_create_testnet_data_with_template =
golden_create_testnet_data $ Just "test/cardano-cli-golden/files/input/shelley/genesis/genesis.spec.json"

-- | This test tests the non-transient case, i.e. it maximizes the files
-- that can be written to disk.
golden_create_testnet_data :: ()
=> Maybe FilePath -- ^ The path to the shelley template use, if any
-> Property
golden_create_testnet_data mShelleyTemplate =
propertyOnce $ moduleWorkspace "tmp" $ \tempDir -> do

let outputDir = tempDir </> "out"
templateArg :: [String] =
case mShelleyTemplate of
Nothing -> []
Just shelleyTemplate -> ["--spec-shelley", shelleyTemplate]

void $ execCardanoCLI $ mkArguments outputDir <> ["--stake-delegators", "4"]
void $ execCardanoCLI $ mkArguments outputDir <> ["--stake-delegators", "4"] <> templateArg

generated <- liftIO $ tree outputDir
-- Sort output for stability, and make relative to avoid storing
Expand Down
28 changes: 7 additions & 21 deletions cardano-cli/test/cardano-cli-golden/files/golden/help.cli
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,7 @@ Usage: cardano-cli shelley genesis create-testnet-data [--spec-shelley FILE]
[--utxo-keys INT]
[--total-supply LOVELACE]
[--delegated-supply LOVELACE]
( --mainnet
| --testnet-magic NATURAL
)
[--testnet-magic NATURAL]
[--start-time UTC-TIME]
--out-dir DIR

Expand Down Expand Up @@ -1429,9 +1427,7 @@ Usage: cardano-cli allegra genesis create-testnet-data [--spec-shelley FILE]
[--utxo-keys INT]
[--total-supply LOVELACE]
[--delegated-supply LOVELACE]
( --mainnet
| --testnet-magic NATURAL
)
[--testnet-magic NATURAL]
[--start-time UTC-TIME]
--out-dir DIR

Expand Down Expand Up @@ -2586,9 +2582,7 @@ Usage: cardano-cli mary genesis create-testnet-data [--spec-shelley FILE]
[--utxo-keys INT]
[--total-supply LOVELACE]
[--delegated-supply LOVELACE]
( --mainnet
| --testnet-magic NATURAL
)
[--testnet-magic NATURAL]
[--start-time UTC-TIME]
--out-dir DIR

Expand Down Expand Up @@ -3727,9 +3721,7 @@ Usage: cardano-cli alonzo genesis create-testnet-data [--spec-shelley FILE]
[--utxo-keys INT]
[--total-supply LOVELACE]
[--delegated-supply LOVELACE]
( --mainnet
| --testnet-magic NATURAL
)
[--testnet-magic NATURAL]
[--start-time UTC-TIME]
--out-dir DIR

Expand Down Expand Up @@ -4892,9 +4884,7 @@ Usage: cardano-cli babbage genesis create-testnet-data [--spec-shelley FILE]
[--utxo-keys INT]
[--total-supply LOVELACE]
[--delegated-supply LOVELACE]
( --mainnet
| --testnet-magic NATURAL
)
[--testnet-magic NATURAL]
[--start-time UTC-TIME]
--out-dir DIR

Expand Down Expand Up @@ -6075,9 +6065,7 @@ Usage: cardano-cli conway genesis create-testnet-data [--spec-shelley FILE]
[--utxo-keys INT]
[--total-supply LOVELACE]
[--delegated-supply LOVELACE]
( --mainnet
| --testnet-magic NATURAL
)
[--testnet-magic NATURAL]
[--start-time UTC-TIME]
--out-dir DIR

Expand Down Expand Up @@ -7628,9 +7616,7 @@ Usage: cardano-cli latest genesis create-testnet-data [--spec-shelley FILE]
[--utxo-keys INT]
[--total-supply LOVELACE]
[--delegated-supply LOVELACE]
( --mainnet
| --testnet-magic NATURAL
)
[--testnet-magic NATURAL]
[--start-time UTC-TIME]
--out-dir DIR

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ Usage: cardano-cli allegra genesis create-testnet-data [--spec-shelley FILE]
[--utxo-keys INT]
[--total-supply LOVELACE]
[--delegated-supply LOVELACE]
( --mainnet
| --testnet-magic NATURAL
)
[--testnet-magic NATURAL]
[--start-time UTC-TIME]
--out-dir DIR

Expand Down Expand Up @@ -44,10 +42,8 @@ Available options:
delegated. Defaults to 500 000 Ada (i.e. (10^12) / 2
Lovelace). Cannot be more than the amount specified
with --total-supply.
--mainnet Use the mainnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id for the cluster. Overrides
the network id supplied in the spec file.
--start-time UTC-TIME The genesis start time in YYYY-MM-DDThh:mm:ssZ
format. If unspecified, will be the current time +30
seconds.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ Usage: cardano-cli alonzo genesis create-testnet-data [--spec-shelley FILE]
[--utxo-keys INT]
[--total-supply LOVELACE]
[--delegated-supply LOVELACE]
( --mainnet
| --testnet-magic NATURAL
)
[--testnet-magic NATURAL]
[--start-time UTC-TIME]
--out-dir DIR

Expand Down Expand Up @@ -44,10 +42,8 @@ Available options:
delegated. Defaults to 500 000 Ada (i.e. (10^12) / 2
Lovelace). Cannot be more than the amount specified
with --total-supply.
--mainnet Use the mainnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id for the cluster. Overrides
the network id supplied in the spec file.
--start-time UTC-TIME The genesis start time in YYYY-MM-DDThh:mm:ssZ
format. If unspecified, will be the current time +30
seconds.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ Usage: cardano-cli babbage genesis create-testnet-data [--spec-shelley FILE]
[--utxo-keys INT]
[--total-supply LOVELACE]
[--delegated-supply LOVELACE]
( --mainnet
| --testnet-magic NATURAL
)
[--testnet-magic NATURAL]
[--start-time UTC-TIME]
--out-dir DIR

Expand Down Expand Up @@ -44,10 +42,8 @@ Available options:
delegated. Defaults to 500 000 Ada (i.e. (10^12) / 2
Lovelace). Cannot be more than the amount specified
with --total-supply.
--mainnet Use the mainnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id for the cluster. Overrides
the network id supplied in the spec file.
--start-time UTC-TIME The genesis start time in YYYY-MM-DDThh:mm:ssZ
format. If unspecified, will be the current time +30
seconds.
Expand Down
Loading
Loading