From 6930bab827886bd7608b2572c7f1573b81a091d9 Mon Sep 17 00:00:00 2001 From: Philipp Kant Date: Mon, 14 Sep 2020 14:27:15 +0200 Subject: [PATCH] Define function for current circulation and use it in the API, and in the rewards calculation. Fixes #1838 --- .../src/Shelley/Spec/Ledger/API/Wallet.hs | 9 ++++----- .../src/Shelley/Spec/Ledger/LedgerState.hs | 14 +++++++++++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/API/Wallet.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/API/Wallet.hs index 0f844f4e30f..8cb5beec63a 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/API/Wallet.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/API/Wallet.hs @@ -12,7 +12,6 @@ where import qualified Cardano.Crypto.VRF as VRF import Cardano.Ledger.Crypto (VRF) import Cardano.Ledger.Era (Crypto, Era) -import qualified Cardano.Ledger.Val as Val import Cardano.Slotting.EpochInfo (epochInfoRange) import Cardano.Slotting.Slot (SlotNo) import qualified Data.ByteString.Short as BSS @@ -35,12 +34,12 @@ import Shelley.Spec.Ledger.Delegation.Certificates (IndividualPoolStake (..), un import qualified Shelley.Spec.Ledger.EpochBoundary as EB import Shelley.Spec.Ledger.Keys (KeyHash, KeyRole (..), SignKeyVRF) import Shelley.Spec.Ledger.LedgerState - ( AccountState (..), - DPState (..), + ( DPState (..), EpochState (..), LedgerState (..), NewEpochState (..), UTxOState (..), + circulation, stakeDistr, ) import Shelley.Spec.Ledger.OverlaySchedule (isOverlaySlot) @@ -60,8 +59,8 @@ import Shelley.Spec.Ledger.UTxO (UTxO (..)) getTotalStake :: Globals -> ShelleyState era -> Coin getTotalStake globals ss = let supply = Coin . fromIntegral $ maxLovelaceSupply globals - EpochState acnt _ _ _ _ _ = nesEs ss - in supply Val.~~ (_reserves acnt) + es = nesEs ss + in circulation es supply -- | Calculate the Non-Myopic Pool Member Rewards for a set of credentials. -- For each given credential, this function returns a map from each stake diff --git a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/LedgerState.hs b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/LedgerState.hs index 972152d6ce2..374d0a7bf83 100644 --- a/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/LedgerState.hs +++ b/shelley/chain-and-ledger/executable-spec/src/Shelley/Spec/Ledger/LedgerState.hs @@ -86,6 +86,7 @@ module Shelley.Spec.Ledger.LedgerState NewEpochEnv (..), getGKeys, updateNES, + circulation, ) where @@ -918,7 +919,7 @@ createRUpd :: EpochState era -> Coin -> ShelleyBase (RewardUpdate era) -createRUpd slotsPerEpoch b@(BlocksMade b') (EpochState acnt ss ls pr _ nm) total = do +createRUpd slotsPerEpoch b@(BlocksMade b') es@(EpochState acnt ss ls pr _ nm) total = do asc <- asks activeSlotCoeff let SnapShot stake' delegs' poolParams = _pstakeGo ss Coin reserves = _reserves acnt @@ -937,9 +938,9 @@ createRUpd slotsPerEpoch b@(BlocksMade b') (EpochState acnt ss ls pr _ nm) total Coin rPot = _feeSS ss <> deltaR1 deltaT1 = floor $ intervalValue (_tau pr) * fromIntegral rPot _R = Coin $ rPot - deltaT1 - circulation = total Val.~~ (_reserves acnt) + totalStake = circulation es total (rs_, newLikelihoods) = - reward pr b _R (Map.keysSet $ _rewards ds) poolParams stake' delegs' circulation asc slotsPerEpoch + reward pr b _R (Map.keysSet $ _rewards ds) poolParams stake' delegs' totalStake asc slotsPerEpoch deltaR2 = _R Val.~~ (Map.foldr (<>) mempty rs_) blocksMade = fromIntegral $ Map.foldr (+) 0 b' :: Integer pure $ @@ -951,6 +952,13 @@ createRUpd slotsPerEpoch b@(BlocksMade b') (EpochState acnt ss ls pr _ nm) total nonMyopic = (updateNonMypopic nm _R newLikelihoods) } +-- | Calculate the current circulation +-- +-- This is used in the rewards calculation, and for API endpoints for pool ranking. +circulation :: EpochState era -> Coin -> Coin +circulation (EpochState acnt _ _ _ _ _) supply = + supply Val.~~ (_reserves acnt) + -- | Update new epoch state updateNES :: NewEpochState era ->