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

Commit 54ab58c

Browse files
authored
Merge pull request #3216 from input-output-hk/coot/cbr-96-benchmarks
[CBR-96] Block verification benchmarks
2 parents eab0a6a + 2502a8d commit 54ab58c

File tree

42 files changed

+1110
-122
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1110
-122
lines changed

block/cardano-sl-block.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ library
4343
, ether
4444
, formatting
4545
, lens
46+
, log-warper
47+
, mmorph
4648
, mtl
4749
, reflection
4850
, safe-exceptions

block/src/Pos/Block/Error.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ data ApplyBlocksException
5151
HeaderHash -- attempted tip
5252
| ApplyBlocksVerifyFailure VerifyBlocksException
5353
| ApplyBlocksError Text -- other error (not covered by constructors above)
54-
deriving (Show)
54+
deriving (Show, Generic)
55+
56+
instance NFData ApplyBlocksException
5557

5658
renderApplyBlocksException :: ApplyBlocksException -> Builder
5759
renderApplyBlocksException = \case

block/src/Pos/Block/Logic/Integrity.hs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module Pos.Block.Logic.Integrity
1212

1313
-- * Block
1414
, VerifyBlockParams (..)
15+
, verifyBlock
1516
, verifyBlocks
1617
) where
1718

@@ -61,7 +62,9 @@ data VerifyHeaderParams = VerifyHeaderParams
6162
-- ^ Maximal allowed header size. It's applied to 'BlockHeader'.
6263
, vhpVerifyNoUnknown :: !Bool
6364
-- ^ Check that header has no unknown attributes.
64-
} deriving (Eq, Show)
65+
} deriving (Eq, Show, Generic)
66+
67+
instance NFData VerifyHeaderParams
6568

6669
verifyFromEither :: Text -> Either Text b -> VerificationRes
6770
verifyFromEither txt (Left reason) = verifyGeneric [(False, txt <> ": " <> reason)]
@@ -246,7 +249,9 @@ data VerifyBlockParams = VerifyBlockParams
246249
-- is either main or genesis block).
247250
, vbpVerifyNoUnknown :: !Bool
248251
-- ^ Check that block has no unknown attributes.
249-
}
252+
} deriving (Generic)
253+
254+
instance NFData VerifyBlockParams
250255

251256
-- CHECK: @verifyBlock
252257
-- | Check predicates defined by VerifyBlockParams.

block/test/Test/Pos/Block/BlockSpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import Test.Pos.Crypto.Dummy (dummyProtocolMagic)
4242

4343
-- This tests are quite slow, hence max success is at most 20.
4444
spec :: Spec
45-
spec = withGenesisSpec 0 defaultCoreConfiguration $ \_ ->
45+
spec = withGenesisSpec 0 defaultCoreConfiguration id $ \_ ->
4646
describe "Block properties" $ modifyMaxSuccess (min 20) $ do
4747
describe "mkMainHeader" $ do
4848
prop mainHeaderFormationDesc mainHeaderFormation

block/test/Test/Pos/Block/CborSpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Test.Pos.Block.Arbitrary ()
1818
import Test.Pos.Core.Arbitrary ()
1919

2020
spec :: Spec
21-
spec = withGenesisSpec 0 defaultCoreConfiguration $ \_ ->
21+
spec = withGenesisSpec 0 defaultCoreConfiguration id $ \_ ->
2222
describe "Cbor.Bi instances" $ do
2323
-- These data types are defined in the 'core' package which suggests that
2424
-- these tests should be there, but they depend on type in eg 'update' so

block/test/Test/Pos/Block/SafeCopySpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Test.Pos.Binary.Helpers (safeCopyTest)
1717
import Test.Pos.Block.Arbitrary ()
1818

1919
spec :: Spec
20-
spec = withGenesisSpec 0 defaultCoreConfiguration $ \_ -> describe "Block types" $ do
20+
spec = withGenesisSpec 0 defaultCoreConfiguration id $ \_ -> describe "Block types" $ do
2121
-- These types are defined in 'core' but the 'Arbitrary' instances require
2222
-- generator components defined in package like 'ssc' and 'update' which
2323
-- means these tests cannot be moved to 'core'.

