Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for cardano-node sync #1224

Merged
merged 2 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions hydra-cluster/bench/Bench/EndToEnd.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import Hydra.Prelude
import Test.Hydra.Prelude

import Bench.Summary (Summary (..), makeQuantiles)
import CardanoClient (awaitTransaction, submitTransaction, submitTx)
import CardanoNode (RunningNode (..), withCardanoNodeDevnet)
import CardanoClient (RunningNode (..), awaitTransaction, submitTransaction, submitTx)
import CardanoNode (withCardanoNodeDevnet)
import Control.Concurrent.Class.MonadSTM (
MonadSTM (readTVarIO),
check,
Expand Down
2 changes: 0 additions & 2 deletions hydra-cluster/exe/hydra-cluster/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module Main where

import Hydra.Prelude

import CardanoClient (waitForFullySynchronized)
import CardanoNode (withCardanoNodeDevnet, withCardanoNodeOnKnownNetwork)
import Hydra.Cluster.Faucet (publishHydraScriptsAs)
import Hydra.Cluster.Fixture (Actor (Faucet))
Expand All @@ -25,7 +24,6 @@ run options =
case knownNetwork of
Just network ->
withCardanoNodeOnKnownNetwork fromCardanoNode workDir network $ \node -> do
waitForFullySynchronized fromCardanoNode node
publishOrReuseHydraScripts tracer node
>>= singlePartyHeadFullLifeCycle tracer workDir node
Nothing ->
Expand Down
30 changes: 27 additions & 3 deletions hydra-cluster/src/CardanoClient.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import Hydra.Cardano.Api hiding (Block)
import Hydra.Chain.CardanoClient

import Cardano.Api.UTxO qualified as UTxO
import Cardano.Ledger.Core (PParams)
import Cardano.Slotting.Time (RelativeTime (getRelativeTime), diffRelativeTime, toRelativeTime)
import CardanoNode (NodeLog (..), RunningNode (..))
import Data.Fixed (Centi)
import Data.Map qualified as Map
import Hydra.Cardano.Api qualified as Api
import Hydra.Chain.CardanoClient qualified as CardanoClient
import Hydra.Logging (Tracer, traceWith)

Expand Down Expand Up @@ -164,13 +166,35 @@ mkGenesisTx networkId pparams signingKey initialAmount recipients =
TxOutDatumNone
ReferenceScriptNone

data RunningNode = RunningNode
{ nodeSocket :: SocketPath
, networkId :: NetworkId
, pparams :: PParams Api.LedgerEra
}

-- Logging

data NodeLog
= MsgNodeCmdSpec Text
| MsgCLI [Text]
| MsgCLIStatus Text Text
| MsgCLIRetry Text
| MsgCLIRetryResult Text Int
| MsgNodeStarting {stateDirectory :: FilePath}
| MsgSocketIsReady FilePath
| MsgSynchronizing {percentDone :: Centi}
| MsgNodeIsReady
deriving stock (Eq, Show, Generic)
deriving anyclass (ToJSON, FromJSON)

-- | Wait until the node is fully caught up with the network. This can take a
-- while!
waitForFullySynchronized ::
Tracer IO NodeLog ->
RunningNode ->
NetworkId ->
SocketPath ->
IO ()
waitForFullySynchronized tracer RunningNode{nodeSocket, networkId} = do
waitForFullySynchronized tracer networkId nodeSocket = do
systemStart <- querySystemStart networkId nodeSocket QueryTip
check systemStart
where
Expand Down
32 changes: 6 additions & 26 deletions hydra-cluster/src/CardanoNode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ module CardanoNode where

import Hydra.Prelude

import Cardano.Ledger.Core (PParams)
import CardanoClient (NodeLog (..), RunningNode (..), waitForFullySynchronized)
import Control.Lens ((?~), (^?!))
import Control.Tracer (Tracer, traceWith)
import Data.Aeson (Value (String), (.=))
import Data.Aeson qualified as Aeson
import Data.Aeson.Lens (atKey, key, _Number)
import Data.Fixed (Centi)
import Data.Text qualified as Text
import Data.Time.Clock.POSIX (posixSecondsToUTCTime, utcTimeToPOSIXSeconds)
import Hydra.Cardano.Api (AsType (AsPaymentKey), File (..), NetworkId, PaymentKey, SigningKey, SocketPath, VerificationKey, generateSigningKey, getVerificationKey)
Expand Down Expand Up @@ -40,12 +39,6 @@ type Port = Int
newtype NodeId = NodeId Int
deriving newtype (Eq, Show, Num, ToJSON, FromJSON)

data RunningNode = RunningNode
{ nodeSocket :: SocketPath
, networkId :: NetworkId
, pparams :: PParams Api.LedgerEra
}

-- | Configuration parameters for a single node devnet
data DevnetConfig = DevnetConfig
{ stateDirectory :: FilePath
Expand Down Expand Up @@ -121,7 +114,7 @@ withCardanoNodeDevnet ::
IO a
withCardanoNodeDevnet tracer stateDirectory action = do
args <- setupCardanoDevnet stateDirectory
withCardanoNode tracer stateDirectory args $ \nodeSocket -> do
withCardanoNode tracer stateDirectory args networkId $ \nodeSocket -> do
traceWith tracer MsgNodeIsReady
pparams <- queryProtocolParameters networkId nodeSocket QueryTip
let rn =
Expand All @@ -147,7 +140,7 @@ withCardanoNodeOnKnownNetwork ::
withCardanoNodeOnKnownNetwork tracer workDir knownNetwork action = do
copyKnownNetworkFiles
networkId <- readNetworkId
withCardanoNode tracer workDir args $ \nodeSocket -> do
withCardanoNode tracer workDir args networkId $ \nodeSocket -> do
traceWith tracer MsgNodeIsReady
pparams <- queryProtocolParameters networkId nodeSocket QueryTip
let rn =
Expand Down Expand Up @@ -279,9 +272,10 @@ withCardanoNode ::
Tracer IO NodeLog ->
FilePath ->
CardanoNodeArgs ->
NetworkId ->
(SocketPath -> IO a) ->
IO a
withCardanoNode tr stateDirectory args@CardanoNodeArgs{nodeSocket} action = do
withCardanoNode tr stateDirectory args@CardanoNodeArgs{nodeSocket} networkId action = do
traceWith tr $ MsgNodeCmdSpec (show $ cmdspec process)
traceWith tr $ MsgNodeStarting{stateDirectory}
withLogFile logFilePath $ \out -> do
Expand All @@ -306,6 +300,7 @@ withCardanoNode tr stateDirectory args@CardanoNodeArgs{nodeSocket} action = do
waitForNode = do
let nodeSocketPath = File socketPath
waitForSocket nodeSocketPath
_ <- waitForFullySynchronized tr networkId nodeSocketPath
traceWith tr $ MsgSocketIsReady $ unFile nodeSocketPath
action nodeSocketPath

Expand Down Expand Up @@ -416,21 +411,6 @@ data ProcessHasExited = ProcessHasExited Text ExitCode

instance Exception ProcessHasExited

-- Logging

data NodeLog
= MsgNodeCmdSpec Text
| MsgCLI [Text]
| MsgCLIStatus Text Text
| MsgCLIRetry Text
| MsgCLIRetryResult Text Int
| MsgNodeStarting {stateDirectory :: FilePath}
| MsgSocketIsReady FilePath
| MsgSynchronizing {percentDone :: Centi}
| MsgNodeIsReady
deriving stock (Eq, Show, Generic)
deriving anyclass (ToJSON, FromJSON)

--
-- Helpers
--
Expand Down
2 changes: 1 addition & 1 deletion hydra-cluster/src/Hydra/Cluster/Faucet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Cardano.Api.UTxO qualified as UTxO
import Cardano.Ledger.Core (PParams)
import CardanoClient (
QueryPoint (QueryTip),
RunningNode (..),
SubmitTransactionException,
awaitTransaction,
buildAddress,
Expand All @@ -18,7 +19,6 @@ import CardanoClient (
submitTransaction,
waitForPayment,
)
import CardanoNode (RunningNode (..))
import Control.Exception (IOException)
import Control.Monad.Class.MonadThrow (Handler (Handler), catches)
import Control.Tracer (Tracer, traceWith)
Expand Down
3 changes: 2 additions & 1 deletion hydra-cluster/src/Hydra/Cluster/Scenarios.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import Test.Hydra.Prelude

import Cardano.Api.UTxO qualified as UTxO
import CardanoClient (
NodeLog,
QueryPoint (QueryTip),
RunningNode (..),
buildTransaction,
queryTip,
queryUTxOFor,
submitTx,
)
import CardanoNode (NodeLog, RunningNode (..))
import Control.Concurrent.Async (mapConcurrently_)
import Control.Lens ((^?))
import Data.Aeson (Value, object, (.=))
Expand Down
4 changes: 2 additions & 2 deletions hydra-cluster/test/Test/CardanoClientSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module Test.CardanoClientSpec where
import Hydra.Prelude
import Test.Hydra.Prelude

import CardanoClient (QueryPoint (..), queryGenesisParameters)
import CardanoNode (RunningNode (..), withCardanoNodeDevnet)
import CardanoClient (QueryPoint (..), RunningNode (..), queryGenesisParameters)
import CardanoNode (withCardanoNodeDevnet)
import Data.Aeson ((.:))
import Data.Aeson qualified as Aeson
import Hydra.Cardano.Api (GenesisParameters (..))
Expand Down
3 changes: 1 addition & 2 deletions hydra-cluster/test/Test/CardanoNodeSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import Hydra.Prelude
import Test.Hydra.Prelude

import CardanoNode (
RunningNode (..),
getCardanoNodeVersion,
withCardanoNodeDevnet,
)

import CardanoClient (queryTipSlotNo)
import CardanoClient (RunningNode (..), queryTipSlotNo)
import Hydra.Cardano.Api (NetworkId (Testnet), NetworkMagic (NetworkMagic), unFile)
import Hydra.Logging (showLogsOnFailure)
import System.Directory (doesFileExist)
Expand Down
4 changes: 2 additions & 2 deletions hydra-cluster/test/Test/ChainObserverSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module Test.ChainObserverSpec where
import Hydra.Prelude
import Test.Hydra.Prelude

import CardanoClient (submitTx)
import CardanoNode (NodeLog, RunningNode (..), withCardanoNodeDevnet)
import CardanoClient (NodeLog, RunningNode (..), submitTx)
import CardanoNode (withCardanoNodeDevnet)
import Control.Concurrent.Class.MonadSTM (modifyTVar', newTVarIO, readTVarIO)
import Control.Exception (IOException)
import Control.Lens ((^?))
Expand Down
4 changes: 3 additions & 1 deletion hydra-cluster/test/Test/DirectChainSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import Test.Hydra.Prelude

import Cardano.Api.UTxO (UTxO' (UTxO, toMap))
import CardanoClient (
NodeLog,
QueryPoint (QueryTip),
RunningNode (..),
buildAddress,
queryTip,
queryUTxO,
submitTx,
waitForUTxO,
)
import CardanoNode (NodeLog, RunningNode (..), withCardanoNodeDevnet)
import CardanoNode (withCardanoNodeDevnet)
import Control.Concurrent.STM (newEmptyTMVarIO, takeTMVar)
import Control.Concurrent.STM.TMVar (putTMVar)
import Hydra.Cardano.Api (
Expand Down
16 changes: 12 additions & 4 deletions hydra-cluster/test/Test/EndToEndSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ import Hydra.Prelude
import Test.Hydra.Prelude

import Cardano.Api.UTxO qualified as UTxO
import CardanoClient (QueryPoint (..), queryEpochNo, queryGenesisParameters, queryTip, queryTipSlotNo, submitTx, waitForUTxO)
import CardanoClient (
QueryPoint (..),
RunningNode (..),
queryEpochNo,
queryGenesisParameters,
queryTip,
queryTipSlotNo,
submitTx,
waitForUTxO,
)
import CardanoNode (
CardanoNodeArgs (..),
RunningNode (..),
forkIntoConwayInEpoch,
setupCardanoDevnet,
unsafeDecodeJsonFile,
Expand Down Expand Up @@ -522,7 +530,7 @@ spec = around (showLogsOnFailure "EndToEndSpec") $ do
withClusterTempDir "unsupported-era" $ \tmpDir -> do
args <- setupCardanoDevnet tmpDir
forkIntoConwayInEpoch tmpDir args 1
withCardanoNode (contramap FromCardanoNode tracer) tmpDir args $ \nodeSocket -> do
withCardanoNode (contramap FromCardanoNode tracer) tmpDir args defaultNetworkId $ \nodeSocket -> do
let pparams = defaultPParams
let node = RunningNode{nodeSocket, networkId = defaultNetworkId, pparams}
hydraScriptsTxId <- publishHydraScriptsAs node Faucet
Expand All @@ -542,7 +550,7 @@ spec = around (showLogsOnFailure "EndToEndSpec") $ do
withClusterTempDir "unsupported-era-startup" $ \tmpDir -> do
args <- setupCardanoDevnet tmpDir
forkIntoConwayInEpoch tmpDir args 1
withCardanoNode (contramap FromCardanoNode tracer) tmpDir args $ \nodeSocket -> do
withCardanoNode (contramap FromCardanoNode tracer) tmpDir args defaultNetworkId $ \nodeSocket -> do
let pparams = defaultPParams
let node = RunningNode{nodeSocket, networkId = defaultNetworkId, pparams}
hydraScriptsTxId <- publishHydraScriptsAs node Faucet
Expand Down
3 changes: 2 additions & 1 deletion hydra-cluster/test/Test/Hydra/Cluster/FaucetSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module Test.Hydra.Cluster.FaucetSpec where
import Hydra.Prelude
import Test.Hydra.Prelude

import CardanoNode (RunningNode (..), withCardanoNodeDevnet)
import CardanoClient (RunningNode (..))
import CardanoNode (withCardanoNodeDevnet)
import Control.Concurrent.Async (replicateConcurrently_)
import Hydra.Cardano.Api (AssetId (AdaAssetId), selectAsset, txOutValue)
import Hydra.Chain.CardanoClient (QueryPoint (..), queryUTxOFor)
Expand Down
3 changes: 2 additions & 1 deletion hydra-tui/test/Hydra/TUISpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import Hydra.Prelude
import Test.Hydra.Prelude

import Blaze.ByteString.Builder.Char8 (writeChar)
import CardanoNode (NodeLog, RunningNode (..), withCardanoNodeDevnet)
import CardanoClient (NodeLog, RunningNode (..))
import CardanoNode (withCardanoNodeDevnet)
import Control.Concurrent.Class.MonadSTM (newTQueueIO, readTQueue, tryReadTQueue, writeTQueue)
import Data.ByteString qualified as BS
import Graphics.Vty (
Expand Down