Skip to content

Commit 1333cdc

Browse files
committed
Review changes
1 parent b8c8b53 commit 1333cdc

File tree

7 files changed

+62
-50
lines changed

7 files changed

+62
-50
lines changed

cardano-testnet/src/Testnet/Components/Query.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import Cardano.Api as Api hiding (txId)
4747
import Cardano.Api.Ledger (Credential, DRepState, EpochInterval (..), KeyRole (DRepRole))
4848
import qualified Cardano.Api.Ledger as L
4949
import qualified Cardano.Api.UTxO as Utxo
50-
50+
import Testnet.Runtime
5151
import Cardano.Ledger.Api (ConwayGovState)
5252
import qualified Cardano.Ledger.Api as L
5353
import qualified Cardano.Ledger.Conway.Governance as L
@@ -253,13 +253,12 @@ getEpochStateView
253253
:: HasCallStack
254254
=> MonadResource m
255255
=> MonadTest m
256-
=> MonadCatch m
257256
=> NodeConfigFile In -- ^ node Yaml configuration file path
258257
-> SocketPath -- ^ node socket path
259258
-> m EpochStateView
260259
getEpochStateView nodeConfigFile socketPath = withFrozenCallStack $ do
261260
epochStateView <- H.evalIO $ newIORef Nothing
262-
H.asyncRegister_ . runExceptT . foldEpochState nodeConfigFile socketPath QuickValidation (EpochNo maxBound) Nothing
261+
void . asyncRegister_ . runExceptT . foldEpochState nodeConfigFile socketPath QuickValidation (EpochNo maxBound) Nothing
263262
$ \epochState slotNumber blockNumber -> do
264263
liftIO . writeIORef epochStateView $ Just (epochState, slotNumber, blockNumber)
265264
pure ConditionNotMet

cardano-testnet/src/Testnet/Orphans.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module Testnet.Orphans () where
44

5-
import RIO (RIO(..), liftIO)
5+
import RIO (RIO(..), throwString)
66

77
instance MonadFail (RIO env) where
8-
fail = liftIO . fail
8+
fail = throwString

