@@ -11,23 +11,26 @@ module Cardano.DbSync.Cache (
1111 insertBlockAndCache ,
1212 insertDatumAndCache ,
1313 insertPoolKeyWithCache ,
14+ insertStakeAddress ,
1415 queryDatum ,
1516 queryMAWithCache ,
17+ queryOrInsertRewardAccount ,
18+ queryOrInsertStakeAddress ,
1619 queryPoolKeyOrInsert ,
1720 queryPoolKeyWithCache ,
1821 queryPrevBlockWithCache ,
19- queryOrInsertStakeAddress ,
20- queryOrInsertRewardAccount ,
21- insertStakeAddress ,
2222 queryStakeAddrWithCache ,
2323 rollbackCache ,
2424
2525 -- * CacheStatistics
2626 getCacheStatistics ,
27- ) where
27+ )
28+ where
2829
2930import Cardano.BM.Trace
3031import qualified Cardano.Db as DB
32+ import Cardano.DbSync.Api (getTrace )
33+ import Cardano.DbSync.Api.Types (InsertOptions (.. ), SyncEnv (.. ), SyncOptions (.. ))
3134import Cardano.DbSync.Cache.Epoch (rollbackMapEpochInCache )
3235import qualified Cardano.DbSync.Cache.LRU as LRU
3336import Cardano.DbSync.Cache.Types (Cache (.. ), CacheInternal (.. ), CacheNew (.. ), CacheStatistics (.. ), StakeAddrCache , initCacheStatistics )
@@ -36,6 +39,7 @@ import Cardano.DbSync.Era.Shelley.Query
3639import Cardano.DbSync.Era.Util
3740import Cardano.DbSync.Error
3841import Cardano.DbSync.Types
42+ import Cardano.DbSync.Util.Whitelist (shelleyInsertWhitelistCheck )
3943import qualified Cardano.Ledger.Address as Ledger
4044import Cardano.Ledger.BaseTypes (Network )
4145import Cardano.Ledger.Mary.Value
@@ -67,7 +71,7 @@ import Ouroboros.Consensus.Cardano.Block (StandardCrypto)
6771-- NOTE: BlockId is cleaned up on rollbacks, since it may get reinserted on
6872-- a different id.
6973-- 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 ()
7175rollbackCache UninitiatedCache _ = pure ()
7276rollbackCache (Cache cache) blockId = do
7377 liftIO $ do
@@ -83,46 +87,65 @@ getCacheStatistics cs =
8387
8488queryOrInsertRewardAccount ::
8589 (MonadBaseControl IO m , MonadIO m ) =>
90+ SyncEnv ->
8691 Cache ->
8792 CacheNew ->
8893 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
95109
96110queryOrInsertStakeAddress ::
97111 (MonadBaseControl IO m , MonadIO m ) =>
112+ SyncEnv ->
98113 Cache ->
99114 CacheNew ->
100115 Network ->
101116 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
105120
106121-- If the address already exists in the table, it will not be inserted again (due to
107122-- the uniqueness constraint) but the function will return the 'StakeAddressId'.
108123insertStakeAddress ::
109124 (MonadBaseControl IO m , MonadIO m ) =>
125+ SyncEnv ->
110126 Ledger. RewardAcnt StandardCrypto ->
111127 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
120142 where
121143 addrBs = fromMaybe (Ledger. serialiseRewardAcnt rewardAddr) stakeCredBs
144+ ioptsShelley = ioShelley . soptInsertOptions $ envOptions syncEnv
122145
123146queryRewardAccountWithCacheRetBs ::
124147 forall m .
125- MonadIO m =>
148+ ( MonadIO m ) =>
126149 Cache ->
127150 CacheNew ->
128151 Ledger. RewardAcnt StandardCrypto ->
@@ -132,7 +155,7 @@ queryRewardAccountWithCacheRetBs cache cacheNew rwdAcc =
132155
133156queryStakeAddrWithCache ::
134157 forall m .
135- MonadIO m =>
158+ ( MonadIO m ) =>
136159 Cache ->
137160 CacheNew ->
138161 Network ->
@@ -143,7 +166,7 @@ queryStakeAddrWithCache cache cacheNew nw cred =
143166
144167queryStakeAddrWithCacheRetBs ::
145168 forall m .
146- MonadIO m =>
169+ ( MonadIO m ) =>
147170 Cache ->
148171 CacheNew ->
149172 Network ->
@@ -161,7 +184,7 @@ queryStakeAddrWithCacheRetBs cache cacheNew nw cred = do
161184 pure mAddrId
162185
163186queryStakeAddrAux ::
164- MonadIO m =>
187+ ( MonadIO m ) =>
165188 CacheNew ->
166189 StakeAddrCache ->
167190 StrictTVar IO CacheStatistics ->
@@ -185,13 +208,13 @@ queryStakeAddrAux cacheNew mp sts nw cred =
185208 (err, _) -> pure (err, mp)
186209
187210queryPoolKeyWithCache ::
188- MonadIO m =>
189- Cache ->
211+ ( MonadIO m ) =>
212+ SyncEnv ->
190213 CacheNew ->
191214 PoolKeyHash ->
192215 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
195218 UninitiatedCache -> do
196219 mPhId <- queryPoolHashId (Generic. unKeyHashRaw hsh)
197220 case mPhId of
@@ -266,14 +289,14 @@ insertPoolKeyWithCache cache cacheNew pHash =
266289queryPoolKeyOrInsert ::
267290 (MonadBaseControl IO m , MonadIO m ) =>
268291 Text ->
269- Trace IO Text ->
292+ SyncEnv ->
270293 Cache ->
271294 CacheNew ->
272295 Bool ->
273296 PoolKeyHash ->
274297 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
277300 case pk of
278301 Right poolHashId -> pure poolHashId
279302 Left err -> do
@@ -290,9 +313,11 @@ queryPoolKeyOrInsert txt trce cache cacheNew logsWarning hsh = do
290313 , " . We will assume that the pool exists and move on."
291314 ]
292315 insertPoolKeyWithCache cache cacheNew hsh
316+ where
317+ trce = getTrace syncEnv
293318
294319queryMAWithCache ::
295- MonadIO m =>
320+ ( MonadIO m ) =>
296321 Cache ->
297322 PolicyID StandardCrypto ->
298323 AssetName ->
@@ -317,11 +342,14 @@ queryMAWithCache cache policyId asset =
317342 let ! assetNameBs = Generic. unAssetName asset
318343 maId <- maybe (Left (policyBs, assetNameBs)) Right <$> DB. queryMultiAssetId policyBs assetNameBs
319344 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)
321349 pure maId
322350
323351queryPrevBlockWithCache ::
324- MonadIO m =>
352+ ( MonadIO m ) =>
325353 Text ->
326354 Cache ->
327355 ByteString ->
@@ -342,7 +370,7 @@ queryPrevBlockWithCache msg cache hsh =
342370 Nothing -> queryFromDb ci
343371 where
344372 queryFromDb ::
345- MonadIO m =>
373+ ( MonadIO m ) =>
346374 CacheInternal ->
347375 ExceptT SyncNodeError (ReaderT SqlBackend m ) DB. BlockId
348376 queryFromDb ci = do
@@ -365,7 +393,7 @@ insertBlockAndCache cache block =
365393 pure bid
366394
367395queryDatum ::
368- MonadIO m =>
396+ ( MonadIO m ) =>
369397 Cache ->
370398 DataHash ->
371399 ReaderT SqlBackend m (Maybe DB. DatumId )
0 commit comments