Skip to content

Commit 8464bf5

Browse files
committed
Extract stake registration and delegation utility functions to ImpTest
1 parent 3fac44c commit 8464bf5

File tree

3 files changed

+66
-50
lines changed

3 files changed

+66
-50
lines changed

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Imp/DelegSpec.hs

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Cardano.Ledger.BaseTypes (
2222
addEpochInterval,
2323
natVersion,
2424
)
25-
import Cardano.Ledger.Coin (Coin (..), compactCoinOrError)
25+
import Cardano.Ledger.Coin (Coin (..))
2626
import Cardano.Ledger.Conway
2727
import Cardano.Ledger.Conway.Core
2828
import Cardano.Ledger.Conway.Governance
@@ -61,7 +61,7 @@ spec = do
6161
submitTx_ $
6262
mkBasicTx mkBasicTxBody
6363
& bodyTxL . certsTxBodyL .~ [regTxCert]
64-
expectRegistered cred
64+
expectStakeCredRegistered cred
6565

6666
it "With correct deposit" $ do
6767
expectedDeposit <- getsNES $ nesEsL . curPParamsEpochStateL . ppKeyDepositL
@@ -70,7 +70,7 @@ spec = do
7070
mkBasicTx mkBasicTxBody
7171
& bodyTxL . certsTxBodyL
7272
.~ [RegDepositTxCert (KeyHashObj kh) expectedDeposit]
73-
expectRegistered (KeyHashObj kh)
73+
expectStakeCredRegistered (KeyHashObj kh)
7474

7575
it "Twice the same certificate in the same transaction" $ do
7676
-- This is expected behavior because `certsTxBodyL` removes duplicates
@@ -80,7 +80,7 @@ spec = do
8080
mkBasicTx mkBasicTxBody
8181
& bodyTxL . certsTxBodyL
8282
.~ [regTxCert, regTxCert]
83-
expectRegistered (KeyHashObj kh)
83+
expectStakeCredRegistered (KeyHashObj kh)
8484

8585
it "When already already registered" $ do
8686
cred <- ScriptHashObj <$> impAddNativeScript (RequireAllOf [])
@@ -94,7 +94,7 @@ spec = do
9494
tx
9595
[ injectFailure $ Shelley.StakeKeyAlreadyRegisteredDELEG cred
9696
]
97-
expectRegistered cred
97+
expectStakeCredRegistered cred
9898

9999
it "With incorrect deposit" $ do
100100
expectedDeposit <- getsNES $ nesEsL . curPParamsEpochStateL . ppKeyDepositL
@@ -119,7 +119,7 @@ spec = do
119119
}
120120
else IncorrectDepositDELEG wrongDeposit
121121
]
122-
expectNotRegistered (KeyHashObj kh)
122+
expectStakeCredNotRegistered (KeyHashObj kh)
123123

124124
describe "Unregister stake credentials" $ do
125125
it "When registered" $ do
@@ -128,14 +128,14 @@ spec = do
128128
submitTx_ $
129129
mkBasicTx mkBasicTxBody
130130
& bodyTxL . certsTxBodyL .~ [regTxCert]
131-
expectRegistered cred
131+
expectStakeCredRegistered cred
132132

133133
unRegTxCert <- genUnRegTxCert cred
134134
submitTx_ $
135135
mkBasicTx mkBasicTxBody
136136
& bodyTxL . certsTxBodyL
137137
.~ [unRegTxCert]
138-
expectNotRegistered cred
138+
expectStakeCredNotRegistered cred
139139

140140
it "When not registered" $ do
141141
freshKeyHash >>= \kh -> do
@@ -176,7 +176,7 @@ spec = do
176176
else IncorrectDepositDELEG wrongDeposit
177177
]
178178

179-
expectRegistered cred
179+
expectStakeCredRegistered cred
180180

181181
-- https://github.com/IntersectMBO/formal-ledger-specifications/issues/917
182182
-- impacts `registerAndRetirePoolToMakeReward`
@@ -198,7 +198,7 @@ spec = do
198198
& bodyTxL . certsTxBodyL .~ [unRegTxCert]
199199
)
200200
[injectFailure $ Shelley.StakeKeyNonZeroAccountBalanceDELEG balance]
201-
expectRegistered cred
201+
expectStakeCredRegistered cred
202202

203203
it "Register and unregister in the same transaction" $ do
204204
freshKeyHash >>= \kh -> do
@@ -207,7 +207,7 @@ spec = do
207207
submitTx_ $
208208
mkBasicTx mkBasicTxBody
209209
& bodyTxL . certsTxBodyL .~ [regTxCert, unRegTxCert]
210-
expectNotRegistered (KeyHashObj kh)
210+
expectStakeCredNotRegistered (KeyHashObj kh)
211211

212212
it "deregistering returns the deposit" $ do
213213
let
@@ -291,7 +291,7 @@ spec = do
291291
then Shelley.StakeDelegationImpossibleDELEG cred
292292
else Shelley.StakeKeyNotRegisteredDELEG cred
293293
]
294-
expectNotRegistered cred
294+
expectStakeCredNotRegistered cred
295295

