Skip to content

Commit

Permalink
move RewardType and Reward to cardano-ledger-core package (IntersectM…
Browse files Browse the repository at this point in the history
…BO#3059)

move RewardType to cardano-ledger-core package
  • Loading branch information
Jared Corduan authored Oct 7, 2022
1 parent ad8470a commit 42db730
Show file tree
Hide file tree
Showing 16 changed files with 152 additions and 88 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ in the naming of release branches.
#2976
- Moved `TxOut` to a separate module in each era #3024
- Moved `mintedTxBodyF` into `ShelleyMAEraTxBody` class #3019
- Moved thet `RewardType` and `Reward` types from the `Cardano.Ledger.Shelley.Reward` module in the
`cardano-ledger-shelley` package into a new module `Cardano.Ledger.Reward`
inside the `cardano-ledger-core` package. #3059

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ import Cardano.Ledger.Shelley.LedgerState.DPState
)
import Cardano.Ledger.Shelley.LedgerState.Types
import Cardano.Ledger.Shelley.RewardUpdate (RewardUpdate (..))
import Cardano.Ledger.Shelley.Rewards
( Reward (..),
aggregateRewards,
filterRewards,
)
import Cardano.Ledger.Shelley.Rewards (aggregateRewards, filterRewards)
import Cardano.Ledger.Shelley.TxBody
( Ptr (..),
)
Expand Down
3 changes: 1 addition & 2 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/RewardUpdate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ import Cardano.Binary
import Cardano.Ledger.BaseTypes (ProtVer (..), ShelleyBase)
import Cardano.Ledger.Coin (Coin (..), CompactForm, DeltaCoin (..))
import Cardano.Ledger.Compactible (Compactible (fromCompact))
import Cardano.Ledger.Core (Reward (..), RewardType (MemberReward))
import Cardano.Ledger.Credential (Credential (..))
import qualified Cardano.Ledger.Crypto as CC (Crypto)
import Cardano.Ledger.Keys (KeyHash, KeyRole (..))
import Cardano.Ledger.Serialization (decodeRecordNamed)
import Cardano.Ledger.Shelley.PoolRank (Likelihood, NonMyopic)
import Cardano.Ledger.Shelley.Rewards
( PoolRewardInfo (..),
Reward (..),
RewardType (..),
rewardOnePoolMember,
)
import Control.DeepSeq (NFData (..))
Expand Down
53 changes: 2 additions & 51 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/Rewards.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ module Cardano.Ledger.Shelley.Rewards
PoolRewardInfo (..),
mkApparentPerformance,
RewardType (..),
Reward (..),
LeaderOnlyReward (..),
leaderRewardToGeneral,
Reward (..),
leaderRew,
memberRew,
aggregateRewards,
Expand All @@ -28,24 +28,21 @@ where
import Cardano.Binary
( FromCBOR (..),
ToCBOR (..),
decodeWord,
encodeWord,
)
import Cardano.Ledger.BaseTypes
( BlocksMade (..),
BoundedRational (..),
NonNegativeInterval,
ProtVer,
UnitInterval,
invalidKey,
)
import Cardano.Ledger.Coin
( Coin (..),
coinToRational,
rationalToCoinViaFloor,
)
import Cardano.Ledger.Compactible (fromCompact)
import Cardano.Ledger.Core
import Cardano.Ledger.Core (EraCrypto, PParams, Reward (..), RewardType (..))
import Cardano.Ledger.Credential (Credential (..))
import qualified Cardano.Ledger.Crypto as CC (Crypto)
import Cardano.Ledger.Keys (KeyHash, KeyRole (..))
Expand Down Expand Up @@ -124,52 +121,6 @@ memberRew (Coin f') pool (StakeShare t) (StakeShare sigma)
(Coin c, m, _) = poolSpec pool
m' = unboundRational m

data RewardType = MemberReward | LeaderReward
deriving (Eq, Show, Ord, Generic)

instance NoThunks RewardType

instance NFData RewardType

instance ToCBOR RewardType where
toCBOR MemberReward = encodeWord 0
toCBOR LeaderReward = encodeWord 1

instance FromCBOR RewardType where
fromCBOR =
decodeWord >>= \case
0 -> pure MemberReward
1 -> pure LeaderReward
n -> invalidKey n

data Reward c = Reward
{ rewardType :: RewardType,
rewardPool :: KeyHash 'StakePool c,
rewardAmount :: Coin
}
deriving (Eq, Show, Generic)

-- | Note that this Ord instance is chosen to align precisely
-- with the Allegra reward aggregation, as given by the
-- function 'aggregateRewards' so that 'Set.findMax' returns
-- the expected value.
instance Ord (Reward c) where
compare (Reward MemberReward _ _) (Reward LeaderReward _ _) = GT
compare (Reward LeaderReward _ _) (Reward MemberReward _ _) = LT
compare (Reward _ pool1 _) (Reward _ pool2 _) = compare pool1 pool2

instance NoThunks (Reward c)

instance NFData (Reward c)

instance CC.Crypto c => ToCBOR (Reward c) where
toCBOR (Reward rt pool c) =
encode $ Rec Reward !> To rt !> To pool !> To c

instance CC.Crypto c => FromCBOR (Reward c) where
fromCBOR =
decode $ RecD Reward <! From <! From <! From

sumRewards ::
forall c pp.
(HasField "_protocolVersion" pp ProtVer) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import Cardano.Ledger.Shelley.AdaPots (AdaPots, totalAdaPotsES)
import Cardano.Ledger.Shelley.EpochBoundary
import Cardano.Ledger.Shelley.Era (ShelleyNEWEPOCH)
import Cardano.Ledger.Shelley.LedgerState
import Cardano.Ledger.Shelley.Rewards (Reward, sumRewards)
import Cardano.Ledger.Shelley.Rewards (sumRewards)
import Cardano.Ledger.Shelley.Rules.Epoch
import Cardano.Ledger.Shelley.Rules.Mir (ShelleyMIR, ShelleyMirEvent, ShelleyMirPredFailure)
import Cardano.Ledger.Shelley.Rules.Rupd (RupdEvent (..))
Expand Down
1 change: 0 additions & 1 deletion eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/Rupd.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import Cardano.Ledger.Shelley.LedgerState
pulseStep,
startStep,
)
import Cardano.Ledger.Shelley.Rewards (Reward)
import Cardano.Ledger.Slot
( Duration (..),
EpochNo,
Expand Down
19 changes: 11 additions & 8 deletions eras/shelley/test-suite/src/Test/Cardano/Ledger/Shelley/Rewards.hs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ import Cardano.Ledger.Shelley.RewardUpdate
RewardPulser (RSLP),
)
import Cardano.Ledger.Shelley.Rewards
( Reward (rewardAmount),
StakeShare (..),
( StakeShare (..),
aggregateRewards,
leaderRew,
memberRew,
Expand Down Expand Up @@ -376,7 +375,7 @@ rewardsBoundedByPot _ = property $ do
show slotsPerEpoch
]
)
(fold (fmap rewardAmount rs) < rewardPot)
(fold (fmap Core.rewardAmount rs) < rewardPot)

