Skip to content

Commit

Permalink
Refactor: Use KeyPair type for key pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
carbolymer committed May 7, 2024
1 parent 569c903 commit cd71efa
Show file tree
Hide file tree
Showing 35 changed files with 660 additions and 689 deletions.
25 changes: 12 additions & 13 deletions cardano-testnet/cardano-testnet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,25 @@ library
hs-source-dirs: src
exposed-modules: Cardano.Testnet
Parsers.Run
Testnet.Start.Byron
Testnet.Start.Types
Testnet.Components.Configuration
Testnet.Components.DReps
Testnet.Components.SPO
Testnet.Components.Query
Testnet.Components.SPO
Testnet.Components.TestWatchdog
Testnet.Defaults
Testnet.Filepath
Testnet.EpochStateProcessing
Testnet.Filepath
Testnet.Ping
Testnet.Process.Cli
Testnet.Process.Run
Testnet.Property.Assert
Testnet.Property.Run
Testnet.Property.Utils
Testnet.Process.Cli
Testnet.Process.Run
Testnet.Runtime
Testnet.Start.Byron
Testnet.Start.Types
Testnet.SubmitApi
Testnet.Ping
Testnet.Types

other-modules: Parsers.Cardano
Parsers.Help
Expand Down Expand Up @@ -185,19 +186,17 @@ test-suite cardano-testnet-test
Cardano.Testnet.Test.Cli.Queries
Cardano.Testnet.Test.Cli.QuerySlotNumber
Cardano.Testnet.Test.FoldBlocks
Cardano.Testnet.Test.Misc

Cardano.Testnet.Test.Gov.DRepActivity
Cardano.Testnet.Test.Gov.DRepDeposits
Cardano.Testnet.Test.Gov.DRepRetirement
Cardano.Testnet.Test.Gov.InfoAction
Cardano.Testnet.Test.Gov.ProposeNewConstitution
Cardano.Testnet.Test.Gov.ProposeNewConstitutionSPO
Cardano.Testnet.Test.Gov.TreasuryWithdrawal
Cardano.Testnet.Test.Gov.DRepActivity
Cardano.Testnet.Test.Gov.TreasuryGrowth
Cardano.Testnet.Test.SanityCheck

Cardano.Testnet.Test.Gov.TreasuryWithdrawal
Cardano.Testnet.Test.Misc
Cardano.Testnet.Test.Node.Shutdown
Cardano.Testnet.Test.SanityCheck
Cardano.Testnet.Test.SubmitApi.Babbage.Transaction

type: exitcode-stdio-1.0
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 @@ -48,6 +48,6 @@ import Testnet.EpochStateProcessing
import Testnet.Filepath
import Testnet.Process.Run (procChairman)
import Testnet.Property.Utils
import Testnet.Runtime
import Testnet.Start.Cardano
import Testnet.Start.Types
import Testnet.Types
4 changes: 2 additions & 2 deletions cardano-testnet/src/Parsers/Cardano.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import qualified Data.List as L
import Options.Applicative
import qualified Options.Applicative as OA

import Testnet.Process.Cli
import Testnet.Process.Cli (pNetworkId)
import Testnet.Property.Utils
import Testnet.Runtime (readNodeLoggingFormat)
import Testnet.Start.Cardano
import Testnet.Start.Types
import Testnet.Types (readNodeLoggingFormat)


optsTestnet :: EnvCli -> Parser CardanoTestnetOptions
Expand Down
130 changes: 82 additions & 48 deletions cardano-testnet/src/Testnet/Components/DReps.hs

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions cardano-testnet/src/Testnet/Components/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ module Testnet.Components.Query
, findUtxosWithAddress
, findLargestUtxoWithAddress
, findLargestUtxoForPaymentKey
, startLedgerNewEpochStateLogging
) where

import Cardano.Api as Api
Expand Down Expand Up @@ -54,7 +53,7 @@ import Lens.Micro (to, (^.))

import Testnet.Property.Assert
import Testnet.Property.Utils (runInBackground)
import Testnet.Runtime
import Testnet.Types

import qualified Hedgehog as H
import Hedgehog.Extras (MonadAssertion)
Expand Down
18 changes: 9 additions & 9 deletions cardano-testnet/src/Testnet/Components/SPO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ import System.FilePath.Posix ((</>))

