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

Commit 2307fbc

Browse files
iohk-bors[bot]erikdintricate
committed
Merge #4081
4081: CBR-504: Fix/improve block validation tests r=erikd a=erikd Co-authored-by: Erik de Castro Lopo <[email protected]> Co-authored-by: Luke Nadur <[email protected]>
2 parents a083aec + 71f9d8e commit 2307fbc

File tree

8 files changed

+240
-146
lines changed

8 files changed

+240
-146
lines changed

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

+14-8
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ module Pos.Chain.Block.Block
5454
, mainBlockUpdatePayload
5555
, mainBlockAttributes
5656
, verifyMainBlock
57+
58+
, blockLastSlotInfo
5759
) where
5860

5961
import Universum
@@ -77,18 +79,19 @@ import Pos.Chain.Block.Header (BlockHeader (..), BlockSignature (..),
7779
MainConsensusData (..), blockHeaderHash, gbhBodyProof,
7880
gbhConsensus, gbhPrevBlock, genHeaderAttributes,
7981
genHeaderDifficulty, genHeaderEpoch, genHeaderProof,
80-
mainHeaderAttributes, mainHeaderBlockVersion,
81-
mainHeaderDifficulty, mainHeaderEBDataProof,
82-
mainHeaderLeaderKey, mainHeaderProof, mainHeaderSignature,
83-
mainHeaderSlot, mainHeaderSoftwareVersion,
84-
mkGenesisHeader, mkMainHeaderExplicit,
85-
verifyMainBlockHeader)
82+
headerLastSlotInfo, mainHeaderAttributes,
83+
mainHeaderBlockVersion, mainHeaderDifficulty,
84+
mainHeaderEBDataProof, mainHeaderLeaderKey,
85+
mainHeaderProof, mainHeaderSignature, mainHeaderSlot,
86+
mainHeaderSoftwareVersion, mkGenesisHeader,
87+
mkMainHeaderExplicit, verifyMainBlockHeader)
8688
import Pos.Chain.Block.Main (BlockBodyAttributes,
8789
BlockHeaderAttributes, MainBody (..),
8890
MainExtraBodyData (..), MainExtraHeaderData (..),
8991
MainProof (..), checkMainProof, mbDlgPayload,
9092
mbSscPayload, mbTxPayload, mbTxs, mbUpdatePayload,
9193
mebAttributes, verifyMainBody)
94+
import Pos.Chain.Block.Slog.Types (LastSlotInfo (..))
9295
import Pos.Chain.Delegation.HeavyDlgIndex (ProxySKBlockInfo)
9396
import Pos.Chain.Delegation.Payload (DlgPayload)
9497
import Pos.Chain.Genesis.Config as Genesis (Config (..))
@@ -107,11 +110,10 @@ import Pos.Core.Attributes (mkAttributes)
107110
import Pos.Core.Common (ChainDifficulty, HasDifficulty (..),
108111
SlotLeaders, slotLeadersF)
109112
import Pos.Core.Slotting (EpochIndex, HasEpochIndex (..),
110-
HasEpochOrSlot (..), SlotId (..))
113+
HasEpochOrSlot (..), SlotCount, SlotId (..))
111114
import Pos.Crypto (Hash, ProtocolMagic, PublicKey, SecretKey, hash)
112115
import Pos.Util.Some (Some (..))
113116

114-
115117
--------------------------------------------------------------------------------
116118
-- Block
117119
--------------------------------------------------------------------------------
@@ -133,6 +135,10 @@ getBlockHeader = \case
133135
Left gb -> BlockHeaderGenesis (_gbHeader gb)
134136
Right mb -> BlockHeaderMain (_gbHeader mb)
135137

138+
blockLastSlotInfo :: SlotCount -> Block -> Maybe LastSlotInfo
139+
blockLastSlotInfo slotCount =
140+
headerLastSlotInfo slotCount . getBlockHeader
141+
136142
-- | Verify a Block in isolation.
137143
verifyBlockInternal
138144
:: MonadError Text m

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

+20-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module Pos.Chain.Block.Header
1111
, _BlockHeaderGenesis
1212
, _BlockHeaderMain
1313
, verifyBlockHeader
14+
, headerLeaderKey
15+
, headerLastSlotInfo
1416

