Skip to content

Commit

Permalink
Merge pull request #575 from input-output-hk/erikd/schema-wibbling
Browse files Browse the repository at this point in the history
Massive update of database schema
  • Loading branch information
erikd committed Apr 7, 2021
2 parents 99fb50c + e4a62a0 commit 8ad7514
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 350 deletions.
21 changes: 9 additions & 12 deletions cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs
Expand Up @@ -159,7 +159,7 @@ insertOnNewEpoch tracer blkId slotNo epochNo newEpoch = do
insertGenericRewards grewards stakes

insertEpochParam tracer blkId epochNo (Generic.euProtoParams esum) (Generic.euNonce esum)
insertEpochStake tracer blkId epochNo stakes
insertEpochStake tracer epochNo stakes

whenJust (Generic.adaPots newEpoch) $ \pots ->
insertPots blkId slotNo epochNo pots
Expand All @@ -178,8 +178,8 @@ insertOnNewEpoch tracer blkId slotNo epochNo newEpoch = do
]

-- Subtract 2 from the epoch to calculate when the epoch in which the reward was earned.
insertRewards tracer blkId (epochNo - 2) (Generic.rewards grewards)
insertOrphanedRewards tracer blkId (epochNo - 2) (Generic.orphaned grewards)
insertRewards tracer (epochNo - 2) (Generic.rewards grewards)
insertOrphanedRewards tracer (epochNo - 2) (Generic.orphaned grewards)

-- -----------------------------------------------------------------------------

Expand Down Expand Up @@ -648,9 +648,9 @@ containsUnicodeNul = Text.isInfixOf "\\u000"

insertRewards
:: (MonadBaseControl IO m, MonadIO m)
=> Trace IO Text -> DB.BlockId -> EpochNo -> Map Generic.StakeCred (Set (Shelley.Reward StandardCrypto))
=> Trace IO Text -> EpochNo -> Map Generic.StakeCred (Set (Shelley.Reward StandardCrypto))
-> ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertRewards _tracer blkId epoch rewards = do
insertRewards _tracer epoch rewards = do
forM_ (chunksOf 1000 $ Map.toList rewards) $ \rewardsChunk -> do
dbRewards <- concatMapM mkRewards rewardsChunk
lift $ putMany dbRewards
Expand All @@ -669,14 +669,13 @@ insertRewards _tracer blkId epoch rewards = do
, DB.rewardAmount = Generic.coinToDbLovelace (Shelley.rewardAmount rwd)
, DB.rewardEpochNo = unEpochNo epoch
, DB.rewardPoolId = poolId
, DB.rewardBlockId = blkId
}

insertOrphanedRewards
:: (MonadBaseControl IO m, MonadIO m)
=> Trace IO Text -> DB.BlockId -> EpochNo -> Map Generic.StakeCred (Set (Shelley.Reward StandardCrypto))
=> Trace IO Text -> EpochNo -> Map Generic.StakeCred (Set (Shelley.Reward StandardCrypto))
-> ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertOrphanedRewards _tracer blkId epoch orphanedRewards =
insertOrphanedRewards _tracer epoch orphanedRewards =
-- There are probably not many of these for each epoch, but just in case there
-- are, it does not hurt to chunk them.
forM_ (chunksOf 1000 $ Map.toList orphanedRewards) $ \orphanedRewardsChunk -> do
Expand All @@ -697,7 +696,6 @@ insertOrphanedRewards _tracer blkId epoch orphanedRewards =
, DB.orphanedRewardAmount = Generic.coinToDbLovelace (Shelley.rewardAmount rwd)
, DB.orphanedRewardEpochNo = unEpochNo epoch
, DB.orphanedRewardPoolId = poolId
, DB.orphanedRewardBlockId = blkId
}

insertEpochParam
Expand Down Expand Up @@ -732,9 +730,9 @@ insertEpochParam _tracer blkId (EpochNo epoch) params nonce =

insertEpochStake
:: (MonadBaseControl IO m, MonadIO m)
=> Trace IO Text -> DB.BlockId -> EpochNo -> Generic.StakeDist
=> Trace IO Text -> EpochNo -> Generic.StakeDist
-> ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertEpochStake _tracer blkId (EpochNo epoch) smap =
insertEpochStake _tracer (EpochNo epoch) smap =
forM_ (chunksOf 1000 $ Map.toList (Generic.unStakeDist smap)) $ \stakeChunk -> do
dbStakes <- mapM mkStake stakeChunk
lift $ putMany dbStakes
Expand All @@ -751,7 +749,6 @@ insertEpochStake _tracer blkId (EpochNo epoch) smap =
, DB.epochStakePoolId = poolId
, DB.epochStakeAmount = Generic.coinToDbLovelace coin
, DB.epochStakeEpochNo = epoch -- The epoch where this delegation becomes valid.
, DB.epochStakeBlockId = blkId
}

insertMaTxMint
Expand Down
14 changes: 7 additions & 7 deletions cardano-db/src/Cardano/Db/Schema.hs
Expand Up @@ -274,34 +274,34 @@ share
-- -----------------------------------------------------------------------------------------------
-- Reward, Stake and Treasury need to be obtained from the ledger state.

-- The reward for each stake address and. This is not a balance, but a reward amount and the
-- epoch in which the reward was earned.
-- The reward for each stake address and. This is not a balance, but a reward amount and the
-- epoch in which the reward was earned.
-- This table should never get rolled back.
Reward
addrId StakeAddressId OnDeleteCascade
type Text sqltype=rewardtype
amount DbLovelace sqltype=lovelace
epochNo Word64
poolId PoolHashId OnDeleteCascade
blockId BlockId OnDeleteCascade
UniqueReward addrId blockId
UniqueReward addrId epochNo

-- Orphaned rewards happen when a stake address earns rewards, but the stake address is
-- deregistered before the rewards are distributed.
-- This table should never get rolled back.
OrphanedReward
addrId StakeAddressId OnDeleteCascade
type Text sqltype=rewardtype
amount DbLovelace sqltype=lovelace
epochNo Word64
poolId PoolHashId OnDeleteCascade
blockId BlockId OnDeleteCascade
UniqueOrphaned addrId blockId
UniqueOrphaned addrId epochNo

-- This table should never get rolled back.
EpochStake
addrId StakeAddressId OnDeleteCascade
poolId PoolHashId OnDeleteCascade
amount DbLovelace sqltype=lovelace
epochNo Word64
blockId BlockId OnDeleteCascade -- To make rollbacks work correctly.
UniqueStake addrId epochNo

Treasury
Expand Down

0 comments on commit 8ad7514

Please sign in to comment.