Skip to content

Commit 36161c9

Browse files
authored
Merge pull request #6132 from IntersectMBO/smelc/do-not-use-create-testnet-data-genesis-when-user-provided-one
cardano-testnet: Use user-specified genesis files (if any) instead of the one generated by create-testnet-data
2 parents 0b7fdbc + 516e566 commit 36161c9

File tree

5 files changed

+71
-36
lines changed

5 files changed

+71
-36
lines changed

cardano-testnet/src/Cardano/Testnet.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module Cardano.Testnet (
1313
TestnetNodeOptions(..),
1414
cardanoDefaultTestnetNodeOptions,
1515
getDefaultAlonzoGenesis,
16-
getDefaultShelleyGenesis,
16+
getDefaultGenesisBatch,
1717

1818
-- * Configuration
1919
Conf(..),

cardano-testnet/src/Testnet/Components/Configuration.hs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@ module Testnet.Components.Configuration
1717
, eraToString
1818
) where
1919

20-
import Cardano.Api.Ledger (StandardCrypto)
2120
import Cardano.Api.Shelley hiding (Value, cardanoEra)
2221

2322
import Cardano.Chain.Genesis (GenesisHash (unGenesisHash), readGenesisData)
2423
import qualified Cardano.Crypto.Hash.Blake2b as Crypto
2524
import qualified Cardano.Crypto.Hash.Class as Crypto
26-
import Cardano.Ledger.Alonzo.Genesis (AlonzoGenesis)
27-
import Cardano.Ledger.Conway.Genesis (ConwayGenesis)
2825
import qualified Cardano.Node.Configuration.Topology as NonP2P
2926
import qualified Cardano.Node.Configuration.TopologyP2P as P2P
3027
import Ouroboros.Network.NodeToNode (DiffusionMode (..))
@@ -57,8 +54,8 @@ import System.FilePath.Posix (takeDirectory, (</>))
5754
import Testnet.Defaults
5855
import Testnet.Filepath
5956
import Testnet.Process.Run (execCli_)
60-
import Testnet.Start.Types (NumDReps (..), NumPools (..), anyEraToString,
61-
anyShelleyBasedEraToString, eraToString)
57+
import Testnet.Start.Types (GenesisBatch (..), GenesisOrigin (..), NumDReps (..),
58+
NumPools (..), anyEraToString, anyShelleyBasedEraToString, eraToString)
6259

6360
import Hedgehog
6461
import qualified Hedgehog as H
@@ -121,13 +118,13 @@ createSPOGenesisAndFiles
121118
-> NumDReps -- ^ The number of pools to make
122119
-> Word64 -- ^ The maximum supply
123120
-> AnyShelleyBasedEra -- ^ The era to use
124-
-> ShelleyGenesis StandardCrypto -- ^ The shelley genesis to use.
125-
-> AlonzoGenesis -- ^ The alonzo genesis to use, for example 'getDefaultAlonzoGenesis' from this module.
126-
-> ConwayGenesis StandardCrypto -- ^ The conway genesis to use, for example 'Defaults.defaultConwayGenesis'.
121+
-> GenesisBatch -- ^ The genesis to use, with their origin (whether they ar provided by the user or defaulted by 'cardano-testnet')
127122
-> TmpAbsolutePath
128123
-> m FilePath -- ^ Shelley genesis directory
129-
createSPOGenesisAndFiles nPoolNodes nDelReps maxSupply asbe@(AnyShelleyBasedEra sbe) shelleyGenesis
130-
alonzoGenesis conwayGenesis (TmpAbsolutePath tempAbsPath) = GHC.withFrozenCallStack $ do
124+
createSPOGenesisAndFiles
125+
nPoolNodes nDelReps maxSupply asbe@(AnyShelleyBasedEra sbe)
126+
(GenesisBatch (shelleyGenesis, alonzoGenesis, conwayGenesis, genesisOrigin))
127+
(TmpAbsolutePath tempAbsPath) = GHC.withFrozenCallStack $ do
131128
let inputGenesisShelleyFp = tempAbsPath </> genesisInputFilepath ShelleyEra
132129
inputGenesisAlonzoFp = tempAbsPath </> genesisInputFilepath AlonzoEra
133130
inputGenesisConwayFp = tempAbsPath </> genesisInputFilepath ConwayEra
@@ -179,6 +176,15 @@ createSPOGenesisAndFiles nPoolNodes nDelReps maxSupply asbe@(AnyShelleyBasedEra
179176
, "--out-dir", tempAbsPath
180177
]
181178

