Skip to content

Commit c039faf

Browse files
authored
Merge pull request #4831 from input-output-hk/jordan/refactor-genesis-create-cardano-testnet
Refactor `genesis create` command usage in cardano testnet
2 parents daeae61 + f2e4dd5 commit c039faf

File tree

11 files changed

+223
-138
lines changed

11 files changed

+223
-138
lines changed

Diff for: cardano-testnet/cardano-testnet.cabal

+3-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ library
7979
Testnet.Babbage
8080
Testnet.Cardano
8181
Testnet.Conf
82-
Testnet.Genesis
82+
Testnet.Commands.Genesis
83+
Testnet.Commands.Governance
84+
Testnet.Options
8385
Testnet.Run
8486
Testnet.Shelley
8587
Testnet.Utils

Diff for: 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

Diff for: 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

Diff for: 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

Diff for: cardano-testnet/src/Testnet/Babbage.hs

+13-39
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

31+
import Testnet.Commands.Genesis
3432
import qualified Testnet.Conf as H
35-
import Testnet.Genesis
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 = 1000
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

Diff for: cardano-testnet/src/Testnet/Cardano.hs

+24-49
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ import qualified Hedgehog.Extras.Test.Concurrent as H
6363
import qualified Hedgehog.Extras.Test.File as H
6464
import qualified Hedgehog.Extras.Test.Network as H
6565

66+
import Testnet.Commands.Genesis
67+
import Testnet.Commands.Governance
6668
import qualified Testnet.Conf as H
67-
import Testnet.Genesis
6869
import qualified Testnet.Util.Assert as H
6970
import qualified Testnet.Util.Process as H
7071
import Testnet.Util.Process (execCli_)
@@ -347,53 +348,31 @@ cardanoTestnet testnetOptions H.Conf {..} = do
347348
]
348349

349350
-- Update Proposal and votes
350-
execCli_
351-
[ "byron", "governance", "create-update-proposal"
352-
, "--filepath", tempAbsPath </> "update-proposal"
353-
, "--testnet-magic", show @Int testnetMagic
354-
, "--signing-key", tempAbsPath </> "byron/delegate-keys.000.key"
355-
, "--protocol-version-major", "1"
356-
, "--protocol-version-minor", "0"
357-
, "--protocol-version-alt", "0"
358-
, "--application-name", "cardano-sl"
359-
, "--software-version-num", "1"
360-
, "--system-tag", "linux"
361-
, "--installer-hash", "0"
362-
]
351+
createByronUpdateProposal
352+
testnetMagic
353+
(tempAbsPath </> "byron/delegate-keys.000.key")
354+
(tempAbsPath </> "update-proposal")
355+
1
363356

364357
forM_ bftNodesN $ \n -> do
365-
execCli_
366-
[ "byron", "governance", "create-proposal-vote"
367-
, "--proposal-filepath", tempAbsPath </> "update-proposal"
368-
, "--testnet-magic", show @Int testnetMagic
369-
, "--signing-key", tempAbsPath </> "byron/delegate-keys.00" <> show @Int (n - 1) <> ".key"
370-
, "--vote-yes"
371-
, "--output-filepath", tempAbsPath </> "update-vote.00" <> show @Int (n - 1)
372-
]
373-
374-
execCli_
375-
[ "byron", "governance", "create-update-proposal"
376-
, "--filepath", tempAbsPath </> "update-proposal-1"
377-
, "--testnet-magic", show @Int testnetMagic
378-
, "--signing-key", tempAbsPath </> "byron/delegate-keys.000.key"
379-
, "--protocol-version-major", "2"
380-
, "--protocol-version-minor", "0"
381-
, "--protocol-version-alt", "0"
382-
, "--application-name", "cardano-sl"
383-
, "--software-version-num", "1"
384-
, "--system-tag", "linux"
385-
, "--installer-hash", "0"
386-
]
358+
createByronUpdateProposalVote
359+
testnetMagic
360+
(tempAbsPath </> "update-proposal")
361+
(tempAbsPath </> "byron/delegate-keys.00" <> show @Int (n - 1) <> ".key")
362+
(tempAbsPath </> "update-vote.00" <> show @Int (n - 1))
363+
364+
createByronUpdateProposal
365+
testnetMagic
366+
(tempAbsPath </> "byron/delegate-keys.000.key")
367+
(tempAbsPath </> "update-proposal-1")
368+
2
387369

388370
forM_ bftNodesN $ \n ->
389-
execCli_
390-
[ "byron", "governance", "create-proposal-vote"
391-
, "--proposal-filepath", tempAbsPath </> "update-proposal-1"
392-
, "--testnet-magic", show @Int testnetMagic
393-
, "--signing-key", tempAbsPath </> "byron/delegate-keys.00" <> show @Int (n - 1) <> ".key"
394-
, "--vote-yes"
395-
, "--output-filepath", tempAbsPath </> "update-vote-1.00" <> show @Int (n - 1)
396-
]
371+
createByronUpdateProposalVote
372+
testnetMagic
373+
(tempAbsPath </> "update-proposal-1")
374+
(tempAbsPath </> "byron/delegate-keys.00" <> show @Int (n - 1) <> ".key")
375+
(tempAbsPath </> "update-vote-1.00" <> show @Int (n - 1))
397376

