Skip to content

Commit

Permalink
Compiles and passes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 committed Jan 18, 2023
1 parent ce3daec commit 0c10c7d
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 59 deletions.
34 changes: 19 additions & 15 deletions cardano-testnet/src/Parsers/Cardano.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ module Parsers.Cardano
) where

import Prelude

import qualified Data.List as L
import Options.Applicative
import qualified Options.Applicative as OA
import Text.Read

import Testnet.Util.Runtime (readNodeLoggingFormat)
import Testnet
import Testnet.Cardano
import Testnet.Run (runTestnet)
import Testnet.Util.Runtime (readNodeLoggingFormat)


data CardanoOptions = CardanoOptions
Expand All @@ -25,20 +26,23 @@ data CardanoOptions = CardanoOptions

optsTestnet :: Parser CardanoTestnetOptions
optsTestnet = CardanoTestnetOptions
<$> OA.option
((`L.replicate` cardanoDefaultTestnetNodeOptions) <$> auto)
( OA.long "num-bft-nodes"
<> OA.help "Number of BFT nodes"
<> OA.metavar "COUNT"
<> OA.showDefault
<> OA.value (cardanoBftNodeOptions defaultTestnetOptions)
)
<*> OA.option auto
( OA.long "num-pool-nodes"
<> OA.help "Number of pool nodes"
<> OA.metavar "COUNT"
<> OA.showDefault
<> OA.value (cardanoNumPoolNodes defaultTestnetOptions)
<$> ((++)
<$> OA.option
((`L.replicate` BftTestnetNodeOptions []) <$> auto)
( OA.long "num-bft-nodes"
<> OA.help "Number of BFT nodes"
<> OA.metavar "COUNT"
<> OA.showDefault
<> OA.value (cardanoNodes defaultTestnetOptions)
)
<*> OA.option
((`L.replicate` SpoTestnetNodeOptions) <$> auto)
( OA.long "num-pool-nodes"
<> OA.help "Number of pool nodes"
<> OA.metavar "COUNT"
<> OA.showDefault
<> OA.value (cardanoNodes defaultTestnetOptions)
)
)
<*> OA.option (OA.eitherReader readEither)
( OA.long "era"
Expand Down
37 changes: 28 additions & 9 deletions cardano-testnet/src/Testnet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,46 @@ module Testnet
, Testnet.testnet
) where

import Data.Eq (Eq)
import Text.Show (Show)
import Control.Monad
import Prelude

import qualified Hedgehog.Extras.Test.Base as H


import Hedgehog
import Hedgehog.Extras.Test.Base (Integration, noteShow_)
import Testnet.Babbage as Babbage
import Testnet.Cardano as Cardano
import Testnet.Conf
import Testnet.Shelley as Shelley
import Testnet.Shelley as Shelley (ShelleyTestnetOptions, defaultTestnetOptions,
shelleyTestnet)

data TestnetOptions
= ShelleyOnlyTestnetOptions ShelleyTestnetOptions
| BabbageOnlyTestnetOptions BabbageTestnetOptions
| CardanoOnlyTestnetOptions CardanoTestnetOptions
deriving (Eq, Show)

testnet :: TestnetOptions -> Conf -> H.Integration TestnetRuntime
testnet options = case options of
ShelleyOnlyTestnetOptions o -> shelleyTestnet o
BabbageOnlyTestnetOptions o -> babbageTestnet o
CardanoOnlyTestnetOptions o -> cardanoTestnet o
-- | There are certain conditions that need to be met in order to run
-- a valid node cluster.
testnetMinimumConfigurationRequirements :: CardanoTestnetOptions -> Integration ()
testnetMinimumConfigurationRequirements cTestnetOpts = do
when (length (cardanoNodes cTestnetOpts) < 2) $ do
noteShow_ ("Need at least two nodes to run a cluster" :: String)
noteShow_ cTestnetOpts
assert False

when (cardanoNumPoolNodes (cardanoNodes cTestnetOpts) < 1) $ do
noteShow_ ("Need at least one SPO to run a cluster" :: String)
noteShow_ cTestnetOpts
assert False

testnet :: TestnetOptions -> Conf -> Integration TestnetRuntime
testnet options conf = case options of
ShelleyOnlyTestnetOptions o -> shelleyTestnet o conf
BabbageOnlyTestnetOptions o -> babbageTestnet o conf
CardanoOnlyTestnetOptions o -> do
testnetMinimumConfigurationRequirements o
cardanoTestnet o conf

babbageDefaultTestnetOptions :: BabbageTestnetOptions
babbageDefaultTestnetOptions = Babbage.defaultTestnetOptions
Expand Down
8 changes: 4 additions & 4 deletions cardano-testnet/src/Testnet/Babbage.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.File as H
import qualified System.Info as OS

import qualified Testnet.Conf as H
import qualified Testnet.Util.Assert as H
import Testnet.Util.Process (execCli_)
import Testnet.Util.Runtime (Delegator (..), NodeLoggingFormat (..), PaymentKeyPair (..),
PoolNode (PoolNode), PoolNodeKeys (..), StakingKeyPair (..),
TestnetRuntime (..), startNode)
import qualified Testnet.Conf as H
PoolNode (PoolNode), PoolNodeKeys (..), StakingKeyPair (..), TestnetRuntime (..),
startNode)


