Skip to content
This repository was archived by the owner on Mar 1, 2019. It is now read-only.

Commit c1a67e1

Browse files
authored
[CO-410] Add RequiresNetworkMagic and modify ProtocolMagic (input-output-hk/cardano-sl#3715)
* [CO-410] Port release/1.3.1 config to develop * [CO-410] Modify ProtocolMagic * [CO-410] Continue modifying ProtocolMagic * [CO-410] Change naming * [CO-410] Add golden files for legacy `RequiresNetworkMagic` * [CO-410] Modify `RequiresNetworkMagic`'s `FromJSON` instance to support legacy encoding * [CO-410] Add golden decoding tests for legacy `ProtocolMagic` encoding * [CO-410] Add golden decode only test for `Configuration` * [CO-410] Run `pkgs/generate.sh`, remove reference files, hardcode `systemTag` in `Configuration_Legacy` golden file
1 parent b73c5c4 commit c1a67e1

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

src/Cardano/Wallet/Kernel/DB/InDb.hs

+32-7
Original file line numberDiff line numberDiff line change
@@ -445,20 +445,20 @@ instance SC.SafeCopy (InDb Core.BlockHeader) where
445445
-- class for the actual fields, so they are actually totally different types.
446446
instance SC.SafeCopy (InDb Core.MainBlockHeader) where
447447
getCopy = SC.contain $ do
448-
InDb protocolMagic <- SC.safeGet
448+
InDb protocolMagicId <- SC.safeGet
449449
InDb prevBlock <- SC.safeGet
450450
InDb bodyProof <- SC.safeGet
451451
InDb consensus <- SC.safeGet
452452
InDb extra <- SC.safeGet
453453
pure . InDb $
454454
Core.mkGenericBlockHeaderUnsafe
455-
protocolMagic
455+
(Core.ProtocolMagic protocolMagicId Core.RequiresNoMagic)
456456
prevBlock
457457
bodyProof
458458
consensus
459459
extra
460460
putCopy (InDb header) = SC.contain $ do
461-
safePutDb $ header ^. Core.gbhProtocolMagic
461+
safePutDb $ header ^. Core.gbhProtocolMagicId
462462
safePutDb $ header ^. Core.gbhPrevBlock
463463
safePutDb $ header ^. Core.gbhBodyProof
464464
safePutDb $ header ^. Core.gbhConsensus
@@ -750,26 +750,51 @@ instance SC.SafeCopy (InDb Core.GenesisBlockHeader) where
750750
InDb extra <- SC.safeGet
751751
pure . InDb $
752752
Core.mkGenericBlockHeaderUnsafe
753-
protocolMagic
753+
(Core.ProtocolMagic protocolMagic Core.RequiresNoMagic)
754754
prevBlock
755755
bodyProof
756756
consensus
757757
extra
758758

759759
putCopy (InDb header) = SC.contain $ do
760-
safePutDb $ header ^. Core.gbhProtocolMagic
760+
safePutDb $ header ^. Core.gbhProtocolMagicId
761761
safePutDb $ header ^. Core.gbhPrevBlock
762762
safePutDb $ header ^. Core.gbhBodyProof
763763
safePutDb $ header ^. Core.gbhConsensus
764764
safePutDb $ header ^. Core.gbhExtra
765765

766766
instance SC.SafeCopy (InDb Core.ProtocolMagic) where
767767
getCopy = SC.contain $ do
768-
InDb . Core.ProtocolMagic <$> SC.safeGet
768+
i <- SC.safeGet
769+
rnm <- SC.safeGet
770+
pure $ Core.ProtocolMagic
771+
<$> i
772+
<*> rnm
773+
774+
putCopy (InDb (Core.ProtocolMagic i rnm)) = SC.contain $ do
775+
safePutDb i
776+
safePutDb rnm
777+
778+
instance SC.SafeCopy (InDb Core.ProtocolMagicId) where
779+
getCopy = SC.contain $ do
780+
InDb . Core.ProtocolMagicId <$> SC.safeGet
769781

770-
putCopy (InDb (Core.ProtocolMagic i)) = SC.contain $ do
782+
putCopy (InDb (Core.ProtocolMagicId i)) = SC.contain $ do
771783
SC.safePut i
772784

785+
instance SC.SafeCopy (InDb Core.RequiresNetworkMagic) where
786+
getCopy = SC.contain $
787+
SC.safeGet >>= \case
788+
0 -> pure (InDb Core.RequiresNoMagic)
789+
1 -> pure (InDb Core.RequiresMagic)
790+
(n :: Word8) -> fail
791+
$ "Expected one of 0,1 for RequiresNetworkMagic tag,\
792+
\ got: "
793+
<> show n
794+
putCopy (InDb rnm) = SC.contain $ case rnm of
795+
Core.RequiresNoMagic -> SC.safePut (0 :: Word8)
796+
Core.RequiresMagic -> SC.safePut (1 :: Word8)
797+
773798
instance SC.SafeCopy (InDb Core.GenesisProof) where
774799
getCopy = SC.contain $ do
775800
hashLeaders <- SC.safeGet

test/MarshallingSpec.hs

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ spec = parallel $ describe "Marshalling & Unmarshalling" $ do
156156
safeCopyRoundTrip @(InDb Core.BlockCount)
157157
safeCopyRoundTrip @(InDb Core.GenesisBlockHeader)
158158
safeCopyRoundTrip @(InDb Core.ProtocolMagic)
159+
safeCopyRoundTrip @(InDb Core.ProtocolMagicId)
160+
safeCopyRoundTrip @(InDb Core.RequiresNetworkMagic)
159161
safeCopyRoundTrip @(InDb Core.GenesisProof)
160162
safeCopyRoundTrip @(InDb Core.GenesisConsensusData)
161163
safeCopyRoundTrip @(InDb Core.GenesisExtraHeaderData)

test/unit/Test/Spec/Submission.hs

+9-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import qualified Formatting as F
2727
import Formatting.Buildable (build)
2828
import qualified Pos.Chain.Txp as Txp
2929
import Pos.Core.Attributes (Attributes (..), UnparsedFields (..))
30-
import Pos.Crypto (ProtocolMagic (..))
30+
import Pos.Crypto (ProtocolMagic (..), ProtocolMagicId (..),
31+
RequiresNetworkMagic (..))
3132
import Pos.Crypto.Hashing (hash)
3233
import Pos.Crypto.Signing.Safe (safeDeterministicKeyGen)
3334
import Serokell.Util.Text (listJsonIndent)
@@ -89,7 +90,7 @@ genSchedule maxRetries pending (Slot lowerBound) = do
8990

9091
genWalletSubmissionState :: HdAccountId -> MaxRetries -> Gen WalletSubmissionState
9192
genWalletSubmissionState accId maxRetries = do
92-
pending <- M.singleton accId <$> genPending (ProtocolMagic 0)
93+
pending <- M.singleton accId <$> genPending protocolMagic
9394
let slot = Slot 0 -- Make the layer always start from 0, to make running the specs predictable.
9495
scheduler <- genSchedule maxRetries pending slot
9596
return $ WalletSubmissionState pending scheduler slot
@@ -178,7 +179,7 @@ dependentTransactions = do
178179
outputForB <- (Txp.TxOut <$> arbitrary <*> arbitrary)
179180
outputForC <- (Txp.TxOut <$> arbitrary <*> arbitrary)
180181
outputForD <- (Txp.TxOut <$> arbitrary <*> arbitrary)
181-
[a,b,c,d] <- vectorOf 4 (Txp.genTxAux (ProtocolMagic 0))
182+
[a,b,c,d] <- vectorOf 4 (Txp.genTxAux protocolMagic)
182183
let a' = a { Txp.taTx = (Txp.taTx a) {
183184
Txp._txInputs = inputForA :| mempty
184185
, Txp._txOutputs = outputForA :| mempty
@@ -219,7 +220,7 @@ genPureWalletSubmission accId =
219220
genPurePair :: Gen (ShowThroughBuild (WalletSubmission, M.Map HdAccountId Pending))
220221
genPurePair = do
221222
STB layer <- genPureWalletSubmission myAccountId
222-
pending <- genPending (ProtocolMagic 0)
223+
pending <- genPending protocolMagic
223224
let pending' = Pending.delete (toTxIdSet $ layer ^. localPendingSet myAccountId) pending
224225
pure $ STB (layer, M.singleton myAccountId pending')
225226

@@ -434,3 +435,7 @@ spec = do
434435
, mustNotIncludeEvents "none of [a,b,c,d] was scheduled" (ScheduleEvents scheduledInSlot2 confirmed2) [a,b,c,d]
435436
, includeEvents "[c,d] scheduled slot 3" (ScheduleEvents scheduledInSlot3 confirmed3) [c,d]
436437
]
438+
439+
440+
protocolMagic :: ProtocolMagic
441+
protocolMagic = ProtocolMagic (ProtocolMagicId 0) RequiresNoMagic

0 commit comments

Comments
 (0)