Skip to content

Commit 089a728

Browse files
committed
Insert PoolStats when flag is on
1 parent da4eef9 commit 089a728

File tree

16 files changed

+104
-27
lines changed

16 files changed

+104
-27
lines changed

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Alonzo/Config.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ insertConfig = do
3131
, sioPlutus = PlutusDisable
3232
, sioGovernance = GovernanceConfig False
3333
, sioOffchainPoolData = OffchainPoolDataConfig False
34+
, sioPoolStats = PoolStatsConfig False
3435
, sioJsonType = JsonTypeDisable
3536
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
3637
}

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/Config/Parse.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ insertConfig = do
3131
, sioPlutus = PlutusDisable
3232
, sioGovernance = GovernanceConfig False
3333
, sioOffchainPoolData = OffchainPoolDataConfig False
34+
, sioPoolStats = PoolStatsConfig False
3435
, sioJsonType = JsonTypeDisable
3536
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
3637
}

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Config/Parse.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ insertConfig = do
101101
, sioPlutus = PlutusDisable
102102
, sioGovernance = GovernanceConfig False
103103
, sioOffchainPoolData = OffchainPoolDataConfig False
104+
, sioPoolStats = PoolStatsConfig False
104105
, sioJsonType = JsonTypeDisable
105106
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
106107
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ extractSyncOptions snp aop snc =
275275
, ioKeepMetadataNames = maybeKeepMNames
276276
, ioPlutusExtra = isPlutusEnabled (sioPlutus (dncInsertOptions snc))
277277
, ioOffChainPoolData = useOffchainPoolData
278+
, ioPoolStats = isPoolStatsEnabled (sioPoolStats (dncInsertOptions snc))
278279
, ioGov = useGovernance
279280
, ioRemoveJsonbFromSchema = isRemoveJsonbFromSchemaEnabled (sioRemoveJsonbFromSchema (dncInsertOptions snc))
280281
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ fullInsertOptions =
232232
, sioPlutus = PlutusEnable
233233
, sioGovernance = GovernanceConfig True
234234
, sioOffchainPoolData = OffchainPoolDataConfig True
235+
, sioPoolStats = PoolStatsConfig True
235236
, sioJsonType = JsonTypeText
236237
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
237238
}
@@ -249,6 +250,7 @@ onlyUTxOInsertOptions =
249250
, sioPlutus = PlutusDisable
250251
, sioGovernance = GovernanceConfig False
251252
, sioOffchainPoolData = OffchainPoolDataConfig False
253+
, sioPoolStats = PoolStatsConfig False
252254
, sioJsonType = JsonTypeText
253255
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
254256
}
@@ -272,6 +274,7 @@ disableAllInsertOptions =
272274
, sioMetadata = MetadataDisable
273275
, sioPlutus = PlutusDisable
274276
, sioOffchainPoolData = OffchainPoolDataConfig False
277+
, sioPoolStats = PoolStatsConfig False
275278
, sioGovernance = GovernanceConfig False
276279
, sioJsonType = JsonTypeText
277280
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False

cardano-db-sync/src/Cardano/DbSync/Api/Types.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ data InsertOptions = InsertOptions
8484
, ioKeepMetadataNames :: Strict.Maybe [Word64]
8585
, ioPlutusExtra :: !Bool
8686
, ioOffChainPoolData :: !Bool
87+
, ioPoolStats :: !Bool
8788
, ioGov :: !Bool
8889
, ioRemoveJsonbFromSchema :: !Bool
8990
}

cardano-db-sync/src/Cardano/DbSync/Config/Types.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module Cardano.DbSync.Config.Types (
2424
SyncInsertConfig (..),
2525
SyncInsertOptions (..),
2626
TxCBORConfig (..),
27+
PoolStatsConfig (..),
2728
TxOutConfig (..),
2829
ForceTxIn (..),
2930
LedgerInsertConfig (..),
@@ -168,6 +169,7 @@ data SyncInsertOptions = SyncInsertOptions
168169
, sioPlutus :: PlutusConfig
169170
, sioGovernance :: GovernanceConfig
170171
, sioOffchainPoolData :: OffchainPoolDataConfig
172+
, sioPoolStats :: PoolStatsConfig
171173
, sioJsonType :: JsonTypeConfig
172174
, sioRemoveJsonbFromSchema :: RemoveJsonbFromSchemaConfig
173175
}
@@ -178,6 +180,11 @@ newtype TxCBORConfig = TxCBORConfig
178180
}
179181
deriving (Eq, Show)
180182