398377
-- Generated genesis keys and genesis files
399378
H.noteEachM_ . H.listDirectory $ tempAbsPath </> "byron"
@@ -644,11 +623,7 @@ cardanoTestnet testnetOptions H.Conf {..} = do
644623
-- 2. register the stake pool 1
645624
-- 3. register the user1 stake address
646625
-- 4. delegate from the user1 stake address to the stake pool
647-
txIn <- H.noteShow . S.strip =<< H.execCli
648-
[ "genesis", "initial-txin"
649-
, "--testnet-magic", show @Int testnetMagic
650-
, "--verification-key-file", tempAbsPath </> "shelley/utxo-keys/utxo1.vkey"
651-
]
626+
txIn <- H.noteShow . S.strip =<< createShelleyGenesisInitialTxIn testnetMagic (tempAbsPath </> "shelley/utxo-keys/utxo1.vkey")
652627

653628
H.note_ txIn
654629

Diff for: cardano-testnet/src/Testnet/Commands/Genesis.hs

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE TypeApplications #-}
3+
4+
-- | All Byron and Shelley Genesis related functionality
5+
module Testnet.Commands.Genesis
6+
( createShelleyGenesisInitialTxIn
7+
, createByronGenesis
8+
, defaultByronGenesisJsonValue
9+
) where
10+
11+
import Prelude
12+
13+
import Control.Monad.Catch
14+
import Control.Monad.IO.Class
15+
import Data.Aeson
16+
import Data.Time.Clock (UTCTime)
17+
import GHC.Stack (HasCallStack, withFrozenCallStack)
18+
19+
import Hedgehog.Extras.Stock.Time (showUTCTimeSeconds)
20+
import Hedgehog.Internal.Property
21+
22+
import Testnet.Options
23+
import Testnet.Util.Process
24+
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+
]
64+
65+
-- | We need a Byron genesis in order to be able to hardfork to the later Shelley based eras.
66+
-- The values here don't matter as the testnet conditions are ultimately determined
67+
-- by the Shelley genesis.
68+
defaultByronGenesisJsonValue :: Value
69+
defaultByronGenesisJsonValue =
70+
object
71+
[ "heavyDelThd" .= toJSON @String "300000000000"
72+
, "maxBlockSize" .= toJSON @String "2000000"
73+
, "maxTxSize" .= toJSON @String "4096"
74+
, "maxHeaderSize" .= toJSON @String "2000000"
75+
, "maxProposalSize" .= toJSON @String "700"
76+
, "mpcThd" .= toJSON @String "20000000000000"
77+
, "scriptVersion" .= toJSON @Int 0
78+
, "slotDuration" .= toJSON @String "1000"
79+
, "softforkRule" .= object
80+
[ "initThd" .= toJSON @String "900000000000000"
81+
, "minThd" .= toJSON @String "600000000000000"
82+
, "thdDecrement" .= toJSON @String "50000000000000"
83+
]
84+
, "txFeePolicy" .= object
85+
[ "multiplier" .= toJSON @String "43946000000"
86+
, "summand" .= toJSON @String "155381000000000"
87+
]
88+
, "unlockStakeEpoch" .= toJSON @String "18446744073709551615"
89+
, "updateImplicit" .= toJSON @String "10000"
90+
, "updateProposalThd" .= toJSON @String "100000000000000"
91+
, "updateVoteThd" .= toJSON @String "1000000000000"
92+
]
93+

Diff for: cardano-testnet/src/Testnet/Commands/Governance.hs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module Testnet.Commands.Governance
2+
( createByronUpdateProposal
3+
, createByronUpdateProposalVote
4+
) where
5+
6+
import Prelude
7+
8+
import Control.Monad.Catch
9+
import Control.Monad.IO.Class
10+
import GHC.Stack (HasCallStack, withFrozenCallStack)
11+
12+
import Testnet.Util.Process (execCli_)
13+
14+
import Hedgehog.Internal.Property
15+
16+
17+
createByronUpdateProposal
18+
:: (MonadTest m, MonadCatch m, MonadIO m, HasCallStack)
19+
=> Int -> String -> String -> Int -> m ()
20+
createByronUpdateProposal testnetMagic signingKeyFp updateProposalFp ptclMajorVersion =
21+
withFrozenCallStack $ execCli_
22+
[ "byron", "governance", "create-update-proposal"
23+
, "--filepath", updateProposalFp
24+
, "--testnet-magic", show testnetMagic
25+
, "--signing-key", signingKeyFp
26+
, "--protocol-version-major", show ptclMajorVersion
27+
, "--protocol-version-minor", "0"
28+
, "--protocol-version-alt", "0"
29+
, "--application-name", "cardano-sl"
30+
, "--software-version-num", "1"
31+
, "--system-tag", "linux"
32+
, "--installer-hash", "0"
33+
]
34+
35+
createByronUpdateProposalVote
36+
:: (MonadTest m, MonadCatch m, MonadIO m, HasCallStack)
37+
=> Int -> String -> String -> String -> m ()
38+
createByronUpdateProposalVote testnetMagic updateProposalFp signingKey outputFp =
39+
withFrozenCallStack $ execCli_
40+
[ "byron", "governance", "create-proposal-vote"
41+
, "--proposal-filepath", updateProposalFp
42+
, "--testnet-magic", show testnetMagic
43+
, "--signing-key", signingKey
44+
, "--vote-yes"
45+
, "--output-filepath", outputFp
46+
]

0 commit comments

Comments
 (0)