-
Notifications
You must be signed in to change notification settings - Fork 721
/
Types.hs
444 lines (361 loc) · 16.1 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
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
module Cardano.Node.Types
( -- * Configuration
AdjustFilePaths(..)
, ConfigError(..)
, ConfigYamlFilePath(..)
, DbFile(..)
, GenesisFile(..)
, ProtocolFilepaths (..)
, GenesisHash(..)
, MaxConcurrencyBulkSync(..)
, MaxConcurrencyDeadline(..)
-- * Node addresses
, NodeAddress'(..)
, NodeIPAddress
, nodeAddressToSockAddr
, NodeIPv4Address
, NodeIPv6Address
, NodeDnsAddress
, nodeIPv4ToIPAddress
, nodeIPv6ToIPAddress
, nodeDnsAddressToDomainAddress
, NodeHostIPAddress (..)
, nodeHostIPAddressToSockAddr
, NodeHostIPv4Address (..)
, NodeHostIPv6Address (..)
, nodeHostIPv4AddressToIPAddress
, nodeHostIPv6AddressToIPAddress
, NodeHostDnsAddress (..)
, nodeHostDnsAddressToDomain
, PortNumber
, SocketPath(..)
, TopologyFile(..)
, NodeDiffusionMode (..)
-- * Consensus protocol configuration
, NodeByronProtocolConfiguration(..)
, NodeHardForkProtocolConfiguration(..)
, NodeProtocolConfiguration(..)
, NodeShelleyProtocolConfiguration(..)
, VRFPrivateKeyFilePermissionError(..)
, protocolName
, renderVRFPrivateKeyFilePermissionError
) where
import Cardano.Prelude
import Prelude (String, fail)
import Data.Aeson
import Data.IP (IP (..), IPv4, IPv6)
import qualified Data.IP as IP
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import qualified Network.DNS as DNS (Domain)
import Network.Socket (PortNumber, SockAddr (..))
import qualified Cardano.Chain.Update as Byron
import Cardano.Crypto (RequiresNetworkMagic (..))
import qualified Cardano.Crypto.Hash as Crypto
import Cardano.Node.Protocol.Types (Protocol (..))
import Cardano.Slotting.Slot (EpochNo)
import Ouroboros.Network.PeerSelection.RootPeersDNS (DomainAddress (..))
--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.
import Ouroboros.Network.NodeToNode (DiffusionMode (..))
-- | Errors for the cardano-config module.
newtype 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 (Eq, 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 = fail $ "Parsing of GenesisFile failed due to type mismatch. "
<> "Encountered: " <> 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 or IPv6 address with a port number.
data NodeAddress' addr = NodeAddress
{ naHostAddress :: !addr
, naPort :: !PortNumber
} deriving (Eq, Ord, Show, Functor)
type NodeIPAddress = NodeAddress' NodeHostIPAddress
type NodeIPv4Address = NodeAddress' NodeHostIPv4Address
type NodeIPv6Address = NodeAddress' NodeHostIPv6Address
type NodeDnsAddress = NodeAddress' NodeHostDnsAddress
instance FromJSON addr => FromJSON (NodeAddress' addr) where
parseJSON = withObject "NodeAddress" $ \v -> do
NodeAddress
<$> v .: "addr"
<*> ((fromIntegral :: Int -> PortNumber) <$> v .: "port")
instance ToJSON addr => ToJSON (NodeAddress' addr) where
toJSON na =
object
[ "addr" .= toJSON (naHostAddress na)
, "port" .= (fromIntegral (naPort na) :: Int)
]
nodeIPv4ToIPAddress :: NodeIPv4Address -> NodeIPAddress
nodeIPv4ToIPAddress = fmap nodeHostIPv4AddressToIPAddress
nodeIPv6ToIPAddress :: NodeIPv6Address -> NodeIPAddress
nodeIPv6ToIPAddress = fmap nodeHostIPv6AddressToIPAddress
nodeDnsAddressToDomainAddress :: NodeDnsAddress -> DomainAddress
nodeDnsAddressToDomainAddress NodeAddress { naHostAddress = NodeHostDnsAddress dns, naPort }
= DomainAddress (Text.encodeUtf8 dns) naPort
nodeAddressToSockAddr :: NodeIPAddress -> SockAddr
nodeAddressToSockAddr (NodeAddress addr port) =
case unNodeHostIPAddress addr of
IP.IPv4 ipv4 -> SockAddrInet port (IP.toHostAddress ipv4)
IP.IPv6 ipv6 -> SockAddrInet6 port 0 (IP.toHostAddress6 ipv6) 0
nodeHostIPAddressToSockAddr :: NodeIPAddress -> SockAddr
nodeHostIPAddressToSockAddr NodeAddress { naHostAddress = NodeHostIPAddress ip, naPort } =
case ip of
IPv4 ipv4 -> SockAddrInet (fromIntegral naPort) (IP.toHostAddress ipv4)
IPv6 ipv6 -> SockAddrInet6 (fromIntegral naPort) 0 (IP.toHostAddress6 ipv6) 0
newtype NodeHostIPv4Address
= NodeHostIPv4Address { unNodeHostIPv4Address :: IPv4 }
deriving newtype Show
deriving (Eq, Ord)
instance FromJSON NodeHostIPv4Address where
parseJSON (String ipStr) =
case readMaybe $ Text.unpack ipStr of
Just ip -> pure $ NodeHostIPv4Address ip
Nothing -> fail $ "Parsing of IPv4 failed: " <> Text.unpack ipStr
parseJSON invalid = fail $ "Parsing of IPv4 failed due to type mismatch. "
<> "Encountered: " <> show invalid <> "\n"
instance ToJSON NodeHostIPv4Address where
toJSON (NodeHostIPv4Address ip) = String (Text.pack $ show ip)
newtype NodeHostIPv6Address
= NodeHostIPv6Address { unNodeHostIPv6Address :: IPv6 }
deriving newtype Show
deriving (Eq, Ord)
instance FromJSON NodeHostIPv6Address where
parseJSON (String ipStr) =
case readMaybe $ Text.unpack ipStr of
Just ip -> pure $ NodeHostIPv6Address ip
Nothing -> fail $ "Parsing of IPv6 failed: " <> Text.unpack ipStr
parseJSON invalid = fail $ "Parsing of IPv6 failed due to type mismatch. "
<> "Encountered: " <> show invalid <> "\n"
instance ToJSON NodeHostIPv6Address where
toJSON (NodeHostIPv6Address ip) = String (Text.pack $ show ip)
newtype NodeHostIPAddress
= NodeHostIPAddress { unNodeHostIPAddress :: IP }
deriving newtype Show
deriving (Eq, Ord)
instance FromJSON NodeHostIPAddress where
parseJSON (String ipStr) =
case readMaybe $ Text.unpack ipStr of
Just ip -> pure $ NodeHostIPAddress ip
Nothing -> fail $ "Parsing of IP failed: " <> Text.unpack ipStr
parseJSON invalid = fail $ "Parsing of IP failed due to type mismatch. "
<> "Encountered: " <> show invalid <> "\n"
instance ToJSON NodeHostIPAddress where
toJSON (NodeHostIPAddress ip) = String (Text.pack $ show ip)
nodeHostIPv6AddressToIPAddress :: NodeHostIPv6Address -> NodeHostIPAddress
nodeHostIPv6AddressToIPAddress (NodeHostIPv6Address ip) = NodeHostIPAddress (IPv6 ip)
nodeHostIPv4AddressToIPAddress :: NodeHostIPv4Address -> NodeHostIPAddress
nodeHostIPv4AddressToIPAddress (NodeHostIPv4Address ip) = NodeHostIPAddress (IPv4 ip)
-- | Domain name.
--
newtype NodeHostDnsAddress
= NodeHostDnsAddress { unNodeHostDnsAddress :: Text }
deriving newtype Show
deriving (Eq, Ord)
nodeHostDnsAddressToDomain :: NodeHostDnsAddress -> DNS.Domain
nodeHostDnsAddressToDomain = Text.encodeUtf8 . unNodeHostDnsAddress
-- | Newtype wrapper which provides 'FromJSON' instance for 'DiffusionMode'.
--
newtype NodeDiffusionMode
= NodeDiffusionMode { getDiffusionMode :: DiffusionMode }
deriving newtype Show
instance FromJSON NodeDiffusionMode where
parseJSON (String str) =
case str of
"InitiatorOnly"
-> pure $ NodeDiffusionMode InitiatorOnlyDiffusionMode
"InitiatorAndResponder"
-> pure $ NodeDiffusionMode InitiatorAndResponderDiffusionMode
_ -> fail "Parsing NodeDiffusionMode failed: can be either 'InitiatorOnly' or 'InitiatorAndResponder'"
parseJSON _ = fail "Parsing NodeDiffusionMode failed"
class AdjustFilePaths a where
adjustFilePaths :: (FilePath -> FilePath) -> a -> a
data ProtocolFilepaths =
ProtocolFilepaths {
byronCertFile :: !(Maybe FilePath)
, byronKeyFile :: !(Maybe FilePath)
, shelleyKESFile :: !(Maybe FilePath)
, shelleyVRFFile :: !(Maybe FilePath)
, shelleyCertFile :: !(Maybe FilePath)
, shelleyBulkCredsFile :: !(Maybe FilePath)
} deriving (Eq, Show)
newtype GenesisHash = GenesisHash (Crypto.Hash Crypto.Blake2b_256 ByteString)
deriving newtype (Eq, Show, ToJSON, FromJSON)
data NodeProtocolConfiguration =
NodeProtocolConfigurationByron NodeByronProtocolConfiguration
| NodeProtocolConfigurationShelley NodeShelleyProtocolConfiguration
| NodeProtocolConfigurationCardano NodeByronProtocolConfiguration
NodeShelleyProtocolConfiguration
NodeHardForkProtocolConfiguration
deriving (Eq, Show)
data NodeShelleyProtocolConfiguration =
NodeShelleyProtocolConfiguration {
npcShelleyGenesisFile :: !GenesisFile
, npcShelleyGenesisFileHash :: !(Maybe GenesisHash)
, npcStubComputeDuration :: !Double
, npcStubComputePrecision :: !Double
}
deriving (Eq, Show)
data NodeByronProtocolConfiguration =
NodeByronProtocolConfiguration {
npcByronGenesisFile :: !GenesisFile
, npcByronGenesisFileHash :: !(Maybe GenesisHash)
, npcByronReqNetworkMagic :: !RequiresNetworkMagic
, npcByronPbftSignatureThresh :: !(Maybe Double)
--TODO: eliminate these two: it can be hard-coded
-- | Update application name.
, npcByronApplicationName :: !Byron.ApplicationName
-- | Application (ie software) version.
, npcByronApplicationVersion :: !Byron.NumSoftwareVersion
--TODO: eliminate these: it can be done automatically in consensus
-- | 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 (Eq, Show)
-- | Configuration relating to a hard forks themselves, not the specific eras.
--
data NodeHardForkProtocolConfiguration =
NodeHardForkProtocolConfiguration {
-- | 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
-- | 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.
--
, npcTestAllegraHardForkAtEpoch :: Maybe EpochNo
-- | For testing purposes we support specifying that the hard fork
-- happens at a given major protocol version.
--
-- Obviously if this is used, all the nodes in the test cluster must be
-- configured the same, or they will disagree.
--
, npcTestAllegraHardForkAtVersion :: Maybe Word
-- | 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.
--
, npcTestMaryHardForkAtEpoch :: 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.
--
, npcTestMaryHardForkAtVersion :: Maybe Word
}
deriving (Eq, Show)
newtype SocketPath = SocketPath
{ unSocketPath :: FilePath }
deriving stock (Eq, Ord)
deriving newtype (FromJSON, IsString, Show)
newtype TopologyFile = TopologyFile
{ unTopology :: FilePath }
deriving newtype (Show, Eq)
instance AdjustFilePaths NodeProtocolConfiguration where
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 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)
instance AdjustFilePaths (Last NodeProtocolConfiguration) where
adjustFilePaths f (Last (Just npc)) =
Last . Just $ adjustFilePaths f npc
adjustFilePaths _ (Last Nothing) = Last Nothing
instance AdjustFilePaths (Last SocketPath) where
adjustFilePaths f (Last (Just (SocketPath p))) = Last . Just $ SocketPath (f p)
adjustFilePaths _ (Last Nothing) = Last Nothing
-- | A human readable name for the protocol
--
protocolName :: Protocol -> String
protocolName ByronProtocol = "Byron"
protocolName ShelleyProtocol = "Shelley"
protocolName CardanoProtocol = "Byron; Shelley"
data VRFPrivateKeyFilePermissionError
= OtherPermissionsExist FilePath
| GroupPermissionsExist FilePath
| GenericPermissionsExist FilePath
deriving Show
renderVRFPrivateKeyFilePermissionError :: VRFPrivateKeyFilePermissionError -> Text
renderVRFPrivateKeyFilePermissionError err =
case err of
OtherPermissionsExist fp ->
"VRF private key file at: " <> Text.pack fp
<> " has \"other\" file permissions. Please remove all \"other\" file permissions."
GroupPermissionsExist fp ->
"VRF private key file at: " <> Text.pack fp
<> "has \"group\" file permissions. Please remove all \"group\" file permissions."
GenericPermissionsExist fp ->
"VRF private key file at: " <> Text.pack fp
<> "has \"generic\" file permissions. Please remove all \"generic\" file permissions."