Skip to content

Commit d3a2163

Browse files
committed
Address review comments
1 parent 0a7b814 commit d3a2163

File tree

1 file changed

+32
-32
lines changed
  • ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus

1 file changed

+32
-32
lines changed

ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/GSM.hs

+32-32
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
-- | Tests for the Genesis State Machine using the [quickcheck-dynamic](https://hackage.haskell.org/package/quickcheck-dynamic) library.
2020
--
21-
-- The instance 'QD.StateModel (Model)' describes the actions that the model supports,
21+
-- The instance 'QD.StateModel Model' describes the actions that the model supports,
2222
-- and their semantics in terms for the model and the system-under-test.
2323
--
2424
-- We use the [reflection](https://hackage.haskell.org/package/reflection) library to solve the problem of
@@ -30,7 +30,7 @@ module Test.Consensus.GSM (tests) where
3030
import Cardano.Network.Types (LedgerStateJudgement (..))
3131
import Control.Concurrent.Class.MonadSTM.Strict.TVar.Checked
3232
import Control.Exception (SomeException (..))
33-
import Control.Monad (replicateM_)
33+
import qualified Control.Monad as Monad
3434
import Control.Monad.Class.MonadAsync (async, poll,
3535
uninterruptibleCancel)
3636
import Control.Monad.Class.MonadFork (MonadFork, yield)
@@ -110,9 +110,9 @@ prop_sequential_iosim pUpstreamPeerBound pInitialJudgement (QC.Fun _ pIsHaaSatis
110110
, pInitialJudgement
111111
, pIsHaaSatisfied
112112
})
113-
-- NOTE: the actions have to be generated with an explicit call, as
114-
-- 'generator' relies on reflection for access to the parameters
115-
$ QC.forAll QC.arbitrary $ \actions ->
113+
-- NOTE: the actions have to be generated within the application of 'Reflection.give',
114+
-- as 'generator' relies on reflection for access to the parameters.
115+
$ QC.property $ \actions ->
116116
runIOSimProp $ prop_sequential_iosim1 actions
117117

118118
setupGsm :: (Set.Set UpstreamPeer -> Bool) -> SystemStateVars (IOSim.IOSim s) -> GSM.GsmEntryPoints (IOSim.IOSim s)
@@ -151,7 +151,7 @@ setupGsm isHaaSatisfied vars = do
151151
prop_sequential_iosim1 ::
152152
forall s.
153153
Reflection.Given StaticParams =>
154-
QD.Actions (Model) ->
154+
QD.Actions Model ->
155155
QC.PropertyM (RunMonad (IOSim.IOSim s)) QC.Property
156156
prop_sequential_iosim1 actions = do
157157
vars@SystemStateVars{varEvents} <- lift ask
@@ -169,7 +169,7 @@ prop_sequential_iosim1 actions = do
169169

170170
lift . lift $ yieldSeveralTimes
171171

172-
-- TODO: figure out how to do withAsync here
172+
-- start the GSM
173173
hGSM <- lift . lift $ async gsmEntryPoint
174174

175175
(metadata, mbExn) <- do
@@ -248,8 +248,8 @@ data Model = Model {
248248
deriving anyclass (TD.ToExpr)
249249

250250
-- | Initialise the 'Model' state from 'StaticParams'
251-
initModel :: StaticParams -> Model
252-
initModel StaticParams{pIsHaaSatisfied, pInitialJudgement} = Model {
251+
initModel :: Reflection.Given StaticParams => Model
252+
initModel = Model {
253253
mCandidates = Map.empty
254254
,
255255
mClock = SI.Time 0
@@ -271,32 +271,34 @@ initModel StaticParams{pIsHaaSatisfied, pInitialJudgement} = Model {
271271
where
272272
idlers = Set.empty
273273

274+
StaticParams{pIsHaaSatisfied, pInitialJudgement} = Reflection.given
275+
274276
-- | The 'StaticParams' are supplied through reflection because this seems to be
275277
-- the cleanest way to pass static configuration needed by the 'initState' and
276278
-- 'arbitraryAction' methods
277-
instance Reflection.Given StaticParams => QD.StateModel (Model) where
278-
data Action (Model) a where
279-
Disconnect :: UpstreamPeer -> QD.Action (Model) ()
279+
instance Reflection.Given StaticParams => QD.StateModel Model where
280+
data Action Model a where
281+
Disconnect :: UpstreamPeer -> QD.Action Model ()
280282
-- ^ INVARIANT must be an existing peer
281283
--
282284
-- Mocks the necessary ChainSync client behavior.
283-
ExtendSelection :: S -> QD.Action (Model) ()
285+
ExtendSelection :: S -> QD.Action Model ()
284286
-- ^ INVARIANT 'selectionIsBehind'
285287
--
286288
-- NOTE Harmless to assume it only advances by @'B' 1@ at a time.
287-
ModifyCandidate :: UpstreamPeer -> B -> QD.Action (Model) ()
289+
ModifyCandidate :: UpstreamPeer -> B -> QD.Action Model ()
288290
-- ^ INVARIANT existing peer
289291
--
290292
-- Mocks the necessary ChainSync client behavior.
291-
NewCandidate :: UpstreamPeer -> B -> QD.Action (Model) ()
293+
NewCandidate :: UpstreamPeer -> B -> QD.Action Model ()
292294
-- ^ INVARIANT new peer
293295
--
294296
-- Mocks the necessary ChainSync client behavior.z
295-
ReadGsmState :: QD.Action (Model) GSM.GsmState
296-
ReadMarker :: QD.Action (Model) MarkerState
297-
StartIdling :: UpstreamPeer -> QD.Action (Model) ()
297+
ReadGsmState :: QD.Action Model GSM.GsmState
298+
ReadMarker :: QD.Action Model MarkerState
299+
StartIdling :: UpstreamPeer -> QD.Action Model ()
298300
-- ^ INVARIANT existing peer, not idling
299-
TimePasses :: Int -> QD.Action (Model) ()
301+
TimePasses :: Int -> QD.Action Model ()
300302
-- ^ tenths of a second
301303
--
302304
-- INVARIANT positive
@@ -313,8 +315,7 @@ instance Reflection.Given StaticParams => QD.StateModel (Model) where
313315

314316
shrinkAction _ctx model = shrinker model
315317

316-
initialState = initModel params
317-
where params = Reflection.given
318+
initialState = initModel
318319

319320
nextState model action _ = transition model action
320321

@@ -408,7 +409,7 @@ newtype RunMonad m a = RunMonad {runMonad :: ReaderT (SystemStateVars m) m a}
408409
instance MonadTrans RunMonad where
409410
lift = RunMonad . lift
410411

411-
instance (IOLike m, Reflection.Given StaticParams) => QD.RunModel (Model) (RunMonad m) where
412+
instance (IOLike m, Reflection.Given StaticParams) => QD.RunModel Model (RunMonad m) where
412413
perform _ action _ = do
413414
SystemStateVars{varSelection, varStates, varGsmState, varMarker, varEvents} <- ask
414415
let
@@ -492,7 +493,7 @@ runIOSimProp p =
492493
-- the modified model and a property that checks that the action brought
493494
-- the model and the SUT into the same state.
494495
takeActionInBoth :: (IOLike m, Reflection.Given StaticParams)
495-
=> String -> Model -> QD.Action (Model) GSM.GsmState
496+
=> String -> Model -> QD.Action Model GSM.GsmState
496497
-> QC.PropertyM (RunMonad m) (QC.Property, Model)
497498
takeActionInBoth conterexampleMessage model action = do
498499
-- run the action in the model
@@ -530,7 +531,7 @@ boringDur model = boringDurImpl clk sel st
530531
mState = st
531532
} = model
532533

533-
addNotableWhen :: Notable -> Bool -> (Model) -> (Model)
534+
addNotableWhen :: Notable -> Bool -> Model -> Model
534535
addNotableWhen n b model =
535536
if not b then model else
536537
model { mNotables = n `Set.insert` mNotables model }
@@ -569,7 +570,7 @@ dummyVar = QD.mkVar 0
569570
--- Definitions used in the 'ModelState' instance
570571

571572
precondition :: forall a. Reflection.Given StaticParams
572-
=> Model -> QD.Action (Model) a -> Bool
573+
=> Model -> QD.Action Model a -> Bool
573574
precondition model = \case
574575
cmd@ExtendSelection{} ->
575576
let model' = QD.nextState model cmd dummyVar
@@ -604,8 +605,8 @@ precondition model = \case
604605
mIdlers = idlers
605606
} = model
606607

607-
generator :: forall. Reflection.Given StaticParams
608-
=> Model -> QC.Gen (QD.Any (QD.Action (Model)))
608+
generator :: Reflection.Given StaticParams
609+
=> Model -> QC.Gen (QD.Any (QD.Action Model))
609610
generator model = QC.frequency $
610611
[ (,) 5 $ QD.Some . Disconnect <$> QC.elements old | notNull old ]
611612
<>
@@ -692,7 +693,7 @@ generator model = QC.frequency $
692693
]
693694

694695

695-
shrinker :: Model -> QD.Action (Model) a -> [QD.Any (QD.Action (Model))]
696+
shrinker :: Model -> QD.Action Model a -> [QD.Any (QD.Action Model)]
696697
shrinker _model = \case
697698
Disconnect{} ->
698699
[]
@@ -715,7 +716,7 @@ shrinker _model = \case
715716
shrinkS (S x) = [ S x' | x' <- QC.shrink x ]
716717

717718
transition :: forall a. Reflection.Given StaticParams
718-
=> Model -> QD.Action (Model) a -> Model
719+
=> Model -> QD.Action Model a -> Model
719720
transition model cmd =
720721
fixupModelState cmd $
721722
case cmd of
@@ -766,9 +767,8 @@ transition model cmd =
766767

767768
-- | Update the 'mState', assuming that's the only stale field in the given
768769
-- 'Model'
769-
--
770770
fixupModelState :: forall a. Reflection.Given StaticParams
771-
=> QD.Action (Model) a -> Model -> Model
771+
=> QD.Action Model a -> Model -> Model
772772
fixupModelState cmd model =
773773
case st of
774774
ModelPreSyncing
@@ -973,7 +973,7 @@ isIdling (PeerState {psIdling = Idling i}) = i
973973
-- Despite the crudeness, this seems much more compositional than invasive
974974
-- explicit synchronization.
975975
yieldSeveralTimes :: MonadFork m => m ()
976-
yieldSeveralTimes = replicateM_ 10 yield
976+
yieldSeveralTimes = Monad.replicateM_ 10 yield
977977

978978
{-
979979

0 commit comments

Comments
 (0)