Skip to content

Commit

Permalink
Add test for stake registration and deregistration
Browse files Browse the repository at this point in the history
  • Loading branch information
carbolymer committed Oct 21, 2024
1 parent cdb45dd commit 31bebc9
Show file tree
Hide file tree
Showing 18 changed files with 224 additions and 28 deletions.
13 changes: 7 additions & 6 deletions cardano-testnet/cardano-testnet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -183,30 +183,31 @@ test-suite cardano-testnet-test

main-is: cardano-testnet-test.hs

other-modules: Cardano.Testnet.Test.Cli.LeadershipSchedule
Cardano.Testnet.Test.Cli.StakeSnapshot
Cardano.Testnet.Test.Cli.Transaction
Cardano.Testnet.Test.Cli.Conway.Plutus
other-modules: Cardano.Testnet.Test.Cli.Conway.Plutus
Cardano.Testnet.Test.Cli.Conway.StakeSnapshot
Cardano.Testnet.Test.Cli.KesPeriodInfo
Cardano.Testnet.Test.Cli.LeadershipSchedule
Cardano.Testnet.Test.Cli.Query
Cardano.Testnet.Test.Cli.QuerySlotNumber
Cardano.Testnet.Test.Cli.StakeSnapshot
Cardano.Testnet.Test.Cli.Transaction
Cardano.Testnet.Test.Cli.Transaction.RegisterDeregisterStakeAddress
Cardano.Testnet.Test.FoldEpochState
Cardano.Testnet.Test.Gov.CommitteeAddNew
Cardano.Testnet.Test.Gov.DRepActivity
Cardano.Testnet.Test.Gov.DRepDeposit
Cardano.Testnet.Test.Gov.DRepRetirement
Cardano.Testnet.Test.Gov.GovActionTimeout
Cardano.Testnet.Test.Gov.InfoAction
Cardano.Testnet.Test.Gov.NoConfidence
Cardano.Testnet.Test.Gov.PParamChangeFailsSPO
Cardano.Testnet.Test.Gov.PredefinedAbstainDRep
Cardano.Testnet.Test.Gov.ProposeNewConstitution
Cardano.Testnet.Test.Gov.ProposeNewConstitutionSPO
Cardano.Testnet.Test.Gov.GovActionTimeout
Cardano.Testnet.Test.Gov.TreasuryDonation
Cardano.Testnet.Test.Gov.TreasuryGrowth
Cardano.Testnet.Test.Gov.TreasuryWithdrawal
Cardano.Testnet.Test.Misc
Cardano.Testnet.Test.Gov.PredefinedAbstainDRep
Cardano.Testnet.Test.Node.Shutdown
Cardano.Testnet.Test.SanityCheck
Cardano.Testnet.Test.SubmitApi.Transaction
Expand Down
5 changes: 4 additions & 1 deletion cardano-testnet/src/Testnet/Defaults.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import qualified Cardano.Api.Shelley as Api
import Cardano.Ledger.Alonzo.Core (PParams (..))
import Cardano.Ledger.Alonzo.Genesis (AlonzoGenesis)
import qualified Cardano.Ledger.Alonzo.Genesis as Ledger
import qualified Cardano.Ledger.Api as L
import Cardano.Ledger.BaseTypes
import qualified Cardano.Ledger.BaseTypes as Ledger
import Cardano.Ledger.Binary.Version ()
Expand Down Expand Up @@ -387,7 +388,9 @@ defaultShelleyGenesis asbe startTime maxSupply options = do
-- TODO: find out why this actually degrates network stability - turned off for now
-- securityParam = ceiling $ fromIntegral epochLength * cardanoActiveSlotsCoeff / 10
pVer = eraToProtocolVersion asbe
protocolParams = Api.sgProtocolParams Api.shelleyGenesisDefaults
-- TODO propagate this default to cardano-api
-- TODO update tests to read this value when registering keys
protocolParams = Api.sgProtocolParams Api.shelleyGenesisDefaults & L.ppKeyDepositL .~ 400_000
protocolParamsWithPVer = protocolParams & ppProtocolVersionL' .~ pVer
Api.shelleyGenesisDefaults
{ Api.sgActiveSlotsCoeff = unsafeBoundedRational activeSlotsCoeff
Expand Down
2 changes: 1 addition & 1 deletion cardano-testnet/src/Testnet/Process/Cli/SPO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ registerSingleSpo asbe identifier tap@(TmpAbsolutePath tempAbsPath') nodeConfigF

createStakeKeyRegistrationCertificate tap asbe
poolOwnerstakeVkeyFp
0
400_000
(workDir </> "pledger.regcert")

void $ execCli' execConfig
Expand Down
2 changes: 0 additions & 2 deletions cardano-testnet/src/Testnet/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ module Testnet.Types
, SomeKeyPair(..)
, VKey
, SKey
, ColdPoolKey
, VrfKey
, StakingKey
, PaymentKey
Expand Down Expand Up @@ -143,7 +142,6 @@ isTestnetNodeSpo = isJust . poolKeys
nodeSocketPath :: TestnetNode -> SocketPath
nodeSocketPath = File . H.sprocketSystemName . nodeSprocket

data ColdPoolKey
data StakingKey
data SpoColdKey

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ hprop_plutus_v3 = integrationWorkspace "all-plutus-script-purposes" $ \tempAbsBa
tempAbsPath
anyEra
plutusScript
0
400_000
scriptStakeRegistrationCertificate

-- 1. Put UTxO and datum at Plutus spending script address
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ hprop_kes_period_info = integrationRetryWorkspace 2 "kes-period-info" $ \tempAbs
tempAbsPath
(cardanoNodeEra cTestnetOptions)
testDelegatorVkeyFp
0
400_000
testDelegatorRegCertFp

-- Test stake address deleg cert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ hprop_leadershipSchedule = integrationRetryWorkspace 2 "leadership-schedule" $ \
tempAbsPath
(cardanoNodeEra cTestnetOptions)
testDelegatorVkeyFp
0
400_000
testDelegatorRegCertFp

-- Test stake address deleg cert
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module Cardano.Testnet.Test.Cli.Transaction.RegisterDeregisterStakeAddress
( hprop_tx_register_deregister_stake_address
) where

import Cardano.Api as Api
import Cardano.Api.Ledger (EpochInterval (..))

import qualified Cardano.Crypto.Hash as L
import qualified Cardano.Ledger.Conway.Governance as L
import qualified Cardano.Ledger.Conway.Governance as Ledger
import qualified Cardano.Ledger.Hashes as L
import qualified Cardano.Ledger.Shelley.LedgerState as L
import Cardano.Testnet

import Prelude

import Control.Monad
import Control.Monad.State.Strict (StateT)
import Data.Default.Class
import Data.Maybe
import Data.Maybe.Strict
import Data.String
import qualified Data.Text as Text
import GHC.Exts (IsList (..))
import Lens.Micro
import System.FilePath ((</>))

import Testnet.Components.Configuration
import Testnet.Components.Query
import Testnet.Defaults
import Testnet.EpochStateProcessing (waitForGovActionVotes)
import Testnet.Process.Cli.DRep
import Testnet.Process.Cli.Keys
import Testnet.Process.Cli.Transaction
import Testnet.Process.Run (execCli', execCliAny, mkExecConfig)
import Testnet.Property.Util (integrationWorkspace)
import Testnet.Start.Types
import Testnet.Types

import Hedgehog
import qualified Hedgehog.Extras as H

-- | Execute me with:
-- @DISABLE_RETRIES=1 cabal test cardano-testnet-test --test-options '-p "//"'@
hprop_tx_register_deregister_stake_address :: Property
hprop_tx_register_deregister_stake_address = integrationWorkspace "register-deregister-stake-address" $ \tempAbsBasePath' -> H.runWithDefaultWatchdog_ $ do
-- Start a local test net
conf@Conf { tempAbsPath } <- mkConf tempAbsBasePath'
let tempAbsPath' = unTmpAbsPath tempAbsPath
tempBaseAbsPath = makeTmpBaseAbsPath tempAbsPath

work <- H.createDirectoryIfMissing $ tempAbsPath' </> "work"

let ceo = ConwayEraOnwardsConway
sbe = conwayEraOnwardsToShelleyBasedEra ceo
era = toCardanoEra sbe
cEra = AnyCardanoEra era
eraName = eraToString sbe
fastTestnetOptions = def { cardanoNodeEra = AnyShelleyBasedEra sbe }
shelleyOptions = def { genesisEpochLength = 200 }

TestnetRuntime
{ testnetMagic
, testnetNodes
, wallets=wallet0:wallet1:_
, configurationFile
}
<- cardanoTestnetDefault fastTestnetOptions shelleyOptions conf

node <- H.headM testnetNodes
poolSprocket1 <- H.noteShow $ nodeSprocket node
execConfig <- mkExecConfig tempBaseAbsPath poolSprocket1 testnetMagic
let socketPath = nodeSocketPath node

epochStateView <- getEpochStateView configurationFile socketPath

H.note_ $ "Sprocket: " <> show poolSprocket1
H.note_ $ "Abs path: " <> tempAbsBasePath'
H.note_ $ "Socketpath: " <> unFile socketPath
H.note_ $ "Foldblocks config file: " <> unFile configurationFile

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

let stakeVkeyFp = gov </> "stake.vkey"
stakeSKeyFp = gov </> "stake.skey"
stakeCertFp = gov </> "stake.regcert"
stakeCertDeregFp = gov </> "stake.deregcert"
stakeKeys = KeyPair { verificationKey = File stakeVkeyFp
, signingKey = File stakeSKeyFp
}

cliStakeAddressKeyGen stakeKeys

-- Register stake address
void $ execCli' execConfig
[ eraName, "stake-address", "registration-certificate"
, "--stake-verification-key-file", stakeVkeyFp
, "--key-reg-deposit-amt", show @Int 400_000
, "--out-file", stakeCertFp
]

stakeAddress <- H.noteM $ execCli' execConfig
[ eraName, "stake-address", "build"
, "--stake-verification-key-file", stakeVkeyFp
, "--out-file", "/dev/stdout"
]

stakeCertTxBodyFp <- H.note $ work </> "stake.registration.txbody"
stakeCertTxSignedFp <- H.note $ work </> "stake.registration.tx"

txin1 <- findLargestUtxoForPaymentKey epochStateView sbe wallet0

(_, stdout, stderr) <- execCliAny execConfig
[ eraName, "transaction", "build"
, "--change-address", Text.unpack $ paymentKeyInfoAddr wallet0
, "--tx-in", Text.unpack $ renderTxIn txin1
, "--tx-out", Text.unpack (paymentKeyInfoAddr wallet1) <> "+" <> show @Int 10_000_000
, "--certificate-file", stakeCertFp
, "--witness-override", show @Int 2
, "--out-file", stakeCertTxBodyFp
]
H.note_ stdout
H.note_ stderr

void $ execCli' execConfig
[ eraName, "transaction", "sign"
, "--tx-body-file", stakeCertTxBodyFp
, "--signing-key-file", signingKeyFp $ paymentKeyInfoPair wallet0
, "--signing-key-file", stakeSKeyFp
, "--out-file", stakeCertTxSignedFp
]

void $ execCli' execConfig
[ eraName, "transaction", "submit"
, "--tx-file", stakeCertTxSignedFp
]

H.noteShowM_ $ waitForBlocks epochStateView 1
-- H.noteShowM_ $ waitForEpochs epochStateView (EpochInterval 2)

do
(_, stdout', stderr') <- execCliAny execConfig
[ eraName, "query", "stake-address-info"
, "--address", stakeAddress
, "--out-file", "/dev/stdout"
]
H.note_ stdout'
H.note_ stderr'

-- deregister stake address
void $ execCli' execConfig
[ eraName, "stake-address", "deregistration-certificate"
, "--stake-verification-key-file", stakeVkeyFp
, "--key-reg-deposit-amt", show @Int 400_000
, "--out-file", stakeCertDeregFp
]

stakeCertDeregTxBodyFp <- H.note $ work </> "stake.deregistration.txbody"
stakeCertDeregTxSignedFp <- H.note $ work </> "stake.deregistration.tx"

txin2 <- findLargestUtxoForPaymentKey epochStateView sbe wallet1

(_, stdout', stderr') <- execCliAny execConfig
[ eraName, "transaction", "build"
, "--change-address", Text.unpack $ paymentKeyInfoAddr wallet1
, "--tx-in", Text.unpack $ renderTxIn txin2
, "--tx-out", Text.unpack (paymentKeyInfoAddr wallet0) <> "+" <> show @Int 10_000_000
, "--certificate-file", stakeCertDeregFp
, "--witness-override", show @Int 2
, "--out-file", stakeCertDeregTxBodyFp
]
H.note_ stdout'
H.note_ stderr'

void $ execCli' execConfig
[ eraName, "transaction", "sign"
, "--tx-body-file", stakeCertDeregTxBodyFp
, "--signing-key-file", signingKeyFp $ paymentKeyInfoPair wallet1
, "--signing-key-file", stakeSKeyFp
, "--out-file", stakeCertDeregTxSignedFp
]

void $ execCli' execConfig
[ eraName, "transaction", "submit"
, "--tx-file", stakeCertDeregTxSignedFp
]
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ hprop_constitutional_committee_add_new = integrationWorkspace "constitutional-co
void $ execCli' execConfig
[ eraName, "stake-address", "registration-certificate"
, "--stake-verification-key-file", stakeVkeyFp
, "--key-reg-deposit-amt", show @Int 0 -- TODO: why this needs to be 0????
, "--key-reg-deposit-amt", show @Int 400_000
, "--out-file", stakeCertFp
]

Expand Down Expand Up @@ -164,7 +164,6 @@ hprop_constitutional_committee_add_new = integrationWorkspace "constitutional-co
, "--tx-file", stakeCertTxSignedFp
]


minGovActDeposit <- getMinGovActionDeposit epochStateView ceo

ccColdKeys <- H.noteShowM $
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ hprop_check_gov_action_timeout = integrationWorkspace "gov-action-timeout" $ \te
void $ execCli' execConfig
[ eraName, "stake-address", "registration-certificate"
, "--stake-verification-key-file", stakeVkeyFp
, "--key-reg-deposit-amt", show @Int 0 -- TODO: why this needs to be 0????
, "--key-reg-deposit-amt", show @Int 400_000
, "--out-file", stakeCertFp
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ hprop_ledger_events_info_action = integrationRetryWorkspace 2 "info-hash" $ \tem
void $ execCli' execConfig
[ eraName, "stake-address", "registration-certificate"
, "--stake-verification-key-file", stakeVkeyFp
, "--key-reg-deposit-amt", show @Int 0 -- TODO: why this needs to be 0????
, "--key-reg-deposit-amt", show @Int 400_000
, "--out-file", stakeCertFp
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ hprop_check_pparam_fails_spo = integrationWorkspace "test-pparam-spo" $ \tempAbs
void $ execCli' execConfig
[ eraName, "stake-address", "registration-certificate"
, "--stake-verification-key-file", stakeVkeyFp
, "--key-reg-deposit-amt", show @Int 0 -- TODO: why this needs to be 0????
, "--key-reg-deposit-amt", show @Int 400_000
, "--out-file", stakeCertFp
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ hprop_ledger_events_propose_new_constitution = integrationWorkspace "propose-new
void $ execCli' execConfig
[ eraName, "stake-address", "registration-certificate"
, "--stake-verification-key-file", stakeVkeyFp
, "--key-reg-deposit-amt", show @Int 0 -- TODO: why this needs to be 0????
, "--key-reg-deposit-amt", show @Int 400_000
, "--out-file", stakeCertFp
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ hprop_ledger_events_treasury_withdrawal = integrationRetryWorkspace 2 "treasury
void $ execCli' execConfig
[ eraName, "stake-address", "registration-certificate"
, "--stake-verification-key-file", stakeVkeyFp
, "--key-reg-deposit-amt", show @Int 0 -- TODO: why this needs to be 0????
, "--key-reg-deposit-amt", show @Int 400_000
, "--out-file", stakeCertFp
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import qualified Cardano.Testnet.Test.Cli.Query
import qualified Cardano.Testnet.Test.Cli.QuerySlotNumber
import qualified Cardano.Testnet.Test.Cli.StakeSnapshot
import qualified Cardano.Testnet.Test.Cli.Transaction
import qualified Cardano.Testnet.Test.Cli.Transaction.RegisterDeregisterStakeAddress
import qualified Cardano.Testnet.Test.FoldEpochState
import qualified Cardano.Testnet.Test.Gov.CommitteeAddNew as Gov
import qualified Cardano.Testnet.Test.Gov.DRepDeposit as Gov
Expand Down Expand Up @@ -76,9 +77,10 @@ tests = do
-- ShutdownOnSigint fails on Mac with
-- "Log file: /private/tmp/tmp.JqcjW7sLKS/kes-period-info-2-test-30c2d0d8eb042a37/logs/test-spo.stdout.log had no logs indicating the relevant node has minted blocks."
, ignoreOnMacAndWindows "Shutdown On Sigint" Cardano.Testnet.Test.Node.Shutdown.hprop_shutdownOnSigint
, ignoreOnWindows "ShutdownOnSlotSynced" Cardano.Testnet.Test.Node.Shutdown.hprop_shutdownOnSlotSynced
, ignoreOnWindows "stake-snapshot" Cardano.Testnet.Test.Cli.StakeSnapshot.hprop_stakeSnapshot
, ignoreOnWindows "Shutdown On SlotSynced" Cardano.Testnet.Test.Node.Shutdown.hprop_shutdownOnSlotSynced
, ignoreOnWindows "stake snapshot" Cardano.Testnet.Test.Cli.StakeSnapshot.hprop_stakeSnapshot
, ignoreOnWindows "simple transaction build" Cardano.Testnet.Test.Cli.Transaction.hprop_transaction
, ignoreOnWindows "register deregister stake address in transaction build" Cardano.Testnet.Test.Cli.Transaction.RegisterDeregisterStakeAddress.hprop_tx_register_deregister_stake_address
-- FIXME
-- , ignoreOnMacAndWindows "leadership-schedule" Cardano.Testnet.Test.Cli.LeadershipSchedule.hprop_leadershipSchedule

Expand Down
Loading

0 comments on commit 31bebc9

Please sign in to comment.