Skip to content

Commit

Permalink
Replace patch with function that waits
Browse files Browse the repository at this point in the history
  • Loading branch information
palas committed Aug 1, 2024
1 parent daf6a7a commit 29ffe92
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
1 change: 0 additions & 1 deletion cardano-testnet/cardano-testnet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ test-suite cardano-testnet-test
, containers
, directory
, exceptions
, extra
, filepath
, hedgehog
, hedgehog-extras
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ module Cardano.Testnet.Test.Cli.Query
) where

import Cardano.Api
import Cardano.Api.Ledger (Coin (Coin), EpochInterval (EpochInterval), extractHash)
import qualified Cardano.Api.Genesis as Api
import Cardano.Api.Ledger (Coin (Coin), EpochInterval (EpochInterval), StandardCrypto,
extractHash, unboundRational)
import Cardano.Api.Shelley (StakeCredential (StakeCredentialByKey), StakePoolKey)

import Cardano.CLI.Types.Key (VerificationKeyOrFile (VerificationKeyFilePath),
Expand All @@ -22,27 +24,27 @@ import Cardano.CLI.Types.Output (QueryTipLocalStateOutput)
import Cardano.Crypto.Hash (hashToStringAsHex)
import qualified Cardano.Ledger.BaseTypes as L
import Cardano.Ledger.Core (valueTxOutL)
import Cardano.Ledger.Shelley.API (fromNominalDiffTimeMicro)
import Cardano.Ledger.Shelley.LedgerState (esLStateL, lsUTxOStateL, nesEpochStateL,
utxosUtxoL)
import qualified Cardano.Ledger.TxIn as L
import qualified Cardano.Ledger.UTxO as L
import Cardano.Slotting.Time (multNominalDiffTime)
import Cardano.Testnet

import Prelude

import Control.Monad (forM_)
import Control.Monad.Catch (MonadCatch)
import Data.Aeson (eitherDecodeStrictText)
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.KeyMap as Aeson
import Data.Bifunctor (bimap)
import Data.Data (type (:~:) (Refl))
import Data.Either.Extra (mapLeft)
import qualified Data.Map as Map
import Data.String (IsString (fromString))
import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8)
import Data.Time (getCurrentTime)
import Data.Time.Clock (addUTCTime, diffUTCTime, nominalDiffTimeToSeconds)
import qualified Data.Vector as Vector
import GHC.Stack (HasCallStack)
import Lens.Micro ((^.))
Expand All @@ -64,6 +66,7 @@ import Testnet.Types

import Hedgehog
import qualified Hedgehog as H
import Hedgehog.Extras (readJsonFile, threadDelay)
import qualified Hedgehog.Extras as H
import qualified Hedgehog.Extras.Test.Golden as H

Expand Down Expand Up @@ -293,14 +296,17 @@ hprop_cli_queries = integrationWorkspace "cli-queries" $ \tempAbsBasePath' -> H.
TestQueryGovStateCmd ->
-- gov-state
do
-- wait for the proposal stage to end
shelleyGenesisVal <- H.evalEitherM $ readJsonFile shelleyGeneisFile
-- currentEpoch <- getCurrentEpochNo epochStateView
-- liftIO $ putStrLn $ "Current epoch is: " ++ show currentEpoch
waitForFuturePParamsToStabilise shelleyGenesisVal
-- to stdout
output <- execCli' execConfig [ eraName, "query", "gov-state" ]
patchedOutput <- H.evalEither $ patchGovStateOutput output
H.diffVsGoldenFile patchedOutput "test/cardano-testnet-test/files/golden/queries/govStateOut.json"
H.diffVsGoldenFile output "test/cardano-testnet-test/files/golden/queries/govStateOut.json"
-- to a file
let govStateOutFile = work </> "gov-state-out.json"
H.noteM_ $ execCli' execConfig [ eraName, "query", "gov-state", "--out-file", govStateOutFile ]
patchGovStateOutputFile govStateOutFile
H.diffFileVsGoldenFile
govStateOutFile
"test/cardano-testnet-test/files/golden/queries/govStateOut.json"
Expand Down Expand Up @@ -341,13 +347,31 @@ hprop_cli_queries = integrationWorkspace "cli-queries" $ \tempAbsBasePath' -> H.
"test/cardano-testnet-test/files/golden/queries/treasuryOut.txt"

