@@ -11,23 +11,26 @@ module Cardano.DbSync.Cache (
11
11
insertBlockAndCache ,
12
12
insertDatumAndCache ,
13
13
insertPoolKeyWithCache ,
14
+ insertStakeAddress ,
14
15
queryDatum ,
15
16
queryMAWithCache ,
17
+ queryOrInsertRewardAccount ,
18
+ queryOrInsertStakeAddress ,
16
19
queryPoolKeyOrInsert ,
17
20
queryPoolKeyWithCache ,
18
21
queryPrevBlockWithCache ,
19
- queryOrInsertStakeAddress ,
20
- queryOrInsertRewardAccount ,
21
- insertStakeAddress ,
22
22
queryStakeAddrWithCache ,
23
23
rollbackCache ,
24
24
25
25
-- * CacheStatistics
26
26
getCacheStatistics ,
27
- ) where
27
+ )
28
+ where
28
29
29
30
import Cardano.BM.Trace
30
31
import qualified Cardano.Db as DB
32
+ import Cardano.DbSync.Api (getTrace )
33
+ import Cardano.DbSync.Api.Types (InsertOptions (.. ), SyncEnv (.. ), SyncOptions (.. ))
31
34
import Cardano.DbSync.Cache.Epoch (rollbackMapEpochInCache )
32
35
import qualified Cardano.DbSync.Cache.LRU as LRU
33
36
import Cardano.DbSync.Cache.Types (CacheAction (.. ), CacheInternal (.. ), CacheStatistics (.. ), CacheStatus (.. ), initCacheStatistics , isCacheActionUpdate )
@@ -36,6 +39,7 @@ import Cardano.DbSync.Era.Shelley.Query
36
39
import Cardano.DbSync.Era.Util
37
40
import Cardano.DbSync.Error
38
41
import Cardano.DbSync.Types
42
+ import Cardano.DbSync.Util.Whitelist (shelleyInsertWhitelistCheck )
39
43
import qualified Cardano.Ledger.Address as Ledger
40
44
import Cardano.Ledger.BaseTypes (Network )
41
45
import Cardano.Ledger.Mary.Value
@@ -68,8 +72,8 @@ import Ouroboros.Consensus.Cardano.Block (StandardCrypto)
68
72
-- a different id.
69
73
-- NOTE: Other tables are not cleaned up since they are not rollbacked.
70
74
rollbackCache :: MonadIO m => CacheStatus -> DB. BlockId -> ReaderT SqlBackend m ()
71
- rollbackCache NoCache _ = pure ()
72
- rollbackCache (ActiveCache cache) blockId = do
75
+ rollbackCache UninitiatedCache _ = pure ()
76
+ rollbackCache (Cache cache) blockId = do
73
77
liftIO $ do
74
78
atomically $ writeTVar (cPrevBlock cache) Nothing
75
79
atomically $ modifyTVar (cDatum cache) LRU. cleanup
@@ -83,44 +87,61 @@ getCacheStatistics cs =
83
87
84
88
queryOrInsertRewardAccount ::
85
89
(MonadBaseControl IO m , MonadIO m ) =>
86
- Trace IO Text ->
90
+ SyncEnv ->
87
91
CacheStatus ->
88
92
CacheAction ->
89
93
Ledger. RewardAccount StandardCrypto ->
90
- ReaderT SqlBackend m DB. StakeAddressId
91
- queryOrInsertRewardAccount trce cache cacheUA rewardAddr = do
92
- eiAddrId <- queryRewardAccountWithCacheRetBs trce cache cacheUA rewardAddr
93
- case eiAddrId of
94
- Left (_err, bs) -> insertStakeAddress rewardAddr (Just bs)
95
- Right addrId -> pure addrId
94
+ ReaderT SqlBackend m (Maybe DB. StakeAddressId )
95
+ queryOrInsertRewardAccount syncEnv cache cacheUA rewardAddr = do
96
+ -- check if the stake address is in the whitelist
97
+ if shelleyInsertWhitelistCheck (ioShelley iopts) laBs
98
+ then do
99
+ eiAddrId <- queryRewardAccountWithCacheRetBs cache cacheUA rewardAddr
100
+ case eiAddrId of
101
+ Left (_err, bs) -> insertStakeAddress syncEnv rewardAddr (Just bs)
102
+ Right addrId -> pure $ Just addrId
103
+ else pure Nothing
104
+ where
105
+ nw = Ledger. getRwdNetwork rewardAddr
106
+ cred = Ledger. getRwdCred rewardAddr
107
+ ! laBs = Ledger. serialiseRewardAcnt (Ledger. RewardAcnt nw cred)
108
+ iopts = soptInsertOptions $ envOptions syncEnv
96
109
97
110
queryOrInsertStakeAddress ::
98
111
(MonadBaseControl IO m , MonadIO m ) =>
99
- Trace IO Text ->
112
+ SyncEnv ->
100
113
CacheStatus ->
101
114
CacheAction ->
102
115
Network ->
103
116
StakeCred ->
104
- ReaderT SqlBackend m DB. StakeAddressId
105
- queryOrInsertStakeAddress trce cache cacheUA nw cred =
106
- queryOrInsertRewardAccount trce cache cacheUA $ Ledger. RewardAccount nw cred
117
+ ReaderT SqlBackend m ( Maybe DB. StakeAddressId)
118
+ queryOrInsertStakeAddress syncEnv cache cacheUA nw cred =
119
+ queryOrInsertRewardAccount syncEnv cache cacheUA $ Ledger. RewardAccount nw cred
107
120
108
121
-- If the address already exists in the table, it will not be inserted again (due to
109
122
-- the uniqueness constraint) but the function will return the 'StakeAddressId'.
110
123
insertStakeAddress ::
111
124
(MonadBaseControl IO m , MonadIO m ) =>
125
+ SyncEnv ->
112
126
Ledger. RewardAccount StandardCrypto ->
113
127
Maybe ByteString ->
114
- ReaderT SqlBackend m DB. StakeAddressId
115
- insertStakeAddress rewardAddr stakeCredBs = do
116
- DB. insertStakeAddress $
117
- DB. StakeAddress
118
- { DB. stakeAddressHashRaw = addrBs
119
- , DB. stakeAddressView = Generic. renderRewardAccount rewardAddr
120
- , DB. stakeAddressScriptHash = Generic. getCredentialScriptHash $ Ledger. raCredential rewardAddr
121
- }
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
122
142
where
123
- addrBs = fromMaybe (Ledger. serialiseRewardAccount rewardAddr) stakeCredBs
143
+ addrBs = fromMaybe (Ledger. serialiseRewardAcnt rewardAddr) stakeCredBs
144
+ ioptsShelley = ioShelley . soptInsertOptions $ envOptions syncEnv
124
145
125
146
queryRewardAccountWithCacheRetBs ::
126
147
forall m .
@@ -154,8 +175,7 @@ queryStakeAddrWithCacheRetBs ::
154
175
Network ->
155
176
StakeCred ->
156
177
ReaderT SqlBackend m (Either (DB. LookupFail , ByteString ) DB. StakeAddressId )
157
- queryStakeAddrWithCacheRetBs trce cache cacheUA nw cred = do
158
- let ! bs = Ledger. serialiseRewardAccount (Ledger. RewardAccount nw cred)
178
+ queryStakeAddrWithCacheRetBs cache cacheUA nw cred = do
159
179
case cache of
160
180
NoCache -> do
161
181
mapLeft (,bs) <$> queryStakeAddress bs
@@ -198,11 +218,12 @@ queryStakeAddrWithCacheRetBs trce cache cacheUA nw cred = do
198
218
199
219
queryPoolKeyWithCache ::
200
220
MonadIO m =>
221
+ SyncEnv ->
201
222
CacheStatus ->
202
223
CacheAction ->
203
224
PoolKeyHash ->
204
225
ReaderT SqlBackend m (Either DB. LookupFail DB. PoolHashId )
205
- queryPoolKeyWithCache cache cacheUA hsh =
226
+ queryPoolKeyWithCache syncEnv cache cacheUA hsh =
206
227
case cache of
207
228
NoCache -> do
208
229
mPhId <- queryPoolHashId (Generic. unKeyHashRaw hsh)
@@ -278,14 +299,14 @@ insertPoolKeyWithCache cache cacheUA pHash =
278
299
queryPoolKeyOrInsert ::
279
300
(MonadBaseControl IO m , MonadIO m ) =>
280
301
Text ->
281
- Trace IO Text ->
302
+ SyncEnv ->
282
303
CacheStatus ->
283
304
CacheAction ->
284
305
Bool ->
285
306
PoolKeyHash ->
286
307
ReaderT SqlBackend m DB. PoolHashId
287
- queryPoolKeyOrInsert txt trce cache cacheUA logsWarning hsh = do
288
- pk <- queryPoolKeyWithCache cache cacheUA hsh
308
+ queryPoolKeyOrInsert txt syncEnv cache cacheUA logsWarning hsh = do
309
+ pk <- queryPoolKeyWithCache syncEnv cache cacheUA hsh
289
310
case pk of
290
311
Right poolHashId -> pure poolHashId
291
312
Left err -> do
@@ -304,8 +325,8 @@ queryPoolKeyOrInsert txt trce cache cacheUA logsWarning hsh = do
304
325
insertPoolKeyWithCache cache cacheUA hsh
305
326
306
327
queryMAWithCache ::
307
- MonadIO m =>
308
- CacheStatus ->
328
+ ( MonadIO m ) =>
329
+ Cache ->
309
330
PolicyID StandardCrypto ->
310
331
AssetName ->
311
332
ReaderT SqlBackend m (Either (ByteString , ByteString ) DB. MultiAssetId )
@@ -329,11 +350,14 @@ queryMAWithCache cache policyId asset =
329
350
let ! assetNameBs = Generic. unAssetName asset
330
351
maId <- maybe (Left (policyBs, assetNameBs)) Right <$> DB. queryMultiAssetId policyBs assetNameBs
331
352
whenRight maId $
332
- liftIO . atomically . modifyTVar (cMultiAssets ci) . LRU. insert (policyId, asset)
353
+ liftIO
354
+ . atomically
355
+ . modifyTVar (cMultiAssets ci)
356
+ . LRU. insert (policyId, asset)
333
357
pure maId
334
358
335
359
queryPrevBlockWithCache ::
336
- MonadIO m =>
360
+ ( MonadIO m ) =>
337
361
Text ->
338
362
CacheStatus ->
339
363
ByteString ->
@@ -354,7 +378,7 @@ queryPrevBlockWithCache msg cache hsh =
354
378
Nothing -> queryFromDb ci
355
379
where
356
380
queryFromDb ::
357
- MonadIO m =>
381
+ ( MonadIO m ) =>
358
382
CacheInternal ->
359
383
ExceptT SyncNodeError (ReaderT SqlBackend m ) DB. BlockId
360
384
queryFromDb ci = do
0 commit comments