Skip to content

Commit 119d5b7

Browse files
committed
db-analyser: add DumpStakeDistributions pass
1 parent 2503a39 commit 119d5b7

File tree

7 files changed

+87
-1
lines changed

7 files changed

+87
-1
lines changed

ouroboros-consensus-cardano/app/DBAnalyser/Parsers.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ parseAnalysis = asum [
120120
]
121121
, benchmarkLedgerOpsParser
122122
, getBlockApplicationMetrics
123+
, flag' DumpStakeDistributions $ mconcat [
124+
long "dump-stake-distributions"
125+
, help "Show the stake distribution for each epoch of some processed block"
126+
]
123127
, pure OnlyValidation
124128
]
125129

ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBAnalyser/Analysis.hs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ module Cardano.Tools.DBAnalyser.Analysis (
2525
, runAnalysis
2626
) where
2727

28+
import Cardano.Ledger.Crypto (StandardCrypto)
29+
import qualified Cardano.Ledger.PoolDistr as SL
2830
import qualified Cardano.Slotting.Slot as Slotting
2931
import qualified Cardano.Tools.DBAnalyser.Analysis.BenchmarkLedgerOps.FileWriting as F
3032
import qualified Cardano.Tools.DBAnalyser.Analysis.BenchmarkLedgerOps.SlotDataPoint as DP
@@ -115,6 +117,7 @@ runAnalysis analysisName = case go analysisName of
115117
go (ReproMempoolAndForge nBks) = mkAnalysis $ reproMempoolForge nBks
116118
go (BenchmarkLedgerOps mOutfile lgrAppMode) = mkAnalysis $ benchmarkLedgerOps mOutfile lgrAppMode
117119
go (GetBlockApplicationMetrics nrBlocks mOutfile) = mkAnalysis $ getBlockApplicationMetrics nrBlocks mOutfile
120+
go DumpStakeDistributions = mkAnalysis $ dumpStakeDistributions
118121

119122
mkAnalysis ::
120123
forall startFrom. SingI startFrom
@@ -218,6 +221,7 @@ data TraceEvent blk =
218221
-- * monotonic time to call 'Mempool.getSnapshotFor'
219222
-- * total time spent in the mutator when calling 'Mempool.getSnapshotFor'
220223
-- * total time spent in gc when calling 'Mempool.getSnapshotFor'
224+
| DumpStakeDistribution EpochNo (SL.PoolDistr StandardCrypto)
221225

222226
instance (HasAnalysis blk, LedgerSupportsProtocol blk) => Show (TraceEvent blk) where
223227
show (StartedEvent analysisName) = "Started " <> (show analysisName)
@@ -271,7 +275,14 @@ instance (HasAnalysis blk, LedgerSupportsProtocol blk) => Show (TraceEvent blk)
271275
, "mutSnap " <> show mutSnap
272276
, "gcSnap " <> show gcSnap
273277
]
274-
278+
show (DumpStakeDistribution eno pd) =
279+
intercalate "\t"
280+
$ (\ss -> show eno : show (SL.pdTotalActiveStake pd) : show (Map.size mp) : ss)
281+
$ [ show (keyhash, SL.individualTotalPoolStake x, SL.individualPoolStake x)
282+
| (keyhash, x) <- Map.assocs mp
283+
]
284+
where
285+
mp = SL.unPoolDistr pd
275286

