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

Commit fd4b172

Browse files
committed
[CO-354] Add RequiresNetworkMagic field to CoreConfiguration
1 parent 24fc9b7 commit fd4b172

File tree

12 files changed

+185
-91
lines changed

12 files changed

+185
-91
lines changed

core/src/Pos/Aeson/Core/Configuration.hs

+19-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,26 @@ module Pos.Aeson.Core.Configuration
66
(
77
) where
88

9-
import Data.Aeson.TH (deriveJSON)
9+
import Universum
10+
11+
import Data.Aeson (FromJSON, parseJSON, withObject, (.:), (.:?))
12+
import Data.Aeson.TH (deriveToJSON)
1013
import Serokell.Aeson.Options (defaultOptions)
1114

1215
import Pos.Core.Configuration.Core (CoreConfiguration (..))
16+
import Pos.Crypto (RequiresNetworkMagic (..))
17+
18+
deriveToJSON defaultOptions ''CoreConfiguration
1319

14-
deriveJSON defaultOptions ''CoreConfiguration
20+
instance FromJSON CoreConfiguration where
21+
parseJSON = withObject "core" $ \obj -> do
22+
ccg <- obj .: "genesis"
23+
ccdsv <- obj .: "dbSerializeVersion"
24+
ccrnm <- determineRNM <$> obj .:? "requiresNetworkMagic"
25+
pure $ CoreConfiguration ccg ccdsv ccrnm
26+
where
27+
-- If "requiresNetworkMagic" is not specified, default to NMMustBeJust
28+
determineRNM :: Maybe RequiresNetworkMagic -> RequiresNetworkMagic
29+
determineRNM mrnm = case mrnm of
30+
Nothing -> NMMustBeJust
31+
Just x -> x

core/src/Pos/Core/Configuration.hs

+24-6
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,21 @@ withCoreConfigurations conf@CoreConfiguration{..} confDir mSystemStart mSeed act
105105
theGenesisData <- case Canonical.fromJSON gdataJSON of
106106
Left err -> throwM $ GenesisDataSchemaError err
107107
Right it -> return it
108+
-- Override the RequiresNetworkMagic in GenesisData with the value
109+
-- specified in CoreConfiguration.
110+
let overriddenGenesisData = updateGD theGenesisData
108111

109-
let (_, theGenesisHash) = canonicalGenesisJson theGenesisData
110-
pc = genesisProtocolConstantsToProtocolConstants (gdProtocolConsts theGenesisData)
111-
pm = gpcProtocolMagic (gdProtocolConsts theGenesisData)
112+
let (_, theGenesisHash) = canonicalGenesisJson overriddenGenesisData
113+
pc = genesisProtocolConstantsToProtocolConstants (gdProtocolConsts overriddenGenesisData)
114+
pm = gpcProtocolMagic (gdProtocolConsts overriddenGenesisData)
112115
when (theGenesisHash /= expectedHash) $
113116
throwM $ GenesisHashMismatch
114117
(show theGenesisHash) (show expectedHash)
115118

116119
withCoreConfiguration conf $
117120
withProtocolConstants pc $
118-
withGenesisBlockVersionData (gdBlockVersionData theGenesisData) $
119-
withGenesisData theGenesisData $
121+
withGenesisBlockVersionData (gdBlockVersionData overriddenGenesisData) $
122+
withGenesisData overriddenGenesisData $
120123
withGenesisHash theGenesisHash $
121124
withGeneratedSecrets Nothing $
122125
act pm
@@ -139,10 +142,25 @@ withCoreConfigurations conf@CoreConfiguration{..} confDir mSystemStart mSeed act
139142
Just newSeed -> spec
140143
{ gsInitializer = overrideSeed newSeed (gsInitializer spec)
141144
}
145+
-- Override the RequiresNetworkMagic in GenesisSpec with the value
146+
-- specified in CoreConfiguration.
147+
overriddenSpec = updateGS theSpec
142148

143-
let theConf = conf {ccGenesis = GCSpec theSpec}
149+
let theConf = conf {ccGenesis = GCSpec overriddenSpec}
144150