core/src/Pos/Core/Configuration.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ withCoreConfigurations
7979
, MonadIO m
8080
)
8181
=> CoreConfiguration
82+
-> (GenesisData -> GenesisData)
83+
-- ^ Update @'GenesisData'@ before passing its parts to @'given'@.
8284
-> FilePath
8385
-- ^ Directory where 'configuration.yaml' is stored.
8486
-> Maybe Timestamp
@@ -89,7 +91,7 @@ withCoreConfigurations
8991
-- provided.
9092
-> (HasConfiguration => ProtocolMagic -> m r)
9193
-> m r
92-
withCoreConfigurations conf@CoreConfiguration{..} confDir mSystemStart mSeed act = case ccGenesis of
94+
withCoreConfigurations conf@CoreConfiguration{..} fn confDir mSystemStart mSeed act = case ccGenesis of
9395
-- If a 'GenesisData' source file is given, we check its hash against the
9496
-- given expected hash, parse it, and use the GenesisData to fill in all of
9597
-- the obligations.
@@ -106,7 +108,7 @@ withCoreConfigurations conf@CoreConfiguration{..} confDir mSystemStart mSeed act
106108

107109
theGenesisData <- case Canonical.fromJSON gdataJSON of
108110
Left err -> throwM $ GenesisDataSchemaError err
109-
Right it -> return it
111+
Right it -> return $ fn it
110112

111113
let (_, theGenesisHash) = canonicalGenesisJson theGenesisData
112114
pc = genesisProtocolConstantsToProtocolConstants (gdProtocolConsts theGenesisData)
@@ -144,14 +146,15 @@ withCoreConfigurations conf@CoreConfiguration{..} confDir mSystemStart mSeed act
144146

145147
let theConf = conf {ccGenesis = GCSpec theSpec}
146148

147-
withGenesisSpec theSystemStart theConf act
149+
withGenesisSpec theSystemStart theConf fn act
148150

149151
withGenesisSpec
150152
:: Timestamp
151153
-> CoreConfiguration
154+
-> (GenesisData -> GenesisData)
152155
-> (HasConfiguration => ProtocolMagic -> r)
153156
-> r
154-
withGenesisSpec theSystemStart conf@CoreConfiguration{..} val = case ccGenesis of
157+
withGenesisSpec theSystemStart conf@CoreConfiguration{..} fn val = case ccGenesis of
155158
GCSrc {} -> error "withGenesisSpec called with GCSrc"
156159
GCSpec spec ->
157160
withProtocolConstants pc $
@@ -168,7 +171,7 @@ withGenesisSpec theSystemStart conf@CoreConfiguration{..} val = case ccGenesis o
168171
(toList $ gsHeavyDelegation spec) <> toList ggdDelegation
169172

