Skip to content

Commit

Permalink
at major PV 5, script stake creds can earn rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
Jared Corduan committed Jul 29, 2021
1 parent 30eca73 commit e88c317
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 15 deletions.
Expand Up @@ -515,7 +515,7 @@ ppRewardSnapShot (RewardSnapShot snaps a0 nopt ver non deltaR1 rR deltaT1 total
]

ppFreeVars :: FreeVars crypto -> PDoc
ppFreeVars (FreeVars b1 del stake1 addrs total active asc1 blocks r1 slots d a0 nOpt) =
ppFreeVars (FreeVars b1 del stake1 addrs total active asc1 blocks r1 slots d a0 nOpt mv) =
ppRecord
"FreeVars"
[ ("b", ppMap ppKeyHash ppNatural b1),
Expand All @@ -530,7 +530,8 @@ ppFreeVars (FreeVars b1 del stake1 addrs total active asc1 blocks r1 slots d a0
("slotserEpoch", ppEpochSize slots),
("d", ppUnitInterval d),
("a0", ppRational $ unboundRational a0),
("nOpt", ppNatural nOpt)
("nOpt", ppNatural nOpt),
("mv", ppNatural mv)
]

ppAns :: RewardAns crypto -> PDoc
Expand Down
Expand Up @@ -6,10 +6,12 @@ module Shelley.Spec.Ledger.HardForks
( aggregatedRewards,
allowMIRTransfer,
validatePoolRewardAccountNetID,
allowScriptStakeCredsToEarnRewards,
)
where

import GHC.Records
import Numeric.Natural (Natural)
import Shelley.Spec.Ledger.PParams (ProtVer (..))

aggregatedRewards ::
Expand Down Expand Up @@ -38,3 +40,10 @@ validatePoolRewardAccountNetID ::
pp ->
Bool
validatePoolRewardAccountNetID pp = pvMajor (getField @"_protocolVersion" pp) > 4

-- | Starting with protocol version 5, Stake Credentials bound by scripts
-- will be eligibile for staking rewards.
allowScriptStakeCredsToEarnRewards ::
Natural ->
Bool
allowScriptStakeCredsToEarnRewards pvM = pvM > 4
Expand Up @@ -1187,6 +1187,7 @@ startStep slotsPerEpoch b@(BlocksMade b') es@(EpochState acnt ss ls pr _ nm) max
(getField @"_d" pr)
(getField @"_a0" pr)
(getField @"_nOpt" pr)
(pvMajor $ getField @"_protocolVersion" pr)
pulser :: Pulser (Crypto era)
pulser =
RSLP
Expand Down
Expand Up @@ -223,7 +223,8 @@ data FreeVars crypto = FreeVars
slotsPerEpoch :: !EpochSize,
pp_d :: !UnitInterval, -- The last three fields come from some version of PParams
pp_a0 :: !NonNegativeInterval,
pp_nOpt :: !Natural
pp_nOpt :: !Natural,
pp_mv :: !Natural
}
deriving (Eq, Show, Generic)
deriving (NoThunks)
Expand All @@ -245,7 +246,8 @@ instance (CC.Crypto crypto) => ToCBOR (FreeVars crypto) where
slotsPerEpoch,
pp_d,
pp_a0,
pp_nOpt
pp_nOpt,
pp_mv
}
) =
encode
Expand All @@ -259,6 +261,7 @@ instance (CC.Crypto crypto) => ToCBOR (FreeVars crypto) where
!> To pp_d
!> E boundedRationalToCBOR pp_a0
!> To pp_nOpt
!> To pp_mv
)

instance (CC.Crypto crypto) => FromCBOR (FreeVars crypto) where
Expand All @@ -274,6 +277,7 @@ instance (CC.Crypto crypto) => FromCBOR (FreeVars crypto) where
<! From {- pp_d -}
<! D boundedRationalFromCBOR {- pp_a0 -}
<! From {- pp_nOpt -}
<! From {- pp_mv -}
)

-- =====================================================================
Expand All @@ -299,7 +303,8 @@ rewardStakePool
slotsPerEpoch,
pp_d,
pp_a0,
pp_nOpt
pp_nOpt,
pp_mv
}
)
(RewardAns m1 m2)
Expand All @@ -318,7 +323,7 @@ rewardStakePool
case blocksProduced of
Nothing -> pure $ RewardAns m1 (Map.insert hk ls m2)
Just n -> do
m <- rewardOnePool (pp_d, pp_a0, pp_nOpt) r n totalBlocks pparams actgr sigma sigmaA (Coin totalStake) addrsRew
m <- rewardOnePool (pp_d, pp_a0, pp_nOpt, pp_mv) r n totalBlocks pparams actgr sigma sigmaA (Coin totalStake) addrsRew
pure $ RewardAns (Map.unionWith Set.union m m1) (Map.insert hk ls m2)