145151
withGenesisSpec theSystemStart theConf act
152+
where
153+
updateGD :: GenesisData -> GenesisData
154+
updateGD gd = gd { gdProtocolConsts = updateGPC (gdProtocolConsts gd) }
155+
--
156+
updateGS :: GenesisSpec -> GenesisSpec
157+
updateGS gs = gs { gsProtocolConstants = updateGPC (gsProtocolConstants gs) }
158+
--
159+
updateGPC :: GenesisProtocolConstants -> GenesisProtocolConstants
160+
updateGPC gpc = gpc { gpcProtocolMagic = updatePM (gpcProtocolMagic gpc) }
161+
--
162+
updatePM :: ProtocolMagic -> ProtocolMagic
163+
updatePM pm = pm { getRequiresNetworkMagic = ccRequiresNetworkMagic }
146164

147165
withGenesisSpec
148166
:: Timestamp

core/src/Pos/Core/Configuration/Core.hs

+9-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import Pos.Core.Genesis (FakeAvvmOptions (..), GenesisAvvmBalances (..
4242
import Pos.Core.ProtocolConstants (VssMaxTTL (..), VssMinTTL (..))
4343
import Pos.Core.Slotting (EpochIndex (..))
4444
import Pos.Core.Update (BlockVersionData (..), SoftforkRule (..))
45-
import Pos.Crypto (ProtocolMagic (..))
45+
import Pos.Crypto (ProtocolMagic (..), RequiresNetworkMagic (..))
4646
import Pos.Crypto.Hashing (Hash)
4747

4848
data GenesisConfiguration
@@ -123,17 +123,22 @@ instance FromJSON GenesisConfiguration where
123123
data CoreConfiguration = CoreConfiguration
124124
{
125125
-- | Specifies the genesis
126-
ccGenesis :: !GenesisConfiguration
126+
ccGenesis :: !GenesisConfiguration
127127

128128
-- | Versioning for values in node's DB
129-
, ccDbSerializeVersion :: !Word8
129+
, ccDbSerializeVersion :: !Word8
130+
131+
-- | Specifies whether address discrimination takes place on this chain
132+
, ccRequiresNetworkMagic :: !RequiresNetworkMagic
130133

131134
}
132135
deriving (Show, Generic)
133136

134137

135138
defaultCoreConfiguration :: ProtocolMagic -> CoreConfiguration
136-
defaultCoreConfiguration pm = CoreConfiguration (GCSpec (defaultGenesisSpec pm)) 0
139+
defaultCoreConfiguration pm = CoreConfiguration (GCSpec (defaultGenesisSpec pm))
140+
0
141+
NMMustBeJust
137142

138143
defaultGenesisSpec :: ProtocolMagic -> GenesisSpec
139144
defaultGenesisSpec pm = UnsafeGenesisSpec

core/src/Pos/Core/Genesis/Canonical.hs

+18-6
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,24 @@ instance Monad m => ToJSON m GenesisProtocolConstants where
196196
]
197197

198198
instance Monad m => ToJSON m ProtocolMagic where
199-
toJSON (ProtocolMagic (ProtocolMagicId ident) rnm) = do
200-
(\jsIdent jsRNM -> JSObject
201-
[ ("pm", jsIdent)
202-
, ("requiresNetworkMagic", jsRNM) ])
203-
<$> toJSON ident
204-
<*> toJSON rnm
199+
-- | We only output the `ProtocolMagicId` such that we don't alter the
200+
-- resulting hash digest of the genesis block.
201+
--
202+
-- In the function, `withCoreConfigurations`, we compare the hash of the
203+
-- canonical JSON representation of a hardcoded genesis block with an
204+
-- accompanying hardcoded hash of that same genesis block at its inception
205+
-- (both of which can be found in lib/configuration.yaml). This allows us
206+
-- to verify the integrity of the genesis block and ensure that it hasn't
207+
-- been altered.
208+
--
209+
-- As a result of this addition of the `RequiresNetworkMagic` field to
210+
-- `ProtocolMagic`, we cannot include the newly introduced
211+
-- `RequiresNetworkMagic` field of `ProtocolMagic` as it would produce
212+
-- invalid hashes for previously existing genesis blocks.
213+
--
214+
-- See the implementation of `withCoreConfigurations` for more detail on
215+
-- how this works.
216+
toJSON (ProtocolMagic (ProtocolMagicId ident) _rnm) = toJSON ident
205217

206218
instance Monad m => ToJSON m GenesisAvvmBalances where
207219
toJSON = toJSON . getGenesisAvvmBalances

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

+15-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ module Test.Pos.Core.Arbitrary
2121
, genVssCertificate
2222
, genSlotId
2323
, genLocalSlotIndex
24+
, genGenesisData
25+
, genGenesisProtocolConstants
2426
) where
2527

