Skip to content

Commit

Permalink
Remove cardano-cli requirement replacing it with our own CardanoClien…
Browse files Browse the repository at this point in the history
…t module
  • Loading branch information
ch1bo authored and abailly-iohk committed May 2, 2022
1 parent ac36d79 commit 6ab8f5f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 86 deletions.
2 changes: 1 addition & 1 deletion docs/docs/getting-started/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ You can also use `nix-build` to build the project and all executables. You will
</TerminalWindow>
````

1. To run integration tests, we need to install `cardano-node` & `cardano-cli` v1.32.0-rc2 to path. For example, see the official doc [here](https://developers.cardano.org/docs/get-started/installing-cardano-node).
1. To run integration tests & benchmarks, do install a recent `cardano-node` to the path. For example, see the official doc [here](https://developers.cardano.org/docs/get-started/installing-cardano-node).

1. Build and test everything:

Expand Down
74 changes: 0 additions & 74 deletions hydra-cluster/src/CardanoNode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module CardanoNode where

import Hydra.Prelude

import Control.Retry (constantDelay, limitRetriesByCumulativeDelay, retrying)
import Control.Tracer (Tracer, traceWith)
import Data.Aeson ((.=))
import qualified Data.Aeson as Aeson
Expand All @@ -23,7 +22,6 @@ import System.Process (
CreateProcess (..),
StdStream (UseHandle),
proc,
readCreateProcessWithExitCode,
readProcess,
withCreateProcess,
)
Expand Down Expand Up @@ -291,78 +289,6 @@ generateCardanoKey = do
sk <- generateSigningKey AsPaymentKey
pure (getVerificationKey sk, sk)

-- | Make a 'CreateProcess' for running @cardano-cli@. The program must be on
-- the @PATH@, as normal. Sets @CARDANO_NODE_SOCKET_PATH@ for the subprocess, if
-- a 'CardanoNodeConn' is provided.
cliCreateProcess ::
-- | for logging the command
Tracer IO NodeLog ->
-- | cardano node socket path
FilePath ->
-- | command-line arguments
[Text] ->
IO CreateProcess
cliCreateProcess tr sock args = do
traceWith tr (MsgCLI args)
let socketEnv = ("CARDANO_NODE_SOCKET_PATH", sock)
let cp = proc "cardano-cli" $ fmap toString args
pure $ cp{env = Just (socketEnv : fromMaybe [] (env cp))}

data ChainTip = ChainTip
{ slot :: Integer
, hash :: Text
, block :: Integer
}
deriving stock (Show, Generic)
deriving anyclass (FromJSON)

-- | Query a cardano node tip with retrying.
cliQueryTip ::
Tracer IO NodeLog ->
-- | cardano node socket path
FilePath ->
IO ChainTip
cliQueryTip tr sock = do
let msg = "Checking for usable socket file " <> toText sock
bytes <-
cliRetry tr msg
=<< cliCreateProcess
tr
sock
["query", "tip", "--testnet-magic", "42", "--cardano-mode"]
traceWith tr $ MsgSocketIsReady sock
case Aeson.eitherDecode' (fromStrict bytes) of
Left e -> fail e
Right tip -> pure tip

-- | Runs a @cardano-cli@ command and retries for up to 30 seconds if the
-- command failed.
--
-- Assumes @cardano-cli@ is available in @PATH@.
cliRetry ::
Tracer IO NodeLog ->
-- | message to print before running command
Text ->
CreateProcess ->
IO ByteString
cliRetry tracer msg cp = do
(st, out, err) <- retrying pol (const isFail) (const cmd)
traceWith tracer $ MsgCLIStatus msg (show st)
case st of
ExitSuccess -> pure $ encodeUtf8 out
ExitFailure _ ->
throwIO $ ProcessHasExited ("cardano-cli failed: " <> toText err) st
where
cmd = do
traceWith tracer $ MsgCLIRetry msg
(st, out, err) <- readCreateProcessWithExitCode cp mempty
case st of
ExitSuccess -> pure ()
ExitFailure code -> traceWith tracer (MsgCLIRetryResult msg code)
pure (st, out, err)
isFail (st, _, _) = pure (st /= ExitSuccess)
pol = limitRetriesByCumulativeDelay 30_000_000 $ constantDelay 1_000_000

data ProcessHasExited = ProcessHasExited Text ExitCode
deriving (Show)

Expand Down
22 changes: 11 additions & 11 deletions hydra-cluster/test/Test/CardanoClusterSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Test.CardanoClusterSpec where
import Hydra.Prelude
import Test.Hydra.Prelude

import CardanoClient (queryTip)
import CardanoCluster (
Actor (Alice),
ClusterConfig (..),
Expand All @@ -14,7 +15,8 @@ import CardanoCluster (
seedFromFaucet_,
withCluster,
)
import CardanoNode (ChainTip (..), RunningNode (..), cliQueryTip)
import CardanoNode (RunningNode (..))
import Hydra.Cardano.Api (ChainPoint (..))
import Hydra.Logging (Tracer, showLogsOnFailure)

spec :: Spec
Expand All @@ -32,17 +34,15 @@ spec =
failAfter 30 $ assertCanSpendInitialFunds cluster

assertNetworkIsProducingBlock :: Tracer IO ClusterLog -> RunningCluster -> IO ()
assertNetworkIsProducingBlock tracer = go (-1)
assertNetworkIsProducingBlock _ = \case
(RunningCluster _ (RunningNode _ socket : _)) -> go socket
_ -> error "empty cluster?"
where
go blk cluster = case cluster of
RunningCluster _ (RunningNode nodeId socket : _) -> do
waitForNewBlock
tip <- cliQueryTip (contramap (MsgFromNode nodeId) tracer) socket
if block tip > blk
then pure ()
else go (block tip) cluster
_ ->
error "empty cluster?"
go socket = do
waitForNewBlock
queryTip defaultNetworkId socket >>= \case
ChainPointAtGenesis -> go socket
ChainPoint{} -> pure ()

assertCanSpendInitialFunds :: HasCallStack => RunningCluster -> IO ()
assertCanSpendInitialFunds = \case
Expand Down

0 comments on commit 6ab8f5f

Please sign in to comment.