|
| 1 | +module Test.Plutip.Internal.ChainIndex ( |
| 2 | + handleChainIndexLaunch, |
| 3 | +) where |
| 4 | + |
| 5 | +import Cardano.Api qualified as CAPI |
| 6 | +import Cardano.BM.Configuration.Model qualified as CM |
| 7 | +import Cardano.BM.Data.Severity qualified as Severity |
| 8 | +import Cardano.Launcher.Node (nodeSocketFile) |
| 9 | + |
| 10 | +import Control.Concurrent.Async (async) |
| 11 | +import Control.Monad (void) |
| 12 | +import Control.Retry (constantDelay, limitRetries, recoverAll) |
| 13 | +import Plutus.ChainIndex.App qualified as ChainIndex |
| 14 | +import Plutus.ChainIndex.Config qualified as ChainIndex |
| 15 | +import Plutus.ChainIndex.Logging (defaultConfig) |
| 16 | +import Servant.Client (BaseUrl (BaseUrl), Scheme (Http), mkClientEnv, runClientM) |
| 17 | +import System.FilePath ((</>)) |
| 18 | +import Test.Plutip.Config ( |
| 19 | + ChainIndexMode (CustomPort, DefaultPort, NotNeeded), |
| 20 | + ) |
| 21 | +import Test.Plutip.Internal.Types ( |
| 22 | + RunningNode (RunningNode), |
| 23 | + ) |
| 24 | +import UnliftIO.Exception (throwString) |
| 25 | + |
| 26 | +import Cardano.Wallet.Primitive.Types ( |
| 27 | + NetworkParameters (NetworkParameters), |
| 28 | + SlotLength (SlotLength), |
| 29 | + SlottingParameters (SlottingParameters), |
| 30 | + ) |
| 31 | +import Data.Default (Default (def)) |
| 32 | +import Data.Function ((&)) |
| 33 | +import Data.Time (nominalDiffTimeToSeconds) |
| 34 | +import Ledger (Slot (Slot)) |
| 35 | +import Ledger.TimeSlot (SlotConfig (scSlotLength)) |
| 36 | +import Network.HTTP.Client (defaultManagerSettings, newManager) |
| 37 | +import Plutus.ChainIndex (Tip (Tip)) |
| 38 | +import Plutus.ChainIndex.Client qualified as ChainIndexClient |
| 39 | +import Plutus.ChainIndex.Config qualified as CIC |
| 40 | +import PlutusPrelude ((.~), (^.)) |
| 41 | + |
| 42 | +type ChainIndexPort = Int |
| 43 | + |
| 44 | +-- | Handle launch of `chain-index`. Depending on `ChainIndexMode`, it can be |
| 45 | +-- launched on default port, on custom port or not launched at all. |
| 46 | +-- In the latter case no port will be returned. |
| 47 | +handleChainIndexLaunch :: |
| 48 | + ChainIndexMode -> |
| 49 | + RunningNode -> |
| 50 | + FilePath -> |
| 51 | + IO (Maybe ChainIndexPort) |
| 52 | +handleChainIndexLaunch mode rn dir = do |
| 53 | + maybePort <- |
| 54 | + case mode of |
| 55 | + DefaultPort -> do |
| 56 | + Just <$> launchChainIndex (CIC.cicPort ChainIndex.defaultConfig) rn dir |
| 57 | + CustomPort port' -> do |
| 58 | + Just <$> launchChainIndex (fromEnum port') rn dir |
| 59 | + NotNeeded -> pure Nothing |
| 60 | + reportLaunch maybePort |
| 61 | + pure maybePort |
| 62 | + where |
| 63 | + reportLaunch = \case |
| 64 | + Just p -> putStrLn $ "Chain index started at port " <> show p |
| 65 | + _ -> pure () |
| 66 | + |
| 67 | +-- | Launch the chain index in a separate thread. |
| 68 | +launchChainIndex :: Int -> RunningNode -> FilePath -> IO Int |
| 69 | +launchChainIndex port (RunningNode sp _block0 (netParams, _vData) _) dir = do |
| 70 | + let (NetworkParameters _ (SlottingParameters (SlotLength slotLen) _ _ _) _) = netParams |
| 71 | + |
| 72 | + config <- defaultConfig |
| 73 | + CM.setMinSeverity config Severity.Notice |
| 74 | + let dbPath = dir </> "chain-index.db" |
| 75 | + chainIndexConfig = |
| 76 | + CIC.defaultConfig |
| 77 | + & CIC.socketPath .~ nodeSocketFile sp |
| 78 | + & CIC.dbPath .~ dbPath |
| 79 | + & CIC.networkId .~ CAPI.Mainnet |
| 80 | + & CIC.port .~ port |
| 81 | + & CIC.slotConfig .~ (def {scSlotLength = toMilliseconds slotLen}) |
| 82 | + |
| 83 | + void $ async $ void $ ChainIndex.runMainWithLog (const $ return ()) config chainIndexConfig |
| 84 | + waitForChainIndex |
| 85 | + return $ chainIndexConfig ^. CIC.port |
| 86 | + where |
| 87 | + toMilliseconds = floor . (1e3 *) . nominalDiffTimeToSeconds |
| 88 | + |
| 89 | + waitForChainIndex = do |
| 90 | + -- TODO: move this to config; ideally, separate chain-index launch from cluster launch |
| 91 | + let policy = constantDelay 1_000_000 <> limitRetries 60 |
| 92 | + recoverAll policy $ \_ -> do |
| 93 | + tip <- queryTipWithChIndex |
| 94 | + case tip of |
| 95 | + Right (Tip (Slot _) _ _) -> pure () |
| 96 | + a -> |
| 97 | + throwString $ |
| 98 | + "Timeout waiting for chain-index to start indexing. Last response:\n" |
| 99 | + <> either show show a |
| 100 | + |
| 101 | + queryTipWithChIndex = do |
| 102 | + manager' <- newManager defaultManagerSettings |
| 103 | + runClientM ChainIndexClient.getTip $ mkClientEnv manager' (BaseUrl Http "localhost" port "") |
0 commit comments