Skip to content

Commit 7b8a6ba

Browse files
committed
Add test for updating time stamps
1 parent 85a0e31 commit 7b8a6ba

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

cardano-testnet/cardano-testnet.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ test-suite cardano-testnet-test
226226
Cardano.Testnet.Test.SanityCheck
227227
Cardano.Testnet.Test.RunTestnet
228228
Cardano.Testnet.Test.SubmitApi.Transaction
229+
Cardano.Testnet.Test.UpdateTimeStamps
229230

230231
type: exitcode-stdio-1.0
231232

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import qualified Cardano.Testnet.Test.P2PTopology
3131
import qualified Cardano.Testnet.Test.RunTestnet
3232
import qualified Cardano.Testnet.Test.SanityCheck as LedgerEvents
3333
import qualified Cardano.Testnet.Test.SubmitApi.Transaction
34+
import qualified Cardano.Testnet.Test.UpdateTimeStamps
3435

3536
import Prelude
3637

@@ -116,6 +117,7 @@ tests = do
116117
[ ignoreOnWindows "Produces blocks" Cardano.Testnet.Test.RunTestnet.hprop_run_testnet
117118
, ignoreOnMacAndWindows "Supports dumping/loading config files" Cardano.Testnet.Test.DumpConfig.hprop_dump_config
118119
, ignoreOnMacAndWindows "Can be started with P2P topology file" Cardano.Testnet.Test.P2PTopology.hprop_p2p_topology
120+
, ignoreOnMacAndWindows "Can have its start time modified" Cardano.Testnet.Test.UpdateTimeStamps.hprop_update_time_stamps
119121
]
120122
, T.testGroup "SubmitApi"
121123
[ ignoreOnMacAndWindows "transaction" Cardano.Testnet.Test.SubmitApi.Transaction.hprop_transaction

0 commit comments

Comments
 (0)