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

Commit 70f2b9c

Browse files
authored
Merge pull request #3756 from input-output-hk/i+j/develop/CO-410/split-test-suites
[CO-410] Split test-suites for NetworkMainOrStage/NetworkTestnet
2 parents 47e08d0 + 94bbdaf commit 70f2b9c

File tree

70 files changed

+1672
-1195
lines changed

Some content is hidden

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

70 files changed

+1672
-1195
lines changed

auxx/src/Command/BlockGen.hs

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import Pos.Chain.Genesis as Genesis (Config (..),
1717
configBootStakeholders)
1818
import Pos.Chain.Txp (TxpConfiguration)
1919
import Pos.Client.KeyStorage (getSecretKeysPlain)
20+
import Pos.Core.NetworkMagic (makeNetworkMagic)
2021
import Pos.Crypto (encToSecret)
2122
import Pos.DB.Txp (txpGlobalSettings)
2223
import Pos.Generator.Block (BlockGenParams (..), genBlocks,
@@ -38,7 +39,8 @@ generateBlocks genesisConfig txpConfig GenBlocksParams{..} = withStateLock HighP
3839
seed <- liftIO $ maybe randomIO pure bgoSeed
3940
logInfo $ "Generating with seed " <> show seed
4041

41-
allSecrets <- mkAllSecretsSimple . map encToSecret <$> getSecretKeysPlain
42+
let nm = makeNetworkMagic $ configProtocolMagic genesisConfig
43+
allSecrets <- (mkAllSecretsSimple nm) . map encToSecret <$> getSecretKeysPlain
4244

4345
let bgenParams =
4446
BlockGenParams

chain/src/Pos/Chain/Genesis/Generate.hs

+2-6
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import Pos.Core.Common (Address, Coin, IsBootstrapEraAddr (..),
4040
coinToInteger, deriveFirstHDAddress,
4141
makePubKeyAddressBoot, mkCoin, sumCoins,
4242
unsafeIntegerToCoin)
43-
import Pos.Core.NetworkMagic (NetworkMagic (..))
43+
import Pos.Core.NetworkMagic (makeNetworkMagic)
4444
import Pos.Core.ProtocolConstants (ProtocolConstants, vssMaxTTL,
4545
vssMinTTL)
4646
import Pos.Crypto (EncryptedSecretKey, ProtocolMagic, RedeemPublicKey,
@@ -176,7 +176,7 @@ generateGenesisData pm pc (GenesisInitializer{..}) realAvvmBalances = determinis
176176
vssCerts <- mkVssCertificatesMap
177177
<$> mapM (generateVssCert pm pc) richmenSecrets
178178

179-
let nm = fixedNM
179+
let nm = makeNetworkMagic pm
180180

181181
-- Non AVVM balances
182182
---- Addresses
@@ -321,7 +321,3 @@ genTestnetDistribution TestnetBalanceOptions {..} testBalance =
321321

322322
getShare :: Double -> Integer -> Integer
323323
getShare sh n = round $ sh * fromInteger n
324-
325-
326-
fixedNM :: NetworkMagic
327-
fixedNM = NetworkMainOrStage

chain/src/Pos/Chain/Txp/GenesisUtxo.hs

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import Universum
1010
import qualified Data.HashMap.Strict as HM
1111
import qualified Data.Map.Strict as Map
1212

13-
import Pos.Chain.Genesis (GenesisData (..), getGenesisAvvmBalances,
13+
import Pos.Chain.Genesis (GenesisData (..),
14+
GenesisProtocolConstants (..), getGenesisAvvmBalances,
1415
getGenesisNonAvvmBalances)
1516
import Pos.Chain.Txp.Toil (Utxo, utxoToStakes)
1617
import Pos.Chain.Txp.Tx (TxIn (..), TxOut (..))
1718
import Pos.Chain.Txp.TxOutAux (TxOutAux (..))
1819
import Pos.Core (Address, Coin, StakesMap, makeRedeemAddress)
19-
import Pos.Core.NetworkMagic (NetworkMagic (..))
20+
import Pos.Core.NetworkMagic (NetworkMagic, makeNetworkMagic)
2021
import Pos.Crypto (unsafeHash)
2122

2223

@@ -28,7 +29,9 @@ genesisUtxo :: GenesisData -> Utxo
2829
genesisUtxo genesisData =
2930
let
3031
networkMagic :: NetworkMagic
31-
networkMagic = fixedNM
32+
networkMagic = makeNetworkMagic $
33+
gpcProtocolMagic $
34+
gdProtocolConsts genesisData
3235

3336
preUtxo :: [(Address, Coin)]
3437
preUtxo =
@@ -44,6 +47,3 @@ genesisUtxo genesisData =
4447
(TxInUtxo (unsafeHash addr) 0, TxOutAux (TxOut addr coin))
4548
in
4649
Map.fromList $ utxoEntry <$> preUtxo
47-
48-
fixedNM :: NetworkMagic
49-
fixedNM = NetworkMainOrStage

chain/src/Pos/Chain/Txp/Toil/Logic.hs

+3-6
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import Pos.Core (AddrAttributes (..), AddrStakeDistribution (..),
4545
import Pos.Core.Common (integerToCoin)
4646
import qualified Pos.Core.Common as Fee (TxFeePolicy (..),
4747
calculateTxSizeLinear)
48-
import Pos.Core.NetworkMagic (NetworkMagic (..))
48+
import Pos.Core.NetworkMagic (makeNetworkMagic)
4949
import Pos.Crypto (ProtocolMagic, WithHash (..), hash)
5050
import Pos.Util (liftEither)
5151

@@ -145,7 +145,8 @@ verifyAndApplyTx ::
145145
-> ExceptT ToilVerFailure UtxoM TxUndo
146146
verifyAndApplyTx pm adoptedBVD lockedAssets curEpoch verifyVersions tx@(_, txAux) = do
147147
whenLeft (checkTxAux txAux) (throwError . ToilInconsistentTxAux)
148-
let ctx = Utxo.VTxContext verifyVersions fixedNM
148+
let nm = makeNetworkMagic pm
149+
ctx = Utxo.VTxContext verifyVersions nm
149150
vtur@VerifyTxUtxoRes {..} <- Utxo.verifyTxUtxo pm ctx lockedAssets txAux
150151
liftEither $ verifyGState adoptedBVD curEpoch txAux vtur
151152
lift $ applyTxToUtxo' tx
@@ -237,7 +238,3 @@ withTxId aux = (hash (taTx aux), aux)
237238

238239
applyTxToUtxo' :: (TxId, TxAux) -> UtxoM ()
239240
applyTxToUtxo' (i, TxAux tx _) = Utxo.applyTxToUtxo (WithHash tx i)
240-
241-
242-
fixedNM :: NetworkMagic
243-
fixedNM = NetworkMainOrStage

chain/test/Test/Pos/Chain/Block/Arbitrary.hs

+99-82
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ module Test.Pos.Chain.Block.Arbitrary
1616
, genMainBlockBody
1717
, genMainBlockBodyForSlot
1818
, genMainBlock
19+
, genHeaderAndParams
20+
, genStubbedBHL
1921
) where
2022

2123
import Universum
@@ -48,7 +50,6 @@ import Test.Pos.Chain.Ssc.Arbitrary (SscPayloadDependsOnSlot (..),
4850
import Test.Pos.Chain.Txp.Arbitrary (genTxPayload)
4951
import Test.Pos.Chain.Update.Arbitrary (genUpdatePayload)
5052
import Test.Pos.Core.Arbitrary (genSlotId)
51-
import Test.Pos.Crypto.Dummy (dummyProtocolMagic)
5253

5354
newtype BodyDependsOnSlot body = BodyDependsOnSlot
5455
{ genBodyDepsOnSlot :: Core.SlotId -> Gen body
@@ -98,8 +99,9 @@ instance Arbitrary Block.GenesisBody where
9899
shrink = genericShrink
99100

100101
instance Arbitrary Block.GenesisBlock where
101-
arbitrary = Block.mkGenesisBlock dummyProtocolMagic
102-
<$> (maybe (Left dummyGenesisHash) Right <$> arbitrary)
102+
arbitrary = Block.mkGenesisBlock
103+
<$> arbitrary
104+
<*> (maybe (Left dummyGenesisHash) Right <$> arbitrary)
103105
<*> arbitrary
104106
<*> arbitrary
105107
shrink = genericShrink
@@ -129,7 +131,8 @@ instance Arbitrary Block.MainBlockHeader where
129131
prevHash <- arbitrary
130132
difficulty <- arbitrary
131133
body <- arbitrary
132-
genMainBlockHeader dummyProtocolMagic prevHash difficulty body
134+
pm <- arbitrary
135+
genMainBlockHeader pm prevHash difficulty body
133136
shrink = genericShrink
134137

135138
instance Arbitrary Block.MainExtraHeaderData where
@@ -197,7 +200,8 @@ instance Arbitrary (BodyDependsOnSlot Block.MainBody) where
197200
txPayload <- arbitrary
198201
generator <- genPayloadDependsOnSlot <$> arbitrary
199202
mpcData <- generator slotId
200-
dlgPayload <- genDlgPayload dummyProtocolMagic $ Core.siEpoch slotId
203+
pm <- arbitrary
204+
dlgPayload <- genDlgPayload pm $ Core.siEpoch slotId
201205
mpcUpload <- arbitrary
202206
return $ Block.MainBody txPayload mpcData dlgPayload mpcUpload
203207

@@ -230,13 +234,14 @@ genMainBlock pm prevHash difficulty = do
230234
instance Arbitrary Block.MainBlock where
231235
arbitrary = do
232236
slot <- arbitrary
237+
pm <- arbitrary
233238
bv <- arbitrary
234239
sv <- arbitrary
235240
prevHeader <- maybe (Left dummyGenesisHash) Right <$> arbitrary
236241
sk <- arbitrary
237242
BodyDependsOnSlot {..} <- arbitrary :: Gen (BodyDependsOnSlot Block.MainBody)
238243
body <- genBodyDepsOnSlot slot
239-
pure $ mkMainBlock dummyProtocolMagic bv sv prevHeader slot sk Nothing body
244+
pure $ mkMainBlock pm bv sv prevHeader slot sk Nothing body
240245
shrink = genericShrink
241246

242247
instance Buildable (Block.BlockHeader, PublicKey) where
@@ -274,26 +279,28 @@ instance Show BlockHeaderList where
274279
-- * if an epoch is `n` slots long, every `n+1`-th block will be of the
275280
-- genesis kind.
276281
recursiveHeaderGen
277-
:: GenesisHash
282+
:: ProtocolMagic
283+
-> GenesisHash
278284
-> Bool -- ^ Whether to create genesis block before creating main block for 0th slot
279285
-> [Either SecretKey (SecretKey, SecretKey)]
280286
-> [Core.SlotId]
281287
-> [Block.BlockHeader]
282288
-> Gen [Block.BlockHeader]
283-
recursiveHeaderGen gHash
289+
recursiveHeaderGen pm
290+
gHash
284291
genesis
285292
(eitherOfLeader : leaders)
286293
(Core.SlotId{..} : rest)
287294
blockchain
288295
| genesis && Core.getSlotIndex siSlot == 0 = do
289296
gBody <- arbitrary
290297
let pHeader = maybe (Left gHash) Right ((fmap fst . uncons) blockchain)
291-
gHeader = Block.BlockHeaderGenesis $ Block.mkGenesisHeader dummyProtocolMagic pHeader siEpoch gBody
298+
gHeader = Block.BlockHeaderGenesis $ Block.mkGenesisHeader pm pHeader siEpoch gBody
292299
mHeader <- genMainHeader (Just gHeader)
293-
recursiveHeaderGen gHash True leaders rest (mHeader : gHeader : blockchain)
300+
recursiveHeaderGen pm gHash True leaders rest (mHeader : gHeader : blockchain)
294301
| otherwise = do
295302
curHeader <- genMainHeader ((fmap fst . uncons) blockchain)
296-
recursiveHeaderGen gHash True leaders rest (curHeader : blockchain)
303+
recursiveHeaderGen pm gHash True leaders rest (curHeader : blockchain)
297304
where
298305
genMainHeader prevHeader = do
299306
body <- arbitrary
@@ -306,13 +313,13 @@ recursiveHeaderGen gHash
306313
Left sk -> (sk, Nothing)
307314
Right (issuerSK, delegateSK) ->
308315
let delegatePK = toPublic delegateSK
309-
proxy = ( createPsk dummyProtocolMagic issuerSK delegatePK (Core.HeavyDlgIndex siEpoch)
316+
proxy = ( createPsk pm issuerSK delegatePK (Core.HeavyDlgIndex siEpoch)
310317
, toPublic issuerSK)
311318
in (delegateSK, Just proxy)
312319
pure $ Block.BlockHeaderMain $
313-
Block.mkMainHeader dummyProtocolMagic (maybe (Left gHash) Right prevHeader) slotId leader proxySK body extraHData
314-
recursiveHeaderGen _ _ [] _ b = return b
315-
recursiveHeaderGen _ _ _ [] b = return b
320+
Block.mkMainHeader pm (maybe (Left gHash) Right prevHeader) slotId leader proxySK body extraHData
321+
recursiveHeaderGen _ _ _ [] _ b = return b
322+
recursiveHeaderGen _ _ _ _ [] b = return b
316323

317324

318325
-- | Maximum start epoch in block header verification tests
@@ -341,19 +348,25 @@ bhlEpochs = 2
341348
-- Note that a leader is generated for each slot.
342349
-- (Not exactly a leader - see previous comment)
343350
instance Arbitrary BlockHeaderList where
344-
arbitrary = do
345-
incompleteEpochSize <- choose (1, dummyEpochSlots - 1)
346-
let slot = Core.SlotId 0 localSlotIndexMinBound
347-
generateBHL dummyGenesisHash True slot (dummyEpochSlots * bhlEpochs + incompleteEpochSize)
351+
arbitrary = arbitrary >>= genStubbedBHL
352+
353+
genStubbedBHL
354+
:: ProtocolMagic
355+
-> Gen BlockHeaderList
356+
genStubbedBHL pm = do
357+
incompleteEpochSize <- choose (1, dummyEpochSlots - 1)
358+
let slot = Core.SlotId 0 localSlotIndexMinBound
359+
generateBHL pm dummyGenesisHash True slot (dummyEpochSlots * bhlEpochs + incompleteEpochSize)
348360

349361
generateBHL
350-
:: GenesisHash
362+
:: ProtocolMagic
363+
-> GenesisHash
351364
-> Bool -- ^ Whether to create genesis block before creating main
352365
-- block for 0th slot
353366
-> Core.SlotId -- ^ Start slot
354367
-> Core.SlotCount -- ^ Slot count
355368
-> Gen BlockHeaderList
356-
generateBHL gHash createInitGenesis startSlot slotCount = BHL <$> do
369+
generateBHL pm gHash createInitGenesis startSlot slotCount = BHL <$> do
357370
let correctLeaderGen :: Gen (Either SecretKey (SecretKey, SecretKey))
358371
correctLeaderGen =
359372
-- We don't want to create blocks with self-signed psks
@@ -368,6 +381,7 @@ generateBHL gHash createInitGenesis startSlot slotCount = BHL <$> do
368381
[Core.flattenSlotId dummyEpochSlots startSlot ..]
369382
(, actualLeaders) <$>
370383
recursiveHeaderGen
384+
pm
371385
gHash
372386
createInitGenesis
373387
leadersList
@@ -383,69 +397,72 @@ newtype HeaderAndParams = HAndP
383397
{ getHAndP :: (Block.VerifyHeaderParams, Block.BlockHeader)
384398
} deriving (Eq, Show)
385399

400+
genHeaderAndParams :: ProtocolMagic -> Gen HeaderAndParams
401+
genHeaderAndParams pm = do
402+
-- This integer is used as a seed to randomly choose a slot down below
403+
seed <- arbitrary :: Gen Int
404+
startSlot <- Core.SlotId <$> choose (0, bhlMaxStartingEpoch) <*> arbitrary
405+
(headers, leaders) <- first reverse . getHeaderList <$>
406+
(generateBHL pm dummyGenesisHash True startSlot =<< choose (1, 2))
407+
let num = length headers
408+
-- 'skip' is the random number of headers that should be skipped in
409+
-- the header chain. This ensures different parts of it are chosen
410+
-- each time.
411+
skip <- choose (0, num - 1)
412+
let atMost2HeadersAndLeaders = take 2 $ drop skip headers
413+
(prev, header) =
414+
case atMost2HeadersAndLeaders of
415+
[h] -> (Nothing, h)
416+
[h1, h2] -> (Just h1, h2)
417+
_ -> error "[BlockSpec] the headerchain doesn't have enough headers"
418+
-- This binding captures the chosen header's epoch. It is used to
419+
-- drop all all leaders of headers from previous epochs.
420+
thisEpochStartIndex = fromIntegral dummyEpochSlots *
421+
fromIntegral (header ^. Core.epochIndexL)
422+
thisHeadersEpoch = drop thisEpochStartIndex leaders
423+
-- A helper function. Given integers 'x' and 'y', it chooses a
424+
-- random integer in the interval [x, y]
425+
betweenXAndY :: Random a => a -> a -> a
426+
betweenXAndY x y = fst . randomR (x, y) . mkStdGen $ seed
427+
-- One of the fields in the 'VerifyHeaderParams' type is 'Just
428+
-- SlotId'. The following binding is where it is calculated.
429+
randomSlotBeforeThisHeader =
430+
case header of
431+
-- If the header is of the genesis kind, this field is
432+
-- not needed.
433+
Block.BlockHeaderGenesis _ -> Nothing
434+
-- If it's a main blockheader, then a valid "current"
435+
-- SlotId for testing is any with an epoch greater than
436+
-- the header's epoch and with any slot index, or any in
437+
-- the same epoch but with a greater or equal slot index
438+
-- than the header.
439+
Block.BlockHeaderMain h -> -- Nothing {-
440+
let (Core.SlotId e s) = view Block.headerSlotL h
441+
rndEpoch :: Core.EpochIndex
442+
rndEpoch = betweenXAndY e maxBound
443+
rndSlotIdx :: Core.LocalSlotIndex
444+
rndSlotIdx = if rndEpoch > e
445+
then betweenXAndY localSlotIndexMinBound (localSlotIndexMaxBound dummyEpochSlots)
446+
else betweenXAndY s (localSlotIndexMaxBound dummyEpochSlots)
447+
rndSlot = Core.SlotId rndEpoch rndSlotIdx
448+
in Just rndSlot
449+
hasUnknownAttributes =
450+
not . areAttributesKnown $
451+
case header of
452+
Block.BlockHeaderGenesis h -> h ^. Block.gbhExtra . Block.gehAttributes
453+
Block.BlockHeaderMain h -> h ^. Block.gbhExtra . Block.mehAttributes
454+
params = Block.VerifyHeaderParams
455+
{ Block.vhpPrevHeader = prev
456+
, Block.vhpCurrentSlot = randomSlotBeforeThisHeader
457+
, Block.vhpLeaders = nonEmpty $ map Core.addressHash thisHeadersEpoch
458+
, Block.vhpMaxSize = Just (biSize header)
459+
, Block.vhpVerifyNoUnknown = not hasUnknownAttributes
460+
}
461+
return . HAndP $ (params, header)
462+
386463
-- | A lot of the work to generate a valid sequence of blockheaders has
387464
-- already been done in the 'Arbitrary' instance of the 'BlockHeaderList'
388465
-- type, so it is used here and at most 3 blocks are taken from the generated
389466
-- list.
390467
instance Arbitrary HeaderAndParams where
391-
arbitrary = do
392-
-- This integer is used as a seed to randomly choose a slot down below
393-
seed <- arbitrary :: Gen Int
394-
startSlot <- Core.SlotId <$> choose (0, bhlMaxStartingEpoch) <*> arbitrary
395-
(headers, leaders) <- first reverse . getHeaderList <$>
396-
(generateBHL dummyGenesisHash True startSlot =<< choose (1, 2))
397-
let num = length headers
398-
-- 'skip' is the random number of headers that should be skipped in
399-
-- the header chain. This ensures different parts of it are chosen
400-
-- each time.
401-
skip <- choose (0, num - 1)
402-
let atMost2HeadersAndLeaders = take 2 $ drop skip headers
403-
(prev, header) =
404-
case atMost2HeadersAndLeaders of
405-
[h] -> (Nothing, h)
406-
[h1, h2] -> (Just h1, h2)
407-
_ -> error "[BlockSpec] the headerchain doesn't have enough headers"
408-
-- This binding captures the chosen header's epoch. It is used to
409-
-- drop all all leaders of headers from previous epochs.
410-
thisEpochStartIndex = fromIntegral dummyEpochSlots *
411-
fromIntegral (header ^. Core.epochIndexL)
412-
thisHeadersEpoch = drop thisEpochStartIndex leaders
413-
-- A helper function. Given integers 'x' and 'y', it chooses a
414-
-- random integer in the interval [x, y]
415-
betweenXAndY :: Random a => a -> a -> a
416-
betweenXAndY x y = fst . randomR (x, y) . mkStdGen $ seed
417-
-- One of the fields in the 'VerifyHeaderParams' type is 'Just
418-
-- SlotId'. The following binding is where it is calculated.
419-
randomSlotBeforeThisHeader =
420-
case header of
421-
-- If the header is of the genesis kind, this field is
422-
-- not needed.
423-
Block.BlockHeaderGenesis _ -> Nothing
424-
-- If it's a main blockheader, then a valid "current"
425-
-- SlotId for testing is any with an epoch greater than
426-
-- the header's epoch and with any slot index, or any in
427-
-- the same epoch but with a greater or equal slot index
428-
-- than the header.
429-
Block.BlockHeaderMain h -> -- Nothing {-
430-
let (Core.SlotId e s) = view Block.headerSlotL h
431-
rndEpoch :: Core.EpochIndex
432-
rndEpoch = betweenXAndY e maxBound
433-
rndSlotIdx :: Core.LocalSlotIndex
434-
rndSlotIdx = if rndEpoch > e
435-
then betweenXAndY localSlotIndexMinBound (localSlotIndexMaxBound dummyEpochSlots)
436-
else betweenXAndY s (localSlotIndexMaxBound dummyEpochSlots)
437-
rndSlot = Core.SlotId rndEpoch rndSlotIdx
438-
in Just rndSlot
439-
hasUnknownAttributes =
440-
not . areAttributesKnown $
441-
case header of
442-
Block.BlockHeaderGenesis h -> h ^. Block.gbhExtra . Block.gehAttributes
443-
Block.BlockHeaderMain h -> h ^. Block.gbhExtra . Block.mehAttributes
444-
params = Block.VerifyHeaderParams
445-
{ Block.vhpPrevHeader = prev
446-
, Block.vhpCurrentSlot = randomSlotBeforeThisHeader
447-
, Block.vhpLeaders = nonEmpty $ map Core.addressHash thisHeadersEpoch
448-
, Block.vhpMaxSize = Just (biSize header)
449-
, Block.vhpVerifyNoUnknown = not hasUnknownAttributes
450-
}
451-
return . HAndP $ (params, header)
468+
arbitrary = arbitrary >>= genHeaderAndParams

0 commit comments

Comments
 (0)