Skip to content

Commit 7c8ef80

Browse files
agustinmistatbagrel1nbacquey
committed
Tweak TestBlock to use getPerasCertInBlock in tests
Co-authored-by: Agustin Mista <agustin.mista@moduscreate.com> Co-authored-by: Thomas BAGREL <thomas.bagrel@tweag.io> Co-authored-by: Nicolas BACQUEY <nicolas.bacquey@tweag.io>
1 parent edfbec4 commit 7c8ef80

5 files changed

Lines changed: 70 additions & 39 deletions

File tree

ouroboros-consensus/bench/PerasCertDB-bench/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ fragments = iterate' addSuccessorBlock genesisFragment
115115
in (xs AF.:> x) AF.:> TestBlock.mkNextBlock x nextBlockSlot dummyBody
116116

117117
dummyBody :: TestBody
118-
dummyBody = TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCertRound = Nothing}
118+
dummyBody = TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCert = Nothing}
119119

120120
-- | Given a chain fragment, construct a weight snapshot where there's a boosted block every 90 slots
121121
uniformWeightSnapshot :: AF.AnchoredFragment TestBlock -> PerasWeightSnapshot TestBlock

ouroboros-consensus/src/unstable-consensus-testlib/Test/Ouroboros/Storage/TestBlock.hs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ import Ouroboros.Consensus.Util.Condense
135135
import Ouroboros.Consensus.Util.IndexedMemPack
136136
import Ouroboros.Consensus.Util.Orphans ()
137137
import qualified Ouroboros.Network.Mock.Chain as Chain
138+
import Ouroboros.Network.Point (Block)
138139
import System.FS.API.Lazy
139140
import Test.Cardano.Slotting.Numeric ()
140141
import Test.Cardano.Slotting.TreeDiff ()
@@ -207,13 +208,13 @@ data TestBody = TestBody
207208
-- Note that this is a /local/ number, it is specific to this block,
208209
-- other blocks need not be aware of it.
209210
, tbIsValid :: !Bool
210-
, tbPerasCertRound :: !(Maybe PerasRoundNo)
211+
, tbPerasCert :: !(Maybe (PerasCert TestBlock))
211212
-- ^ Some real blocks will ocasionally carry a Peras certificate inside their
212-
-- body to coordinate the end of a cooldown period. For the purposes of the
213-
-- ChainDB, we don't really care about the details of the certificate other
214-
-- than its round number, which needs to be stored (and carefully updated
215-
-- whenever a newer one pops up) so it can be used to evaluate the Peras
216-
-- voting rules and decide if a node should resume voting.
213+
-- body to coordinate the end of a cooldown period.
214+
-- NOTE: for the purposes of the ChainDB, we don't care about the details of
215+
-- the certificate other than its round number, which needs to be stored (and
216+
-- carefully updated whenever a newer one pops up) so it can be used to
217+
-- evaluate the Peras voting rules and decide if a node should resume voting.
217218
}
218219
deriving stock (Eq, Show, Generic)
219220
deriving anyclass (NFData, NoThunks, Serialise, Hashable)
@@ -953,16 +954,22 @@ corruptionFiles = map snd . NE.toList
953954
Orphans
954955
-------------------------------------------------------------------------------}
955956

957+
-- ** Hashable
958+
956959
deriving newtype instance Hashable SlotNo
957960
deriving newtype instance Hashable BlockNo
958961
deriving newtype instance Hashable PerasRoundNo
959-
instance Hashable IsEBB
960-
961-
-- use generic instance
962962

963+
instance Hashable IsEBB
964+
instance Hashable TestHeader
965+
instance Hashable TestBlock
966+
instance Hashable (Block SlotNo TestHeaderHash)
967+
instance Hashable (Point TestBlock)
968+
instance Hashable (MockPerasCert TestBlock)
969+
instance (Hashable a, Hashable (WithOrigin a)) => Hashable (WithOrigin a)
963970
instance (StandardHash b, Hashable (HeaderHash b)) => Hashable (ChainHash b)
964971

965-
-- use generic instance
972+
-- ** ToExpr
966973

967974
instance ToExpr EBB
968975
instance ToExpr IsEBB

ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/ChainDB/Iterator.hs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ tests =
115115