{- HLINT ignore "Redundant flip" -}
Expand All @@ -52,7 +52,7 @@ data BabbageTestnetOptions = BabbageTestnetOptions
defaultTestnetOptions :: BabbageTestnetOptions
defaultTestnetOptions = BabbageTestnetOptions
{ babbageNumSpoNodes = 3
, babbageSlotDuration = 1000
, babbageSlotDuration = 200
, babbageSecurityParam = 10
, babbageTotalBalance = 10020000000
, babbageNodeLoggingFormat = NodeLoggingFormatAsJson
Expand Down
60 changes: 41 additions & 19 deletions cardano-testnet/src/Testnet/Cardano.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
module Testnet.Cardano
( ForkPoint(..)
, CardanoTestnetOptions(..)
, cardanoNumPoolNodes
, extraBftNodeCliArgs
, defaultTestnetOptions
, TestnetNodeOptions(..)
, cardanoDefaultTestnetNodeOptions
Expand All @@ -17,6 +19,8 @@ module Testnet.Cardano
, cardanoTestnet
) where

import Prelude

import qualified Cardano.Crypto.Hash.Blake2b
import qualified Cardano.Crypto.Hash.Class
import Control.Monad
Expand All @@ -33,7 +37,6 @@ import Hedgehog.Extras.Stock.IO.Network.Sprocket (Sprocket (..))
import Hedgehog.Extras.Stock.Time (formatIso8601, showUTCTimeSeconds)
import Ouroboros.Network.PeerSelection.LedgerPeers (UseLedgerAfter (..))
import Ouroboros.Network.PeerSelection.RelayAccessPoint (RelayAccessPoint (..))
import Prelude
import System.FilePath.Posix ((</>))

import Cardano.Chain.Genesis (GenesisHash (unGenesisHash), readGenesisData)
Expand Down Expand Up @@ -77,8 +80,7 @@ data Era = Byron | Shelley | Allegra | Mary | Alonzo deriving (Eq, Enum, Bounded
data CardanoTestnetOptions = CardanoTestnetOptions
{ -- | List of node options. Each option will result in a single node being
-- created.
cardanoBftNodeOptions :: [TestnetNodeOptions]
, cardanoNumPoolNodes :: Int
cardanoNodes :: [TestnetNodeOptions]
, cardanoEra :: Era
, cardanoEpochLength :: Int
, cardanoSlotLength :: Double
Expand All @@ -89,8 +91,7 @@ data CardanoTestnetOptions = CardanoTestnetOptions

defaultTestnetOptions :: CardanoTestnetOptions
defaultTestnetOptions = CardanoTestnetOptions
{ cardanoBftNodeOptions = L.replicate 2 cardanoDefaultTestnetNodeOptions
, cardanoNumPoolNodes = 1
{ cardanoNodes = cardanoDefaultTestnetNodeOptions
, cardanoEra = Alonzo
, cardanoEpochLength = 1500
, cardanoSlotLength = 0.2
Expand All @@ -99,16 +100,32 @@ defaultTestnetOptions = CardanoTestnetOptions
, cardanoNodeLoggingFormat = NodeLoggingFormatAsText
}

newtype TestnetNodeOptions = TestnetNodeOptions
{ -- | These arguments will be appended to the default set of CLI options when
-- | Specify a BFT node (Pre-Babbage era only) or an SPO (Shelley era onwards only)
data TestnetNodeOptions
= BftTestnetNodeOptions [String]
-- ^ These arguments will be appended to the default set of CLI options when
-- starting the node.
extraNodeCliArgs :: [String]
} deriving (Eq, Show)
| SpoTestnetNodeOptions
deriving (Eq, Show)

cardanoDefaultTestnetNodeOptions :: TestnetNodeOptions
cardanoDefaultTestnetNodeOptions = TestnetNodeOptions
{ extraNodeCliArgs = []
}
extraBftNodeCliArgs :: TestnetNodeOptions -> [String ]
extraBftNodeCliArgs (BftTestnetNodeOptions args) = args
extraBftNodeCliArgs SpoTestnetNodeOptions = []

cardanoNumPoolNodes :: [TestnetNodeOptions] -> Int
cardanoNumPoolNodes testnetNodeOpts =
length $ filter (== SpoTestnetNodeOptions) testnetNodeOpts

cardanoNumBftNodes :: [TestnetNodeOptions] -> Int
cardanoNumBftNodes testnetNodeOpts =
length $ filter (/= SpoTestnetNodeOptions) testnetNodeOpts

cardanoDefaultTestnetNodeOptions :: [TestnetNodeOptions]
cardanoDefaultTestnetNodeOptions =
[ BftTestnetNodeOptions []
, BftTestnetNodeOptions []
, SpoTestnetNodeOptions
]

ifaceAddress :: String
ifaceAddress = "127.0.0.1"
Expand Down Expand Up @@ -161,9 +178,9 @@ cardanoTestnet testnetOptions H.Conf {..} = do
currentTime <- H.noteShowIO DTC.getCurrentTime
startTime <- H.noteShow $ DTC.addUTCTime startTimeOffsetSeconds currentTime
configurationFile <- H.noteShow $ tempAbsPath </> "configuration.yaml"
let numBftNodes = L.length (cardanoBftNodeOptions testnetOptions)
let numBftNodes = cardanoNumBftNodes $ cardanoNodes testnetOptions
bftNodesN = [1 .. numBftNodes]
poolNodesN = [1 .. cardanoNumPoolNodes testnetOptions]
poolNodesN = [1 .. 1] -- TODO: Figure out default config settings cardanoNumPoolNodes $ cardanoNodes testnetOptions]
bftNodeNames = ("node-bft" <>) . show @Int <$> bftNodesN
poolNodeNames = ("node-pool" <>) . show @Int <$> poolNodesN
allNodeNames = bftNodeNames <> poolNodeNames
Expand Down Expand Up @@ -247,7 +264,7 @@ cardanoTestnet testnetOptions H.Conf {..} = do
forM_ allNodeNames $ \node -> do
let port = fromJust $ M.lookup node nodeToPort
H.lbsWriteFile (tempAbsPath </> node </> "topology.json") $
mkTopologyConfig (numBftNodes + cardanoNumPoolNodes testnetOptions)
mkTopologyConfig (numBftNodes + cardanoNumPoolNodes (cardanoNodes testnetOptions))
allPorts port (cardanoEnableP2P testnetOptions)

H.writeFile (tempAbsPath </> node </> "port") (show port)
Expand Down Expand Up @@ -426,13 +443,18 @@ cardanoTestnet testnetOptions H.Conf {..} = do
)

-- Now generate for real:

when (cardanoNumBftNodes (cardanoNodes testnetOptions) < 2) $ do
H.noteShow_ testnetOptions
H.assert False

execCli_
[ "genesis", "create"
, "--testnet-magic", show @Int testnetMagic
, "--genesis-dir", tempAbsPath </> "shelley"
, "--gen-genesis-keys", show @Int numBftNodes
, "--start-time", formatIso8601 startTime
, "--gen-utxo-keys", show @Int (cardanoNumPoolNodes testnetOptions)
, "--gen-utxo-keys", show (cardanoNumBftNodes $ cardanoNodes testnetOptions)
]

-- Generated genesis keys and genesis files
Expand Down Expand Up @@ -725,7 +747,7 @@ cardanoTestnet testnetOptions H.Conf {..} = do
--------------------------------
-- Launch cluster of three nodes

let bftNodeNameAndOpts = L.zip bftNodeNames (cardanoBftNodeOptions testnetOptions)
let bftNodeNameAndOpts = L.zip bftNodeNames (cardanoNodes testnetOptions)
bftNodes <- forM bftNodeNameAndOpts $ \(node, nodeOpts) -> do
startNode tempBaseAbsPath tempAbsPath logDir socketDir node
([ "run"
Expand All @@ -737,7 +759,7 @@ cardanoTestnet testnetOptions H.Conf {..} = do
, "--shelley-operational-certificate", tempAbsPath </> node </> "shelley/node.cert"
, "--delegation-certificate", tempAbsPath </> node </> "byron/delegate.cert"
, "--signing-key", tempAbsPath </> node </> "byron/delegate.key"
] <> extraNodeCliArgs nodeOpts)
] <> extraBftNodeCliArgs nodeOpts)