183+
newtype PoolStatsConfig = PoolStatsConfig
184+
{ isPoolStatsEnabled :: Bool
185+
}
186+
deriving (Eq, Show)
187+
181188
data TxOutConfig
182189
= TxOutEnable
183190
| TxOutDisable
@@ -410,6 +417,7 @@ instance FromJSON SyncInsertOptions where
410417
<*> obj .:? "plutus" .!= sioPlutus def
411418
<*> obj .:? "governance" .!= sioGovernance def
412419
<*> obj .:? "offchain_pool_data" .!= sioOffchainPoolData def
420+
<*> obj .:? "pool_stats" .!= sioPoolStats def
413421
<*> obj .:? "json_type" .!= sioJsonType def
414422
<*> obj .:? "remove_jsonb_from_schema" .!= sioRemoveJsonbFromSchema def
415423

@@ -425,19 +433,29 @@ instance ToJSON SyncInsertOptions where
425433
, "plutus" .= sioPlutus
426434
, "governance" .= sioGovernance
427435
, "offchain_pool_data" .= sioOffchainPoolData
436+
, "pool_stats" .= sioPoolStats
428437
, "json_type" .= sioJsonType
429438
, "remove_jsonb_from_schema" .= sioRemoveJsonbFromSchema
430439
]
431440

432441
instance ToJSON TxCBORConfig where
433442
toJSON = boolToEnableDisable . isTxCBOREnabled
434443

444+
instance ToJSON PoolStatsConfig where
445+
toJSON = boolToEnableDisable . isPoolStatsEnabled
446+
435447
instance FromJSON TxCBORConfig where
436448
parseJSON = Aeson.withText "tx_cbor" $ \v ->
437449
case enableDisableToBool v of
438450
Just g -> pure (TxCBORConfig g)
439451
Nothing -> fail $ "unexpected tx_cbor: " <> show v
440452

453+
instance FromJSON PoolStatsConfig where
454+
parseJSON = Aeson.withText "pool_stat" $ \v ->
455+
case enableDisableToBool v of
456+
Just g -> pure (PoolStatsConfig g)
457+
Nothing -> fail $ "unexpected pool_stat: " <> show v
458+
441459
instance ToJSON TxOutConfig where
442460
toJSON cfg =
443461
Aeson.object
@@ -623,6 +641,7 @@ instance Default SyncInsertOptions where
623641
, sioPlutus = PlutusEnable
624642
, sioGovernance = GovernanceConfig True
625643
, sioOffchainPoolData = OffchainPoolDataConfig True
644+
, sioPoolStats = PoolStatsConfig False
626645
, sioJsonType = JsonTypeText
627646
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
628647
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ data PlutusData = PlutusData
145145
data TxOutDatum = InlineDatum PlutusData | DatumHash DataHash | NoDatum
146146

147147
data PoolStats = PoolStats
148-
{ nBlocks :: Maybe Natural
149-
, nDelegators :: Maybe Word64
150-
, stake :: Maybe Coin
148+
{ nBlocks :: Natural
149+
, nDelegators :: Word64
150+
, stake :: Coin
151151
, votingPower :: Maybe Coin
152152
}
153153

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ insertBlockUniversal syncEnv shouldLog withinTwoMins withinHalfHour blk details
147147
]
148148

149149
whenStrictJust (apNewEpoch applyResult) $ \newEpoch -> do
150-
insertOnNewEpoch tracer cache iopts blkId (Generic.blkSlotNo blk) epochNo newEpoch
150+
insertOnNewEpoch syncEnv blkId (Generic.blkSlotNo blk) epochNo newEpoch
151151

