@@ -14,10 +14,10 @@ import Data.Default (Default (..))
1414import Data.List (find )
1515import qualified Data.Map as Map
1616import Data.Maybe (fromMaybe )
17- import qualified Data.Traversable as Traversable
1817import qualified ExamplesRelay
1918import qualified ExamplesRelayP2P
2019import qualified ExamplesTCP
20+ import LeiosProtocol.Common (BlockBody , PraosConfig )
2121import qualified LeiosProtocol.Config as OnDisk
2222import qualified LeiosProtocol.Short.VizSim as VizShortLeios
2323import qualified LeiosProtocol.Short.VizSimP2P as VizShortLeiosP2P
@@ -30,6 +30,7 @@ import Options.Applicative (
3030 Parser ,
3131 ParserInfo ,
3232 ParserPrefs ,
33+ asum ,
3334 auto ,
3435 command ,
3536 commandGroup ,
@@ -199,7 +200,7 @@ data VizSubCommand
199200 | VizPraos1
200201 | VizPraosP2P1
201202 { seed :: Int
202- , optionalConfigFile :: Maybe FilePath
203+ , configOptions :: ConfigOptions
203204 , topologyOptions :: TopologyOptions
204205 , overrideUnlimitedBps :: Bytes
205206 }
@@ -208,7 +209,12 @@ data VizSubCommand
208209 | VizRelayTest2
209210 | VizRelayTest3
210211 | VizShortLeios1
211- | VizShortLeiosP2P1 { seed :: Int , configFile :: FilePath , topologyOptions :: TopologyOptions , overrideUnlimitedBps :: Bytes }
212+ | VizShortLeiosP2P1
213+ { seed :: Int
214+ , configOptions :: ConfigOptions
215+ , topologyOptions :: TopologyOptions
216+ , overrideUnlimitedBps :: Bytes
217+ }
212218
213219parserVizSubCommand :: Parser VizSubCommand
214220parserVizSubCommand =
@@ -262,7 +268,7 @@ parserPraosP2P1 :: Parser VizSubCommand
262268parserPraosP2P1 =
263269 VizPraosP2P1
264270 <$> parserSeed
265- <*> optional parserLeiosConfigFile
271+ <*> parserConfigOptions
266272 <*> parserTopologyOptions
267273 <*> parserOverrideUnlimited
268274
@@ -286,7 +292,7 @@ parserShortLeiosP2P1 =
286292 <> help " The seed for the random number generator."
287293 <> shownDefValue 0
288294 )
289- <*> parserLeiosConfigFile
295+ <*> parserConfigOptions
290296 <*> parserTopologyOptions
291297 <*> parserOverrideUnlimited
292298
@@ -306,7 +312,7 @@ vizOptionsToViz VizCommandWithOptions{..} = case vizSubCommand of
306312 let rng0 = Random. mkStdGen seed
307313 let (rng1, rng2) = Random. split rng0
308314 p2pNetwork <- execTopologyOptions rng1 topologyOptions
309- cfg <- fromMaybe def <$> traverse ( fmap VizPraosP2P. convertConfig . OnDisk. readConfig) optionalConfigFile
315+ cfg <- execConfigOptions configOptions
310316 pure $ VizPraosP2P. example1 rng2 cfg p2pNetwork
311317 VizPraosP2P2 -> pure VizPraosP2P. example2
312318 VizRelayTest1 -> pure VizSimTestRelay. example1
@@ -317,8 +323,8 @@ vizOptionsToViz VizCommandWithOptions{..} = case vizSubCommand of
317323 let rng0 = Random. mkStdGen seed
318324 let (rng1, rng2) = Random. split rng0
319325 p2pNetwork <- execTopologyOptions rng1 topologyOptions
320- config <- OnDisk. readConfig configFile
321- pure $ VizShortLeiosP2P. example2 rng2 config p2pNetwork
326+ cfg <- execConfigOptions configOptions
327+ pure $ VizShortLeiosP2P. example2 rng2 cfg p2pNetwork
322328
323329type VizSize = (Int , Int )
324330
@@ -356,14 +362,14 @@ runSimOptions SimOptions{..} = case simCommand of
356362 SimPraosDiffusion {.. } -> do
357363 let rng0 = Random. mkStdGen seed
358364 let (rng1, rng2) = Random. split rng0
359- mconfig <- Traversable. traverse OnDisk. readConfig optionalConfigFile
365+ config <- execConfigOptions configOptions
360366 p2pNetwork <- execTopologyOptions rng1 topologyOptions
361367 -- let bandwidth = 10 * 125_000_000 :: Bytes -- 10 Gbps TODO: set in config
362- VizPraosP2P. example1000Diffusion rng2 mconfig p2pNetwork simOutputSeconds simOutputFile
368+ VizPraosP2P. example1000Diffusion rng2 config p2pNetwork simOutputSeconds simOutputFile
363369 SimShortLeios {.. } -> do
364370 let rng0 = Random. mkStdGen seed
365371 let (rng1, rng2) = Random. split rng0
366- config <- OnDisk. readConfig configFile
372+ config <- execConfigOptions configOptions
367373 p2pNetwork <- execTopologyOptions rng1 topologyOptions
368374 VizShortLeiosP2P. exampleSim (not skipLog) rng2 config p2pNetwork emitControl simOutputSeconds simOutputFile
369375
@@ -390,13 +396,13 @@ parserSimOptions =
390396data SimCommand
391397 = SimPraosDiffusion
392398 { seed :: Int
393- , optionalConfigFile :: Maybe FilePath
399+ , configOptions :: ConfigOptions
394400 , topologyOptions :: TopologyOptions
395401 , overrideUnlimitedBps :: Bytes
396402 }
397403 | SimShortLeios
398404 { seed :: Int
399- , configFile :: FilePath
405+ , configOptions :: ConfigOptions
400406 , topologyOptions :: TopologyOptions
401407 , overrideUnlimitedBps :: Bytes
402408 , emitControl :: Bool
@@ -417,20 +423,39 @@ parserSimPraosDiffusion :: Parser SimCommand
417423parserSimPraosDiffusion =
418424 SimPraosDiffusion
419425 <$> parserSeed
420- <*> optional parserLeiosConfigFile
426+ <*> parserConfigOptions
421427 <*> parserTopologyOptions
422428 <*> parserOverrideUnlimited
423429
424430parserShortLeios :: Parser SimCommand
425431parserShortLeios =
426432 SimShortLeios
427433 <$> parserSeed
428- <*> parserLeiosConfigFile
434+ <*> parserConfigOptions
429435 <*> parserTopologyOptions
430436 <*> parserOverrideUnlimited
431437 <*> switch (long " log-control" <> help " Include control messages in log." )
432438 <*> switch (long " no-log" <> help " Do not output event log." )
433439
440+ data ConfigOptions
441+ = LeiosConfigFile FilePath
442+ | LeiosConfig OnDisk. Config
443+
444+ execConfigOptions :: ConfigOptions -> IO OnDisk. Config
445+ execConfigOptions = \ case
446+ LeiosConfigFile configFile -> OnDisk. readConfig configFile
447+ LeiosConfig config -> pure config
448+
449+ execConfigOptionsForPraos :: ConfigOptions -> IO (PraosConfig BlockBody )
450+ execConfigOptionsForPraos = fmap VizPraosP2P. convertConfig . execConfigOptions
451+
452+ parserConfigOptions :: Parser ConfigOptions
453+ parserConfigOptions =
454+ asum
455+ [ LeiosConfigFile <$> parserLeiosConfigFile
456+ , pure $ LeiosConfig def
457+ ]
458+
434459parserLeiosConfigFile :: Parser FilePath
435460parserLeiosConfigFile =
436461 strOption $
@@ -559,10 +584,16 @@ data TopologyOptions
559584 = TopologyFile FilePath
560585 | TopologyGenerationOptions TopologyGenerationOptions
561586
587+ instance Default TopologyOptions where
588+ def = TopologyGenerationOptions def
589+
562590parserTopologyOptions :: Parser TopologyOptions
563591parserTopologyOptions =
564- (TopologyFile <$> parserTopologyFile)
565- <|> (TopologyGenerationOptions <$> parserTopologyGenerationOptions)
592+ asum
593+ [ TopologyFile <$> parserTopologyFile
594+ , TopologyGenerationOptions <$> parserTopologyGenerationOptions
595+ , pure def
596+ ]
566597
567598parserTopologyFile :: Parser FilePath
568599parserTopologyFile =
@@ -577,6 +608,9 @@ data TopologyGenerationOptions
577608 = TopologyGenerationStrategyFile FilePath
578609 | TopologyGenerationStrategy TopologyGenerationStrategy
579610
611+ instance Default TopologyGenerationOptions where
612+ def = TopologyGenerationStrategy def
613+
580614parserTopologyGenerationOptions :: Parser TopologyGenerationOptions
581615parserTopologyGenerationOptions =
582616 (TopologyGenerationStrategyFile <$> parserTopologyGenerationStrategyFile)
@@ -595,7 +629,7 @@ parserTopologyGenerationStrategy :: Parser TopologyGenerationStrategy
595629parserTopologyGenerationStrategy =
596630 subparser . mconcat $
597631 [ commandGroup " Available topology generation strategies:"
598- , command " close-and-random" . info (PickLinksCloseAndRandom <$> parserPickLinksCloseAndRandomOptions) $
632+ , command " close-and-random" . info (PickLinksCloseAndRandom <$> parserPickLinksCloseAndRandomOptions <**> helper ) $
599633 progDesc " Pick links to close and random nodes."
600634 ]
601635
@@ -605,21 +639,21 @@ parserPickLinksCloseAndRandomOptions =
605639 <$> parserWorld
606640 <*> option
607641 auto
608- ( long " tg-num-nodes"
642+ ( long " tg-cnr- num-nodes"
609643 <> metavar " NUMBER"
610644 <> help " The number of nodes."
611645 <> shownDefValue (numNodes def)
612646 )
613647 <*> option
614648 auto
615- ( long " tg-num-links-close"
649+ ( long " tg-cnr- num-links-close"
616650 <> metavar " NUMBER"
617651 <> help " The number of links to close peers for each node."
618652 <> shownDefValue (numCloseLinksPerNode def)
619653 )
620654 <*> option
621655 auto
622- ( long " tg-num-links-random"
656+ ( long " tg-cnr- num-links-random"
623657 <> metavar " NUMBER"
624658 <> help " The number of links to random peers for each node."
625659 <> shownDefValue (numRandomLinksPerNode def)
0 commit comments