296296
it "Delegate to unregistered pool" $ do
297297
expectedDeposit <- getsNES $ nesEsL . curPParamsEpochStateL . ppKeyDepositL
@@ -358,7 +358,7 @@ spec = do
358358
mkBasicTx mkBasicTxBody
359359
& bodyTxL . certsTxBodyL
360360
.~ [regTxCert, delegStakeTxCert cred poolKh, unRegTxCert]
361-
expectNotRegistered cred
361+
expectStakeCredNotRegistered cred
362362

363363
describe "Delegate vote" $ do
364364
it "Delegate vote of registered stake credentials to registered drep" $ do
@@ -433,7 +433,7 @@ spec = do
433433
)
434434
[injectFailure $ StakeKeyNotRegisteredDELEG cred]
435435

436-
expectNotRegistered cred
436+
expectStakeCredNotRegistered cred
437437

438438
it "Redelegate vote" $ do
439439
expectedDeposit <- getsNES $ nesEsL . curPParamsEpochStateL . ppKeyDepositL
@@ -472,7 +472,7 @@ spec = do
472472
mkBasicTx mkBasicTxBody
473473
& bodyTxL . certsTxBodyL
474474
.~ [UnRegDepositTxCert cred expectedDeposit]
475-
expectNotRegistered cred
475+
expectStakeCredNotRegistered cred
476476
expectNotDelegatedVote cred
477477
-- https://github.com/IntersectMBO/formal-ledger-specifications/issues/917
478478
-- TODO: Re-enable after issue is resolved, by removing this override
@@ -502,7 +502,7 @@ spec = do
502502
& bodyTxL . certsTxBodyL
503503
.~ [RegDepositDelegTxCert cred (DelegVote DRepAlwaysAbstain) expectedDeposit]
504504
registerAndRetirePoolToMakeReward cred
505-
expectRegistered cred
505+
expectStakeCredRegistered cred
506506
expectDelegatedVote cred DRepAlwaysAbstain
507507
impAnn "Version should be unchanged" $
508508
getProtVer `shouldReturn` initialProtVer
@@ -523,7 +523,7 @@ spec = do
523523
& bodyTxL . certsTxBodyL .~ [UnRegDepositTxCert cred expectedDeposit]
524524
& bodyTxL . withdrawalsTxBodyL
525525
.~ Withdrawals (Map.singleton rewardAccount withdrawalAmount)
526-
expectNotRegistered cred
526+
expectStakeCredNotRegistered cred
527527
expectNotDelegatedVote cred
528528
-- https://github.com/IntersectMBO/formal-ledger-specifications/issues/916
529529
-- TODO: Re-enable after issue is resolved, by removing this override
@@ -536,7 +536,7 @@ spec = do
536536
& bodyTxL . certsTxBodyL
537537
.~ [RegDepositDelegTxCert cred (DelegVote DRepAlwaysAbstain) expectedDeposit]
538538
registerAndRetirePoolToMakeReward cred
539-
expectRegistered cred
539+
expectStakeCredRegistered cred
540540
expectDelegatedVote cred DRepAlwaysAbstain
541541
forM_ @[] [1 .. 3 :: Int] $ \_ -> do
542542
submitTx_ $
@@ -552,7 +552,7 @@ spec = do
552552
.~ [UnRegDepositTxCert cred expectedDeposit]
553553
& bodyTxL . withdrawalsTxBodyL
554554
.~ Withdrawals (Map.singleton rewardAccount withdrawalAmount)
555-
expectNotRegistered cred
555+
expectStakeCredNotRegistered cred
556556
expectNotDelegatedVote cred
557557

558558
-- https://github.com/IntersectMBO/formal-ledger-specifications/issues/640
@@ -648,7 +648,7 @@ spec = do
648648
mkBasicTx mkBasicTxBody
649649
& bodyTxL . certsTxBodyL
650650
.~ [UnRegDepositTxCert cred expectedDeposit]
651-
expectNotRegistered cred
651+
expectStakeCredNotRegistered cred
652652

