Skip to content

Commit

Permalink
Define function for current circulation
Browse files Browse the repository at this point in the history
and use it in the API, and in the rewards calculation.

Fixes #1838
  • Loading branch information
kantp committed Sep 14, 2020
1 parent 28d9a6d commit 6930bab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
Expand Up @@ -86,6 +86,7 @@ module Shelley.Spec.Ledger.LedgerState
NewEpochEnv (..),
getGKeys,
updateNES,
circulation,
)
where

Expand Down Expand Up @@ -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
Expand All @@ -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 $
Expand All @@ -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 ->
Expand Down

0 comments on commit 6930bab

Please sign in to comment.