|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE NamedFieldPuns #-} |
| 3 | +{-# LANGUAGE OverloadedStrings #-} |
| 4 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 5 | + |
| 6 | +module Cardano.Testnet.Test.P2PTopology |
| 7 | + ( hprop_p2p_topology |
| 8 | + ) where |
| 9 | + |
| 10 | +import Cardano.Api (BlockNo (..), ChainTip (..)) |
| 11 | +import Cardano.CLI.Type.Output (QueryTipLocalStateOutput (..)) |
| 12 | +import Cardano.Node.Configuration.Topology (NodeId) |
| 13 | +import qualified Cardano.Node.Configuration.TopologyP2P as P2P |
| 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 System.Exit (ExitCode (..)) |
| 22 | +import System.FilePath ((</>)) |
| 23 | +import qualified System.Process as IO |
| 24 | + |
| 25 | +import Testnet.Process.Run (execCli', mkExecConfig) |
| 26 | +import Testnet.Property.Util (integrationRetryWorkspace) |
| 27 | +import Testnet.Start.Types (GenesisOptions (..), |
| 28 | + UserProvidedData (..), UserProvidedEnv (..), TopologyType (..)) |
| 29 | + |
| 30 | +import Hedgehog ((===)) |
| 31 | +import qualified Hedgehog as H |
| 32 | +import qualified Hedgehog.Extras as H |
| 33 | + |
| 34 | + |
| 35 | +-- | Execute me with: |
| 36 | +-- @DISABLE_RETRIES=1 cabal test cardano-testnet-test --test-options '-p "/Can be started with P2P topology file/"'@ |
| 37 | +hprop_p2p_topology :: H.Property |
| 38 | +hprop_p2p_topology = integrationRetryWorkspace 2 "p2p-topology" $ \tmpDir -> H.runWithDefaultWatchdog_ $ do |
| 39 | + |
| 40 | + let testnetOptions = def { cardanoOutputDir = UserProvidedEnv tmpDir } |
| 41 | + genesisOptions = def { genesisEpochLength = 200 } |
| 42 | + someTopologyFile = tmpDir </> "node-data" </> "node1" </> "topology.json" |
| 43 | + |
| 44 | + -- Generate the sandbox |
| 45 | + conf <- mkConf tmpDir |
| 46 | + createTestnetEnv |
| 47 | + testnetOptions genesisOptions P2PTopology |
| 48 | + NoUserProvidedData NoUserProvidedData NoUserProvidedData |
| 49 | + conf |
| 50 | + |
| 51 | + -- Check that the topology is indeed P2P |
| 52 | + eTopology <- H.readJsonFile someTopologyFile |
| 53 | + (_topology :: P2P.NetworkTopology NodeId) <- H.leftFail eTopology |
| 54 | + |
| 55 | + -- Run testnet with generated config |
| 56 | + TestnetRuntime |
| 57 | + { testnetNodes |
| 58 | + , testnetMagic |
| 59 | + } <- cardanoTestnet testnetOptions genesisOptions conf |
| 60 | + |
| 61 | + -- There should only be one SPO node among three |
| 62 | + TestnetNode |
| 63 | + { nodeProcessHandle |
| 64 | + , nodeSprocket |
| 65 | + } <- case testnetNodes of |
| 66 | + [spoNode, _relayNode1, _relayNode2] -> do |
| 67 | + (isTestnetNodeSpo <$> testnetNodes) === [True, False, False] |
| 68 | + pure spoNode |
| 69 | + _ -> H.failure |
| 70 | + |
| 71 | + -- Check that blocks have been produced on the chain after 2 minutes at most |
| 72 | + H.byDurationM 5 120 "Expected blocks to be minted" $ do |
| 73 | + execConfig <- mkExecConfig tmpDir nodeSprocket testnetMagic |
| 74 | + tipStr <- H.noteM $ execCli' execConfig |
| 75 | + [ "query", "tip" |
| 76 | + , "--output-json" |
| 77 | + ] |
| 78 | + QueryTipLocalStateOutput |
| 79 | + { localStateChainTip = tip |
| 80 | + } <- H.nothingFail $ A.decode $ B.pack tipStr |
| 81 | + |
| 82 | + case tip of |
| 83 | + ChainTipAtGenesis -> H.failure |
| 84 | + ChainTip _ _ (BlockNo blockNo) -> |
| 85 | + -- Blocks have been produced if the tip of the chain is > 0 |
| 86 | + H.assertWith blockNo (> 0) |
| 87 | + |
| 88 | + -- If everything went fine, terminate the node and exit with success |
| 89 | + exit <- H.evalIO $ do |
| 90 | + IO.terminateProcess nodeProcessHandle |
| 91 | + IO.waitForProcess nodeProcessHandle |
| 92 | + -- Nodes are expected to exit successfully when terminated |
| 93 | + exit === ExitSuccess |
0 commit comments