Skip to content

Commit 62f890a

Browse files
committed
rework shelley stake address whitelist
Signed-off-by: Cmdv <[email protected]>
1 parent 8a7728e commit 62f890a

File tree

13 files changed

+344
-316
lines changed

13 files changed

+344
-316
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ registerStakeCreds = do
412412

413413
registerStakeCredsNoShelley :: IOManager -> [(Text, Text)] -> Assertion
414414
registerStakeCredsNoShelley = do
415-
withCustomConfig args Nothing cfgDir testLabel $ \interpreter mockServer dbSync -> do
415+
withCustomConfigAndDropDB args Nothing cfgDir testLabel $ \interpreter mockServer dbSync -> do
416416
startDBSync dbSync
417417

418418
-- These should not be saved when shelley is disabled

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

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ where
3030
import Cardano.BM.Trace
3131
import qualified Cardano.Db as DB
3232
import Cardano.DbSync.Api (getTrace)
33-
import Cardano.DbSync.Api.Types (InsertOptions (..), SyncEnv (..), SyncOptions (..))
33+
import Cardano.DbSync.Api.Types (SyncEnv (..))
3434
import Cardano.DbSync.Cache.Epoch (rollbackMapEpochInCache)
3535
import qualified Cardano.DbSync.Cache.LRU as LRU
3636
import Cardano.DbSync.Cache.Types (Cache (..), CacheInternal (..), CacheNew (..), CacheStatistics (..), StakeAddrCache, initCacheStatistics)
@@ -39,7 +39,6 @@ import Cardano.DbSync.Era.Shelley.Query
3939
import Cardano.DbSync.Era.Util
4040
import Cardano.DbSync.Error
4141
import Cardano.DbSync.Types
42-
import Cardano.DbSync.Util.Whitelist (shelleyInsertWhitelistCheck)
4342
import qualified Cardano.Ledger.Address as Ledger
4443
import Cardano.Ledger.BaseTypes (Network)
4544
import Cardano.Ledger.Mary.Value
@@ -91,21 +90,12 @@ queryOrInsertRewardAccount ::
9190
Cache ->
9291
CacheNew ->
9392
Ledger.RewardAccount StandardCrypto ->
94-
ReaderT SqlBackend m (Maybe DB.StakeAddressId)
93+
ReaderT SqlBackend m DB.StakeAddressId
9594
queryOrInsertRewardAccount syncEnv cache cacheNew rewardAddr = do
96-
if shelleyInsertWhitelistCheck (ioShelley iopts) laBs
97-
then do
98-
eiAddrId <- queryRewardAccountWithCacheRetBs cache cacheNew rewardAddr
99-
case eiAddrId of
100-
Left (_err, bs) -> insertStakeAddress syncEnv rewardAddr (Just bs)
101-
Right addrId -> pure $ Just addrId
102-
else pure Nothing
103-
where
104-
-- this is the stake address of the reward account used when whitelisting
105-
!laBs = Ledger.serialiseRewardAcnt (Ledger.RewardAcnt nw cred)
106-
nw = Ledger.getRwdNetwork rewardAddr
107-
cred = Ledger.getRwdCred rewardAddr
108-
iopts = soptInsertOptions $ envOptions syncEnv
95+
eiAddrId <- queryRewardAccountWithCacheRetBs cache cacheNew rewardAddr
96+
case eiAddrId of
97+
Left (_err, bs) -> insertStakeAddress syncEnv rewardAddr (Just bs)
98+
Right addrId -> pure addrId
10999

110100
queryOrInsertStakeAddress ::
111101
(MonadBaseControl IO m, MonadIO m) =>
@@ -114,7 +104,7 @@ queryOrInsertStakeAddress ::
114104
CacheNew ->
115105
Network ->
116106
StakeCred ->
117-
ReaderT SqlBackend m (Maybe DB.StakeAddressId)
107+
ReaderT SqlBackend m DB.StakeAddressId
118108
queryOrInsertStakeAddress syncEnv cache cacheNew nw cred =
119109
queryOrInsertRewardAccount syncEnv cache cacheNew $ Ledger.RewardAccount nw cred
120110

