Skip to content

Commit 7942638

Browse files
committed
Create Testnet.Options module
Implement createByronGenesis
1 parent 0d51ee1 commit 7942638

File tree

8 files changed

+103
-61
lines changed

8 files changed

+103
-61
lines changed

cardano-testnet/cardano-testnet.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ library
8181
Testnet.Conf
8282
Testnet.Commands.Genesis
8383
Testnet.Commands.Governance
84+
Testnet.Options
8485
Testnet.Run
8586
Testnet.Shelley
8687
Testnet.Utils

cardano-testnet/src/Cardano/Testnet.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ module Cardano.Testnet (
3737
) where
3838

3939
import Testnet
40-
import Testnet.Babbage
4140
import Testnet.Cardano
4241
import Testnet.Conf hiding (base)
42+
import Testnet.Options
4343
import Testnet.Shelley as Shelley
4444
import Testnet.Utils (waitUntilEpoch)
4545

cardano-testnet/src/Parsers/Babbage.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ module Parsers.Babbage
44
, runBabbageOptions
55
) where
66

7-
import Prelude
87
import Options.Applicative
98
import qualified Options.Applicative as OA
9+
import Prelude
1010

1111
import Testnet
12-
import Testnet.Babbage
12+
import Testnet.Options
1313
import Testnet.Run (runTestnet)
1414
import Testnet.Util.Runtime (readNodeLoggingFormat)
1515

cardano-testnet/src/Testnet.hs

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import Hedgehog.Extras.Test.Base (Integration, noteShow_)
1616
import Testnet.Babbage as Babbage
1717
import Testnet.Cardano as Cardano
1818
import Testnet.Conf
19+
import qualified Testnet.Options as Options
20+
import Testnet.Options
1921
import Testnet.Shelley as Shelley (ShelleyTestnetOptions, defaultTestnetOptions,
2022
shelleyTestnet)
2123

@@ -48,7 +50,7 @@ testnet options conf = case options of
4850
cardanoTestnet o conf
4951

5052
babbageDefaultTestnetOptions :: BabbageTestnetOptions
51-
babbageDefaultTestnetOptions = Babbage.defaultTestnetOptions
53+
babbageDefaultTestnetOptions = Options.defaultTestnetOptions
5254

5355
cardanoDefaultTestnetOptions :: CardanoTestnetOptions
5456
cardanoDefaultTestnetOptions = Cardano.defaultTestnetOptions

cardano-testnet/src/Testnet/Babbage.hs