-- ================================================================
Expand Down
Expand Up @@ -464,7 +464,7 @@ aggregateRewards pp rewards =
rewardOnePool ::
forall c m.
Monad m =>
(UnitInterval, NonNegativeInterval, Natural) ->
(UnitInterval, NonNegativeInterval, Natural, Natural) ->
Coin ->
Natural ->
Natural ->
Expand All @@ -479,7 +479,8 @@ rewardOnePool ::
m
(Map (Credential 'Staking c) (Set (Reward c)))
rewardOnePool
(pp_d, pp_a0, pp_nOpt) -- (Decentralization parameter, Pool influence, Desired number of pools)
(pp_d, pp_a0, pp_nOpt, pp_mv)
-- (Decentralization parameter, Pool influence, Desired number of pools, major version)
r
blocksN
blocksTotal
Expand Down Expand Up @@ -523,7 +524,7 @@ rewardOnePool
hk `Set.member` addrsRew
]
notPoolOwner (KeyHashObj hk) = hk `Set.notMember` _poolOwners pool
notPoolOwner (ScriptHashObj _) = False
notPoolOwner (ScriptHashObj _) = HardForks.allowScriptStakeCredsToEarnRewards pp_mv
lReward =
leaderRew
poolR
Expand Down
Expand Up @@ -993,6 +993,7 @@ instance
<*> arbitrary {- pp_d -}
<*> arbitrary {- pp_a0 -}
<*> arbitrary {- pp_nOpt -}
<*> arbitrary {- pp_mv -}

instance
Mock crypto =>
Expand Down
Expand Up @@ -323,7 +323,7 @@ rewardsBoundedByPot _ = property $ do
runShelleyBase $
runProvM $
reward
(_d pp, _a0 pp, _nOpt pp)
(_d pp, _a0 pp, _nOpt pp, pvMajor (_protocolVersion pp))
bs
rewardPot
rewardAcnts
Expand Down Expand Up @@ -391,7 +391,7 @@ rewardsProvenance _ = generate $ do
runShelleyBase $
runWithProvM def $
reward
(_d pp, _a0 pp, _nOpt pp)
(_d pp, _a0 pp, _nOpt pp, pvMajor (_protocolVersion pp))
bs
rewardPot
rewardAcnts
Expand Down Expand Up @@ -749,7 +749,7 @@ newEpochProp tracelen propf = withMaxSuccess 100 $

reward ::
forall crypto.
(UnitInterval, NonNegativeInterval, Natural) ->
(UnitInterval, NonNegativeInterval, Natural, Natural) ->
BlocksMade crypto ->
Coin ->
Set (Credential 'Staking crypto) ->
Expand All @@ -765,7 +765,7 @@ reward pp bm r addrsRew poolParams stake delegs tot asc slotsPerEpoch =

rewardPulser ::
forall c.
(UnitInterval, NonNegativeInterval, Natural) ->
(UnitInterval, NonNegativeInterval, Natural, Natural) ->
BlocksMade c ->
Coin ->
Set (Credential 'Staking c) ->
Expand All @@ -777,7 +777,7 @@ rewardPulser ::
EpochSize ->
Pulser c
rewardPulser
(pp_d, pp_a0, pp_nOpt)
(pp_d, pp_a0, pp_nOpt, pp_mv)
(BlocksMade b)
r
addrsRew
Expand All @@ -790,7 +790,7 @@ rewardPulser
where
totalBlocks = sum b
Coin activeStake = fold . unStake $ stake
free = (FreeVars b delegs stake addrsRew totalStake activeStake asc totalBlocks r slotsPerEpoch pp_d pp_a0 pp_nOpt)
free = (FreeVars b delegs stake addrsRew totalStake activeStake asc totalBlocks r slotsPerEpoch pp_d pp_a0 pp_nOpt pp_mv)
pulser :: Pulser c
pulser = RSLP 2 free (StrictSeq.fromList $ Map.elems poolParams) (RewardAns Map.empty Map.empty)

Expand Down

0 comments on commit e88c317

Please sign in to comment.