Skip to content

Commit

Permalink
Test predefined always abstain DRep
Browse files Browse the repository at this point in the history
  • Loading branch information
palas committed Apr 17, 2024
1 parent 90dfd4b commit ef3a2c2
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 36 deletions.
53 changes: 36 additions & 17 deletions cardano-testnet/src/Testnet/Components/DReps.hs
@@ -1,10 +1,13 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE TypeApplications #-}

module Testnet.Components.DReps
( generateDRepKeyPair
( SomeKeyPair(..)
, generateDRepKeyPair
, generateRegistrationCertificate
, createDRepRegistrationTxBody
, createCertificatePublicationTxBody
, generateVoteFiles
, createVotingTxBody
, signTx
Expand Down Expand Up @@ -32,7 +35,8 @@ import System.FilePath ((</>))

import Testnet.Components.Query (EpochStateView, findLargestUtxoForPaymentKey)
import qualified Testnet.Process.Run as H
import Testnet.Runtime (PaymentKeyInfo (paymentKeyInfoAddr), PaymentKeyPair (..))
import Testnet.Runtime (PaymentKeyInfo (paymentKeyInfoAddr), PaymentKeyPair (..),
StakingKeyPair (stakingSKey))
import Testnet.Start.Types (anyEraToString)

import Hedgehog (MonadTest)
Expand Down Expand Up @@ -66,7 +70,7 @@ generateDRepKeyPair execConfig work prefix = do

-- DRep registration certificate generation

data DRepRegistrationCertificate
data Certificate

-- | Generates a registration certificate for a decentralized representative (DRep)
-- using @cardano-cli@.
Expand All @@ -90,7 +94,7 @@ generateRegistrationCertificate
-> String
-> PaymentKeyPair
-> Integer
-> m (File DRepRegistrationCertificate In)
-> m (File Certificate In)
generateRegistrationCertificate execConfig work prefix drepKeyPair depositAmount = do
let dRepRegistrationCertificate = File (work </> prefix <> ".regcert")
void $ H.execCli' execConfig [ "conway", "governance", "drep", "registration-certificate"
Expand All @@ -104,8 +108,7 @@ generateRegistrationCertificate execConfig work prefix drepKeyPair depositAmount

data TxBody

-- | Composes a decentralized representative (DRep) registration transaction body
-- (without signing) using @cardano-cli@.
-- | Composes a certificate publication transaction body (without signing) using @cardano-cli@.
--
-- This function takes seven parameters:
--
Expand All @@ -115,30 +118,29 @@ data TxBody
-- * 'sbe': The Shelley-based era (e.g., 'ShelleyEra') in which the transaction will be constructed.
-- * 'work': Base directory path where the transaction body file will be stored.
-- * 'prefix': Prefix for the output transaction body file name. The extension will be @.txbody@.
-- * 'drepRegCert': The file name of the registration certificate for the DRep, obtained using
-- 'generateRegistrationCertificate'.
-- * 'certificate': The file name of the certificate.
-- * 'wallet': Payment key information associated with the transaction,
-- as returned by 'cardanoTestnetDefault'.
--
-- Returns the generated @File TxBody In@ file path to the transaction body.
createDRepRegistrationTxBody
createCertificatePublicationTxBody
:: (H.MonadAssertion m, MonadTest m, MonadCatch m, MonadIO m)
=> H.ExecConfig
-> EpochStateView
-> ShelleyBasedEra era
-> FilePath
-> String
-> File DRepRegistrationCertificate In
-> File Certificate In
-> PaymentKeyInfo
-> m (File TxBody In)
createDRepRegistrationTxBody execConfig epochStateView sbe work prefix drepRegCert wallet = do
createCertificatePublicationTxBody execConfig epochStateView sbe work prefix cert wallet = do
let dRepRegistrationTxBody = File (work </> prefix <> ".txbody")
walletLargestUTXO <- findLargestUtxoForPaymentKey epochStateView sbe wallet
void $ H.execCli' execConfig
[ "conway", "transaction", "build"
, "--change-address", Text.unpack $ paymentKeyInfoAddr wallet
, "--tx-in", Text.unpack $ renderTxIn walletLargestUTXO
, "--certificate-file", unFile drepRegCert
, "--certificate-file", unFile cert
, "--witness-override", show @Int 2
, "--out-file", unFile dRepRegistrationTxBody
]
Expand Down Expand Up @@ -231,6 +233,23 @@ createVotingTxBody execConfig epochStateView sbe work prefix votes wallet = do

data SignedTx

class KeyPair a where
secretKey :: a -> FilePath

instance KeyPair PaymentKeyPair where
secretKey :: PaymentKeyPair -> FilePath
secretKey = paymentSKey

instance KeyPair StakingKeyPair where
secretKey :: StakingKeyPair -> FilePath
secretKey = stakingSKey

data SomeKeyPair = forall a . KeyPair a => SomeKeyPair a

instance KeyPair SomeKeyPair where
secretKey :: SomeKeyPair -> FilePath
secretKey (SomeKeyPair x) = secretKey x

-- | Calls @cardano-cli@ to signs a transaction body using the specified key pairs.
--
-- This function takes five parameters:
Expand All @@ -239,24 +258,24 @@ data SignedTx
-- * 'cEra': Specifies the current Cardano era.
-- * 'work': Base directory path where the signed transaction file will be stored.
-- * 'prefix': Prefix for the output signed transaction file name. The extension will be @.tx@.
-- * 'txBody': Transaction body to be signed, obtained using 'createDRepRegistrationTxBody' or similar.
-- * 'txBody': Transaction body to be signed, obtained using 'createCertificatePublicationTxBody' or similar.
-- * 'signatoryKeyPairs': List of payment key pairs used for signing the transaction.
--
-- Returns the generated @File SignedTx In@ file path to the signed transaction file.
signTx :: (MonadTest m, MonadCatch m, MonadIO m)
signTx :: (MonadTest m, MonadCatch m, MonadIO m, KeyPair k)
=> H.ExecConfig
-> AnyCardanoEra
-> FilePath
-> String
-> File TxBody In
-> [PaymentKeyPair]
-> [k]
-> m (File SignedTx In)
signTx execConfig cEra work prefix txBody signatoryKeyPairs = do
let signedTx = File (work </> prefix <> ".tx")
void $ H.execCli' execConfig $
[ anyEraToString cEra, "transaction", "sign"
, "--tx-body-file", unFile txBody
] ++ (concat [["--signing-key-file", paymentSKey kp] | kp <- signatoryKeyPairs]) ++
] ++ (concat [["--signing-key-file", secretKey kp] | kp <- signatoryKeyPairs]) ++
[ "--out-file", unFile signedTx
]
return signedTx
Expand Down
19 changes: 18 additions & 1 deletion cardano-testnet/src/Testnet/Defaults.hs
Expand Up @@ -17,6 +17,7 @@ module Testnet.Defaults
, defaultDRepVkeyFp
, defaultDRepSkeyFp
, defaultDRepKeyPair
, defaultDelegatorStakeKeyPair
, defaultShelleyGenesis
, defaultGenesisFilepath
, defaultYamlHardforkViaConfig
Expand Down Expand Up @@ -71,7 +72,7 @@ import Numeric.Natural
import System.FilePath ((</>))

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

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

-- | 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)

