-
Notifications
You must be signed in to change notification settings - Fork 721
/
POM.hs
343 lines (300 loc) · 14.8 KB
/
POM.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# OPTIONS_GHC -Wno-noncanonical-monoid-instances #-}
module Cardano.Node.Configuration.POM
( NodeConfiguration (..)
, PartialNodeConfiguration(..)
, defaultPartialNodeConfiguration
, lastOption
, makeNodeConfiguration
, parseNodeConfigurationFP
, pncProtocol
, ncProtocol
)
where
import Cardano.Prelude
import Prelude (String)
import Control.Monad (fail)
import Data.Aeson
import Data.Yaml (decodeFileThrow)
import Generic.Data (gmappend)
import Generic.Data.Orphans ()
import Options.Applicative
import System.FilePath (takeDirectory, (</>))
import System.Posix.Types (Fd (..))
import qualified Cardano.Chain.Update as Byron
import Cardano.Crypto (RequiresNetworkMagic (..))
import Cardano.Node.Protocol.Types (Protocol (..))
import Cardano.Node.Types
import Cardano.Tracing.Config
import Ouroboros.Consensus.Storage.LedgerDB.DiskPolicy (SnapshotInterval (..))
import Ouroboros.Network.Block (MaxSlotNo (..))
import Ouroboros.Network.NodeToNode (DiffusionMode (..))
data NodeConfiguration
= NodeConfiguration
{ ncNodeIPv4Addr :: !(Maybe NodeHostIPv4Address)
, ncNodeIPv6Addr :: !(Maybe NodeHostIPv6Address)
, ncNodePortNumber :: !(Maybe PortNumber)
-- | Filepath of the configuration yaml file. This file determines
-- all the configuration settings required for the cardano node
-- (logging, tracing, protocol, slot length etc)
, ncConfigFile :: !ConfigYamlFilePath
, ncTopologyFile :: !TopologyFile
, ncDatabaseFile :: !DbFile
, ncProtocolFiles :: !ProtocolFilepaths
, ncValidateDB :: !Bool
, ncShutdownIPC :: !(Maybe Fd)
, ncShutdownOnSlotSynced :: !MaxSlotNo
-- Protocol-specific parameters:
, ncProtocolConfig :: !NodeProtocolConfiguration
-- Node parameters, not protocol-specific:
, ncSocketPath :: !(Maybe SocketPath)
, ncDiffusionMode :: !DiffusionMode
, ncSnapshotInterval :: !SnapshotInterval
-- BlockFetch configuration
, ncMaxConcurrencyBulkSync :: !(Maybe MaxConcurrencyBulkSync)
, ncMaxConcurrencyDeadline :: !(Maybe MaxConcurrencyDeadline)
-- Logging parameters:
, ncLoggingSwitch :: !Bool
, ncLogMetrics :: !Bool
, ncTraceConfig :: !TraceOptions
} deriving (Eq, Show)
data PartialNodeConfiguration
= PartialNodeConfiguration
{ pncNodeIPv4Addr :: !(Last NodeHostIPv4Address)
, pncNodeIPv6Addr :: !(Last NodeHostIPv6Address)
, pncNodePortNumber :: !(Last PortNumber)
-- | Filepath of the configuration yaml file. This file determines
-- all the configuration settings required for the cardano node
-- (logging, tracing, protocol, slot length etc)
, pncConfigFile :: !(Last ConfigYamlFilePath)
, pncTopologyFile :: !(Last TopologyFile)
, pncDatabaseFile :: !(Last DbFile)
, pncProtocolFiles :: !(Last ProtocolFilepaths)
, pncValidateDB :: !(Last Bool)
, pncShutdownIPC :: !(Last (Maybe Fd))
, pncShutdownOnSlotSynced :: !(Last MaxSlotNo)
-- Protocol-specific parameters:
, pncProtocolConfig :: !(Last NodeProtocolConfiguration)
-- Node parameters, not protocol-specific:
, pncSocketPath :: !(Last SocketPath)
, pncDiffusionMode :: !(Last DiffusionMode)
, pncSnapshotInterval :: !(Last SnapshotInterval)
-- BlockFetch configuration
, pncMaxConcurrencyBulkSync :: !(Last MaxConcurrencyBulkSync)
, pncMaxConcurrencyDeadline :: !(Last MaxConcurrencyDeadline)
-- Logging parameters:
, pncLoggingSwitch :: !(Last Bool)
, pncLogMetrics :: !(Last Bool)
, pncTraceConfig :: !(Last TraceOptions)
} deriving (Eq, Generic, Show)
instance AdjustFilePaths PartialNodeConfiguration where
adjustFilePaths f x =
x { pncProtocolConfig = adjustFilePaths f (pncProtocolConfig x)
, pncSocketPath = adjustFilePaths f (pncSocketPath x)
}
instance Semigroup PartialNodeConfiguration where
(<>) = gmappend
instance FromJSON PartialNodeConfiguration where
parseJSON =
withObject "PartialNodeConfiguration" $ \v -> do
-- Node parameters, not protocol-specific
pncSocketPath' <- Last <$> v .:? "SocketPath"
pncDiffusionMode'
<- Last . fmap getDiffusionMode <$> v .:? "DiffusionMode"
pncSnapshotInterval'
<- Last . fmap RequestedSnapshotInterval <$> v .:? "SnapshotInterval"
-- Blockfetch parameters
pncMaxConcurrencyBulkSync' <- Last <$> v .:? "MaxConcurrencyBulkSync"
pncMaxConcurrencyDeadline' <- Last <$> v .:? "MaxConcurrencyDeadline"
-- Logging parameters
pncLoggingSwitch' <- v .:? "TurnOnLogging" .!= True
pncLogMetrics' <- Last <$> v .:? "TurnOnLogMetrics"
pncTraceConfig' <- if pncLoggingSwitch'
then Last . Just <$> traceConfigParser v
else return . Last $ Just TracingOff
-- Protocol parameters
protocol <- v .:? "Protocol" .!= ByronProtocol
pncProtocolConfig' <-
case protocol of
ByronProtocol ->
Last . Just . NodeProtocolConfigurationByron <$> parseByronProtocol v
ShelleyProtocol ->
Last . Just . NodeProtocolConfigurationShelley <$> parseShelleyProtocol v
CardanoProtocol ->
Last . Just <$> (NodeProtocolConfigurationCardano <$> parseByronProtocol v
<*> parseShelleyProtocol v
<*> parseHardForkProtocol v)
pure PartialNodeConfiguration {
pncProtocolConfig = pncProtocolConfig'
, pncSocketPath = pncSocketPath'
, pncDiffusionMode = pncDiffusionMode'
, pncSnapshotInterval = pncSnapshotInterval'
, pncMaxConcurrencyBulkSync = pncMaxConcurrencyBulkSync'
, pncMaxConcurrencyDeadline = pncMaxConcurrencyDeadline'
, pncLoggingSwitch = Last $ Just pncLoggingSwitch'
, pncLogMetrics = pncLogMetrics'
, pncTraceConfig = pncTraceConfig'
, pncNodeIPv4Addr = mempty
, pncNodeIPv6Addr = mempty
, pncNodePortNumber = mempty
, pncConfigFile = mempty
, pncTopologyFile = mempty
, pncDatabaseFile = mempty
, pncProtocolFiles = mempty
, pncValidateDB = mempty
, pncShutdownIPC = mempty
, pncShutdownOnSlotSynced = mempty
}
where
parseByronProtocol v = do
primary <- v .:? "ByronGenesisFile"
secondary <- v .:? "GenesisFile"
npcByronGenesisFile <-
case (primary, secondary) of
(Just g, Nothing) -> return g
(Nothing, Just g) -> return g
(Nothing, Nothing) -> fail $ "Missing required field, either "
++ "ByronGenesisFile or GenesisFile"
(Just _, Just _) -> fail $ "Specify either ByronGenesisFile"
++ "or GenesisFile, but not both"
npcByronGenesisFileHash <- v .:? "ByronGenesisHash"
npcByronReqNetworkMagic <- v .:? "RequiresNetworkMagic"
.!= RequiresNoMagic
npcByronPbftSignatureThresh <- v .:? "PBftSignatureThreshold"
npcByronApplicationName <- v .:? "ApplicationName"
.!= Byron.ApplicationName "cardano-sl"
npcByronApplicationVersion <- v .:? "ApplicationVersion" .!= 1
protVerMajor <- v .: "LastKnownBlockVersion-Major"
protVerMinor <- v .: "LastKnownBlockVersion-Minor"
protVerAlt <- v .: "LastKnownBlockVersion-Alt" .!= 0
pure NodeByronProtocolConfiguration {
npcByronGenesisFile
, npcByronGenesisFileHash
, npcByronReqNetworkMagic
, npcByronPbftSignatureThresh
, npcByronApplicationName
, npcByronApplicationVersion
, npcByronSupportedProtocolVersionMajor = protVerMajor
, npcByronSupportedProtocolVersionMinor = protVerMinor
, npcByronSupportedProtocolVersionAlt = protVerAlt
}
parseShelleyProtocol v = do
primary <- v .:? "ShelleyGenesisFile"
secondary <- v .:? "GenesisFile"
npcShelleyGenesisFile <-
case (primary, secondary) of
(Just g, Nothing) -> return g
(Nothing, Just g) -> return g
(Nothing, Nothing) -> fail $ "Missing required field, either "
++ "ShelleyGenesisFile or GenesisFile"
(Just _, Just _) -> fail $ "Specify either ShelleyGenesisFile"
++ "or GenesisFile, but not both"
npcShelleyGenesisFileHash <- v .:? "ShelleyGenesisHash"
npcStubComputeDuration <- fromMaybe 0.1 <$> v .:? "StubComputeDuration"
npcStubComputePrecision <- fromMaybe 0.01 <$> v .:? "StubComputePrecision"
pure NodeShelleyProtocolConfiguration {
npcShelleyGenesisFile
, npcShelleyGenesisFileHash
, npcStubComputeDuration
, npcStubComputePrecision
}
parseHardForkProtocol v = do
npcTestShelleyHardForkAtEpoch <- v .:? "TestShelleyHardForkAtEpoch"
npcTestShelleyHardForkAtVersion <- v .:? "TestShelleyHardForkAtVersion"
npcTestAllegraHardForkAtEpoch <- v .:? "TestAllegraHardForkAtEpoch"
npcTestAllegraHardForkAtVersion <- v .:? "TestAllegraHardForkAtVersion"
npcTestMaryHardForkAtEpoch <- v .:? "TestMaryHardForkAtEpoch"
npcTestMaryHardForkAtVersion <- v .:? "TestMaryHardForkAtVersion"
pure NodeHardForkProtocolConfiguration {
npcTestShelleyHardForkAtEpoch,
npcTestShelleyHardForkAtVersion,
npcTestAllegraHardForkAtEpoch,
npcTestAllegraHardForkAtVersion,
npcTestMaryHardForkAtEpoch,
npcTestMaryHardForkAtVersion
}
-- | Default configuration is mainnet
defaultPartialNodeConfiguration :: PartialNodeConfiguration
defaultPartialNodeConfiguration =
PartialNodeConfiguration
{ pncConfigFile = Last . Just $ ConfigYamlFilePath "configuration/cardano/mainnet-config.json"
, pncDatabaseFile = Last . Just $ DbFile "mainnet/db/"
, pncLoggingSwitch = Last $ Just True
, pncSocketPath = mempty
, pncDiffusionMode = Last $ Just InitiatorAndResponderDiffusionMode
, pncSnapshotInterval = Last $ Just DefaultSnapshotInterval
, pncTopologyFile = Last . Just $ TopologyFile "configuration/cardano/mainnet-topology.json"
, pncNodeIPv4Addr = mempty
, pncNodeIPv6Addr = mempty
, pncNodePortNumber = mempty
, pncProtocolFiles = mempty
, pncValidateDB = mempty
, pncShutdownIPC = mempty
, pncShutdownOnSlotSynced = mempty
, pncProtocolConfig = mempty
, pncMaxConcurrencyBulkSync = mempty
, pncMaxConcurrencyDeadline = mempty
, pncLogMetrics = mempty
, pncTraceConfig = mempty
}
lastOption :: Parser a -> Parser (Last a)
lastOption = fmap Last . optional
lastToEither :: String -> Last a -> Either String a
lastToEither errMsg (Last x) = maybe (Left errMsg) Right x
makeNodeConfiguration :: PartialNodeConfiguration -> Either String NodeConfiguration
makeNodeConfiguration pnc = do
configFile <- lastToEither "Missing YAML config file" $ pncConfigFile pnc
topologyFile <- lastToEither "Missing TopologyFile" $ pncTopologyFile pnc
databaseFile <- lastToEither "Missing DatabaseFile" $ pncDatabaseFile pnc
protocolFiles <- lastToEither "Missing ProtocolFiles" $ pncProtocolFiles pnc
validateDB <- lastToEither "Missing ValidateDB" $ pncValidateDB pnc
shutdownIPC <- lastToEither "Missing ShutdownIPC" $ pncShutdownIPC pnc
shutdownOnSlotSynced <- lastToEither "Missing ShutdownOnSlotSynced" $ pncShutdownOnSlotSynced pnc
protocolConfig <- lastToEither "Missing ProtocolConfig" $ pncProtocolConfig pnc
loggingSwitch <- lastToEither "Missing LoggingSwitch" $ pncLoggingSwitch pnc
logMetrics <- lastToEither "Missing LogMetrics" $ pncLogMetrics pnc
traceConfig <- lastToEither "Missing TraceConfig" $ pncTraceConfig pnc
diffusionMode <- lastToEither "Missing DiffusionMode" $ pncDiffusionMode pnc
snapshotInterval <- lastToEither "Missing SnapshotInterval" $ pncSnapshotInterval pnc
return $ NodeConfiguration
{ ncNodeIPv4Addr = getLast $ pncNodeIPv4Addr pnc
, ncNodeIPv6Addr = getLast $ pncNodeIPv6Addr pnc
, ncNodePortNumber = getLast $ pncNodePortNumber pnc
, ncConfigFile = configFile
, ncTopologyFile = topologyFile
, ncDatabaseFile = databaseFile
, ncProtocolFiles = protocolFiles
, ncValidateDB = validateDB
, ncShutdownIPC = shutdownIPC
, ncShutdownOnSlotSynced = shutdownOnSlotSynced
, ncProtocolConfig = protocolConfig
, ncSocketPath = getLast $ pncSocketPath pnc
, ncDiffusionMode = diffusionMode
, ncSnapshotInterval = snapshotInterval
, ncMaxConcurrencyBulkSync = getLast $ pncMaxConcurrencyBulkSync pnc
, ncMaxConcurrencyDeadline = getLast $ pncMaxConcurrencyDeadline pnc
, ncLoggingSwitch = loggingSwitch
, ncLogMetrics = logMetrics
, ncTraceConfig = traceConfig
}
ncProtocol :: NodeConfiguration -> Protocol
ncProtocol nc =
case ncProtocolConfig nc of
NodeProtocolConfigurationByron{} -> ByronProtocol
NodeProtocolConfigurationShelley{} -> ShelleyProtocol
NodeProtocolConfigurationCardano{} -> CardanoProtocol
pncProtocol :: PartialNodeConfiguration -> Either Text Protocol
pncProtocol pnc =
case pncProtocolConfig pnc of
Last Nothing -> Left "Node protocol configuration not found"
Last (Just NodeProtocolConfigurationByron{}) -> Right ByronProtocol
Last (Just NodeProtocolConfigurationShelley{}) -> Right ShelleyProtocol
Last (Just NodeProtocolConfigurationCardano{}) -> Right CardanoProtocol
parseNodeConfigurationFP :: Maybe ConfigYamlFilePath -> IO PartialNodeConfiguration
parseNodeConfigurationFP Nothing = parseNodeConfigurationFP . getLast $ pncConfigFile defaultPartialNodeConfiguration
parseNodeConfigurationFP (Just (ConfigYamlFilePath fp)) = do
nc <- decodeFileThrow fp
-- Make all the files be relative to the location of the config file.
pure $ adjustFilePaths (takeDirectory fp </>) nc