Skip to content

Commit

Permalink
db: Introduce RewardSource type
Browse files Browse the repository at this point in the history
Rewards from the ledger are either `RewardLeader` (rewards earned as the
pool owner) or `RewardMember` (earned as a delegatee).

There are however also instantaneous rewards that can come from either
the reserves or the treasury. This means we need a new type that includes
all four source.
  • Loading branch information
erikd committed Jul 23, 2021
1 parent 31e5966 commit 4204d88
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ insertRewards epoch icache rewardsChunk = do
poolId <- hoistEither $ lookupPoolIdPair "insertRewards StakePool" (Shelley.rewardPool rwd) icache
pure $ DB.Reward
{ DB.rewardAddrId = saId
, DB.rewardType = DB.showRewardType (Shelley.rewardType rwd)
, DB.rewardType = DB.showRewardSource (DB.rewardTypeToSource $ Shelley.rewardType rwd)
, DB.rewardAmount = Generic.coinToDbLovelace (Shelley.rewardAmount rwd)
, DB.rewardEpochNo = unEpochNo epoch
, DB.rewardPoolId = poolId
Expand All @@ -190,7 +190,7 @@ insertOrphanedRewards epoch icache orphanedRewardsChunk = do
poolId <- hoistEither $ lookupPoolIdPair "insertOrphanedRewards StakePool" (Shelley.rewardPool rwd) icache
pure $ DB.OrphanedReward
{ DB.orphanedRewardAddrId = saId
, DB.orphanedRewardType = DB.showRewardType (Shelley.rewardType rwd)
, DB.orphanedRewardType = DB.showRewardSource (DB.rewardTypeToSource $ Shelley.rewardType rwd)
, DB.orphanedRewardAmount = Generic.coinToDbLovelace (Shelley.rewardAmount rwd)
, DB.orphanedRewardEpochNo = unEpochNo epoch
, DB.orphanedRewardPoolId = poolId
Expand Down
41 changes: 30 additions & 11 deletions cardano-db/src/Cardano/Db/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Cardano.Db.Types
, DbLovelace (..)
, DbInt65 (..)
, DbWord64 (..)
, RewardSource (..)
, SyncState (..)
, deltaCoinToDbInt65
, integerToDbInt65
Expand All @@ -18,10 +19,11 @@ module Cardano.Db.Types
, scientificToAda
, readDbInt65
, showDbInt65
, readRewardType
, readRewardSource
, readSyncState
, renderSyncState
, showRewardType
, rewardTypeToSource
, showRewardSource
, word64ToAda
) where

Expand Down Expand Up @@ -83,6 +85,13 @@ newtype DbWord64
deriving (Eq, Generic)
deriving (Read, Show) via (Quiet DbWord64)

data RewardSource
= RwdMember
| RwdLeader
| RwdReserves
| RwdTreasury
deriving (Bounded, Enum, Eq, Show)

data SyncState
= SyncLagging -- Local tip is lagging the global chain tip.
| SyncFollowing -- Local tip is following global chain tip.
Expand Down Expand Up @@ -124,14 +133,16 @@ showDbInt65 i65 =
NegInt65 0 -> "0"
NegInt65 w -> '-' : show w

readRewardType :: Text -> Shelley.RewardType
readRewardType str =
readRewardSource :: Text -> RewardSource
readRewardSource str =
case str of
"member" -> Shelley.MemberReward
"leader" -> Shelley.LeaderReward
"member" -> RwdMember
"leader" -> RwdLeader
"reserves" -> RwdReserves
"treasury" -> RwdTreasury
-- This should never happen. On the Postgres side we defined an ENUM with
-- only the two values as above.
_other -> error $ "readRewardType: Unknown RewardType " ++ Text.unpack str
_other -> error $ "readRewardSource: Unknown RewardSource " ++ Text.unpack str

readSyncState :: String -> SyncState
readSyncState str =
Expand All @@ -148,11 +159,19 @@ renderSyncState ss =
SyncFollowing -> "following"
SyncLagging -> "lagging"

showRewardType :: Shelley.RewardType -> Text
showRewardType rt =
rewardTypeToSource :: Shelley.RewardType -> RewardSource
rewardTypeToSource rt =
case rt of
Shelley.LeaderReward -> RwdLeader
Shelley.MemberReward -> RwdMember

showRewardSource :: RewardSource -> Text
showRewardSource rt =
case rt of
Shelley.MemberReward -> "member"
Shelley.LeaderReward -> "leader"
RwdMember -> "member"
RwdLeader -> "leader"
RwdReserves -> "reserves"
RwdTreasury -> "treasury"

word64ToAda :: Word64 -> Ada
word64ToAda w =
Expand Down
4 changes: 2 additions & 2 deletions schema/migration-1-0005-20210311.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ BEGIN
IF next_version = 5 THEN

-- Would normally put this inside an "EXECUTE" statement, but that does not work for some
-- reason and this does.
CREATE TYPE rewardtype AS ENUM ('leader', 'member');
-- reason and this does. In Haskell code this is the RewardSource type.
CREATE TYPE rewardtype AS ENUM ('leader', 'member', 'reserves', 'treasury');

UPDATE "schema_version" SET stage_one = next_version;
RAISE NOTICE 'DB has been migrated to stage_one version %', next_version;
Expand Down

0 comments on commit 4204d88

Please sign in to comment.