Skip to content

Commit 09616be

Browse files
committed
Fix reward tests
1 parent d5c4221 commit 09616be

File tree

5 files changed

+36
-18
lines changed

5 files changed

+36
-18
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import Test.Cardano.Db.Mock.UnifiedApi (
5252
withAlonzoFindLeaderAndSubmit,
5353
withAlonzoFindLeaderAndSubmitTx,
5454
)
55-
import Test.Cardano.Db.Mock.Validate (assertBlockNoBackoff, assertRewardCount, assertRewardCounts)
55+
import Test.Cardano.Db.Mock.Validate (assertBlockNoBackoff, assertInstantRewardCount, assertRewardCount, assertRewardCounts)
5656
import Test.Tasty.HUnit (Assertion)
5757

5858
simpleRewards :: IOManager -> [(Text, Text)] -> Assertion
@@ -481,6 +481,6 @@ singleMIRCertMultiOut =
481481
b <- fillUntilNextEpoch interpreter mockServer
482482

483483
assertBlockNoBackoff dbSync (2 + length a + length b)
484-
assertRewardCount dbSync 4
484+
assertInstantRewardCount dbSync 4
485485
where
486486
testLabel = "singleMIRCertMultiOut-alonzo"

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import Test.Cardano.Db.Mock.UnifiedApi (
5151
import Test.Cardano.Db.Mock.Validate (
5252
assertBlockNoBackoff,
5353
assertCurrentEpoch,
54+
assertInstantRewardCount,
5455
assertRewardCount,
5556
assertRewardCounts,
5657
)
@@ -566,6 +567,6 @@ singleMIRCertMultiOut =
566567
b <- fillUntilNextEpoch interpreter mockServer
567568

568569
assertBlockNoBackoff dbSync (2 + length a + length b)
569-
assertRewardCount dbSync 4
570+
assertInstantRewardCount dbSync 4
570571
where
571572
testLabel = "singleMIRCertMultiOut"

cardano-chain-gen/test/Test/Cardano/Db/Mock/Validate.hs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Test.Cardano.Db.Mock.Validate (
1414
assertTxInCount,
1515
assertUnspentTx,
1616
assertRewardCount,
17+
assertInstantRewardCount,
1718
assertBlockNoBackoff,
1819
assertBlockNoBackoffTimes,
1920
assertEqQuery,
@@ -79,7 +80,7 @@ import Database.Esqueleto.Legacy (
7980
(==.),
8081
(^.),
8182
)
82-
import Database.Persist.Sql (Entity, SqlBackend, entityVal)
83+
import Database.Persist.Sql (Entity, SqlBackend)
8384
import Database.PostgreSQL.Simple (SqlError (..))
8485
import Ouroboros.Consensus.Cardano.Block
8586
import Ouroboros.Consensus.Shelley.Ledger (ShelleyBlock)
@@ -112,6 +113,10 @@ assertRewardCount :: DBSyncEnv -> Word64 -> IO ()
112113
assertRewardCount env n =
113114
assertEqBackoff env queryRewardCount n defaultDelays "Unexpected rewards count"
114115

116+
assertInstantRewardCount :: DBSyncEnv -> Word64 -> IO ()
117+
assertInstantRewardCount env n =
118+
assertEqBackoff env queryInstantRewardCount n defaultDelays "Unexpected instant rewards count"
119+
115120
assertBlockNoBackoff :: DBSyncEnv -> Int -> IO ()
116121
assertBlockNoBackoff = assertBlockNoBackoffTimes defaultDelays
117122

@@ -242,7 +247,7 @@ assertRewardCounts env st filterAddr mEpoch expected = do
242247
expectedMap :: Map ByteString (Word64, Word64, Word64, Word64, Word64)
243248
expectedMap = Map.fromList $ fmap (first mkDBStakeAddress) expected
244249

245-
groupByAddress :: [(Reward, ByteString)] -> Map ByteString (Word64, Word64, Word64, Word64, Word64)
250+
groupByAddress :: [(RewardSource, ByteString)] -> Map ByteString (Word64, Word64, Word64, Word64, Word64)
246251
groupByAddress rewards =
247252
let res = foldr updateMap Map.empty rewards
248253
in if filterAddr
@@ -255,39 +260,46 @@ assertRewardCounts env st filterAddr mEpoch expected = do
255260
Right cred -> Ledger.serialiseRewardAcnt $ Ledger.RewardAcnt Testnet cred
256261

257262
updateAddrCounters ::
258-
Reward ->
263+
RewardSource ->
259264
Maybe (Word64, Word64, Word64, Word64, Word64) ->
260265
(Word64, Word64, Word64, Word64, Word64)
261-
updateAddrCounters reward Nothing = updateCounters reward (0, 0, 0, 0, 0)
262-
updateAddrCounters reward (Just cs) = updateCounters reward cs
266+
updateAddrCounters rs Nothing = updateCounters rs (0, 0, 0, 0, 0)
267+
updateAddrCounters rs (Just cs) = updateCounters rs cs
263268

264269
updateCounters ::
265-
Reward ->
270+
RewardSource ->
266271
(Word64, Word64, Word64, Word64, Word64) ->
267272
(Word64, Word64, Word64, Word64, Word64)
268-
updateCounters reward (a, b, c, d, e) = case rewardType reward of
273+
updateCounters rs (a, b, c, d, e) = case rs of
269274
RwdLeader -> (a + 1, b, c, d, e)
270275
RwdMember -> (a, b + 1, c, d, e)
271276
RwdReserves -> (a, b, c + 1, d, e)
272277
RwdTreasury -> (a, b, c, d + 1, e)
273278
RwdDepositRefund -> (a, b, c, d, e + 1)
274279

275280
updateMap ::
276-
(Reward, ByteString) ->
281+
(RewardSource, ByteString) ->
277282
Map ByteString (Word64, Word64, Word64, Word64, Word64) ->
278283
Map ByteString (Word64, Word64, Word64, Word64, Word64)
279-
updateMap (rew, addr) = Map.alter (Just . updateAddrCounters rew) addr
284+
updateMap (rs, addr) = Map.alter (Just . updateAddrCounters rs) addr
280285

281286
filterEpoch rw = case mEpoch of
282287
Nothing -> val True
283288
Just e -> rw ^. RewardSpendableEpoch ==. val e
289+
filterEpoch' rw = case mEpoch of
290+
Nothing -> val True
291+
Just e -> rw ^. InstantRewardSpendableEpoch ==. val e
284292

285293
q = do
286-
res <- select . from $ \(reward `InnerJoin` stake_addr) -> do
294+
res1 <- select . from $ \(reward `InnerJoin` stake_addr) -> do
287295
on (reward ^. RewardAddrId ==. stake_addr ^. StakeAddressId)
288296
where_ (filterEpoch reward)
289-
pure (reward, stake_addr ^. StakeAddressHashRaw)
290-
pure $ fmap (bimap entityVal unValue) res
297+
pure (reward ^. RewardType, stake_addr ^. StakeAddressHashRaw)
298+
res2 <- select . from $ \(ireward `InnerJoin` stake_addr) -> do
299+
on (ireward ^. InstantRewardAddrId ==. stake_addr ^. StakeAddressId)
300+
where_ (filterEpoch' ireward)
301+
pure (ireward ^. InstantRewardType, stake_addr ^. StakeAddressHashRaw)
302+
pure $ fmap (bimap unValue unValue) (res1 <> res2)
291303

292304
assertEpochStake :: DBSyncEnv -> Word64 -> IO ()
293305
assertEpochStake env expected =

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ module Cardano.Db.Query (
8989
-- queries used only in tests
9090
queryAddressOutputs,
9191
queryRewardCount,
92+
queryInstantRewardCount,
9293
queryTxInCount,
9394
queryEpochCount,
9495
queryCostModel,
@@ -1199,6 +1200,13 @@ queryRewardCount = do
11991200
pure countRows
12001201
pure $ maybe 0 unValue (listToMaybe res)
12011202

1203+
queryInstantRewardCount :: MonadIO m => ReaderT SqlBackend m Word64
1204+
queryInstantRewardCount = do
1205+
res <- select $ do
1206+
_ <- from $ table @InstantReward
1207+
pure countRows
1208+
pure $ maybe 0 unValue (listToMaybe res)
1209+
12021210
-- | Count the number of transactions in the Tx table.
12031211
queryTxInCount :: MonadIO m => ReaderT SqlBackend m Word
12041212
queryTxInCount = do

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,6 @@ share
316316
amount DbLovelace sqltype=lovelace
317317
earnedEpoch Word64 generated="(CASE WHEN spendable_epoch >= 1 then spendable_epoch-1 else 0 end)"
318318
spendableEpoch Word64
319-
-- Here used to lie a unique constraint which would slow down inserts when in syncing mode
320-
-- Now the constraint is set manually inside of `applyAndInsertBlockMaybe` once the tip of
321-
-- the chain has been reached.
322319
UniqueInstantReward addrId earnedEpoch type
323320
deriving Show
324321

0 commit comments

Comments
 (0)