1517
, HeaderHash
1618
, headerHashF
@@ -100,15 +102,16 @@ import Pos.Chain.Update.SoftwareVersion (HasSoftwareVersion (..),
100102
import Pos.Core.Attributes (mkAttributes)
101103
import Pos.Core.Common (ChainDifficulty, HasDifficulty (..))
102104
import Pos.Core.Slotting (EpochIndex (..), EpochOrSlot (..),
103-
HasEpochIndex (..), HasEpochOrSlot (..), SlotId (..),
104-
slotIdF)
105+
HasEpochIndex (..), HasEpochOrSlot (..), SlotCount (..),
106+
SlotId (..), flattenSlotId, slotIdF)
105107
import Pos.Crypto (Hash, ProtocolMagic (..), ProtocolMagicId (..),
106108
PublicKey, SecretKey, SignTag (..), Signature, checkSig,
107109
hashHexF, isSelfSignedPsk, proxySign, proxyVerify,
108110
psigPsk, sign, toPublic, unsafeHash)
109111
import Pos.Util.Some (Some, applySome)
110112
import Pos.Util.Util (cborError, cerealError)
111113

114+
import Pos.Chain.Block.Slog.Types (LastSlotInfo (..))
112115

113116
--------------------------------------------------------------------------------
114117
-- GenesisBlock ∪ MainBlock
@@ -173,6 +176,16 @@ verifyBlockHeader
173176
verifyBlockHeader _ (BlockHeaderGenesis _) = pure ()
174177
verifyBlockHeader pm (BlockHeaderMain bhm) = verifyMainBlockHeader pm bhm
175178

179+
headerLastSlotInfo :: SlotCount -> BlockHeader -> Maybe LastSlotInfo
180+
headerLastSlotInfo slotCount = \case
181+
BlockHeaderGenesis _ -> Nothing
182+
BlockHeaderMain mbh -> Just $ convert mbh
183+
where
184+
convert :: MainBlockHeader -> LastSlotInfo
185+
convert bh =
186+
LastSlotInfo
187+
(flattenSlotId slotCount . _mcdSlot $ _gbhConsensus bh)
188+
(_mcdLeaderKey $ _gbhConsensus bh)
176189

177190
--------------------------------------------------------------------------------
178191
-- HeaderHash
@@ -675,6 +688,11 @@ mainHeaderSlot = gbhConsensus . mcdSlot
675688
mainHeaderLeaderKey :: Lens' MainBlockHeader PublicKey
676689
mainHeaderLeaderKey = gbhConsensus . mcdLeaderKey
677690

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

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

+43-28
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ import Pos.Chain.Block.Slog (ConsensusEraLeaders (..),
4040
import Pos.Chain.Genesis as Genesis (Config (..))
4141
import Pos.Chain.Txp (TxValidationRules)
4242
import Pos.Chain.Update (BlockVersionData (..), ConsensusEra (..))
43-
import Pos.Core (BlockCount (..), ChainDifficulty, EpochOrSlot (..),
44-
HasDifficulty (..), HasEpochOrSlot (..),
43+
import Pos.Core (AddressHash, BlockCount (..), ChainDifficulty,
44+
EpochOrSlot (..), HasDifficulty (..), HasEpochOrSlot (..),
4545
LocalSlotIndex (..), SlotId (..), addressHash,
4646
getSlotIndex)
4747
import Pos.Core.Attributes (areAttributesKnown)
4848
import Pos.Core.Chrono (NewestFirst (..), OldestFirst (..))
4949
import Pos.Crypto (ProtocolMagic (..), ProtocolMagicId (..),
50-
getProtocolMagic)
50+
PublicKey, getProtocolMagic)
5151

5252
----------------------------------------------------------------------------
5353
-- Header
@@ -135,6 +135,7 @@ verifyHeader pm VerifyHeaderParams {..} h =
135135
("slots are not monotonic ("%build%" >= "%build%")")
136136
oldEOS newEOS
137137
)
138+
checkProtocolMagicId :: [(Bool, Text)]
138139
checkProtocolMagicId =
139140
[ ( getProtocolMagicId pm == blockHeaderProtocolMagicId h
140141
, sformat
@@ -143,6 +144,7 @@ verifyHeader pm VerifyHeaderParams {..} h =
143144
(getProtocolMagic pm)
144145
)
145146
]
147+
checkSize :: [(Bool, Text)]
146148
checkSize =
147149
case vhpMaxSize of
148150
Nothing -> mempty
@@ -186,6 +188,7 @@ verifyHeader pm VerifyHeaderParams {..} h =
186188
]
187189