where
patchGovStateOutput :: String -> Either JsonDecodeError String
patchGovStateOutput output = do
eOutput <- mapLeft JsonDecodeError $ eitherDecodeStrictText (T.pack output)
return $ T.unpack $ decodeUtf8 $ prettyPrintJSON $ patchGovStateJSON eOutput
where
patchGovStateJSON :: Aeson.Object -> Aeson.Object
patchGovStateJSON = Aeson.delete "futurePParams"
waitForFuturePParamsToStabilise :: (MonadTest m, MonadIO m) => ShelleyGenesis StandardCrypto -> m ()
waitForFuturePParamsToStabilise
ShelleyGenesis{ Api.sgActiveSlotsCoeff = activeSlotsCoeff
, Api.sgEpochLength = L.EpochSize epochLength
, Api.sgSecurityParam = securityParam
, Api.sgSlotLength = slotLength
, Api.sgSystemStart = systemStart
} = do
currentTime <- liftIO getCurrentTime
let slotsAfterEpochToWaitTo :: Int = floor $ 4 * fromIntegral securityParam / unboundRational activeSlotsCoeff + 1
timeAfterEpochToWaitTo = fromNominalDiffTimeMicro slotLength `multNominalDiffTime` slotsAfterEpochToWaitTo
chainRuntime = currentTime `diffUTCTime` systemStart
currentSlot = L.SlotNo $ floor $ nominalDiffTimeToSeconds chainRuntime / nominalDiffTimeToSeconds (fromNominalDiffTimeMicro slotLength)
currentEpoch = L.EpochNo $ unSlotNo currentSlot `div` epochLength
currentEpochStart = fromNominalDiffTimeMicro slotLength `multNominalDiffTime` (epochLength * unEpochNo currentEpoch) `addUTCTime` systemStart
currentEpochStabilisationTime = timeAfterEpochToWaitTo `addUTCTime` currentEpochStart
timeToWait = currentEpochStabilisationTime `diffUTCTime` currentTime
-- liftIO $ putStrLn $ "I get the currentEpoch is: " ++ show currentEpoch
-- liftIO $ putStrLn $ "I get the currentSlot is: " ++ show currentSlot
-- liftIO $ putStrLn $ "I get the securityParam is: " ++ show securityParam
-- liftIO $ putStrLn $ "I get the activeSlotsCoeff is: " ++ show activeSlotsCoeff
-- liftIO $ putStrLn $ "I get I need to wait till slot: " ++ show (unSlotNo currentSlot `div` epochLength * epochLength + fromIntegral slotsAfterEpochToWaitTo)
if timeToWait > 0

Check warning on line 372 in cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/Cli/Query.hs

View workflow job for this annotation

GitHub Actions / build

Warning in hprop_cli_queries in module Cardano.Testnet.Test.Cli.Query: Use when ▫︎ Found: "if timeToWait > 0 then\n threadDelay $ truncate $ timeToWait * 1_000_000\nelse\n pure ()" ▫︎ Perhaps: "Control.Monad.when (timeToWait > 0)\n $ threadDelay $ truncate $ timeToWait * 1_000_000"
then threadDelay $ truncate $ timeToWait * 1_000_000
else pure ()

readVerificationKeyFromFile :: (MonadIO m, MonadCatch m, MonadTest m, HasTextEnvelope (VerificationKey keyrole), SerialiseAsBech32 (VerificationKey keyrole))
=> AsType keyrole
Expand All @@ -361,12 +385,6 @@ hprop_cli_queries = integrationWorkspace "cli-queries" $ \tempAbsBasePath' -> H.
verificationStakeKeyToStakeAddress testnetMagic delegatorVKey =
makeStakeAddress (fromNetworkMagic $ NetworkMagic $ fromIntegral testnetMagic) (StakeCredentialByKey $ verificationKeyHash delegatorVKey)

patchGovStateOutputFile :: (MonadTest m, MonadIO m) => FilePath -> m ()
patchGovStateOutputFile fp = do
fileContents <- liftIO $ readFile fp
patchedOutput <- H.evalEither $ patchGovStateOutput fileContents
liftIO $ writeFile fp patchedOutput

getTxIx :: forall m era. MonadTest m => ShelleyBasedEra era -> String -> Coin -> (AnyNewEpochState, SlotNo, BlockNo) -> m (Maybe Int)
getTxIx sbe txId amount (AnyNewEpochState sbe' newEpochState, _, _) = do
Refl <- H.leftFail $ assertErasEqual sbe sbe'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,9 @@
"txFeePerByte": 1,
"utxoCostPerByte": 4310
},
"futurePParams": {
"tag": "NoPParamsUpdate"
},
"nextRatifyState": {
"enactedGovActions": [],
"expiredGovActions": [],
Expand Down

0 comments on commit 29ffe92

Please sign in to comment.