Skip to content

Commit 1a5df46

Browse files
committed
WIP
1 parent 0b3b8dc commit 1a5df46

File tree

31 files changed

+424
-311
lines changed

31 files changed

+424
-311
lines changed

eras/conway/impl/src/Cardano/Ledger/Conway/State/Stake.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ resolveConwayInstantStake ::
124124
(EraStake era, InstantStake era ~ ConwayInstantStake era) =>
125125
ConwayInstantStake era ->
126126
Accounts era ->
127-
Stake
127+
ActiveStake
128128
resolveConwayInstantStake instantStake accounts =
129-
Stake $ VMap.fromMap $ resolveActiveInstantStakeCredentials instantStake accounts
129+
ActiveStake $ VMap.fromMap $ resolveActiveInstantStakeCredentials instantStake accounts
130130
{-# INLINE resolveConwayInstantStake #-}

eras/shelley/impl/src/Cardano/Ledger/Shelley/API/Wallet.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,14 @@ getNonMyopicMemberRewards globals ss = Map.fromSet nmmRewards
228228
totalStakeCoin@(Coin totalStake) = circulation es maxSupply
229229
toShare (Coin x) = StakeShare $ x %? totalStake
230230
memShare (Right cred) =
231-
toShare $ maybe mempty fromCompact $ VMap.lookup cred (EB.unStake stake)
231+
toShare $
232+
maybe mempty (fromCompact . unNonZero . EB.swdStake) $
233+
VMap.lookup cred (EB.unActiveStake activeStake)
232234
memShare (Left coin) = toShare coin
233235
es = nesEs ss
234236
pp = es ^. curPParamsEpochStateL
235237
NonMyopic {likelihoodsNM = ls, rewardPotNM = rPot} = esNonMyopic es
236-
EB.SnapShot stake _ _ stakePoolsSnapShot = currentSnapshot ss
238+
EB.SnapShot activeStake _ stakePoolsSnapShot = currentSnapshot ss
237239
calcNMMRewards t poolId spss
238240
| spssPledge <= spssSelfDelegatedOwnersStake =
239241
calcNonMyopicMemberReward pp rPot poolId spssCost spssMargin s sigma t topPools hitRateEst

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ module Cardano.Ledger.Shelley.LedgerState (
115115
ssStakeGoL,
116116
ssFeeL,
117117
ssStakeL,
118-
ssStakeDistrL,
119-
ssDelegationsL,
120118
) where
121119

122120
import Cardano.Ledger.Shelley.LedgerState.IncrementalStake

eras/shelley/impl/src/Cardano/Ledger/Shelley/LedgerState/PulsingReward.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ startStep ::
9797
NonZero Word64 ->
9898
PulsingRewUpdate
9999
startStep slotsPerEpoch b@(BlocksMade b') es@(EpochState acnt ls ss nm) maxSupply asc secparam =
100-
let SnapShot stake totalActiveStake delegs stakePoolSnapShots = ssStakeGo ss
101-
numStakeCreds = fromIntegral (VMap.size $ unStake stake)
100+
let SnapShot activeStake totalActiveStake stakePoolSnapShots = ssStakeGo ss
101+
numStakeCreds = fromIntegral (VMap.size $ unActiveStake activeStake)
102102
k = toIntegerNonZero secparam
103103
-- We expect approximately 10k-many blocks to be produced each epoch.
104104
-- The reward calculation begins (4k/f)-many slots into the epoch,
@@ -198,7 +198,6 @@ startStep slotsPerEpoch b@(BlocksMade b') es@(EpochState acnt ls ss nm) maxSuppl
198198
-- the neccessary information to compute their individual rewards.
199199
free =
200200
FreeVars
201-
delegs
202201
(Map.keysSet (accounts ^. accountsMapL)) -- TODO optimize. This is an expensive operation
203202
totalStake
204203
(pr ^. ppProtocolVersionL)
@@ -208,7 +207,7 @@ startStep slotsPerEpoch b@(BlocksMade b') es@(EpochState acnt ls ss nm) maxSuppl
208207
RSLP
209208
pulseSize
210209
free
211-
(unStake stake)
210+
(unActiveStake activeStake)
212211
(RewardAns Map.empty Map.empty)
213212
in Pulsing rewsnap pulser
214213

eras/shelley/impl/src/Cardano/Ledger/Shelley/LedgerState/Types.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import Cardano.Ledger.Binary (
4343
encodeMemPack,
4444
)
4545
import Cardano.Ledger.Binary.Coders (Decode (From, RecD), Encode (..), decode, encode, (!>), (<!))
46-
import Cardano.Ledger.Coin (Coin (..), CompactForm)
46+
import Cardano.Ledger.Coin (Coin (..))
4747
import Cardano.Ledger.Credential (Credential (..))
4848
import Cardano.Ledger.Shelley.Core
4949
import Cardano.Ledger.Shelley.Era (ShelleyEra)
@@ -56,7 +56,6 @@ import Control.Monad.Trans (MonadTrans (lift))
5656
import Data.Aeson (ToJSON (..), (.=))
5757
import Data.Default (Default, def)
5858
import Data.Map.Strict (Map)
59-
import Data.VMap (VB, VMap, VP)
6059
import GHC.Generics (Generic)
6160
import Lens.Micro
6261
import NoThunks.Class (NoThunks (..))
@@ -671,8 +670,8 @@ epochStateStakePoolsL ::
671670
epochStateStakePoolsL = esLStateL . lsCertStateL . certPStateL . psStakePoolsL
672671

673672
epochStateStakeDistrL ::
674-
Lens' (EpochState era) (VMap VB VP (Credential Staking) (CompactForm Coin))
675-
epochStateStakeDistrL = esSnapshotsL . ssStakeMarkL . ssStakeDistrL
673+
Lens' (EpochState era) ActiveStake
674+
epochStateStakeDistrL = esSnapshotsL . ssStakeMarkL . ssActiveStakeL
676675

677676
potEqualsObligation ::
678677
(EraGov era, EraCertState era) =>

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import Cardano.Ledger.BaseTypes (
3737
ProtVer (..),
3838
ShelleyBase,
3939
ToKeyValuePairs (..),
40+
unNonZero,
4041
)
4142
import Cardano.Ledger.Binary (
4243
DecCBOR (..),
@@ -53,7 +54,7 @@ import Cardano.Ledger.Binary.Coders (
5354
(!>),
5455
(<!),
5556
)
56-
import Cardano.Ledger.Coin (Coin (..), CompactForm, DeltaCoin (..))
57+
import Cardano.Ledger.Coin (Coin (..), DeltaCoin (..))
5758
import Cardano.Ledger.Compactible (Compactible (fromCompact))
5859
import Cardano.Ledger.Credential (Credential (..))
5960
import Cardano.Ledger.Keys (KeyHash, KeyRole (..))
@@ -63,6 +64,7 @@ import Cardano.Ledger.Shelley.Rewards (
6364
PoolRewardInfo (..),
6465
rewardOnePoolMember,
6566
)
67+
import Cardano.Ledger.State (StakeWithDelegation (..))
6668
import Control.DeepSeq (NFData (..))
6769
import Data.Aeson (ToJSON (..), Value (Null), (.=))
6870
import Data.Default (def)
@@ -206,8 +208,7 @@ instance DecCBOR RewardSnapShot where
206208
-- Pulsable function.
207209

208210
data FreeVars = FreeVars
209-
{ fvDelegs :: !(VMap VB VB (Credential Staking) (KeyHash StakePool))
210-
, fvAddrsRew :: !(Set (Credential Staking))
211+
{ fvAddrsRew :: !(Set (Credential Staking))
211212
, fvTotalStake :: !Coin
212213
, fvProtVer :: !ProtVer
213214
, fvPoolRewardInfo :: !(VMap VB VB (KeyHash StakePool) PoolRewardInfo)
@@ -220,15 +221,13 @@ instance NFData FreeVars
220221
instance EncCBOR FreeVars where
221222
encCBOR
222223
FreeVars
223-
{ fvDelegs
224-
, fvAddrsRew
224+
{ fvAddrsRew
225225
, fvTotalStake
226226
, fvProtVer
227227
, fvPoolRewardInfo
228228
} =
229229
encode
230230
( Rec FreeVars
231-
!> To fvDelegs
232231
!> To fvAddrsRew
233232
!> To fvTotalStake
234233
!> To fvProtVer
@@ -239,7 +238,6 @@ instance DecCBOR FreeVars where
239238
decCBOR =
240239
decode
241240
( RecD FreeVars
242-
<! From {- fvDelegs -}
243241
<! From {- fvAddrsRew -}
244242
<! From {- fvTotalStake -}
245243
<! From {- fvProtver -}
@@ -253,20 +251,23 @@ rewardStakePoolMember ::
253251
FreeVars ->
254252
RewardAns ->
255253
Credential Staking ->
256-
CompactForm Coin ->
254+
StakeWithDelegation ->
257255
RewardAns
258-
rewardStakePoolMember freeVars inputAnswer@(RewardAns accum recent) cred c =
256+
rewardStakePoolMember freeVars inputAnswer@(RewardAns accum recent) cred swd =
259257
fromMaybe inputAnswer $ do
260258
let FreeVars
261-
{ fvDelegs
262-
, fvAddrsRew
259+
{ fvAddrsRew
263260
, fvTotalStake
264261
, fvPoolRewardInfo
265262
, fvProtVer
266263
} = freeVars
267-
poolId <- VMap.lookup cred fvDelegs
264+
poolId = swdDelegation swd
268265
poolRI <- VMap.lookup poolId fvPoolRewardInfo
269-
r <- rewardOnePoolMember fvProtVer fvTotalStake fvAddrsRew poolRI cred (fromCompact c)
266+
r <-
267+
rewardOnePoolMember fvProtVer fvTotalStake fvAddrsRew poolRI cred $
268+
fromCompact $
269+
unNonZero $
270+
swdStake swd
270271
let ans = Reward MemberReward poolId r
271272
-- There is always just 1 member reward, so Set.singleton is appropriate
272273
pure $ RewardAns (Map.insert cred ans accum) (Map.insert cred (Set.singleton ans) recent)
@@ -285,7 +286,7 @@ data RewardPulser (m :: Type -> Type) ans where
285286
(ans ~ RewardAns, m ~ ShelleyBase) =>
286287
!Int ->
287288
!FreeVars ->
288-
!(VMap.VMap VMap.VB VMap.VP (Credential Staking) (CompactForm Coin)) ->
289+
!(VMap.VMap VMap.VB VMap.VB (Credential Staking) StakeWithDelegation) ->
289290
!ans ->
290291
RewardPulser m ans
291292

eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/Snap.hs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ module Cardano.Ledger.Shelley.Rules.Snap (
1616
SnapEnv (..),
1717
) where
1818

19-
import Cardano.Ledger.BaseTypes (ShelleyBase)
20-
import Cardano.Ledger.Coin (Coin, CompactForm)
19+
import Cardano.Ledger.BaseTypes (ShelleyBase, unNonZero)
20+
import Cardano.Ledger.Coin (Coin)
2121
import Cardano.Ledger.Compactible (fromCompact)
2222
import Cardano.Ledger.Core
2323
import Cardano.Ledger.Credential (Credential)
@@ -85,17 +85,11 @@ snapTransition = do
8585
snapShotFromInstantStake instantStake (certState ^. certDStateL) (certState ^. certPStateL)
8686

8787
tellEvent $
88-
let stMap :: Map (Credential Staking) (CompactForm Coin)
89-
stMap = VMap.toMap . unStake $ ssActiveStake istakeSnap
90-
91-
stakeCoinMap :: Map (Credential Staking) Coin
92-
stakeCoinMap = fmap fromCompact stMap
93-
94-
stakePoolMap :: Map (Credential Staking) (KeyHash StakePool)
95-
stakePoolMap = VMap.toMap $ ssDelegations istakeSnap
96-
97-
stakeMap :: Map (Credential Staking) (Coin, KeyHash StakePool)
98-
stakeMap = Map.intersectionWith (,) stakeCoinMap stakePoolMap
88+
let stakeMap :: Map (Credential Staking) (Coin, KeyHash StakePool)
89+
stakeMap =
90+
Map.map
91+
(\swd -> (fromCompact $ unNonZero $ swdStake swd, swdDelegation swd))
92+
(VMap.toMap $ unActiveStake $ ssActiveStake istakeSnap)
9993
in StakeDistEvent stakeMap
10094

10195
pure $

eras/shelley/impl/src/Cardano/Ledger/Shelley/State/Stake.hs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ module Cardano.Ledger.Shelley.State.Stake (
2121
) where
2222

2323
import Cardano.Ledger.Address
24-
import Cardano.Ledger.BaseTypes (KeyValuePairs (..), ToKeyValuePairs (..))
24+
import Cardano.Ledger.BaseTypes (
25+
KeyValuePairs (..),
26+
NonZero (..),
27+
ToKeyValuePairs (..),
28+
unsafeNonZero,
29+
)
2530
import Cardano.Ledger.Binary (
2631
DecShareCBOR (..),
2732
EncCBOR (..),
@@ -149,19 +154,17 @@ resolveShelleyInstantStake ::
149154
) =>
150155
ShelleyInstantStake era ->
151156
ShelleyAccounts era ->
152-
Stake
157+
ActiveStake
153158
resolveShelleyInstantStake instantStake@ShelleyInstantStake {sisPtrStake} sas =
154-
Stake $ VMap.fromMap $ Map.foldlWithKey' addPtrStake credentialStakeMap sisPtrStake
159+
ActiveStake $ VMap.fromMap $ Map.foldlWithKey' addPtrStake credentialStakeMap sisPtrStake
155160
where
156161
!credentialStakeMap = resolveActiveInstantStakeCredentials instantStake sas
157162
addPtrStake !acc ptr ptrStake = fromMaybe acc $ do
158163
cred <- Map.lookup ptr (saPtrs sas)
159-
-- Ensure only staking credential that delegates to a pool receive Ptr delegations
160164
accountState <- lookupAccountState cred sas
161-
_ <- accountState ^. stakePoolDelegationAccountStateL
165+
poolId <- accountState ^. stakePoolDelegationAccountStateL
162166
let plusPtrStake =
163167
Just . \case
164-
Nothing -> ptrStake
165-
Just curStake -> curStake <> ptrStake
166-
-- instant stake is guaranteed to be non-zero due to minUTxO, so no need to guard against mempty
168+
Nothing -> StakeWithDelegation (unsafeNonZero ptrStake) poolId
169+
Just swd -> swd {swdStake = unsafeNonZero $ unNonZero (swdStake swd) <> ptrStake}
167170
pure $ Map.alter plusPtrStake cred acc

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,6 @@ instance Arbitrary FreeVars where
304304
<*> arbitrary
305305
<*> arbitrary
306306
<*> arbitrary
307-
<*> arbitrary
308307
shrink = genericShrink
309308

310309
------------------------------------------------------------------------------------------

eras/shelley/impl/testlib/Test/Cardano/Ledger/Shelley/Binary/Golden.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import Cardano.Ledger.TxIn (TxIn)
3131
import qualified Data.Map.Strict as Map
3232
import qualified Data.Sequence.Strict as SSeq
3333
import qualified Data.Set as Set
34-
import qualified Data.VMap as VMap
3534
import Lens.Micro ((&), (.~))
3635
import Test.Cardano.Ledger.Binary.Golden
3736
import Test.Cardano.Ledger.Binary.Plain.Golden
@@ -126,9 +125,8 @@ goldenNewEpochStateExpectation
126125
me = Em [Ev ver k <> Ev ver v | (k, v) <- Map.toList m]
127126
snapShotEnc SnapShot {..} =
128127
Em
129-
[ E (TkListLen 3)
130-
, mapEnc (VMap.toMap (unStake ssActiveStake))
131-
, Ev ver ssDelegations
128+
[ E (TkListLen 2)
129+
, Ev ver ssActiveStake
132130
, Ev ver ssStakePoolsSnapShot
133131
]
134132

0 commit comments

Comments
 (0)