170173
-- Construct the final value
171-
theGenesisData =
174+
theGenesisData = fn $
172175
GenesisData
173176
{ gdBootStakeholders = ggdBootStakeholders
174177
, gdHeavyDelegation = finalHeavyDelegation

core/test/Test/Pos/Core/CborSpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ instance Bi (Attributes X2) where
8383

8484

8585
spec :: Spec
86-
spec = withGenesisSpec 0 defaultCoreConfiguration $ \_ ->
86+
spec = withGenesisSpec 0 defaultCoreConfiguration id $ \_ ->
8787
describe "Cbor Bi instances" $ do
8888
describe "Core.Address" $ do
8989
binaryTest @Address

core/test/Test/Pos/Core/SlottingSpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import Test.Pos.Core.Arbitrary (EoSToIntOverflow (..),
2020
import Test.Pos.Util.QuickCheck.Property (shouldThrowException, (.=.))
2121

2222
spec :: Spec
23-
spec = withGenesisSpec 0 defaultCoreConfiguration $ \_ -> describe "Slotting" $ do
23+
spec = withGenesisSpec 0 defaultCoreConfiguration id $ \_ -> describe "Slotting" $ do
2424
describe "SlotId" $ do
2525
describe "Ord" $ do
2626
prop "is consistent with flatten/unflatten"

db/cardano-sl-db.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ library
112112
Pos.DB.Block.Logic.Creation
113113
Pos.DB.Block.Logic.Header
114114
Pos.DB.Block.Logic.Internal
115+
Pos.DB.Block.Logic.Types
115116
Pos.DB.Block.Logic.Util
116117
Pos.DB.Block.Logic.VAR
117118
Pos.DB.Block.Lrc

db/src/Pos/DB/Block.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import Pos.DB.Block.Load as X
6969
import Pos.DB.Block.Logic.Creation as X
7070
import Pos.DB.Block.Logic.Header as X
7171
import Pos.DB.Block.Logic.Internal as X
72+
import Pos.DB.Block.Logic.Types as X
7273
import Pos.DB.Block.Logic.Util as X
7374
import Pos.DB.Block.Logic.VAR as X
7475
import Pos.DB.Block.Lrc as X

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ import Pos.Core.Util.LogSafe (logInfoS)
4747
import Pos.Crypto (ProtocolMagic, SecretKey)
4848
import Pos.DB.Block.Logic.Internal (MonadBlockApply,
4949
applyBlocksUnsafe, normalizeMempool)
50+
import Pos.DB.Block.Logic.Types (VerifyBlocksContext (..),
51+
getVerifyBlocksContext)
5052
import Pos.DB.Block.Logic.Util (calcChainQualityM)
5153
import Pos.DB.Block.Logic.VAR (verifyBlocksPrefix)
5254
import Pos.DB.Block.Lrc (LrcModeFull, lrcSingleShot)
@@ -161,11 +163,17 @@ createGenesisBlockDo pm epoch = do
161163
LrcDB.getLeadersForEpoch
162164
let blk = mkGenesisBlock pm (Right tipHeader) epoch leaders
163165
let newTip = headerHash blk
164-
verifyBlocksPrefix pm (one (Left blk)) >>= \case
166+
ctx <- getVerifyBlocksContext
167+
verifyBlocksPrefix pm ctx (one (Left blk)) >>= \case
165168
Left err -> reportFatalError $ pretty err
166169
Right (undos, pollModifier) -> do
167170
let undo = undos ^. _Wrapped . _neHead
168-
applyBlocksUnsafe pm (ShouldCallBListener True) (one (Left blk, undo)) (Just pollModifier)
171+
applyBlocksUnsafe pm
172+
(vbcBlockVersion ctx)
173+
(vbcBlockVersionData ctx)
174+
(ShouldCallBListener True)
175+
(one (Left blk, undo))
176+
(Just pollModifier)
169177
normalizeMempool pm
170178
pure (newTip, Just blk)
171179
logShouldNot =
@@ -358,15 +366,18 @@ applyCreatedBlock pm pske createdBlock = applyCreatedBlockDo False createdBlock
358366
where
359367
slotId = createdBlock ^. BC.mainBlockSlot
360368
applyCreatedBlockDo :: Bool -> MainBlock -> m MainBlock
361-
applyCreatedBlockDo isFallback blockToApply =
362-
verifyBlocksPrefix pm (one (Right blockToApply)) >>= \case
369+
applyCreatedBlockDo isFallback blockToApply = do
370+
ctx <- getVerifyBlocksContext
371+
verifyBlocksPrefix pm ctx (one (Right blockToApply)) >>= \case
363372
Left (pretty -> reason)
364373
| isFallback -> onFailedFallback reason
365374
| otherwise -> fallback reason
366375
Right (undos, pollModifier) -> do
367376
let undo = undos ^. _Wrapped . _neHead
368377
applyBlocksUnsafe
369378
pm
379+
(vbcBlockVersion ctx)
380+
(vbcBlockVersionData ctx)
370381
(ShouldCallBListener True)
371382
(one (Right blockToApply, undo))
372383
(Just pollModifier)

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import Pos.Core.Block (Block, ComponentBlock (..), GenesisBlock,
4141
import Pos.Core.Chrono (NE, NewestFirst (..), OldestFirst (..))
4242
import Pos.Core.Exception (assertionFailed)
4343
import Pos.Core.Reporting (MonadReporting)
44+
import Pos.Core.Update (BlockVersion, BlockVersionData)
4445
import Pos.Crypto (ProtocolMagic)
4546
import Pos.DB (MonadDB, MonadDBRead, MonadGState, SomeBatchOp (..))
4647
import Pos.DB.Block.BListener (MonadBListener)
@@ -143,11 +144,13 @@ applyBlocksUnsafe
143144
, HasTxpConfiguration
144145
)
145146
=> ProtocolMagic
147+
-> BlockVersion
148+
-> BlockVersionData
146149
-> ShouldCallBListener
147150
-> OldestFirst NE Blund
148151
-> Maybe PollModifier
149152
-> m ()
150-
applyBlocksUnsafe pm scb blunds pModifier = do
153+
applyBlocksUnsafe pm bv bvd scb blunds pModifier = do
151154
-- Check that all blunds have the same epoch.
152155
unless (null nextEpoch) $ assertionFailed $
153156
sformat ("applyBlocksUnsafe: tried to apply more than we should"%
@@ -167,7 +170,7 @@ applyBlocksUnsafe pm scb blunds pModifier = do
167170
(b@(Left _,_):|(x:xs)) -> app' (b:|[]) >> app' (x:|xs)
168171
_ -> app blunds
169172
where
170-
app x = applyBlocksDbUnsafeDo pm scb x pModifier
173+
app x = applyBlocksDbUnsafeDo pm bv bvd scb x pModifier
171174
app' = app . OldestFirst
172175
(thisEpoch, nextEpoch) =
173176
spanSafe ((==) `on` view (_1 . epochIndexL)) $ getOldestFirst blunds
@@ -177,22 +180,24 @@ applyBlocksDbUnsafeDo
177180
, HasTxpConfiguration
178181
)
179182
=> ProtocolMagic
183+
-> BlockVersion
184+
-> BlockVersionData
180185
-> ShouldCallBListener
181186
-> OldestFirst NE Blund
182187
-> Maybe PollModifier
183188
-> m ()
184-
applyBlocksDbUnsafeDo pm scb blunds pModifier = do
189+
applyBlocksDbUnsafeDo pm bv bvd scb blunds pModifier = do
185190
let blocks = fmap fst blunds
186191
-- Note: it's important to do 'slogApplyBlocks' first, because it
187192
-- puts blocks in DB.
188193
slogBatch <- slogApplyBlocks scb blunds
189194
TxpGlobalSettings {..} <- view (lensOf @TxpGlobalSettings)
190-
usBatch <- SomeBatchOp <$> usApplyBlocks pm (map toUpdateBlock blocks) pModifier
195+
usBatch <- SomeBatchOp <$> usApplyBlocks pm bv (map toUpdateBlock blocks) pModifier
191196
delegateBatch <- SomeBatchOp <$> dlgApplyBlocks (map toDlgBlund blunds)
192197
txpBatch <- tgsApplyBlocks $ map toTxpBlund blunds
193198
sscBatch <- SomeBatchOp <$>
194199
-- TODO: pass not only 'Nothing'
195-
sscApplyBlocks pm (map toSscBlock blocks) Nothing
200+
sscApplyBlocks pm bvd (map toSscBlock blocks) Nothing
196201
GS.writeBatchGState
197202
[ delegateBatch
198203
, usBatch

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module Pos.DB.Block.Logic.Types
2+
( VerifyBlocksContext (..)
3+
, getVerifyBlocksContext
4+
, getVerifyBlocksContext'
5+
) where
6+
7+
import Universum
8+
9+
import Pos.Core.Slotting (MonadSlots (getCurrentSlot), SlotId)
10+
import Pos.Core.Update (BlockVersion, BlockVersionData)
11+
import Pos.DB.Class (MonadDBRead)
12+
import Pos.DB.Update (getAdoptedBVFull)
13+
14+
-- | Initial context for `verifyBlocksPrefix` which runs in `MonadBlockVerify`
15+
-- monad.
16+
data VerifyBlocksContext = VerifyBlocksContext
17+
{ vbcCurrentSlot :: !(Maybe SlotId)
18+
-- ^ used to check if headers are not from future
19+
, vbcBlockVersion :: !BlockVersion
20+
, vbcBlockVersionData :: !BlockVersionData
21+
} deriving Generic
22+
23+
instance NFData VerifyBlocksContext
24+
25+
getVerifyBlocksContext
26+
:: forall ctx m.
27+
( MonadDBRead m
28+
, MonadSlots ctx m
29+
)
30+
=> m VerifyBlocksContext
31+
getVerifyBlocksContext =
32+
getCurrentSlot >>= getVerifyBlocksContext'
33+
34+
getVerifyBlocksContext'
35+
:: MonadDBRead m
36+
=> Maybe SlotId
37+
-> m VerifyBlocksContext
38+
getVerifyBlocksContext' vbcCurrentSlot = do
39+
(vbcBlockVersion, vbcBlockVersionData) <- getAdoptedBVFull
40+
return $ VerifyBlocksContext {..}
41+

0 commit comments

Comments
 (0)