+12-38
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,29 @@
88
{-# OPTIONS_GHC -Wno-unused-local-binds -Wno-unused-matches #-}
99

1010
module Testnet.Babbage
11-
( BabbageTestnetOptions(..)
12-
, defaultTestnetOptions
13-
, TestnetRuntime (..)
11+
( TestnetRuntime (..)
1412
, PaymentKeyPair(..)
1513

1614
, babbageTestnet
1715
) where
1816

19-
import Control.Monad
20-
import Data.Aeson (encode, object, toJSON, (.=))
21-
import Hedgehog.Extras.Stock.Time (showUTCTimeSeconds)
2217
import Prelude
23-
import System.FilePath.Posix ((</>))
2418

19+
import Control.Monad
20+
import Data.Aeson (encode, object, toJSON, (.=))
2521
import qualified Data.HashMap.Lazy as HM
2622
import qualified Data.List as L
2723
import qualified Data.Time.Clock as DTC
2824
import qualified Hedgehog.Extras.Stock.Aeson as J
2925
import qualified Hedgehog.Extras.Stock.OS as OS
3026
import qualified Hedgehog.Extras.Test.Base as H
3127
import qualified Hedgehog.Extras.Test.File as H
28+
import System.FilePath.Posix ((</>))
3229
import qualified System.Info as OS
3330

3431
import Testnet.Commands.Genesis
3532
import qualified Testnet.Conf as H
33+
import Testnet.Options
3634
import qualified Testnet.Util.Assert as H
3735
import Testnet.Util.Process (execCli_)
3836
import Testnet.Util.Runtime (Delegator (..), NodeLoggingFormat (..), PaymentKeyPair (..),
@@ -42,23 +40,6 @@ import Testnet.Util.Runtime (Delegator (..), NodeLoggingFormat (..), P
4240

4341
{- HLINT ignore "Redundant flip" -}
4442

45-
data BabbageTestnetOptions = BabbageTestnetOptions
46-
{ babbageNumSpoNodes :: Int
47-
, babbageSlotDuration :: Int
48-
, babbageSecurityParam :: Int
49-
, babbageTotalBalance :: Int
50-
, babbageNodeLoggingFormat :: NodeLoggingFormat
51-
} deriving (Eq, Show)
52-
53-
defaultTestnetOptions :: BabbageTestnetOptions
54-
defaultTestnetOptions = BabbageTestnetOptions
55-
{ babbageNumSpoNodes = 3
56-
, babbageSlotDuration = 200
57-
, babbageSecurityParam = 10
58-
, babbageTotalBalance = 10020000000
59-
, babbageNodeLoggingFormat = NodeLoggingFormatAsJson
60-
}
61-
6243
-- | For an unknown reason, CLI commands are a lot slower on Windows than on Linux and
6344
-- MacOS. We need to allow a lot more time to set up a testnet.
6445
startTimeOffsetSeconds :: DTC.NominalDiffTime
@@ -75,20 +56,13 @@ babbageTestnet testnetOptions H.Conf {..} = do
7556
currentTime <- H.noteShowIO DTC.getCurrentTime
7657
startTime <- H.noteShow $ DTC.addUTCTime startTimeOffsetSeconds currentTime
7758

78-
execCli_
79-
[ "byron", "genesis", "genesis"
80-
, "--protocol-magic", show @Int testnetMagic
81-
, "--start-time", showUTCTimeSeconds startTime
82-
, "--k", show @Int (babbageSecurityParam testnetOptions)
83-
, "--n-poor-addresses", "0"
84-
, "--n-delegate-addresses", show @Int (babbageNumSpoNodes testnetOptions)
85-
, "--total-balance", show @Int (babbageTotalBalance testnetOptions)
86-
, "--delegate-share", "1"
87-
, "--avvm-entry-count", "0"
88-
, "--avvm-entry-balance", "0"
89-
, "--protocol-parameters-file", tempAbsPath </> "byron.genesis.spec.json"
90-
, "--genesis-output-dir", tempAbsPath </> "byron-gen-command"
91-
]
59+
createByronGenesis
60+
testnetMagic
61+
startTime
62+
testnetOptions
63+
(tempAbsPath </> "byron.genesis.spec.json")
64+
(tempAbsPath </> "byron-gen-command")
65+
9266

9367
-- Because in Babbage the overlay schedule and decentralization parameter
9468
-- are deprecated, we must use the "create-staked" cli command to create

cardano-testnet/src/Testnet/Commands/Genesis.hs

+46-15
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,64 @@
33

44
-- | All Byron and Shelley Genesis related functionality
55
module Testnet.Commands.Genesis
6-
( defaultByronGenesisJsonValue
7-
, createShelleyGenesisInitialTxIn
6+
( createShelleyGenesisInitialTxIn
7+
, createByronGenesis
8+
, defaultByronGenesisJsonValue
89
) where
910

1011
import Prelude
1112

1213
import Control.Monad.Catch
1314
import Control.Monad.IO.Class
1415
import Data.Aeson
16+
import Data.Time.Clock (UTCTime)
17+
import GHC.Stack (HasCallStack, withFrozenCallStack)
1518

19+
import Hedgehog.Extras.Stock.Time (showUTCTimeSeconds)
1620
import Hedgehog.Internal.Property
1721

22+
import Testnet.Options
1823
import Testnet.Util.Process
1924

25+
-- | Creates a default Byron genesis. This is required for any testnet, predominantly because
26+
-- we inject our ADA supply into our testnet via the Byron genesis.
27+
createByronGenesis
28+
:: (MonadTest m, MonadCatch m, MonadIO m, HasCallStack)
29+
=> Int
30+
-> UTCTime
31+
-> BabbageTestnetOptions
32+
-> String
33+
-> String
34+
-> m ()
35+
createByronGenesis testnetMagic startTime testnetOptions pParamFp genOutputDir =
36+
withFrozenCallStack $ execCli_
37+
[ "byron", "genesis", "genesis"
38+
, "--protocol-magic", show testnetMagic
39+
, "--start-time", showUTCTimeSeconds startTime
40+
, "--k", show (babbageSecurityParam testnetOptions)
41+
, "--n-poor-addresses", "0"
42+
, "--n-delegate-addresses", show @Int (babbageNumSpoNodes testnetOptions)
43+
, "--total-balance", show @Int (babbageTotalBalance testnetOptions)
44+
, "--delegate-share", "1"
45+
, "--avvm-entry-count", "0"
46+
, "--avvm-entry-balance", "0"
47+
, "--protocol-parameters-file", pParamFp
48+
, "--genesis-output-dir", genOutputDir
49+
]
50+
51+
-- | The Shelley initial UTxO is constructed from the 'sgInitialFunds' field which
52+
-- is not a full UTxO but just a map from addresses to coin values. Therefore this
53+
-- command creates a transaction input that defaults to the 0th index and therefore
54+
-- we can spend spend this tx input in a transaction.
55+
createShelleyGenesisInitialTxIn
56+
:: (MonadTest m, MonadCatch m, MonadIO m, HasCallStack)
57+
=> Int -> FilePath -> m String
58+
createShelleyGenesisInitialTxIn testnetMagic vKeyFp =
59+
withFrozenCallStack $ execCli
60+
[ "genesis", "initial-txin"
61+
, "--testnet-magic", show @Int testnetMagic
62+
, "--verification-key-file", vKeyFp
63+
]
2064

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

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

cardano-testnet/src/Testnet/Commands/Governance.hs

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ import Prelude
99

1010
import Control.Monad.Catch
1111
import Control.Monad.IO.Class
12+
import GHC.Stack (HasCallStack, withFrozenCallStack)
1213

1314
import Testnet.Util.Process (execCli_)
1415

1516
import Hedgehog.Internal.Property
1617

1718

1819
createByronUpdateProposal
19-
:: (MonadTest m, MonadCatch m, MonadIO m)
20+
:: (MonadTest m, MonadCatch m, MonadIO m, HasCallStack)
2021
=> Int -> String -> String -> Int -> m ()
2122
createByronUpdateProposal testnetMagic signingKeyFp updateProposalFp ptclMajorVersion =
22-
execCli_
23+
withFrozenCallStack $ execCli_
2324
[ "byron", "governance", "create-update-proposal"
2425
, "--filepath", updateProposalFp
2526
, "--testnet-magic", show testnetMagic
@@ -34,10 +35,10 @@ createByronUpdateProposal testnetMagic signingKeyFp updateProposalFp ptclMajorVe
3435
]
3536

3637
createByronUpdateProposalVote
37-
:: (MonadTest m, MonadCatch m, MonadIO m)
38+
:: (MonadTest m, MonadCatch m, MonadIO m, HasCallStack)
3839
=> Int -> String -> String -> String -> m ()
3940
createByronUpdateProposalVote testnetMagic updateProposalFp signingKey outputFp =
40-
execCli_
41+
withFrozenCallStack $ execCli_
4142
[ "byron", "governance", "create-proposal-vote"
4243
, "--proposal-filepath", updateProposalFp
4344
, "--testnet-magic", show testnetMagic
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE OverloadedStrings #-}
3+
{-# LANGUAGE ScopedTypeVariables #-}
4+
5+
{-# OPTIONS_GHC -Wno-unused-local-binds -Wno-unused-matches #-}
6+
7+
module Testnet.Options
8+
( BabbageTestnetOptions(..)
9+
, defaultTestnetOptions
10+
) where
11+
12+
import Prelude
13+
14+
import Testnet.Util.Runtime (NodeLoggingFormat (..))
15+
16+
{- HLINT ignore "Redundant flip" -}
17+
18+
data BabbageTestnetOptions = BabbageTestnetOptions
19+
{ babbageNumSpoNodes :: Int
20+
, babbageSlotDuration :: Int
21+
, babbageSecurityParam :: Int
22+
, babbageTotalBalance :: Int
23+
, babbageNodeLoggingFormat :: NodeLoggingFormat
24+
} deriving (Eq, Show)
25+
26+
defaultTestnetOptions :: BabbageTestnetOptions
27+
defaultTestnetOptions = BabbageTestnetOptions
28+
{ babbageNumSpoNodes = 3
29+
, babbageSlotDuration = 200
30+
, babbageSecurityParam = 10
31+
, babbageTotalBalance = 10020000000
32+
, babbageNodeLoggingFormat = NodeLoggingFormatAsJson
33+
}

0 commit comments

Comments
 (0)