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

Commit

Permalink
Merge pull request #3482 from input-output-hk/ruhatch/CDEC-509
Browse files Browse the repository at this point in the history
[CDEC-369] Remove HasProtocolConstants in favour of parameters
  • Loading branch information
erikd authored Aug 28, 2018
2 parents a8911cb + 737f771 commit 7683f28
Show file tree
Hide file tree
Showing 201 changed files with 3,224 additions and 2,826 deletions.
17 changes: 8 additions & 9 deletions auxx/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import Ntp.Client (NtpConfiguration)
import Pos.Chain.Txp (TxpConfiguration)
import qualified Pos.Client.CLI as CLI
import Pos.Context (NodeContext (..))
import Pos.Core as Core (Config (..), ConfigurationError, epochSlots)
import Pos.Crypto (ProtocolMagic)
import Pos.Core as Core (Config (..), ConfigurationError,
configBlkSecurityParam)
import Pos.DB.DB (initNodeDBs)
import Pos.DB.Txp (txpGlobalSettings)
import Pos.Infra.Diffusion.Types (Diffusion, hoistDiffusion)
Expand Down Expand Up @@ -77,13 +77,13 @@ correctNodeParams AuxxOptions {..} np = do

runNodeWithSinglePlugin ::
(HasConfigurations, HasCompileInfo)
=> ProtocolMagic
=> Core.Config
-> TxpConfiguration
-> NodeResources EmptyMempoolExt
-> (Diffusion AuxxMode -> AuxxMode ())
-> Diffusion AuxxMode -> AuxxMode ()
runNodeWithSinglePlugin pm txpConfig nr plugin =
runNode pm txpConfig nr [plugin]
runNodeWithSinglePlugin coreConfig txpConfig nr plugin =
runNode coreConfig txpConfig nr [plugin]

action :: HasCompileInfo => AuxxOptions -> Either WithCommandAction Text -> IO ()
action opts@AuxxOptions {..} command = do
Expand All @@ -97,7 +97,6 @@ action opts@AuxxOptions {..} command = do
Light
-> runWithoutNode pa
_ -> withConfigurations Nothing conf (runWithConfig pa)

where
runWithoutNode :: PrintAction IO -> IO ()
runWithoutNode printAction = printAction "Mode: light" >> rawExec Nothing Nothing Nothing opts Nothing command
Expand Down Expand Up @@ -125,13 +124,13 @@ action opts@AuxxOptions {..} command = do
sscParams = CLI.gtSscParams cArgs vssSK (npBehaviorConfig nodeParams)

let pm = configProtocolMagic coreConfig
bracketNodeResources nodeParams sscParams (txpGlobalSettings pm txpConfig) (initNodeDBs pm epochSlots) $ \nr ->
bracketNodeResources (configBlkSecurityParam coreConfig) nodeParams sscParams (txpGlobalSettings pm txpConfig) (initNodeDBs coreConfig) $ \nr ->
let NodeContext {..} = nrContext nr
modifier = if aoStartMode == WithNode
then runNodeWithSinglePlugin pm txpConfig nr
then runNodeWithSinglePlugin coreConfig txpConfig nr
else identity
auxxModeAction = modifier (auxxPlugin coreConfig txpConfig opts command)
in runRealMode pm txpConfig nr $ \diffusion ->
in runRealMode coreConfig txpConfig nr $ \diffusion ->
toRealMode (auxxModeAction (hoistDiffusion realModeToAuxx toRealMode diffusion))

cArgs@CLI.CommonNodeArgs {..} = aoCommonNodeArgs
Expand Down
13 changes: 13 additions & 0 deletions auxx/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
help: ## Print documentation
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

ghcid: ## Run ghcid with the cardano-sl-auxx package
ghcid \
--command "stack ghci cardano-sl-auxx --ghci-options=-fno-code"

ghcid-test: ## Have ghcid run the test suite for the wallet-new-specs on successful recompile
ghcid \
--command "stack ghci cardano-sl-auxx:lib cardano-sl-auxx:test:cardano-auxx-test --ghci-options=-fobject-code" \
--test "Main.main"

