Skip to content

Commit f44c36b

Browse files
committed
Update conway Transition to set the pool delegations
and add a test for it
1 parent 7500f0b commit f44c36b

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

eras/conway/impl/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 1.21.0.0
44

5+
* Expose `conwayRegisterInitialAccounts`
56
* Move withdrawal-validation and DRep expiry updates from `CERTS` to `LEDGER` starting protocol version 11.
67
- Add `ConwayWithdrawalsMissingAccounts` and `ConwayIncompleteWithdrawals` to `ConwayLedgerPredFailure`.
78
- Add `hardforkConwayMoveWithdrawalsAndDRepChecksToLedgerRule` to `Conway.Era`.

eras/conway/impl/src/Cardano/Ledger/Conway/Transition.hs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module Cardano.Ledger.Conway.Transition (
1616
TransitionConfig (..),
1717
toConwayTransitionConfigPairs,
1818
registerDRepsThenDelegs,
19+
conwayRegisterInitialAccounts,
1920
conwayRegisterInitialFundsThenStaking,
2021
) where
2122

@@ -44,6 +45,7 @@ import Data.Aeson (KeyValue (..))
4445
import Data.ListMap (ListMap)
4546
import qualified Data.ListMap as ListMap
4647
import qualified Data.Map.Strict as Map
48+
import qualified Data.Set as Set
4749
import GHC.Generics
4850
import GHC.Stack
4951
import Lens.Micro
@@ -127,14 +129,20 @@ conwayRegisterInitialAccounts ::
127129
NewEpochState era
128130
conwayRegisterInitialAccounts ShelleyGenesisStaking {sgsStake} nes =
129131
nes
130-
& nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL %~ \initAccounts ->
131-
foldr registerAndDelegate initAccounts $ ListMap.toList sgsStake
132+
& nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL .~ updatedAccounts
133+
& nesEsL . esLStateL . lsCertStateL . certPStateL . psStakePoolsL .~ updatedStakePoolStates
132134
where
133135
stakePools = nes ^. nesEsL . esLStateL . lsCertStateL . certPStateL . psStakePoolsL
136+
initialAccounts = nes ^. nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL
134137
deposit = compactCoinOrError $ nes ^. nesEsL . curPParamsEpochStateL . ppKeyDepositL
135-
registerAndDelegate (stakeKeyHash, stakePool) !accounts
138+
139+
!(!updatedAccounts, !updatedStakePoolStates) =
140+
foldr registerAndDelegate (initialAccounts, stakePools) (ListMap.toList sgsStake)
141+
registerAndDelegate (stakeKeyHash, stakePool) (!accounts, !stakePoolMap)
136142
| stakePool `Map.member` stakePools =
137-
registerConwayAccount (KeyHashObj stakeKeyHash) deposit (Just (DelegStake stakePool)) accounts
143+
( (registerConwayAccount (KeyHashObj stakeKeyHash) deposit (Just (DelegStake stakePool)) accounts)
144+
, Map.adjust (spsDelegsL %~ Set.insert (KeyHashObj stakeKeyHash)) stakePool stakePoolMap
145+
)
138146
| otherwise =
139147
error $
140148
"Invariant of a delegation of "

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,18 @@ import Cardano.Ledger.Conway.Core
2828
import Cardano.Ledger.Conway.Governance
2929
import Cardano.Ledger.Conway.Rules (ConwayDelegPredFailure (..))
3030
import Cardano.Ledger.Conway.State hiding (balance)
31+
import Cardano.Ledger.Conway.Transition (conwayRegisterInitialAccounts)
3132
import Cardano.Ledger.Conway.TxCert
3233
import Cardano.Ledger.Credential (Credential (..))
3334
import Cardano.Ledger.DRep
3435
import Cardano.Ledger.Plutus (
3536
SLanguage (..),
3637
hashPlutusScript,
3738
)
39+
import Cardano.Ledger.Shelley.Genesis (ShelleyGenesisStaking (..))
3840
import Cardano.Ledger.Shelley.LedgerState
3941
import Cardano.Ledger.Val (Val (..))
42+
import qualified Data.ListMap as LM
4043
import qualified Data.Map.Strict as Map
4144
import qualified Data.Sequence.Strict as SSeq
4245
import qualified Data.Set as Set
@@ -531,6 +534,29 @@ spec = do
531534
expectDelegatedToPool cred poolKh'
532535
expectNotDelegatedToPool cred poolKh
533536
expectDelegatedVote cred (DRepCredential drepCred)
537+
538+
it "Transition creates the delegations correctly" $ do
539+
pool1 <- freshKeyHash >>= \kh -> kh <$ registerPool kh
540+
pool2 <- freshKeyHash >>= \kh -> kh <$ registerPool kh
541+
pool3 <- freshKeyHash >>= \kh -> kh <$ registerPool kh
542+
poolParams <- freshKeyHash >>= \kh -> registerRewardAccount >>= freshPoolParams kh
543+
deleg1 <- freshKeyHash >>= \kh -> kh <$ registerStakeCredential (KeyHashObj kh)
544+
deleg2 <- freshKeyHash >>= \kh -> kh <$ registerStakeCredential (KeyHashObj kh)
545+
deleg3 <- freshKeyHash >>= \kh -> kh <$ registerStakeCredential (KeyHashObj kh)
546+
nes <- getsNES id
547+
let sgs =
548+
ShelleyGenesisStaking
549+
{ sgsPools = LM.ListMap [(pool1, poolParams), (pool2, poolParams), (pool3, poolParams)]
550+
, sgsStake = LM.ListMap [(deleg1, pool1), (deleg2, pool1), (deleg3, pool2)]
551+
}
552+
let updatedNES = conwayRegisterInitialAccounts sgs nes
553+
delegateStake (KeyHashObj deleg1) pool1
554+
delegateStake (KeyHashObj deleg2) pool1
555+
delegateStake (KeyHashObj deleg3) pool2
556+
getPoolsState <$> (getsNES id) `shouldReturn` getPoolsState updatedNES
557+
getDelegs deleg1 updatedNES `shouldReturn` Just pool1
558+
getDelegs deleg2 updatedNES `shouldReturn` Just pool1
559+
getDelegs deleg3 updatedNES `shouldReturn` Just pool2
534560
where
535561
expectDelegatedVote :: HasCallStack => Credential 'Staking -> DRep -> ImpTestM era ()
536562
expectDelegatedVote cred drep = do
@@ -561,6 +587,10 @@ spec = do
561587
assertBool
562588
("Expected no drep state delegation to contain the stake credential: " <> show cred)
563589
(all (Set.notMember cred . drepDelegs) (Map.elems dreps))
590+
getDelegs kh nes = do
591+
let accounts = nes ^. nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL . accountsMapL
592+
pure $ Map.lookup (KeyHashObj kh) accounts >>= (^. stakePoolDelegationAccountStateL)
593+
getPoolsState nes = nes ^. nesEsL . esLStateL . lsCertStateL . certPStateL . psStakePoolsL
564594

565595
conwayEraSpecificSpec :: SpecWith (ImpInit (LedgerSpec ConwayEra))
566596
conwayEraSpecificSpec = do

0 commit comments

Comments
 (0)