Skip to content

Commit

Permalink
Use absolute files in local-cluster functions
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Apr 16, 2024
1 parent 54ce4f1 commit 49e7668
Show file tree
Hide file tree
Showing 33 changed files with 716 additions and 471 deletions.
7 changes: 3 additions & 4 deletions .gitignore
Expand Up @@ -75,7 +75,6 @@ test-output

## Membench uncompressed data
lib/wallet-benchmarks/data/membench-snapshot
babbage-integration-tests-output/test.log
babbage-integration-tests-output/wallet.log
lib/integration/babbage-integration-tests-output/test.log
lib/integration/babbage-integration-tests-output/wallet.log

## Integration tests output
babbage-integration-tests-output
2 changes: 2 additions & 0 deletions lib/benchmarks/cardano-wallet-benchmarks.cabal
Expand Up @@ -143,6 +143,8 @@ benchmark latency
, local-cluster
, mtl
, optparse-applicative
, path
, path-io
, resourcet
, servant-client
, temporary-extra
Expand Down
36 changes: 22 additions & 14 deletions lib/benchmarks/exe/latency-bench.hs
Expand Up @@ -6,6 +6,7 @@
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
Expand Down Expand Up @@ -85,7 +86,6 @@ import Cardano.Wallet.Faucet
import Cardano.Wallet.Launch.Cluster
( Config (..)
, FaucetFunds (..)
, FileOf (..)
, RunningNode (..)
, defaultPoolConfigs
, testnetMagicToNatural
Expand All @@ -95,8 +95,9 @@ import Cardano.Wallet.Launch.Cluster
import Cardano.Wallet.Launch.Cluster.CommandLine
( clusterConfigsDirParser
)
import Cardano.Wallet.Launch.Cluster.Config
( NodePathSegment (..)
import Cardano.Wallet.Launch.Cluster.FileOf
( DirOf
, mkAbsolutize
)
import Cardano.Wallet.Network.Implementation.Ouroboros
( tunedForMainnetPipeliningStrategy
Expand Down Expand Up @@ -193,6 +194,13 @@ import Network.Wai.Middleware.Logging
import Numeric.Natural
( Natural
)
import Path
( Abs
, fromAbsDir
, parseAbsDir
, reldir
, (</>)
)
import Servant.Client
( ClientError
, ClientM
Expand All @@ -203,9 +211,6 @@ import System.Directory
import System.Environment.Extended
( isEnvSet
)
import System.FilePath
( (</>)
)
import System.IO.Temp.Extra
( SkipCleanup (..)
, withSystemTempDir
Expand Down Expand Up @@ -642,8 +647,9 @@ withShelleyServer tracers action = withFaucet $ \faucetClientEnv -> do
withServer cfgTestnetMagic faucetFunds setupAction = do
skipCleanup <- SkipCleanup <$> isEnvSet "NO_CLEANUP"
withSystemTempDir stdoutTextTracer "latency" skipCleanup $ \dir -> do
let db = dir </> "wallets"
createDirectory db
dirPath <- parseAbsDir dir
let db = dirPath </> [reldir|wallets|]
createDirectory $ fromAbsDir db
CommandLineOptions{clusterConfigsDir} <- parseCommandLineOptions
clusterEra <- Cluster.clusterEraFromEnv
cfgNodeLogging <-
Expand All @@ -654,7 +660,7 @@ withShelleyServer tracers action = withFaucet $ \faucetClientEnv -> do
{ cfgStakePools = pure (NE.head defaultPoolConfigs)
, cfgLastHardFork = clusterEra
, cfgNodeLogging
, cfgClusterDir = FileOf @"cluster" dir
, cfgClusterDir = dirPath
, cfgClusterConfigs = clusterConfigsDir
, cfgTestnetMagic
, cfgShelleyGenesisMods =
Expand All @@ -666,7 +672,7 @@ withShelleyServer tracers action = withFaucet $ \faucetClientEnv -> do
]
, cfgTracer = stdoutTextTracer
, cfgNodeOutputFile = Nothing
, cfgRelayNodePath = NodePathSegment "relay"
, cfgRelayNodePath = [reldir|relay|]
, cfgClusterLogFile = Nothing
}
withCluster
Expand All @@ -685,7 +691,7 @@ withShelleyServer tracers action = withFaucet $ \faucetClientEnv -> do
(NTestnet . fromIntegral $ testnetMagicToNatural testnetMagic)
[] -- pool certificates
tracers
(Just db)
(Just $ fromAbsDir db)
Nothing -- db decorator
"127.0.0.1"
(ListenOnPort 8_090)
Expand All @@ -710,12 +716,14 @@ era = maxBound
-- Command line options --------------------------------------------------------

newtype CommandLineOptions = CommandLineOptions
{clusterConfigsDir :: FileOf "cluster-configs"}
{clusterConfigsDir :: DirOf "cluster-configs" Abs}
deriving stock (Show)

parseCommandLineOptions :: IO CommandLineOptions
parseCommandLineOptions =
parseCommandLineOptions = do
absolutize <- mkAbsolutize
O.execParser
$ O.info
(fmap CommandLineOptions clusterConfigsDirParser <**> O.helper)
(fmap CommandLineOptions (clusterConfigsDirParser absolutize)
<**> O.helper)
(O.progDesc "Cardano Wallet's Latency Benchmark")
4 changes: 4 additions & 0 deletions lib/integration/cardano-wallet-integration.cabal
Expand Up @@ -89,6 +89,8 @@ library framework
, microstache
, monad-loops
, mtl
, path
, path-io
, network-uri
, resourcet
, retry
Expand Down Expand Up @@ -157,6 +159,8 @@ library scenarios
, lens-aeson
, local-cluster
, memory
, path
, path-io
, pretty-simple
, resourcet
, servant-client
Expand Down
@@ -1,9 +1,11 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
{-# OPTIONS_GHC -Wno-redundant-constraints #-}
{-# LANGUAGE ScopedTypeVariables #-}

{-# OPTIONS_GHC -Wno-redundant-constraints #-}

{-
Control the launching of a cluster of nodes for testing purposes.
The cluster will run on the testnet network.
Expand Down Expand Up @@ -33,10 +35,9 @@ import Cardano.Wallet.Launch.Cluster
( ClusterEra
, ClusterLog (..)
, FaucetFunds
, FileOf (..)
, FileOf
, RunningNode
, clusterEraToString
, pathOf
)
import Cardano.Wallet.Launch.Cluster.CommandLine
( CommandLineOptions (..)
Expand All @@ -45,7 +46,9 @@ import Cardano.Wallet.Launch.Cluster.CommandLine
)
import Cardano.Wallet.Launch.Cluster.Config
( Config (..)
, NodePathSegment (pathOfNodePathSegment)
)
import Cardano.Wallet.Launch.Cluster.FileOf
( DirOf
)
import Cardano.Wallet.Launch.Cluster.Monitoring.Http.Client
( Query (..)
Expand Down Expand Up @@ -92,12 +95,17 @@ import Ouroboros.Network.Magic
import Ouroboros.Network.NodeToClient
( NodeToClientVersionData (..)
)
import Path
( Abs
, fromAbsDir
, fromAbsFile
, parseAbsFile
, relfile
, (</>)
)
import System.Environment
( getEnvironment
)
import System.FilePath
( (</>)
)
import System.IO.Extra
( IOMode (..)
, withFile
Expand Down Expand Up @@ -129,34 +137,33 @@ withLocalClusterProcess CommandLineOptions{..} cfgTracer era = do
]
output <- case clusterLogs of
Nothing -> pure Inherit
Just (FileOf logFile) ->
Just logFile ->
fmap UseHandle
$ ContT
$ withFile logFile WriteMode
$ withFile (fromAbsFile logFile) WriteMode

void
$ ContT
$ withBackendCreateProcess
(MsgLauncher "local-cluster" >$< cfgTracer)
$ (proc "local-cluster" args)
{ env = Just $ myEnv ++ envs
, -- , cwd = Just $ nodeDir cfg
std_out = output
, std_out = output
, std_err = output
}
where
args =
renderControl clusterControl
<> [ "--cluster-configs"
, pathOf clusterConfigsDir
, fromAbsDir clusterConfigsDir
, "--faucet-funds"
, pathOf faucetFundsFile
, fromAbsFile faucetFundsFile
]
<> case clusterDir of
Nothing -> []
Just clusterDir' ->
[ "--cluster"
, pathOf clusterDir'
, fromAbsDir clusterDir'
]
<> case monitoring of
Nothing -> []
Expand All @@ -168,24 +175,25 @@ withLocalClusterProcess CommandLineOptions{..} cfgTracer era = do
withFaucetFunds
:: HasCallStack
=> FaucetFunds
-> ContT r IO (FileOf s)
-> ContT r IO (FileOf s Abs)
withFaucetFunds faucetFunds = ContT $ \action ->
withTempFile $ \faucetFundsPath -> do
saveFunds faucetFundsPath faucetFunds
action $ FileOf faucetFundsPath
withTempFile $ \faucetFile -> do
faucetFilePath <- parseAbsFile faucetFile
saveFunds faucetFilePath faucetFunds
action faucetFilePath

withSocketPath
:: HasCallStack
=> FileOf s
=> DirOf s Abs
-> ContT r m CardanoNodeConn
withSocketPath cfgClusterDir = ContT $ \f ->
case cardanoNodeConn $ nodeSocketPath $ pathOf cfgClusterDir of
case cardanoNodeConn $ nodeSocketPath $ fromAbsDir cfgClusterDir of
Left err -> error $ "Failed to get socket path: " ++ err
Right socketPath -> f socketPath

withGenesisData :: FromJSON a => FilePath -> ContT r IO a
withGenesisData :: FromJSON a => FileOf "genesis-data" Abs -> ContT r IO a
withGenesisData shelleyGenesis = ContT $ \f -> do
genesisContent <- BS.readFile shelleyGenesis
genesisContent <- BS.readFile (fromAbsFile shelleyGenesis)

eGenesisData <- try $ Aeson.throwDecodeStrict genesisContent
case eGenesisData of
Expand Down Expand Up @@ -218,18 +226,16 @@ withLocalCluster
action = do
let
clusterConfigsDir = cfgClusterConfigs
relayDir =
pathOf cfgClusterDir
</> pathOfNodePathSegment cfgRelayNodePath
shelleyGenesis = pathOf cfgClusterDir </> "shelley-genesis.json"
relayDir = cfgClusterDir </> cfgRelayNodePath
shelleyGenesis = cfgClusterDir </> [relfile|shelley-genesis.json|]
clusterDir = Just cfgClusterDir
clusterLogs = cfgClusterLogFile
clusterControl = Nothing
evalContT $ do
(monitoring, RunQuery queryMonitor) <-
withHttpMonitoring $ MsgHttpMonitoring >$< cfgTracer
faucetFundsFile <- withFaucetFunds faucetFunds
socketPath <- withSocketPath $ FileOf relayDir
socketPath <- withSocketPath relayDir
withLocalClusterProcess
CommandLineOptions{monitoring = Just monitoring, ..}
cfgTracer
Expand Down

0 comments on commit 49e7668

Please sign in to comment.