Skip to content

Commit 9e632b5

Browse files
committed
Optimize MonadWriter ImpTestM instance by using a Seq instead of a list
Also abstract the interface for obtaining events from ImpTestM actions
1 parent d5b6eb3 commit 9e632b5

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Imp/EnactSpec.hs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import Cardano.Ledger.Shelley.LedgerState
2323
import Cardano.Ledger.Shelley.Rules (Event, ShelleyTickEvent (..))
2424
import Cardano.Ledger.Val (zero, (<->))
2525
import Control.Monad (forM)
26-
import Control.Monad.Writer (listen)
2726
import Data.Default (def)
2827
import Data.Foldable as F (foldl', traverse_)
2928
import Data.List.NonEmpty (NonEmpty (..))
@@ -214,18 +213,18 @@ hardForkInitiationSpec =
214213
submitYesVote_ (DRepVoter dRep1) govActionId
215214
submitYesVote_ (StakePoolVoter stakePoolId1) govActionId
216215
passNEpochs 2
217-
& listen
218-
>>= expectHardForkEvents . snd <*> pure []
216+
& impEventsFrom
217+
>>= expectHardForkEvents <*> pure []
219218
getProtVer `shouldReturn` curProtVer
220219
submitYesVote_ (DRepVoter dRep2) govActionId
221220
passNEpochs 2
222-
& listen
223-
>>= expectHardForkEvents . snd <*> pure []
221+
& impEventsFrom
222+
>>= expectHardForkEvents <*> pure []
224223
getProtVer `shouldReturn` curProtVer
225224
submitYesVote_ (StakePoolVoter stakePoolId2) govActionId
226225
passNEpochs 2
227-
& listen
228-
>>= expectHardForkEvents . snd
226+
& impEventsFrom
227+
>>= expectHardForkEvents
229228
<*> pure
230229
[ SomeSTSEvent @era @"TICK" . injectEvent $ ConwayHardForkEvent nextProtVer
231230
]
@@ -255,13 +254,13 @@ hardForkInitiationNoDRepsSpec =
255254
submitYesVoteCCs_ committeeMembers' govActionId
256255
submitYesVote_ (StakePoolVoter stakePoolId1) govActionId
257256
passNEpochs 2
258-
& listen
259-
>>= expectHardForkEvents . snd <*> pure []
257+
& impEventsFrom
258+
>>= expectHardForkEvents <*> pure []
260259
getProtVer `shouldReturn` curProtVer
261260
submitYesVote_ (StakePoolVoter stakePoolId2) govActionId
262261
passNEpochs 2
263-
& listen
264-
>>= expectHardForkEvents . snd
262+
& impEventsFrom
263+
>>= expectHardForkEvents
265264
<*> pure
266265
[ SomeSTSEvent @era @"TICK" . injectEvent $ ConwayHardForkEvent nextProtVer
267266
]

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Imp/EpochSpec.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import Cardano.Ledger.Conway.State
2525
import Cardano.Ledger.Shelley.LedgerState
2626
import Cardano.Ledger.Shelley.Rules (Event, ShelleyTickEvent (..))
2727
import Cardano.Ledger.Val
28-
import Control.Monad.Writer (listen)
2928
import Data.Default (Default (..))
3029
import qualified Data.List.NonEmpty as NE
3130
import qualified Data.Map.Strict as Map
@@ -528,7 +527,7 @@ eventsSpec = describe "Events" $ do
528527
| Just (TickNewEpochEvent (EpochEvent (GovInfoEvent {})) :: ShelleyTickEvent era) <- cast ev = True
529528
isGovInfoEvent _ = False
530529
passEpochWithNoDroppedActions = do
531-
(_, evs) <- listen passEpoch
530+
evs <- impEventsFrom passEpoch
532531
filter isGovInfoEvent evs
533532
`shouldBeExpr` [ SomeSTSEvent @era @"TICK" . injectEvent $
534533
GovInfoEvent mempty mempty mempty mempty
@@ -545,7 +544,7 @@ eventsSpec = describe "Events" $ do
545544
& bodyTxL . certsTxBodyL
546545
.~ SSeq.singleton (UnRegDepositTxCert rewardCred keyDeposit)
547546
passEpochWithNoDroppedActions
548-
(_, evs) <- listen passEpoch
547+
evs <- impEventsFrom passEpoch
549548
checkProposedParameterA
550549
let
551550
filteredEvs = filter isGovInfoEvent evs

eras/shelley/impl/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949

5050
### `testlib`
5151

52+
* Add `impEventsFrom`
53+
* Change type of `ImpTestState.impEvents` field from `[]` to `Seq`
5254
* Renamed `impLastTick` to `impCurSlotNo` and `impLastTickG` to `impCurSlotNoG`
5355
* Add CDDL certificate definitions: `account_registration_cert`, `account_unregistration_cert`, `delegation_to_stake_pool_cert`
5456
* Add CDDL pool certificate definitions via `mkPoolRules`: `pool_registration_cert`, `pool_retirement_cert`

eras/shelley/impl/testlib/Test/Cardano/Ledger/Shelley/ImpTest.hs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ module Test.Cardano.Ledger.Shelley.ImpTest (
147147
withNoFixup,
148148
withPostFixup,
149149
withPreFixup,
150+
impEventsFrom,
150151
impNESL,
151152
impGlobalsL,
152153
impCurSlotNoG,
@@ -262,6 +263,8 @@ import Data.Map.Strict (Map)
262263
import qualified Data.Map.Strict as Map
263264
import Data.Maybe (catMaybes, isNothing, mapMaybe)
264265
import Data.Ratio (denominator, numerator, (%))
266+
import Data.Sequence (Seq)
267+
import qualified Data.Sequence as Seq
265268
import Data.Sequence.Strict (StrictSeq (..))
266269
import qualified Data.Sequence.Strict as SSeq
267270
import qualified Data.Set as Set
@@ -346,7 +349,7 @@ data ImpTestState era = ImpTestState
346349
, impNativeScripts :: !(Map ScriptHash (NativeScript era))
347350
, impCurSlotNo :: !SlotNo
348351
, impGlobals :: !Globals
349-
, impEvents :: [SomeSTSEvent era]
352+
, impEvents :: Seq (SomeSTSEvent era)
350353
}
351354

352355
-- | This is a preliminary state that is used to prepare the actual `ImpTestState`
@@ -409,7 +412,7 @@ impNativeScriptsG ::
409412
SimpleGetter (ImpTestState era) (Map ScriptHash (NativeScript era))
410413
impNativeScriptsG = impNativeScriptsL
411414

412-
impEventsL :: Lens' (ImpTestState era) [SomeSTSEvent era]
415+
impEventsL :: Lens' (ImpTestState era) (Seq (SomeSTSEvent era))
413416
impEventsL = lens impEvents (\x y -> x {impEvents = y})
414417

415418
class
@@ -897,7 +900,7 @@ itePostEpochBoundaryHookL ::
897900
)
898901
itePostEpochBoundaryHookL = lens itePostEpochBoundaryHook (\x y -> x {itePostEpochBoundaryHook = y})
899902

900-
instance MonadWriter [SomeSTSEvent era] (ImpTestM era) where
903+
instance MonadWriter (Seq (SomeSTSEvent era)) (ImpTestM era) where
901904
writer (x, evs) = (impEventsL %= (<> evs)) $> x
902905
listen act = do
903906
oldEvs <- use impEventsL
@@ -910,6 +913,11 @@ instance MonadWriter [SomeSTSEvent era] (ImpTestM era) where
910913
((a, f), evs) <- listen act
911914
writer (a, f evs)
912915

916+
impEventsFrom ::
917+
ImpTestM era () ->
918+
ImpTestM era [SomeSTSEvent era]
919+
impEventsFrom = fmap (toList . snd) . listen
920+
913921
runShelleyBase :: ShelleyBase a -> ImpTestM era a
914922
runShelleyBase act = do
915923
globals <- use impGlobalsL
@@ -1208,7 +1216,7 @@ trySubmitTx tx = do
12081216
rootIndex
12091217
| outsSize > 0 = outsSize - 1
12101218
| otherwise = error ("Expected at least 1 output after submitting tx: " <> show txId)
1211-
tell $ fmap (SomeSTSEvent @era @"LEDGER") events
1219+
tell . Seq.fromList $ SomeSTSEvent @era @"LEDGER" <$> events
12121220
modify $ impNESL . nesEsL . esLStateL .~ st'
12131221
UTxO utxo <- getUTxO
12141222
-- This TxIn is in the utxo, and thus can be the new root, only if the transaction
@@ -1328,7 +1336,7 @@ runImpRule env st sig = do
13281336
unlines $
13291337
("Failed to run " <> ruleName <> ":") : map show (toList fs)
13301338
Right res -> evaluateDeep res
1331-
tell $ fmap (SomeSTSEvent @era @rule) ev
1339+
tell . Seq.fromList $ SomeSTSEvent @era @rule <$> ev
13321340
pure res
13331341

13341342
-- | Runs the TICK rule once

0 commit comments

Comments
 (0)