Skip to content

Commit

Permalink
Remove previousProposalInfo parameter from `activityChangeProposalT…
Browse files Browse the repository at this point in the history
…est`
  • Loading branch information
palas committed Apr 25, 2024
1 parent 6bcc25b commit 00b385e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 24 deletions.
52 changes: 49 additions & 3 deletions cardano-testnet/src/Testnet/Components/DReps.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}

module Testnet.Components.DReps
Expand All @@ -17,23 +18,28 @@ module Testnet.Components.DReps
, retrieveTransactionId
, registerDRep
, delegateToDRep
, getLastPParamUpdateActionId
) where

import Cardano.Api (AnyCardanoEra (..), ConwayEra, EpochNo (EpochNo), FileDirection (In),
ShelleyBasedEra (..), ToCardanoEra (toCardanoEra), renderTxIn)
MonadIO, ShelleyBasedEra (..), ToCardanoEra (toCardanoEra), renderTxIn)

import Cardano.CLI.Types.Common (File (..))

import Prelude

import Control.Monad (forM, void)
import Control.Monad.Catch (MonadCatch)
import Control.Monad.IO.Class (MonadIO)
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Lens as AL
import qualified Data.ByteString.Lazy.Char8 as LBS
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Word (Word32)
import GHC.IO.Exception (ExitCode (..))
import GHC.Stack (HasCallStack)
import qualified GHC.Stack as GHC
import Lens.Micro ((^?))
import System.FilePath ((</>))