116116
-- All blocks on the same chain
117117
a, b, c, d, e :: TestBlock
118-
a = firstBlock 0 TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCertRound = Nothing}
119-
b = mkNextBlock a 1 TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCertRound = Nothing}
120-
c = mkNextBlock b 2 TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCertRound = Nothing}
121-
d = mkNextBlock c 3 TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCertRound = Nothing}
122-
e = mkNextBlock d 4 TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCertRound = Nothing}
118+
a = firstBlock 0 TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCert = Nothing}
119+
b = mkNextBlock a 1 TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCert = Nothing}
120+
c = mkNextBlock b 2 TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCert = Nothing}
121+
d = mkNextBlock c 3 TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCert = Nothing}
122+
e = mkNextBlock d 4 TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCert = Nothing}
123123

124124
-- | Requested stream = A -> C
125125
--
@@ -176,9 +176,9 @@ prop_1435_case1 =
176176
(Left (ForkTooOld (StreamFromInclusive (blockRealPoint b'))))
177177
where
178178
canContainEBB = const True
179-
ebb = firstEBB canContainEBB TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCertRound = Nothing}
180-
b = mkNextBlock ebb 0 TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCertRound = Nothing}
181-
b' = mkNextBlock ebb 0 TestBody{tbForkNo = 1, tbIsValid = True, tbPerasCertRound = Nothing}
179+
ebb = firstEBB canContainEBB TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCert = Nothing}
180+
b = mkNextBlock ebb 0 TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCert = Nothing}
181+
b' = mkNextBlock ebb 0 TestBody{tbForkNo = 1, tbIsValid = True, tbPerasCert = Nothing}
182182

183183
-- | Requested stream = EBB' -> EBB' where EBB, B, and EBB' are all blocks in
184184
-- the same slot, and EBB' is not part of the current chain nor ChainDB.
@@ -197,9 +197,9 @@ prop_1435_case2 =
197197
(Left (ForkTooOld (StreamFromInclusive (blockRealPoint ebb'))))
198198
where
199199
canContainEBB = const True
200-
ebb = firstEBB canContainEBB TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCertRound = Nothing}
201-
b = mkNextBlock ebb 0 TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCertRound = Nothing}
202-
ebb' = firstEBB canContainEBB TestBody{tbForkNo = 1, tbIsValid = True, tbPerasCertRound = Nothing}
200+
ebb = firstEBB canContainEBB TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCert = Nothing}
201+
b = mkNextBlock ebb 0 TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCert = Nothing}
202+
ebb' = firstEBB canContainEBB TestBody{tbForkNo = 1, tbIsValid = True, tbPerasCert = Nothing}
203203

204204
-- | Requested stream = EBB -> EBB where EBB and B are all blocks in the same
205205
-- slot.
@@ -218,8 +218,8 @@ prop_1435_case3 =
218218
(Right (map Right [ebb]))
219219
where
220220
canContainEBB = const True
221-
ebb = firstEBB canContainEBB TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCertRound = Nothing}
222-
b = mkNextBlock ebb 0 TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCertRound = Nothing}
221+
ebb = firstEBB canContainEBB TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCert = Nothing}
222+
b = mkNextBlock ebb 0 TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCert = Nothing}
223223

224224
-- | Requested stream = EBB -> EBB where EBB and B are all blocks in the same
225225
-- slot.
@@ -238,8 +238,8 @@ prop_1435_case4 =
238238
(Right (map Right [ebb]))
239239
where
240240
canContainEBB = const True
241-
ebb = firstEBB canContainEBB TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCertRound = Nothing}
242-
b = mkNextBlock ebb 0 TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCertRound = Nothing}
241+
ebb = firstEBB canContainEBB TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCert = Nothing}
242+
b = mkNextBlock ebb 0 TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCert = Nothing}
243243

