-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
184 lines (169 loc) · 7.69 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
{-# LANGUAGE RecordWildCards #-}
module Main
( main
) where
import Universum
import Control.Exception.Safe (handle)
import Data.Maybe (fromMaybe)
import Formatting (sformat, shown, (%))
import qualified Network.Transport.TCP as TCP (TCPAddr (..))
import qualified System.IO.Temp as Temp
import Ntp.Client (NtpConfiguration)
import Pos.Chain.Genesis as Genesis (Config (..), ConfigurationError)
import Pos.Chain.Txp (TxpConfiguration)
import Pos.Chain.Update (updateConfiguration)
import qualified Pos.Client.CLI as CLI
import Pos.Context (NodeContext (..))
import Pos.DB.DB (initNodeDBs)
import Pos.DB.Txp (txpGlobalSettings)
import Pos.Infra.Diffusion.Types (Diffusion, hoistDiffusion)
import Pos.Infra.Network.Types (NetworkConfig (..), Topology (..),
topologyDequeuePolicy, topologyEnqueuePolicy,
topologyFailurePolicy)
import Pos.Launcher (HasConfigurations, NodeParams (..),
NodeResources (..), WalletConfiguration,
bracketNodeResources, loggerBracket, lpConsoleLog,
runNode, runRealMode, withConfigurations)
import Pos.Util (logException)
import Pos.Util.CompileInfo (HasCompileInfo, withCompileInfo)
import Pos.Util.Config (ConfigurationException (..))
import Pos.Util.Trace (fromTypeclassWlog, noTrace)
import Pos.Util.UserSecret (usVss)
import Pos.Util.Wlog (LoggerName, logInfo)
import Pos.WorkMode (EmptyMempoolExt, RealMode)
import AuxxOptions (AuxxAction (..), AuxxOptions (..),
AuxxStartMode (..), getAuxxOptions)
import Mode (AuxxContext (..), AuxxMode, realModeToAuxx)
import Plugin (auxxPlugin, rawExec)
import Repl (PrintAction, WithCommandAction (..), withAuxxRepl)
loggerName :: LoggerName
loggerName = "auxx"
-- 'NodeParams' obtained using 'CLI.getNodeParams' are not perfect for
-- Auxx, so we need to adapt them slightly.
correctNodeParams :: AuxxOptions -> NodeParams -> IO (NodeParams, Bool)
correctNodeParams AuxxOptions {..} np = do
(dbPath, isTempDbUsed) <- case npDbPathM np of
Nothing -> do
tempDir <- Temp.getCanonicalTemporaryDirectory
dbPath <- Temp.createTempDirectory tempDir "nodedb"
logInfo $ sformat ("Temporary db created: "%shown) dbPath
return (dbPath, True)
Just dbPath -> do
logInfo $ sformat ("Supplied db used: "%shown) dbPath
return (dbPath, False)
let np' = np
{ npNetworkConfig = networkConfig
, npRebuildDb = npRebuildDb np || isTempDbUsed
, npDbPathM = Just dbPath }
return (np', isTempDbUsed)
where
topology = TopologyAuxx aoPeers
networkConfig =
NetworkConfig
{ ncDefaultPort = 3000
, ncSelfName = Nothing
, ncEnqueuePolicy = topologyEnqueuePolicy topology
, ncDequeuePolicy = topologyDequeuePolicy topology
, ncFailurePolicy = topologyFailurePolicy topology
, ncTopology = topology
, ncTcpAddr = TCP.Unaddressable
, ncCheckPeerHost = True
}
runNodeWithSinglePlugin ::
(HasConfigurations, HasCompileInfo)
=> Genesis.Config
-> TxpConfiguration
-> NodeResources EmptyMempoolExt
-> (Diffusion AuxxMode -> AuxxMode ())
-> Diffusion AuxxMode -> AuxxMode ()
runNodeWithSinglePlugin genesisConfig txpConfig nr plugin =
runNode genesisConfig txpConfig nr [ ("runNodeWithSinglePlugin", plugin) ]
action :: HasCompileInfo => AuxxOptions -> Either WithCommandAction Text -> IO ()
action opts@AuxxOptions {..} command = do
let pa = either printAction (const putText) command
case aoStartMode of
Automatic
->
handle @_ @ConfigurationException (\_ -> runWithoutNode pa)
. handle @_ @ConfigurationError (\_ -> runWithoutNode pa)
$ withConfigurations noTrace
Nothing
cnaDumpGenesisDataPath
cnaDumpConfiguration
conf
(runWithConfig pa)
Light
-> runWithoutNode pa
_ -> withConfigurations noTrace
Nothing
cnaDumpGenesisDataPath
cnaDumpConfiguration
conf
(runWithConfig pa)
where
runWithoutNode :: PrintAction IO -> IO ()
runWithoutNode printAction = printAction "Mode: light" >> rawExec Nothing Nothing Nothing opts Nothing command
runWithConfig
:: HasConfigurations
=> PrintAction IO
-> Genesis.Config
-> WalletConfiguration
-> TxpConfiguration
-> NtpConfiguration
-> IO ()
runWithConfig printAction genesisConfig _walletConfig txpConfig _ntpConfig = do
printAction "Mode: with-config"
(nodeParams, tempDbUsed) <- (correctNodeParams opts . fst) =<< CLI.getNodeParams
fromTypeclassWlog
loggerName
cArgs
nArgs
(configGeneratedSecrets genesisConfig)
let toRealMode :: AuxxMode a -> RealMode EmptyMempoolExt a
toRealMode auxxAction = do
realModeContext <- ask
let auxxContext =
AuxxContext
{ acRealModeContext = realModeContext
, acTempDbUsed = tempDbUsed }
lift $ runReaderT auxxAction auxxContext
vssSK = fromMaybe (error "no user secret given")
(npUserSecret nodeParams ^. usVss)
sscParams = CLI.gtSscParams cArgs vssSK (npBehaviorConfig nodeParams)
bracketNodeResources genesisConfig
nodeParams
sscParams
(txpGlobalSettings genesisConfig txpConfig)
(initNodeDBs genesisConfig) $ \nr ->
let NodeContext {..} = nrContext nr
modifier = if aoStartMode == WithNode
then runNodeWithSinglePlugin genesisConfig txpConfig nr
else identity
auxxModeAction = modifier (auxxPlugin genesisConfig txpConfig opts command)
in runRealMode updateConfiguration genesisConfig txpConfig nr $ \diffusion ->
toRealMode (auxxModeAction (hoistDiffusion realModeToAuxx toRealMode diffusion))
[email protected] {..} = aoCommonNodeArgs
conf = CLI.configurationOptions (CLI.commonArgs cArgs)
nArgs =
CLI.NodeArgs
{ behaviorConfigPath = Nothing
}
main :: IO ()
main = withCompileInfo $ do
opts <- getAuxxOptions
let disableConsoleLog
| Repl <- aoAction opts =
-- Logging in the REPL disrupts the prompt,
-- so we disable it.
-- TODO: When LW-25 is resolved we also want
-- to be able to enable logging in REPL using
-- a Haskeline-compatible print action.
\lp -> lp { lpConsoleLog = Just False }
| otherwise = identity
loggingParams = disableConsoleLog $
CLI.loggingParams loggerName (aoCommonNodeArgs opts)
loggerBracket "auxx" loggingParams . logException "auxx" $ do
let runAction a = action opts a
case aoAction opts of
Repl -> withAuxxRepl $ \c -> runAction (Left c)
Cmd cmd -> runAction (Right cmd)