Skip to content

Commit b1f8cfc

Browse files
committed
Simplify withHydraNode
1 parent fb22d96 commit b1f8cfc

File tree

1 file changed

+3
-101
lines changed

1 file changed

+3
-101
lines changed

hydra-cluster/src/HydraNode.hs

Lines changed: 3 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -477,107 +477,9 @@ withHydraNode ::
477477
[Int] ->
478478
(HydraClient -> IO a) ->
479479
IO a
480-
withHydraNode tracer chainConfig workDir hydraNodeId hydraSKey hydraVKeys allNodeIds action =
481-
withLogFile logFilePath $ \logFileHandle -> do
482-
-- NOTE: AirPlay on MacOS uses 5000 and we must avoid it.
483-
when (os == "darwin") $ port `shouldNotBe` (5_000 :: Network.PortNumber)
484-
let stateDir = workDir </> "state-" <> show hydraNodeId
485-
createDirectoryIfMissing True stateDir
486-
let cardanoLedgerProtocolParametersFile = stateDir </> "protocol-parameters.json"
487-
case chainConfig of
488-
Offline _ ->
489-
readConfigFile "protocol-parameters.json"
490-
>>= writeFileBS cardanoLedgerProtocolParametersFile
491-
Cardano CardanoChainConfig{chainBackendOptions} -> do
492-
protocolParameters <- case chainBackendOptions of
493-
Direct DirectOptions{networkId, nodeSocket} ->
494-
-- NOTE: This implicitly tests of cardano-cli with hydra-node
495-
cliQueryProtocolParameters nodeSocket networkId
496-
Blockfrost BlockfrostOptions{projectPath} -> do
497-
prj <- Blockfrost.projectFromFile projectPath
498-
toJSON <$> Blockfrost.runBlockfrostM prj Blockfrost.queryProtocolParameters
499-
500-
Aeson.encodeFile cardanoLedgerProtocolParametersFile $
501-
protocolParameters
502-
& atKey "txFeeFixed" ?~ toJSON (Number 0)
503-
& atKey "txFeePerByte" ?~ toJSON (Number 0)
504-
& key "executionUnitPrices" . atKey "priceMemory" ?~ toJSON (Number 0)
505-
& key "executionUnitPrices" . atKey "priceSteps" ?~ toJSON (Number 0)
506-
& atKey "utxoCostPerByte" ?~ toJSON (Number 0)
507-
& atKey "treasuryCut" ?~ toJSON (Number 0)
508-
& atKey "minFeeRefScriptCostPerByte" ?~ toJSON (Number 0)
509-
510-
let hydraSigningKey = stateDir </> "me.sk"
511-
void $ writeFileTextEnvelope (File hydraSigningKey) Nothing hydraSKey
512-
hydraVerificationKeys <- forM (zip [1 ..] hydraVKeys) $ \(i :: Int, vKey) -> do
513-
let filepath = stateDir </> ("other-" <> show i <> ".vk")
514-
filepath <$ writeFileTextEnvelope (File filepath) Nothing vKey
515-
516-
let cmd =
517-
( proc "hydra-node" . toArgs $
518-
-- NOTE: Using 0.0.0.0 over 127.0.0.1 will make the hydra-node
519-
-- crash if it can't bind the interface and make tests fail more
520-
-- obvious when e.g. a hydra-node instance is already running.
521-
RunOptions
522-
{ verbosity = Verbose "HydraNode"
523-
, nodeId = NodeId $ show hydraNodeId
524-
, listen = Host "0.0.0.0" (fromIntegral $ 5_000 + hydraNodeId)
525-
, advertise = Nothing
526-
, peers
527-
, apiHost = "0.0.0.0"
528-
, apiPort = fromIntegral $ 4_000 + hydraNodeId
529-
, tlsCertPath = Nothing
530-
, tlsKeyPath = Nothing
531-
, monitoringPort = Just $ fromIntegral $ 6_000 + hydraNodeId
532-
, hydraSigningKey
533-
, hydraVerificationKeys
534-
, persistenceDir = stateDir
535-
, persistenceRotateAfter = Nothing
536-
, chainConfig
537-
, whichEtcd = EmbeddedEtcd
538-
, ledgerConfig =
539-
CardanoLedgerConfig
540-
{ cardanoLedgerProtocolParametersFile
541-
}
542-
}
543-
)
544-
& setStdout (useHandleOpen logFileHandle)
545-
& setStderr createPipe
546-
547-
traceWith tracer $ HydraNodeCommandSpec $ show cmd
548-
549-
withProcessTerm cmd $ \p -> do
550-
-- NOTE: exit code thread gets cancelled if 'action' terminates first
551-
race
552-
(collectAndCheckExitCode p)
553-
(withConnectionToNode tracer hydraNodeId action)
554-
<&> either absurd id
555-
where
556-
collectAndCheckExitCode p = do
557-
let h = getStderr p
558-
waitExitCode p >>= \case
559-
ExitSuccess -> failure "hydra-node stopped early"
560-
ExitFailure ec -> do
561-
err <- hGetContents h
562-
failure . toString $
563-
unlines
564-
[ "hydra-node (nodeId = " <> show hydraNodeId <> ") exited with failure code: " <> show ec
565-
, decodeUtf8 err
566-
]
567-
568-
port = fromIntegral $ 5_000 + hydraNodeId
569-
570-
-- NOTE: See comment above about 0.0.0.0 vs 127.0.0.1
571-
peers =
572-
[ Host
573-
{ Network.hostname = "0.0.0.0"
574-
, Network.port = fromIntegral $ 5_000 + i
575-
}
576-
| i <- allNodeIds
577-
, i /= hydraNodeId
578-
]
579-
580-
logFilePath = workDir </> "logs" </> "hydra-node-" <> show hydraNodeId <.> "log"
480+
withHydraNode tracer chainConfig workDir hydraNodeId hydraSKey hydraVKeys allNodeIds action = do
481+
opts <- prepareHydraNode chainConfig workDir hydraNodeId hydraSKey hydraVKeys allNodeIds id
482+
withPreparedHydraNode tracer workDir hydraNodeId opts action
581483

582484
withConnectionToNode :: forall a. Tracer IO HydraNodeLog -> Int -> (HydraClient -> IO a) -> IO a
583485
withConnectionToNode tracer hydraNodeId =

0 commit comments

Comments
 (0)