244244
-- | Requested stream = EBB -> EBB where EBB and B' are all blocks in the same
245245
-- slot, and B' is not part of the current chain nor ChainDB.
@@ -258,8 +258,8 @@ prop_1435_case5 =
258258
(Left (ForkTooOld (StreamFromInclusive (blockRealPoint b'))))
259259
where
260260
canContainEBB = const True
261-
ebb = firstEBB canContainEBB TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCertRound = Nothing}
262-
b' = mkNextBlock ebb 0 TestBody{tbForkNo = 1, tbIsValid = True, tbPerasCertRound = Nothing}
261+
ebb = firstEBB canContainEBB TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCert = Nothing}
262+
b' = mkNextBlock ebb 0 TestBody{tbForkNo = 1, tbIsValid = True, tbPerasCert = Nothing}
263263

264264
-- | Requested stream = EBB' -> EBB' where EBB and EBB' are all blocks in the
265265
-- same slot, and EBB' is not part of the current chain nor ChainDB.
@@ -278,8 +278,8 @@ prop_1435_case6 =
278278
(Left (ForkTooOld (StreamFromInclusive (blockRealPoint ebb'))))
279279
where
280280
canContainEBB = const True
281-
ebb = firstEBB canContainEBB TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCertRound = Nothing}
282-
ebb' = firstEBB canContainEBB TestBody{tbForkNo = 1, tbIsValid = True, tbPerasCertRound = Nothing}
281+
ebb = firstEBB canContainEBB TestBody{tbForkNo = 0, tbIsValid = True, tbPerasCert = Nothing}
282+
ebb' = firstEBB canContainEBB TestBody{tbForkNo = 1, tbIsValid = True, tbPerasCert = Nothing}
283283

284284
-- | The general property test
285285
prop_general_test ::

ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/ChainDB/StateMachine.hs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,24 +2120,37 @@ genBlkPair chunkInfo loe Model{..} =
21202120
[ (4, return True)
21212121
, (1, return False)
21222122
]
2123-
perasCertRound <- do
2124-
let maxRoundNo =
2125-
case Model.roundNoOfLatestCertSeen dbModel of
2126-
Nothing -> 0
2127-
Just (PerasRoundNo r) -> r + 1
2123+
perasCert <- do
21282124
frequency
2129-
[ (9, return Nothing)
2125+
[ (4, return Nothing)
21302126
, let freq = case loe of
21312127
LoEDisabled -> 1
21322128
-- The LoE does not yet support Peras.
21332129
LoEEnabled () -> 0
2134-
in (freq, Just . PerasRoundNo <$> choose (0, maxRoundNo))
2130+
in (freq, Just <$> genPerasCert)
21352131
]
21362132
return
21372133
TestBody
21382134
{ tbForkNo = forkNo
21392135
, tbIsValid = isValid
2140-
, tbPerasCertRound = perasCertRound
2136+
, tbPerasCert = perasCert
2137+
}
2138+
2139+
genPerasCert :: Gen (PerasCert TestBlock)
2140+
genPerasCert = do
2141+
let maxRoundNo =
2142+
case Model.roundNoOfLatestCertSeen dbModel of
2143+
Nothing -> 0
2144+
Just (PerasRoundNo r) -> r + 1
2145+
roundNo <-
2146+
PerasRoundNo <$> choose (0, maxRoundNo)
2147+
boostedBlock <-
2148+
-- NOTE: we don't care about this boosted block, it could be @Genesis@
2149+
blockPoint <$> genSuccOfCurrentChainTip
2150+
pure
2151+
MockPerasCert
2152+
{ mockCertRound = roundNo
2153+
, mockCertBlock = boostedBlock
21412154
}
21422155

21432156
-- | Generate a random security parameter (k)

ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/VolatileDB/StateMachine.hs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import GHC.Generics
4949
import GHC.Stack
5050
import qualified Generics.SOP as SOP
5151
import Ouroboros.Consensus.Block
52+
import Ouroboros.Consensus.Peras.Cert.Mock (MockPerasCert (..))
5253
import Ouroboros.Consensus.Storage.Common
5354
import Ouroboros.Consensus.Storage.VolatileDB
5455
import Ouroboros.Consensus.Storage.VolatileDB.Impl.Types (FileId)
@@ -392,7 +393,7 @@ generatorCmdImpl Model{..} =
392393
TestBody
393394
<$> arbitrary
394395
<*> arbitrary
395-
<*> liftArbitrary (PerasRoundNo <$> arbitrary)
396+
<*> liftArbitrary genPerasCert
396397
prevHash <-
397398
frequency
398399
[ (1, return GenesisHash)
@@ -407,6 +408,16 @@ generatorCmdImpl Model{..} =
407408
let clen = ChainLength (fromIntegral (unBlockNo no))
408409
return $ mkBlock canContainEBB body prevHash slot no clen ebb
409410

411+
genPerasCert :: Gen (PerasCert Block)
412+
genPerasCert = do
413+
mockCertRound <- PerasRoundNo <$> arbitrary
414+
mockCertBlock <- blockPoint <$> genRandomBlock
415+
pure $
416+
MockPerasCert
417+
{ mockCertRound
418+
, mockCertBlock
419+
}
420+
410421
genHash :: Gen (HeaderHash Block)
411422
genHash =
412423
frequency

0 commit comments

Comments
 (0)