Skip to content

Commit b348e7a

Browse files
committed
fix(simulation): consistent CLI options
1 parent 76428e6 commit b348e7a

File tree

3 files changed

+66
-29
lines changed

3 files changed

+66
-29
lines changed

simulation/src/Main.hs

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import Data.Default (Default (..))
1414
import Data.List (find)
1515
import qualified Data.Map as Map
1616
import Data.Maybe (fromMaybe)
17-
import qualified Data.Traversable as Traversable
1817
import qualified ExamplesRelay
1918
import qualified ExamplesRelayP2P
2019
import qualified ExamplesTCP
20+
import LeiosProtocol.Common (BlockBody, PraosConfig)
2121
import qualified LeiosProtocol.Config as OnDisk
2222
import qualified LeiosProtocol.Short.VizSim as VizShortLeios
2323
import 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

213219
parserVizSubCommand :: Parser VizSubCommand
214220
parserVizSubCommand =
@@ -262,7 +268,7 @@ parserPraosP2P1 :: Parser VizSubCommand
262268
parserPraosP2P1 =
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

323329
type 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 =
390396
data 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
417423
parserSimPraosDiffusion =
418424
SimPraosDiffusion
419425
<$> parserSeed
420-
<*> optional parserLeiosConfigFile
426+
<*> parserConfigOptions
421427
<*> parserTopologyOptions
422428
<*> parserOverrideUnlimited
423429

424430
parserShortLeios :: Parser SimCommand
425431
parserShortLeios =
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+
434459
parserLeiosConfigFile :: Parser FilePath
435460
parserLeiosConfigFile =
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+
562590
parserTopologyOptions :: Parser TopologyOptions
563591
parserTopologyOptions =
564-
(TopologyFile <$> parserTopologyFile)
565-
<|> (TopologyGenerationOptions <$> parserTopologyGenerationOptions)
592+
asum
593+
[ TopologyFile <$> parserTopologyFile
594+
, TopologyGenerationOptions <$> parserTopologyGenerationOptions
595+
, pure def
596+
]
566597

567598
parserTopologyFile :: Parser FilePath
568599
parserTopologyFile =
@@ -577,6 +608,9 @@ data TopologyGenerationOptions
577608
= TopologyGenerationStrategyFile FilePath
578609
| TopologyGenerationStrategy TopologyGenerationStrategy
579610

611+
instance Default TopologyGenerationOptions where
612+
def = TopologyGenerationStrategy def
613+
580614
parserTopologyGenerationOptions :: Parser TopologyGenerationOptions
581615
parserTopologyGenerationOptions =
582616
(TopologyGenerationStrategyFile <$> parserTopologyGenerationStrategyFile)
@@ -595,7 +629,7 @@ parserTopologyGenerationStrategy :: Parser TopologyGenerationStrategy
595629
parserTopologyGenerationStrategy =
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)

simulation/src/PraosProtocol/ExamplesPraosP2P.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ import System.Random (StdGen, mkStdGen)
4545
import qualified Topology as P2P
4646
import Viz
4747

48-
example1 :: StdGen -> PraosConfig BlockBody -> P2P.P2PNetwork -> Visualization
48+
example1 :: StdGen -> OnDisk.Config -> P2P.P2PNetwork -> Visualization
4949
example1 _rng0 _cfg _p2pNetwork@P2P.P2PNetwork{p2pLinks, p2pNodeStakes}
5050
| not $ null [() | (_, Nothing) <- Map.elems p2pLinks] =
5151
error "Only finite bandwidth for this vizualization"
5252
| length (List.group $ Map.elems p2pNodeStakes) /= 1 =
5353
error "Only uniform stake supported for this vizualization"
5454
example1 rng0 cfg p2pNetwork =
55-
Viz (praosSimVizModel (example1Trace rng0 cfg p2pNetwork)) $
55+
Viz (praosSimVizModel (example1Trace rng0 (convertConfig cfg) p2pNetwork)) $
5656
LayoutAbove
5757
[ layoutLabelTime
5858
, LayoutBeside
@@ -260,7 +260,7 @@ convertConfig disk = praos
260260
-- | Diffusion example with 1000 nodes.
261261
example1000Diffusion ::
262262
StdGen ->
263-
Maybe OnDisk.Config ->
263+
OnDisk.Config ->
264264
P2P.P2PNetwork ->
265265
-- | when to stop simulation.
266266
Time ->
@@ -296,8 +296,8 @@ example1000Diffusion rng0 cfg [email protected]{p2pNodeStakes, p2pNodeCo
296296
logMsg ((PraosMessage _)) = Nothing
297297

298298
traceFile = dropExtension fp <.> "log"
299-
praosConfig = maybe defaultPraosConfig convertConfig cfg
300299
stake nid = maybe undefined coerce $ Map.lookup nid p2pNodeStakes
300+
praosConfig = convertConfig cfg
301301
trace =
302302
tracePraosP2P
303303
rng0

simulation/src/Topology.hs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -890,10 +890,6 @@ data PickLinksCloseAndRandomOptions = PickLinksCloseAndRandomOptions
890890
}
891891
deriving (Eq, Show, Generic)
892892

893-
data TopologyGenerationStrategy
894-
= PickLinksCloseAndRandom !PickLinksCloseAndRandomOptions
895-
deriving (Eq, Show, Generic)
896-
897893
instance Default PickLinksCloseAndRandomOptions where
898894
def =
899895
PickLinksCloseAndRandomOptions
@@ -903,6 +899,13 @@ instance Default PickLinksCloseAndRandomOptions where
903899
, numRandomLinksPerNode = 5
904900
}
905901

902+
data TopologyGenerationStrategy
903+
= PickLinksCloseAndRandom !PickLinksCloseAndRandomOptions
904+
deriving (Eq, Show, Generic)
905+
906+
instance Default TopologyGenerationStrategy where
907+
def = PickLinksCloseAndRandom def
908+
906909
generateTopology :: RandomGen g => g -> TopologyGenerationStrategy -> IO P2PNetwork
907910
generateTopology rng0 = \case
908911
PickLinksCloseAndRandom PickLinksCloseAndRandomOptions{..} -> do

0 commit comments

Comments
 (0)