.PHONY: ghcid ghcid-test help
22 changes: 13 additions & 9 deletions auxx/src/Command/BlockGen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import System.Random (mkStdGen, randomIO)
import Pos.AllSecrets (mkAllSecretsSimple)
import Pos.Chain.Txp (TxpConfiguration)
import Pos.Client.KeyStorage (getSecretKeysPlain)
import Pos.Core (genesisData)
import Pos.Core as Core (Config (..), genesisData)
import Pos.Core.Genesis (gdBootStakeholders)
import Pos.Crypto (ProtocolMagic, encToSecret)
import Pos.Crypto (encToSecret)
import Pos.DB.Txp (txpGlobalSettings)
import Pos.Generator.Block (BlockGenParams (..), genBlocks,
tgpTxCountRange)
Expand All @@ -29,12 +29,12 @@ import Pos.Util.Wlog (logInfo)
import Lang.Value (GenBlocksParams (..))
import Mode (MonadAuxxMode)


generateBlocks :: MonadAuxxMode m
=> ProtocolMagic
=> Core.Config
-> TxpConfiguration
-> GenBlocksParams -> m ()
generateBlocks pm txpConfig GenBlocksParams{..} = withStateLock HighPriority ApplyBlock $ \_ -> do
-> GenBlocksParams
-> m ()
generateBlocks coreConfig txpConfig GenBlocksParams{..} = withStateLock HighPriority ApplyBlock $ \_ -> do
seed <- liftIO $ maybe randomIO pure bgoSeed
logInfo $ "Generating with seed " <> show seed

