|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE NamedFieldPuns #-} |
| 3 | +{-# LANGUAGE NumericUnderscores #-} |
| 4 | +{-# LANGUAGE OverloadedStrings #-} |
| 5 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 6 | + |
| 7 | +module Cardano.Testnet.Test.UpdateTimeStamps |
| 8 | + ( hprop_update_time_stamps |
| 9 | + ) where |
| 10 | + |
| 11 | +import Cardano.Api (BlockNo (..), ChainTip (..)) |
| 12 | + |
| 13 | +import Cardano.CLI.Type.Output (QueryTipLocalStateOutput (..)) |
| 14 | +import Cardano.Testnet hiding (shelleyGenesisFile) |
| 15 | + |
| 16 | +import Prelude |
| 17 | + |
| 18 | +import qualified Data.Aeson as A |
| 19 | +import qualified Data.ByteString.Lazy.Char8 as B |
| 20 | +import Data.Default.Class (def) |
| 21 | +import GHC.Float (double2Int) |
| 22 | +import System.Exit (ExitCode (..)) |
| 23 | +import System.FilePath ((</>)) |
| 24 | +import qualified System.Process as IO |
| 25 | + |
| 26 | +import Testnet.Components.Configuration (startTimeOffsetSeconds) |
| 27 | +import Testnet.Process.Run (execCli', mkExecConfig) |
| 28 | +import Testnet.Property.Util (integrationRetryWorkspace) |
| 29 | +import Testnet.Start.Types (CreateEnvOptions (..), CreateEnvUpdateTime (..), |
| 30 | + GenesisHashesPolicy (..), GenesisOptions (..), |
| 31 | + UserProvidedData (..), UserProvidedEnv (..)) |
| 32 | + |
| 33 | +import Hedgehog ((===)) |
| 34 | +import qualified Hedgehog as H |
| 35 | +import qualified Hedgehog.Extras as H |
| 36 | + |
| 37 | +-- | Execute me with: |
| 38 | +-- @DISABLE_RETRIES=1 cabal test cardano-testnet-test --test-options '-p "/Can have its start time modified/"'@ |
| 39 | +hprop_update_time_stamps :: H.Property |
| 40 | +hprop_update_time_stamps = integrationRetryWorkspace 2 "update-time-stamps" $ \tmpDir -> H.runWithDefaultWatchdog_ $ do |
| 41 | + |
| 42 | + let testnetOptions = def { cardanoOutputDir = UserProvidedEnv tmpDir } |
| 43 | + genesisOptions = def { genesisEpochLength = 200 } |
| 44 | + byronGenesisFile = tmpDir </> "byron-genesis.json" |
| 45 | + shelleyGenesisFile = tmpDir </> "shelley-genesis.json" |
| 46 | + |
| 47 | + -- Generate the sandbox |
| 48 | + conf <- mkConf tmpDir |
| 49 | + createTestnetEnv |
| 50 | + testnetOptions genesisOptions def |
| 51 | + NoUserProvidedData NoUserProvidedData NoUserProvidedData |
| 52 | + -- Do not add hashes to the main config file, so that genesis files |
| 53 | + -- can be modified without having to recompute hashes every time. |
| 54 | + conf{genesisHashesPolicy = WithoutHashes} |
| 55 | + |
| 56 | + -- Wait for 20% more than `startTimeOffsetSeconds`, to ensure that |
| 57 | + -- the time bounds in the sandbox' config files are no longer valid |
| 58 | + H.threadDelay $ double2Int $ realToFrac startTimeOffsetSeconds * 1_000_000 * 1.2 |
| 59 | + |
| 60 | + -- Call `createTestnetEnv` again to update the time stamps |
| 61 | + createTestnetEnv |
| 62 | + testnetOptions genesisOptions |
| 63 | + def{ceoUpdateTime = UpdateTimeAndExit} |
| 64 | + NoUserProvidedData NoUserProvidedData NoUserProvidedData |
| 65 | + conf |
| 66 | + |
| 67 | + -- Run testnet with generated config |
| 68 | + TestnetRuntime |
| 69 | + { testnetNodes |
| 70 | + , testnetMagic |
| 71 | + } <- cardanoTestnet testnetOptions genesisOptions conf |
| 72 | + |
| 73 | + -- There should only be one SPO node among three |
| 74 | + TestnetNode |
| 75 | + { nodeProcessHandle |
| 76 | + , nodeSprocket |
| 77 | + } <- case testnetNodes of |
| 78 | + [spoNode, _relayNode1, _relayNode2] -> do |
| 79 | + (isTestnetNodeSpo <$> testnetNodes) === [True, False, False] |
| 80 | + pure spoNode |
| 81 | + _ -> H.failure |
| 82 | + |
| 83 | + -- Check that blocks have been produced on the chain after 2 minutes at most |
| 84 | + H.byDurationM 5 120 "Expected blocks to be minted" $ do |
| 85 | + execConfig <- mkExecConfig tmpDir nodeSprocket testnetMagic |
| 86 | + tipStr <- H.noteM $ execCli' execConfig |
| 87 | + [ "query", "tip" |
| 88 | + , "--output-json" |
| 89 | + ] |
| 90 | + QueryTipLocalStateOutput |
| 91 | + { localStateChainTip = tip |
| 92 | + } <- H.nothingFail $ A.decode $ B.pack tipStr |
| 93 | + |
| 94 | + case tip of |
| 95 | + ChainTipAtGenesis -> H.failure |
| 96 | + ChainTip _ _ (BlockNo blockNo) -> |
| 97 | + -- Blocks have been produced if the tip of the chain is > 0 |
| 98 | + H.assertWith blockNo (> 0) |
| 99 | + |
| 100 | + -- If everything went fine, terminate the node and exit with success |
| 101 | + exit <- H.evalIO $ do |
| 102 | + IO.terminateProcess nodeProcessHandle |
| 103 | + IO.waitForProcess nodeProcessHandle |
| 104 | + -- Nodes are expected to exit successfully when terminated |
| 105 | + exit === ExitSuccess |
0 commit comments