Skip to content

Commit

Permalink
Finish implementation of Predefined No Confidence DRep test
Browse files Browse the repository at this point in the history
  • Loading branch information
palas committed Apr 29, 2024
1 parent 3af71f3 commit 5bef651
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 73 deletions.
52 changes: 49 additions & 3 deletions cardano-testnet/src/Testnet/Components/SPO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Testnet.Components.SPO
, createStakeKeyDeregistrationCertificate
, decodeEraUTxO
, registerSingleSpo
, generateVoteFiles
) where

import qualified Cardano.Api.Ledger as L
Expand All @@ -32,21 +33,24 @@ import qualified Data.Map.Strict as Map
import Data.Set (Set)
import qualified Data.Set as Set
import qualified Data.Text as Text
import Data.Word (Word32)
import GHC.Stack (HasCallStack)
import qualified GHC.Stack as GHC
import Lens.Micro
import System.FilePath.Posix ((</>))

import Testnet.Components.DReps (VoteFile)
import Testnet.Filepath
import Testnet.Process.Cli
import Testnet.Process.Cli hiding (File, unFile)
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 Hedgehog
import Hedgehog.Extras (ExecConfig)
import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.File as H
import qualified Hedgehog.Extras as H

checkStakePoolRegistered
:: (MonadTest m, MonadCatch m, MonadIO m, HasCallStack)
Expand Down Expand Up @@ -401,3 +405,45 @@ registerSingleSpo identifier tap@(TmpAbsolutePath tempAbsPath') nodeConfigFile s
poolColdVkeyFp
currentRegistedPoolsJson
return (poolId, poolColdSkeyFp, poolColdVkeyFp, vrfSkeyFp, vrfVkeyFp)


-- | Generates Stake Pool Operator (SPO) voting files (without signing)
-- using @cardano-cli@.
--
-- This function takes the following parameters:
--
-- * 'execConfig': Specifies the CLI execution configuration.
-- * 'work': Base directory path where the voting files and directories will be
-- stored.
-- * 'prefix': Name for the subfolder that will be created under 'work' to store
-- the output voting files.
-- * 'governanceActionTxId': Transaction ID string of the governance action.
-- * 'governanceActionIndex': Index of the governance action.
-- * 'allVotes': List of tuples where each tuple contains a 'PoolNodeKeys'
-- representing the SPO keys and a 'String' representing the
-- vote type (i.e: "yes", "no", or "abstain").
--
-- Returns a list of generated @File VoteFile In@ representing the paths to
-- the generated voting files.
generateVoteFiles :: (MonadTest m, MonadIO m, MonadCatch m)
=> H.ExecConfig
-> FilePath
-> String
-> String
-> Word32
-> [(PoolNodeKeys, [Char])]
-> m [File VoteFile In]
generateVoteFiles execConfig work prefix governanceActionTxId governanceActionIndex allVotes = do
baseDir <- H.createDirectoryIfMissing $ work </> prefix
forM (zip [(1 :: Integer)..] allVotes) $ \(idx, (spoKeys, vote)) -> do
let path = File (baseDir </> "vote-" <> show idx)
void $ H.execCli' execConfig
[ "conway", "governance", "vote", "create"
, "--" ++ vote
, "--governance-action-tx-id", governanceActionTxId
, "--governance-action-index", show @Word32 governanceActionIndex
, "--cold-verification-key-file", poolNodeKeysColdVkey spoKeys
, "--out-file", unFile path
]
return path

20 changes: 17 additions & 3 deletions cardano-testnet/src/Testnet/Defaults.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Testnet.Defaults
, defaultMainnetTopology
, plutusV3NonSpendingScript
, plutusV3SpendingScript
, defaultSPOKeys
) where

import Cardano.Api (AnyCardanoEra (..), CardanoEra (..), pshow)
Expand Down Expand Up @@ -72,7 +73,8 @@ import Numeric.Natural
import System.FilePath ((</>))

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

{- HLINT ignore "Use underscore" -}
Expand Down Expand Up @@ -515,15 +517,27 @@ defaultDRepSkeyFp n = "drep-keys" </> ("drep" <> show n) </> "drep.skey"
defaultDRepKeyPair :: Int -> PaymentKeyPair
defaultDRepKeyPair n = PaymentKeyPair (defaultDRepVkeyFp n) (defaultDRepSkeyFp n)

