Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 8868619

Browse files
committed
CBR-504: Fix/improve block verify property tests
The existing tests failed to test the validity of the key signing each block and there were no other tests for that. Since we are in the process of adding two new consensus validation algorithms (OBDT strict and lenient) we should add this now.
1 parent cda4d02 commit 8868619

File tree

3 files changed

+154
-73
lines changed

3 files changed

+154
-73
lines changed

chain/src/Pos/Chain/Block/Header.hs

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module Pos.Chain.Block.Header
1111
, _BlockHeaderGenesis
1212
, _BlockHeaderMain
1313
, verifyBlockHeader
14+
, headerLeaderKey
1415
, headerLastSlotInfo
1516

1617
, HeaderHash
@@ -687,6 +688,11 @@ mainHeaderSlot = gbhConsensus . mcdSlot
687688
mainHeaderLeaderKey :: Lens' MainBlockHeader PublicKey
688689
mainHeaderLeaderKey = gbhConsensus . mcdLeaderKey
689690

691+
headerLeaderKey :: BlockHeader -> Maybe PublicKey
692+
headerLeaderKey = \case
693+
BlockHeaderGenesis _ -> Nothing
694+
BlockHeaderMain mbh -> Just $ view mainHeaderLeaderKey mbh
695+
690696
-- | Lens from 'MainBlockHeader' to 'ChainDifficulty'.
691697
mainHeaderDifficulty :: Lens' MainBlockHeader ChainDifficulty
692698
mainHeaderDifficulty = gbhConsensus . mcdDifficulty

chain/src/Pos/Chain/Block/Logic/Integrity.hs

+31-22
Original file line numberDiff line numberDiff line change
@@ -204,37 +204,46 @@ verifyHeader pm VerifyHeaderParams {..} h =
204204
-- a slot leader schedule as it would for the `OBFT ObftStrict`
205205
-- and `Original` cases.
206206
ObftLenientLeaders ldrs blkSecurityParam lastBlkSlots ->
207-
[ ( (blockSlotLeader `elem` ldrs)
208-
, sformat ("slot leader who published block, "%build%", is not an acceptable leader.")
209-
blockSlotLeader)
210-
, ( (obftLeaderCanMint blockSlotLeader blkSecurityParam lastBlkSlots)
211-
, sformat ("slot leader who published block, "%build%", has minted too many blocks in the past "%build%" slots.")
207+
[ ( blockSlotLeader `elem` ldrs
208+
, sformat ("ObftLenient: slot leader who published block, "%build%", is not an acceptable leader.")
212209
blockSlotLeader
213-
(getBlockCount blkSecurityParam))
214-
]
210+
)
211+
, ( obftLeaderCanMint blockSlotLeader blkSecurityParam lastBlkSlots
212+
, sformat ("ObftLenient: slot leader who published block, "%build%", has minted too many blocks ("% build %") in the past "%build%" slots.")
213+
blockSlotLeader
214+
(blocksMintedByLeaderInLastKSlots blockSlotLeader $ getOldestFirst lastBlkSlots)
215+
(getBlockCount blkSecurityParam)
216+
)
217+
]
215218

216219
ObftStrictLeaders ldrs ->
217-
[ ( (Just blockSlotLeader == (scheduleSlotLeader ldrs))
218-
, sformat ("ObftStrict: slot leader from schedule, "%build%", is different from slot leader who published block, "%build%". slotIndex: "%build%", leaders: "%shown)
219-
(scheduleSlotLeader ldrs)
220-
blockSlotLeader)
221-
]
220+
if isNothing (scheduleSlotLeader ldrs)
221+
then [ (isJust (scheduleSlotLeader ldrs), "ObftStrict: scheduled slot leader is missing") ]
222+
else
223+
[ ( Just blockSlotLeader == scheduleSlotLeader ldrs
224+
, sformat ("ObftStrict: slot leader from schedule, "%build%", is different from slot leader who published block, "%build%".")
225+
(scheduleSlotLeader ldrs)
226+
blockSlotLeader
227+
)
228+
]
222229

223230
OriginalLeaders ldrs ->
224-
[ ( (Just blockSlotLeader == (scheduleSlotLeader ldrs))
225-
, sformat ("Original: slot leader from schedule, "%build%", is different from slot leader who published block, "%build%". slotIndex: "%build%", leaders: "%shown)
226-
(scheduleSlotLeader ldrs)
227-
blockSlotLeader)
228-
]
231+
if isNothing (scheduleSlotLeader ldrs)
232+
then [ (isJust (scheduleSlotLeader ldrs), "ObftStrict: scheduled slot leader is missing") ]
233+
else
234+
[ ( Just blockSlotLeader == scheduleSlotLeader ldrs
235+
, sformat ("Original: slot leader from schedule, "%build%", is different from slot leader who published block, "%build%".")
236+
(scheduleSlotLeader ldrs)
237+
blockSlotLeader
238+
)
239+
]
229240
where
230241
-- Determine whether the leader is allowed to mint a block based on
231242
-- whether blocksMintedByLeaderInLastKSlots <= floor (k * t)
232243
obftLeaderCanMint :: AddressHash PublicKey -> BlockCount -> OldestFirst [] LastSlotInfo -> Bool
233-
obftLeaderCanMint leaderAddrHash
234-
blkSecurityParam
235-
(OldestFirst lastBlkSlots) =
236-
(blocksMintedByLeaderInLastKSlots leaderAddrHash lastBlkSlots)
237-
<= (leaderMintThreshold blkSecurityParam)
244+
obftLeaderCanMint leaderAddrHash blkSecurityParam (OldestFirst lastBlkSlots) =
245+
blocksMintedByLeaderInLastKSlots leaderAddrHash lastBlkSlots
246+
<= leaderMintThreshold blkSecurityParam
238247

239248
blocksMintedByLeaderInLastKSlots :: AddressHash PublicKey -> [LastSlotInfo] -> Int
240249
blocksMintedByLeaderInLastKSlots leaderAddrHash lastBlkSlots =

0 commit comments

Comments
 (0)