-- TODO: We should not hardcode a script like this. We need to move
-- plutus-example from plutus apps to cardano-node-testnet. This will
-- let us directly compile the plutus validators and avoid bit rotting of
Expand Down
Expand Up @@ -19,7 +19,7 @@ import Prelude
import qualified Data.Map as Map
import System.FilePath ((</>))

import Testnet.Components.DReps (createDRepRegistrationTxBody, failToSubmitTx,
import Testnet.Components.DReps (createCertificatePublicationTxBody, failToSubmitTx,
generateDRepKeyPair, generateRegistrationCertificate, signTx, submitTx)
import Testnet.Components.Query (checkDRepState, getEpochStateView, getMinDRepDeposit)
import qualified Testnet.Process.Run as H
Expand Down Expand Up @@ -87,8 +87,8 @@ hprop_ledger_events_drep_deposits = H.integrationWorkspace "drep-deposits" $ \te
drepKeyPair1 <- generateDRepKeyPair execConfig drepDir1 "keys"
drepRegCert1 <- generateRegistrationCertificate execConfig drepDir1 "reg-cert"
drepKeyPair1 (minDRepDeposit - 1)
drepRegTxBody1 <- createDRepRegistrationTxBody execConfig epochStateView sbe drepDir1 "reg-cert-txbody"
drepRegCert1 wallet0
drepRegTxBody1 <- createCertificatePublicationTxBody execConfig epochStateView sbe drepDir1 "reg-cert-txbody"
drepRegCert1 wallet0
drepSignedRegTx1 <- signTx execConfig cEra drepDir1 "signed-reg-tx"
drepRegTxBody1 [drepKeyPair1, paymentKeyInfoPair wallet0]

Expand All @@ -101,8 +101,8 @@ hprop_ledger_events_drep_deposits = H.integrationWorkspace "drep-deposits" $ \te
drepKeyPair2 <- generateDRepKeyPair execConfig drepDir2 "keys"
drepRegCert2 <- generateRegistrationCertificate execConfig drepDir2 "reg-cert"
drepKeyPair2 minDRepDeposit
drepRegTxBody2 <- createDRepRegistrationTxBody execConfig epochStateView sbe drepDir2 "reg-cert-txbody"
drepRegCert2 wallet1
drepRegTxBody2 <- createCertificatePublicationTxBody execConfig epochStateView sbe drepDir2 "reg-cert-txbody"
drepRegCert2 wallet1
drepSignedRegTx2 <- signTx execConfig cEra drepDir2 "signed-reg-tx"
drepRegTxBody2 [drepKeyPair2, paymentKeyInfoPair wallet1]

Expand Down

0 comments on commit ef3a2c2

Please sign in to comment.