@@ -125,24 +115,18 @@ insertStakeAddress ::
125115
SyncEnv ->
126116
Ledger.RewardAccount StandardCrypto ->
127117
Maybe ByteString ->
128-
ReaderT SqlBackend m (Maybe DB.StakeAddressId)
129-
insertStakeAddress syncEnv rewardAddr stakeCredBs =
130-
-- check if the address is in the whitelist
131-
if shelleyInsertWhitelistCheck ioptsShelley addrBs
132-
then do
133-
stakeAddrsId <-
134-
DB.insertStakeAddress $
135-
DB.StakeAddress
136-
{ DB.stakeAddressHashRaw = addrBs
137-
, DB.stakeAddressView = Generic.renderRewardAcnt rewardAddr
138-
, DB.stakeAddressScriptHash = Generic.getCredentialScriptHash $ Ledger.getRwdCred rewardAddr
139-
}
140-
pure $ Just stakeAddrsId
141-
else pure Nothing
118+
ReaderT SqlBackend m DB.StakeAddressId
119+
insertStakeAddress _syncEnv rewardAddr stakeCredBs = do
120+
DB.insertStakeAddress $
121+
DB.StakeAddress
122+
{ DB.stakeAddressHashRaw = addrBs
123+
, DB.stakeAddressView = Generic.renderRewardAcnt rewardAddr
124+
, DB.stakeAddressScriptHash = Generic.getCredentialScriptHash $ Ledger.getRwdCred rewardAddr
125+
}
142126
where
143127
addrBs = fromMaybe (Ledger.serialiseRewardAcnt rewardAddr) stakeCredBs
144-
ioptsShelley = ioShelley . soptInsertOptions $ envOptions syncEnv
145128

