Skip to content

Commit

Permalink
Create Testnet.Options module
Browse files Browse the repository at this point in the history
Implement createByronGenesis
  • Loading branch information
Jimbo4350 committed Jan 26, 2023
1 parent 5842c62 commit 761a00b
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 57 deletions.
1 change: 1 addition & 0 deletions cardano-testnet/cardano-testnet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ library
Testnet.Conf
Testnet.Commands.Genesis
Testnet.Commands.Governance
Testnet.Options
Testnet.Run
Testnet.Shelley
Testnet.Utils
Expand Down
2 changes: 1 addition & 1 deletion cardano-testnet/src/Cardano/Testnet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ module Cardano.Testnet (
) where

import Testnet
import Testnet.Babbage
import Testnet.Cardano
import Testnet.Conf hiding (base)
import Testnet.Options
import Testnet.Shelley as Shelley
import Testnet.Utils (waitUntilEpoch)

Expand Down
4 changes: 2 additions & 2 deletions cardano-testnet/src/Parsers/Babbage.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ module Parsers.Babbage
, runBabbageOptions
) where

import Prelude
import Options.Applicative
import qualified Options.Applicative as OA
import Prelude

import Testnet
import Testnet.Babbage
import Testnet.Options
import Testnet.Run (runTestnet)
import Testnet.Util.Runtime (readNodeLoggingFormat)

Expand Down
4 changes: 3 additions & 1 deletion cardano-testnet/src/Testnet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import Hedgehog.Extras.Test.Base (Integration, noteShow_)
import Testnet.Babbage as Babbage
import Testnet.Cardano as Cardano
import Testnet.Conf
import qualified Testnet.Options as Options
import Testnet.Options
import Testnet.Shelley as Shelley (ShelleyTestnetOptions, defaultTestnetOptions,
shelleyTestnet)

Expand Down Expand Up @@ -48,7 +50,7 @@ testnet options conf = case options of
cardanoTestnet o conf

babbageDefaultTestnetOptions :: BabbageTestnetOptions
babbageDefaultTestnetOptions = Babbage.defaultTestnetOptions
babbageDefaultTestnetOptions = Options.defaultTestnetOptions

