11{-# LANGUAGE GADTs #-}
22{-# LANGUAGE OverloadedStrings #-}
3-
3+ {-# LANGUAGE TypeApplications #-}
44
55module Testnet.Components.Configuration
66 ( createConfigYaml
7+ , createSPOGenesisAndFiles
78 , mkTopologyConfig
89 ) where
910
@@ -15,29 +16,40 @@ import Cardano.Node.Types
1516import Ouroboros.Network.PeerSelection.LedgerPeers
1617import Ouroboros.Network.PeerSelection.State.LocalRootPeers
1718
19+ import Control.Monad
20+ import Control.Monad.Catch (MonadCatch )
1821import Control.Monad.IO.Class (MonadIO )
22+ import Data.Aeson
1923import qualified Data.Aeson as Aeson
2024import qualified Data.ByteString.Lazy as LBS
25+ import qualified Data.HashMap.Lazy as HM
2126import qualified Data.List as List
2227import Data.String
28+ import Data.Time
2329import GHC.Stack (HasCallStack )
2430import qualified GHC.Stack as GHC
2531import System.FilePath.Posix ((</>) )
2632
2733import Hedgehog
34+ import qualified Hedgehog as H
35+ import qualified Hedgehog.Extras.Stock.Aeson as J
36+ import qualified Hedgehog.Extras.Stock.Time as DTC
37+ import qualified Hedgehog.Extras.Test.Base as H
38+ import qualified Hedgehog.Extras.Test.File as H
2839
2940import Testnet.Defaults
3041import Testnet.Filepath
42+ import Testnet.Process.Run (execCli_ )
3143import Testnet.Property.Utils
44+ import Testnet.Start.Types
45+
3246
3347createConfigYaml
3448 :: (MonadTest m , MonadIO m , HasCallStack )
3549 => TmpAbsolutePath
3650 -> AnyCardanoEra
3751 -> m LBS. ByteString
38- createConfigYaml tempAbsPath anyCardanoEra' = GHC. withFrozenCallStack $ do
39- let tempAbsPath' = unTmpAbsPath tempAbsPath
40-
52+ createConfigYaml (TmpAbsolutePath tempAbsPath') anyCardanoEra' = GHC. withFrozenCallStack $ do
4153 -- Add Byron, Shelley and Alonzo genesis hashes to node configuration
4254 -- TODO: These genesis filepaths should not be hardcoded. Using the cli as a library
4355 -- rather as an executable will allow us to get the genesis files paths in a more
@@ -58,6 +70,79 @@ createConfigYaml tempAbsPath anyCardanoEra' = GHC.withFrozenCallStack $ do
5870 ]
5971
6072
73+ createSPOGenesisAndFiles
74+ :: (MonadTest m , MonadCatch m , MonadIO m , HasCallStack )
75+ => CardanoTestnetOptions
76+ -> UTCTime -- ^ Start time
77+ -> TmpAbsolutePath
78+ -> m FilePath -- ^ Shelley genesis directory
79+ createSPOGenesisAndFiles testnetOptions startTime (TmpAbsolutePath tempAbsPath') = do
80+ let testnetMagic = cardanoTestnetMagic testnetOptions
81+ numPoolNodes = length $ cardanoNodes testnetOptions
82+ -- TODO: Even this is cumbersome. You need to know where to put the initial
83+ -- shelley genesis for create-staked to use.
84+ createStakedInitialGenesisFile = tempAbsPath' </> " genesis.spec.json"
85+
86+ -- TODO: We need to read the genesis files into Haskell and modify them
87+ -- based on cardano-testnet's cli parameters
88+
89+ -- We create the initial genesis file to avoid having to re-write the genesis file later
90+ -- with the parameters we want. The user must provide genesis files or we will use a default.
91+ -- We should *never* be modifying the genesis file after cardano-testnet is run because this
92+ -- is sure to be a source of confusion if users provide genesis files and we are mutating them
93+ -- without their knowledge.
94+ let shelleyGenesis :: LBS. ByteString
95+ shelleyGenesis = encode $ defaultShelleyGenesis startTime testnetOptions
96+
97+ H. evalIO $ LBS. writeFile createStakedInitialGenesisFile shelleyGenesis
98+
99+ -- TODO: Remove this rewrite.
100+ -- 50 second epochs
101+ -- Epoch length should be "10 * k / f" where "k = securityParam, f = activeSlotsCoeff"
102+ H. rewriteJsonFile createStakedInitialGenesisFile $ J. rewriteObject
103+ ( HM. insert " securityParam" (toJSON @ Int 5 ) -- TODO: USE config p arameter
104+ . HM. adjust
105+ (J. rewriteObject
106+ $ HM. adjust
107+ (J. rewriteObject (HM. insert " major" (toJSON @ Int 8 )))
108+ " protocolVersion"
109+ ) " protocolParams"
110+ . HM. insert " rho" (toJSON @ Double 0.1 )
111+ . HM. insert " tau" (toJSON @ Double 0.1 )
112+ . HM. insert " updateQuorum" (toJSON @ Int 2 )
113+ )
114+
115+ execCli_
116+ [ " genesis" , " create-staked"
117+ , " --genesis-dir" , tempAbsPath'
118+ , " --testnet-magic" , show @ Int testnetMagic
119+ , " --gen-pools" , show @ Int numPoolNodes
120+ , " --supply" , " 1000000000000"
121+ , " --supply-delegated" , " 1000000000000"
122+ , " --gen-stake-delegs" , " 3"
123+ , " --gen-utxo-keys" , " 3"
124+ , " --start-time" , DTC. formatIso8601 startTime
125+ ]
126+
127+ -- Here we move all of the keys etc generated by create-staked
128+ -- for the nodes to use
129+
130+ -- Move all genesis related files
131+
132+ genesisByronDir <- H. createDirectoryIfMissing $ tempAbsPath' </> " byron"
133+ genesisShelleyDir <- H. createDirectoryIfMissing $ tempAbsPath' </> " shelley"
134+
135+ files <- H. listDirectory tempAbsPath'
136+ forM_ files $ \ file -> do
137+ H. note file
138+
139+ H. renameFile (tempAbsPath' </> " byron-gen-command/genesis.json" ) (genesisByronDir </> " genesis.json" )
140+ H. renameFile (tempAbsPath' </> " genesis.alonzo.json" ) (genesisShelleyDir </> " genesis.alonzo.json" )
141+ H. renameFile (tempAbsPath' </> " genesis.conway.json" ) (genesisShelleyDir </> " genesis.conway.json" )
142+ H. renameFile (tempAbsPath' </> " genesis.json" ) (genesisShelleyDir </> " genesis.json" )
143+
144+ return genesisShelleyDir
145+
61146ifaceAddress :: String
62147ifaceAddress = " 127.0.0.1"
63148
0 commit comments