cardano-testnet/src/Testnet/Process/RunIO.hs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
{-# LANGUAGE LambdaCase #-}
3+
{-# LANGUAGE NamedFieldPuns #-}
24
{-# LANGUAGE OverloadedStrings #-}
35
{-# LANGUAGE ScopedTypeVariables #-}
46
{-# LANGUAGE TypeApplications #-}
@@ -44,6 +46,7 @@ defaultExecConfig = ExecConfig
4446

4547

4648
mkExecConfig :: ()
49+
=> HasCallStack
4750
=> MonadIO m
4851
=> FilePath
4952
-> IO.Sprocket
@@ -65,7 +68,8 @@ mkExecConfig tempBaseAbsPath sprocket networkId = do
6568

6669

6770
execCli'
68-
:: MonadIO m
71+
:: HasCallStack
72+
=> MonadIO m
6973
=> ExecConfig
7074
-> [String]
7175
-> m String
@@ -94,14 +98,16 @@ execCli = GHC.withFrozenCallStack $ execFlex "cardano-cli" "CARDANO_CLI"
9498
-- When running outside a nix environment, the `pkgBin` describes the name of the binary
9599
-- to launch via cabal exec.
96100
execFlex
97-
:: String
101+
:: HasCallStack
102+
=> String
98103
-> String
99104
-> [String]
100105
-> RIO env String
101106
execFlex = execFlex' defaultExecConfig
102107

103108
execFlex'
104109
:: MonadIO m
110+
=> HasCallStack
105111
=> ExecConfig
106112
-> String
107113
-> String
@@ -215,8 +221,7 @@ exeSuffix = if OS.isWin32 then ".exe" else ""
215221
-- executable has been built.
216222
-- Throws an exception on failure.
217223
binDist
218-
:: HasCallStack
219-
=> MonadIO m
224+
:: (HasCallStack, MonadIO m)
220225
=> String
221226
-- ^ Package name
222227
-> String
@@ -233,20 +238,25 @@ binDist pkg binaryEnv = do
233238
<> "\" if you are working with sources. Otherwise define "
234239
<> binaryEnv
235240
<> " and have it point to the executable you want."
236-
contents <- liftIOAnnotated $ LBS.readFile planJsonFile
237-
238-
case eitherDecode contents of
239-
Right plan -> case L.filter matching (plan & installPlan) of
240-
(component:_) -> case component & binFile of
241-
Just bin -> return $ addExeSuffix (T.unpack bin)
242-
Nothing -> error $ "missing \"bin-file\" key in plan component: " <> show component <> " in the plan in: " <> planJsonFile
243-
[] -> error $ "Cannot find \"component-name\" key with the value \"exe:" <> pkg <> "\" in the plan in: " <> planJsonFile
244-
Left message -> error $ "Cannot decode plan in " <> planJsonFile <> ": " <> message
245-
where matching :: Component -> Bool
246-
matching component = case componentName component of
247-
Just name -> name == "exe:" <> T.pack pkg
248-
Nothing -> False
249241

242+
Plan{installPlan} <- eitherDecode <$> liftIOAnnotated (LBS.readFile planJsonFile)
243+
>>= \case
244+
Left message -> error $ "Cannot decode plan in " <> planJsonFile <> ": " <> message
245+
Right plan -> pure plan
246+
247+
let componentName = "exe:" <> fromString pkg
248+
case findComponent componentName installPlan of
249+
Just Component{binFile=Just binFilePath} -> pure . addExeSuffix $ T.unpack binFilePath
250+
Just component@Component{binFile=Nothing} ->
251+
error $ "missing \"bin-file\" key in plan component: " <> show component <> " in the plan in: " <> planJsonFile
252+
Nothing ->
253+
error $ "Cannot find \"component-name\" key with the value \"exe:" <> pkg <> "\" in the plan in: " <> planJsonFile
254+
where
255+
findComponent :: Text -> [Component] -> Maybe Component
256+
findComponent _ [] = Nothing
257+
findComponent needle (c@Component{componentName, components}:topLevelComponents)
258+
| componentName == Just needle = Just c
259+
| otherwise = findComponent needle topLevelComponents <|> findComponent needle components
250260

251261

252262
procNode
@@ -278,7 +288,7 @@ procFlex
278288
-- ^ Captured stdout
279289
procFlex = procFlex' defaultExecConfig
280290

281-
291+
-- This will also catch async exceptions as well.
282292
liftIOAnnotated :: (HasCallStack, MonadIO m) => IO a -> m a
283293
liftIOAnnotated action = GHC.withFrozenCallStack $
284-
liftIO $ action `catch` (\(e :: SomeException) -> throwM $ exceptionWithCallStack e)
294+
liftIOAnnotated $ action `catch` (\(e :: SomeException) -> throwM $ exceptionWithCallStack e)

cardano-testnet/src/Testnet/Runtime.hs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ startNode tp node ipv4 port _testnetMagic nodeCmd = GHC.withFrozenCallStack $ do
156156

157157
isClosed <- liftIOAnnotated $ Ping.waitForPortClosed 30 0.1 port
158158
unless isClosed $
159-
throwString $ "Port is still in use after 30 seconds before starting node: " <> show port
159+
throwString $ "Port is still in use after 30 seconds before starting node: " <> show port
160160

161161
(Just stdIn, _, _, hProcess, _)
162162
<- firstExceptT ProcessRelatedFailure $ initiateProcess
@@ -278,7 +278,6 @@ createSubdirectoryIfMissingNew parent subdirectory = GHC.withFrozenCallStack $ d
278278
-- Idempotent.
279279
startLedgerNewEpochStateLogging
280280
:: HasCallStack
281-
=> MonadCatch m
282281
=> MonadResource m
283282
=> TestnetRuntime
284283
-> FilePath -- ^ tmp workspace directory
@@ -294,23 +293,24 @@ startLedgerNewEpochStateLogging testnetRuntime tmpWorkspace = withFrozenCallStac
294293
False -> do
295294
throwString $ "Log directory does not exist: " <> logDir <> " - cannot start logging epoch states"
296295

297-
liftIOAnnotated $ IO.doesFileExist logFile >>= \case
296+
liftIOAnnotated (IO.doesFileExist logFile) >>= \case
298297
True -> return ()
299-
False -> liftIO $ appendFile logFile ""
300-
301-
let socketPath = case uncons (testnetSprockets testnetRuntime) of
302-
Just (sprocket, _) -> H.sprocketSystemName sprocket
303-
Nothing -> throwString "No testnet sprocket available"
304-
305-
let act = runExceptT $
306-
foldEpochState
307-
(configurationFile testnetRuntime)
308-
(Api.File socketPath)
309-
Api.QuickValidation
310-
(EpochNo maxBound)
311-
Nothing
312-
(handler logFile diffFile)
313-
void $ asyncRegister_ act
298+
False -> do
299+
liftIOAnnotated $ appendFile logFile ""
300+
301+
let socketPath = case uncons (testnetSprockets testnetRuntime) of
302+
Just (sprocket, _) -> H.sprocketSystemName sprocket
303+
Nothing -> throwString "No testnet sprocket available"
304+
305+
void $ asyncRegister_ . runExceptT $
306+
foldEpochState
307+
(configurationFile testnetRuntime)
308+
(Api.File socketPath)
309+
Api.QuickValidation
310+
(EpochNo maxBound)
311+
Nothing
312+
(handler logFile diffFile)
313+
314314
where
315315
handler :: FilePath -- ^ log file
316316
-> FilePath -- ^ diff file

cardano-testnet/src/Testnet/Start/Cardano.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ import Testnet.Orphans ()
8080
import RIO.Orphans (ResourceMap)
8181
import UnliftIO.Async
8282

83+
8384
-- | There are certain conditions that need to be met in order to run
8485
-- a valid node cluster.
8586
testMinimumConfigurationRequirements :: ()
@@ -90,7 +91,7 @@ testMinimumConfigurationRequirements options = withFrozenCallStack $ do
9091
when (cardanoNumPools options < 1) $ do
9192
throwString "Need at least one SPO node to produce blocks, but got none."
9293

93-
liftToIntegration :: RIO ResourceMap a -> H.Integration a
94+
liftToIntegration :: HasCallStack => RIO ResourceMap a -> H.Integration a
9495
liftToIntegration r = do
9596
rMap <- lift $ lift getInternalState
9697
liftIOAnnotated $ runRIO rMap r

cardano-testnet/src/Testnet/Start/Types.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ module Testnet.Start.Types
4242
, NodeConfigurationYaml
4343
, mkConf
4444
, mkConfigAbs
45-
, mkConfig
4645
) where
4746

4847
import Cardano.Api hiding (cardanoEra)
@@ -52,6 +51,7 @@ import Cardano.Ledger.Conway.Genesis (ConwayGenesis)
5251
import Prelude
5352

5453
import Control.Exception (throw)
54+
import Control.Monad (unless)
5555
import qualified Data.Aeson as Aeson
5656
import Data.Aeson.Types (parseFail)
5757
import Data.Char (toLower)
@@ -288,7 +288,8 @@ data Conf = Conf
288288
, updateTimestamps :: UpdateTimestamps
289289
} deriving (Eq, Show)
290290

291-
-- Logs the argument in the test.
291+
-- | Same as mkConfig except that it renders the path
292+
-- when failing in a property test.
292293
mkConf :: (HasCallStack, MonadTest m) => FilePath -> m Conf
293294
mkConf tempAbsPath' = withFrozenCallStack $ do
294295
H.note_ tempAbsPath'
@@ -304,17 +305,16 @@ mkConfig tempAbsPath' =
304305
, updateTimestamps = DontUpdateTimestamps
305306
}
306307

308+
-- | Create a 'Conf' from an absolute path, with Genesis Hashes enabled
309+
-- and updating time stamps disabled.
307310
mkConfigAbs :: FilePath -> IO Conf
308311
mkConfigAbs userOutputDir = do
309312
absUserOutputDir <- makeAbsolute userOutputDir
310313
dirExists <- doesDirectoryExist absUserOutputDir
311314
let conf = mkConfig absUserOutputDir
312-
if dirExists then
313-
-- Happens when the environment has previously been created by the user
314-
return conf
315-
else do
315+
unless dirExists $
316316
createDirectory absUserOutputDir
317-
return conf
317+
pure conf
318318

319319
-- | @anyEraToString (AnyCardanoEra ByronEra)@ returns @"byron"@
320320
anyEraToString :: AnyCardanoEra -> String

cardano-testnet/test/cardano-testnet-test/cardano-testnet-test.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import qualified Cardano.Testnet.Test.Cli.Plutus.Scripts
1212
import qualified Cardano.Testnet.Test.Cli.Query
1313
import qualified Cardano.Testnet.Test.Cli.QuerySlotNumber
1414
import qualified Cardano.Testnet.Test.Cli.StakeSnapshot
15+
import qualified Cardano.Testnet.Test.SanityCheck
1516
import qualified Cardano.Testnet.Test.Cli.Transaction
1617
import qualified Cardano.Testnet.Test.Cli.Transaction.RegisterDeregisterStakeAddress
1718
import qualified Cardano.Testnet.Test.DumpConfig
@@ -56,6 +57,7 @@ tests = do
5657
[ T.testGroup "Spec"
5758
[ T.testGroup "Ledger Events"
5859
[ ignoreOnWindows "Sanity Check" LedgerEvents.hprop_ledger_events_sanity_check
60+
, ignoreOnWindows "Async Register" Cardano.Testnet.Test.SanityCheck.hprop_asyncRegister_sanity_check
5961
-- FIXME this tests gets stuck - investigate why
6062
-- , ignoreOnWindows "Treasury Growth" Gov.prop_check_if_treasury_is_growing
6163
-- TODO: Replace foldBlocks with checkConditionResult

0 commit comments

Comments
 (0)