Skip to content

Commit a29bd5d

Browse files
committed
cardano-testnet | Add stake registration & deregistration test
1 parent 4ca29e2 commit a29bd5d

File tree

1 file changed

+194
-0
lines changed

1 file changed

+194
-0
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
{-# LANGUAGE NamedFieldPuns #-}
2+
{-# LANGUAGE NumericUnderscores #-}
3+
{-# LANGUAGE OverloadedStrings #-}
4+
{-# LANGUAGE ScopedTypeVariables #-}
5+
{-# LANGUAGE TypeApplications #-}
6+
7+
module Cardano.Testnet.Test.Cli.Transaction.RegisterDeregisterStakeAddress
8+
( hprop_tx_register_deregister_stake_address
9+
) where
10+
11+
import Cardano.Api as Api
12+
import Cardano.Api.Ledger (Coin (..), EpochInterval (..))
13+
14+
import qualified Cardano.Crypto.Hash as L
15+
import qualified Cardano.Ledger.Conway.Governance as L
16+
import qualified Cardano.Ledger.Conway.Governance as Ledger
17+
import qualified Cardano.Ledger.Hashes as L
18+
import qualified Cardano.Ledger.Shelley.LedgerState as L
19+
import Cardano.Testnet
20+
21+
import Prelude
22+
23+
import Control.Monad
24+
import Control.Monad.State.Strict (StateT)
25+
import Data.Default.Class
26+
import Data.Maybe
27+
import Data.Maybe.Strict
28+
import Data.String
29+
import qualified Data.Text as Text
30+
import GHC.Exts (IsList (..))
31+
import Lens.Micro
32+
import System.FilePath ((</>))
33+
34+
import Testnet.Components.Configuration
35+
import Testnet.Components.Query
36+
import Testnet.Defaults
37+
import Testnet.EpochStateProcessing (waitForGovActionVotes)
38+
import Testnet.Process.Cli.DRep
39+
import Testnet.Process.Cli.Keys
40+
import Testnet.Process.Cli.Transaction
41+
import Testnet.Process.Run (execCli', execCliAny, mkExecConfig)
42+
import Testnet.Property.Util (integrationWorkspace)
43+
import Testnet.Start.Types
44+
import Testnet.Types
45+
46+
import Hedgehog
47+
import qualified Hedgehog.Extras as H
48+
49+
-- | Execute me with:
50+
-- @DISABLE_RETRIES=1 cabal test cardano-testnet-test --test-options '-p "/register deregister stake address in transaction build/"'@
51+
hprop_tx_register_deregister_stake_address :: Property
52+
hprop_tx_register_deregister_stake_address = integrationWorkspace "register-deregister-stake-address" $ \tempAbsBasePath' -> H.runWithDefaultWatchdog_ $ do
53+
-- Start a local test net
54+
conf@Conf { tempAbsPath } <- mkConf tempAbsBasePath'
55+
let tempAbsPath' = unTmpAbsPath tempAbsPath
56+
tempBaseAbsPath = makeTmpBaseAbsPath tempAbsPath
57+
58+
work <- H.createDirectoryIfMissing $ tempAbsPath' </> "work"
59+
60+
let ceo = ConwayEraOnwardsConway
61+
sbe = conwayEraOnwardsToShelleyBasedEra ceo
62+
era = toCardanoEra sbe
63+
cEra = AnyCardanoEra era
64+
eraName = eraToString sbe
65+
fastTestnetOptions = def { cardanoNodeEra = AnyShelleyBasedEra sbe }
66+
shelleyOptions = def { genesisEpochLength = 200 }
67+
68+
TestnetRuntime
69+
{ testnetMagic
70+
, testnetNodes
71+
, wallets=wallet0:wallet1:_
72+
, configurationFile
73+
}
74+
<- cardanoTestnetDefault fastTestnetOptions shelleyOptions conf
75+
76+
node <- H.headM testnetNodes
77+
poolSprocket1 <- H.noteShow $ nodeSprocket node
78+
execConfig <- mkExecConfig tempBaseAbsPath poolSprocket1 testnetMagic
79+
let socketPath = nodeSocketPath node
80+
81+
epochStateView <- getEpochStateView configurationFile socketPath
82+
83+
H.note_ $ "Sprocket: " <> show poolSprocket1
84+
H.note_ $ "Abs path: " <> tempAbsBasePath'
85+
H.note_ $ "Socketpath: " <> unFile socketPath
86+
H.note_ $ "Foldblocks config file: " <> unFile configurationFile
87+
88+
-- Create Conway constitution
89+
gov <- H.createDirectoryIfMissing $ work </> "governance"
90+
91+
let stakeVkeyFp = gov </> "stake.vkey"
92+
stakeSKeyFp = gov </> "stake.skey"
93+
stakeCertFp = gov </> "stake.regcert"
94+
stakeCertDeregFp = gov </> "stake.deregcert"
95+
stakeKeys = KeyPair { verificationKey = File stakeVkeyFp
96+
, signingKey = File stakeSKeyFp
97+
}
98+
99+
cliStakeAddressKeyGen stakeKeys
100+
101+
keyDepositStr <- show . unCoin <$> getKeyDeposit epochStateView ceo
102+
-- Register stake address
103+
void $ execCli' execConfig
104+
[ eraName, "stake-address", "registration-certificate"
105+
, "--stake-verification-key-file", stakeVkeyFp
106+
, "--key-reg-deposit-amt", keyDepositStr
107+
, "--out-file", stakeCertFp
108+
]
109+
110+
stakeAddress <- H.noteM $ execCli' execConfig
111+
[ eraName, "stake-address", "build"
112+
, "--stake-verification-key-file", stakeVkeyFp
113+
, "--out-file", "/dev/stdout"
114+
]
115+
116+
stakeCertTxBodyFp <- H.note $ work </> "stake.registration.txbody"
117+
stakeCertTxSignedFp <- H.note $ work </> "stake.registration.tx"
118+
119+
txin1 <- findLargestUtxoForPaymentKey epochStateView sbe wallet0
120+
121+
(_, stdout, stderr) <- execCliAny execConfig
122+
[ eraName, "transaction", "build"
123+
, "--change-address", Text.unpack $ paymentKeyInfoAddr wallet0
124+
, "--tx-in", Text.unpack $ renderTxIn txin1
125+
, "--tx-out", Text.unpack (paymentKeyInfoAddr wallet1) <> "+" <> show @Int 10_000_000
126+
, "--certificate-file", stakeCertFp
127+
, "--witness-override", show @Int 2
128+
, "--out-file", stakeCertTxBodyFp
129+
]
130+
H.note_ stdout
131+
H.note_ stderr
132+
133+
void $ execCli' execConfig
134+
[ eraName, "transaction", "sign"
135+
, "--tx-body-file", stakeCertTxBodyFp
136+
, "--signing-key-file", signingKeyFp $ paymentKeyInfoPair wallet0
137+
, "--signing-key-file", stakeSKeyFp
138+
, "--out-file", stakeCertTxSignedFp
139+
]
140+
141+
void $ execCli' execConfig
142+
[ eraName, "transaction", "submit"
143+
, "--tx-file", stakeCertTxSignedFp
144+
]
145+
146+
H.noteShowM_ $ waitForBlocks epochStateView 1
147+
-- H.noteShowM_ $ waitForEpochs epochStateView (EpochInterval 2)
148+
149+
do
150+
(_, stdout', stderr') <- execCliAny execConfig
151+
[ eraName, "query", "stake-address-info"
152+
, "--address", stakeAddress
153+
, "--out-file", "/dev/stdout"
154+
]
155+
H.note_ stdout'
156+
H.note_ stderr'
157+
158+
-- deregister stake address
159+
void $ execCli' execConfig
160+
[ eraName, "stake-address", "deregistration-certificate"
161+
, "--stake-verification-key-file", stakeVkeyFp
162+
, "--key-reg-deposit-amt", keyDepositStr
163+
, "--out-file", stakeCertDeregFp
164+
]
165+
166+
stakeCertDeregTxBodyFp <- H.note $ work </> "stake.deregistration.txbody"
167+
stakeCertDeregTxSignedFp <- H.note $ work </> "stake.deregistration.tx"
168+
169+
txin2 <- findLargestUtxoForPaymentKey epochStateView sbe wallet1
170+
171+
(_, stdout', stderr') <- execCliAny execConfig
172+
[ eraName, "transaction", "build"
173+
, "--change-address", Text.unpack $ paymentKeyInfoAddr wallet1
174+
, "--tx-in", Text.unpack $ renderTxIn txin2
175+
, "--tx-out", Text.unpack (paymentKeyInfoAddr wallet0) <> "+" <> show @Int 10_000_000
176+
, "--certificate-file", stakeCertDeregFp
177+
, "--witness-override", show @Int 2
178+
, "--out-file", stakeCertDeregTxBodyFp
179+
]
180+
H.note_ stdout'
181+
H.note_ stderr'
182+
183+
void $ execCli' execConfig
184+
[ eraName, "transaction", "sign"
185+
, "--tx-body-file", stakeCertDeregTxBodyFp
186+
, "--signing-key-file", signingKeyFp $ paymentKeyInfoPair wallet1
187+
, "--signing-key-file", stakeSKeyFp
188+
, "--out-file", stakeCertDeregTxSignedFp
189+
]
190+
191+
void $ execCli' execConfig
192+
[ eraName, "transaction", "submit"
193+
, "--tx-file", stakeCertDeregTxSignedFp
194+
]

0 commit comments

Comments
 (0)