From f49f55b82f50966af2b0e6b828057975df08f4d4 Mon Sep 17 00:00:00 2001 From: Mateusz Galazyn Date: Tue, 30 Apr 2024 12:09:59 +0200 Subject: [PATCH] Enable epoch state logging by default --- cardano-testnet/src/Parsers/Cardano.hs | 5 +++ cardano-testnet/src/Testnet/Runtime.hs | 32 +++++++++++-------- cardano-testnet/src/Testnet/Start/Cardano.hs | 3 ++ cardano-testnet/src/Testnet/Start/Types.hs | 2 ++ .../files/golden/help.cli | 1 + .../files/golden/help/cardano.cli | 4 +++ .../Test/LedgerEvents/Gov/DRepActivity.hs | 3 +- .../Test/LedgerEvents/Gov/InfoAction.hs | 4 +-- .../LedgerEvents/Gov/TreasuryWithdrawal.hs | 4 +-- .../Test/LedgerEvents/TreasuryGrowth.hs | 5 +-- 10 files changed, 39 insertions(+), 24 deletions(-) diff --git a/cardano-testnet/src/Parsers/Cardano.hs b/cardano-testnet/src/Parsers/Cardano.hs index 391a78bc00e..0a1092bca7d 100644 --- a/cardano-testnet/src/Parsers/Cardano.hs +++ b/cardano-testnet/src/Parsers/Cardano.hs @@ -68,6 +68,11 @@ optsTestnet envCli = CardanoTestnetOptions <> OA.showDefault <> OA.value 3 ) + <*> OA.flag False True + ( OA.long "enable-new-epoch-state-logging" + <> OA.help "Enable new epoch state logging to logs/ledger-epoch-state.log" + <> OA.showDefault + ) pNumSpoNodes :: Parser [TestnetNodeOptions] pNumSpoNodes = diff --git a/cardano-testnet/src/Testnet/Runtime.hs b/cardano-testnet/src/Testnet/Runtime.hs index b50f4022daa..85e01d76502 100644 --- a/cardano-testnet/src/Testnet/Runtime.hs +++ b/cardano-testnet/src/Testnet/Runtime.hs @@ -63,7 +63,6 @@ import qualified GHC.Stack as GHC import Network.Socket (PortNumber) import Prettyprinter (unAnnotate) import qualified System.Directory as IO -import System.Directory (doesDirectoryExist) import System.FilePath import qualified System.IO as IO import qualified System.Process as IO @@ -332,8 +331,9 @@ createSubdirectoryIfMissingNew parent subdirectory = GHC.withFrozenCallStack $ d -- | Start ledger's new epoch state logging for the first node in the background. -- Logs will be placed in /logs/ledger-new-epoch-state.log -- The logging thread will be cancelled when `MonadResource` releases all resources. +-- Idempotent. startLedgerNewEpochStateLogging - :: forall m. HasCallStack + :: HasCallStack => MonadCatch m => MonadResource m => MonadTest m @@ -342,24 +342,30 @@ startLedgerNewEpochStateLogging -> m () startLedgerNewEpochStateLogging testnetRuntime tmpWorkspace = withFrozenCallStack $ do let logDir = makeLogDir (TmpAbsolutePath tmpWorkspace) + -- used as a lock to start only a single instance of epoch state logging logFile = logDir "ledger-epoch-state.log" - H.evalIO (doesDirectoryExist logDir) >>= \case + H.evalIO (IO.doesDirectoryExist logDir) >>= \case True -> pure () False -> do H.note_ $ "Log directory does not exist: " <> logDir <> " - cannot start logging epoch states" H.failure - socketPath <- H.noteM $ H.sprocketSystemName <$> H.headM (poolSprockets testnetRuntime) - _ <- runInBackground . runExceptT $ - foldEpochState - (File $ configurationFile testnetRuntime) - (Api.File socketPath) - Api.QuickValidation - (EpochNo maxBound) - () - (\epochState _ _ -> handler logFile epochState) - H.note_ $ "Started logging epoch states to to: " <> logFile + H.evalIO (IO.doesFileExist logFile) >>= \case + True -> do + H.note_ $ "Epoch states logging to " <> logFile <> " is already started." + False -> do + H.evalIO $ appendFile logFile "" + socketPath <- H.noteM $ H.sprocketSystemName <$> H.headM (poolSprockets testnetRuntime) + _ <- runInBackground . runExceptT $ + foldEpochState + (File $ configurationFile testnetRuntime) + (Api.File socketPath) + Api.QuickValidation + (EpochNo maxBound) + () + (\epochState _ _ -> handler logFile epochState) + H.note_ $ "Started logging epoch states to: " <> logFile where handler :: FilePath -> AnyNewEpochState -> StateT () IO LedgerStateCondition handler outputFp anyNewEpochState = handleException . liftIO $ do diff --git a/cardano-testnet/src/Testnet/Start/Cardano.hs b/cardano-testnet/src/Testnet/Start/Cardano.hs index d1ceb00fbf4..fd3a9d34baa 100644 --- a/cardano-testnet/src/Testnet/Start/Cardano.hs +++ b/cardano-testnet/src/Testnet/Start/Cardano.hs @@ -418,6 +418,9 @@ cardanoTestnet H.assertExpectedSposInLedgerState stakePoolsFp testnetOptions execConfig + when (cardanoEnableNewEpochStateLogging testnetOptions) $ + TR.startLedgerNewEpochStateLogging runtime tempBaseAbsPath + pure runtime where writeGenesisSpecFile :: (MonadTest m, MonadIO m, HasCallStack) => ToJSON a => String -> a -> m () diff --git a/cardano-testnet/src/Testnet/Start/Types.hs b/cardano-testnet/src/Testnet/Start/Types.hs index 4f1accf2a3b..3af0832b4eb 100644 --- a/cardano-testnet/src/Testnet/Start/Types.hs +++ b/cardano-testnet/src/Testnet/Start/Types.hs @@ -47,6 +47,7 @@ data CardanoTestnetOptions = CardanoTestnetOptions , cardanoEnableP2P :: Bool , cardanoNodeLoggingFormat :: NodeLoggingFormat , cardanoNumDReps :: Int -- ^ The number of DReps to generate at creation + , cardanoEnableNewEpochStateLogging :: Bool -- ^ if epoch state logging is enabled } deriving (Eq, Show) cardanoDefaultTestnetOptions :: CardanoTestnetOptions @@ -61,6 +62,7 @@ cardanoDefaultTestnetOptions = CardanoTestnetOptions , cardanoEnableP2P = False , cardanoNodeLoggingFormat = NodeLoggingFormatAsJson , cardanoNumDReps = 3 + , cardanoEnableNewEpochStateLogging = True } -- | Specify a BFT node (Pre-Babbage era only) or an SPO (Shelley era onwards only) diff --git a/cardano-testnet/test/cardano-testnet-golden/files/golden/help.cli b/cardano-testnet/test/cardano-testnet-golden/files/golden/help.cli index d21492d3d18..33676d05532 100644 --- a/cardano-testnet/test/cardano-testnet-golden/files/golden/help.cli +++ b/cardano-testnet/test/cardano-testnet-golden/files/golden/help.cli @@ -16,6 +16,7 @@ Usage: cardano-testnet cardano [--num-pool-nodes COUNT] [--enable-p2p BOOL] [--nodeLoggingFormat LOGGING_FORMAT] [--num-dreps NUMBER] + [--enable-new-epoch-state-logging] Start a testnet in any era diff --git a/cardano-testnet/test/cardano-testnet-golden/files/golden/help/cardano.cli b/cardano-testnet/test/cardano-testnet-golden/files/golden/help/cardano.cli index 82a44b76ab2..e9edd1f9c4d 100644 --- a/cardano-testnet/test/cardano-testnet-golden/files/golden/help/cardano.cli +++ b/cardano-testnet/test/cardano-testnet-golden/files/golden/help/cardano.cli @@ -14,6 +14,7 @@ Usage: cardano-testnet cardano [--num-pool-nodes COUNT] [--enable-p2p BOOL] [--nodeLoggingFormat LOGGING_FORMAT] [--num-dreps NUMBER] + [--enable-new-epoch-state-logging] Start a testnet in any era @@ -41,4 +42,7 @@ Available options: (default: NodeLoggingFormatAsJson) --num-dreps NUMBER Number of delegate representatives (DReps) to generate (default: 3) + --enable-new-epoch-state-logging + Enable new epoch state logging to + logs/ledger-epoch-state.log -h,--help Show this help text diff --git a/cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/LedgerEvents/Gov/DRepActivity.hs b/cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/LedgerEvents/Gov/DRepActivity.hs index b0a74c48044..afc408d55d8 100644 --- a/cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/LedgerEvents/Gov/DRepActivity.hs +++ b/cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/LedgerEvents/Gov/DRepActivity.hs @@ -38,6 +38,7 @@ import Testnet.Components.DReps (createVotingTxBody, delegateToDRep, g import Testnet.Components.Query (EpochStateView, checkDRepState, findLargestUtxoForPaymentKey, getCurrentEpochNo, getEpochStateView, getMinDRepDeposit) +import Testnet.Components.TestWatchdog import Testnet.Defaults import qualified Testnet.Process.Cli as P import qualified Testnet.Process.Run as H @@ -51,7 +52,7 @@ import qualified Hedgehog.Extras.Stock.IO.Network.Sprocket as IO -- | Execute me with: -- @DISABLE_RETRIES=1 cabal test cardano-testnet-test --test-options '-p "/DRep Activity/"'@ hprop_check_drep_activity :: Property -hprop_check_drep_activity = H.integrationWorkspace "test-activity" $ \tempAbsBasePath' -> do +hprop_check_drep_activity = H.integrationWorkspace "test-activity" $ \tempAbsBasePath' -> runWithDefaultWatchdog_ $ do -- Start a local test net conf@Conf { tempAbsPath } <- mkConf tempAbsBasePath' let tempAbsPath' = unTmpAbsPath tempAbsPath diff --git a/cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/LedgerEvents/Gov/InfoAction.hs b/cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/LedgerEvents/Gov/InfoAction.hs index 655d68e9623..567bbb40431 100644 --- a/cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/LedgerEvents/Gov/InfoAction.hs +++ b/cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/LedgerEvents/Gov/InfoAction.hs @@ -62,7 +62,7 @@ hprop_ledger_events_info_action = H.integrationRetryWorkspace 0 "info-hash" $ \t , cardanoNodeEra = AnyCardanoEra era } - testnetRuntime@TestnetRuntime + TestnetRuntime { testnetMagic , poolNodes , wallets=wallet0:wallet1:_ @@ -80,8 +80,6 @@ hprop_ledger_events_info_action = H.integrationRetryWorkspace 0 "info-hash" $ \t epochStateView <- getEpochStateView (File configurationFile) (File socketPath) - startLedgerNewEpochStateLogging testnetRuntime tempAbsPath' - H.note_ $ "Sprocket: " <> show poolSprocket1 H.note_ $ "Abs path: " <> tempAbsBasePath' H.note_ $ "Socketpath: " <> socketPath diff --git a/cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/LedgerEvents/Gov/TreasuryWithdrawal.hs b/cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/LedgerEvents/Gov/TreasuryWithdrawal.hs index 4fa99ccab96..eb63124f7eb 100644 --- a/cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/LedgerEvents/Gov/TreasuryWithdrawal.hs +++ b/cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/LedgerEvents/Gov/TreasuryWithdrawal.hs @@ -69,7 +69,7 @@ hprop_ledger_events_treasury_withdrawal = H.integrationRetryWorkspace 1 "treasu , cardanoActiveSlotsCoeff = 0.3 } - testnetRuntime@TestnetRuntime + TestnetRuntime { testnetMagic , poolNodes , wallets=wallet0:wallet1:_ @@ -92,8 +92,6 @@ hprop_ledger_events_treasury_withdrawal = H.integrationRetryWorkspace 1 "treasu H.note_ $ "Socketpath: " <> socketPath H.note_ $ "Foldblocks config file: " <> configurationFile - startLedgerNewEpochStateLogging testnetRuntime tempAbsPath' - gov <- H.createDirectoryIfMissing $ work "governance" proposalAnchorFile <- H.note $ work gov "sample-proposal-anchor" treasuryWithdrawalActionFp <- H.note $ work gov "treasury-withdrawal.action" diff --git a/cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/LedgerEvents/TreasuryGrowth.hs b/cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/LedgerEvents/TreasuryGrowth.hs index 1db19eff1ad..70a4affc582 100644 --- a/cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/LedgerEvents/TreasuryGrowth.hs +++ b/cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/LedgerEvents/TreasuryGrowth.hs @@ -45,12 +45,9 @@ prop_check_if_treasury_is_growing = H.integrationRetryWorkspace 0 "growing-treas runtime@TestnetRuntime{configurationFile} <- cardanoTestnetDefault options conf - -- uncomment for epoch state live access - -- startLedgerNewEpochStateLogging runtime tempAbsBasePath' - -- Get socketPath socketPathAbs <- do - socketPath' <- H.noteShowM $ H.sprocketArgumentName <$> H.headM (poolSprockets runtime) + socketPath' <- H.sprocketArgumentName <$> H.headM (poolSprockets runtime) H.noteIO (IO.canonicalizePath $ tempAbsPath' socketPath') (_condition, treasuryValues) <- H.leftFailM . H.evalIO . runExceptT $