-- ====================================================================================
-- To demonstrate that the code we wrote that uses pulsing does not
Expand Down Expand Up @@ -715,15 +714,19 @@ newEpochEventsProp tracelen propf = withMaxSuccess 10 $
propf (concat (runShelleyBase $ getEvents tr)) (chainNes target)
_ -> property True

aggIncrementalRewardEvents :: [ChainEvent C] -> Map (Credential 'Staking (EraCrypto C)) (Set (Reward (EraCrypto C)))
aggIncrementalRewardEvents ::
[ChainEvent C] ->
Map (Credential 'Staking (EraCrypto C)) (Set (Core.Reward (EraCrypto C)))
aggIncrementalRewardEvents = foldl' accum Map.empty
where
accum ans (TickEvent (TickRupdEvent (RupdEvent _ m))) = Map.unionWith Set.union m ans
accum ans (TickEvent (TickNewEpochEvent (DeltaRewardEvent (RupdEvent _ m)))) =
Map.unionWith Set.union m ans
accum ans _ = ans

getMostRecentTotalRewardEvent :: [ChainEvent C] -> Map (Credential 'Staking (EraCrypto C)) (Set (Reward (EraCrypto C)))
getMostRecentTotalRewardEvent ::
[ChainEvent C] ->
Map (Credential 'Staking (EraCrypto C)) (Set (Core.Reward (EraCrypto C)))
getMostRecentTotalRewardEvent = foldl' accum Map.empty
where
accum ans (TickEvent (TickNewEpochEvent (TotalRewardEvent _ m))) = Map.unionWith Set.union m ans
Expand Down Expand Up @@ -757,16 +760,16 @@ eventsMirrorRewards events nes = same eventRew compRew
x
y

ppAgg :: Map (Credential 'Staking (EraCrypto C)) (Set (Reward (EraCrypto C))) -> PDoc
ppAgg :: Map (Credential 'Staking (EraCrypto C)) (Set (Core.Reward (EraCrypto C))) -> PDoc
ppAgg = ppMap prettyA (ppSet ppReward)

instance Terse (Reward c) where
instance Terse (Core.Reward c) where
terse x = show (ppReward x)

instance PrettyA x => Terse (Set x) where
terse x = show (ppSet prettyA x)

instance PrettyA (Reward c) where
instance PrettyA (Core.Reward c) where
prettyA = ppReward

-- ================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ import Cardano.Ledger.BaseTypes
textToUrl,
)
import Cardano.Ledger.Coin (CompactForm (..), DeltaCoin (..))
import Cardano.Ledger.Core (Era, EraCrypto, EraScript (..), EraSegWits (..))
import Cardano.Ledger.Core
( Era,
EraCrypto,
EraScript (..),
EraSegWits (..),
Reward (..),
RewardType (..),
)
import qualified Cardano.Ledger.Core as Core
import Cardano.Ledger.Crypto (DSIGN)
import qualified Cardano.Ledger.Crypto as CC (Crypto, HASH)
Expand All @@ -85,8 +92,6 @@ import Cardano.Ledger.Shelley.RewardUpdate
import Cardano.Ledger.Shelley.Rewards
( LeaderOnlyReward (..),
PoolRewardInfo (..),
Reward (..),
RewardType (..),
StakeShare (..),
)
import qualified Cardano.Ledger.Shelley.Rules as STS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import Cardano.Ledger.BaseTypes
import Cardano.Ledger.Block (Block, bheader, txid)
import Cardano.Ledger.Coin (Coin (..), DeltaCoin (..), addDeltaCoin, toDeltaCoin)
import Cardano.Ledger.Compactible
import Cardano.Ledger.Core (Reward (..), RewardType (..))
import Cardano.Ledger.Credential (Credential, Ptr (..))
import qualified Cardano.Ledger.Crypto as Cr
import Cardano.Ledger.Era (EraCrypto (..))
Expand Down Expand Up @@ -62,7 +63,6 @@ import Cardano.Ledger.Shelley.PoolRank
leaderProbability,
likelihood,
)
import Cardano.Ledger.Shelley.Rewards (Reward (..), RewardType (..))
import Cardano.Ledger.Shelley.Tx
( ShelleyTx (..),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ import Cardano.Ledger.Shelley.PoolRank
likelihood,
)
import Cardano.Ledger.Shelley.Rewards
( Reward (..),
RewardType (..),
StakeShare (..),
( StakeShare (..),
aggregateRewards,
leaderRew,
memberRew,
Expand Down Expand Up @@ -760,7 +758,7 @@ rewardUpdateEx9 ::
forall era.
ExMock (EraCrypto era) =>
ShelleyPParams era ->
Map (Credential 'Staking (EraCrypto era)) (Set (Reward (EraCrypto era))) ->
Map (Credential 'Staking (EraCrypto era)) (Set (Core.Reward (EraCrypto era))) ->
RewardUpdate (EraCrypto era)
rewardUpdateEx9 pp rewards =
RewardUpdate
Expand Down Expand Up @@ -812,15 +810,15 @@ twoPools9 = CHAINExample expectedStEx8 blockEx9 (Right $ expectedStEx9 ppEx)
--
-- Now test with Aggregation
--
carlsRewards :: forall c. ExMock c => Set (Reward c)
carlsRewards :: forall c. ExMock c => Set (Core.Reward c)
carlsRewards =
Set.fromList
[ Reward MemberReward (hk Cast.alicePoolKeys) (carlMemberRewardsFromAlice @c),
Reward LeaderReward (hk Cast.alicePoolKeys) (carlLeaderRewardsFromAlice @c),
Reward LeaderReward (hk Cast.bobPoolKeys) (carlLeaderRewardsFromBob @c)
[ Core.Reward Core.MemberReward (hk Cast.alicePoolKeys) (carlMemberRewardsFromAlice @c),
Core.Reward Core.LeaderReward (hk Cast.alicePoolKeys) (carlLeaderRewardsFromAlice @c),
Core.Reward Core.LeaderReward (hk Cast.bobPoolKeys) (carlLeaderRewardsFromBob @c)
]

rsEx9Agg :: forall c. ExMock c => Map (Credential 'Staking c) (Set (Reward c))
rsEx9Agg :: forall c. ExMock c => Map (Credential 'Staking c) (Set (Core.Reward c))
rsEx9Agg = Map.singleton Cast.carlSHK carlsRewards

ppProtVer3 :: ShelleyPParams era
Expand All @@ -840,13 +838,13 @@ twoPools9Agg = CHAINExample expectedStEx8Agg blockEx9 (Right expectedStEx9Agg)
testAggregateRewardsLegacy :: HasCallStack => Assertion
testAggregateRewardsLegacy = do
let expectedReward = carlLeaderRewardsFromBob @(EraCrypto C)
expectedReward @?= rewardAmount (minimum (carlsRewards @(EraCrypto C)))
expectedReward @?= Core.rewardAmount (minimum (carlsRewards @(EraCrypto C)))
aggregateRewards @C_Crypto ppEx rsEx9Agg @?= Map.singleton Cast.carlSHK expectedReward

testAggregateRewardsNew :: Assertion
testAggregateRewardsNew =
aggregateRewards @C_Crypto ppProtVer3 rsEx9Agg
@?= Map.singleton Cast.carlSHK (foldMap rewardAmount (carlsRewards @(EraCrypto C)))
@?= Map.singleton Cast.carlSHK (foldMap Core.rewardAmount (carlsRewards @(EraCrypto C)))

--
-- Two Pools Test Group
Expand Down
1 change: 1 addition & 0 deletions libs/cardano-ledger-core/cardano-ledger-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ library
Cardano.Ledger.Language
Cardano.Ledger.MemoBytes
Cardano.Ledger.PoolDistr
Cardano.Ledger.Rewards
Cardano.Ledger.Rules.ValidationMode
Cardano.Ledger.SafeHash
Cardano.Ledger.Serialization
Expand Down
5 changes: 5 additions & 0 deletions libs/cardano-ledger-core/src/Cardano/Ledger/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ module Cardano.Ledger.Core
getPhase1,
getPhase2,

-- * Rewards
RewardType (..),
Reward (..),

-- * Re-exports
module Cardano.Ledger.Hashes,

Expand All @@ -92,6 +96,7 @@ import Cardano.Ledger.Keys (KeyRole (Staking, Witness))
import Cardano.Ledger.Keys.Bootstrap (BootstrapWitness)
import Cardano.Ledger.Keys.WitVKey (WitVKey)
import Cardano.Ledger.Language (Language)
import Cardano.Ledger.Rewards (Reward (..), RewardType (..))
import Cardano.Ledger.SafeHash (HashAnnotated (..), SafeToHash (..))
import Cardano.Ledger.Serialization (Sized (sizedValue), ToCBORGroup (..), mkSized)
import Cardano.Ledger.TxIn (TxIn (..))
Expand Down
Loading

0 comments on commit 42db730

Please sign in to comment.