-- | The relative path to SPO key pairs in directories created by cardano-testnet
defaultSPOKeys :: Int -> PoolNodeKeys
defaultSPOKeys n =
PoolNodeKeys
{ poolNodeKeysColdVkey = "pools-keys" </> "pool" ++ show n </> "cold.vkey"
, poolNodeKeysColdSkey = "pools-keys" </> "pool" ++ show n </> "cold.skey"
, 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"
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ import GHC.Stack (callStack)
import Lens.Micro ((^?))
import System.FilePath ((</>))

import qualified Testnet.Components.DReps as DRep
import Testnet.Components.DReps (createCertificatePublicationTxBody, createVotingTxBody,
generateVoteFiles, retrieveTransactionId, signTx, submitTx)
retrieveTransactionId, signTx, submitTx)
import Testnet.Components.Query (EpochStateView, findLargestUtxoForPaymentKey,
getCurrentEpochNo, getEpochStateView, getMinDRepDeposit)
import Testnet.Defaults (defaultDRepKeyPair, defaultDelegatorStakeKeyPair)
import qualified Testnet.Components.SPO as SPO
import Testnet.Defaults (defaultDRepKeyPair, defaultDelegatorStakeKeyPair, defaultSPOKeys)
import qualified Testnet.Process.Cli as P
import qualified Testnet.Process.Run as H
import qualified Testnet.Property.Utils as H
Expand Down Expand Up @@ -209,7 +211,7 @@ desiredPoolNumberProposalTest execConfig epochStateView configurationFile socket
ceo baseDir "proposal" previousProposalInfo (fromIntegral change) wallet

voteChangeProposal execConfig epochStateView sbe baseDir "vote"
governanceActionTxId governanceActionIndex propVotes wallet
governanceActionTxId governanceActionIndex propVotes [] wallet

(EpochNo epochAfterProp) <- getCurrentEpochNo epochStateView
H.note_ $ "Epoch after \"" <> prefix <> "\" prop: " <> show epochAfterProp
Expand Down Expand Up @@ -319,26 +321,42 @@ voteChangeProposal :: (MonadTest m, MonadIO m, MonadCatch m, H.MonadAssertion m)
-> FilePath
-> String
-> Word32
-> [([Char], Int)]
-> [(String, Int)]
-> [(String, Int)]
-> PaymentKeyInfo
-> m ()
voteChangeProposal execConfig epochStateView sbe work prefix governanceActionTxId governanceActionIndex votes wallet = do
voteChangeProposal execConfig epochStateView sbe work prefix governanceActionTxId governanceActionIndex drepVotes spoVotes wallet = do
baseDir <- H.createDirectoryIfMissing $ work </> prefix

let era = toCardanoEra sbe
cEra = AnyCardanoEra era

voteFiles <- generateVoteFiles execConfig baseDir "vote-files"
governanceActionTxId governanceActionIndex
[(defaultDRepKeyPair idx, vote) | (vote, idx) <- votes]
drepVoteFiles <- DRep.generateVoteFiles execConfig baseDir "drep-vote-files"
governanceActionTxId governanceActionIndex
[(defaultDRepKeyPair idx, vote) | (vote, idx) <- drepVotes]

spoVoteFiles <- SPO.generateVoteFiles execConfig baseDir "spo-vote-files"
governanceActionTxId governanceActionIndex
[(defaultSPOKeys idx, vote) | (vote, idx) <- spoVotes]

let voteFiles = drepVoteFiles ++ spoVoteFiles

voteTxBodyFp <- createVotingTxBody execConfig epochStateView sbe baseDir "vote-tx-body"
voteFiles wallet

voteTxFp <- signTx execConfig cEra baseDir "signed-vote-tx" voteTxBodyFp
(paymentKeyInfoPair wallet:[defaultDRepKeyPair n | (_, n) <- votes])
(paymentKeyInfoPair wallet:
[defaultDRepKeyPair n | (_, n) <- drepVotes] ++
[defaultSPOColdKeyPair n | (_, n) <- drepVotes]
)
submitTx execConfig cEra voteTxFp

defaultSPOColdKeyPair :: Int -> PaymentKeyPair
defaultSPOColdKeyPair n = PaymentKeyPair { paymentVKey = poolNodeKeysColdVkey spoKeys
, paymentSKey = poolNodeKeysColdSkey spoKeys
}
where spoKeys = defaultSPOKeys n

getDesiredPoolNumberValue :: (MonadTest m, MonadCatch m, MonadIO m) => H.ExecConfig -> m Integer
getDesiredPoolNumberValue execConfig = do
govStateString <- H.execCli' execConfig
Expand Down

0 comments on commit 5bef651

Please sign in to comment.