2628
import Universum
@@ -558,15 +560,22 @@ instance Arbitrary ProtocolConstants where
558560
ProtocolConstants <$> choose (1, 20000) <*> pure vssMin <*> pure vssMax
559561

560562
instance Arbitrary G.GenesisProtocolConstants where
561-
arbitrary = do
562-
pm <- arbitrary
563-
flip G.genesisProtocolConstantsFromProtocolConstants pm <$> arbitrary
563+
arbitrary = genGenesisProtocolConstants arbitrary
564+
565+
genGenesisProtocolConstants
566+
:: Gen ProtocolMagic
567+
-> Gen G.GenesisProtocolConstants
568+
genGenesisProtocolConstants genPM =
569+
flip G.genesisProtocolConstantsFromProtocolConstants <$> genPM <*> arbitrary
564570

565571
instance (HasProtocolConstants) => Arbitrary G.GenesisData where
566-
arbitrary = G.GenesisData
572+
arbitrary = genGenesisData arbitrary
573+
574+
genGenesisData :: Gen G.GenesisProtocolConstants -> Gen G.GenesisData
575+
genGenesisData genGPC = G.GenesisData
567576
<$> arbitrary <*> arbitrary <*> arbitraryStartTime
568577
<*> arbitraryVssCerts <*> arbitrary <*> arbitraryBVD
569-
<*> arbitrary <*> arbitrary <*> arbitrary
578+
<*> genGPC <*> arbitrary <*> arbitrary
570579
where
571580
-- System start time should be multiple of a second.
572581
arbitraryStartTime = Timestamp . convertUnit @Second <$> arbitrary
@@ -576,6 +585,7 @@ instance (HasProtocolConstants) => Arbitrary G.GenesisData where
576585
True
577586
hasKnownFeePolicy _ = False
578587
arbitraryVssCerts = G.GenesisVssCertificatesMap . mkVssCertificatesMapLossy <$> arbitrary
588+
579589
----------------------------------------------------------------------------
580590
-- Arbitrary miscellaneous types
581591
----------------------------------------------------------------------------

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ module Test.Pos.Core.ExampleHelpers
9696

9797
-- Helpers
9898
, feedPM
99+
, feedPMWithNMMustBeJust
99100
, feedPC
100101
, feedPMC
101102
) where
@@ -168,7 +169,7 @@ import Pos.Merkle (mkMerkleTree, mtRoot)
168169

169170
import Test.Pos.Core.Gen (genProtocolConstants)
170171
import Test.Pos.Crypto.Bi (getBytes)
171-
import Test.Pos.Crypto.Gen (genProtocolMagic)
172+
import Test.Pos.Crypto.Gen (genProtocolMagic, genProtocolMagicId)
172173

173174
--------------------------------------------------------------------------------
174175
-- Helpers
@@ -177,6 +178,11 @@ import Test.Pos.Crypto.Gen (genProtocolMagic)
177178
feedPM :: (ProtocolMagic -> H.Gen a) -> H.Gen a
178179
feedPM genA = genA =<< genProtocolMagic
179180

181+
feedPMWithNMMustBeJust :: (ProtocolMagic -> H.Gen a) -> H.Gen a
182+
feedPMWithNMMustBeJust genA = do
183+
pm <- flip ProtocolMagic NMMustBeJust <$> genProtocolMagicId
184+
genA pm
185+
180186
feedPC :: (ProtocolConstants -> H.Gen a) -> H.Gen a
181187
feedPC genA = genA =<< genProtocolConstants
182188

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ import Pos.Util.Util (leftToPanic)
218218
import Serokell.Data.Memory.Units (Byte)
219219

220220
import Test.Pos.Crypto.Gen (genAbstractHash, genDecShare, genHDAddressPayload,
221-
genProtocolMagic, genProxySignature, genPublicKey,
222-
genRedeemPublicKey, genRedeemSignature, genSafeSigner,
221+
genProxySignature, genPublicKey, genRedeemPublicKey,
222+
genRedeemSignature, genRequiresNetworkMagic, genSafeSigner,
223223
genSecretKey, genSignTag, genSignature, genVssPublicKey)
224224