188190
-- CHECK: Checks that the block leader is the expected one.
191+
relatedToLeaders :: ConsensusEraLeaders -> [(Bool, Text)]
189192
relatedToLeaders leaders =
190193
case h of
191194
BlockHeaderGenesis _ -> []
@@ -201,50 +204,62 @@ verifyHeader pm VerifyHeaderParams {..} h =
201204
-- a slot leader schedule as it would for the `OBFT ObftStrict`
202205
-- and `Original` cases.
203206
ObftLenientLeaders ldrs blkSecurityParam lastBlkSlots ->
204-
[ ( (blockSlotLeader `elem` ldrs)
205-
, sformat ("slot leader who published block, "%build%", is not an acceptable leader.")
206-
blockSlotLeader)
207-
, ( (obftLeaderCanMint blockSlotLeader blkSecurityParam lastBlkSlots)
208-
, 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.")
209209
blockSlotLeader
210-
(getBlockCount blkSecurityParam))
211-
]
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+
]
212218

213219
ObftStrictLeaders ldrs ->
214-
[ ( (Just blockSlotLeader == (scheduleSlotLeader ldrs))
215-
, sformat ("slot leader from schedule, "%build%", is different from slot leader who published block, "%build%".")
216-
(scheduleSlotLeader ldrs)
217-
blockSlotLeader)
218-
]
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+
]
219229

220230
OriginalLeaders ldrs ->
221-
[ ( (Just blockSlotLeader == (scheduleSlotLeader ldrs))
222-
, sformat ("slot leader from schedule, "%build%", is different from slot leader who published block, "%build%".")
223-
(scheduleSlotLeader ldrs)
224-
blockSlotLeader)
225-
]
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+
]
226240
where
227241
-- Determine whether the leader is allowed to mint a block based on
228242
-- whether blocksMintedByLeaderInLastKSlots <= floor (k * t)
229-
obftLeaderCanMint leaderAddrHash
230-
blkSecurityParam
231-
(OldestFirst lastBlkSlots) =
232-
(blocksMintedByLeaderInLastKSlots leaderAddrHash lastBlkSlots)
233-
<= (leaderMintThreshold blkSecurityParam)
234-
--
243+
obftLeaderCanMint :: AddressHash PublicKey -> BlockCount -> OldestFirst [] LastSlotInfo -> Bool
244+
obftLeaderCanMint leaderAddrHash blkSecurityParam (OldestFirst lastBlkSlots) =
245+
blocksMintedByLeaderInLastKSlots leaderAddrHash lastBlkSlots
246+
<= leaderMintThreshold blkSecurityParam
247+
248+
blocksMintedByLeaderInLastKSlots :: AddressHash PublicKey -> [LastSlotInfo] -> Int
235249
blocksMintedByLeaderInLastKSlots leaderAddrHash lastBlkSlots =
236250
length $
237251
filter (\lsi -> leaderAddrHash == (addressHash $ lsiLeaderPubkeyHash lsi))
238252
lastBlkSlots
239-
--
253+
240254
leaderMintThreshold :: BlockCount -> Int
241255
leaderMintThreshold blkSecurityParam =
242256
let k = getBlockCount blkSecurityParam
243257
in floor $ (fromIntegral k :: Double) * t
244-
--
258+
245259
t :: Double
246260
t = 0.22
247261

262+
verifyNoUnknown :: BlockHeader -> [(Bool, Text)]
248263
verifyNoUnknown (BlockHeaderGenesis genH) =
249264
let attrs = genH ^. gbhExtra . gehAttributes
250265
in [ ( areAttributesKnown attrs

0 commit comments

Comments
 (0)