179+
-- Overwrite the genesis files created by create-testnet-data with the files
180+
-- specified by the user (if any)
181+
case genesisOrigin of
182+
DefaultedOrigin -> pure ()
183+
UserProvidedOrigin -> do
184+
overwriteCreateTestnetDataGenesis inputGenesisShelleyFp ShelleyEra
185+
overwriteCreateTestnetDataGenesis inputGenesisAlonzoFp AlonzoEra
186+
overwriteCreateTestnetDataGenesis inputGenesisConwayFp ConwayEra
187+
182188
-- Remove the input files. We don't need them anymore, since create-testnet-data wrote new versions.
183189
forM_ [inputGenesisShelleyFp, inputGenesisAlonzoFp, inputGenesisConwayFp] (liftIO . System.removeFile)
184190

@@ -188,6 +194,12 @@ createSPOGenesisAndFiles nPoolNodes nDelReps maxSupply asbe@(AnyShelleyBasedEra
188194
return genesisShelleyDir
189195
where
190196
genesisInputFilepath e = "genesis-input." <> eraToString e <> ".json"
197+
-- Overwrites the genesis file created by create-testnet-data with the one provided by the user
198+
overwriteCreateTestnetDataGenesis ::
199+
Pretty (eon era) => MonadTest m => MonadIO m =>
200+
FilePath -> eon era -> m ()
201+
overwriteCreateTestnetDataGenesis genesisFp e =
202+
H.copyFile genesisFp (tempAbsPath </> eraToString e <> "-genesis.json")
191203

192204
ifaceAddress :: String
193205
ifaceAddress = "127.0.0.1"

cardano-testnet/src/Testnet/Start/Cardano.hs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ module Testnet.Start.Cardano
1919

2020
, cardanoTestnet
2121
, cardanoTestnetDefault
22-
, getDefaultAlonzoGenesis
23-
, getDefaultShelleyGenesis
22+
, getDefaultAlonzoGenesis -- TODO, stop exporting me when Shutdown.hs is refactored to use cardanoTestnet
23+
, getDefaultGenesisBatch
2424
, retryOnAddressInUseError
2525
) where
2626

@@ -29,7 +29,6 @@ import Cardano.Api
2929
import Cardano.Api.Ledger (StandardCrypto)
3030

3131
import Cardano.Ledger.Alonzo.Genesis (AlonzoGenesis)
32-
import Cardano.Ledger.Conway.Genesis (ConwayGenesis)
3332
import Cardano.Node.Configuration.Topology
3433

3534
import Prelude hiding (lines)
@@ -88,6 +87,7 @@ startTimeOffsetSeconds = if OS.isWin32 then 90 else 15
8887
-- | Like 'cardanoTestnet', but using 'GenesisOptions' to obtain
8988
-- the genesis files, instead of passing them directly.
9089
-- See 'cardanoTestnet' for additional documentation.
90+
-- TODO delete me? @cardano-testnet@ plus @getDefaultGenesisBatch@ suffice now.
9191
cardanoTestnetDefault
9292
:: ()
9393
=> HasCallStack
@@ -96,12 +96,8 @@ cardanoTestnetDefault
9696
-> Conf
9797
-> H.Integration TestnetRuntime
9898
cardanoTestnetDefault testnetOptions shelleyOptions conf = do
99-
AnyShelleyBasedEra sbe <- pure cardanoNodeEra
100-
alonzoGenesis <- getDefaultAlonzoGenesis sbe
101-
shelleyGenesis <- getDefaultShelleyGenesis cardanoNodeEra cardanoMaxSupply shelleyOptions
102-
cardanoTestnet testnetOptions conf UserNodeConfigNotSubmitted shelleyGenesis alonzoGenesis Defaults.defaultConwayGenesis
103-
where
104-
CardanoTestnetOptions{cardanoNodeEra, cardanoMaxSupply} = testnetOptions
99+
batch <- getDefaultGenesisBatch testnetOptions shelleyOptions
100+
cardanoTestnet testnetOptions conf UserNodeConfigNotSubmitted batch
105101