Expand All @@ -45,13 +45,17 @@ generateBlocks pm txpConfig GenBlocksParams{..} = withStateLock HighPriority App
{ _bgpSecrets = allSecrets
, _bgpGenStakeholders = gdBootStakeholders genesisData
, _bgpBlockCount = fromIntegral bgoBlockN
-- tx generation is disalbed for now
-- tx generation is disabled for now
, _bgpTxGenParams = def & tgpTxCountRange .~ (0,0)
, _bgpInplaceDB = True
, _bgpSkipNoKey = True
, _bgpTxpGlobalSettings = txpGlobalSettings pm txpConfig
, _bgpTxpGlobalSettings = txpGlobalSettings
(configProtocolMagic coreConfig)
txpConfig
}
withCompileInfo $ evalRandT (genBlocks pm txpConfig bgenParams (const ())) (mkStdGen seed)
withCompileInfo $ evalRandT
(genBlocks coreConfig txpConfig bgenParams (const ()))
(mkStdGen seed)
-- We print it twice because there can be a ton of logs and
-- you don't notice the first message.
logInfo $ "Generated with seed " <> show seed
39 changes: 19 additions & 20 deletions auxx/src/Command/Proc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import Pos.Chain.Txp (TxpConfiguration)
import Pos.Chain.Update (BlockVersionModifier (..))
import Pos.Client.KeyStorage (addSecretKey, getSecretKeysPlain)
import Pos.Client.Txp.Balances (getBalance)
import Pos.Core as Core (AddrStakeDistribution (..), Address,
Config (..), StakeholderId, addressHash,
import Pos.Core as Core (AddrStakeDistribution (..), Config (..),
StakeholderId, addressHash, configEpochSlots,
configGeneratedSecretsThrow, mkMultiKeyDistr,
unsafeGetCoin)
import Pos.Core.Common (AddrAttributes (..), AddrSpendingData (..),
Expand Down Expand Up @@ -103,6 +103,7 @@ createCommandProcs mCoreConfig mTxpConfig hasAuxxMode printAction mDiffusion = r
},

let name = "addr" in
needsCoreConfig name >>= \coreConfig ->
needsAuxxMode name >>= \Dict ->
return CommandProc
{ cpName = name
Expand All @@ -114,7 +115,7 @@ createCommandProcs mCoreConfig mTxpConfig hasAuxxMode printAction mDiffusion = r
, cpExec = \(pk', mDistr) -> do
pk <- toLeft pk'
addr <- case mDistr of
Nothing -> makePubKeyAddressAuxx pk
Nothing -> makePubKeyAddressAuxx (configEpochSlots coreConfig) pk
Just distr -> return $
makeAddress (PubKeyASD pk) (AddrAttributes Nothing distr)
return $ ValueAddress addr
Expand All @@ -124,6 +125,7 @@ createCommandProcs mCoreConfig mTxpConfig hasAuxxMode printAction mDiffusion = r
},

let name = "addr-hd" in
needsCoreConfig name >>= \coreConfig ->
needsAuxxMode name >>= \Dict ->
return CommandProc
{ cpName = name
Expand All @@ -134,7 +136,7 @@ createCommandProcs mCoreConfig mTxpConfig hasAuxxMode printAction mDiffusion = r
sk <- evaluateWHNF (sks !! i) -- WHNF is sufficient to force possible errors
-- from using (!!). I'd use NF but there's no
-- NFData instance for secret keys.
addrHD <- deriveHDAddressAuxx sk
addrHD <- deriveHDAddressAuxx (configEpochSlots coreConfig) sk
return $ ValueAddress addrHD
, cpHelp = "address of the HD wallet for the specified public key"
},
Expand Down Expand Up @@ -191,13 +193,16 @@ createCommandProcs mCoreConfig mTxpConfig hasAuxxMode printAction mDiffusion = r
return . procConst "false" $ ValueBool False,

let name = "balance" in
needsCoreConfig name >>= \coreConfig ->
needsAuxxMode name >>= \Dict ->
return CommandProc
{ cpName = name
, cpArgumentPrepare = identity
, cpArgumentConsumer = getArg (tyAddress `tyEither` tyPublicKey `tyEither` tyInt) "addr"
, cpExec = \addr' -> do
addr <- toLeft addr'
addr <-
either return (makePubKeyAddressAuxx $ configEpochSlots coreConfig) <=<
traverse (either return getPublicKeyFromIndex) $ addr'
balance <- getBalance addr
return $ ValueNumber (fromIntegral . unsafeGetCoin $ balance)
, cpHelp = "check the amount of coins on the specified address"
Expand Down Expand Up @@ -229,10 +234,7 @@ createCommandProcs mCoreConfig mTxpConfig hasAuxxMode printAction mDiffusion = r
return Tx.SendToAllGenesisParams{..}
, cpExec = \stagp -> do
secretKeys <- gsSecretKeys <$> configGeneratedSecretsThrow coreConfig
Tx.sendToAllGenesis (configProtocolMagic coreConfig)
secretKeys
diffusion
stagp
Tx.sendToAllGenesis coreConfig secretKeys diffusion stagp
return ValueUnit
, cpHelp = "create and send transactions from all genesis addresses \
\ for <duration> seconds, <delay> in ms. <conc> is the \
Expand Down Expand Up @@ -263,7 +265,7 @@ createCommandProcs mCoreConfig mTxpConfig hasAuxxMode printAction mDiffusion = r
(,) <$> getArg tyInt "i"
<*> getArgSome tyTxOut "out"
, cpExec = \(i, outputs) -> do
Tx.send (configProtocolMagic coreConfig) diffusion i outputs
Tx.send coreConfig diffusion i outputs
return ValueUnit
, cpHelp = "send from #i to specified transaction outputs \
\ (use 'tx-out' to build them)"
Expand Down Expand Up @@ -424,7 +426,7 @@ createCommandProcs mCoreConfig mTxpConfig hasAuxxMode printAction mDiffusion = r
bgoSeed <- getArgOpt tyInt "seed"
return GenBlocksParams{..}
, cpExec = \params -> do
generateBlocks (configProtocolMagic coreConfig) txpConfig params
generateBlocks coreConfig txpConfig params
return ValueUnit
, cpHelp = "generate <n> blocks"
},
Expand Down Expand Up @@ -479,26 +481,26 @@ createCommandProcs mCoreConfig mTxpConfig hasAuxxMode printAction mDiffusion = r
rpDumpPath <- getArg tyFilePath "dump-file"
pure RollbackParams{..}
, cpExec = \RollbackParams{..} -> do
Rollback.rollbackAndDump (configProtocolMagic coreConfig)
rpNum
rpDumpPath
Rollback.rollbackAndDump coreConfig rpNum rpDumpPath
return ValueUnit
, cpHelp = ""
},

let name = "listaddr" in
needsCoreConfig name >>= \coreConfig ->
needsAuxxMode name >>= \Dict ->
return CommandProc
{ cpName = name
, cpArgumentPrepare = identity
, cpArgumentConsumer = do pure ()
, cpExec = \() -> do
let epochSlots = configEpochSlots coreConfig
sks <- getSecretKeysPlain
printAction "Available addresses:"
for_ (zip [0 :: Int ..] sks) $ \(i, sk) -> do
let pk = encToPublic sk
addr <- makePubKeyAddressAuxx pk
addrHD <- deriveHDAddressAuxx sk
addr <- makePubKeyAddressAuxx epochSlots pk
addrHD <- deriveHDAddressAuxx epochSlots sk
printAction $
sformat (" #"%int%": addr: "%build%"\n"%
" pk: "%fullPublicKeyF%"\n"%
Expand All @@ -507,7 +509,7 @@ createCommandProcs mCoreConfig mTxpConfig hasAuxxMode printAction mDiffusion = r
i addr pk (addressHash pk) addrHD
walletMB <- (^. usWallet) <$> (view userSecret >>= readTVarIO)
whenJust walletMB $ \wallet -> do
addrHD <- deriveHDAddressAuxx (_wusRootKey wallet)
addrHD <- deriveHDAddressAuxx epochSlots (_wusRootKey wallet)
printAction $
sformat (" Wallet address:\n"%
" HD addr: "%build)
Expand Down Expand Up @@ -558,9 +560,6 @@ instance MonadAuxxMode m => ToLeft m PublicKey Int where
instance MonadAuxxMode m => ToLeft m StakeholderId PublicKey where
toLeft = return . either identity addressHash

instance MonadAuxxMode m => ToLeft m Address PublicKey where
toLeft = either return makePubKeyAddressAuxx

getPublicKeyFromIndex :: MonadAuxxMode m => Int -> m PublicKey
getPublicKeyFromIndex i = do
sks <- getSecretKeysPlain
Expand Down
9 changes: 4 additions & 5 deletions auxx/src/Command/Rollback.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ import Formatting (build, int, sformat, string, (%))

import Pos.Chain.Block (Blund, mainBlockTxPayload)
import Pos.Chain.Txp (flattenTxPayload)
import Pos.Core (difficultyL, epochIndexL)
import Pos.Core as Core (Config, difficultyL, epochIndexL)
import Pos.Core.Chrono (NewestFirst, _NewestFirst)
import Pos.Core.Txp (TxAux)
import Pos.Crypto (ProtocolMagic)
import Pos.DB.Block (BypassSecurityCheck (..),
ShouldCallBListener (..), rollbackBlocksUnsafe)
import qualified Pos.DB.Block as DB
Expand All @@ -32,11 +31,11 @@ import Mode (MonadAuxxMode)
-- from it to the given file.
rollbackAndDump
:: MonadAuxxMode m
=> ProtocolMagic
=> Core.Config
-> Word
-> FilePath
-> m ()
rollbackAndDump pm numToRollback outFile = withStateLock HighPriority ApplyBlockWithRollback $ \_ -> do
rollbackAndDump coreConfig numToRollback outFile = withStateLock HighPriority ApplyBlockWithRollback $ \_ -> do
printTipDifficulty
blundsMaybeEmpty <- modifyBlunds <$>
DB.loadBlundsFromTipByDepth (fromIntegral numToRollback)
Expand All @@ -53,7 +52,7 @@ rollbackAndDump pm numToRollback outFile = withStateLock HighPriority ApplyBlock
liftIO $ BSL.writeFile outFile (encode allTxs)
logInfo $ sformat ("Dumped "%int%" transactions to "%string)
(length allTxs) (outFile)
rollbackBlocksUnsafe pm (BypassSecurityCheck True) (ShouldCallBListener True) blunds
rollbackBlocksUnsafe coreConfig (BypassSecurityCheck True) (ShouldCallBListener True) blunds
logInfo $ sformat ("Rolled back "%int%" blocks") (length blunds)
printTipDifficulty
where
Expand Down
35 changes: 20 additions & 15 deletions auxx/src/Command/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,18 @@ import Pos.Client.KeyStorage (getSecretKeysPlain)
import Pos.Client.Txp.Balances (getOwnUtxoForPk)
import Pos.Client.Txp.Network (prepareMTx, submitTxRaw)
import Pos.Client.Txp.Util (createTx)
import Pos.Core (IsBootstrapEraAddr (..), Timestamp (..),
deriveFirstHDAddress, makePubKeyAddress, mkCoin)
import Pos.Core as Core (Config, IsBootstrapEraAddr (..),
Timestamp (..), configEpochSlots, deriveFirstHDAddress,
makePubKeyAddress, mkCoin)
import Pos.Core.Conc (concurrently, currentTime, delay,
forConcurrently, modifySharedAtomic, newSharedAtomic)
import Pos.Core.Configuration (genesisBlockVersionData)
import Pos.Core.Txp (TxAux (..), TxIn (TxInUtxo), TxOut (..),
TxOutAux (..), txaF)
import Pos.Core.Update (BlockVersionData (..))
import Pos.Crypto (EncryptedSecretKey, ProtocolMagic, SecretKey,
emptyPassphrase, encToPublic, fakeSigner, hash,
safeToPublic, toPublic, withSafeSigners)
import Pos.Crypto (EncryptedSecretKey, SecretKey, emptyPassphrase,
encToPublic, fakeSigner, hash, safeToPublic, toPublic,
withSafeSigners)
import Pos.Infra.Diffusion.Types (Diffusion (..))
import Pos.Util.UserSecret (usWallet, userSecret, wusRootKey)
import Pos.Util.Util (maybeThrow)
Expand Down Expand Up @@ -84,13 +85,14 @@ addTxSubmit =
pure (TxCount (submitted + 1) sending, ()))

sendToAllGenesis
:: forall m. MonadAuxxMode m
=> ProtocolMagic
:: forall m
. MonadAuxxMode m
=> Core.Config
-> [SecretKey]
-> Diffusion m
-> SendToAllGenesisParams
-> m ()
sendToAllGenesis pm keysToSend diffusion (SendToAllGenesisParams genesisTxsPerThread txsPerThread conc delay_ tpsSentFile) = do
sendToAllGenesis coreConfig keysToSend diffusion (SendToAllGenesisParams genesisTxsPerThread txsPerThread conc delay_ tpsSentFile) = do
let genesisSlotDuration = fromIntegral (toMicroseconds $ bvdSlotDuration genesisBlockVersionData) `div` 1000000 :: Int
tpsMVar <- newSharedAtomic $ TxCount 0 conc
startTime <- show . toInteger . getTimestamp . Timestamp <$> currentTime
Expand All @@ -114,14 +116,16 @@ sendToAllGenesis pm keysToSend diffusion (SendToAllGenesisParams genesisTxsPerTh
let signer = fakeSigner secretKey
publicKey = toPublic secretKey
-- construct transaction output
outAddr <- makePubKeyAddressAuxx publicKey
outAddr <- makePubKeyAddressAuxx
(configEpochSlots coreConfig)
publicKey
let txOut1 = TxOut {
txOutAddress = outAddr,
txOutValue = mkCoin 1
}
txOuts = TxOutAux txOut1 :| []
utxo <- getOwnUtxoForPk $ safeToPublic signer
etx <- createTx pm mempty utxo signer txOuts publicKey
etx <- createTx coreConfig mempty utxo signer txOuts publicKey
case etx of
Left err -> logError (sformat ("Error: "%build%" while trying to contruct tx") err)
Right (tx, _) -> do
Expand All @@ -143,7 +147,7 @@ sendToAllGenesis pm keysToSend diffusion (SendToAllGenesisParams genesisTxsPerTh
txOuts2 = TxOutAux txOut1' :| []
-- It is expected that the output from the previously sent transaction is
-- included in the UTxO by the time this transaction will actually be sent.
etx' <- createTx pm mempty utxo' (fakeSigner senderKey) txOuts2 (toPublic senderKey)
etx' <- createTx coreConfig mempty utxo' (fakeSigner senderKey) txOuts2 (toPublic senderKey)
case etx' of
Left err -> logError (sformat ("Error: "%build%" while trying to contruct tx") err)
Right (tx', _) -> do
Expand Down Expand Up @@ -218,13 +222,14 @@ newtype AuxxException = AuxxException Text
instance Exception AuxxException

send
:: forall m. MonadAuxxMode m
=> ProtocolMagic
:: forall m
. MonadAuxxMode m
=> Core.Config
-> Diffusion m
-> Int
-> NonEmpty TxOut
-> m ()
send pm diffusion idx outputs = do
send coreConfig diffusion idx outputs = do
skey <- takeSecret
let curPk = encToPublic skey
let plainAddresses = map (flip makePubKeyAddress curPk . IsBootstrapEraAddr) [False, True]
Expand All @@ -237,7 +242,7 @@ send pm diffusion idx outputs = do
let addrSig = HM.fromList $ zip allAddresses signers
let getSigner addr = HM.lookup addr addrSig
-- BE CAREFUL: We create remain address using our pk, wallet doesn't show such addresses
(txAux,_) <- lift $ prepareMTx pm getSigner mempty def (NE.fromList allAddresses) (map TxOutAux outputs) curPk
(txAux,_) <- lift $ prepareMTx coreConfig getSigner mempty def (NE.fromList allAddresses) (map TxOutAux outputs) curPk
txAux <$ (ExceptT $ try $ submitTxRaw diffusion txAux)
case etx of
Left err -> logError $ sformat ("Error: "%stext) (toText $ displayException err)
Expand Down
Loading

0 comments on commit 7683f28

Please sign in to comment.