Skip to content

Commit 4a7bcf8

Browse files
committed
Added code comments to leios trace verifier
1 parent dcf8561 commit 4a7bcf8

File tree

9 files changed

+47
-0
lines changed

9 files changed

+47
-0
lines changed

leios-trace-verifier/hs-src/app/linear/Main.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{-# LANGUAGE OverloadedStrings #-}
33
{-# LANGUAGE RecordWildCards #-}
44

5+
-- | Main entry for trace verification of Linear Leios.
56
module Main where
67

78
import Control.Monad (unless)
@@ -18,6 +19,7 @@ import System.IO (hPutStrLn, stderr)
1819

1920
import qualified Data.Text as T (unpack)
2021

22+
-- | Run the CLI.
2123
main :: IO ()
2224
main =
2325
do
@@ -47,6 +49,7 @@ main =
4749
putStrLn . T.unpack $ snd (snd result)
4850
exitFailure
4951

52+
-- | CLI commands.
5053
data Command = Command
5154
{ logFile :: FilePath
5255
, configFile :: FilePath
@@ -55,6 +58,7 @@ data Command = Command
5558
}
5659
deriving (Eq, Ord, Read, Show)
5760

61+
-- | Command parser.
5862
commandParser :: ParserInfo Command
5963
commandParser =
6064
info (com <**> helper) $

leios-trace-verifier/hs-src/app/short/Main.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{-# LANGUAGE OverloadedStrings #-}
33
{-# LANGUAGE RecordWildCards #-}
44

5+
-- | Main entry for trace verification of Short Leios.
56
module Main where
67

78
import Control.Monad (unless)
@@ -18,6 +19,7 @@ import System.IO (hPutStrLn, stderr)
1819

1920
import qualified Data.Text as T (unpack)
2021

22+
-- | Run the CLI.
2123
main :: IO ()
2224
main =
2325
do
@@ -47,6 +49,7 @@ main =
4749
putStrLn . T.unpack $ snd (snd result)
4850
exitFailure
4951

52+
-- | CLI commands.
5053
data Command = Command
5154
{ logFile :: FilePath
5255
, configFile :: FilePath
@@ -55,6 +58,7 @@ data Command = Command
5558
}
5659
deriving (Eq, Ord, Read, Show)
5760

61+
-- | Command parser.
5862
commandParser :: ParserInfo Command
5963
commandParser =
6064
info (com <**> helper) $

leios-trace-verifier/hs-src/src/LinearLeiosLib.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
-- | Imports from Agda.
12
module LinearLeiosLib (
23
module P,
34
module V,

leios-trace-verifier/hs-src/src/ShortLeiosLib.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
-- | Imports from Agda.
12
module ShortLeiosLib (
23
module P,
34
module V,

leios-trace-verifier/hs-src/test/Spec.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
--| Main entry point.
2+
13
module Main where
24

35
import Spec.Generated (generated)
46
import Spec.Golden (golden)
57
import Test.Hspec (describe, hspec)
68

9+
-- | Test the trace verifier.
710
main :: IO ()
811
main =
912
hspec $ do

leios-trace-verifier/hs-src/test/Spec/Generated.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{-# LANGUAGE OverloadedStrings #-}
44
{-# LANGUAGE RecordWildCards #-}
55

6+
-- | Arbitrary and generated tests.
67
module Spec.Generated where
78

89
import Control.Monad (join, liftM2, mzero, replicateM)
@@ -20,6 +21,7 @@ import Test.QuickCheck hiding (scale)
2021
import qualified Data.Map.Strict as M
2122
import qualified Spec.Scenario as Scenario (config, idSut, topology)
2223

24+
-- | Run the verify on a list of events.
2325
verify :: [TraceEvent] -> (Integer, (Text, Text))
2426
verify =
2527
let
@@ -33,12 +35,14 @@ verify =
3335
in
3436
verifyTrace nrNodes Scenario.idSut stakeDistribution stageLength' ledgerQuality lateIBInclusion
3537

38+
-- | Expectation for checking a trace.
3639
data Check
3740
= MustBeOkay
3841
| MustNotBeOkay
3942
| MustBe Text
4043
deriving (Show)
4144

45+
-- | Check that a trace has the expected number of actions and result.
4246
check ::
4347
Maybe Integer ->
4448
Check ->
@@ -57,6 +61,7 @@ check expectedActions expectedMessage events =
5761
Nothing -> checkMessage $ fst (snd result)
5862
Just expectedActions' -> fst result === expectedActions' .&&. checkMessage (fst (snd result))
5963

64+
-- | Generate the initial IB and events leading up to it.
6065
initStageIB :: Gen [Transition]
6166
initStageIB =
6267
let
@@ -65,6 +70,7 @@ initStageIB =
6570
in
6671
join <$> replicateM stageLength' ((: [NextSlot]) <$> gIB)
6772

73+
-- | Generate the initial EB and events leading up to it.
6874
initStageEB :: Gen [Transition]
6975
initStageEB =
7076
let
@@ -79,6 +85,7 @@ initStageEB =
7985
a <- join <$> replicateM (stageLength' - 1) ((: [NextSlot]) <$> gIB)
8086
pure $ l ++ [NextSlot] ++ a
8187

88+
-- | Generate the initial vote and events leading up to it.
8289
initStageVT :: Gen [Transition]
8390
initStageVT =
8491
let
@@ -103,6 +110,7 @@ initStageVT =
103110
)
104111
pure $ l ++ [NextSlot] ++ a
105112

113+
-- | Generate the initial events.
106114
initPipelines :: Gen [Transition]
107115
initPipelines = do
108116
s1 <- initStageIB
@@ -112,6 +120,7 @@ initPipelines = do
112120
s5 <- initStageVT
113121
pure $ s1 ++ s2 ++ s3 ++ s4 ++ s5
114122

123+
-- | Wrapper for skipping production of RBs, IBs, EBs, or votes.
115124
newtype SkipProduction = SkipProduction {unSkipProduction :: [Transition]}
116125
deriving (Show)
117126

@@ -127,6 +136,7 @@ instance Arbitrary SkipProduction where
127136
pure $ SkipProduction (i ++ r)
128137
shrink = fmap SkipProduction . init . inits . unSkipProduction
129138

139+
-- | Wrapper for sporadic production of RBs, IBs, EBs, or votes.
130140
newtype SporadicProduction = SporadicProduction {unSporadicProduction :: [Transition]}
131141
deriving (Show)
132142

@@ -153,6 +163,7 @@ instance Arbitrary SporadicProduction where
153163
pure $ SporadicProduction (i ++ r)
154164
shrink = fmap SporadicProduction . init . inits . unSporadicProduction
155165

166+
-- | Wrapper for noisy production (i.e., shuffled) of RBs, IBs, EB, and votes.
156167
newtype NoisyProduction = NoisyProduction {unNoisyProduction :: [Transition]}
157168
deriving (Show)
158169

@@ -182,6 +193,7 @@ instance Arbitrary NoisyProduction where
182193
pure $ NoisyProduction (i ++ r)
183194
shrink = fmap NoisyProduction . init . inits . unNoisyProduction
184195

196+
-- | Wrapper for sporadically missing RBs, IBs, EB, and votes.
185197
newtype SporadicMisses = SporadicMisses {unSporadicMisses :: [Transition]}
186198
deriving (Show)
187199

@@ -192,6 +204,7 @@ instance Arbitrary SporadicMisses where
192204
i <- choose (1, length valid - 1)
193205
pure . SporadicMisses $ take i valid <> drop (i + 1) valid <> pure NextSlot
194206

207+
-- | Generate tests.
195208
generated :: Spec
196209
generated =
197210
do

leios-trace-verifier/hs-src/test/Spec/Golden.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{-# LANGUAGE RecordWildCards #-}
44
{-# LANGUAGE ScopedTypeVariables #-}
55

6+
-- | Golden tests.
67
module Spec.Golden (
78
golden,
89
) where
@@ -22,6 +23,7 @@ import System.Directory (listDirectory)
2223
import System.FilePath ((</>))
2324
import Test.Hspec (Expectation, Spec, SpecWith, describe, it, runIO, shouldBe, shouldNotBe)
2425

26+
-- | Run golden tests.
2527
golden :: Spec
2628
golden = do
2729
dir <- runIO $ Paths.getDataDir

leios-trace-verifier/hs-src/test/Spec/Scenario.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{-# LANGUAGE OverloadedStrings #-}
44
{-# LANGUAGE RecordWildCards #-}
55

6+
-- | Scenario variables for tests.
67
module Spec.Scenario (
78
config,
89
topology,
@@ -19,14 +20,18 @@ import LeiosTypes (Point (..))
1920
import qualified Data.Map.Strict as M
2021
import qualified Data.Set as S
2122

23+
-- | The protocol configuration.
2224
config :: Config
2325
config = def{relayStrategy = RequestFromFirst, tcpCongestionControl = True, multiplexMiniProtocols = True, treatBlocksAsFull = False, cleanupPolicies = CleanupPolicies (S.fromList [CleanupExpiredVote]), simulateTransactions = True, leiosStageLengthSlots = 2, leiosStageActiveVotingSlots = 1, leiosVoteSendRecvStages = False, leiosVariant = Short, leiosLateIbInclusion = False, leiosHeaderDiffusionTimeMs = 1000.0, praosChainQuality = 20.0, txGenerationDistribution = Exp{lambda = 0.85, scale = pure 1000.0}, txSizeBytesDistribution = LogNormal{mu = 6.833, sigma = 1.127}, txValidationCpuTimeMs = 1.5, txMaxSizeBytes = 16384, rbGenerationProbability = 5.0e-2, rbGenerationCpuTimeMs = 1.0, rbHeadValidationCpuTimeMs = 1.0, rbHeadSizeBytes = 1024, rbBodyMaxSizeBytes = 90112, rbBodyLegacyPraosPayloadValidationCpuTimeMsConstant = 50.0, rbBodyLegacyPraosPayloadValidationCpuTimeMsPerByte = 5.0e-4, rbBodyLegacyPraosPayloadAvgSizeBytes = 0, ibGenerationProbability = 5.0, ibGenerationCpuTimeMs = 130.0, ibHeadSizeBytes = 304, ibHeadValidationCpuTimeMs = 1.0, ibBodyValidationCpuTimeMsConstant = 50.0, ibBodyValidationCpuTimeMsPerByte = 5.0e-4, ibBodyMaxSizeBytes = 327680, ibBodyAvgSizeBytes = 98304, ibDiffusionStrategy = FreshestFirst, ibDiffusionMaxWindowSize = 100, ibDiffusionMaxHeadersToRequest = 100, ibDiffusionMaxBodiesToRequest = 1, ibShards = 50, ebGenerationProbability = 1.5, ebGenerationCpuTimeMs = 75.0, ebValidationCpuTimeMs = 1.0, ebSizeBytesConstant = 240, ebSizeBytesPerIb = 32, ebDiffusionStrategy = PeerOrder, ebDiffusionMaxWindowSize = 100, ebDiffusionMaxHeadersToRequest = 100, ebDiffusionMaxBodiesToRequest = 1, ebMaxAgeSlots = 100, ebMaxAgeForRelaySlots = 40, voteGenerationProbability = 500.0, voteGenerationCpuTimeMsConstant = 0.164, voteGenerationCpuTimeMsPerIb = 0.0, voteValidationCpuTimeMs = 0.816, voteThreshold = 300, voteBundleSizeBytesConstant = 0, voteBundleSizeBytesPerEb = 105, voteDiffusionStrategy = PeerOrder, voteDiffusionMaxWindowSize = 100, voteDiffusionMaxHeadersToRequest = 100, voteDiffusionMaxBodiesToRequest = 1, certGenerationCpuTimeMsConstant = 90.0, certGenerationCpuTimeMsPerNode = 0.0, certValidationCpuTimeMsConstant = 130.0, certValidationCpuTimeMsPerNode = 0.0, certSizeBytesConstant = 7168, certSizeBytesPerNode = 0}
2426

27+
-- | The topology.
2528
topology :: Topology 'COORD2D
2629
topology = Topology{nodes = M.fromList [(NodeName "node-0", Node{nodeInfo = NodeInfo{stake = 500, cpuCoreCount = CpuCoreCount mzero, location = LocCoord2D{coord2D = Point{_1 = 0.12000040231003672, _2 = 0.1631004621065356}}, adversarial = mzero}, producers = M.fromList [(NodeName "node-1", LinkInfo{latencyMs = 141.01364015418432, bandwidthBytesPerSecond = BandwidthBps $ pure 1024000}), (NodeName "node-2", LinkInfo{latencyMs = 254.6249782835189, bandwidthBytesPerSecond = BandwidthBps $ pure 1024000})]}), (NodeName "node-1", Node{nodeInfo = NodeInfo{stake = 200, cpuCoreCount = CpuCoreCount mzero, location = LocCoord2D{coord2D = Point{_1 = 0.34276660615051174, _2 = 0.2636899791034371}}, adversarial = mzero}, producers = M.fromList [(NodeName "node-2", LinkInfo{latencyMs = 175.32530255486685, bandwidthBytesPerSecond = BandwidthBps $ pure 1024000}), (NodeName "node-3", LinkInfo{latencyMs = 379.1167948193313, bandwidthBytesPerSecond = BandwidthBps $ pure 1024000})]}), (NodeName "node-2", Node{nodeInfo = NodeInfo{stake = 100, cpuCoreCount = CpuCoreCount mzero, location = LocCoord2D{coord2D = Point{_1 = 0.5150493264153491, _2 = 0.27873594531347595}}, adversarial = mzero}, producers = M.fromList [(NodeName "node-3", LinkInfo{latencyMs = 248.31457793649423, bandwidthBytesPerSecond = BandwidthBps $ pure 1024000})]}), (NodeName "node-3", Node{nodeInfo = NodeInfo{stake = 0, cpuCoreCount = CpuCoreCount mzero, location = LocCoord2D{coord2D = Point{_1 = 0.3503537969220088, _2 = 0.13879558055660354}}, adversarial = mzero}, producers = M.fromList [(NodeName "node-0", LinkInfo{latencyMs = 140.19739576271448, bandwidthBytesPerSecond = BandwidthBps $ pure 1024000})]})]}
2730

31+
-- | The system under test.
2832
idSut :: Integer
2933
idSut = 0
3034

35+
-- | The system not under test.
3136
idOther :: Integer
3237
idOther = 1

leios-trace-verifier/hs-src/test/Spec/Transition.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{-# LANGUAGE OverloadedStrings #-}
44
{-# LANGUAGE RecordWildCards #-}
55

6+
-- | Generation of valid sequences of events.
67
module Spec.Transition where
78

89
import Control.Lens hiding (elements)
@@ -23,6 +24,7 @@ import qualified Data.Set as S
2324
import qualified Data.Text as T
2425
import qualified Spec.Scenario as Scenario (config, idOther, idSut)
2526

27+
-- | The context for tracking the state, used in generating valid events.
2628
data TracingContext = TracingContext
2729
{ _clock :: Time
2830
, _slotNo :: SlotNo
@@ -51,6 +53,8 @@ instance Default TracingContext where
5153
Scenario.idOther
5254
(leiosStageLengthSlots Scenario.config)
5355

56+
-- Various lenses.
57+
5458
clock :: Lens' TracingContext Time
5559
clock = lens _clock $ \ctx x -> ctx{_clock = x}
5660

@@ -87,6 +91,7 @@ other = to $ T.pack . ("node-" <>) . show . _idOther
8791
stageLength :: Getter TracingContext Word
8892
stageLength = to _stageLength
8993

94+
-- | An abstract (i.e., contextless) event.
9095
data Transition
9196
= NextSlot
9297
| SkipSlot
@@ -103,13 +108,15 @@ data Transition
103108
| ReceiveVT
104109
deriving (Show)
105110

111+
-- | Generate a new identifier.
106112
genId :: Integer -> Word64 -> Set Text -> Gen Text
107113
genId system slot forbidden =
108114
let
109115
g = T.pack . ((show system <> "-" <> show slot <> "-") <>) . show <$> (arbitrary :: Gen Word16)
110116
in
111117
g `suchThat` (not . (`S.member` forbidden))
112118

119+
-- | Generate a valid RB.
113120
genRB :: Integer -> StateT TracingContext Gen (Text, Nullable BlockRef)
114121
genRB i =
115122
do
@@ -123,6 +130,7 @@ genRB i =
123130
rbs %= M.insert block_id parent
124131
pure (block_id, Nullable . pure $ BlockRef parent)
125132

133+
-- | Generate a valid IB.
126134
genIB :: Integer -> StateT TracingContext Gen Text
127135
genIB i =
128136
do
@@ -132,6 +140,7 @@ genIB i =
132140
ibs %= S.insert ib
133141
pure ib
134142

143+
-- | Generate a valid EB.
135144
genEB :: Integer -> StateT TracingContext Gen Text
136145
genEB i =
137146
do
@@ -141,6 +150,7 @@ genEB i =
141150
ebs %= S.insert eb
142151
pure eb
143152

153+
-- | Generate a valid vote.
144154
genVT :: Integer -> StateT TracingContext Gen Text
145155
genVT i =
146156
do
@@ -150,9 +160,11 @@ genVT i =
150160
vts %= S.insert vt
151161
pure vt
152162

163+
-- | Advance the clock.
153164
tick :: StateT TracingContext Gen ()
154165
tick = clock %= (+ 0.000001)
155166

167+
-- | Generate an actual valid event from its abstract representation.
156168
transition :: Transition -> StateT TracingContext Gen [Event]
157169
transition SkipSlot =
158170
do
@@ -257,12 +269,14 @@ transition ReceiveVT =
257269
block_id <- genVT =<< use idOther
258270
pure [VTBundleReceived{..}]
259271

272+
-- | Generate a valid trace from abstract events.
260273
transitions :: [Transition] -> Gen [TraceEvent]
261274
transitions =
262275
(`evalStateT` def)
263276
. (mapM timestamp =<<)
264277
. fmap concat
265278
. mapM transition
266279

280+
-- Timestamp an event.
267281
timestamp :: Monad m => Event -> StateT TracingContext m TraceEvent
268282
timestamp = uses clock . flip TraceEvent

0 commit comments

Comments
 (0)