106102
-- | An 'AlonzoGenesis' value that is fit to pass to 'cardanoTestnet'
107103
getDefaultAlonzoGenesis :: ()
@@ -125,6 +121,23 @@ getDefaultShelleyGenesis asbe maxSupply opts = do
125121
startTime <- H.noteShow $ DTC.addUTCTime startTimeOffsetSeconds currentTime
126122
return $ Defaults.defaultShelleyGenesis asbe startTime maxSupply opts
127123

124+
-- | Get a 'GenesisBatch' that is fit to pass to 'cardanoTestnet'
125+
getDefaultGenesisBatch
126+
:: ()
127+
=> HasCallStack
128+
=> MonadIO m
129+
=> MonadTest m
130+
=> CardanoTestnetOptions
131+
-> GenesisOptions
132+
-> m GenesisBatch
133+
getDefaultGenesisBatch testnetOptions shelleyOptions = do
134+
AnyShelleyBasedEra sbe <- pure cardanoNodeEra
135+
alonzoGenesis <- getDefaultAlonzoGenesis sbe
136+
shelleyGenesis <- getDefaultShelleyGenesis cardanoNodeEra cardanoMaxSupply shelleyOptions
137+
return (GenesisBatch (shelleyGenesis, alonzoGenesis, Defaults.defaultConwayGenesis, DefaultedOrigin))
138+
where
139+
CardanoTestnetOptions{cardanoNodeEra, cardanoMaxSupply} = testnetOptions
140+
128141
-- | Setup a number of credentials and nodes (SPOs and relays), like this:
129142
--
130143
-- > ├── byron-gen-command
@@ -194,14 +207,15 @@ cardanoTestnet :: ()
194207
=> CardanoTestnetOptions -- ^ The options to use
195208
-> Conf
196209
-> UserNodeConfig -- ^ The node configuration file to use. If omitted it's generated.
197-
-> ShelleyGenesis StandardCrypto -- ^ The shelley genesis to use, for example 'getDefaultShelleyGenesis' from this module.
198-
-- Some fields are overridden by the accompanying 'CardanoTestnetOptions'.
199-
-> AlonzoGenesis -- ^ The alonzo genesis to use, for example 'getDefaultAlonzoGenesis' from this module.
200-
-> ConwayGenesis StandardCrypto -- ^ The conway genesis to use, for example 'Defaults.defaultConwayGenesis'.
210+
-> GenesisBatch
211+
-- ^ The shelley genesis to use, for example 'getDefaultShelleyGenesis' from this module,
212+
-- and whether they ar provided by the user or defaulted by 'cardano-testnet'.
213+
-- Some fields are overridden by the accompanying 'CardanoTestnetOptions' (TODO: don't override
214+
-- anything when the user provided the files)
201215
-> H.Integration TestnetRuntime
202216
cardanoTestnet
203217
testnetOptions Conf{tempAbsPath=TmpAbsolutePath tmpAbsPath} mNodeConfigFile
204-
shelleyGenesis alonzoGenesis conwayGenesis = do
218+
genesisBatch@(GenesisBatch (shelleyGenesis, alonzoGenesis, conwayGenesis, _)) = do
205219
let CardanoTestnetOptions
206220
{ cardanoNodeEra=asbe
207221
, cardanoMaxSupply=maxSupply
@@ -225,7 +239,7 @@ cardanoTestnet
225239
writeGenesisSpecFile "alonzo" alonzoGenesis
226240
writeGenesisSpecFile "conway" conwayGenesis
227241

228-
_ <- createSPOGenesisAndFiles nPools nDReps maxSupply asbe shelleyGenesis alonzoGenesis conwayGenesis (TmpAbsolutePath tmpAbsPath)
242+
_ <- createSPOGenesisAndFiles nPools nDReps maxSupply asbe genesisBatch (TmpAbsolutePath tmpAbsPath)
229243

230244
-- TODO: This should come from the configuration!
231245
let makePathsAbsolute :: (Element a ~ FilePath, MonoFunctor a) => a -> a

cardano-testnet/src/Testnet/Start/Types.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ module Testnet.Start.Types
2323
, isSpoNodeOptions
2424
, isRelayNodeOptions
2525
, cardanoDefaultTestnetNodeOptions
26+
, GenesisBatch(..)
2627
, GenesisOptions(..)
28+
, GenesisOrigin(..)
2729

2830
, NodeLoggingFormat(..)
2931
, Conf(..)
@@ -33,6 +35,11 @@ module Testnet.Start.Types
3335
) where
3436

