Skip to content

Commit da4eef9

Browse files
committed
Extract pool stats
1 parent 464f52c commit da4eef9

File tree

6 files changed

+99
-2
lines changed

6 files changed

+99
-2
lines changed

cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/EpochUpdate.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Cardano.DbSync.Era.Shelley.Generic.ProtoParams
1010
import Cardano.DbSync.Types
1111
import Cardano.DbSync.Util
1212
import qualified Cardano.Ledger.BaseTypes as Ledger
13+
import Cardano.Ledger.Coin (Coin)
1314
import Cardano.Ledger.Conway.Governance
1415
import qualified Cardano.Ledger.Shelley.API.Wallet as Shelley
1516
import Cardano.Prelude hiding (Maybe (..), fromMaybe)
@@ -31,6 +32,7 @@ data NewEpoch = NewEpoch
3132
, neEpochUpdate :: !EpochUpdate
3233
, neDRepState :: !(Maybe (DRepPulsingState StandardConway))
3334
, neEnacted :: !(Maybe (ConwayGovState StandardConway))
35+
, nePoolDistr :: !(Maybe (Map PoolKeyHash (Coin, Word64), Map PoolKeyHash Natural))
3436
}
3537

3638
data EpochUpdate = EpochUpdate

cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/StakeDist.hs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Cardano.DbSync.Era.Shelley.Generic.StakeDist (
1414
StakeSlice (..),
1515
getSecurityParameter,
1616
getStakeSlice,
17+
getPoolDistr,
1718
) where
1819

1920
import Cardano.DbSync.Types
@@ -24,11 +25,13 @@ import qualified Cardano.Ledger.EpochBoundary as Ledger
2425
import Cardano.Ledger.Era (EraCrypto)
2526
import Cardano.Ledger.Keys (KeyHash (..), KeyRole (..))
2627
import qualified Cardano.Ledger.Shelley.LedgerState as Shelley
28+
import Cardano.Ledger.Val ((<+>))
2729
import Cardano.Prelude
2830
import qualified Data.Map.Strict as Map
2931
import Data.VMap (VB, VMap (..), VP)
3032
import qualified Data.VMap as VMap
3133
import qualified Data.Vector.Generic as VG
34+
import Lens.Micro
3235
import Ouroboros.Consensus.Block
3336
import Ouroboros.Consensus.Cardano.Block (LedgerState (..), StandardCrypto)
3437
import Ouroboros.Consensus.Config
@@ -172,3 +175,46 @@ genericStakeSlice pInfo epochBlockNo lstate isMigration
172175
VMap.toMap $
173176
VMap.mapMaybe id $
174177
VMap.mapWithKey (\a p -> (,p) <$> lookupStake a) delegationsSliced
178+
179+
getPoolDistr ::
180+
ExtLedgerState CardanoBlock ->
181+
Maybe (Map PoolKeyHash (Coin, Word64), Map PoolKeyHash Natural)
182+
getPoolDistr els =
183+
case ledgerState els of
184+
LedgerStateByron _ -> Nothing
185+
LedgerStateShelley sls -> Just $ genericPoolDistr sls
186+
LedgerStateAllegra als -> Just $ genericPoolDistr als
187+
LedgerStateMary mls -> Just $ genericPoolDistr mls
188+
LedgerStateAlonzo als -> Just $ genericPoolDistr als
189+
LedgerStateBabbage bls -> Just $ genericPoolDistr bls
190+
LedgerStateConway cls -> Just $ genericPoolDistr cls
191+
192+
genericPoolDistr ::
193+
forall era p.
194+
(EraCrypto era ~ StandardCrypto) =>
195+
LedgerState (ShelleyBlock p era) ->
196+
(Map PoolKeyHash (Coin, Word64), Map PoolKeyHash Natural)
197+
genericPoolDistr lstate =
198+
(stakePerPool, blocksPerPool)
199+
where
200+
nes :: Shelley.NewEpochState era
201+
nes = Consensus.shelleyLedgerState lstate
202+
203+
stakeMark :: Ledger.SnapShot StandardCrypto
204+
stakeMark = Ledger.ssStakeMark $ Shelley.esSnapshots $ Shelley.nesEs nes
205+
206+
stakePerPool = countStakePerPool (Ledger.ssDelegations stakeMark) (Ledger.ssStake stakeMark)
207+
blocksPerPool = nes ^. Shelley.nesBprevL
208+
209+
countStakePerPool ::
210+
VMap VB VB StakeCred PoolKeyHash ->
211+
Ledger.Stake StandardCrypto ->
212+
Map PoolKeyHash (Coin, Word64)
213+
countStakePerPool delegs (Ledger.Stake stake) = VMap.foldlWithKey accum Map.empty stake
214+
where
215+
accum !acc cred compactCoin =
216+
case VMap.lookup cred delegs of
217+
Nothing -> acc
218+
Just kh -> Map.insertWith addDel kh (Ledger.fromCompact compactCoin, 1) acc
219+
220+
addDel (c, n) (c', _) = (c <+> c', n + 1)

cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Types.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module Cardano.DbSync.Era.Shelley.Generic.Tx.Types (
1717
TxScript (..),
1818
PlutusData (..),
1919
TxOutDatum (..),
20+
PoolStats (..),
2021
DBScriptPurpose (..),
2122
DBPlutusScript (..),
2223
toTxCert,
@@ -143,6 +144,13 @@ data PlutusData = PlutusData
143144

144145
data TxOutDatum = InlineDatum PlutusData | DatumHash DataHash | NoDatum
145146

147+
data PoolStats = PoolStats
148+
{ nBlocks :: Maybe Natural
149+
, nDelegators :: Maybe Word64
150+
, stake :: Maybe Coin
151+
, votingPower :: Maybe Coin
152+
}
153+
146154
toTxCert :: Word16 -> Cert -> TxCertificate
147155
toTxCert idx dcert =
148156
TxCertificate

cardano-db-sync/src/Cardano/DbSync/Era/Universal/Epoch.hs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
{-# LANGUAGE RankNTypes #-}
77
{-# LANGUAGE RecordWildCards #-}
88
{-# LANGUAGE ScopedTypeVariables #-}
9+
{-# LANGUAGE TupleSections #-}
910
{-# LANGUAGE TypeFamilies #-}
1011
{-# LANGUAGE NoImplicitPrelude #-}
1112

@@ -34,15 +35,17 @@ import Cardano.DbSync.Era.Universal.Insert.Other (toDouble)
3435
import Cardano.DbSync.Error
3536
import Cardano.DbSync.Ledger.Event
3637
import Cardano.DbSync.Types
37-
import Cardano.DbSync.Util (whenStrictJust)
38+
import Cardano.DbSync.Util (whenDefault, whenStrictJust, whenStrictJustDefault)
3839
import Cardano.DbSync.Util.Constraint (constraintNameEpochStake, constraintNameReward)
3940
import Cardano.Ledger.Address (RewardAccount (..))
4041
import Cardano.Ledger.BaseTypes (Network, unEpochInterval)
4142
import qualified Cardano.Ledger.BaseTypes as Ledger
4243
import Cardano.Ledger.Binary.Version (getVersion)
4344
import qualified Cardano.Ledger.Coin as Shelley
45+
import Cardano.Ledger.Compactible
4446
import Cardano.Ledger.Conway.Core (PoolVotingThresholds (..))
4547
import Cardano.Ledger.Conway.Governance (finishDRepPulser)
48+
import qualified Cardano.Ledger.Conway.Governance.DRepPulser as Ledger
4649
import Cardano.Ledger.Conway.PParams (DRepVotingThresholds (..))
4750
import Cardano.Ledger.Conway.Rules (RatifyState (..))
4851
import Cardano.Prelude
@@ -73,18 +76,37 @@ insertOnNewEpoch tracer cache iopts blkId slotNo epochNo newEpoch = do
7376
lift $ insertEpochParam tracer blkId epochNo params (Generic.euNonce epochUpdate)
7477
whenStrictJust (Generic.neAdaPots newEpoch) $ \pots ->
7578
insertPots blkId slotNo epochNo pots
76-
whenStrictJust (Generic.neDRepState newEpoch) $ \dreps -> when (ioGov iopts) $ do
79+
spoVoting <- whenStrictJustDefault Map.empty (Generic.neDRepState newEpoch) $ \dreps -> whenDefault Map.empty (ioGov iopts) $ do
7780
let (drepSnapshot, ratifyState) = finishDRepPulser dreps
7881
lift $ insertDrepDistr epochNo drepSnapshot
7982
updateRatified cache epochNo (toList $ rsEnacted ratifyState)
8083
updateExpired cache epochNo (toList $ rsExpired ratifyState)
84+
pure (Ledger.psPoolDistr drepSnapshot)
8185
whenStrictJust (Generic.neEnacted newEpoch) $ \enactedSt -> do
8286
when (ioGov iopts) $ do
8387
insertUpdateEnacted tracer cache blkId epochNo enactedSt
88+
whenStrictJust (Generic.nePoolDistr newEpoch) $ \(poolDistrDeleg, poolDistrNBlocks) -> do
89+
let nothingMap = Map.fromList $ (,Nothing) <$> (Map.keys poolDistrNBlocks <> Map.keys spoVoting)
90+
let mapWithAllKeys = Map.union (Map.map Just poolDistrDeleg) nothingMap
91+
let _ = Map.mapWithKey (mkPoolStats poolDistrNBlocks spoVoting) mapWithAllKeys
92+
pure ()
8493
where
8594
epochUpdate :: Generic.EpochUpdate
8695
epochUpdate = Generic.neEpochUpdate newEpoch
8796

97+
mkPoolStats :: Map PoolKeyHash Natural -> Map PoolKeyHash (Shelley.CompactForm Shelley.Coin) -> PoolKeyHash -> Maybe (Shelley.Coin, Word64) -> Generic.PoolStats
98+
mkPoolStats blocks voting pkh deleg =
99+
let
100+
mnBlock = Map.lookup pkh blocks
101+
mVoting = Map.lookup pkh voting
102+
in
103+
Generic.PoolStats
104+
{ Generic.nBlocks = mnBlock
105+
, Generic.nDelegators = snd <$> deleg
106+
, Generic.stake = fst <$> deleg
107+
, Generic.votingPower = fromCompact <$> mVoting
108+
}
109+
88110
insertEpochParam ::
89111
(MonadBaseControl IO m, MonadIO m) =>
90112
Trace IO Text ->
@@ -372,3 +394,10 @@ sumRewardTotal =
372394
sumCoin :: Integer -> Set Generic.Reward -> Integer
373395
sumCoin !acc sr =
374396
acc + sum (map (Shelley.unCoin . Generic.rewardAmount) $ Set.toList sr)
397+
398+
_inseertPoolStats ::
399+
Monad m =>
400+
SyncEnv ->
401+
Map PoolKeyHash Generic.PoolStats ->
402+
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
403+
_inseertPoolStats _syncEnv _mp = pure () -- TODO

cardano-db-sync/src/Cardano/DbSync/Ledger/State.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ applyBlock env blk = do
278278
, Generic.neEpochUpdate = Generic.epochUpdate newState
279279
, Generic.neDRepState = maybeToStrict $ getDrepState newState
280280
, Generic.neEnacted = maybeToStrict $ getGovState newState
281+
, Generic.nePoolDistr = maybeToStrict $ Generic.getPoolDistr newState
281282
}
282283

283284
applyToEpochBlockNo :: Bool -> Bool -> EpochBlockNo -> EpochBlockNo

cardano-db-sync/src/Cardano/DbSync/Util.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ module Cardano.DbSync.Util (
2828
splitLast,
2929
traverseMEither,
3030
whenStrictJust,
31+
whenStrictJustDefault,
32+
whenDefault,
3133
whenMaybe,
3234
mlookup,
3335
whenRight,
@@ -181,6 +183,15 @@ whenStrictJust ma f =
181183
Strict.Nothing -> pure ()
182184
Strict.Just a -> f a
183185

186+
whenStrictJustDefault :: Applicative m => b -> Strict.Maybe a -> (a -> m b) -> m b
187+
whenStrictJustDefault b ma f =
188+
case ma of
189+
Strict.Nothing -> pure b
190+
Strict.Just a -> f a
191+
192+
whenDefault :: Applicative m => a -> Bool -> m a -> m a
193+
whenDefault a bl ma = if bl then ma else pure a
194+
184195
whenMaybe :: Monad m => Maybe a -> (a -> m b) -> m (Maybe b)
185196
whenMaybe (Just a) f = Just <$> f a
186197
whenMaybe Nothing _f = pure Nothing

0 commit comments

Comments
 (0)