/
Types.hs
558 lines (473 loc) · 20.9 KB
/
Types.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE StandaloneDeriving #-}
module Cardano.Node.Types
( ConfigError(..)
, ConfigYamlFilePath(..)
, DbFile(..)
, GenesisFile(..)
, NodeCLI(..)
, NodeConfiguration(..)
, Protocol(..)
, ProtocolFilepaths (..)
, MockProtocol(..)
, GenesisHash(..)
, MaxConcurrencyBulkSync(..)
, MaxConcurrencyDeadline(..)
, NodeAddress(..)
, NodeHostAddress (..)
, NodeByronProtocolConfiguration(..)
, NodeHardForkProtocolConfiguration(..)
, NodeProtocolConfiguration(..)
, NodeShelleyProtocolConfiguration(..)
, NodeMockProtocolConfiguration(..)
, NodeProtocolMode(..)
, SocketPath(..)
, TopologyFile(..)
, TraceOptions(..)
, ViewMode(..)
, ncProtocol
, parseNodeConfiguration
, parseNodeConfigurationFP
, protocolName
) where
import Cardano.Prelude
import Prelude (String)
import Control.Monad.Fail (fail)
import Data.Aeson
import Data.IP (IP)
import qualified Data.Text as Text
import Data.Yaml (decodeFileThrow)
import Network.Socket (PortNumber)
import System.FilePath (takeDirectory)
import System.Posix.Types (Fd)
import Cardano.Api.Typed (EpochNo)
import qualified Cardano.Chain.Update as Byron
import Cardano.Crypto (RequiresNetworkMagic (..))
import qualified Cardano.Crypto.Hash as Crypto
import Cardano.Node.TraceConfig (TraceOptions (..), traceConfigParser)
import Ouroboros.Consensus.NodeId (CoreNodeId (..))
import Ouroboros.Network.Block (MaxSlotNo (..))
--TODO: things will probably be clearer if we don't use these newtype wrappers and instead
-- use records with named fields in the CLI code.
-- | Errors for the cardano-config module.
data ConfigError
= ConfigErrorFileNotFound !FilePath
deriving Show
-- | Filepath of the configuration yaml file. This file determines
-- all the configuration settings required for the cardano node
-- (logging, tracing, protocol, slot length etc)
newtype ConfigYamlFilePath = ConfigYamlFilePath
{ unConfigPath :: FilePath }
deriving newtype (Eq, Show)
newtype DbFile = DbFile
{ unDB :: FilePath }
deriving newtype Show
newtype GenesisFile = GenesisFile
{ unGenesisFile :: FilePath }
deriving stock (Eq, Ord)
deriving newtype (IsString, Show)
instance FromJSON GenesisFile where
parseJSON (String genFp) = pure . GenesisFile $ Text.unpack genFp
parseJSON invalid = panic $ "Parsing of GenesisFile failed due to type mismatch. "
<> "Encountered: " <> (Text.pack $ show invalid)
-- Node can be run in two modes.
data ViewMode = LiveView -- Live mode with TUI
| SimpleView -- Simple mode, just output text.
deriving (Eq, Show)
instance FromJSON ViewMode where
parseJSON (String str) = case str of
"LiveView" -> pure LiveView
"SimpleView" -> pure SimpleView
view -> panic $ "Parsing of ViewMode: "
<> view <> " failed. "
<> view <> " is not a valid view mode"
parseJSON invalid = panic $ "Parsing of ViewMode failed due to type mismatch. "
<> "Encountered: " <> (Text.pack $ show invalid)
newtype MaxConcurrencyBulkSync = MaxConcurrencyBulkSync
{ unMaxConcurrencyBulkSync :: Word }
deriving stock (Eq, Ord)
deriving newtype (FromJSON, Show)
newtype MaxConcurrencyDeadline = MaxConcurrencyDeadline
{ unMaxConcurrencyDeadline :: Word }
deriving stock (Eq, Ord)
deriving newtype (FromJSON, Show)
-- | IPv4 address with a port number.
data NodeAddress = NodeAddress
{ naHostAddress :: !NodeHostAddress
, naPort :: !PortNumber
} deriving (Eq, Ord, Show)
instance FromJSON NodeAddress where
parseJSON = withObject "NodeAddress" $ \v -> do
NodeAddress
<$> v .: "addr"
<*> ((fromIntegral :: Int -> PortNumber) <$> v .: "port")
instance ToJSON NodeAddress where
toJSON na =
object
[ "addr" .= toJSON (naHostAddress na)
, "port" .= (fromIntegral (naPort na) :: Int)
]
-- Embedding a Maybe inside a newtype is somewhat icky but this seems to work
-- and removing the Maybe breaks the functionality in a subtle way that is difficult
-- to diagnose.
newtype NodeHostAddress
= NodeHostAddress { unNodeHostAddress :: Maybe IP }
deriving newtype Show
deriving (Eq, Ord)
instance FromJSON NodeHostAddress where
parseJSON (String ipStr) =
case readMaybe $ Text.unpack ipStr of
Just ip -> pure $ NodeHostAddress (Just ip)
Nothing -> panic $ "Parsing of IP failed: " <> ipStr
parseJSON Null = pure $ NodeHostAddress Nothing
parseJSON invalid = panic $ "Parsing of IP failed due to type mismatch. "
<> "Encountered: " <> (Text.pack $ show invalid) <> "\n"
instance ToJSON NodeHostAddress where
toJSON mha =
case unNodeHostAddress mha of
Just ip -> String (Text.pack $ show ip)
Nothing -> Null
data NodeCLI = NodeCLI
{ nodeMode :: !NodeProtocolMode
, nodeAddr :: !(Maybe NodeAddress)
-- | Filepath of the configuration yaml file. This file determines
-- all the configuration settings required for the cardano node
-- (logging, tracing, protocol, slot length etc)
, configFile :: !ConfigYamlFilePath
, topologyFile :: !TopologyFile
, databaseFile :: !DbFile
, socketFile :: !(Maybe SocketPath)
, protocolFiles :: !ProtocolFilepaths
, validateDB :: !Bool
, shutdownIPC :: !(Maybe Fd)
, shutdownOnSlotSynced :: !MaxSlotNo
}
data NodeConfiguration
= NodeConfiguration
{ -- Protocol-specific parameters:
ncProtocolConfig :: NodeProtocolConfiguration
-- Node parameters, not protocol-specific:
, ncSocketPath :: Maybe SocketPath
-- BlockFetch configuration
, ncMaxConcurrencyBulkSync :: Maybe MaxConcurrencyBulkSync
, ncMaxConcurrencyDeadline :: Maybe MaxConcurrencyDeadline
-- Logging parameters:
, ncViewMode :: ViewMode
, ncLoggingSwitch :: Bool
, ncLogMetrics :: Bool
, ncTraceConfig :: TraceOptions
} deriving Show
class AdjustFilePaths a where
adjustFilePaths :: (FilePath -> FilePath) -> a -> a
instance AdjustFilePaths NodeConfiguration where
adjustFilePaths f x@NodeConfiguration {
ncProtocolConfig,
ncSocketPath
} =
x {
ncProtocolConfig = adjustFilePaths f ncProtocolConfig,
ncSocketPath = adjustFilePaths f ncSocketPath
}
instance FromJSON NodeConfiguration where
parseJSON =
withObject "NodeConfiguration" $ \v -> do
-- Node parameters, not protocol-specific
ncSocketPath <- v .:? "SocketPath"
-- Blockfetch parameters
ncMaxConcurrencyBulkSync <- v .:? "MaxConcurrencyBulkSync"
ncMaxConcurrencyDeadline <- v .:? "MaxConcurrencyDeadline"
-- Logging parameters
ncViewMode <- v .:? "ViewMode" .!= SimpleView
ncLoggingSwitch <- v .:? "TurnOnLogging" .!= True
ncLogMetrics <- v .:? "TurnOnLogMetrics" .!= True
ncTraceConfig <- if ncLoggingSwitch
then traceConfigParser v
else return TracingOff
-- Protocol parameters
protocol <- v .: "Protocol" .!= ByronProtocol
ncProtocolConfig <-
case protocol of
MockProtocol ptcl ->
NodeProtocolConfigurationMock <$> parseMockProtocol ptcl v
ByronProtocol ->
NodeProtocolConfigurationByron <$> parseByronProtocol v
ShelleyProtocol ->
NodeProtocolConfigurationShelley <$> parseShelleyProtocol v
CardanoProtocol ->
NodeProtocolConfigurationCardano <$> parseByronProtocol v
<*> parseShelleyProtocol v
<*> parseHardForkProtocol v
pure NodeConfiguration {
ncProtocolConfig
, ncSocketPath
, ncMaxConcurrencyBulkSync
, ncMaxConcurrencyDeadline
, ncViewMode
, ncLoggingSwitch
, ncLogMetrics
, ncTraceConfig
}
where
parseMockProtocol npcMockProtocol v = do
npcMockNodeId <- v .: "NodeId"
npcMockNumCoreNodes <- v .: "NumCoreNodes"
pure NodeMockProtocolConfiguration {
npcMockProtocol
, npcMockNodeId
, npcMockNumCoreNodes
}
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"
--TODO: these are silly names, allow better aliases:
protVerMajor <- v .: "LastKnownBlockVersion-Major"
protVerMinor <- v .: "LastKnownBlockVersion-Minor"
protVerMajroMax <- v .:? "MaxKnownMajorProtocolVersion" .!= 1
pure NodeShelleyProtocolConfiguration {
npcShelleyGenesisFile
, npcShelleyGenesisFileHash
, npcShelleySupportedProtocolVersionMajor = protVerMajor
, npcShelleySupportedProtocolVersionMinor = protVerMinor
, npcShelleyMaxSupportedProtocolVersion = protVerMajroMax
}
parseHardForkProtocol v = do
npcTestShelleyHardForkAtEpoch <- v .:? "TestShelleyHardForkAtEpoch"
npcTestShelleyHardForkAtVersion <- v .:? "TestShelleyHardForkAtVersion"
npcShelleyHardForkNotBeforeEpoch <- v .:? "ShelleyHardForkNotBeforeEpoch"
pure NodeHardForkProtocolConfiguration {
npcTestShelleyHardForkAtEpoch,
npcTestShelleyHardForkAtVersion,
npcShelleyHardForkNotBeforeEpoch
}
data Protocol = MockProtocol !MockProtocol
| ByronProtocol
| ShelleyProtocol
| CardanoProtocol
deriving (Eq, Show, Generic)
instance FromJSON Protocol where
parseJSON =
withText "Protocol" $ \str -> case str of
-- The new names
"MockBFT" -> pure (MockProtocol MockBFT)
"MockPBFT" -> pure (MockProtocol MockPBFT)
"MockPraos" -> pure (MockProtocol MockPraos)
"Byron" -> pure ByronProtocol
"Shelley" -> pure ShelleyProtocol
"Cardano" -> pure CardanoProtocol
-- The old names
"BFT" -> pure (MockProtocol MockBFT)
--"MockPBFT" -- same as new name
"Praos" -> pure (MockProtocol MockPraos)
"RealPBFT" -> pure ByronProtocol
"TPraos" -> pure ShelleyProtocol
_ -> fail $ "Parsing of Protocol failed. "
<> show str <> " is not a valid protocol"
deriving instance NFData Protocol
deriving instance NoUnexpectedThunks Protocol
data ProtocolFilepaths =
ProtocolFilepaths {
byronCertFile :: !(Maybe FilePath)
, byronKeyFile :: !(Maybe FilePath)
, shelleyKESFile :: !(Maybe FilePath)
, shelleyVRFFile :: !(Maybe FilePath)
, shelleyCertFile :: !(Maybe FilePath)
}
data MockProtocol = MockBFT
| MockPBFT
| MockPraos
deriving (Eq, Show, Generic)
deriving instance NFData MockProtocol
deriving instance NoUnexpectedThunks MockProtocol
newtype GenesisHash = GenesisHash (Crypto.Hash Crypto.Blake2b_256 ByteString)
deriving newtype (Eq, Show, ToJSON, FromJSON)
data NodeProtocolConfiguration =
NodeProtocolConfigurationMock NodeMockProtocolConfiguration
| NodeProtocolConfigurationByron NodeByronProtocolConfiguration
| NodeProtocolConfigurationShelley NodeShelleyProtocolConfiguration
| NodeProtocolConfigurationCardano NodeByronProtocolConfiguration
NodeShelleyProtocolConfiguration
NodeHardForkProtocolConfiguration
deriving Show
-- | Mock protocols requires different parameters to real protocols.
-- Therefore we distinguish this at the top level on the command line.
data NodeProtocolMode = MockProtocolMode
| RealProtocolMode
data NodeShelleyProtocolConfiguration =
NodeShelleyProtocolConfiguration {
npcShelleyGenesisFile :: !GenesisFile
, npcShelleyGenesisFileHash :: !(Maybe GenesisHash)
-- | These declare the version of the protocol that the node is prepared
-- to run. This is usually the version of the protocol in use on the
-- chain now, but during protocol updates this version will be the one
-- that we declare that we are ready to move to. This is the endorsement
-- mechanism for determining when enough block producers are ready to
-- move to the next version.
--
, npcShelleySupportedProtocolVersionMajor :: !Natural
, npcShelleySupportedProtocolVersionMinor :: !Natural
-- | The maximum major version of the protocol this node supports.
-- If the actual version ever goes higher than this then the node
-- will stop with an appropriate error message.
, npcShelleyMaxSupportedProtocolVersion :: !Natural
}
deriving Show
data NodeByronProtocolConfiguration =
NodeByronProtocolConfiguration {
npcByronGenesisFile :: !GenesisFile
, npcByronGenesisFileHash :: !(Maybe GenesisHash)
, npcByronReqNetworkMagic :: !RequiresNetworkMagic
, npcByronPbftSignatureThresh :: !(Maybe Double)
-- | Update application name.
, npcByronApplicationName :: !Byron.ApplicationName
-- | Application (ie software) version.
, npcByronApplicationVersion :: !Byron.NumSoftwareVersion
-- | These declare the version of the protocol that the node is prepared
-- to run. This is usually the version of the protocol in use on the
-- chain now, but during protocol updates this version will be the one
-- that we declare that we are ready to move to. This is the endorsement
-- mechanism for determining when enough block producers are ready to
-- move to the next version.
--
, npcByronSupportedProtocolVersionMajor :: !Word16
, npcByronSupportedProtocolVersionMinor :: !Word16
, npcByronSupportedProtocolVersionAlt :: !Word8
}
deriving Show
data NodeMockProtocolConfiguration =
NodeMockProtocolConfiguration {
npcMockProtocol :: MockProtocol
, npcMockNodeId :: CoreNodeId
, npcMockNumCoreNodes :: Word64
}
deriving Show
-- | Configuration relating to a hard forks themselves, not the specific eras.
--
data NodeHardForkProtocolConfiguration =
NodeHardForkProtocolConfiguration {
-- | If we have knowledge about when the Shelley hard fork is then we
-- have an opportunity to optimise the bulk sync slightly.
--
npcShelleyHardForkNotBeforeEpoch :: Maybe EpochNo
-- | For testing purposes we support specifying that the hard fork
-- happens at an exact epoch number (ie the first epoch of the new era).
--
-- Obviously if this is used, all the nodes in the test cluster must be
-- configured the same, or they will disagree.
--
, npcTestShelleyHardForkAtEpoch :: Maybe EpochNo
-- | For testing purposes we support specifying that the hard fork
-- happens at a given major protocol version. For example this can be
-- used to cause the Shelley hard fork to occur at the transition from
-- protocol version 0 to version 1 (rather than the default of from 1 to
-- 2) which can make the test setup simpler.
--
-- Obviously if this is used, all the nodes in the test cluster must be
-- configured the same, or they will disagree.
--
, npcTestShelleyHardForkAtVersion :: Maybe Word
}
deriving Show
newtype SocketPath = SocketPath
{ unSocketPath :: FilePath }
deriving stock (Eq, Ord)
deriving newtype (FromJSON, IsString, Show)
newtype TopologyFile = TopologyFile
{ unTopology :: FilePath }
deriving newtype Show
instance AdjustFilePaths NodeProtocolConfiguration where
adjustFilePaths f (NodeProtocolConfigurationMock pc) =
NodeProtocolConfigurationMock (adjustFilePaths f pc)
adjustFilePaths f (NodeProtocolConfigurationByron pc) =
NodeProtocolConfigurationByron (adjustFilePaths f pc)
adjustFilePaths f (NodeProtocolConfigurationShelley pc) =
NodeProtocolConfigurationShelley (adjustFilePaths f pc)
adjustFilePaths f (NodeProtocolConfigurationCardano pcb pcs pch) =
NodeProtocolConfigurationCardano (adjustFilePaths f pcb)
(adjustFilePaths f pcs)
pch
instance AdjustFilePaths NodeMockProtocolConfiguration where
adjustFilePaths _f x = x -- Contains no file paths
instance AdjustFilePaths NodeByronProtocolConfiguration where
adjustFilePaths f x@NodeByronProtocolConfiguration {
npcByronGenesisFile
} =
x { npcByronGenesisFile = adjustFilePaths f npcByronGenesisFile }
instance AdjustFilePaths NodeShelleyProtocolConfiguration where
adjustFilePaths f x@NodeShelleyProtocolConfiguration {
npcShelleyGenesisFile
} =
x { npcShelleyGenesisFile = adjustFilePaths f npcShelleyGenesisFile }
instance AdjustFilePaths SocketPath where
adjustFilePaths f (SocketPath p) = SocketPath (f p)
instance AdjustFilePaths GenesisFile where
adjustFilePaths f (GenesisFile p) = GenesisFile (f p)
instance AdjustFilePaths a => AdjustFilePaths (Maybe a) where
adjustFilePaths f = fmap (adjustFilePaths f)
ncProtocol :: NodeConfiguration -> Protocol
ncProtocol nc =
case ncProtocolConfig nc of
NodeProtocolConfigurationMock npc -> MockProtocol (npcMockProtocol npc)
NodeProtocolConfigurationByron{} -> ByronProtocol
NodeProtocolConfigurationShelley{} -> ShelleyProtocol
NodeProtocolConfigurationCardano{} -> CardanoProtocol
parseNodeConfiguration :: NodeCLI -> IO NodeConfiguration
parseNodeConfiguration NodeCLI{configFile} = parseNodeConfigurationFP configFile
parseNodeConfigurationFP :: ConfigYamlFilePath -> IO NodeConfiguration
parseNodeConfigurationFP (ConfigYamlFilePath fp) = do
nc <- decodeFileThrow fp
-- Make all the files be relative to the location of the config file.
pure $ adjustFilePaths (\p -> takeDirectory fp <> "/" <> p) nc
-- | A human readable name for the protocol
--
protocolName :: Protocol -> String
protocolName (MockProtocol MockBFT) = "Mock BFT"
protocolName (MockProtocol MockPBFT) = "Mock PBFT"
protocolName (MockProtocol MockPraos) = "Mock Praos"
protocolName ByronProtocol = "Byron"
protocolName ShelleyProtocol = "Shelley"
protocolName CardanoProtocol = "Byron; Shelley"