Skip to content

Commit

Permalink
wait chain-index
Browse files Browse the repository at this point in the history
- wait till chain-index start indexing after node start
- better timeout error messages
  • Loading branch information
mikekeke committed Aug 25, 2022
1 parent c3d4693 commit a6c487f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions local-cluster/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ main = do
workingDir = maybe Temporary (`Fixed` False) workDir
plutipConfig = def {clusterWorkingDir = workingDir}

putStrLn "Starting cluster..."
(st, _) <- startCluster plutipConfig $ do
ws <- initWallets numWallets numUtxos amt dirWallets
waitSeconds 2 -- let wallet Tx finish, it can take more time with bigger slot length
Expand Down
32 changes: 26 additions & 6 deletions src/Test/Plutip/Internal/LocalCluster.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import Paths_plutip (getDataFileName)
import Plutus.ChainIndex.App qualified as ChainIndex
import Plutus.ChainIndex.Config qualified as ChainIndex
import Plutus.ChainIndex.Logging (defaultConfig)
import Servant.Client (BaseUrl (BaseUrl), Scheme (Http))
import Servant.Client (BaseUrl (BaseUrl), Scheme (Http), mkClientEnv, runClientM)
import System.Directory (canonicalizePath, copyFile, createDirectoryIfMissing, doesPathExist, findExecutable, removeDirectoryRecursive)
import System.Environment (setEnv)
import System.Exit (die)
Expand Down Expand Up @@ -70,7 +70,7 @@ import Test.Plutip.Internal.Types (
import Test.Plutip.Tools.CardanoApi qualified as Tools
import Text.Printf (printf)
import UnliftIO.Concurrent (forkFinally, myThreadId, throwTo)
import UnliftIO.Exception (bracket, catchIO, finally)
import UnliftIO.Exception (bracket, catchIO, finally, throwString)
import UnliftIO.STM (TVar, atomically, newTVarIO, readTVar, retrySTM, writeTVar)

import Cardano.Wallet.Primitive.Types (
Expand All @@ -81,7 +81,11 @@ import Cardano.Wallet.Primitive.Types (
import Data.Default (Default (def))
import Data.Function ((&))
import Data.Time (nominalDiffTimeToSeconds)
import Ledger (Slot (Slot))
import Ledger.TimeSlot (SlotConfig (scSlotLength))
import Network.HTTP.Client (defaultManagerSettings, newManager)
import Plutus.ChainIndex (Tip (Tip))
import Plutus.ChainIndex.Client qualified as ChainIndexClient
import Plutus.ChainIndex.Config qualified as CIC
import PlutusPrelude ((.~), (^.))

Expand Down Expand Up @@ -145,7 +149,7 @@ withPlutusInterface conf action = do
runActionWthSetup rn dir trCluster userActon = do
let tracer' = trMessageText trCluster
waitForRelayNode tracer' rn
-- launch chain index in seperate thread, logs to stdout
-- launch chain index in separate thread
ciPort <- launchChainIndex conf rn dir
traceWith tracer' (ChaiIndexStartedAt ciPort)
let cEnv =
Expand Down Expand Up @@ -250,8 +254,10 @@ waitForRelayNode trCluster rn =
getTip = trace >> Tools.queryTip rn
trace = traceWith trCluster WaitingRelayNode
wait _ = do
-- give some time for setup
(ChainTip (SlotNo ((> 5) -> True)) _ _) <- getTip
tip <- getTip
case tip of
(ChainTip (SlotNo _) _ _) -> pure ()
_ -> throwString "Timeout waiting for node to start"
pure ()

-- | Launch the chain index in a separate thread.
Expand All @@ -262,19 +268,33 @@ launchChainIndex conf (RunningNode sp _block0 (netParams, _vData) _) dir = do
config <- defaultConfig
CM.setMinSeverity config Severity.Notice
let dbPath = dir </> "chain-index.db"
port = maybe (CIC.cicPort ChainIndex.defaultConfig) fromEnum (chainIndexPort conf)
chainIndexConfig =
CIC.defaultConfig
& CIC.socketPath .~ nodeSocketFile sp
& CIC.socketPath .~ (nodeSocketFile sp ++ "lol")
& CIC.dbPath .~ dbPath
& CIC.networkId .~ CAPI.Mainnet
& CIC.port .~ maybe (CIC.cicPort ChainIndex.defaultConfig) fromEnum (chainIndexPort conf)
& CIC.slotConfig .~ (def {scSlotLength = toMilliseconds slotLen})

void . async $ void $ ChainIndex.runMainWithLog (const $ return ()) config chainIndexConfig
waitForChainIndex port
return $ chainIndexConfig ^. CIC.port
where
toMilliseconds = floor . (1e3 *) . nominalDiffTimeToSeconds

waitForChainIndex port = do
let policy = constantDelay 500000 <> limitRetries 50
recoverAll policy $ \_ -> do
tip <- queryTipWithChIndex port
case tip of
Right (Tip (Slot _) _ _) -> pure ()
_ -> throwString "Timeout waiting for chain-index to start"

queryTipWithChIndex port = do
manager' <- newManager defaultManagerSettings
runClientM ChainIndexClient.getTip $ mkClientEnv manager' (BaseUrl Http "localhost" port "")

handleLogs :: HasCallStack => FilePath -> PlutipConfig -> IO ()
handleLogs clusterDir conf =
copyRelayLog `catchIO` (error . printf "Failed to save relay node log: %s" . show)
Expand Down

0 comments on commit a6c487f

Please sign in to comment.