152152
insertStakeSlice syncEnv $ apStakeSlice applyResult
153153

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

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,13 @@ import Database.Persist.Sql (SqlBackend)
6363
--------------------------------------------------------------------------------------------
6464
insertOnNewEpoch ::
6565
(MonadBaseControl IO m, MonadIO m) =>
66-
Trace IO Text ->
67-
CacheStatus ->
68-
InsertOptions ->
66+
SyncEnv ->
6967
DB.BlockId ->
7068
SlotNo ->
7169
EpochNo ->
7270
Generic.NewEpoch ->
7371
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
74-
insertOnNewEpoch tracer cache iopts blkId slotNo epochNo newEpoch = do
72+
insertOnNewEpoch syncEnv blkId slotNo epochNo newEpoch = do
7573
whenStrictJust (Generic.euProtoParams epochUpdate) $ \params ->
7674
lift $ insertEpochParam tracer blkId epochNo params (Generic.euNonce epochUpdate)
7775
whenStrictJust (Generic.neAdaPots newEpoch) $ \pots ->
@@ -85,27 +83,27 @@ insertOnNewEpoch tracer cache iopts blkId slotNo epochNo newEpoch = do
8583
whenStrictJust (Generic.neEnacted newEpoch) $ \enactedSt -> do
8684
when (ioGov iopts) $ do
8785
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 ()
86+
whenStrictJust (Generic.nePoolDistr newEpoch) $ \(poolDistrDeleg, poolDistrNBlocks) ->
87+
when (ioPoolStats iopts) $ do
88+
let nothingMap = Map.fromList $ (,Nothing) <$> (Map.keys poolDistrNBlocks <> Map.keys spoVoting)
89+
let mapWithAllKeys = Map.union (Map.map Just poolDistrDeleg) nothingMap
90+
let poolStats = Map.mapWithKey (mkPoolStats poolDistrNBlocks spoVoting) mapWithAllKeys
91+
lift $ insertPoolStats syncEnv epochNo poolStats
9392
where
9493
epochUpdate :: Generic.EpochUpdate
9594
epochUpdate = Generic.neEpochUpdate newEpoch
9695

9796
mkPoolStats :: Map PoolKeyHash Natural -> Map PoolKeyHash (Shelley.CompactForm Shelley.Coin) -> PoolKeyHash -> Maybe (Shelley.Coin, Word64) -> Generic.PoolStats
9897
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-
}
98+
Generic.PoolStats
99+
{ Generic.nBlocks = fromMaybe 0 (Map.lookup pkh blocks)
100+
, Generic.nDelegators = maybe 0 snd deleg
101+
, Generic.stake = maybe (Shelley.Coin 0) fst deleg
102+
, Generic.votingPower = fromCompact <$> Map.lookup pkh voting
103+
}
104+
tracer = getTrace syncEnv
105+
cache = envCache syncEnv
106+
iopts = getInsertOptions syncEnv
109107

110108
insertEpochParam ::
111109
(MonadBaseControl IO m, MonadIO m) =>
@@ -395,9 +393,29 @@ sumRewardTotal =
395393
sumCoin !acc sr =
396394
acc + sum (map (Shelley.unCoin . Generic.rewardAmount) $ Set.toList sr)
397395

398-
_inseertPoolStats ::
399-
Monad m =>
396+
insertPoolStats ::
397+
forall m.
398+
(MonadBaseControl IO m, MonadIO m) =>
400399
SyncEnv ->
400+
EpochNo ->
401401
Map PoolKeyHash Generic.PoolStats ->
402-
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
403-
_inseertPoolStats _syncEnv _mp = pure () -- TODO
402+
ReaderT SqlBackend m ()
403+
insertPoolStats syncEnv epochNo mp = do
404+
poolStats <- mapM preparePoolStat $ Map.toList mp
405+
DB.insertManyPoolStat poolStats
406+
where
407+
preparePoolStat :: (PoolKeyHash, Generic.PoolStats) -> ReaderT SqlBackend m DB.PoolStat
408+
preparePoolStat (pkh, ps) = do
409+
poolId <- queryPoolKeyOrInsert "insertPoolStats" trce cache UpdateCache True pkh
410+
pure
411+
DB.PoolStat
412+
{ DB.poolStatPoolHashId = poolId
413+
, DB.poolStatEpochNo = unEpochNo epochNo
414+
, DB.poolStatNumberOfBlocks = fromIntegral $ Generic.nBlocks ps
415+
, DB.poolStatNumberOfDelegators = fromIntegral $ Generic.nDelegators ps
416+
, DB.poolStatStake = fromIntegral . Shelley.unCoin $ Generic.stake ps
417+
, DB.poolStatVotingPower = fromIntegral . Shelley.unCoin <$> Generic.votingPower ps
418+
}
419+
420+
cache = envCache syncEnv
421+
trce = getTrace syncEnv

0 commit comments

Comments
 (0)