cardanoDefaultTestnetOptions :: CardanoTestnetOptions
cardanoDefaultTestnetOptions = Cardano.defaultTestnetOptions
Expand Down
50 changes: 12 additions & 38 deletions cardano-testnet/src/Testnet/Babbage.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,29 @@
{-# OPTIONS_GHC -Wno-unused-local-binds -Wno-unused-matches #-}

module Testnet.Babbage
( BabbageTestnetOptions(..)
, defaultTestnetOptions
, TestnetRuntime (..)
( TestnetRuntime (..)
, PaymentKeyPair(..)

, babbageTestnet
) where

import Control.Monad
import Data.Aeson (encode, object, toJSON, (.=))
import Hedgehog.Extras.Stock.Time (showUTCTimeSeconds)
import Prelude
import System.FilePath.Posix ((</>))

import Control.Monad
import Data.Aeson (encode, object, toJSON, (.=))
import qualified Data.HashMap.Lazy as HM
import qualified Data.List as L
import qualified Data.Time.Clock as DTC
import qualified Hedgehog.Extras.Stock.Aeson as J
import qualified Hedgehog.Extras.Stock.OS as OS
import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.File as H
import System.FilePath.Posix ((</>))
import qualified System.Info as OS

import Testnet.Commands.Genesis
import qualified Testnet.Conf as H
import Testnet.Options
import qualified Testnet.Util.Assert as H
import Testnet.Util.Process (execCli_)
import Testnet.Util.Runtime (Delegator (..), NodeLoggingFormat (..), PaymentKeyPair (..),
Expand All @@ -42,23 +40,6 @@ import Testnet.Util.Runtime (Delegator (..), NodeLoggingFormat (..), P

{- HLINT ignore "Redundant flip" -}

data BabbageTestnetOptions = BabbageTestnetOptions
{ babbageNumSpoNodes :: Int
, babbageSlotDuration :: Int
, babbageSecurityParam :: Int
, babbageTotalBalance :: Int
, babbageNodeLoggingFormat :: NodeLoggingFormat
} deriving (Eq, Show)

defaultTestnetOptions :: BabbageTestnetOptions
defaultTestnetOptions = BabbageTestnetOptions
{ babbageNumSpoNodes = 3
, babbageSlotDuration = 200
, babbageSecurityParam = 10
, babbageTotalBalance = 10020000000
, babbageNodeLoggingFormat = NodeLoggingFormatAsJson
}

-- | For an unknown reason, CLI commands are a lot slower on Windows than on Linux and
-- MacOS. We need to allow a lot more time to set up a testnet.
startTimeOffsetSeconds :: DTC.NominalDiffTime
Expand All @@ -75,20 +56,13 @@ babbageTestnet testnetOptions H.Conf {..} = do
currentTime <- H.noteShowIO DTC.getCurrentTime
startTime <- H.noteShow $ DTC.addUTCTime startTimeOffsetSeconds currentTime

execCli_
[ "byron", "genesis", "genesis"
, "--protocol-magic", show @Int testnetMagic
, "--start-time", showUTCTimeSeconds startTime
, "--k", show @Int (babbageSecurityParam testnetOptions)
, "--n-poor-addresses", "0"
, "--n-delegate-addresses", show @Int (babbageNumSpoNodes testnetOptions)
, "--total-balance", show @Int (babbageTotalBalance testnetOptions)
, "--delegate-share", "1"
, "--avvm-entry-count", "0"
, "--avvm-entry-balance", "0"
, "--protocol-parameters-file", tempAbsPath </> "byron.genesis.spec.json"
, "--genesis-output-dir", tempAbsPath </> "byron-gen-command"
]
createByronGenesis
testnetMagic
startTime
testnetOptions
(tempAbsPath </> "byron.genesis.spec.json")
(tempAbsPath </> "byron-gen-command")


-- Because in Babbage the overlay schedule and decentralization parameter
-- are deprecated, we must use the "create-staked" cli command to create
Expand Down
60 changes: 45 additions & 15 deletions cardano-testnet/src/Testnet/Commands/Genesis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,63 @@

-- | All Byron and Shelley Genesis related functionality
module Testnet.Commands.Genesis
( defaultByronGenesisJsonValue
, createShelleyGenesisInitialTxIn
( createShelleyGenesisInitialTxIn
, createByronGenesis
, defaultByronGenesisJsonValue
) where

import Prelude

import Control.Monad.Catch
import Control.Monad.IO.Class
import Data.Aeson
import Data.Time.Clock (UTCTime)

import Hedgehog.Extras.Stock.Time (showUTCTimeSeconds)
import Hedgehog.Internal.Property

import Testnet.Options
import Testnet.Util.Process

-- | Creates a default Byron genesis. This is required for any testnet, predominantly because
-- we inject our ADA supply into our testnet via the Byron genesis.
createByronGenesis
:: (MonadTest m, MonadCatch m, MonadIO m)
=> Int
-> UTCTime
-> BabbageTestnetOptions
-> String
-> String
-> m ()
createByronGenesis testnetMagic startTime testnetOptions pParamFp genOutputDir =
execCli_
[ "byron", "genesis", "genesis"
, "--protocol-magic", show testnetMagic
, "--start-time", showUTCTimeSeconds startTime
, "--k", show (babbageSecurityParam testnetOptions)
, "--n-poor-addresses", "0"
, "--n-delegate-addresses", show @Int (babbageNumSpoNodes testnetOptions)
, "--total-balance", show @Int (babbageTotalBalance testnetOptions)
, "--delegate-share", "1"
, "--avvm-entry-count", "0"
, "--avvm-entry-balance", "0"
, "--protocol-parameters-file", pParamFp
, "--genesis-output-dir", genOutputDir
]

-- | The Shelley initial UTxO is constructed from the 'sgInitialFunds' field which
-- is not a full UTxO but just a map from addresses to coin values. Therefore this
-- command creates a transaction input that defaults to the 0th index and therefore
-- we can spend spend this tx input in a transaction.
createShelleyGenesisInitialTxIn
:: (MonadTest m, MonadCatch m, MonadIO m)
=> Int -> FilePath -> m String
createShelleyGenesisInitialTxIn testnetMagic vKeyFp =
execCli
[ "genesis", "initial-txin"
, "--testnet-magic", show @Int testnetMagic
, "--verification-key-file", vKeyFp
]

-- | We need a Byron genesis in order to be able to hardfork to the later Shelley based eras.
-- The values here don't matter as the testnet conditions are ultimately determined
Expand Down Expand Up @@ -47,16 +90,3 @@ defaultByronGenesisJsonValue =
, "updateVoteThd" .= toJSON @String "1000000000000"
]

-- | The Shelley initial UTxO is constructed from the 'sgInitialFunds' field which
-- is not a full UTxO but just a map from addresses to coin values. Therefore this
-- command creates a transaction input that defaults to the 0th index and therefore
-- we can spend spend this tx input in a transaction.
createShelleyGenesisInitialTxIn
:: (MonadTest m, MonadCatch m, MonadIO m)
=> Int -> FilePath -> m String
createShelleyGenesisInitialTxIn testnetMagic vKeyFp =
execCli
[ "genesis", "initial-txin"
, "--testnet-magic", show @Int testnetMagic
, "--verification-key-file", vKeyFp
]
33 changes: 33 additions & 0 deletions cardano-testnet/src/Testnet/Options.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}

{-# OPTIONS_GHC -Wno-unused-local-binds -Wno-unused-matches #-}

module Testnet.Options
( BabbageTestnetOptions(..)
, defaultTestnetOptions
) where

import Prelude

import Testnet.Util.Runtime (NodeLoggingFormat (..))

{- HLINT ignore "Redundant flip" -}

data BabbageTestnetOptions = BabbageTestnetOptions
{ babbageNumSpoNodes :: Int
, babbageSlotDuration :: Int
, babbageSecurityParam :: Int
, babbageTotalBalance :: Int
, babbageNodeLoggingFormat :: NodeLoggingFormat
} deriving (Eq, Show)

defaultTestnetOptions :: BabbageTestnetOptions
defaultTestnetOptions = BabbageTestnetOptions
{ babbageNumSpoNodes = 3
, babbageSlotDuration = 200
, babbageSecurityParam = 10
, babbageTotalBalance = 10020000000
, babbageNodeLoggingFormat = NodeLoggingFormatAsJson
}

0 comments on commit 761a00b

Please sign in to comment.