import Testnet.Components.DReps (VoteFile)
import Testnet.Filepath
import Testnet.Process.Cli hiding (File, unFile)
import Testnet.Process.Cli
import qualified Testnet.Process.Run as H
import Testnet.Process.Run (execCli, execCli', execCli_)
import Testnet.Property.Utils
import Testnet.Runtime (PoolNodeKeys (poolNodeKeysColdVkey))
import Testnet.Start.Types
import Testnet.Types

import Hedgehog
import Hedgehog.Extras (ExecConfig)
Expand Down Expand Up @@ -283,8 +283,8 @@ registerSingleSpo identifier tap@(TmpAbsolutePath tempAbsPath') nodeConfigFile s
let poolOwnerstakeVkeyFp = spoReqDir </> "pool-owner-stake.vkey"
poolOwnerstakeSKeyFp = spoReqDir </> "pool-owner-stake.skey"

_ <- cliStakeAddressKeyGen tempAbsPath'
$ KeyNames poolOwnerstakeVkeyFp poolOwnerstakeSKeyFp
cliStakeAddressKeyGen
$ KeyPair (File poolOwnerstakeVkeyFp) (File poolOwnerstakeSKeyFp)

poolownerstakeaddr <- filter (/= '\n')
<$> execCli
Expand All @@ -296,8 +296,8 @@ registerSingleSpo identifier tap@(TmpAbsolutePath tempAbsPath') nodeConfigFile s
-- 2. Generate stake pool owner payment key pair
let poolOwnerPaymentVkeyFp = spoReqDir </> "pool-owner-payment.vkey"
poolOwnerPaymentSkeyFp = spoReqDir </> "pool-owner-payment.skey"
_ <- cliAddressKeyGen tempAbsPath'
$ KeyNames poolOwnerPaymentVkeyFp poolOwnerPaymentSkeyFp
cliAddressKeyGen
$ KeyPair (File poolOwnerPaymentVkeyFp) (File poolOwnerPaymentSkeyFp)

poolowneraddresswstakecred <- execCli [ "address", "build"
, "--payment-verification-key-file", poolOwnerPaymentVkeyFp
Expand All @@ -319,8 +319,8 @@ registerSingleSpo identifier tap@(TmpAbsolutePath tempAbsPath') nodeConfigFile s
-- 4. Generate VRF keys
let vrfVkeyFp = spoReqDir </> "pool-vrf.vkey"
vrfSkeyFp = spoReqDir </> "pool-vrf.skey"
_ <- cliNodeKeyGenVrf tempAbsPath'
$ KeyNames vrfVkeyFp vrfSkeyFp
cliNodeKeyGenVrf
$ KeyPair (File vrfVkeyFp) (File vrfSkeyFp)

-- 5. Create registration certificate
let poolRegCertFp = spoReqDir </> "registration.cert"
Expand Down Expand Up @@ -433,7 +433,7 @@ generateVoteFiles ceo execConfig work prefix governanceActionTxId governanceActi
, "--" ++ vote
, "--governance-action-tx-id", governanceActionTxId
, "--governance-action-index", show @Word32 governanceActionIndex
, "--cold-verification-key-file", poolNodeKeysColdVkey spoKeys
, "--cold-verification-key-file", verificationKeyFp $ poolNodeKeysCold spoKeys
, "--out-file", unFile path
]
return path
94 changes: 39 additions & 55 deletions cardano-testnet/src/Testnet/Defaults.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,10 @@ module Testnet.Defaults
, defaultByronProtocolParamsJsonValue
, defaultYamlConfig
, defaultConwayGenesis
, defaultDRepVkeyFp
, defaultDRepSkeyFp
, defaultDRepKeyPair
, defaultDelegatorStakeKeyPair
, defaultSPOKeys
, defaultSPOColdKeyPair
, defaultSPOColdVKeyFp
, defaultSPOColdSKeyFp
, defaultSpoKeys
, defaultSpoColdKeyPair
, defaultShelleyGenesis
, defaultGenesisFilepath
, defaultYamlHardforkViaConfig
Expand All @@ -30,7 +26,7 @@ module Testnet.Defaults
, plutusV3SpendingScript
) where

import Cardano.Api (AnyCardanoEra (..), CardanoEra (..), pshow)
import Cardano.Api (AnyCardanoEra (..), CardanoEra (..), File (..), pshow)
import qualified Cardano.Api.Shelley as Api

import Cardano.Ledger.Alonzo.Core (PParams (..))
Expand Down Expand Up @@ -76,9 +72,8 @@ import Numeric.Natural
import System.FilePath ((</>))

import Test.Cardano.Ledger.Core.Rational
import Testnet.Runtime (PaymentKeyPair (PaymentKeyPair), PoolNodeKeys (..),
SPOColdKeyPair (..), StakingKeyPair (StakingKeyPair))
import Testnet.Start.Types
import Testnet.Types

{- HLINT ignore "Use underscore" -}

Expand Down Expand Up @@ -505,60 +500,49 @@ defaultGenesisFilepath era =
eraToString era <> "-genesis.json"

-- | The relative path to DRep keys in directories created by cardano-testnet
defaultDRepVkeyFp
defaultDRepKeyPair
:: Int -- ^ The DRep's index (starts at 1)
-> FilePath
defaultDRepVkeyFp n = "drep-keys" </> ("drep" <> show n) </> "drep.vkey"

-- | The relative path to DRep secret keys in directories created by cardano-testnet
defaultDRepSkeyFp
:: Int -- ^ The DRep's index (starts at 1)
-> FilePath
defaultDRepSkeyFp n = "drep-keys" </> ("drep" <> show n) </> "drep.skey"

-- | The relative path to DRep key pairs in directories created by cardano-testnet
defaultDRepKeyPair :: Int -> PaymentKeyPair
defaultDRepKeyPair n = PaymentKeyPair (defaultDRepVkeyFp n) (defaultDRepSkeyFp n)

-- | The relative path to SPO cold verification key in directories created by cardano-testnet
defaultSPOColdVKeyFp :: Int -> FilePath
defaultSPOColdVKeyFp n = "pools-keys" </> "pool" <> show n </> "cold.vkey"

-- | The relative path to SPO cold secret key in directories created by cardano-testnet
defaultSPOColdSKeyFp :: Int -> FilePath
defaultSPOColdSKeyFp n = "pools-keys" </> "pool" <> show n </> "cold.skey"
-> KeyPair PaymentKey
defaultDRepKeyPair n =
KeyPair
{ verificationKey = File $ "drep-keys" </> ("drep" <> show n) </> "drep.vkey"
, signingKey = File $ "drep-keys" </> ("drep" <> show n) </> "drep.skey"
}

-- | The relative path to SPO key cold key pair in directories created by cardano-testnet
defaultSPOColdKeyPair :: Int -> SPOColdKeyPair
defaultSPOColdKeyPair n = SPOColdKeyPair (defaultSPOColdVKeyFp n) (defaultSPOColdSKeyFp n)
-- | The relative path to SPO keys in directories created by cardano-testnet
defaultSpoColdKeyPair
:: Int
-> KeyPair SpoColdKey
defaultSpoColdKeyPair n =
KeyPair
{ verificationKey = File $ "pools-keys" </> "pool" <> show n </> "cold.vkey"
, signingKey = File $ "pools-keys" </> "pool" <> show n </> "cold.skey"
}

-- | The relative path to SPO key pairs in directories created by cardano-testnet
defaultSPOKeys :: Int -> PoolNodeKeys
defaultSPOKeys n =
defaultSpoKeys :: Int -> PoolNodeKeys
defaultSpoKeys n =
PoolNodeKeys
{ poolNodeKeysColdVkey = defaultSPOColdVKeyFp n
, poolNodeKeysColdSkey = defaultSPOColdSKeyFp n
, poolNodeKeysVrfVkey = "pools-keys" </> "pool" ++ show n </> "vrf.vkey"
, poolNodeKeysVrfSkey = "pools-keys" </> "pool" ++ show n </> "vrf.skey"
, poolNodeKeysStakingVkey = "pools-keys" </> "pool" ++ show n </> "staking-reward.vkey"
, poolNodeKeysStakingSkey = "pools-keys" </> "pool" ++ show n </> "staking-reward.skey"
{ poolNodeKeysCold = defaultSpoColdKeyPair n
, poolNodeKeysVrf =
KeyPair
{ verificationKey = File $ "pools-keys" </> "pool" ++ show n </> "vrf.vkey"
, signingKey = File $ "pools-keys" </> "pool" ++ show n </> "vrf.skey"
}
, poolNodeKeysStaking =
KeyPair
{ verificationKey = File $ "pools-keys" </> "pool" ++ show n </> "staking-reward.vkey"
, signingKey = File $ "pools-keys" </> "pool" ++ show n </> "staking-reward.skey"
}
}

-- | The relative path to stake delegator stake keys in directories created by cardano-testnet
defaultDelegatorStakeVkeyFp
:: Int -- ^ The Stake delegator index (starts at 1)
-> FilePath
defaultDelegatorStakeVkeyFp n = "stake-delegators" </> ("delegator" <> show n) </> "staking.vkey"

-- | The relative path to stake delegator stake secret keys in directories created by cardano-testnet
defaultDelegatorStakeSkeyFp
:: Int -- ^ The Stake delegator index (starts at 1)
-> FilePath
defaultDelegatorStakeSkeyFp n = "stake-delegators" </> ("delegator" <> show n) </> "staking.skey"

-- | The relative path to stake delegator key pairs in directories created by cardano-testnet
defaultDelegatorStakeKeyPair :: Int -> StakingKeyPair
defaultDelegatorStakeKeyPair n = StakingKeyPair (defaultDelegatorStakeVkeyFp n) (defaultDelegatorStakeSkeyFp n)
defaultDelegatorStakeKeyPair :: Int -> KeyPair StakingKey
defaultDelegatorStakeKeyPair n =
KeyPair
{ verificationKey = File $ "stake-delegators" </> ("delegator" <> show n) </> "staking.vkey"
, signingKey = File $ "stake-delegators" </> ("delegator" <> show n) </> "staking.skey"
}

-- TODO: We should not hardcode a script like this. We need to move
-- plutus-example from plutus apps to cardano-node-testnet. This will
Expand Down
45 changes: 22 additions & 23 deletions cardano-testnet/src/Testnet/EpochStateProcessing.hs
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}

module Testnet.EpochStateProcessing
( maybeExtractGovernanceActionIndex
, findCondition
) where

import Cardano.Api (AnyNewEpochState (..), ConwayEra, EpochNo, File (File),
FoldBlocksError, LedgerStateCondition (..), MonadIO, ShelleyBasedEra,
ValidationMode (FullValidation), foldEpochState, runExceptT,
shelleyBasedEraConstraints)
import qualified Cardano.Api as Api
import Cardano.Api
import Cardano.Api.Ledger (GovActionId (..))
import qualified Cardano.Api.Ledger as L

import qualified Cardano.Ledger.Conway.Governance as L
import Cardano.Ledger.Shelley.API (TxId (..))
import qualified Cardano.Ledger.Shelley.API as L
import qualified Cardano.Ledger.Shelley.LedgerState as L

import Prelude

import Control.Monad.State.Strict (MonadState (put), StateT)
import Data.Data ((:~:) (..))
import qualified Data.Map as Map
import Data.Type.Equality (TestEquality (..))
import Data.Word (Word32)
import GHC.Stack
import Lens.Micro ((^.))

import Testnet.Property.Assert (assertErasEqual)

import Hedgehog

findCondition
:: HasCallStack
=> MonadTest m
=> MonadIO m
=> (AnyNewEpochState -> Maybe a)
-> FilePath
-> FilePath
-> NodeConfigFile In
-> SocketPath
-> EpochNo -- ^ The termination epoch: the condition must be found *before* this epoch
-> m (Either FoldBlocksError (Maybe a))
findCondition epochStateFoldFunc configurationFile socketPath maxEpochNo = withFrozenCallStack $ evalIO . runExceptT $ do
result <-
foldEpochState
(File configurationFile)
(File socketPath)
configurationFile
socketPath
FullValidation
maxEpochNo
Nothing
Expand All @@ -58,20 +58,19 @@ findCondition epochStateFoldFunc configurationFile socketPath maxEpochNo = withF
Just x -> put (Just x) >> pure ConditionMet
Nothing -> pure ConditionNotMet

maybeExtractGovernanceActionIndex :: ShelleyBasedEra ConwayEra -- ^ The era in which the test runs
-> Api.TxId
maybeExtractGovernanceActionIndex
:: forall era. HasCallStack
=> ConwayEraOnwards era -- ^ The era in which the test runs
-> TxId -- ^ transaction id searched for
-> AnyNewEpochState
-> Maybe Word32
maybeExtractGovernanceActionIndex sbe txid (AnyNewEpochState actualEra newEpochState) =
case testEquality sbe actualEra of
Just Refl -> do
let proposals = shelleyBasedEraConstraints sbe newEpochState
^. L.newEpochStateGovStateL
. L.proposalsGovStateL
Map.foldlWithKey' (compareWithTxId txid) Nothing (L.proposalsActionsMap proposals)
Nothing -> do
error $ "Eras mismatch! expected: " <> show sbe <> ", actual: " <> show actualEra
maybeExtractGovernanceActionIndex ceo txid (AnyNewEpochState actualEra newEpochState) = conwayEraOnwardsConstraints ceo $ do
let sbe = conwayEraOnwardsToShelleyBasedEra ceo
Refl <- either error pure $ assertErasEqual sbe actualEra
let proposals = newEpochState ^. L.newEpochStateGovStateL . L.proposalsGovStateL
Map.foldlWithKey' (compareWithTxId txid) Nothing (L.proposalsActionsMap proposals)
where
compareWithTxId (Api.TxId ti1) Nothing (GovActionId (TxId ti2) (L.GovActionIx gai)) _
compareWithTxId (TxId ti1) Nothing (GovActionId (L.TxId ti2) (L.GovActionIx gai)) _
| ti1 == L.extractHash ti2 = Just gai
compareWithTxId _ x _ _ = x

0 comments on commit cd71efa

Please sign in to comment.