import Testnet.Components.Query (EpochStateView, findLargestUtxoForPaymentKey,
Expand All @@ -43,7 +49,7 @@ import Testnet.Runtime (PaymentKeyInfo (paymentKeyInfoAddr, paymentKey
PaymentKeyPair (..), StakingKeyPair (StakingKeyPair, stakingSKey))
import Testnet.Start.Types (anyEraToString)

import Hedgehog (MonadTest)
import Hedgehog (MonadTest, evalMaybe)
import qualified Hedgehog.Extras as H

-- | Generates a key pair for a decentralized representative (DRep) using @cardano-cli@.
Expand Down Expand Up @@ -455,3 +461,43 @@ delegateToDRep execConfig epochStateView configurationFile socketPath sbe work p
-- Wait two epochs
(EpochNo epochAfterProp) <- getCurrentEpochNo epochStateView sbe
void $ waitUntilEpoch (File configurationFile) (File socketPath) (EpochNo (epochAfterProp + 2))

-- | This function obtains the identifier for the last enacted parameter update proposal
-- if any.
--
-- This function takes the following parameter:
--
-- * 'execConfig': Specifies the CLI execution configuration.
--
-- If no previous proposal was enacted, the function returns 'Nothing'.
-- If there was a previous enacted proposal, the function returns a tuple with its transaction
-- identifier (as a 'String') and the action index (as a 'Word32').
getLastPParamUpdateActionId :: (MonadTest m, MonadCatch m, MonadIO m) => H.ExecConfig -> m (Maybe (String, Word32))
getLastPParamUpdateActionId execConfig = do
govStateString <- H.execCli' execConfig
[ "conway", "query", "gov-state"
, "--volatile-tip"
]

govStateJSON <- H.nothingFail (Aeson.decode (LBS.pack govStateString) :: Maybe Aeson.Value)
let mLastPParamUpdateActionId :: Maybe Aeson.Value
mLastPParamUpdateActionId = govStateJSON
^? AL.key "nextRatifyState"
. AL.key "nextEnactState"
. AL.key "prevGovActionIds"
. AL.key "PParamUpdate"
lastPParamUpdateActionId <- evalMaybe mLastPParamUpdateActionId

if lastPParamUpdateActionId == Aeson.Null
then return Nothing
else do let mActionIx :: Maybe Integer
mActionIx = lastPParamUpdateActionId
^? AL.key "govActionIx"
. AL._Integer
mTxId :: Maybe Text
mTxId = lastPParamUpdateActionId
^? AL.key "txId"
. AL._String
actionIx <- evalMaybe mActionIx
txId <- evalMaybe mTxId
return (Just (Text.unpack txId, fromIntegral actionIx))
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import Lens.Micro ((^?))
import System.FilePath ((</>))

import Testnet.Components.DReps (createVotingTxBody, delegateToDRep, generateVoteFiles,
registerDRep, retrieveTransactionId, signTx, submitTx)
getLastPParamUpdateActionId, registerDRep, retrieveTransactionId, signTx,
submitTx)
import Testnet.Components.Query (EpochStateView, checkDRepState,
findLargestUtxoForPaymentKey, getCurrentEpochNo, getEpochStateView,
getMinDRepDeposit)
Expand Down Expand Up @@ -92,13 +93,9 @@ hprop_check_drep_activity = H.integrationWorkspace "test-activity" $ \tempAbsBas

gov <- H.createDirectoryIfMissing $ work </> "governance"

-- First proposal (set activity to something feasible in the test)
dRepActivityBeforeFirstProp <- getDRepActivityValue execConfig
dRepActivityBeforeFirstProp === 100 -- This is the default value

-- This proposal should pass
firstProposalInfo <- activityChangeProposalTest execConfig epochStateView configurationFile socketPath sbe gov
"firstProposal" wallet0 Nothing [(1, "yes")] 6 (Just 6) 3
void $ activityChangeProposalTest execConfig epochStateView configurationFile socketPath sbe gov
"firstProposal" wallet0 [(1, "yes")] 3 (Just 3) 3

-- Now we register two new DReps
drep2 <- registerDRep execConfig epochStateView sbe work "drep2" wallet1
Expand All @@ -113,24 +110,27 @@ hprop_check_drep_activity = H.integrationWorkspace "test-activity" $ \tempAbsBas
(\m -> if length m == 3 then Just $ Map.map drepExpiry m else Nothing)
H.note_ $ "Expiration dates for the registered DReps: " ++ show expirationDates

-- Proposals before expiration (set activity to something else and it should fail).
-- We send three because DRep won't expire if there is not enough activity
-- (opportunites to participate). This is accounted for by the dormant epoch count
void $ activityChangeProposalTest execConfig epochStateView configurationFile socketPath sbe gov
"secondProposal" wallet2 (Just firstProposalInfo) [(1, "yes")] 7 (Just 6) 3
void $ activityChangeProposalTest execConfig epochStateView configurationFile socketPath sbe gov
"thirdProposal" wallet0 (Just firstProposalInfo) [(1, "yes")] 7 (Just 6) 3
-- This proposal should fail because there is 2 DReps that don't vote (out of 3)
-- and we have the stake distributed evenly
void $ activityChangeProposalTest execConfig epochStateView configurationFile socketPath sbe gov
"fourthProposal" wallet1 (Just firstProposalInfo) [(1, "yes")] 7 (Just 6) 3
"failingProposal" wallet2 [(1, "yes")] 4 (Just 3) 3

-- We now send a bunch of proposals to make sure that the 2 new DReps expire.
-- because DReps won't expire if there is not enough activity (opportunites to participate).
-- This is accounted for by the dormant epoch count
sequence_
[activityChangeProposalTest execConfig epochStateView configurationFile socketPath sbe gov
("fillerProposalNum" ++ show proposalNum) wallet [(1, "yes")]
(fromIntegral $ 4 + proposalNum) Nothing 3
| (proposalNum, wallet) <- zip [1..(4 :: Int)] (cycle [wallet0, wallet1, wallet2])]

(EpochNo epochAfterTimeout) <- getCurrentEpochNo epochStateView sbe
H.note_ $ "Epoch after which we are going to test timeout: " <> show epochAfterTimeout

-- Last proposal (set activity to something else again and it should pass, because of inactivity)
-- Because 2 out of 3 DReps were inactive, prop should pass
void $ activityChangeProposalTest execConfig epochStateView configurationFile socketPath sbe gov
"lastProposal" wallet2 (Just firstProposalInfo) [(1, "yes")] 8 (Just 8) 3

"lastProposal" wallet0 [(1, "yes")] 9 (Just 9) 3

activityChangeProposalTest
:: (MonadTest m, MonadIO m, H.MonadAssertion m, MonadCatch m, Foldable t)
Expand All @@ -142,17 +142,16 @@ activityChangeProposalTest
-> FilePath
-> FilePath
-> PaymentKeyInfo
-> Maybe (String, Word32)
-> t (Int, String)
-> Word32
-> Maybe Integer
-> Word64
-> m (String, Word32)
activityChangeProposalTest execConfig epochStateView configurationFile socketPath sbe work prefix
wallet previousProposalInfo votes change mExpected epochsToWait = do
wallet votes change mExpected epochsToWait = do
mPreviousProposalInfo <- getLastPParamUpdateActionId execConfig

baseDir <- H.createDirectoryIfMissing $ work </> prefix

let propVotes :: [(String, Int)]
propVotes = zip (concatMap (uncurry replicate) votes) [1..]
annotateShow propVotes
Expand All @@ -162,7 +161,7 @@ activityChangeProposalTest execConfig epochStateView configurationFile socketPat

thisProposal@(governanceActionTxId, governanceActionIndex) <-
makeActivityChangeProposal execConfig epochStateView (File configurationFile) (File socketPath)
sbe baseDir "proposal" previousProposalInfo change wallet
sbe baseDir "proposal" mPreviousProposalInfo change wallet

voteChangeProposal execConfig epochStateView sbe baseDir "vote"
governanceActionTxId governanceActionIndex propVotes wallet
Expand Down

0 comments on commit 00b385e

Please sign in to comment.