@@ -65,6 +65,11 @@ import Hedgehog.Extras (failMessage)
65
65
import qualified Hedgehog.Extras.Stock.OS as OS
66
66
import qualified Hedgehog.Extras.Test.Base as H
67
67
import qualified Hedgehog.Extras.Test.File as H
68
+ import Data.IORef
69
+ import GHC.IO.Unsafe (unsafePerformIO )
70
+ import Hedgehog (MonadTest )
71
+ import GHC.Stack
72
+ import Text.Printf (printf )
68
73
69
74
{- HLINT ignore "Redundant flip" -}
70
75
{- HLINT ignore "Redundant id" -}
@@ -106,6 +111,31 @@ cardanoTestnetDefault opts conf = do
106
111
opts conf startTime
107
112
(Defaults. defaultShelleyGenesis startTime opts) alonzoGenesis Defaults. defaultConwayGenesis
108
113
114
+
115
+ -- | Starting port number, from which testnet nodes will get new ports.
116
+ defaultTestnetNodeStartingPortNumber :: Int
117
+ defaultTestnetNodeStartingPortNumber = 13000
118
+
119
+ availablePortNumber :: IORef Int
120
+ availablePortNumber = unsafePerformIO $ newIORef defaultTestnetNodeStartingPortNumber
121
+ {-# NOINLINE availablePortNumber #-}
122
+
123
+ requestAvailablePortNumbers
124
+ :: HasCallStack
125
+ => MonadIO m
126
+ => MonadTest m
127
+ => Int
128
+ -> m [Int ]
129
+ requestAvailablePortNumbers numberOfPorts
130
+ | numberOfPorts > maxPortsPerRequest = withFrozenCallStack $ do
131
+ H. note_ $ " Tried to allocate " <> show numberOfPorts <> " port numbers in one request. "
132
+ <> " It's allowed to allocate no more than " <> show maxPortsPerRequest <> " per request."
133
+ H. failure
134
+ | otherwise = liftIO $ atomicModifyIORef' availablePortNumber $ \ n ->
135
+ (n + maxPortsPerRequest, [n.. n + numberOfPorts - 1 ])
136
+ where
137
+ maxPortsPerRequest = 100
138
+
109
139
-- | Setup a number of credentials and pools, like this:
110
140
--
111
141
-- > ├── byron
@@ -178,6 +208,8 @@ cardanoTestnet
178
208
nbPools = numPools testnetOptions
179
209
era = cardanoNodeEra testnetOptions
180
210
211
+ portNumbers <- requestAvailablePortNumbers numPoolNodes
212
+
181
213
-- Sanity checks
182
214
testnetMinimumConfigurationRequirements testnetOptions
183
215
when (shelleyStartTime /= startTime) $ do
@@ -284,87 +316,26 @@ cardanoTestnet
284
316
H. evalIO $ LBS. writeFile configurationFile finalYamlConfig
285
317
286
318
-- Byron related
287
-
288
- H. renameFile (tmpAbsPath </> " byron-gen-command/delegate-keys.000.key" ) (tmpAbsPath </> poolKeyDir 1 </> " byron-delegate.key" )
289
- H. renameFile (tmpAbsPath </> " byron-gen-command/delegate-keys.001.key" ) (tmpAbsPath </> poolKeyDir 2 </> " byron-delegate.key" )
290
- H. renameFile (tmpAbsPath </> " byron-gen-command/delegate-keys.002.key" ) (tmpAbsPath </> poolKeyDir 3 </> " byron-delegate.key" )
291
-
292
- H. renameFile (tmpAbsPath </> " byron-gen-command/delegation-cert.000.json" ) (tmpAbsPath </> poolKeyDir 1 </> " byron-delegation.cert" )
293
- H. renameFile (tmpAbsPath </> " byron-gen-command/delegation-cert.001.json" ) (tmpAbsPath </> poolKeyDir 2 </> " byron-delegation.cert" )
294
- H. renameFile (tmpAbsPath </> " byron-gen-command/delegation-cert.002.json" ) (tmpAbsPath </> poolKeyDir 3 </> " byron-delegation.cert" )
295
-
296
- H. writeFile (tmpAbsPath </> poolKeyDir 1 </> " port" ) " 3001"
297
- H. writeFile (tmpAbsPath </> poolKeyDir 2 </> " port" ) " 3002"
298
- H. writeFile (tmpAbsPath </> poolKeyDir 3 </> " port" ) " 3003"
319
+ forM_ (zip [1 .. ] portNumbers) $ \ (i, portNumber) -> do
320
+ let iStr = printf " %03d" (i - 1 )
321
+ H. renameFile (tmpAbsPath </> " byron-gen-command" </> " delegate-keys." <> iStr <> " .key" ) (tmpAbsPath </> poolKeyDir i </> " byron-delegate.key" )
322
+ H. renameFile (tmpAbsPath </> " byron-gen-command" </> " delegation-cert." <> iStr <> " .json" ) (tmpAbsPath </> poolKeyDir i </> " byron-delegation.cert" )
323
+ H. writeFile (tmpAbsPath </> poolKeyDir i </> " port" ) (show portNumber)
299
324
300
325
301
326
-- Make topology files
302
- -- TODO generalise this over the N BFT nodes and pool nodes
303
-
304
- H. lbsWriteFile (tmpAbsPath </> poolKeyDir 1 </> " topology.json" ) $ encode $
305
- object
306
- [ " Producers" .= toJSON
307
- [ object
308
- [ " addr" .= toJSON @ String " 127.0.0.1"
309
- , " port" .= toJSON @ Int 3002
310
- , " valency" .= toJSON @ Int 1
311
- ]
312
- , object
313
- [ " addr" .= toJSON @ String " 127.0.0.1"
314
- , " port" .= toJSON @ Int 3003
315
- , " valency" .= toJSON @ Int 1
316
- ]
317
- , object
318
- [ " addr" .= toJSON @ String " 127.0.0.1"
319
- , " port" .= toJSON @ Int 3005
320
- , " valency" .= toJSON @ Int 1
321
- ]
322
- ]
323
- ]
324
-
325
- H. lbsWriteFile (tmpAbsPath </> poolKeyDir 2 </> " topology.json" ) $ encode $
326
- object
327
- [ " Producers" .= toJSON
328
- [ object
329
- [ " addr" .= toJSON @ String " 127.0.0.1"
330
- , " port" .= toJSON @ Int 3001
331
- , " valency" .= toJSON @ Int 1
332
- ]
333
- , object
334
- [ " addr" .= toJSON @ String " 127.0.0.1"
335
- , " port" .= toJSON @ Int 3003
336
- , " valency" .= toJSON @ Int 1
337
- ]
338
- , object
339
- [ " addr" .= toJSON @ String " 127.0.0.1"
340
- , " port" .= toJSON @ Int 3005
341
- , " valency" .= toJSON @ Int 1
342
- ]
343
- ]
344
- ]
345
-
346
- H. lbsWriteFile (tmpAbsPath </> poolKeyDir 3 </> " topology.json" ) $ encode $
347
- object
348
- [ " Producers" .= toJSON
349
- [ object
350
- [ " addr" .= toJSON @ String " 127.0.0.1"
351
- , " port" .= toJSON @ Int 3001
352
- , " valency" .= toJSON @ Int 1
353
- ]
354
- , object
355
- [ " addr" .= toJSON @ String " 127.0.0.1"
356
- , " port" .= toJSON @ Int 3002
357
- , " valency" .= toJSON @ Int 1
358
- ]
359
- , object
360
- [ " addr" .= toJSON @ String " 127.0.0.1"
361
- , " port" .= toJSON @ Int 3005
362
- , " valency" .= toJSON @ Int 1
363
- ]
364
- ]
365
- ]
366
-
367
- let spoNodesWithPortNos = L. zip poolKeysFps [3001 .. ]
327
+ forM_ (zip [1 .. ] portNumbers) $ \ (i, myPortNumber) -> do
328
+ let producers = flip map (filter (/= myPortNumber) portNumbers) $ \ otherProducerPort ->
329
+ object
330
+ [ " addr" .= toJSON @ String " 127.0.0.1"
331
+ , " port" .= toJSON otherProducerPort
332
+ , " valency" .= toJSON @ Int 1
333
+ ]
334
+
335
+ H. lbsWriteFile (tmpAbsPath </> poolKeyDir i </> " topology.json" ) $ encode $
336
+ object [ " Producers" .= producers ]
337
+
338
+ let spoNodesWithPortNos = L. zip poolKeysFps portNumbers
368
339
ePoolNodes <- forM (L. zip spoNodesWithPortNos poolKeys) $ \ ((node, port),key) -> do
369
340
let nodeName = tail $ dropWhile (/= ' /' ) node
370
341
H. note_ $ " Node name: " <> nodeName
0 commit comments