@@ -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 (Cache (.. ), CacheInternal (.. ), CacheNew (.. ), CacheStatistics (.. ), StakeAddrCache , initCacheStatistics )
@@ -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
@@ -67,7 +71,7 @@ import Ouroboros.Consensus.Cardano.Block (StandardCrypto)
67
71
-- NOTE: BlockId is cleaned up on rollbacks, since it may get reinserted on
68
72
-- a different id.
69
73
-- NOTE: Other tables are not cleaned up since they are not rollbacked.
70
- rollbackCache :: MonadIO m => Cache -> DB. BlockId -> ReaderT SqlBackend m ()
74
+ rollbackCache :: ( MonadIO m ) => Cache -> DB. BlockId -> ReaderT SqlBackend m ()
71
75
rollbackCache UninitiatedCache _ = pure ()
72
76
rollbackCache (Cache cache) blockId = do
73
77
liftIO $ do
@@ -83,46 +87,65 @@ getCacheStatistics cs =
83
87
84
88
queryOrInsertRewardAccount ::
85
89
(MonadBaseControl IO m , MonadIO m ) =>
90
+ SyncEnv ->
86
91
Cache ->
87
92
CacheNew ->
88
93
Ledger. RewardAcnt StandardCrypto ->
89
- ReaderT SqlBackend m DB. StakeAddressId
90
- queryOrInsertRewardAccount cache cacheNew rewardAddr = do
91
- eiAddrId <- queryRewardAccountWithCacheRetBs cache cacheNew rewardAddr
92
- case eiAddrId of
93
- Left (_err, bs) -> insertStakeAddress rewardAddr (Just bs)
94
- Right addrId -> pure addrId
94
+ ReaderT SqlBackend m (Maybe DB. StakeAddressId )
95
+ queryOrInsertRewardAccount syncEnv cache cacheNew 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 cacheNew 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
95
109
96
110
queryOrInsertStakeAddress ::
97
111
(MonadBaseControl IO m , MonadIO m ) =>
112
+ SyncEnv ->
98
113
Cache ->
99
114
CacheNew ->
100
115
Network ->
101
116
StakeCred ->
102
- ReaderT SqlBackend m DB. StakeAddressId
103
- queryOrInsertStakeAddress cache cacheNew nw cred =
104
- queryOrInsertRewardAccount cache cacheNew $ Ledger. RewardAcnt nw cred
117
+ ReaderT SqlBackend m ( Maybe DB. StakeAddressId)
118
+ queryOrInsertStakeAddress syncEnv cache cacheNew nw cred =
119
+ queryOrInsertRewardAccount syncEnv cache cacheNew $ Ledger. RewardAcnt nw cred
105
120
106
121
-- If the address already exists in the table, it will not be inserted again (due to
107
122
-- the uniqueness constraint) but the function will return the 'StakeAddressId'.
108
123
insertStakeAddress ::
109
124
(MonadBaseControl IO m , MonadIO m ) =>
125
+ SyncEnv ->
110
126
Ledger. RewardAcnt StandardCrypto ->
111
127
Maybe ByteString ->
112
- ReaderT SqlBackend m DB. StakeAddressId
113
- insertStakeAddress rewardAddr stakeCredBs =
114
- DB. insertStakeAddress $
115
- DB. StakeAddress
116
- { DB. stakeAddressHashRaw = addrBs
117
- , DB. stakeAddressView = Generic. renderRewardAcnt rewardAddr
118
- , DB. stakeAddressScriptHash = Generic. getCredentialScriptHash $ Ledger. getRwdCred rewardAddr
119
- }
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
120
142
where
121
143
addrBs = fromMaybe (Ledger. serialiseRewardAcnt rewardAddr) stakeCredBs
144
+ ioptsShelley = ioShelley . soptInsertOptions $ envOptions syncEnv
122
145
123
146
queryRewardAccountWithCacheRetBs ::
124
147
forall m .
125
- MonadIO m =>
148
+ ( MonadIO m ) =>
126
149
Cache ->
127
150
CacheNew ->
128
151
Ledger. RewardAcnt StandardCrypto ->
@@ -132,7 +155,7 @@ queryRewardAccountWithCacheRetBs cache cacheNew rwdAcc =
132
155
133
156
queryStakeAddrWithCache ::
134
157
forall m .
135
- MonadIO m =>
158
+ ( MonadIO m ) =>
136
159
Cache ->
137
160
CacheNew ->
138
161
Network ->
@@ -143,7 +166,7 @@ queryStakeAddrWithCache cache cacheNew nw cred =
143
166
144
167
queryStakeAddrWithCacheRetBs ::
145
168
forall m .
146
- MonadIO m =>
169
+ ( MonadIO m ) =>
147
170
Cache ->
148
171
CacheNew ->
149
172
Network ->
@@ -161,7 +184,7 @@ queryStakeAddrWithCacheRetBs cache cacheNew nw cred = do
161
184
pure mAddrId
162
185
163
186
queryStakeAddrAux ::
164
- MonadIO m =>
187
+ ( MonadIO m ) =>
165
188
CacheNew ->
166
189
StakeAddrCache ->
167
190
StrictTVar IO CacheStatistics ->
@@ -185,13 +208,13 @@ queryStakeAddrAux cacheNew mp sts nw cred =
185
208
(err, _) -> pure (err, mp)
186
209
187
210
queryPoolKeyWithCache ::
188
- MonadIO m =>
189
- Cache ->
211
+ ( MonadIO m ) =>
212
+ SyncEnv ->
190
213
CacheNew ->
191
214
PoolKeyHash ->
192
215
ReaderT SqlBackend m (Either DB. LookupFail DB. PoolHashId )
193
- queryPoolKeyWithCache cache cacheNew hsh =
194
- case cache of
216
+ queryPoolKeyWithCache syncEnv cacheNew hsh =
217
+ case envCache syncEnv of
195
218
UninitiatedCache -> do
196
219
mPhId <- queryPoolHashId (Generic. unKeyHashRaw hsh)
197
220
case mPhId of
@@ -266,14 +289,14 @@ insertPoolKeyWithCache cache cacheNew pHash =
266
289
queryPoolKeyOrInsert ::
267
290
(MonadBaseControl IO m , MonadIO m ) =>
268
291
Text ->
269
- Trace IO Text ->
292
+ SyncEnv ->
270
293
Cache ->
271
294
CacheNew ->
272
295
Bool ->
273
296
PoolKeyHash ->
274
297
ReaderT SqlBackend m DB. PoolHashId
275
- queryPoolKeyOrInsert txt trce cache cacheNew logsWarning hsh = do
276
- pk <- queryPoolKeyWithCache cache cacheNew hsh
298
+ queryPoolKeyOrInsert txt syncEnv cache cacheNew logsWarning hsh = do
299
+ pk <- queryPoolKeyWithCache syncEnv cacheNew hsh
277
300
case pk of
278
301
Right poolHashId -> pure poolHashId
279
302
Left err -> do
@@ -290,9 +313,11 @@ queryPoolKeyOrInsert txt trce cache cacheNew logsWarning hsh = do
290
313
, " . We will assume that the pool exists and move on."
291
314
]
292
315
insertPoolKeyWithCache cache cacheNew hsh
316
+ where
317
+ trce = getTrace syncEnv
293
318
294
319
queryMAWithCache ::
295
- MonadIO m =>
320
+ ( MonadIO m ) =>
296
321
Cache ->
297
322
PolicyID StandardCrypto ->
298
323
AssetName ->
@@ -317,11 +342,14 @@ queryMAWithCache cache policyId asset =
317
342
let ! assetNameBs = Generic. unAssetName asset
318
343
maId <- maybe (Left (policyBs, assetNameBs)) Right <$> DB. queryMultiAssetId policyBs assetNameBs
319
344
whenRight maId $
320
- liftIO . atomically . modifyTVar (cMultiAssets ci) . LRU. insert (policyId, asset)
345
+ liftIO
346
+ . atomically
347
+ . modifyTVar (cMultiAssets ci)
348
+ . LRU. insert (policyId, asset)
321
349
pure maId
322
350
323
351
queryPrevBlockWithCache ::
324
- MonadIO m =>
352
+ ( MonadIO m ) =>
325
353
Text ->
326
354
Cache ->
327
355
ByteString ->
@@ -342,7 +370,7 @@ queryPrevBlockWithCache msg cache hsh =
342
370
Nothing -> queryFromDb ci
343
371
where
344
372
queryFromDb ::
345
- MonadIO m =>
373
+ ( MonadIO m ) =>
346
374
CacheInternal ->
347
375
ExceptT SyncNodeError (ReaderT SqlBackend m ) DB. BlockId
348
376
queryFromDb ci = do
@@ -365,7 +393,7 @@ insertBlockAndCache cache block =
365
393
pure bid
366
394
367
395
queryDatum ::
368
- MonadIO m =>
396
+ ( MonadIO m ) =>
369
397
Cache ->
370
398
DataHash ->
371
399
ReaderT SqlBackend m (Maybe DB. DatumId )
0 commit comments