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

Commit cda4d02

Browse files
committed
Convert toLastSlotInfo to headerLastSlotInfo/blockLastSlotInfo
1 parent 9549a08 commit cda4d02

File tree

4 files changed

+35
-39
lines changed

4 files changed

+35
-39
lines changed

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

+12-5
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,9 +79,10 @@ 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,
82+
headerLastSlotInfo, mainHeaderAttributes,
83+
mainHeaderBlockVersion, mainHeaderDifficulty,
84+
mainHeaderEBDataProof, mainHeaderLeaderKey,
85+
mainHeaderProof, mainHeaderSignature,
8386
mainHeaderSlot, mainHeaderSoftwareVersion,
8487
mkGenesisHeader, mkMainHeaderExplicit,
8588
verifyMainBlockHeader)
@@ -89,6 +92,7 @@ import Pos.Chain.Block.Main (BlockBodyAttributes,
8992
MainProof (..), checkMainProof, mbDlgPayload,
9093
mbSscPayload, mbTxPayload, mbTxs, mbUpdatePayload,
9194
mebAttributes, verifyMainBody)
95+
import Pos.Chain.Block.Slog.Types (LastSlotInfo (..))
9296
import Pos.Chain.Delegation.HeavyDlgIndex (ProxySKBlockInfo)
9397
import Pos.Chain.Delegation.Payload (DlgPayload)
9498
import Pos.Chain.Genesis.Config as Genesis (Config (..))
@@ -107,11 +111,10 @@ import Pos.Core.Attributes (mkAttributes)
107111
import Pos.Core.Common (ChainDifficulty, HasDifficulty (..),
108112
SlotLeaders, slotLeadersF)
109113
import Pos.Core.Slotting (EpochIndex, HasEpochIndex (..),
110-
HasEpochOrSlot (..), SlotId (..))
114+
HasEpochOrSlot (..), SlotId (..), SlotCount)
111115
import Pos.Crypto (Hash, ProtocolMagic, PublicKey, SecretKey, hash)
112116
import Pos.Util.Some (Some (..))
113117

114-
115118
--------------------------------------------------------------------------------
116119
-- Block
117120
--------------------------------------------------------------------------------
@@ -133,6 +136,10 @@ getBlockHeader = \case
133136
Left gb -> BlockHeaderGenesis (_gbHeader gb)
134137
Right mb -> BlockHeaderMain (_gbHeader mb)
135138

139+
blockLastSlotInfo :: SlotCount -> Block -> Maybe LastSlotInfo
140+
blockLastSlotInfo slotCount =
141+
headerLastSlotInfo slotCount . getBlockHeader
142+
136143
-- | Verify a Block in isolation.
137144
verifyBlockInternal
138145
:: MonadError Text m

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

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

1516
, HeaderHash
1617
, headerHashF
@@ -100,15 +101,16 @@ import Pos.Chain.Update.SoftwareVersion (HasSoftwareVersion (..),
100101
import Pos.Core.Attributes (mkAttributes)
101102
import Pos.Core.Common (ChainDifficulty, HasDifficulty (..))
102103
import Pos.Core.Slotting (EpochIndex (..), EpochOrSlot (..),
103-
HasEpochIndex (..), HasEpochOrSlot (..), SlotId (..),
104-
slotIdF)
104+
HasEpochIndex (..), HasEpochOrSlot (..), SlotCount (..),
105+
SlotId (..), flattenSlotId, slotIdF)
105106
import Pos.Crypto (Hash, ProtocolMagic (..), ProtocolMagicId (..),
106107
PublicKey, SecretKey, SignTag (..), Signature, checkSig,
107108
hashHexF, isSelfSignedPsk, proxySign, proxyVerify,
108109
psigPsk, sign, toPublic, unsafeHash)
109110
import Pos.Util.Some (Some, applySome)
110111
import Pos.Util.Util (cborError, cerealError)
111112

113+
import Pos.Chain.Block.Slog.Types (LastSlotInfo (..))
112114

113115
--------------------------------------------------------------------------------
114116
-- GenesisBlock ∪ MainBlock
@@ -173,6 +175,16 @@ verifyBlockHeader
173175
verifyBlockHeader _ (BlockHeaderGenesis _) = pure ()
174176
verifyBlockHeader pm (BlockHeaderMain bhm) = verifyMainBlockHeader pm bhm
175177

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

177189
--------------------------------------------------------------------------------
178190
-- HeaderHash

db/src/Pos/DB/Block/Slog/Logic.hs

+8-19
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ import Serokell.Util (Color (Red), colorize)
3333
import Serokell.Util.Verify (formatAllErrors, verResToMonadError)
3434