225225

@@ -494,6 +494,7 @@ genCoreConfiguration pm =
494494
CoreConfiguration
495495
<$> genGenesisConfiguration pm
496496
<*> genWord8
497+
<*> genRequiresNetworkMagic
497498

498499
----------------------------------------------------------------------------
499500
-- Pos.Core.Delegation Generators
@@ -542,7 +543,7 @@ genGenesisData pm =
542543
<*> genGenesisVssCertificatesMap pm
543544
<*> genGenesisNonAvvmBalances
544545
<*> genBlockVersionDataByTxFP genLinearTxFP
545-
<*> genGenesisProtocolConstants
546+
<*> genGenesisProtocolConstants pm
546547
<*> genGenesisAvvmBalances
547548
<*> genSharedSeed
548549
where
@@ -586,11 +587,11 @@ genGenesisInitializer =
586587
<*> Gen.bool
587588
<*> Gen.integral (Range.constant 0 10)
588589

589-
genGenesisProtocolConstants :: Gen GenesisProtocolConstants
590-
genGenesisProtocolConstants =
590+
genGenesisProtocolConstants :: ProtocolMagic -> Gen GenesisProtocolConstants
591+
genGenesisProtocolConstants pm =
591592
GenesisProtocolConstants
592593
<$> Gen.int (Range.constant 0 100)
593-
<*> genProtocolMagic
594+
<*> pure pm
594595
<*> genVssMaxTTL
595596
<*> genVssMinTTL
596597

@@ -602,7 +603,7 @@ genGenesisSpec pm = mkGenSpec >>= either (error . toText) pure
602603
<*> genSharedSeed
603604
<*> genGenesisDelegation pm
604605
<*> genBlockVersionData
605-
<*> genGenesisProtocolConstants
606+
<*> genGenesisProtocolConstants pm
606607
<*> genGenesisInitializer
607608

608609
genTestnetBalanceOptions :: Gen TestnetBalanceOptions

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import Test.Pos.Core.ExampleHelpers (exampleAddress, exampleAddress1,
2121
exampleGenesisProtocolConstants0,
2222
exampleGenesisProtocolConstants0YesNetworkMagic,
2323
exampleGenesisProtocolConstants1,
24-
exampleGenesisProtocolConstants2, feedPM)
24+
exampleGenesisProtocolConstants2, feedPM,
25+
feedPMWithNMMustBeJust)
2526
import Test.Pos.Core.Gen (genAddress, genBlockVersionData, genByte, genCoin,
2627
genCoinPortion, genEpochIndex, genFlatSlotId,
2728
genGenesisAvvmBalances, genGenesisConfiguration, genGenesisData,
@@ -138,7 +139,7 @@ golden_GenesisDataDec2 =
138139

139140
roundTripGenesisData :: Property
140141
roundTripGenesisData =
141-
eachOf 100 (feedPM genGenesisData) roundTripsCanonicalJSONShow
142+
eachOf 100 (feedPMWithNMMustBeJust genGenesisData) roundTripsCanonicalJSONShow
142143

143144
--------------------------------------------------------------------------------
144145
-- GenesisAvvmBalances
@@ -264,7 +265,7 @@ golden_GenesisProtocolConstants2Dec =
264265

265266
roundTripGenesisProtocolConstants :: Property
266267
roundTripGenesisProtocolConstants =
267-
eachOf 1000 genGenesisProtocolConstants roundTripsAesonShow
268+
eachOf 1000 (feedPM genGenesisProtocolConstants) roundTripsAesonShow
268269

269270
--------------------------------------------------------------------------------
270271
-- GenesisInitializer

crypto/test/Test/Pos/Crypto/Gen.hs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Test.Pos.Crypto.Gen
33
-- Protocol Magic Generator
44
genProtocolMagic
55
, genProtocolMagicId
6+
, genRequiresNetworkMagic
67

78
-- Sign Tag Generator
89
, genSignTag

lib/configuration.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ mainnet_base: &mainnet_base
259259
avvmDistr: {}
260260

261261
dbSerializeVersion: 0
262+
requiresNetworkMagic: NMMustBeNothing
262263

263264
ntp:
264265
responseTimeout: 30000000 # 30 sec

0 commit comments

Comments
 (0)