<commit_before>{-# LANGUAGE CPP                 #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Main where

import qualified Data.ByteString.Lazy as LBS
import           Data.List            ((!!))
import           Data.Proxy           (Proxy (..))
import           Node                 (SendActions, hoistSendActions)
import           Mockable             (Production)
import           System.Directory     (createDirectoryIfMissing)
import           System.FilePath      ((</>))
import           System.Wlog          (LoggerName)
import           Universum

import           Pos.Binary           (Bi, decode, encode)
import qualified Pos.CLI              as CLI
import           Pos.Communication     (BiP)
import           Pos.Constants        (RunningMode (..), runningMode)
import           Pos.Crypto           (VssKeyPair, vssKeyGen)
import           Pos.DHT.Model        (DHTKey, DHTNodeType (..), dhtNodeType)
import           Pos.Genesis          (genesisSecretKeys, genesisUtxo)
import           Pos.Launcher         (BaseParams (..), LoggingParams (..),
                                       NodeParams (..), RealModeResources,
                                       bracketResources, runNodeProduction, runNodeStats,
                                       runTimeLordReal, runTimeSlaveReal, stakesDistr)
import           Pos.Ssc.GodTossing   (genesisVssKeyPairs)
import           Pos.Ssc.GodTossing   (GtParams (..), SscGodTossing)
import           Pos.Ssc.NistBeacon   (SscNistBeacon)
import           Pos.Ssc.SscAlgo      (SscAlgo (..))
import           Pos.Statistics       (getNoStatsT, runStatsT', getStatsMap)
import           Pos.Types            (Timestamp)
#ifdef WITH_WEB
import           Pos.Ssc.Class        (Ssc, SscConstraint)
import           Pos.Web              (serveWebBase, serveWebGT)
import           Pos.WorkMode         (WorkMode)
#ifdef WITH_WALLET
import           Pos.WorkMode         (ProductionMode, RawRealMode, StatsMode)

import           Pos.Wallet.Web       (walletServeWebFull)
#endif
#endif

import           NodeOptions          (Args (..), getNodeOptions)


getKey
    :: Bi key
    => Maybe key -> Maybe FilePath -> FilePath -> IO key -> IO key
getKey (Just key) _ _ _ = return key
getKey _ (Just path) _ _ = decode' path
getKey _ _ fpath gen = do
    createDirectoryIfMissing True "cardano-keys"
    decode' ("cardano-keys" </> fpath) `catch` \(_ :: SomeException) -> do
        key <- gen
        LBS.writeFile ("cardano-keys" </> fpath) $ encode key
        putStrLn $ "Generated key " ++ ("cardano-keys" </> fpath)
        return key

decode' :: Bi key => FilePath -> IO key
decode' fpath = either fail' return . decode =<< LBS.readFile fpath
  where
    fail' e = fail $ "Error reading key from " ++ fpath ++ ": " ++ e

getSystemStart :: Ssc ssc => Proxy ssc -> RealModeResources -> Args -> Production Timestamp
getSystemStart sscProxy inst args  =
    case runningMode of
        Development ->
            if timeLord args
                then runTimeLordReal (loggingParams "time-lord" args)
                else runTimeSlaveReal sscProxy inst (baseParams "time-slave" args)
        Production systemStart -> return systemStart

loggingParams :: LoggerName -> Args -> LoggingParams
loggingParams tag Args{..} =
    LoggingParams
    { lpHandlerPrefix = CLI.logPrefix commonArgs
    , lpConfigPath    = CLI.logConfig commonArgs
    , lpRunnerTag = tag
    }

baseParams :: LoggerName -> Args -> BaseParams
baseParams loggingTag args@Args {..} =
    BaseParams
    { bpLoggingParams = loggingParams loggingTag args
    , bpPort = port
    , bpDHTPeers = CLI.dhtPeers commonArgs
    , bpDHTKeyOrType = dhtKeyOrType
    , bpDHTExplicitInitial = CLI.dhtExplicitInitial commonArgs
    }
  where
    dhtKeyOrType<commit_msg>[CSL-541] Fix compilation<commit_after>{-# LANGUAGE CPP                 #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Main where

import qualified Data.ByteString.Lazy as LBS
import           Data.List            ((!!))
import           Data.Proxy           (Proxy (..))
import           Node                 (SendActions, hoistSendActions)
import           Mockable             (Production)
import           System.Directory     (createDirectoryIfMissing)
import           System.FilePath      ((</>))
import           System.Wlog          (LoggerName)
import           Universum

import           Pos.Binary           (Bi, decode, encode)
import qualified Pos.CLI              as CLI
import           Pos.Communication     (BiP)
import           Pos.Constants        (RunningMode (..), runningMode)
import           Pos.Crypto           (VssKeyPair, vssKeyGen)
import           Pos.DHT.Model        (DHTKey, DHTNodeType (..), dhtNodeType)
import           Pos.Genesis          (genesisSecretKeys, genesisUtxo)
import           Pos.Launcher         (BaseParams (..), LoggingParams (..),
                                       NodeParams (..), RealModeResources,
                                       bracketResources, runNodeProduction, runNodeStats,
                                       runTimeLordReal, runTimeSlaveReal, stakesDistr)
import           Pos.Ssc.GodTossing   (genesisVssKeyPairs)
import           Pos.Ssc.GodTossing   (GtParams (..), SscGodTossing)
import           Pos.Ssc.NistBeacon   (SscNistBeacon)
import           Pos.Ssc.SscAlgo      (SscAlgo (..))
import           Pos.Statistics       (getNoStatsT, runStatsT', getStatsMap)
import           Pos.Types            (Timestamp)
#ifdef WITH_WEB
import           Pos.Ssc.Class        (SscConstraint, SscListenersClass)
import           Pos.Web              (serveWebBase, serveWebGT)
import           Pos.WorkMode         (WorkMode)
#ifdef WITH_WALLET
import           Pos.WorkMode         (ProductionMode, RawRealMode, StatsMode)

import           Pos.Wallet.Web       (walletServeWebFull)
#endif
#endif

import           NodeOptions          (Args (..), getNodeOptions)


getKey
    :: Bi key
    => Maybe key -> Maybe FilePath -> FilePath -> IO key -> IO key
getKey (Just key) _ _ _ = return key
getKey _ (Just path) _ _ = decode' path
getKey _ _ fpath gen = do
    createDirectoryIfMissing True "cardano-keys"
    decode' ("cardano-keys" </> fpath) `catch` \(_ :: SomeException) -> do
        key <- gen
        LBS.writeFile ("cardano-keys" </> fpath) $ encode key
        putStrLn $ "Generated key " ++ ("cardano-keys" </> fpath)
        return key

decode' :: Bi key => FilePath -> IO key
decode' fpath = either fail' return . decode =<< LBS.readFile fpath
  where
    fail' e = fail $ "Error reading key from " ++ fpath ++ ": " ++ e

getSystemStart :: SscListenersClass ssc => Proxy ssc -> RealModeResources -> Args -> Production Timestamp
getSystemStart sscProxy inst args  =
    case runningMode of
        Development ->
            if timeLord args
                then runTimeLordReal (loggingParams "time-lord" args)
                else runTimeSlaveReal sscProxy inst (baseParams "time-slave" args)
        Production systemStart -> return systemStart

loggingParams :: LoggerName -> Args -> LoggingParams
loggingParams tag Args{..} =
    LoggingParams
    { lpHandlerPrefix = CLI.logPrefix commonArgs
    , lpConfigPath    = CLI.logConfig commonArgs
    , lpRunnerTag = tag
    }

baseParams :: LoggerName -> Args -> BaseParams
baseParams loggingTag args@Args {..} =
    BaseParams
    { bpLoggingParams = loggingParams loggingTag args
    , bpPort = port
    , bpDHTPeers = CLI.dhtPeers commonArgs
    , bpDHTKeyOrType = dhtKeyOrType
    , bpDHTExplicitInitial = CLI.dhtExplicitInitial commonArgs
    }
  where
    dhtKeyOrType
{'fraction_non_alphanumeric': 0.0898920768623322, 'fraction_numerical': 0.0003948407475651487, 'mean_word_length': 2.0035573122529646, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4392970', 'n_tokens_mistral': 2170, 'n_tokens_neox': 2131, 'n_words': 553}