Skip to content

Commit 7500f0b

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

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

eras/shelley/impl/src/Cardano/Ledger/Shelley/Transition.hs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import Data.Default
7575
import Data.Kind
7676
import qualified Data.ListMap as ListMap
7777
import qualified Data.Map.Strict as Map
78+
import qualified Data.Set as Set
7879
import Data.Typeable
7980
import Data.Void (Void)
8081
import GHC.Generics (Generic)
@@ -409,14 +410,20 @@ shelleyRegisterInitialAccounts ::
409410
NewEpochState era
410411
shelleyRegisterInitialAccounts ShelleyGenesisStaking {sgsStake} nes =
411412
nes
412-
& nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL %~ \initAccounts ->
413-
foldr registerAndDelegate initAccounts $ zip (ListMap.toList sgsStake) ptrs
413+
& nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL .~ updatedAccounts
414+
& nesEsL . esLStateL . lsCertStateL . certPStateL . psStakePoolsL .~ updatedStakePoolStates
414415
where
415416
stakePools = nes ^. nesEsL . esLStateL . lsCertStateL . certPStateL . psStakePoolsL
417+
initialAccounts = nes ^. nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL
416418
deposit = compactCoinOrError $ nes ^. nesEsL . curPParamsEpochStateL . ppKeyDepositL
417-
registerAndDelegate ((stakeKeyHash, stakePool), ptr) !accounts
419+
420+
!(!updatedAccounts, !updatedStakePoolStates) =
421+
foldr registerAndDelegate (initialAccounts, stakePools) (zip (ListMap.toList sgsStake) ptrs)
422+
registerAndDelegate ((stakeKeyHash, stakePool), ptr) (!accounts, !stakePoolMap)
418423
| stakePool `Map.member` stakePools =
419-
registerShelleyAccount (KeyHashObj stakeKeyHash) ptr deposit (Just stakePool) accounts
424+
( (registerShelleyAccount (KeyHashObj stakeKeyHash) ptr deposit (Just stakePool) accounts)
425+
, Map.adjust (spsDelegsL %~ Set.insert (KeyHashObj stakeKeyHash)) stakePool stakePoolMap
426+
)
420427
| otherwise =
421428
error $
422429
"Invariant of a delegation of "

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Test.Cardano.Ledger.Shelley.Imp (spec, shelleyEraSpecificSpec) where
1010
import Cardano.Ledger.Shelley (ShelleyEra)
1111
import Cardano.Ledger.Shelley.Core
1212
import Cardano.Ledger.Shelley.Rules
13+
import Cardano.Ledger.Shelley.State (ShelleyEraAccounts)
1314
import Test.Cardano.Ledger.Imp.Common
1415
import qualified Test.Cardano.Ledger.Shelley.Imp.DelegSpec as Deleg
1516
import qualified Test.Cardano.Ledger.Shelley.Imp.EpochSpec as Epoch
@@ -41,6 +42,7 @@ spec = do
4142
shelleyEraSpecificSpec ::
4243
forall era.
4344
( ShelleyEraImp era
45+
, ShelleyEraAccounts era
4446
, InjectRuleFailure "LEDGER" ShelleyDelegsPredFailure era
4547
) =>
4648
SpecWith (ImpInit (LedgerSpec era))

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ import Cardano.Ledger.BaseTypes
1616
import Cardano.Ledger.Coin (Coin (Coin))
1717
import Cardano.Ledger.Credential (Credential (..))
1818
import Cardano.Ledger.Shelley.Core
19+
import Cardano.Ledger.Shelley.Genesis
20+
import Cardano.Ledger.Shelley.LedgerState
1921
import Cardano.Ledger.Shelley.Rules
2022
import Cardano.Ledger.Shelley.Scripts
23+
import Cardano.Ledger.Shelley.State (ShelleyEraAccounts)
24+
import Cardano.Ledger.Shelley.Transition (shelleyRegisterInitialAccounts)
25+
import Cardano.Ledger.State (accountsL, accountsMapL, stakePoolDelegationAccountStateL)
26+
import qualified Data.ListMap as LM
2127
import qualified Data.Map.Strict as Map
2228
import Lens.Micro
2329
import Test.Cardano.Ledger.Imp.Common
@@ -26,6 +32,7 @@ import Test.Cardano.Ledger.Shelley.ImpTest
2632

2733
shelleyEraSpecificSpec ::
2834
( ShelleyEraImp era
35+
, ShelleyEraAccounts era
2936
, InjectRuleFailure "LEDGER" ShelleyDelegsPredFailure era
3037
) =>
3138
SpecWith (ImpInit (LedgerSpec era))
@@ -100,6 +107,34 @@ shelleyEraSpecificSpec = do
100107
getBalance otherStakeCred `shouldReturn` Coin 0
101108
expectNotRegisteredRewardAddress rewardAccount
102109

110+
it "Transition creates the delegations correctly" $ do
111+
pool1 <- freshKeyHash >>= \kh -> kh <$ registerPool kh
112+
pool2 <- freshKeyHash >>= \kh -> kh <$ registerPool kh
113+
pool3 <- freshKeyHash >>= \kh -> kh <$ registerPool kh
114+
poolParams <- freshKeyHash >>= \kh -> registerRewardAccount >>= freshPoolParams kh
115+
deleg1 <- freshKeyHash >>= \kh -> kh <$ registerStakeCredential (KeyHashObj kh)
116+
deleg2 <- freshKeyHash >>= \kh -> kh <$ registerStakeCredential (KeyHashObj kh)
117+
deleg3 <- freshKeyHash >>= \kh -> kh <$ registerStakeCredential (KeyHashObj kh)
118+
nes <- getsNES id
119+
let sgs =
120+
ShelleyGenesisStaking
121+
{ sgsPools = LM.ListMap [(pool1, poolParams), (pool2, poolParams), (pool3, poolParams)]
122+
, sgsStake = LM.ListMap [(deleg1, pool1), (deleg2, pool1), (deleg3, pool2)]
123+
}
124+
let updatedNES = shelleyRegisterInitialAccounts sgs nes
125+
delegateStake (KeyHashObj deleg1) pool1
126+
delegateStake (KeyHashObj deleg2) pool1
127+
delegateStake (KeyHashObj deleg3) pool2
128+
getPoolsState <$> (getsNES id) `shouldReturn` getPoolsState updatedNES
129+
getDelegs deleg1 updatedNES `shouldReturn` Just pool1
130+
getDelegs deleg2 updatedNES `shouldReturn` Just pool1
131+
getDelegs deleg3 updatedNES `shouldReturn` Just pool2
132+
where
133+
getDelegs kh nes = do
134+
let accounts = nes ^. nesEsL . esLStateL . lsCertStateL . certDStateL . accountsL . accountsMapL
135+
pure $ Map.lookup (KeyHashObj kh) accounts >>= (^. stakePoolDelegationAccountStateL)
136+
getPoolsState nes = nes ^. nesEsL . esLStateL . lsCertStateL . certPStateL . psStakePoolsL
137+
103138
spec ::
104139
ShelleyEraImp era =>
105140
SpecWith (ImpInit (LedgerSpec era))

0 commit comments

Comments
 (0)