-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathOptions.hs
789 lines (724 loc) · 23.6 KB
/
Options.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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
{-# OPTIONS_GHC -Wno-orphans #-}
module Hydra.Options (
module Hydra.Options,
ParserResult (..),
renderFailure,
) where
import Hydra.Prelude
import Control.Arrow (left)
import Data.ByteString qualified as BS
import Data.ByteString.Char8 qualified as BSC
import Data.IP (IP (IPv4), toIPv4, toIPv4w)
import Data.Text (unpack)
import Data.Text qualified as T
import Data.Time.Clock (nominalDiffTimeToSeconds)
import Data.Version (Version (..), showVersion)
import Hydra.Cardano.Api (
AsType (AsTxId),
ChainPoint (..),
File (..),
NetworkId (..),
NetworkMagic (..),
SlotNo (..),
SocketPath,
TxId (..),
deserialiseFromRawBytes,
deserialiseFromRawBytesHex,
proxyToAsType,
serialiseToRawBytesHexText,
)
import Hydra.Chain (maximumNumberOfParties)
import Hydra.ContestationPeriod (ContestationPeriod (UnsafeContestationPeriod))
import Hydra.Contract qualified as Contract
import Hydra.Ledger.Cardano ()
import Hydra.Logging (Verbosity (..))
import Hydra.Network (Host, NodeId (NodeId), PortNumber, readHost, readPort)
import Hydra.Party (Party)
import Hydra.Version (embeddedRevision, gitRevision, unknownVersion)
import Options.Applicative (
Parser,
ParserInfo,
ParserResult (..),
auto,
command,
completer,
defaultPrefs,
eitherReader,
execParserPure,
flag,
flag',
footer,
fullDesc,
handleParseResult,
header,
help,
helper,
hsubparser,
info,
infoOption,
listCompleter,
long,
maybeReader,
metavar,
option,
progDesc,
progDescDoc,
renderFailure,
short,
showDefault,
strOption,
subparser,
value,
)
import Options.Applicative.Builder (str)
import Options.Applicative.Help (vsep)
import Paths_hydra_node (version)
import Test.QuickCheck (elements, listOf, listOf1, oneof, suchThat, vectorOf)
data ParamMismatch
= ContestationPeriodMismatch {loadedCp :: ContestationPeriod, configuredCp :: ContestationPeriod}
| PartiesMismatch {loadedParties :: [Party], configuredParties :: [Party]}
deriving stock (Generic, Eq, Show)
deriving anyclass (ToJSON, FromJSON)
instance Arbitrary ParamMismatch where
arbitrary = genericArbitrary
data Command
= Run RunOptions
| Publish PublishOptions
| GenHydraKey GenerateKeyPair
deriving stock (Show, Eq)
commandParser :: Parser Command
commandParser =
asum
[ Run <$> runOptionsParser
, Publish <$> publishScriptsParser
, GenHydraKey <$> genHydraKeyParser
]
where
publishScriptsParser :: Parser PublishOptions
publishScriptsParser =
subparser $
command
"publish-scripts"
( info
(helper <*> publishOptionsParser)
( fullDesc
<> progDescDoc
( Just $
vsep
[ "Publish Hydra's Plutus scripts on chain to be used"
, "by the hydra-node as --hydra-script-tx-id."
, ""
, " ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ "
, " ┃ ⚠ WARNING ⚠ ┃ "
, " ┣═══════════════════════════════════════┫ "
, " ┃ This costs money. About 50 Ada. ┃ "
, " ┃ Spent using the provided signing key. ┃ "
, " ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ "
]
)
<> footer
"The command outputs the transaction id (in base16) \
\of the publishing transaction. This transaction id \
\can then be passed onto '--hydra-scripts-tx-id' to \
\start a hydra-node using the referenced scripts."
)
)
data PublishOptions = PublishOptions
{ publishNetworkId :: NetworkId
, publishNodeSocket :: SocketPath
, publishSigningKey :: FilePath
}
deriving stock (Show, Eq)
publishOptionsParser :: Parser PublishOptions
publishOptionsParser =
PublishOptions
<$> networkIdParser
<*> nodeSocketParser
<*> cardanoSigningKeyFileParser
data RunOptions = RunOptions
{ verbosity :: Verbosity
, nodeId :: NodeId
, -- NOTE: Why not a 'Host'?
host :: IP
, port :: PortNumber
, peers :: [Host]
, apiHost :: IP
, apiPort :: PortNumber
, monitoringPort :: Maybe PortNumber
, hydraSigningKey :: FilePath
, hydraVerificationKeys :: [FilePath]
, hydraScriptsTxId :: TxId
, persistenceDir :: FilePath
, chainConfig :: ChainConfig
, ledgerConfig :: LedgerConfig
}
deriving stock (Eq, Show, Generic)
deriving anyclass (ToJSON, FromJSON)
instance Arbitrary RunOptions where
arbitrary = do
verbosity <- elements [Quiet, Verbose "HydraNode"]
nodeId <- arbitrary
host <- IPv4 . toIPv4w <$> arbitrary
port <- arbitrary
peers <- reasonablySized arbitrary
apiHost <- IPv4 . toIPv4w <$> arbitrary
apiPort <- arbitrary
monitoringPort <- arbitrary
hydraSigningKey <- genFilePath "sk"
hydraVerificationKeys <- reasonablySized (listOf (genFilePath "vk"))
hydraScriptsTxId <- arbitrary
persistenceDir <- genDirPath
chainConfig <- arbitrary
ledgerConfig <- arbitrary
pure $
RunOptions
{ verbosity
, nodeId
, host
, port
, peers
, apiHost
, apiPort
, monitoringPort
, hydraSigningKey
, hydraVerificationKeys
, hydraScriptsTxId
, persistenceDir
, chainConfig
, ledgerConfig
}
runOptionsParser :: Parser RunOptions
runOptionsParser =
RunOptions
<$> verbosityParser
<*> nodeIdParser
<*> hostParser
<*> portParser
<*> many peerParser
<*> apiHostParser
<*> apiPortParser
<*> optional monitoringPortParser
<*> hydraSigningKeyFileParser
<*> many hydraVerificationKeyFileParser
<*> hydraScriptsTxIdParser
<*> persistenceDirParser
<*> chainConfigParser
<*> ledgerConfigParser
newtype GenerateKeyPair = GenerateKeyPair
{ outputFile :: FilePath
}
deriving stock (Eq, Show)
genHydraKeyParser :: Parser GenerateKeyPair
genHydraKeyParser =
hsubparser
( command
"gen-hydra-key"
( info
(helper <*> (GenerateKeyPair <$> outputFileParser))
(progDesc "Generate a pair of Hydra signing/verification keys (off-chain keys).")
)
)
outputFileParser :: Parser FilePath
outputFileParser =
strOption
( long "output-file"
<> metavar "FILE"
<> value "hydra-key"
<> help "Basename of files to generate key-pair into. Signing key will be suffixed '.sk' and verification key '.vk'"
)
newtype LedgerConfig = CardanoLedgerConfig
{ cardanoLedgerProtocolParametersFile :: FilePath
}
deriving stock (Eq, Show, Generic)
deriving anyclass (ToJSON, FromJSON)
defaultLedgerConfig :: LedgerConfig
defaultLedgerConfig =
CardanoLedgerConfig
{ cardanoLedgerProtocolParametersFile = "protocol-parameters.json"
}
instance Arbitrary LedgerConfig where
arbitrary = do
cardanoLedgerProtocolParametersFile <- genFilePath ".json"
pure $ CardanoLedgerConfig{cardanoLedgerProtocolParametersFile}
ledgerConfigParser :: Parser LedgerConfig
ledgerConfigParser =
CardanoLedgerConfig
<$> cardanoLedgerProtocolParametersParser
cardanoLedgerProtocolParametersParser :: Parser FilePath
cardanoLedgerProtocolParametersParser =
strOption
( long "ledger-protocol-parameters"
<> metavar "FILE"
<> value "protocol-parameters.json"
<> showDefault
<> help
"Path to protocol parameters used in the Hydra Head. \
\See manual how to configure this."
)
data ChainConfig = DirectChainConfig
{ networkId :: NetworkId
-- ^ Network identifer to which we expect to connect.
, nodeSocket :: SocketPath
-- ^ Path to a domain socket used to connect to the server.
, cardanoSigningKey :: FilePath
-- ^ Path to the cardano signing key of the internal wallet.
, cardanoVerificationKeys :: [FilePath]
-- ^ Paths to other node's verification keys.
, startChainFrom :: Maybe ChainPoint
-- ^ Point at which to start following the chain.
, contestationPeriod :: ContestationPeriod
}
deriving stock (Eq, Show, Generic)
deriving anyclass (ToJSON, FromJSON)
defaultChainConfig :: ChainConfig
defaultChainConfig =
DirectChainConfig
{ networkId = Testnet (NetworkMagic 42)
, nodeSocket = "node.socket"
, cardanoSigningKey = "cardano.sk"
, cardanoVerificationKeys = []
, startChainFrom = Nothing
, contestationPeriod = defaultContestationPeriod
}
instance Arbitrary ChainConfig where
arbitrary = do
networkId <- Testnet . NetworkMagic <$> arbitrary
nodeSocket <- File <$> genFilePath "socket"
cardanoSigningKey <- genFilePath ".sk"
cardanoVerificationKeys <- reasonablySized (listOf (genFilePath ".vk"))
startChainFrom <- oneof [pure Nothing, Just <$> genChainPoint]
contestationPeriod <- arbitrary `suchThat` (> UnsafeContestationPeriod 0)
pure $
DirectChainConfig
{ networkId
, nodeSocket
, cardanoSigningKey
, cardanoVerificationKeys
, startChainFrom
, contestationPeriod
}
chainConfigParser :: Parser ChainConfig
chainConfigParser =
DirectChainConfig
<$> networkIdParser
<*> nodeSocketParser
<*> cardanoSigningKeyFileParser
<*> many cardanoVerificationKeyFileParser
<*> optional startChainFromParser
<*> contestationPeriodParser
networkIdParser :: Parser NetworkId
networkIdParser = pMainnet <|> fmap Testnet pTestnetMagic
where
pMainnet :: Parser NetworkId
pMainnet =
flag'
Mainnet
( long "mainnet"
<> help "Use the mainnet magic id."
)
pTestnetMagic :: Parser NetworkMagic
pTestnetMagic =
NetworkMagic
<$> option
auto
( long "testnet-magic"
<> metavar "NATURAL"
<> value 42
<> showDefault
<> completer (listCompleter ["1", "2", "42"])
<> help
"Network identifier for a testnet to connect to. We only need to \
\provide the magic number here. For example: '2' is the 'preview' \
\network. See https://book.world.dev.cardano.org/environments.html for available networks."
)
nodeSocketParser :: Parser SocketPath
nodeSocketParser =
strOption
( long "node-socket"
<> metavar "FILE"
<> value "node.socket"
<> showDefault
<> help
"Filepath to local unix domain socket used to communicate with \
\the cardano node."
)
cardanoSigningKeyFileParser :: Parser FilePath
cardanoSigningKeyFileParser =
strOption
( long "cardano-signing-key"
<> metavar "FILE"
<> showDefault
<> value "cardano.sk"
<> help
"Cardano signing key of our hydra-node. This will be used to authorize \
\Hydra protocol transactions for heads the node takes part in and any \
\funds owned by this key will be used as 'fuel'."
)
cardanoVerificationKeyFileParser :: Parser FilePath
cardanoVerificationKeyFileParser =
option
str
( long "cardano-verification-key"
<> metavar "FILE"
<> help
( "Cardano verification key of another party in the Head. Can be \
\provided multiple times, once for each participant (current maximum limit is "
<> show maximumNumberOfParties
<> ")."
)
)
hydraSigningKeyFileParser :: Parser FilePath
hydraSigningKeyFileParser =
option
str
( long "hydra-signing-key"
<> metavar "FILE"
<> value "hydra.sk"
<> showDefault
<> help "Hydra signing key used by our hydra-node."
)
hydraVerificationKeyFileParser :: Parser FilePath
hydraVerificationKeyFileParser =
option
str
( long "hydra-verification-key"
<> metavar "FILE"
<> help
( "Hydra verification key of another party in the Head. Can be \
\provided multiple times, once for each participant (current maximum limit is "
<> show maximumNumberOfParties
<> " )."
)
)
peerParser :: Parser Host
peerParser =
option
(maybeReader readHost)
( long "peer"
<> short 'P'
<> help
( "A peer address in the form <host>:<port>, where <host> can be an IP \
\address, or a host name. Can be provided multiple times, once for \
\each peer (current maximum limit is "
<> show maximumNumberOfParties
<> " peers)."
)
)
nodeIdParser :: Parser NodeId
nodeIdParser =
option
str
( long "node-id"
<> short 'n'
<> metavar "NODE-ID"
<> help
"The Hydra node identifier used on the Hydra network. It is \
\important to have a unique identifier in order to be able to \
\distinguish between connected peers."
)
verbosityParser :: Parser Verbosity
verbosityParser =
flag
(Verbose "HydraNode")
Quiet
( long "quiet"
<> short 'q'
<> help "Turns off logging."
)
hostParser :: Parser IP
hostParser =
option
auto
( long "host"
<> short 'h'
-- XXX: This is default does not make sense, should use 0.0.0.0.
<> value "127.0.0.1"
<> showDefault
<> metavar "IP"
<> help "Listen address for incoming Hydra network connections."
)
portParser :: Parser PortNumber
portParser =
option
(maybeReader readPort)
( long "port"
<> short 'p'
<> value 5001
<> showDefault
<> metavar "PORT"
<> help "Listen port for incoming Hydra network connections."
)
apiHostParser :: Parser IP
apiHostParser =
option
auto
( long "api-host"
<> value "127.0.0.1"
<> metavar "IP"
<> showDefault
<> help "Listen address for incoming client API connections."
)
apiPortParser :: Parser PortNumber
apiPortParser =
option
(maybeReader readPort)
( long "api-port"
<> value 4001
<> showDefault
<> metavar "PORT"
<> help "Listen port for incoming client API connections."
)
monitoringPortParser :: Parser PortNumber
monitoringPortParser =
option
(maybeReader readPort)
( long "monitoring-port"
<> metavar "PORT"
<> help
"Listen port for monitoring and metrics via prometheus. If left \
\empty, monitoring server is not started."
)
startChainFromParser :: Parser ChainPoint
startChainFromParser =
option
(maybeReader readChainPoint)
( long "start-chain-from"
<> metavar "SLOT.HEADER_HASH"
<> help
"The id of the block we want to start observing the chain from. \
\If not given, uses the chain tip at startup. Composed by the slot \
\number, a separator ('.') and the hash of the block header. \
\For example: 52970883.d36a9936ae7a07f5f4bdc9ad0b23761cb7b14f35007e54947e27a1510f897f04."
)
where
readChainPoint :: String -> Maybe ChainPoint
readChainPoint = \case
"0" -> Just ChainPointAtGenesis
chainPointStr ->
case T.splitOn "." (toText chainPointStr) of
[slotNoTxt, headerHashTxt] -> do
slotNo <- SlotNo <$> readMaybe (toString slotNoTxt)
headerHash <-
either (const Nothing) Just $
deserialiseFromRawBytesHex (proxyToAsType Proxy) (encodeUtf8 headerHashTxt)
pure $ ChainPoint slotNo headerHash
_emptyOrSingularList ->
Nothing
hydraScriptsTxIdParser :: Parser TxId
hydraScriptsTxIdParser =
option
(eitherReader $ left show . deserialiseFromRawBytesHex AsTxId . BSC.pack)
( long "hydra-scripts-tx-id"
<> metavar "TXID"
<> value "0101010101010101010101010101010101010101010101010101010101010101"
<> help
"The transaction which is expected to have published Hydra scripts as \
\reference scripts in its outputs. Note: All scripts need to be in the \
\first 10 outputs. See release notes for pre-published versions. You \
\can use the 'publish-scripts' sub-command to publish them yourself."
)
persistenceDirParser :: Parser FilePath
persistenceDirParser =
option
str
( long "persistence-dir"
<> metavar "DIR"
<> value "./"
<> help
"The directory where the Hydra Head state is stored.\
\Do not edit these files manually!"
)
hydraNodeCommand :: ParserInfo Command
hydraNodeCommand =
info
( commandParser
<**> versionInfo
<**> scriptInfo
<**> helper
)
( fullDesc
<> progDesc "Starts a Hydra Node"
<> header "hydra-node - Implementation of the Hydra Head protocol"
)
where
versionInfo =
infoOption
(showVersion hydraNodeVersion)
(long "version" <> help "Show version")
scriptInfo =
infoOption
(decodeUtf8 $ encodePretty Contract.scriptInfo)
(long "script-info" <> help "Dump script info as JSON")
hydraNodeVersion :: Version
hydraNodeVersion =
version & \(Version semver _) -> Version semver revision
where
revision =
maybeToList $
embeddedRevision
<|> gitRevision
<|> Just unknownVersion
defaultContestationPeriod :: ContestationPeriod
defaultContestationPeriod = UnsafeContestationPeriod 60
contestationPeriodParser :: Parser ContestationPeriod
contestationPeriodParser =
option
(parseNatural <|> parseNominalDiffTime)
( long "contestation-period"
<> metavar "CONTESTATION-PERIOD"
<> value defaultContestationPeriod
<> showDefault
<> completer (listCompleter ["60", "180", "300"])
<> help
"Contestation period for close transaction in seconds. \
\ If this value is not in sync with other participants hydra-node will ignore the initial tx.\
\ Additionally, this value needs to make sense compared to the current network we are running."
)
where
parseNatural = UnsafeContestationPeriod <$> auto
parseNominalDiffTime =
auto >>= \dt -> do
let s = nominalDiffTimeToSeconds dt
if s <= 0
then fail "negative contestation period"
else pure $ UnsafeContestationPeriod $ truncate s
data InvalidOptions
= MaximumNumberOfPartiesExceeded
| CardanoAndHydraKeysMissmatch
deriving stock (Eq, Show)
explain :: InvalidOptions -> String
explain = \case
MaximumNumberOfPartiesExceeded -> "Maximum number of parties is currently set to: " <> show maximumNumberOfParties
CardanoAndHydraKeysMissmatch -> "Number of loaded cardano and hydra keys needs to match"
-- | Validate cmd line arguments for hydra-node and check if they make sense before actually running the node.
-- Rules we apply:
-- - Check if number of parties is bigger than our hardcoded limit
-- (by looking at loaded hydra or cardano keys and comparing it to the 'maximumNumberOfParties')
-- - Check that number of loaded hydra keys match with the number of loaded cardano keys
-- (by comparing lengths of the two lists)
validateRunOptions :: RunOptions -> Either InvalidOptions ()
validateRunOptions RunOptions{hydraVerificationKeys, chainConfig}
| numberOfOtherParties + 1 > maximumNumberOfParties = Left MaximumNumberOfPartiesExceeded
| length (cardanoVerificationKeys chainConfig) /= length hydraVerificationKeys =
Left CardanoAndHydraKeysMissmatch
| otherwise = Right ()
where
-- let's take the higher number of loaded cardano/hydra keys
numberOfOtherParties =
max (length hydraVerificationKeys) (length $ cardanoVerificationKeys chainConfig)
-- | Parse command-line arguments into a `Option` or exit with failure and error message.
parseHydraCommand :: IO Command
parseHydraCommand = getArgs <&> parseHydraCommandFromArgs >>= handleParseResult
-- | Pure parsing of `Option` from a list of arguments.
parseHydraCommandFromArgs :: [String] -> ParserResult Command
parseHydraCommandFromArgs = execParserPure defaultPrefs hydraNodeCommand
-- | Convert an 'Options' instance into the corresponding list of command-line arguments.
--
-- This is useful in situations where one wants to programatically define 'Options', providing
-- some measure of type safety, without having to juggle with strings.
toArgs :: RunOptions -> [String]
toArgs
RunOptions
{ verbosity
, nodeId
, host
, port
, peers
, apiHost
, apiPort
, monitoringPort
, hydraSigningKey
, hydraVerificationKeys
, hydraScriptsTxId
, persistenceDir
, chainConfig
, ledgerConfig
} =
isVerbose verbosity
<> ["--node-id", unpack nId]
<> ["--host", show host]
<> ["--port", show port]
<> ["--api-host", show apiHost]
<> ["--api-port", show apiPort]
<> ["--hydra-signing-key", hydraSigningKey]
<> concatMap (\vk -> ["--hydra-verification-key", vk]) hydraVerificationKeys
<> concatMap toArgPeer peers
<> maybe [] (\mport -> ["--monitoring-port", show mport]) monitoringPort
<> ["--hydra-scripts-tx-id", toString $ serialiseToRawBytesHexText hydraScriptsTxId]
<> ["--persistence-dir", persistenceDir]
<> argsChainConfig
<> argsLedgerConfig
where
(NodeId nId) = nodeId
isVerbose = \case
Quiet -> ["--quiet"]
_ -> []
toArgPeer p =
["--peer", show p]
toArgStartChainFrom = \case
Just ChainPointAtGenesis ->
error "ChainPointAtGenesis"
Just (ChainPoint (SlotNo slotNo) headerHash) ->
let headerHashBase16 = toString (serialiseToRawBytesHexText headerHash)
in ["--start-chain-from", show slotNo <> "." <> headerHashBase16]
Nothing ->
[]
argsChainConfig =
toArgNetworkId networkId
<> ["--node-socket", unFile nodeSocket]
<> ["--cardano-signing-key", cardanoSigningKey]
<> ["--contestation-period", show contestationPeriod]
<> concatMap (\vk -> ["--cardano-verification-key", vk]) cardanoVerificationKeys
<> toArgStartChainFrom startChainFrom
argsLedgerConfig =
["--ledger-protocol-parameters", cardanoLedgerProtocolParametersFile]
CardanoLedgerConfig
{ cardanoLedgerProtocolParametersFile
} = ledgerConfig
DirectChainConfig
{ networkId
, nodeSocket
, cardanoSigningKey
, cardanoVerificationKeys
, startChainFrom
, contestationPeriod
} = chainConfig
defaultRunOptions :: RunOptions
defaultRunOptions =
RunOptions
{ verbosity = Verbose "HydraNode"
, nodeId = NodeId "hydra-node-1"
, host = localhost
, port = 5001
, peers = []
, apiHost = localhost
, apiPort = 4001
, monitoringPort = Nothing
, hydraSigningKey = "hydra.sk"
, hydraVerificationKeys = []
, hydraScriptsTxId = TxId "0101010101010101010101010101010101010101010101010101010101010101"
, persistenceDir = "./"
, chainConfig = defaultChainConfig
, ledgerConfig = defaultLedgerConfig
}
where
localhost = IPv4 $ toIPv4 [127, 0, 0, 1]
toArgNetworkId :: NetworkId -> [String]
toArgNetworkId = \case
Mainnet -> ["--mainnet"]
Testnet (NetworkMagic magic) -> ["--testnet-magic", show magic]
genFilePath :: String -> Gen FilePath
genFilePath extension = do
path <- reasonablySized (listOf1 (elements ["a", "b", "c"]))
pure $ intercalate "/" path <> "." <> extension
genDirPath :: Gen FilePath
genDirPath = do
path <- reasonablySized (listOf1 (elements ["a", "b", "c"]))
pure $ intercalate "/" path
genChainPoint :: Gen ChainPoint
genChainPoint = ChainPoint <$> (SlotNo <$> arbitrary) <*> someHeaderHash
where
someHeaderHash = do
bytes <- vectorOf 32 arbitrary
let hash = either (error "invalid bytes") id $ deserialiseFromRawBytes (proxyToAsType Proxy) . BS.pack $ bytes
pure hash