653653
it "Delegate to DRep and SPO and change delegation to a different SPO" $ do
654654
expectedDeposit <- getsNES $ nesEsL . curPParamsEpochStateL . ppKeyDepositL
@@ -678,19 +678,6 @@ spec = do
678678
expectDelegatedToPool cred poolKh'
679679
expectDelegatedVote cred (DRepCredential drepCred)
680680
where
681-
expectNotRegistered :: Credential 'Staking -> ImpTestM era ()
682-
expectNotRegistered cred = do
683-
accounts <- getsNES $ nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL
684-
impAnn (show cred <> " expected to not be in Accounts") $ do
685-
expectNothingExpr $ lookupAccountState cred accounts
686-
687-
expectNotDelegatedToPool :: Credential 'Staking -> ImpTestM era ()
688-
expectNotDelegatedToPool cred = do
689-
accounts <- getsNES $ nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL
690-
impAnn (show cred <> " expected to not have delegated to a stake pool") $ do
691-
accountState <- expectJust $ lookupAccountState cred accounts
692-
expectNothingExpr (accountState ^. stakePoolDelegationAccountStateL)
693-
694681
expectDelegatedVote :: HasCallStack => Credential 'Staking -> DRep -> ImpTestM era ()
695682
expectDelegatedVote cred drep = do
696683
accounts <- getsNES $ nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL
@@ -742,20 +729,3 @@ conwayEraSpecificSpec = do
742729
, DelegStakeTxCert cred2 poolKh -- using the pattern from Shelley
743730
]
744731
expectDelegatedToPool cred2 poolKh
745-
746-
expectRegistered :: (HasCallStack, ConwayEraImp era) => Credential 'Staking -> ImpTestM era ()
747-
expectRegistered cred = do
748-
accounts <- getsNES $ nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL
749-
expectedDeposit <- getsNES $ nesEsL . curPParamsEpochStateL . ppKeyDepositL
750-
751-
accountState <- expectJust $ lookupAccountState cred accounts
752-
impAnn (show cred <> " expected to be in Accounts with the correct deposit") $ do
753-
accountState ^. depositAccountStateL `shouldBe` compactCoinOrError expectedDeposit
754-
755-
expectDelegatedToPool ::
756-
(HasCallStack, ConwayEraImp era) => Credential 'Staking -> KeyHash 'StakePool -> ImpTestM era ()
757-
expectDelegatedToPool cred poolKh = do
758-
accounts <- getsNES $ nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL
759-
impAnn (show cred <> " expected to have delegated to " <> show poolKh) $ do
760-
accountState <- expectJust $ lookupAccountState cred accounts
761-
accountState ^. stakePoolDelegationAccountStateL `shouldBe` Just poolKh

eras/shelley/impl/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* Added `shelleyGenRegTxCert`
1313
* Added `genRegTxCert` to `ShelleyEraImp`
1414
* Added `delegStakeTxCert` to `ShelleyEraImp`
15+
* Added `expectStakeCredRegistered`, `expectStakeCredNotRegistered`, `expectDelegatedToPool`, `expectNotDelegatedToPool` to `ImpTest`
1516

1617
## 1.17.0.0
1718

eras/shelley/impl/testlib/Test/Cardano/Ledger/Shelley/ImpTest.hs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ module Test.Cardano.Ledger.Shelley.ImpTest (
7171
delegateStake,
7272
registerRewardAccount,
7373
registerStakeCredential,
74+
expectNotDelegatedToPool,
75+
expectStakeCredRegistered,
76+
expectStakeCredNotRegistered,
77+
expectDelegatedToPool,
7478
getRewardAccountFor,
7579
getReward,
7680
lookupReward,
@@ -1607,6 +1611,47 @@ delegateStake cred poolKH = do
16071611
mkBasicTx mkBasicTxBody
16081612
& bodyTxL . certsTxBodyL .~ [delegStakeTxCert cred poolKH]
16091613

1614+
expectStakeCredRegistered ::
1615+
(HasCallStack, ShelleyEraImp era) =>
1616+
Credential 'Staking ->
1617+
ImpTestM era ()
1618+
expectStakeCredRegistered cred = do
1619+
accounts <- getsNES $ nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL
1620+
expectedDeposit <- getsNES $ nesEsL . curPParamsEpochStateL . ppKeyDepositL
1621+
accountState <- expectJust $ lookupAccountState cred accounts
1622+
impAnn (show cred <> " expected to be in Accounts with the correct deposit") $ do
1623+
accountState ^. depositAccountStateL `shouldBe` compactCoinOrError expectedDeposit
1624+
1625+
expectStakeCredNotRegistered ::
1626+
(HasCallStack, ShelleyEraImp era) =>
1627+
Credential 'Staking ->
1628+
ImpTestM era ()
1629+
expectStakeCredNotRegistered cred = do
1630+
accounts <- getsNES $ nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL
1631+
impAnn (show cred <> " expected to not be in Accounts") $ do
1632+
expectNothingExpr $ lookupAccountState cred accounts
1633+
1634+
expectDelegatedToPool ::
1635+
(HasCallStack, ShelleyEraImp era) =>
1636+
Credential 'Staking ->
1637+
KeyHash 'StakePool ->
1638+
ImpTestM era ()
1639+
expectDelegatedToPool cred poolKh = do
1640+
accounts <- getsNES $ nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL
1641+
impAnn (show cred <> " expected to have delegated to " <> show poolKh) $ do
1642+
accountState <- expectJust $ lookupAccountState cred accounts
1643+
accountState ^. stakePoolDelegationAccountStateL `shouldBe` Just poolKh
1644+
1645+
expectNotDelegatedToPool ::
1646+
(HasCallStack, ShelleyEraImp era) =>
1647+
Credential 'Staking ->
1648+
ImpTestM era ()
1649+
expectNotDelegatedToPool cred = do
1650+
accounts <- getsNES $ nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL
1651+
impAnn (show cred <> " expected to not have delegated to a stake pool") $ do
1652+
accountState <- expectJust $ lookupAccountState cred accounts
1653+
expectNothingExpr (accountState ^. stakePoolDelegationAccountStateL)
1654+
16101655
registerRewardAccount ::
16111656
forall era.
16121657
( HasCallStack

0 commit comments

Comments
 (0)