Skip to content

Commit de8b54b

Browse files
committed
Update checks in DelegSpec to include delegs in StakePoolState
and add a test case for redelegating drep
1 parent 6799187 commit de8b54b

File tree

3 files changed

+81
-12
lines changed

3 files changed

+81
-12
lines changed

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

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ spec = do
184184
.~ [DelegTxCert cred (DelegStake poolKh)]
185185
)
186186
[injectFailure $ DelegateeStakePoolNotRegisteredDELEG poolKh]
187-
expectNotDelegatedToPool cred
187+
expectNotDelegatedToAnyPool cred
188188

189189
describe "Delegate vote" $ do
190190
it "Delegate vote of registered stake credentials to registered drep" $ do
@@ -203,7 +203,7 @@ spec = do
203203
.~ [DelegTxCert cred (DelegVote (DRepCredential drepCred))]
204204

205205
expectDelegatedVote cred (DRepCredential drepCred)
206-
expectNotDelegatedToPool cred
206+
expectNotDelegatedToAnyPool cred
207207
whenBootstrap $ do
208208
impAnn "Ensure DRep delegation is populated after bootstrap" $ do
209209
-- Clear out delegation, in order to check its repopulation from accounts.
@@ -223,6 +223,25 @@ spec = do
223223
getLastEnactedHardForkInitiation `shouldReturn` SJust (GovPurposeId gai)
224224
expectDelegatedVote cred (DRepCredential drepCred)
225225

226+
it "Redelegate vote to the same DRep" $ do
227+
expectedDeposit <- getsNES $ nesEsL . curPParamsEpochStateL . ppKeyDepositL
228+
229+
cred <- KeyHashObj <$> freshKeyHash
230+
drepCred <- KeyHashObj <$> registerDRep
231+
232+
submitTx_ $
233+
mkBasicTx mkBasicTxBody
234+
& bodyTxL . certsTxBodyL
235+
.~ [RegDepositDelegTxCert cred (DelegVote (DRepCredential drepCred)) expectedDeposit]
236+
expectDelegatedVote cred (DRepCredential drepCred)
237+
238+
submitTx_ $
239+
mkBasicTx mkBasicTxBody
240+
& bodyTxL . certsTxBodyL
241+
.~ [DelegTxCert cred (DelegVote (DRepCredential drepCred))]
242+
243+
expectDelegatedVote cred (DRepCredential drepCred)
244+
226245
it "Delegate vote of registered stake credentials to unregistered drep" $ do
227246
RewardAccount _ cred <- registerRewardAccount
228247
drepCred <- KeyHashObj <$> freshKeyHash
@@ -284,7 +303,13 @@ spec = do
284303
impAnn "Check that unregistration of previous delegation does not affect current delegation" $ do
285304
unRegisterDRep drepCred
286305
-- we need to preserve the buggy behavior until the boostrap phase is over.
287-
ifBootstrap (expectNotDelegatedVote cred) (expectDelegatedVote cred (DRepCredential drepCred2))
306+
ifBootstrap
307+
( do
308+
-- we cannot `expectNotDelegatedVote` because the the delegation is still in the DRepState of the other pool
309+
accounts <- getsNES $ nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL
310+
expectNothingExpr (lookupDRepDelegation cred accounts)
311+
)
312+
(expectDelegatedVote cred (DRepCredential drepCred2))
288313

289314
it "Delegate vote and unregister stake credentials" $ do
290315
expectedDeposit <- getsNES $ nesEsL . curPParamsEpochStateL . ppKeyDepositL
@@ -300,6 +325,8 @@ spec = do
300325
.~ [UnRegDepositTxCert cred expectedDeposit]
301326
expectStakeCredNotRegistered cred
302327
expectNotDelegatedVote cred
328+
expectNotDelegatedToAnyPool cred
329+
303330
-- https://github.com/IntersectMBO/formal-ledger-specifications/issues/917
304331
-- TODO: Re-enable after issue is resolved, by removing this override
305332
disableInConformanceIt "Delegate vote and unregister after hardfork" $ do
@@ -412,9 +439,9 @@ spec = do
412439

