Skip to content

Commit

Permalink
use total block production instead of latest one
Browse files Browse the repository at this point in the history
  The rationale is that it is quite confusing as many pools
  have only a few blocks produced per epoch (1 or 2) and sometimes
  even 0 for several epochs.
  This makes it even harder to assess whether a pool is doing okay
  or not. Looking at the total number of blocks produced makes more
  sense.
  • Loading branch information
KtorZ committed Jan 21, 2020
1 parent 9f45466 commit c698e9a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/core/src/Cardano/Pool/DB.hs
Expand Up @@ -70,6 +70,10 @@ data DBLayer m = forall stm. (MonadFail stm, MonadIO stm) => DBLayer
-- ^ Read the all stake pools together with corresponding slot ids
-- for a given epoch.

, readTotalProduction
:: stm (Map PoolId (Quantity "block" Word64))
-- ^ Read the total pool production since the pool was first registered.

, putStakeDistribution
:: EpochNo
-> [(PoolId, Quantity "lovelace" Word64)]
Expand Down
4 changes: 4 additions & 0 deletions lib/core/src/Cardano/Pool/DB/MVar.hs
Expand Up @@ -33,6 +33,7 @@ import Cardano.Pool.DB.Model
, mReadPoolRegistration
, mReadStakeDistribution
, mReadSystemSeed
, mReadTotalProduction
, mRollbackTo
)
import Control.Concurrent.MVar
Expand Down Expand Up @@ -62,6 +63,9 @@ newDBLayer = do
, readPoolProduction =
readPoolDB db . mReadPoolProduction

, readTotalProduction =
readPoolDB db mReadTotalProduction

, putStakeDistribution = \a0 a1 ->
void $ alterPoolDB (const Nothing) db (mPutStakeDistribution a0 a1)

Expand Down
5 changes: 5 additions & 0 deletions lib/core/src/Cardano/Pool/DB/Model.hs
Expand Up @@ -35,6 +35,7 @@ module Cardano.Pool.DB.Model
, mCleanPoolProduction
, mPutPoolProduction
, mReadPoolProduction
, mReadTotalProduction
, mPutStakeDistribution
, mReadStakeDistribution
, mPutPoolRegistration
Expand Down Expand Up @@ -147,6 +148,10 @@ mReadPoolProduction epoch db@PoolDatabase{pools} =
updatePools = Map.filter (not . L.null)
in (Right (updatePools $ (updateSlots epoch) pools), db)

mReadTotalProduction :: ModelPoolOp (Map PoolId (Quantity "block" Word64))
mReadTotalProduction db@PoolDatabase{pools} =
( Right (Map.map (Quantity . fromIntegral . length) pools), db )

mPutStakeDistribution
:: EpochNo
-> [(PoolId, Quantity "lovelace" Word64)]
Expand Down
11 changes: 11 additions & 0 deletions lib/core/src/Cardano/Pool/DB/Sqlite.hs
Expand Up @@ -166,6 +166,17 @@ newDBLayer trace fp = do

pure (foldl' toMap Map.empty production)

, readTotalProduction = do
production <- fmap entityVal <$>
selectList ([] :: [Filter PoolProduction]) []

let toMap m (PoolProduction{poolProductionPoolId}) =
Map.alter alter poolProductionPoolId m
where
alter = Just . maybe 1 (+1)

pure $ Map.map Quantity $ foldl' toMap Map.empty production

, putStakeDistribution = \epoch@(EpochNo ep) distribution -> do
deleteWhere [StakeDistributionEpoch ==. fromIntegral ep]
insertMany_ (mkStakeDistribution epoch distribution)
Expand Down
8 changes: 4 additions & 4 deletions lib/core/src/Cardano/Pool/Metrics.hs
Expand Up @@ -57,7 +57,7 @@ import Cardano.Pool.Metadata
, sameStakePoolMetadata
)
import Cardano.Pool.Performance
( count, readPoolsPerformances )
( readPoolsPerformances )
import Cardano.Pool.Ranking
( EpochConstants (..), unsafeMkNonNegative, unsafeMkRatio )
import Cardano.Wallet.Network
Expand Down Expand Up @@ -277,7 +277,7 @@ newStakePoolLayer tr getEpCst db@DBLayer{..} nl metadataDir = StakePoolLayer

(distr, prod, prodTip) <- liftIO . atomically $ (,,)
<$> (Map.fromList <$> readStakeDistribution nodeEpoch)
<*> readPoolProduction nodeEpoch
<*> readTotalProduction
<*> readPoolProductionTip

when (Map.null distr || Map.null prod) $ do
Expand All @@ -288,13 +288,13 @@ newStakePoolLayer tr getEpCst db@DBLayer{..} nl metadataDir = StakePoolLayer
then do
seed <- liftIO $ atomically readSystemSeed
let epCst = getEpCst 0
combineWith epCst (sortArbitrarily seed) distr (count prod) mempty
combineWith epCst (sortArbitrarily seed) distr prod mempty

else do
let currentEpoch = prodTip ^. #slotId . #epochNumber
perfs <- liftIO $ readPoolsPerformances db currentEpoch
let epCst = getEpCst currentEpoch
combineWith epCst (pure . sortByDesirability) distr (count prod) perfs
combineWith epCst (pure . sortByDesirability) distr prod perfs

readPoolProductionTip = readPoolProductionCursor 1 <&> \case
[] -> header block0
Expand Down

0 comments on commit c698e9a

Please sign in to comment.