Skip to content

Commit 5029a8f

Browse files
committed
rework shelley stake address whitelist
Signed-off-by: Cmdv <[email protected]>
1 parent 700467f commit 5029a8f

File tree

13 files changed

+339
-319
lines changed

13 files changed

+339
-319
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 (CacheAction (..), CacheInternal (..), CacheStatistics (..), CacheStatus (..), initCacheStatistics, isCacheActionUpdate)
@@ -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
CacheStatus ->
9291
CacheAction ->
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 cacheUA 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
CacheAction ->
115105
Network ->
116106
StakeCred ->
117-
ReaderT SqlBackend m (Maybe DB.StakeAddressId)
107+
ReaderT SqlBackend m DB.StakeAddressId
118108
queryOrInsertStakeAddress syncEnv cache cacheUA nw cred =
119109
queryOrInsertRewardAccount syncEnv cache cacheUA $ 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ insertBlockUniversal syncEnv shouldLog withinTwoMins withinHalfHour blk details
6666
mPhid <- lift $ queryPoolKeyWithCache syncEnv UpdateCache $ 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: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ import Cardano.DbSync.Ledger.Event
3939
import Cardano.DbSync.Types
4040
import Cardano.DbSync.Util (whenStrictJust)
4141
import Cardano.DbSync.Util.Constraint (constraintNameEpochStake, constraintNameReward)
42-
import Cardano.Ledger.Address (RewardAccount (..))
42+
import Cardano.DbSync.Util.Whitelist (shelleyStakeAddrWhitelistCheck)
43+
import qualified Cardano.Ledger.Address as Ledger
4344
import Cardano.Ledger.BaseTypes (Network, unEpochInterval)
4445
import qualified Cardano.Ledger.BaseTypes as Ledger
4546
import Cardano.Ledger.Binary.Version (getVersion)
@@ -215,21 +216,23 @@ insertEpochStake syncEnv nw epochNo stakeChunk = do
215216
CacheStatus ->
216217
(StakeCred, (Shelley.Coin, PoolKeyHash)) ->
217218
ExceptT SyncNodeError (ReaderT SqlBackend m) (Maybe DB.EpochStake)
218-
mkStake cache (saddr, (coin, pool)) = do
219-
mSaId <- lift $ queryOrInsertStakeAddress syncEnv cache CacheNew nw saddr
220-
poolId <- lift $ queryPoolKeyOrInsert "insertEpochStake" syncEnv cache CacheNew (isShelleyEnabled $ ioShelley iopts) pool
221-
case mSaId of
222-
Nothing -> pure Nothing
223-
Just saId ->
224-
pure $
225-
Just $
226-
DB.EpochStake
227-
{ DB.epochStakeAddrId = saId
228-
, DB.epochStakePoolId = poolId
229-
, DB.epochStakeAmount = Generic.coinToDbLovelace coin
230-
, DB.epochStakeEpochNo = unEpochNo epochNo -- The epoch where this delegation becomes valid.
231-
}
232-
219+
mkStake cache (saddr, (coin, pool)) =
220+
-- Check if the stake address is in the shelley whitelist
221+
if shelleyStakeAddrWhitelistCheck syncEnv $ Ledger.RewardAcnt nw saddr
222+
then
223+
( do
224+
saId <- lift $ queryOrInsertStakeAddress syncEnv cache UpdateCache nw saddr
225+
poolId <- lift $ queryPoolKeyOrInsert "insertEpochStake" syncEnv cache UpdateCache (isShelleyNotDisabled $ ioShelley iopts) pool
226+
pure $
227+
Just $
228+
DB.EpochStake
229+
{ DB.epochStakeAddrId = saId
230+
, DB.epochStakePoolId = poolId
231+
, DB.epochStakeAmount = Generic.coinToDbLovelace coin
232+
, DB.epochStakeEpochNo = unEpochNo epochNo -- The epoch where this delegation becomes valid.
233+
}
234+
)
235+
else pure Nothing
233236
iopts = getInsertOptions syncEnv
234237

235238
insertRewards ::
@@ -252,11 +255,13 @@ insertRewards syncEnv nw earnedEpoch spendableEpoch cache rewardsChunk = do
252255
(MonadBaseControl IO m, MonadIO m) =>
253256
(StakeCred, Set Generic.Reward) ->
254257
ExceptT SyncNodeError (ReaderT SqlBackend m) [DB.Reward]
255-
mkRewards (saddr, rset) = do
256-
mSaId <- lift $ queryOrInsertStakeAddress syncEnv cache CacheNew nw saddr
257-
case mSaId of
258-
Nothing -> pure []
259-
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 UpdateCache nw saddr
263+
mapM (prepareReward saId) (Set.toList rset)
264+
else pure []
260265

261266
prepareReward ::
262267
(MonadBaseControl IO m, MonadIO m) =>
@@ -280,8 +285,7 @@ insertRewards syncEnv nw earnedEpoch spendableEpoch cache rewardsChunk = do
280285
PoolKeyHash ->
281286
ExceptT SyncNodeError (ReaderT SqlBackend m) DB.PoolHashId
282287
queryPool poolHash =
283-
lift (queryPoolKeyOrInsert "insertRewards" syncEnv cache CacheNew (isShelleyEnabled $ ioShelley iopts) poolHash)
284-
288+
lift (queryPoolKeyOrInsert "insertRewards" syncEnv cache UpdateCache (isShelleyEnabled $ ioShelley iopts) poolHash)
285289
iopts = getInsertOptions syncEnv
286290

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

312318
prepareReward ::
313319
DB.StakeAddressId ->

0 commit comments

Comments
 (0)