Skip to content

Commit

Permalink
Merge pull request #2797 from input-output-hk/td/new-adapots-event
Browse files Browse the repository at this point in the history
Emit new TotalAdaPotEvent after epoch transition (gh #2780)
  • Loading branch information
Jared Corduan authored May 18, 2022
2 parents 2763385 + 3d9f369 commit 09b60ce
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 64 deletions.
3 changes: 2 additions & 1 deletion eras/shelley/impl/cardano-ledger-shelley.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ library
Cardano.Ledger.Shelley.API.Wallet
Cardano.Ledger.Shelley.API.Mempool
Cardano.Ledger.Shelley.API.Types
Cardano.Ledger.Shelley.AdaPots
Cardano.Ledger.Shelley.BlockChain
Cardano.Ledger.Shelley.CompactAddr
Cardano.Ledger.Shelley.Delegation.Certificates
Expand Down Expand Up @@ -114,4 +115,4 @@ library
text,
time,
transformers,
validation-selective,
validation-selective,
67 changes: 9 additions & 58 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/API/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ module Cardano.Ledger.Shelley.API.Wallet
-- * Transaction helpers
CLI (..),
addShelleyKeyWitnesses,

-- * Ada Pots
-- -- * Ada pots
AdaPots (..),
totalAdaES,
totalAdaPotsES,
Expand Down Expand Up @@ -73,11 +72,14 @@ import Cardano.Ledger.PoolDistr
PoolDistr (..),
)
import Cardano.Ledger.Shelley (ShelleyEra)
import Cardano.Ledger.Shelley.Constraints (UsesValue)
import Cardano.Ledger.Shelley.AdaPots
( AdaPots (..),
totalAdaES,
totalAdaPotsES,
)
import qualified Cardano.Ledger.Shelley.EpochBoundary as EB
import Cardano.Ledger.Shelley.LedgerState
( AccountState (..),
DPState (..),
( DPState (..),
EpochState (..),
LedgerState (..),
NewEpochState (..),
Expand All @@ -90,7 +92,6 @@ import Cardano.Ledger.Shelley.LedgerState
incrementalStakeDistr,
minfee,
produced,
rewards,
)
import Cardano.Ledger.Shelley.PParams (PParams' (..))
import Cardano.Ledger.Shelley.PoolRank
Expand All @@ -105,11 +106,10 @@ import Cardano.Ledger.Shelley.Rewards (StakeShare (..))
import Cardano.Ledger.Shelley.Rules.NewEpoch (calculatePoolDistr)
import Cardano.Ledger.Shelley.Tx (Tx (..), WitnessSet, WitnessSetHKD (..))
import Cardano.Ledger.Shelley.TxBody (DCert, PoolParams (..), WitVKey (..))
import Cardano.Ledger.Shelley.UTxO (UTxO (..), balance)
import Cardano.Ledger.Shelley.UTxO (UTxO (..))
import Cardano.Ledger.Slot (epochInfoSize)
import Cardano.Ledger.TxIn (TxIn (..))
import Cardano.Ledger.Val ((<->))
import qualified Cardano.Ledger.Val as Val
import Cardano.Slotting.Slot (EpochSize)
import Control.DeepSeq (NFData)
import Control.Monad.Trans.Reader (runReader)
Expand All @@ -126,7 +126,7 @@ import Data.Coders
)
import Data.Default.Class (Default (..))
import Data.Either (fromRight)
import Data.Foldable (fold, foldMap')
import Data.Foldable (foldMap')
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Proxy (Proxy (..))
Expand Down Expand Up @@ -557,55 +557,6 @@ instance CC.Crypto c => CLI (ShelleyEra c) where

evaluateMinLovelaceOutput pp _out = _minUTxOValue pp

data AdaPots = AdaPots
{ treasuryAdaPot :: Coin,
reservesAdaPot :: Coin,
rewardsAdaPot :: Coin,
utxoAdaPot :: Coin,
depositsAdaPot :: Coin,
feesAdaPot :: Coin
}
deriving (Show, Eq)

-- | Calculate the total ada pots in the epoch state
totalAdaPotsES ::
UsesValue era =>
EpochState era ->
AdaPots
totalAdaPotsES (EpochState (AccountState treasury_ reserves_) _ ls _ _ _) =
AdaPots
{ treasuryAdaPot = treasury_,
reservesAdaPot = reserves_,
rewardsAdaPot = rewards_,
utxoAdaPot = coins,
depositsAdaPot = deposits,
feesAdaPot = fees_
}
where
(UTxOState u deposits fees_ _ _) = lsUTxOState ls
(DPState dstate _) = lsDPState ls
rewards_ = fold (rewards dstate)
coins = Val.coin $ balance u

-- | Calculate the total ada in the epoch state
totalAdaES :: UsesValue era => EpochState era -> Coin
totalAdaES cs =
treasuryAdaPot
<> reservesAdaPot
<> rewardsAdaPot
<> utxoAdaPot
<> depositsAdaPot
<> feesAdaPot
where
AdaPots
{ treasuryAdaPot,
reservesAdaPot,
rewardsAdaPot,
utxoAdaPot,
depositsAdaPot,
feesAdaPot
} = totalAdaPotsES cs

--------------------------------------------------------------------------------
-- CBOR instances
--------------------------------------------------------------------------------
Expand Down
78 changes: 78 additions & 0 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/AdaPots.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

module Cardano.Ledger.Shelley.AdaPots
( AdaPots (..),
totalAdaES,
totalAdaPotsES,
)
where

import Cardano.Ledger.Coin (Coin (..))
import Cardano.Ledger.Shelley.Constraints (UsesValue)
import Cardano.Ledger.Shelley.LedgerState
( AccountState (..),
DPState (..),
EpochState (..),
LedgerState (..),
UTxOState (..),
rewards,
)
import Cardano.Ledger.Shelley.UTxO (balance)
import qualified Cardano.Ledger.Val as Val
import Data.Foldable (fold)

data AdaPots = AdaPots
{ treasuryAdaPot :: Coin,
reservesAdaPot :: Coin,
rewardsAdaPot :: Coin,
utxoAdaPot :: Coin,
depositsAdaPot :: Coin,
feesAdaPot :: Coin
}
deriving (Show, Eq)

-- | Calculate the total ada pots in the epoch state
totalAdaPotsES ::
UsesValue era =>
EpochState era ->
AdaPots
totalAdaPotsES (EpochState (AccountState treasury_ reserves_) _ ls _ _ _) =
AdaPots
{ treasuryAdaPot = treasury_,
reservesAdaPot = reserves_,
rewardsAdaPot = rewards_,
utxoAdaPot = coins,
depositsAdaPot = deposits,
feesAdaPot = fees_
}
where
(UTxOState u deposits fees_ _ _) = lsUTxOState ls
(DPState dstate _) = lsDPState ls
rewards_ = fold (rewards dstate)
coins = Val.coin $ balance u

-- | Calculate the total ada in the epoch state
totalAdaES :: UsesValue era => EpochState era -> Coin
totalAdaES cs =
treasuryAdaPot
<> reservesAdaPot
<> rewardsAdaPot
<> utxoAdaPot
<> depositsAdaPot
<> feesAdaPot
where
AdaPots
{ treasuryAdaPot,
reservesAdaPot,
rewardsAdaPot,
utxoAdaPot,
depositsAdaPot,
feesAdaPot
} = totalAdaPotsES cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import Cardano.Ledger.Credential (Credential)
import Cardano.Ledger.Era (Crypto, Era)
import Cardano.Ledger.Keys (KeyRole (Staking))
import Cardano.Ledger.PoolDistr (IndividualPoolStake (..), PoolDistr (..))
import Cardano.Ledger.Shelley.AdaPots (AdaPots, totalAdaPotsES)
import Cardano.Ledger.Shelley.Constraints (UsesTxOut, UsesValue)
import Cardano.Ledger.Shelley.EpochBoundary
import Cardano.Ledger.Shelley.LedgerState
Expand Down Expand Up @@ -82,6 +83,7 @@ data NewEpochEvent era
| TotalRewardEvent EpochNo (Map.Map (Credential 'Staking (Crypto era)) (Set (Reward (Crypto era))))
| EpochEvent (Event (Core.EraRule "EPOCH" era))
| MirEvent (Event (Core.EraRule "MIR" era))
| TotalAdaPotsEvent AdaPots

instance
( UsesTxOut era,
Expand Down Expand Up @@ -175,7 +177,9 @@ newEpochTransition = do
SJust (Complete ru') -> updateRewards ru'
es'' <- trans @(Core.EraRule "MIR" era) $ TRC ((), es', ())
es''' <- trans @(Core.EraRule "EPOCH" era) $ TRC ((), es'', e)
let EpochState _acnt ss _ls _pr _ _ = es'''
let adaPots = totalAdaPotsES es'''
tellEvent $ TotalAdaPotsEvent adaPots
let ss = esSnapshots es'''
pd' = calculatePoolDistr (_pstakeSet ss)
pure $
src
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import Cardano.Ledger.PoolDistr (PoolDistr (..))
import qualified Cardano.Ledger.Pretty as PP
import Cardano.Ledger.Serialization (ToCBORGroup)
import Cardano.Ledger.Shelley (ShelleyEra)
import Cardano.Ledger.Shelley.API.Wallet
import Cardano.Ledger.Shelley.AdaPots
( AdaPots (..),
totalAdaES,
totalAdaPotsES,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Test.Cardano.Ledger.Shelley.ShelleyTranslation (testGroupShelleyTranslation) where

import Cardano.Ledger.Shelley (ShelleyEra)
import Cardano.Ledger.Shelley.API.Wallet (totalAdaES)
import Cardano.Ledger.Shelley.AdaPots (totalAdaES)
import Cardano.Ledger.Shelley.LedgerState (EpochState, returnRedeemAddrsToReserves)
import Test.Cardano.Ledger.Shelley.ConcreteCryptoTypes (C_Crypto)
import Test.Cardano.Ledger.Shelley.Generator.ShelleyEraGen ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import Cardano.Ledger.Pretty
ppWord64,
)
import Cardano.Ledger.SafeHash (hashAnnotated)
import Cardano.Ledger.Shelley.API.Wallet (totalAdaPotsES)
import Cardano.Ledger.Shelley.AdaPots (totalAdaPotsES)
import Cardano.Ledger.Shelley.Constraints (UsesValue)
import Cardano.Ledger.Shelley.LedgerState
( AccountState (..),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ import Cardano.Ledger.Shelley.API.Mempool
mkMempoolState,
)
import Cardano.Ledger.Shelley.API.Validation (ApplyBlock, applyTick)
import Cardano.Ledger.Shelley.API.Wallet (totalAdaES)
import Cardano.Ledger.Shelley.AdaPots (totalAdaES)
import Cardano.Ledger.Shelley.Constraints (UsesTxOut, UsesValue, makeTxOut)
import Cardano.Ledger.Shelley.Genesis (initialFundsPseudoTxIn)
import Cardano.Ledger.Shelley.LedgerState (NewEpochState)
Expand Down

0 comments on commit 09b60ce

Please sign in to comment.