276287
{-------------------------------------------------------------------------------
277288
Analysis: show block and slot number and hash for all blocks
@@ -863,6 +874,40 @@ reproMempoolForge numBlks env = do
863874
-- this flushes blk from the mempool, since every tx in it is now on the chain
864875
void $ Mempool.syncWithLedger mempool
865876

877+
{-------------------------------------------------------------------------------
878+
Analysis: print out the stake distibution for each epoch
879+
-------------------------------------------------------------------------------}
880+
881+
dumpStakeDistributions ::
882+
forall blk.
883+
( HasAnalysis blk,
884+
LedgerSupportsProtocol blk
885+
) =>
886+
Analysis blk StartFromLedgerState
887+
dumpStakeDistributions env = do
888+
void $ processAll db registry GetBlock startFrom limit (initLedger, Nothing) process
889+
pure Nothing
890+
where
891+
AnalysisEnv {db, cfg, limit, registry, startFrom, tracer} = env
892+
893+
FromLedgerState initLedger = startFrom
894+
895+
process
896+
:: (ExtLedgerState blk, Maybe EpochNo)
897+
-> blk
898+
-> IO (ExtLedgerState blk, Maybe EpochNo)
899+
process (oldLedger, mbEpoch) blk = do
900+
let lcfg = ExtLedgerCfg cfg
901+
newLedger = tickThenReapply lcfg blk oldLedger
902+
lst = ledgerState newLedger
903+
904+
(,) newLedger <$> case HasAnalysis.epochPoolDistr lst of
905+
Just (epoch, pd)
906+
| mbEpoch /= Just epoch ->
907+
Just epoch <$ traceWith tracer (DumpStakeDistribution epoch pd)
908+
909+
_ -> pure mbEpoch
910+
866911
{-------------------------------------------------------------------------------
867912
Auxiliary: processing all blocks in the DB
868913
-------------------------------------------------------------------------------}

ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBAnalyser/Block/Byron.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ instance HasAnalysis ByronBlock where
4141
-- metrics for the Byron era only.
4242
blockApplicationMetrics = []
4343

44+
epochPoolDistr _lst = Nothing
45+
4446
instance HasProtocolInfo ByronBlock where
4547
data Args ByronBlock =
4648
ByronBlockArgs {

ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBAnalyser/Block/Cardano.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ analyseBlock f =
9191
p :: Proxy HasAnalysis
9292
p = Proxy
9393

94+
analyseLedgerState ::
95+
(forall blk. HasAnalysis blk => LedgerState blk -> a)
96+
-> LedgerState (CardanoBlock StandardCrypto) -> a
97+
analyseLedgerState f =
98+
hcollapse
99+
. hcmap (Proxy @HasAnalysis) (K . f . currentState)
100+
. Telescope.tip
101+
. getHardForkState
102+
. hardForkLedgerStatePerEra
103+
94104
-- | Lift a function polymorphic over all block types supporting `HasAnalysis`
95105
-- into a corresponding function over `CardanoBlock.`
96106
analyseWithLedgerState ::
@@ -299,6 +309,8 @@ instance (HasAnnTip (CardanoBlock StandardCrypto), GetPrevHash (CardanoBlock Sta
299309
)
300310
]
301311

312+
epochPoolDistr = analyseLedgerState epochPoolDistr
313+
302314
dispatch ::
303315
LedgerState (CardanoBlock StandardCrypto)
304316
-> (LedgerState ByronBlock -> IO Builder)

ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBAnalyser/Block/Shelley.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
{-# LANGUAGE ScopedTypeVariables #-}
66
{-# LANGUAGE TypeApplications #-}
77
{-# LANGUAGE TypeFamilies #-}
8+
{-# LANGUAGE TypeOperators #-}
89
{-# LANGUAGE UndecidableInstances #-}
910

1011
{-# OPTIONS_GHC -Wno-orphans #-}
@@ -49,11 +50,13 @@ import qualified Ouroboros.Consensus.Shelley.Ledger.Block as Shelley
4950
import Ouroboros.Consensus.Shelley.Node (Nonce (..),
5051
ProtocolParamsShelleyBased (..), ShelleyGenesis,
5152
protocolInfoShelley)
53+
import Ouroboros.Consensus.Shelley.Protocol.Abstract (ProtoCrypto)
5254
import Text.Builder (decimal)
5355

5456
-- | Usable for each Shelley-based era
5557
instance ( ShelleyCompatible proto era
5658
, PerEraAnalysis era
59+
, ProtoCrypto proto ~ StandardCrypto
5760
) => HasAnalysis (ShelleyBlock proto era) where
5861

5962
countTxOutputs blk = case Shelley.shelleyBlockRaw blk of
@@ -103,6 +106,13 @@ instance ( ShelleyCompatible proto era
103106
-- metrics for Shelley-only eras.
104107
blockApplicationMetrics = []
105108

109+
epochPoolDistr lst =
110+
Just (SL.nesEL nes, SL.nesPd nes)
111+
where
112+
nes = shelleyLedgerState lst
113+
114+
-----
115+
106116
class PerEraAnalysis era where
107117
txExUnitsSteps :: Maybe (Core.Tx era -> Word64)
108118

ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBAnalyser/HasAnalysis.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module Cardano.Tools.DBAnalyser.HasAnalysis (
88
, WithLedgerState (..)
99
) where
1010

11+
import Cardano.Ledger.Crypto (StandardCrypto)
12+
import Cardano.Ledger.Shelley.API (PoolDistr)
1113
import Data.Map.Strict (Map)
1214
import Ouroboros.Consensus.Block
1315
import Ouroboros.Consensus.HeaderValidation (HasAnnTip (..))
@@ -58,6 +60,16 @@ class (HasAnnTip blk, GetPrevHash blk, Condense (HeaderHash blk)) => HasAnalysis
5860
-- the IO monad.
5961
blockApplicationMetrics :: [(Builder, WithLedgerState blk -> IO Builder)]
6062

63+
-- | The epoch number of the block's slot, and the stake distribution used
64+
-- for the leader schedule of that epoch
65+
--
66+
-- This pool distribution should match 'protocolLedgerView', for example.
67+
--
68+
-- It should return 'Nothing' if and only if the block is in the Byron era.
69+
epochPoolDistr ::
70+
LedgerState blk
71+
-> Maybe (EpochNo, PoolDistr StandardCrypto)
72+
6173
class HasProtocolInfo blk where
6274
data Args blk
6375
mkProtocolInfo :: Args blk -> IO (ProtocolInfo blk)

ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBAnalyser/Types.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ data AnalysisName =
3838
-- The metrics will be written to the provided file path, or to
3939
-- the standard output if no file path is specified.
4040
| GetBlockApplicationMetrics NumberOfBlocks (Maybe FilePath)
41+
| DumpStakeDistributions
4142
deriving Show
4243

4344
data AnalysisResult =

0 commit comments

Comments
 (0)