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

Commit

Permalink
CBR-504: Fix/improve block verify property tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
erikd committed Feb 18, 2019
1 parent cda4d02 commit 693990d
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 73 deletions.
6 changes: 6 additions & 0 deletions chain/src/Pos/Chain/Block/Header.hs
Expand Up @@ -11,6 +11,7 @@ module Pos.Chain.Block.Header
, _BlockHeaderGenesis
, _BlockHeaderMain
, verifyBlockHeader
, headerLeaderKey
, headerLastSlotInfo

, HeaderHash
Expand Down Expand Up @@ -687,6 +688,11 @@ mainHeaderSlot = gbhConsensus . mcdSlot
mainHeaderLeaderKey :: Lens' MainBlockHeader PublicKey
mainHeaderLeaderKey = gbhConsensus . mcdLeaderKey

headerLeaderKey :: BlockHeader -> Maybe PublicKey
headerLeaderKey = \case
BlockHeaderGenesis _ -> Nothing
BlockHeaderMain mbh -> Just $ view mainHeaderLeaderKey mbh

-- | Lens from 'MainBlockHeader' to 'ChainDifficulty'.
mainHeaderDifficulty :: Lens' MainBlockHeader ChainDifficulty
mainHeaderDifficulty = gbhConsensus . mcdDifficulty
Expand Down
53 changes: 31 additions & 22 deletions chain/src/Pos/Chain/Block/Logic/Integrity.hs
Expand Up @@ -204,37 +204,46 @@ verifyHeader pm VerifyHeaderParams {..} h =
-- a slot leader schedule as it would for the `OBFT ObftStrict`
-- and `Original` cases.
ObftLenientLeaders ldrs blkSecurityParam lastBlkSlots ->
[ ( (blockSlotLeader `elem` ldrs)
, sformat ("slot leader who published block, "%build%", is not an acceptable leader.")
blockSlotLeader)
, ( (obftLeaderCanMint blockSlotLeader blkSecurityParam lastBlkSlots)
, sformat ("slot leader who published block, "%build%", has minted too many blocks in the past "%build%" slots.")
[ ( blockSlotLeader `elem` ldrs
, sformat ("ObftLenient: slot leader who published block, "%build%", is not an acceptable leader.")
blockSlotLeader
(getBlockCount blkSecurityParam))
]
)
, ( obftLeaderCanMint blockSlotLeader blkSecurityParam lastBlkSlots
, sformat ("ObftLenient: slot leader who published block, "%build%", has minted too many blocks ("% build %") in the past "%build%" slots.")
blockSlotLeader
(blocksMintedByLeaderInLastKSlots blockSlotLeader $ getOldestFirst lastBlkSlots)
(getBlockCount blkSecurityParam)
)
]

ObftStrictLeaders ldrs ->
[ ( (Just blockSlotLeader == (scheduleSlotLeader ldrs))
, sformat ("ObftStrict: slot leader from schedule, "%build%", is different from slot leader who published block, "%build%". slotIndex: "%build%", leaders: "%shown)
(scheduleSlotLeader ldrs)
blockSlotLeader)
]
if isNothing (scheduleSlotLeader ldrs)
then [ (isJust (scheduleSlotLeader ldrs), "ObftStrict: scheduled slot leader is missing") ]
else
[ ( Just blockSlotLeader == scheduleSlotLeader ldrs
, sformat ("ObftStrict: slot leader from schedule, "%build%", is different from slot leader who published block, "%build%".")
(scheduleSlotLeader ldrs)
blockSlotLeader
)
]

OriginalLeaders ldrs ->
[ ( (Just blockSlotLeader == (scheduleSlotLeader ldrs))
, sformat ("Original: slot leader from schedule, "%build%", is different from slot leader who published block, "%build%". slotIndex: "%build%", leaders: "%shown)
(scheduleSlotLeader ldrs)
blockSlotLeader)
]
if isNothing (scheduleSlotLeader ldrs)
then [ (isJust (scheduleSlotLeader ldrs), "ObftStrict: scheduled slot leader is missing") ]
else
[ ( Just blockSlotLeader == scheduleSlotLeader ldrs
, sformat ("Original: slot leader from schedule, "%build%", is different from slot leader who published block, "%build%".")
(scheduleSlotLeader ldrs)
blockSlotLeader
)
]
where
-- Determine whether the leader is allowed to mint a block based on
-- whether blocksMintedByLeaderInLastKSlots <= floor (k * t)
obftLeaderCanMint :: AddressHash PublicKey -> BlockCount -> OldestFirst [] LastSlotInfo -> Bool
obftLeaderCanMint leaderAddrHash
blkSecurityParam
(OldestFirst lastBlkSlots) =
(blocksMintedByLeaderInLastKSlots leaderAddrHash lastBlkSlots)
<= (leaderMintThreshold blkSecurityParam)
obftLeaderCanMint leaderAddrHash blkSecurityParam (OldestFirst lastBlkSlots) =
blocksMintedByLeaderInLastKSlots leaderAddrHash lastBlkSlots
<= leaderMintThreshold blkSecurityParam

blocksMintedByLeaderInLastKSlots :: AddressHash PublicKey -> [LastSlotInfo] -> Int
blocksMintedByLeaderInLastKSlots leaderAddrHash lastBlkSlots =
Expand Down

0 comments on commit 693990d

Please sign in to comment.