413440
-- when pool is re-registered after its expiration, all delegations are cleared
414441
passNEpochs $ fromIntegral poolLifetime
415-
expectNotDelegatedToPool cred
442+
expectNotDelegatedToAnyPool cred
416443
registerPoolWithRewardAccount poolKh rewardAccount
417-
expectNotDelegatedToPool cred
444+
expectNotDelegatedToAnyPool cred
418445
-- the vote delegation is kept
419446
expectDelegatedVote cred (DRepCredential drepCred)
420447

@@ -502,6 +529,7 @@ spec = do
502529
& bodyTxL . certsTxBodyL
503530
.~ [DelegTxCert cred (DelegStake poolKh')]
504531
expectDelegatedToPool cred poolKh'
532+
expectNotDelegatedToPool cred poolKh
505533
expectDelegatedVote cred (DRepCredential drepCred)
506534
where
507535
expectDelegatedVote :: HasCallStack => Credential 'Staking -> DRep -> ImpTestM era ()
@@ -527,8 +555,12 @@ spec = do
527555
expectNotDelegatedVote :: Credential 'Staking -> ImpTestM era ()
528556
expectNotDelegatedVote cred = do
529557
accounts <- getsNES $ nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL
530-
impAnn (show cred <> " expected to not have their vote delegated") $
558+
dreps <- getsNES $ nesEsL . epochStateRegDrepL
559+
impAnn (show cred <> " expected to not have their vote delegated") $ do
531560
expectNothingExpr (lookupDRepDelegation cred accounts)
561+
assertBool
562+
("Expected no drep state delegation to contain the stake credential: " <> show cred)
563+
(all (Set.notMember cred . drepDelegs) (Map.elems dreps))
532564

533565
conwayEraSpecificSpec :: SpecWith (ImpInit (LedgerSpec ConwayEra))
534566
conwayEraSpecificSpec = do

eras/shelley/impl/testlib/Test/Cardano/Ledger/Shelley/Imp/DelegSpec.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ shelleyEraSpecificSpec = do
5454
& bodyTxL . certsTxBodyL .~ [delegStakeTxCert cred poolKh]
5555
)
5656
[injectFailure $ DelegateeNotRegisteredDELEG poolKh]
57-
expectNotDelegatedToPool cred
57+
expectNotDelegatedToAnyPool cred
5858

5959
it "Deregistering returns the deposit" $ do
6060
let keyDeposit = Coin 2
@@ -228,6 +228,7 @@ spec = do
228228
else StakeKeyNotRegisteredDELEG cred
229229
]
230230
expectStakeCredNotRegistered cred
231+
expectNotDelegatedToAnyPool cred
231232

232233
it "Delegate already delegated credentials" $ do
233234
cred <- KeyHashObj <$> freshKeyHash
@@ -251,6 +252,7 @@ spec = do
251252
& bodyTxL . certsTxBodyL
252253
.~ [delegStakeTxCert cred poolKh1]
253254
expectDelegatedToPool cred poolKh1
255+
expectNotDelegatedToPool cred poolKh
254256

255257
poolKh2 <- freshKeyHash
256258
registerPool poolKh2
@@ -265,6 +267,8 @@ spec = do
265267
]
266268

267269
expectDelegatedToPool cred poolKh3
270+
expectNotDelegatedToPool cred poolKh2
271+
expectNotDelegatedToPool cred poolKh
268272