H.threadDelay 100000

Expand Down
4 changes: 2 additions & 2 deletions cardano-testnet/test/Test/Cli/KesPeriodInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import qualified Hedgehog.Extras.Test.Process as H
import qualified System.Directory as IO
import qualified System.Info as SYS

import Test.Misc
import Cardano.Testnet
import Test.Misc
import Testnet.Util.Process
import Testnet.Util.Runtime

Expand All @@ -51,7 +51,7 @@ hprop_kes_period_info = integration . H.runFinallies . H.workspace "chairman" $
tempAbsBasePath' Nothing

let fastTestnetOptions = CardanoOnlyTestnetOptions $ cardanoDefaultTestnetOptions
{ cardanoBftNodeOptions = replicate 1 cardanoDefaultTestnetNodeOptions
{ cardanoNodes = cardanoDefaultTestnetNodeOptions
, cardanoEpochLength = 500
, cardanoSlotLength = 0.02
, cardanoActiveSlotsCoeff = 0.1
Expand Down
18 changes: 8 additions & 10 deletions cardano-testnet/test/Test/ShutdownOnSlotSynced.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ module Test.ShutdownOnSlotSynced
( hprop_shutdownOnSlotSynced
) where

import Prelude
import Control.Monad
import Data.Either (isRight)
import Data.List (find, isInfixOf)
import Data.Maybe
import GHC.IO.Exception (ExitCode (ExitSuccess))
import GHC.Stack (callStack)
import System.FilePath ((</>))
import Prelude
import qualified System.Directory as IO
import Text.Read (readMaybe)
import System.FilePath ((</>))
import Text.Read (readMaybe)

import Hedgehog (Property, assert, (===))
import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.File as H
import qualified Hedgehog.Extras.Test.Process as H

import Cardano.Testnet
import Testnet.Util.Runtime (TestnetRuntime(..))
import Testnet.Util.Runtime (TestnetRuntime (..))

hprop_shutdownOnSlotSynced :: Property
hprop_shutdownOnSlotSynced = integration . H.runFinallies . H.workspace "chairman" $ \tempAbsBasePath' -> do
Expand All @@ -37,12 +37,10 @@ hprop_shutdownOnSlotSynced = integration . H.runFinallies . H.workspace "chairma
let fastTestnetOptions = CardanoOnlyTestnetOptions $ cardanoDefaultTestnetOptions
{ cardanoEpochLength = 300
, cardanoSlotLength = slotLen
, cardanoBftNodeOptions =
[ TestnetNodeOptions
{ extraNodeCliArgs = ["--shutdown-on-slot-synced", show maxSlot]
}
, cardanoDefaultTestnetNodeOptions
, cardanoDefaultTestnetNodeOptions
, cardanoNodes =
[ BftTestnetNodeOptions ["--shutdown-on-slot-synced", show maxSlot]
, BftTestnetNodeOptions []
, SpoTestnetNodeOptions
]
}
TestnetRuntime { bftNodes = node:_ } <- testnet fastTestnetOptions conf
Expand Down

0 comments on commit 0c10c7d

Please sign in to comment.