Skip to content

Commit

Permalink
Merge pull request #595 from IntersectMBO/cl/testnetdata
Browse files Browse the repository at this point in the history
Cl/testnetdata
  • Loading branch information
CarlosLopezDeLara authored Feb 8, 2024
2 parents a71f8ac + 04a5f33 commit 79b264d
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 91 deletions.
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"
15 changes: 15 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3211,6 +3211,21 @@ pTxId l h =
, Opt.help h
]

pNetworkIdForTestnetData :: EnvCli -> Parser NetworkId
pNetworkIdForTestnetData envCli = asum $ mconcat
[ [ fmap (Testnet . NetworkMagic) $ Opt.option (bounded "TESTNET_MAGIC") $ mconcat
[ Opt.long "testnet-magic"
, Opt.metavar "NATURAL"
, Opt.help $ mconcat
[ "Specify a testnet magic id for the cluster. "
, "This overrides both the network magic from the "
, "spec file and CARDANO_NODE_NETWORK_ID environment variable."
]
]
]
, -- Default to the network id specified by the environment variable if it is available.
pure <$> maybeToList (envCliNetworkId envCli)
]

--------------------------------------------------------------------------------
-- Helpers
Expand Down
5 changes: 3 additions & 2 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Genesis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ pGenesisCreateTestNetData envCli =
<*> pNumUtxoKeys
<*> pSupply
<*> pSupplyDelegated
<*> pNetworkId envCli
<*> (optional $ pNetworkIdForTestnetData envCli)
<*> pMaybeSystemStart
<*> pOutputDir
where
Expand Down Expand Up @@ -409,7 +409,6 @@ pSlotLength =
, Opt.value 1_000
]


pSlotCoefficient :: Parser Rational
pSlotCoefficient =
Opt.option readRationalUnitInterval $ mconcat
Expand All @@ -436,3 +435,5 @@ pBulkPoolsPerFile =
, 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 =
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,9 @@ 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. This
overrides both the network magic from the spec file
and CARDANO_NODE_NETWORK_ID environment variable.
--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,9 @@ 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. This
overrides both the network magic from the spec file
and CARDANO_NODE_NETWORK_ID environment variable.
--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,9 @@ 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. This
overrides both the network magic from the spec file
and CARDANO_NODE_NETWORK_ID environment variable.
--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

0 comments on commit 79b264d

Please sign in to comment.