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

Commit 2160095

Browse files
authored
Merge pull request #3775 from input-output-hk/i+j/develop/CO-410/implement-networkmagic
[CO-410] Implement `NetworkMagic` logic
2 parents 150fb11 + 80e3d99 commit 2160095

File tree

63 files changed

+863
-710
lines changed

Some content is hidden

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

63 files changed

+863
-710
lines changed

auxx/src/Command/Proc.hs

+12-12
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import Pos.Core (AddrStakeDistribution (..), StakeholderId,
2828
addressHash, mkMultiKeyDistr, unsafeGetCoin)
2929
import Pos.Core.Common (AddrAttributes (..), AddrSpendingData (..),
3030
makeAddress)
31-
import Pos.Core.NetworkMagic (NetworkMagic (..))
31+
import Pos.Core.NetworkMagic (makeNetworkMagic)
3232
import Pos.Crypto (PublicKey, emptyPassphrase, encToPublic,
3333
fullPublicKeyF, hashHexF, noPassEncrypt, safeCreatePsk,
3434
unsafeCheatingHashCoerce, withSafeSigner)
@@ -105,6 +105,7 @@ createCommandProcs mCoreConfig mTxpConfig hasAuxxMode printAction mDiffusion = r
105105
let name = "addr" in
106106
needsCoreConfig name >>= \genesisConfig ->
107107
needsAuxxMode name >>= \Dict ->
108+
let nm = makeNetworkMagic (configProtocolMagic genesisConfig) in
108109
return CommandProc
109110
{ cpName = name
110111
, cpArgumentPrepare = map
@@ -115,9 +116,9 @@ createCommandProcs mCoreConfig mTxpConfig hasAuxxMode printAction mDiffusion = r
115116
, cpExec = \(pk', mDistr) -> do
116117
pk <- toLeft pk'
117118
addr <- case mDistr of
118-
Nothing -> makePubKeyAddressAuxx fixedNM (configEpochSlots genesisConfig) pk
119+
Nothing -> makePubKeyAddressAuxx nm (configEpochSlots genesisConfig) pk
119120
Just distr -> return $
120-
makeAddress (PubKeyASD pk) (AddrAttributes Nothing distr fixedNM)
121+
makeAddress (PubKeyASD pk) (AddrAttributes Nothing distr nm)
121122
return $ ValueAddress addr
122123
, cpHelp = "address for the specified public key. a stake distribution \
123124
\ can be specified manually (by default it uses the current epoch \
@@ -136,7 +137,8 @@ createCommandProcs mCoreConfig mTxpConfig hasAuxxMode printAction mDiffusion = r
136137
sk <- evaluateWHNF (sks !! i) -- WHNF is sufficient to force possible errors
137138
-- from using (!!). I'd use NF but there's no
138139
-- NFData instance for secret keys.
139-
addrHD <- deriveHDAddressAuxx fixedNM (configEpochSlots genesisConfig) sk
140+
let nm = makeNetworkMagic (configProtocolMagic genesisConfig)
141+
addrHD <- deriveHDAddressAuxx nm (configEpochSlots genesisConfig) sk
140142
return $ ValueAddress addrHD
141143
, cpHelp = "address of the HD wallet for the specified public key"
142144
},
@@ -195,13 +197,14 @@ createCommandProcs mCoreConfig mTxpConfig hasAuxxMode printAction mDiffusion = r
195197
let name = "balance" in
196198
needsCoreConfig name >>= \genesisConfig ->
197199
needsAuxxMode name >>= \Dict ->
200+
let nm = makeNetworkMagic (configProtocolMagic genesisConfig) in
198201
return CommandProc
199202
{ cpName = name
200203
, cpArgumentPrepare = identity
201204
, cpArgumentConsumer = getArg (tyAddress `tyEither` tyPublicKey `tyEither` tyInt) "addr"
202205
, cpExec = \addr' -> do
203206
addr <-
204-
either return (makePubKeyAddressAuxx fixedNM $ configEpochSlots genesisConfig) <=<
207+
either return (makePubKeyAddressAuxx nm $ configEpochSlots genesisConfig) <=<
205208
traverse (either return getPublicKeyFromIndex) $ addr'
206209
balance <- getBalance (configGenesisData genesisConfig) addr
207210
return $ ValueNumber (fromIntegral . unsafeGetCoin $ balance)
@@ -489,6 +492,7 @@ createCommandProcs mCoreConfig mTxpConfig hasAuxxMode printAction mDiffusion = r
489492
let name = "listaddr" in
490493
needsCoreConfig name >>= \genesisConfig ->
491494
needsAuxxMode name >>= \Dict ->
495+
let nm = makeNetworkMagic (configProtocolMagic genesisConfig) in
492496
return CommandProc
493497
{ cpName = name
494498
, cpArgumentPrepare = identity
@@ -499,8 +503,8 @@ createCommandProcs mCoreConfig mTxpConfig hasAuxxMode printAction mDiffusion = r
499503
printAction "Available addresses:"
500504
for_ (zip [0 :: Int ..] sks) $ \(i, sk) -> do
501505
let pk = encToPublic sk
502-
addr <- makePubKeyAddressAuxx fixedNM epochSlots pk
503-
addrHD <- deriveHDAddressAuxx fixedNM epochSlots sk
506+
addr <- makePubKeyAddressAuxx nm epochSlots pk
507+
addrHD <- deriveHDAddressAuxx nm epochSlots sk
504508
printAction $
505509
sformat (" #"%int%": addr: "%build%"\n"%
506510
" pk: "%fullPublicKeyF%"\n"%
@@ -509,7 +513,7 @@ createCommandProcs mCoreConfig mTxpConfig hasAuxxMode printAction mDiffusion = r
509513
i addr pk (addressHash pk) addrHD
510514
walletMB <- (^. usWallet) <$> (view userSecret >>= readTVarIO)
511515
whenJust walletMB $ \wallet -> do
512-
addrHD <- deriveHDAddressAuxx fixedNM epochSlots (_wusRootKey wallet)
516+
addrHD <- deriveHDAddressAuxx nm epochSlots (_wusRootKey wallet)
513517
printAction $
514518
sformat (" Wallet address:\n"%
515519
" HD addr: "%build)
@@ -566,7 +570,3 @@ getPublicKeyFromIndex i = do
566570
let sk = sks !! i
567571
pk = encToPublic sk
568572
evaluateNF pk
569-
570-
571-
fixedNM :: NetworkMagic
572-
fixedNM = NetworkMainOrStage

auxx/src/Command/Tx.hs

+6-7
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import Pos.Core (IsBootstrapEraAddr (..), Timestamp (..),
4646
deriveFirstHDAddress, makePubKeyAddress, mkCoin)
4747
import Pos.Core.Conc (concurrently, currentTime, delay,
4848
forConcurrently, modifySharedAtomic, newSharedAtomic)
49-
import Pos.Core.NetworkMagic (NetworkMagic (..))
49+
import Pos.Core.NetworkMagic (makeNetworkMagic)
5050
import Pos.Crypto (EncryptedSecretKey, SecretKey, emptyPassphrase,
5151
encToPublic, fakeSigner, hash, safeToPublic, toPublic,
5252
withSafeSigners)
@@ -111,13 +111,14 @@ sendToAllGenesis genesisConfig keysToSend diffusion (SendToAllGenesisParams gene
111111
logInfo $ sformat ("Found "%shown%" keys in the genesis block.") (length keysToSend)
112112
startAtTxt <- liftIO $ lookupEnv "AUXX_START_AT"
113113
let startAt = fromMaybe 0 . readMaybe . fromMaybe "" $ startAtTxt :: Int
114+
let nm = makeNetworkMagic $ configProtocolMagic genesisConfig
114115
-- construct a transaction, and add it to the queue
115116
let addTx secretKey = do
116117
let signer = fakeSigner secretKey
117118
publicKey = toPublic secretKey
118119
-- construct transaction output
119120
outAddr <- makePubKeyAddressAuxx
120-
fixedNM
121+
nm
121122
(configEpochSlots genesisConfig)
122123
publicKey
123124
let txOut1 = TxOut {
@@ -234,11 +235,12 @@ send
234235
-> m ()
235236
send genesisConfig diffusion idx outputs = do
236237
skey <- takeSecret
238+
let nm = makeNetworkMagic $ configProtocolMagic genesisConfig
237239
let curPk = encToPublic skey
238-
let plainAddresses = map (flip (makePubKeyAddress fixedNM) curPk . IsBootstrapEraAddr) [False, True]
240+
let plainAddresses = map (flip (makePubKeyAddress nm) curPk . IsBootstrapEraAddr) [False, True]
239241
let (hdAddresses, hdSecrets) = unzip $ map
240242
(\ibea -> fromMaybe (error "send: pass mismatch") $
241-
deriveFirstHDAddress fixedNM (IsBootstrapEraAddr ibea) emptyPassphrase skey) [False, True]
243+
deriveFirstHDAddress nm (IsBootstrapEraAddr ibea) emptyPassphrase skey) [False, True]
242244
let allAddresses = hdAddresses ++ plainAddresses
243245
let allSecrets = hdSecrets ++ [skey, skey]
244246
etx <- withSafeSigners allSecrets (pure emptyPassphrase) $ \signers -> runExceptT @AuxxException $ do
@@ -286,6 +288,3 @@ sendTxsFromFile diffusion txsFile = do
286288
(topsortTxAuxes txAuxes)
287289
let submitOne = submitTxRaw diffusion
288290
mapM_ submitOne sortedTxAuxes
289-
290-
fixedNM :: NetworkMagic
291-
fixedNM = NetworkMainOrStage

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ verifyAndApplyTx ::
145145
-> ExceptT ToilVerFailure UtxoM TxUndo
146146
verifyAndApplyTx pm adoptedBVD lockedAssets curEpoch verifyVersions tx@(_, txAux) = do
147147
whenLeft (checkTxAux txAux) (throwError . ToilInconsistentTxAux)
148-
let nm = makeNetworkMagic pm
149-
ctx = Utxo.VTxContext verifyVersions nm
148+
let ctx = Utxo.VTxContext verifyVersions (makeNetworkMagic pm)
150149
vtur@VerifyTxUtxoRes {..} <- Utxo.verifyTxUtxo pm ctx lockedAssets txAux
151150
liftEither $ verifyGState adoptedBVD curEpoch txAux vtur
152151
lift $ applyTxToUtxo' tx

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import Pos.Core.Attributes (mkAttributes)
4343
import Pos.Core.Common (Coin, IsBootstrapEraAddr (..),
4444
makePubKeyAddress)
4545
import Pos.Core.Merkle (MerkleNode (..), MerkleRoot (..))
46-
import Pos.Core.NetworkMagic (NetworkMagic (..))
46+
import Pos.Core.NetworkMagic (makeNetworkMagic)
4747
import Pos.Crypto (Hash, ProtocolMagic, SecretKey, SignTag (SignTx),
4848
hash, sign, toPublic)
4949

@@ -156,7 +156,7 @@ buildProperTx pm inputList (inCoin, outCoin) =
156156
mkWitness fromSk =
157157
PkWitness (toPublic fromSk) (sign pm SignTx fromSk $ TxSigData newTxHash)
158158
makeTxOutput s c =
159-
TxOut (makePubKeyAddress fixedNM (IsBootstrapEraAddr True) $ toPublic s) c
159+
TxOut (makePubKeyAddress (makeNetworkMagic pm) (IsBootstrapEraAddr True) $ toPublic s) c
160160

161161
-- | Well-formed transaction 'Tx'.
162162
--
@@ -248,6 +248,3 @@ genTxPayload pm = mkTxPayload <$> genTxOutDist pm
248248
instance Arbitrary TxPayload where
249249
arbitrary = genTxPayload dummyProtocolMagic
250250
shrink = genericShrink
251-
252-
fixedNM :: NetworkMagic
253-
fixedNM = NetworkMainOrStage

chain/test/Test/Pos/Chain/Txp/Example.hs

+1-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ exampleTxInUtxo :: TxIn
5858
exampleTxInUtxo = TxInUtxo exampleHashTx 47 -- TODO: loop here
5959

6060
exampleTxOut :: TxOut
61-
exampleTxOut = TxOut (makePubKeyAddress fixedNM (IsBootstrapEraAddr True) pkey) (Coin 47)
61+
exampleTxOut = TxOut (makePubKeyAddress NetworkMainOrStage (IsBootstrapEraAddr True) pkey) (Coin 47)
6262
where
6363
Right pkey = PublicKey <$> CC.xpub (getBytes 0 64)
6464

@@ -93,6 +93,3 @@ exampleRedeemSignature = redeemSign exampleProtocolMagic SignForTestingOnly rsk
9393

9494
exampleHashTx :: Hash Tx
9595
exampleHashTx = coerce (hash "golden" :: Hash Text)
96-
97-
fixedNM :: NetworkMagic
98-
fixedNM = NetworkMainOrStage

0 commit comments

Comments
 (0)