129+
------------------------------------------------------------------------------------------------
146130
queryRewardAccountWithCacheRetBs ::
147131
forall m.
148132
MonadIO m =>

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ module Cardano.DbSync.Config.Types (
4444
isTxOutEnabled,
4545
hasLedger,
4646
shouldUseLedger,
47-
isShelleyEnabled,
47+
isShelleyNotDisabled,
4848
isMultiAssetEnabled,
4949
isMetadataEnabled,
5050
isPlutusEnabled,
@@ -325,10 +325,10 @@ shouldUseLedger LedgerDisable = False
325325
shouldUseLedger LedgerEnable = True
326326
shouldUseLedger LedgerIgnore = False
327327

328-
isShelleyEnabled :: ShelleyInsertConfig -> Bool
329-
isShelleyEnabled ShelleyDisable = False
330-
isShelleyEnabled ShelleyEnable = True
331-
isShelleyEnabled (ShelleyStakeAddrs _) = True
328+
isShelleyNotDisabled :: ShelleyInsertConfig -> Bool
329+
isShelleyNotDisabled ShelleyDisable = False
330+
isShelleyNotDisabled ShelleyEnable = True
331+
isShelleyNotDisabled (ShelleyStakeAddrs _) = True
332332

333333
isMultiAssetEnabled :: MultiAssetConfig -> Bool
334334
isMultiAssetEnabled MultiAssetDisable = False
@@ -463,7 +463,7 @@ instance FromJSON LedgerInsertConfig where
463463
instance ToJSON ShelleyInsertConfig where
464464
toJSON cfg =
465465
Aeson.object
466-
[ "enable" .= isShelleyEnabled cfg
466+
[ "enable" .= isShelleyNotDisabled cfg
467467
, "stake_addresses" .= stakeAddrs cfg
468468
]
469469
where

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Cardano.DbSync.Api.Types (InsertOptions (..), SyncEnv (..), SyncOptions (
1818
import Cardano.DbSync.Cache (insertBlockAndCache, queryPoolKeyWithCache, queryPrevBlockWithCache)
1919
import Cardano.DbSync.Cache.Epoch (writeEpochBlockDiffToCache)
2020
import Cardano.DbSync.Cache.Types (Cache (..), CacheNew (..), EpochBlockDiff (..))
21-
import Cardano.DbSync.Config.Types (isShelleyEnabled)
21+
import Cardano.DbSync.Config.Types (isShelleyNotDisabled)
2222
import qualified Cardano.DbSync.Era.Shelley.Generic as Generic
2323
import Cardano.DbSync.Era.Universal.Epoch
2424
import Cardano.DbSync.Era.Universal.Insert.Grouped
@@ -66,7 +66,7 @@ insertBlockUniversal syncEnv shouldLog withinTwoMins withinHalfHour blk details
6666
mPhid <- lift $ queryPoolKeyWithCache syncEnv CacheNew $ coerceKeyRole $ Generic.blkSlotLeader blk
6767
let epochNo = sdEpochNo details
6868

69-
slid <- lift . DB.insertSlotLeader $ Generic.mkSlotLeader (isShelleyEnabled $ ioShelley iopts) (Generic.unKeyHashRaw $ Generic.blkSlotLeader blk) (eitherToMaybe mPhid)
69+
slid <- lift . DB.insertSlotLeader $ Generic.mkSlotLeader (isShelleyNotDisabled $ ioShelley iopts) (Generic.unKeyHashRaw $ Generic.blkSlotLeader blk) (eitherToMaybe mPhid)
7070
blkId <-
7171
lift . insertBlockAndCache cache $
7272
DB.Block

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

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import Cardano.DbSync.Api
2828
import Cardano.DbSync.Api.Types (InsertOptions (..), SyncEnv (..))
2929
import Cardano.DbSync.Cache (queryOrInsertStakeAddress, queryPoolKeyOrInsert)
3030
import Cardano.DbSync.Cache.Types (Cache, CacheNew (..))
31-
import Cardano.DbSync.Config.Types (isShelleyEnabled)
31+
import Cardano.DbSync.Config.Types (isShelleyNotDisabled)
3232
import qualified Cardano.DbSync.Era.Shelley.Generic as Generic
3333
import Cardano.DbSync.Era.Universal.Insert.Certificate (insertPots)
3434
import Cardano.DbSync.Era.Universal.Insert.GovAction (insertCostModel, insertDrepDistr, updateEnacted, updateExpired, updateRatified)
@@ -38,7 +38,8 @@ import Cardano.DbSync.Ledger.Event
3838
import Cardano.DbSync.Types
3939
import Cardano.DbSync.Util (whenStrictJust)
4040
import Cardano.DbSync.Util.Constraint (constraintNameEpochStake, constraintNameReward)
41-
import Cardano.Ledger.Address (RewardAccount (..))
41+
import Cardano.DbSync.Util.Whitelist (shelleyStakeAddrWhitelistCheck)
42+
import qualified Cardano.Ledger.Address as Ledger
4243
import Cardano.Ledger.BaseTypes (Network, unEpochInterval)
4344
import qualified Cardano.Ledger.BaseTypes as Ledger
4445
import Cardano.Ledger.Binary.Version (getVersion)
@@ -214,20 +215,23 @@ insertEpochStake syncEnv nw epochNo stakeChunk = do
214215
Cache ->
215216
(StakeCred, (Shelley.Coin, PoolKeyHash)) ->
216217
ExceptT SyncNodeError (ReaderT SqlBackend m) (Maybe DB.EpochStake)
217-
mkStake cache (saddr, (coin, pool)) = do
218-
mSaId <- lift $ queryOrInsertStakeAddress syncEnv cache CacheNew nw saddr
219-
poolId <- lift $ queryPoolKeyOrInsert "insertEpochStake" syncEnv cache CacheNew (isShelleyEnabled $ ioShelley iopts) pool
220-
case mSaId of
221-
Nothing -> pure Nothing
222-
Just saId ->
223-
pure $
224-
Just $
225-
DB.EpochStake
226-
{ DB.epochStakeAddrId = saId
227-
, DB.epochStakePoolId = poolId
228-
, DB.epochStakeAmount = Generic.coinToDbLovelace coin
229-
, DB.epochStakeEpochNo = unEpochNo epochNo -- The epoch where this delegation becomes valid.
230-
}
218+
mkStake cache (saddr, (coin, pool)) =
219+
-- Check if the stake address is in the shelley whitelist
220+
if shelleyStakeAddrWhitelistCheck syncEnv $ Ledger.RewardAcnt nw saddr
221+
then
222+
( do
223+
saId <- lift $ queryOrInsertStakeAddress syncEnv cache CacheNew nw saddr
224+
poolId <- lift $ queryPoolKeyOrInsert "insertEpochStake" syncEnv cache CacheNew (isShelleyNotDisabled $ ioShelley iopts) pool
225+
pure $
226+
Just $
227+
DB.EpochStake
228+
{ DB.epochStakeAddrId = saId
229+
, DB.epochStakePoolId = poolId
230+
, DB.epochStakeAmount = Generic.coinToDbLovelace coin
231+
, DB.epochStakeEpochNo = unEpochNo epochNo -- The epoch where this delegation becomes valid.
232+
}
233+
)
234+
else pure Nothing
231235

232236
iopts = getInsertOptions syncEnv
233237

@@ -251,11 +255,13 @@ insertRewards syncEnv nw earnedEpoch spendableEpoch cache rewardsChunk = do
251255
(MonadBaseControl IO m, MonadIO m) =>
252256
(StakeCred, Set Generic.Reward) ->
253257
ExceptT SyncNodeError (ReaderT SqlBackend m) [DB.Reward]
254-
mkRewards (saddr, rset) = do
255-
mSaId <- lift $ queryOrInsertStakeAddress syncEnv cache CacheNew nw saddr
256-
case mSaId of
257-
Nothing -> pure []
258-
Just saId -> mapM (prepareReward saId) (Set.toList rset)
258+
mkRewards (saddr, rset) =
259+
-- Check if the stake address is in the shelley whitelist
260+
if shelleyStakeAddrWhitelistCheck syncEnv $ Ledger.RewardAcnt nw saddr
261+
then do
262+
saId <- lift $ queryOrInsertStakeAddress syncEnv cache CacheNew nw saddr
263+
mapM (prepareReward saId) (Set.toList rset)
264+
else pure []
259265

260266
prepareReward ::
261267
(MonadBaseControl IO m, MonadIO m) =>
@@ -279,7 +285,7 @@ insertRewards syncEnv nw earnedEpoch spendableEpoch cache rewardsChunk = do
279285
PoolKeyHash ->
280286
ExceptT SyncNodeError (ReaderT SqlBackend m) DB.PoolHashId
281287
queryPool poolHash =
282-
lift (queryPoolKeyOrInsert "insertRewards" syncEnv cache CacheNew (isShelleyEnabled $ ioShelley iopts) poolHash)
288+
lift (queryPoolKeyOrInsert "insertRewards" syncEnv cache CacheNew (isShelleyNotDisabled $ ioShelley iopts) poolHash)
283289

284290
iopts = getInsertOptions syncEnv
285291

@@ -302,11 +308,13 @@ insertInstantRewards syncEnv nw earnedEpoch spendableEpoch cache rewardsChunk =
302308
(MonadBaseControl IO m, MonadIO m) =>
303309
(StakeCred, Set Generic.RewardRest) ->
304310
ExceptT SyncNodeError (ReaderT SqlBackend m) [DB.RewardRest]
305-
mkRewards (saddr, rset) = do
306-
mSaId <- lift $ queryOrInsertStakeAddress syncEnv cache CacheNew nw saddr
307-
case mSaId of
308-
Nothing -> pure []
309-
Just saId -> pure $ map (prepareReward saId) (Set.toList rset)
311+
mkRewards (saddr, rset) =
312+
-- Check if the stake address is in the shelley whitelist
313+
if shelleyStakeAddrWhitelistCheck syncEnv $ Ledger.RewardAccount nw saddr
314+
then do
315+
saId <- lift $ queryOrInsertStakeAddress syncEnv cache CacheNew nw saddr
316+
pure $ map (prepareReward saId) (Set.toList rset)
317+
else pure []
310318

311319
prepareReward ::
312320
DB.StakeAddressId ->

0 commit comments

Comments
 (0)