269273
it "Delegate and unregister" $ do
270274
cred <- KeyHashObj <$> freshKeyHash
@@ -277,3 +281,4 @@ spec = do
277281
& bodyTxL . certsTxBodyL
278282
.~ [regTxCert, delegStakeTxCert cred poolKh, unRegTxCert]
279283
expectStakeCredNotRegistered cred
284+
expectNotDelegatedToAnyPool cred

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

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ module Test.Cardano.Ledger.Shelley.ImpTest (
7171
delegateStake,
7272
registerRewardAccount,
7373
registerStakeCredential,
74+
expectNotDelegatedToAnyPool,
7475
expectNotDelegatedToPool,
7576
expectStakeCredRegistered,
7677
expectStakeCredNotRegistered,
@@ -1637,20 +1638,51 @@ expectDelegatedToPool ::
16371638
KeyHash 'StakePool ->
16381639
ImpTestM era ()
16391640
expectDelegatedToPool cred poolKh = do
1640-
accounts <- getsNES $ nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL
1641+
certState <- getsNES $ nesEsL . esLStateL . lsCertStateL
1642+
let accounts = certState ^. certDStateL . accountsL
1643+
let pools = certState ^. certPStateL . psStakePoolsL
16411644
impAnn (show cred <> " expected to have delegated to " <> show poolKh) $ do
16421645
accountState <- expectJust $ lookupAccountState cred accounts
16431646
accountState ^. stakePoolDelegationAccountStateL `shouldBe` Just poolKh
1647+
case Map.lookup poolKh pools of
1648+
Nothing ->
1649+
assertFailure $
1650+
"Expected stake pool state for: " <> show poolKh
1651+
Just poolState ->
1652+
assertBool
1653+
("Expected pool delegations to contain the stake credential: " <> show cred)
1654+
(cred `Set.member` (poolState ^. spsDelegsL))
1655+
1656+
expectNotDelegatedToAnyPool ::
1657+
(HasCallStack, ShelleyEraImp era) =>
1658+
Credential 'Staking ->
1659+
ImpTestM era ()
1660+
expectNotDelegatedToAnyPool cred = do
1661+
certState <- getsNES $ nesEsL . esLStateL . lsCertStateL
1662+
let accounts = certState ^. certDStateL . accountsL
1663+
let pools = certState ^. certPStateL . psStakePoolsL
1664+
impAnn (show cred <> " expected to not have delegated to a stake pool") $ do
1665+
forM_ (lookupAccountState cred accounts) $ \accountState ->
1666+
expectNothingExpr (accountState ^. stakePoolDelegationAccountStateL)
1667+
assertBool
1668+
("Expected no stake pool state delegation to contain the stake credential: " <> show cred)
1669+
(all (Set.notMember cred . spsDelegs) (Map.elems pools))
16441670

16451671
expectNotDelegatedToPool ::
16461672
(HasCallStack, ShelleyEraImp era) =>
16471673
Credential 'Staking ->
1674+
KeyHash 'StakePool ->
16481675
ImpTestM era ()
1649-
expectNotDelegatedToPool cred = do
1650-
accounts <- getsNES $ nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL
1676+
expectNotDelegatedToPool cred pool = do
1677+
certState <- getsNES $ nesEsL . esLStateL . lsCertStateL
1678+
let accounts = certState ^. certDStateL . accountsL
1679+
let pools = certState ^. certPStateL . psStakePoolsL
16511680
impAnn (show cred <> " expected to not have delegated to a stake pool") $ do
1652-
accountState <- expectJust $ lookupAccountState cred accounts
1653-
expectNothingExpr (accountState ^. stakePoolDelegationAccountStateL)
1681+
forM_ (lookupAccountState cred accounts) $ \accountState ->
1682+
accountState ^. stakePoolDelegationAccountStateL `shouldNotBe` Just pool
1683+
assertBool
1684+
("Expected stake pool state delegation to not contain the stake credential: " <> show cred)
1685+
(maybe True (Set.notMember cred . spsDelegs) (Map.lookup pool pools))
16541686

16551687
registerRewardAccount ::
16561688
forall era.

0 commit comments

Comments
 (0)