Skip to content

Commit 85a0e31

Browse files
committed
Implement flag --update-time
1 parent bd90e5a commit 85a0e31

File tree

2 files changed

+61
-37
lines changed

2 files changed

+61
-37
lines changed

cardano-testnet/cardano-testnet.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ library
5555
, cardano-ledger-shelley
5656
, cardano-node
5757
, cardano-ping ^>= 0.8
58+
, cardano-prelude
5859
, contra-tracer
5960
, containers
6061
, data-default-class

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

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ module Testnet.Start.Cardano
2727

2828

2929
import Cardano.Api
30+
import Cardano.Api.Byron (GenesisData (..))
31+
import qualified Cardano.Api.Byron as Byron
3032

3133
import Cardano.Ledger.Alonzo.Genesis (AlonzoGenesis)
3234
import Cardano.Ledger.Conway.Genesis (ConwayGenesis)
3335
import Cardano.Node.Configuration.Topology (RemoteAddress(..))
3436
import qualified Cardano.Node.Configuration.Topology as Direct
3537
import qualified Cardano.Node.Configuration.TopologyP2P as P2P
38+
import Cardano.Prelude (canonicalEncodePretty)
3639
import Ouroboros.Network.PeerSelection.RelayAccessPoint (RelayAccessPoint(..))
3740

3841
import Prelude hiding (lines)
@@ -100,48 +103,68 @@ createTestnetEnv
100103
genesisOptions
101104
CreateEnvOptions
102105
{ ceoTopologyType=topologyType
103-
, ceoUpdateTime=_createEnvUpdateTime
106+
, ceoUpdateTime=createEnvUpdateTime
104107
}
105108
mShelley mAlonzo mConway
106109
Conf
107110
{ genesisHashesPolicy
108111
, tempAbsPath=TmpAbsolutePath tmpAbsPath
109-
} = do
110-
111-
testMinimumConfigurationRequirements testnetOptions
112-
113-
AnyShelleyBasedEra sbe <- pure asbe
114-
_ <- createSPOGenesisAndFiles
115-
testnetOptions genesisOptions
116-
mShelley mAlonzo mConway
117-
(TmpAbsolutePath tmpAbsPath)
118-
119-
configurationFile <- H.noteShow $ tmpAbsPath </> "configuration.yaml"
120-
-- Add Byron, Shelley and Alonzo genesis hashes to node configuration
121-
config' <- case genesisHashesPolicy of
122-
WithHashes -> createConfigJson (TmpAbsolutePath tmpAbsPath) sbe
123-
WithoutHashes -> pure $ createConfigJsonNoHash sbe
124-
-- Setup P2P configuration value
125-
let config = A.insert
126-
"EnableP2P"
127-
(Bool $ topologyType == P2PTopology)
128-
config'
129-
H.evalIO $ LBS.writeFile configurationFile $ A.encodePretty $ Object config
130-
131-
-- Create network topology, with abstract IDs in lieu of addresses
132-
let nodeIds = fst <$> zip [1..] cardanoNodes
133-
forM_ nodeIds $ \i -> do
134-
let nodeDataDir = tmpAbsPath </> Defaults.defaultNodeDataDir i
135-
H.evalIO $ IO.createDirectoryIfMissing True nodeDataDir
136-
137-
let producers = NodeId <$> filter (/= i) nodeIds
138-
case topologyType of
139-
DirectTopology ->
140-
let topology = Direct.RealNodeTopology producers
141-
in H.lbsWriteFile (nodeDataDir </> "topology.json") $ A.encodePretty topology
142-
P2PTopology ->
143-
let topology = Defaults.defaultP2PTopology producers
144-
in H.lbsWriteFile (nodeDataDir </> "topology.json") $ A.encodePretty topology
112+
} = case createEnvUpdateTime of
113+
114+
CreateEnv -> do
115+
testMinimumConfigurationRequirements testnetOptions
116+
117+
AnyShelleyBasedEra sbe <- pure asbe
118+
_ <- createSPOGenesisAndFiles
119+
testnetOptions genesisOptions
120+
mShelley mAlonzo mConway
121+
(TmpAbsolutePath tmpAbsPath)
122+
123+
configurationFile <- H.noteShow $ tmpAbsPath </> "configuration.yaml"
124+
-- Add Byron, Shelley and Alonzo genesis hashes to node configuration
125+
config' <- case genesisHashesPolicy of
126+
WithHashes -> createConfigJson (TmpAbsolutePath tmpAbsPath) sbe
127+
WithoutHashes -> pure $ createConfigJsonNoHash sbe
128+
-- Setup P2P configuration value
129+
let config = A.insert
130+
"EnableP2P"
131+
(Bool $ topologyType == P2PTopology)
132+
config'
133+
H.evalIO $ LBS.writeFile configurationFile $ A.encodePretty $ Object config
134+
135+
-- Create network topology, with abstract IDs in lieu of addresses
136+
let nodeIds = fst <$> zip [1..] cardanoNodes
137+
forM_ nodeIds $ \i -> do
138+
let nodeDataDir = tmpAbsPath </> Defaults.defaultNodeDataDir i
139+
H.evalIO $ IO.createDirectoryIfMissing True nodeDataDir
140+
141+
let producers = NodeId <$> filter (/= i) nodeIds
142+
case topologyType of
143+
DirectTopology ->
144+
let topology = Direct.RealNodeTopology producers
145+
in H.lbsWriteFile (nodeDataDir </> "topology.json") $ A.encodePretty topology
146+
P2PTopology ->
147+
let topology = Defaults.defaultP2PTopology producers
148+
in H.lbsWriteFile (nodeDataDir </> "topology.json") $ A.encodePretty topology
149+
150+
UpdateTimeAndExit -> do
151+
let byronGenesisFile = tmpAbsPath </> "byron-genesis.json"
152+
shelleyGenesisFile = tmpAbsPath </> "shelley-genesis.json"
153+
154+
currentTime <- H.noteShowIO DTC.getCurrentTime
155+
startTime <- H.noteShow $ DTC.addUTCTime startTimeOffsetSeconds currentTime
156+
157+
-- Update start time in Byron genesis file
158+
eByron <- runExceptT $ Byron.readGenesisData byronGenesisFile
159+
(byronGenesis', _byronHash) <- H.leftFail eByron
160+
let byronGenesis = byronGenesis'{gdStartTime = startTime}
161+
H.lbsWriteFile byronGenesisFile $ canonicalEncodePretty byronGenesis
162+
163+
-- Update start time in Shelley genesis file
164+
eShelley <- H.readJsonFile shelleyGenesisFile
165+
shelleyGenesis' :: ShelleyGenesis <- H.leftFail eShelley
166+
let shelleyGenesis = shelleyGenesis'{sgSystemStart = startTime}
167+
H.lbsWriteFile shelleyGenesisFile $ A.encodePretty shelleyGenesis
145168

146169
-- | Starts a number of nodes, as configured by the value of the 'cardanoNodes'
147170
-- field in the 'CardanoTestnetOptions' argument. Regarding this field, you can either:

0 commit comments

Comments
 (0)