Skip to content

Commit 947b887

Browse files
authored
Merge pull request #962 from input-output-hk/erikd/more-rewards
Another rewards accounting issue
2 parents 689253c + 6d94816 commit 947b887

File tree

5 files changed

+21
-17
lines changed

5 files changed

+21
-17
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ insertTxMetadata tracer txId metadata =
762762
insert (key, md) = do
763763
let jsonbs = LBS.toStrict $ Aeson.encode (metadataValueToJsonNoSchema md)
764764
singleKeyCBORMetadata = serialiseToCBOR $ makeTransactionMetadata (Map.singleton key md)
765-
mjson <- safeDecodeToJson tracer "insertTxMetdata" jsonbs
765+
mjson <- safeDecodeToJson tracer "insertTxMetadata" jsonbs
766766

767767
void . lift . DB.insertTxMetadata $
768768
DB.TxMetadata

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ finalizeEpochBulkOps lenv = do
6262
mapM_ (insertEpochInterleaved (leTrace lenv)) bops
6363

6464
-- | This gets called with the full set of rewards. If there are no blocks produced in the last 20%
65-
-- of the slots within an epoch, the rewards are updated in the normal way so they will be inserted
66-
-- here.
65+
-- of the slots within an epoch, the rewards will not be updated in the normal way so they will be
66+
-- inserted here.
6767
forceInsertRewards
6868
:: (MonadBaseControl IO m, MonadIO m)
6969
=> Trace IO Text -> LedgerEnv -> Generic.Rewards -> ReaderT SqlBackend m ()
7070
forceInsertRewards tracer lenv rwds = do
71-
let mapSize = Map.size $ Generic.rwdRewards rwds
72-
hasEntry <- DB.queryHasRewardsForEpoch $ unEpochNo (Generic.rwdEpoch rwds)
73-
when (mapSize > 0 && not hasEntry) $ do
71+
let mapSize = Generic.elemCount rwds
72+
count <- fromIntegral <$> DB.queryEpochRewardCount (unEpochNo $ Generic.rwdEpoch rwds)
73+
when (mapSize > count) $ do
7474
liftIO . logWarning tracer $ mconcat
7575
[ "forceInsertRewards: ", textShow mapSize, " rewards for epoch "
7676
, textShow (unEpochNo $ Generic.rwdEpoch rwds), " is "

cardano-db/src/Cardano/Db/Query.hs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ module Cardano.Db.Query
1717
, queryBlocksAfterSlot
1818
, queryCalcEpochEntry
1919
, queryCheckPoints
20+
, queryCurrentEpochNo
2021
, queryDepositUpToBlockNo
2122
, queryEpochEntry
2223
, queryEpochNo
23-
, queryCurrentEpochNo
24+
, queryEpochRewardCount
2425
, queryFeesUpToBlockNo
2526
, queryFeesUpToSlotNo
2627
, queryGenesisSupply
27-
, queryHasRewardsForEpoch
2828
, queryShelleyGenesisSupply
2929
, queryLatestBlock
3030
, queryLatestCachedEpochNo
@@ -335,6 +335,13 @@ queryCurrentEpochNo = do
335335
pure $ max_ (blk ^. BlockEpochNo)
336336
pure $ join (unValue =<< listToMaybe res)
337337

338+
queryEpochRewardCount :: MonadIO m => Word64 -> ReaderT SqlBackend m Word64
339+
queryEpochRewardCount epochNum = do
340+
res <- select . from $ \ rwds -> do
341+
where_ (rwds ^. RewardSpendableEpoch ==. val epochNum)
342+
pure countRows
343+
pure $ maybe 0 unValue (listToMaybe res)
344+
338345
-- | Get the fees paid in all block from genesis up to and including the specified block.
339346
queryFeesUpToBlockNo :: MonadIO m => Word64 -> ReaderT SqlBackend m Ada
340347
queryFeesUpToBlockNo blkNo = do
@@ -363,13 +370,6 @@ queryGenesisSupply = do
363370
pure $ sum_ (txOut ^. TxOutValue)
364371
pure $ unValueSumAda (listToMaybe res)
365372

366-
queryHasRewardsForEpoch :: MonadIO m => Word64 -> ReaderT SqlBackend m Bool
367-
queryHasRewardsForEpoch epochNum = do
368-
res <- select . from $ \ rwds -> do
369-
where_ (rwds ^. RewardSpendableEpoch ==. val epochNum)
370-
pure countRows
371-
pure $ maybe False (\c -> unValue c > 0) (listToMaybe res :: Maybe (Value Word64))
372-
373373
-- | Return the total Shelley Genesis coin supply. The Shelley Genesis Block
374374
-- is the unique which has a non-null PreviousId, but has null Epoch.
375375
queryShelleyGenesisSupply :: MonadIO m => ReaderT SqlBackend m Ada

cardano-sync/src/Cardano/Sync/Era/Shelley/Generic/Rewards.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
module Cardano.Sync.Era.Shelley.Generic.Rewards
77
( Reward (..)
88
, Rewards (..)
9+
, elemCount
910
, epochRewards
1011
, mergeRewards
1112
, rewardsPoolHashKeys
@@ -59,6 +60,9 @@ data Rewards = Rewards
5960
, rwdRewards :: !(Map StakeCred (Set Reward))
6061
} deriving Eq
6162

63+
elemCount :: Rewards -> Int
64+
elemCount = sum . map Set.size . Map.elems . rwdRewards
65+
6266
epochRewards :: Ledger.Network -> EpochNo -> ExtLedgerState CardanoBlock -> Maybe Rewards
6367
epochRewards nw epoch lstate =
6468
case ledgerState lstate of
@@ -75,7 +79,7 @@ mergeRewards :: Rewards -> Rewards -> Rewards
7579
mergeRewards amap bmap =
7680
Rewards
7781
{ rwdEpoch = max (rwdEpoch amap) (rwdEpoch bmap)
78-
, rwdRewards = Map.unionWith Set.union (rwdRewards amap) (rwdRewards bmap)
82+
, rwdRewards = Map.unionWith mappend (rwdRewards amap) (rwdRewards bmap)
7983
}
8084

8185
rewardsPoolHashKeys :: Rewards -> Set StakePoolKeyHash

cardano-sync/src/Cardano/Sync/LedgerEvent.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ convertMirRewards nw resPay trePay =
116116
convertResPay = mapBimap (Generic.toStakeCred nw) (mkPayment RwdReserves)
117117

118118
convertTrePay :: Map (Ledger.StakeCredential StandardCrypto) Coin -> Map Generic.StakeCred (Set Generic.Reward)
119-
convertTrePay = mapBimap (Generic.toStakeCred nw) (mkPayment RwdReserves)
119+
convertTrePay = mapBimap (Generic.toStakeCred nw) (mkPayment RwdTreasury)
120120

121121
mkPayment :: RewardSource -> Coin -> Set Generic.Reward
122122
mkPayment src coin =

0 commit comments

Comments
 (0)