3537
import Cardano.Api hiding (cardanoEra)
38+
import Cardano.Api.Ledger (StandardCrypto)
39+
40+
import Cardano.Ledger.Alonzo.Genesis
41+
import Cardano.Ledger.Conway.Genesis
42+
import Cardano.Ledger.Shelley.Genesis
3643

3744
import Prelude
3845

@@ -139,6 +146,14 @@ data UserNodeConfig =
139146
UserNodeConfigNotSubmitted
140147
| UserNodeConfig FilePath
141148

149+
-- | Type to track if a genesis file was provided by the user or defaulted by @cardano-testnet@.
150+
data GenesisOrigin =
151+
UserProvidedOrigin -- ^ Caller of @cardano-tesnet@ provided the genesis files
152+
| DefaultedOrigin -- ^ Genesis file provided by @cardano-testnet@ itself
153+
154+
-- | Type to bundle all genesis files, whether provided by the user or defaulted by @cardano-testnet@.
155+
newtype GenesisBatch = GenesisBatch (ShelleyGenesis StandardCrypto, AlonzoGenesis, ConwayGenesis StandardCrypto, GenesisOrigin)
156+
142157
-- | Get extra CLI arguments passed to the node executable
143158
testnetNodeExtraCliArgs :: TestnetNodeOptions -> [String]
144159
testnetNodeExtraCliArgs (SpoNodeOptions args) = args

cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/Gov/NoConfidence.hs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,8 @@ hprop_gov_no_confidence = integrationWorkspace "no-confidence" $ \tempAbsBasePat
104104
committeeThreshold = unsafeBoundedRational 0.5
105105
committee = L.Committee (Map.fromList [(comKeyCred1, EpochNo 100)]) committeeThreshold
106106

107-
alonzoGenesis <- getDefaultAlonzoGenesis sbe
108-
shelleyGenesis' <-
109-
getDefaultShelleyGenesis
110-
asbe
111-
(cardanoMaxSupply fastTestnetOptions)
112-
shelleyOptions
113-
let conwayGenesisWithCommittee =
114-
defaultConwayGenesis { L.cgCommittee = committee }
107+
GenesisBatch(shelleyGenesis', alonzoGenesis, conwayGenesis, _) <- getDefaultGenesisBatch fastTestnetOptions shelleyOptions
108+
let conwayGenesisWithCommittee = conwayGenesis { L.cgCommittee = committee }
115109

116110
TestnetRuntime
117111
{ testnetMagic
@@ -120,8 +114,8 @@ hprop_gov_no_confidence = integrationWorkspace "no-confidence" $ \tempAbsBasePat
120114
, configurationFile
121115
} <- cardanoTestnet
122116
fastTestnetOptions
123-
conf UserNodeConfigNotSubmitted shelleyGenesis'
124-
alonzoGenesis conwayGenesisWithCommittee
117+
conf UserNodeConfigNotSubmitted
118+
(GenesisBatch (shelleyGenesis', alonzoGenesis, conwayGenesisWithCommittee, UserProvidedOrigin))
125119

126120
poolNode1 <- H.headM testnetNodes
127121
poolSprocket1 <- H.noteShow $ nodeSprocket poolNode1

0 commit comments

Comments
 (0)