3535
import Pos.Chain.Block (Block, Blund, ConsensusEraLeaders (..),
36-
HasSlogGState, LastBlkSlots, LastSlotInfo (..), MainBlock,
36+
HasSlogGState, LastBlkSlots, LastSlotInfo (..),
3737
SlogUndo (..), genBlockLeaders, headerHash, headerHashG,
38-
mainBlockLeaderKey, mainBlockSlot, prevBlockL,
39-
verifyBlocks)
38+
prevBlockL, blockLastSlotInfo, verifyBlocks)
4039
import Pos.Chain.Genesis as Genesis (Config (..),
4140
configBlkSecurityParam, configEpochSlots,
4241
configGenesisWStakeholders, configK)
@@ -45,14 +44,14 @@ import Pos.Chain.Update (BlockVersion (..), ConsensusEra (..),
4544
ObftConsensusStrictness (..), UpdateConfiguration,
4645
lastKnownBlockVersion)
4746
import Pos.Core (BlockCount, difficultyL, epochIndexL,
48-
epochOrSlotToEpochIndex, flattenSlotId, kEpochSlots,
47+
epochOrSlotToEpochIndex, kEpochSlots,
4948
pcBlkSecurityParam)
5049
import Pos.Core.Chrono (NE, NewestFirst (getNewestFirst),
5150
OldestFirst (..), toOldestFirst, _OldestFirst)
5251
import Pos.Core.Exception (assertionFailed, reportFatalError)
5352
import Pos.Core.NetworkMagic (NetworkMagic (..), makeNetworkMagic)
54-
import Pos.Core.Slotting (HasEpochIndex, MonadSlots, SlotCount,
55-
SlotId (..), getEpochOrSlot)
53+
import Pos.Core.Slotting (HasEpochIndex, MonadSlots, SlotId (..),
54+
getEpochOrSlot)
5655
import Pos.DB (SomeBatchOp (..))
5756
import Pos.DB.Block.BListener (MonadBListener (..))
5857
import qualified Pos.DB.Block.GState.BlockExtra as GS
@@ -221,7 +220,7 @@ slogVerifyBlocks genesisConfig curSlot blocks = runExceptT $ do
221220
-- these slots will be added if we apply all blocks
222221
let newSlots :: [LastSlotInfo]
223222
newSlots =
224-
mapMaybe (toLastSlotInfo (configEpochSlots genesisConfig)) $ toList blocks
223+
mapMaybe (blockLastSlotInfo (configEpochSlots genesisConfig)) $ toList blocks
225224
let combinedSlots :: LastBlkSlots
226225
combinedSlots = lastSlots & _Wrapped %~ (<> newSlots)
227226
-- these slots will be removed if we apply all blocks, because we store
@@ -246,16 +245,6 @@ slogVerifyBlocks genesisConfig curSlot blocks = runExceptT $ do
246245
-- 'slogUndo' is the same as the size of 'blocks'.
247246
return $ over _Wrapped NE.fromList $ map (SlogUndo . fmap lsiFlatSlotId) slogUndo
248247

249-
toLastSlotInfo :: SlotCount -> Block -> Maybe LastSlotInfo
250-
toLastSlotInfo slotCount blk =
251-
convert <$> rightToMaybe blk
252-
where
253-
convert :: MainBlock -> LastSlotInfo
254-
convert b =
255-
LastSlotInfo
256-
(flattenSlotId slotCount $ view mainBlockSlot b)
257-
(view mainBlockLeaderKey b)
258-
259248
-- | Set of constraints necessary to apply/rollback blocks in Slog.
260249
type MonadSlogApply ctx m =
261250
( MonadSlogBase ctx m
@@ -328,7 +317,7 @@ slogApplyBlocks nm k (ShouldCallBListener callBListener) blunds = do
328317
fmap (GS.SetInMainChain True . view headerHashG . fst) blunds
329318

330319
newSlots :: [LastSlotInfo]
331-
newSlots = mapMaybe (toLastSlotInfo (kEpochSlots k)) $ toList blocks
320+
newSlots = mapMaybe (blockLastSlotInfo (kEpochSlots k)) $ toList blocks
332321

333322
newLastSlots :: LastBlkSlots -> LastBlkSlots
334323
newLastSlots = OldestFirst . updateLastSlots . getOldestFirst
@@ -417,7 +406,7 @@ slogRollbackBlocks genesisConfig (BypassSecurityCheck bypassSecurity) (ShouldCal
417406

418407
lastSlotsToAppend :: [LastSlotInfo]
419408
lastSlotsToAppend =
420-
mapMaybe (toLastSlotInfo (configEpochSlots genesisConfig) . fst)
409+
mapMaybe (blockLastSlotInfo (configEpochSlots genesisConfig) . fst)
421410
$ toList (toOldestFirst blunds)
422411

423412
newLastSlots :: LastBlkSlots -> LastBlkSlots

utxo/src/UTxO/Verify.hs

+1-13
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ slogVerifyBlocks era curSlot txValRules leaders lastSlots blocks = do
339339
-- we can remove one of the last slots stored in
340340
-- 'BlockExtra'. This removed slot must be put into 'SlogUndo'.
341341
-- these slots will be added if we apply all blocks
342-
let newSlots = mapMaybe (toLastSlotInfo dummyEpochSlots) $ toList blocks
342+
let newSlots = mapMaybe (blockLastSlotInfo dummyEpochSlots) $ toList blocks
343343
let combinedSlots :: LastBlkSlots
344344
combinedSlots = lastSlots & _Wrapped %~ (<> newSlots)
345345
-- these slots will be removed if we apply all blocks, because we store
@@ -363,18 +363,6 @@ slogVerifyBlocks era curSlot txValRules leaders lastSlots blocks = do
363363
-- 'slogUndo' is the same as the size of 'blocks'.
364364
return $ over _Wrapped NE.fromList $ map (SlogUndo . fmap lsiFlatSlotId) slogUndo
365365

366-
367-
368-
toLastSlotInfo :: SlotCount -> Block -> Maybe LastSlotInfo
369-
toLastSlotInfo slotCount blk =
370-
convert <$> rightToMaybe blk
371-
where
372-
convert :: MainBlock -> LastSlotInfo
373-
convert b =
374-
LastSlotInfo
375-
(flattenSlotId slotCount $ view mainBlockSlot b)
376-
(view mainBlockLeaderKey b)
377-
378366
-- | Verify block transactions
379367
--
380368
-- Adapted from 'Pos.DB.Txp.Logic.Global.verifyBlocks'.

0 commit comments

Comments
 (0)