<commit_before>module Main where

import           Universum

import           Control.Monad.Random.Strict (evalRandT)
import           Data.Default                (def)
import qualified Data.HashMap.Strict         as HM
import qualified Data.Map                    as M
import           Formatting                  (build, sformat, (%))
import           Mockable                    (runProduction)
import           System.Directory            (doesDirectoryExist)
import           System.Random               (mkStdGen, randomIO)
import           System.Wlog                 (usingLoggerName)

import           Pos.Core                    (StakeDistribution (..),
                                              genesisDevSecretKeys,
                                              genesisProdAddrDistribution, isDevelopment,
                                              makePubKeyAddress, mkCoin)
import           Pos.Crypto                  (SecretKey, toPublic)
import           Pos.DB                      (closeNodeDBs, openNodeDBs)
import           Pos.Generator.Block         (AllSecrets (..), BlockGenParams (..),
                                              genBlocks, mkInvSecretsMap, unInvSecretsMap)
import           Pos.Genesis                 (GenesisWStakeholders (..), devAddrDistr,
                                              genesisUtxo)
import           Pos.Txp.Core                (TxOut (..), TxOutAux (..))
import           Pos.Txp.Toil                (GenesisUtxo (..), Utxo, _GenesisUtxo)
import           Pos.Util.UserSecret         (peekUserSecret, usPrimKey)

import           Context                     (initTBlockGenMode)
import           Error                       (TBlockGenError (..))
import           Options                     (BlockGenOptions (..), getBlockGenOptions)

main :: IO ()
main = flip catch catchEx $ do
    if isDevelopment then
        putText $ "Generating in DEV mode"
    else
        putText $ "Generating in PROD mode"
    BlockGenOptions{..} <- getBlockGenOptions
    when bgoAppend $ checkExistence bgoPath
    invSecretsMap <- mkInvSecretsMap <$> case bgoNodes of
        Left bgoNodesN -> do
            unless (bgoNodesN > 0) $
                throwM NoOneSecrets
            let secretKeys = take (fromIntegral bgoNodesN) genesisDevSecretKeys
            pure secretKeys
        Right bgoSecretFiles -> do
            when (null bgoSecretFiles) $
                throwM NoOneSecrets
            usingLoggerName "block-gen" $ mapM parseSecret bgoSecretFiles

    let nodes = length invSecretsMap
    let flatDistr = FlatStakes (fromIntegral nodes) (mkCoin $ fromIntegral nodes)
    let bootStakeholders =
            GenesisWStakeholders $ HM.fromList $
            zip (HM.keys $ unInvSecretsMap invSecretsMap) (repeat 1)
    -- We need to select from utxo TxOut's corresponding to passed secrets
    -- to avoid error "Secret key of %hash% is required but isn't known"
    let genUtxoUnfiltered
            | isDevelopment = genesisUtxo bootStakeholders (fst $ devAddrDistr flatDistr)
            | otherwise = genesisUtxo bootStakeholders genesisProdAddrDistribution
    let genUtxo = genUtxoUnfiltered &
            _GenesisUtxo %~ filterSecretsUtxo (toList invSecretsMap)
    when (M.null $ unGenesisUtxo genUtxo) $
        throwM EmptyUtxo

    let bgenParams =
            BlockGenParams
                (AllSecrets invSecretsMap)
                (fromIntegral bgoBlockN)
                def<commit_msg>[CSL-1457] Use genesisProdBootStakeholders in block-gen<commit_after>module Main where

import           Universum

import           Control.Monad.Random.Strict (evalRandT)
import           Data.Default                (def)
import qualified Data.HashMap.Strict         as HM
import qualified Data.Map                    as M
import           Formatting                  (build, sformat, (%))
import           Mockable                    (runProduction)
import           System.Directory            (doesDirectoryExist)
import           System.Random               (mkStdGen, randomIO)
import           System.Wlog                 (usingLoggerName)

import           Pos.Core                    (StakeDistribution (..),
                                              genesisDevSecretKeys,
                                              genesisProdAddrDistribution,
                                              genesisProdBootStakeholders, isDevelopment,
                                              makePubKeyAddress, mkCoin)
import           Pos.Crypto                  (SecretKey, toPublic)
import           Pos.DB                      (closeNodeDBs, openNodeDBs)
import           Pos.Generator.Block         (AllSecrets (..), BlockGenParams (..),
                                              genBlocks, mkInvSecretsMap, unInvSecretsMap)
import           Pos.Genesis                 (GenesisWStakeholders (..), devAddrDistr,
                                              genesisUtxo)
import           Pos.Txp.Core                (TxOut (..), TxOutAux (..))
import           Pos.Txp.Toil                (GenesisUtxo (..), Utxo, _GenesisUtxo)
import           Pos.Util.UserSecret         (peekUserSecret, usPrimKey)

import           Context                     (initTBlockGenMode)
import           Error                       (TBlockGenError (..))
import           Options                     (BlockGenOptions (..), getBlockGenOptions)

main :: IO ()
main = flip catch catchEx $ do
    if isDevelopment then
        putText $ "Generating in DEV mode"
    else
        putText $ "Generating in PROD mode"
    BlockGenOptions{..} <- getBlockGenOptions
    when bgoAppend $ checkExistence bgoPath
    invSecretsMap <- mkInvSecretsMap <$> case bgoNodes of
        Left bgoNodesN -> do
            unless (bgoNodesN > 0) $
                throwM NoOneSecrets
            let secretKeys = take (fromIntegral bgoNodesN) genesisDevSecretKeys
            pure secretKeys
        Right bgoSecretFiles -> do
            when (null bgoSecretFiles) $
                throwM NoOneSecrets
            usingLoggerName "block-gen" $ mapM parseSecret bgoSecretFiles

    let nodes = length invSecretsMap
    let flatDistr = FlatStakes (fromIntegral nodes) (mkCoin $ fromIntegral nodes)
    let bootStakeholders
            | isDevelopment =
                GenesisWStakeholders $ HM.fromList $
                zip (HM.keys $ unInvSecretsMap invSecretsMap) (repeat 1)
            | otherwise = genesisProdBootStakeholders
    let addrDistribution
            | isDevelopment = fst $ devAddrDistr flatDistr
            | otherwise = genesisProdAddrDistribution
    -- We need to select from utxo TxOut's corresponding to passed secrets
    -- to avoid error "Secret key of %hash% is required but isn't known"
    let genUtxoUnfiltered = genesisUtxo bootStakeholders addrDistribution
    let genUtxo = genUtxoUnfiltered &
            _GenesisUtxo %~ filterSecretsUtxo (toList invSecretsMap)
    when (M.null $ unGenesisUtxo genUtxo) $
        throwM EmptyUtxo

    let bgenParams =
            BlockGenParams
                (AllSecrets invSecretsMap)
                (fromIntegral bgoBlockN)
                def
{'fraction_non_alphanumeric': 0.06044109436069235, 'fraction_numerical': 0.0011166945840312675, 'mean_word_length': 1.5371813031161472, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 29, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4982927', 'n_tokens_mistral': 1886, 'n_tokens_neox': 1783, 'n_words': 464}