@@ -16,34 +16,32 @@ module Cardano.DbSync.Cache (
16
16
queryPoolKeyOrInsert ,
17
17
queryPoolKeyWithCache ,
18
18
queryPrevBlockWithCache ,
19
- queryOrInsertStakeAddress ,
20
- queryOrInsertRewardAccount ,
21
19
insertAddressUsingCache ,
22
- insertStakeAddress ,
23
- queryStakeAddrWithCache ,
24
20
queryTxIdWithCache ,
25
21
rollbackCache ,
26
22
optimiseCaches ,
27
23
tryUpdateCacheTx ,
28
24
29
25
-- * CacheStatistics
30
26
getCacheStatistics ,
27
+ module X ,
31
28
) where
32
29
33
30
import Cardano.BM.Trace
34
31
import qualified Cardano.Db as DB
35
32
import qualified Cardano.Db.Schema.Variant.TxOut as V
33
+ import Cardano.DbSync.Api
34
+ import Cardano.DbSync.Api.Types
36
35
import Cardano.DbSync.Cache.Epoch (rollbackMapEpochInCache )
37
36
import qualified Cardano.DbSync.Cache.FIFO as FIFO
38
37
import qualified Cardano.DbSync.Cache.LRU as LRU
38
+ import Cardano.DbSync.Cache.Stake as X
39
39
import Cardano.DbSync.Cache.Types (CacheAction (.. ), CacheInternal (.. ), CacheStatistics (.. ), CacheStatus (.. ), StakeCache (.. ), initCacheStatistics , shouldCache )
40
+ import Cardano.DbSync.Cache.Util
40
41
import qualified Cardano.DbSync.Era.Shelley.Generic.Util as Generic
41
- import Cardano.DbSync.Era.Shelley.Query
42
42
import Cardano.DbSync.Era.Util
43
43
import Cardano.DbSync.Error
44
44
import Cardano.DbSync.Types
45
- import qualified Cardano.Ledger.Address as Ledger
46
- import Cardano.Ledger.BaseTypes (Network )
47
45
import Cardano.Ledger.Mary.Value
48
46
import qualified Cardano.Ledger.TxIn as Ledger
49
47
import Cardano.Prelude
@@ -109,113 +107,6 @@ getCacheStatistics cs =
109
107
NoCache -> pure initCacheStatistics
110
108
ActiveCache ci -> readTVarIO (cStats ci)
111
109
112
- queryOrInsertRewardAccount ::
113
- (MonadBaseControl IO m , MonadIO m ) =>
114
- Trace IO Text ->
115
- CacheStatus ->
116
- CacheAction ->
117
- Ledger. RewardAccount StandardCrypto ->
118
- ReaderT SqlBackend m DB. StakeAddressId
119
- queryOrInsertRewardAccount trce cache cacheUA rewardAddr = do
120
- eiAddrId <- queryStakeAddrWithCacheRetBs trce cache cacheUA rewardAddr
121
- case eiAddrId of
122
- Left (_err, bs) -> insertStakeAddress rewardAddr (Just bs)
123
- Right addrId -> pure addrId
124
-
125
- queryOrInsertStakeAddress ::
126
- (MonadBaseControl IO m , MonadIO m ) =>
127
- Trace IO Text ->
128
- CacheStatus ->
129
- CacheAction ->
130
- Network ->
131
- StakeCred ->
132
- ReaderT SqlBackend m DB. StakeAddressId
133
- queryOrInsertStakeAddress trce cache cacheUA nw cred =
134
- queryOrInsertRewardAccount trce cache cacheUA $ Ledger. RewardAccount nw cred
135
-
136
- -- If the address already exists in the table, it will not be inserted again (due to
137
- -- the uniqueness constraint) but the function will return the 'StakeAddressId'.
138
- insertStakeAddress ::
139
- (MonadBaseControl IO m , MonadIO m ) =>
140
- Ledger. RewardAccount StandardCrypto ->
141
- Maybe ByteString ->
142
- ReaderT SqlBackend m DB. StakeAddressId
143
- insertStakeAddress rewardAddr stakeCredBs = do
144
- DB. insertStakeAddress $
145
- DB. StakeAddress
146
- { DB. stakeAddressHashRaw = addrBs
147
- , DB. stakeAddressView = Generic. renderRewardAccount rewardAddr
148
- , DB. stakeAddressScriptHash = Generic. getCredentialScriptHash $ Ledger. raCredential rewardAddr
149
- }
150
- where
151
- addrBs = fromMaybe (Ledger. serialiseRewardAccount rewardAddr) stakeCredBs
152
-
153
- queryStakeAddrWithCache ::
154
- forall m .
155
- MonadIO m =>
156
- Trace IO Text ->
157
- CacheStatus ->
158
- CacheAction ->
159
- Network ->
160
- StakeCred ->
161
- ReaderT SqlBackend m (Either DB. LookupFail DB. StakeAddressId )
162
- queryStakeAddrWithCache trce cache cacheUA nw cred =
163
- mapLeft fst <$> queryStakeAddrWithCacheRetBs trce cache cacheUA (Ledger. RewardAccount nw cred)
164
-
165
- queryStakeAddrWithCacheRetBs ::
166
- forall m .
167
- MonadIO m =>
168
- Trace IO Text ->
169
- CacheStatus ->
170
- CacheAction ->
171
- Ledger. RewardAccount StandardCrypto ->
172
- ReaderT SqlBackend m (Either (DB. LookupFail , ByteString ) DB. StakeAddressId )
173
- queryStakeAddrWithCacheRetBs _trce cache cacheUA ra@ (Ledger. RewardAccount _ cred) = do
174
- let bs = Ledger. serialiseRewardAccount ra
175
- case cache of
176
- NoCache -> rsStkAdrrs bs
177
- ActiveCache ci -> do
178
- withCacheOptimisationCheck ci (rsStkAdrrs bs) $ do
179
- stakeCache <- liftIO $ readTVarIO (cStake ci)
180
- case queryStakeCache cred stakeCache of
181
- Just (addrId, stakeCache') -> do
182
- liftIO $ hitCreds (cStats ci)
183
- case cacheUA of
184
- EvictAndUpdateCache -> do
185
- liftIO $ atomically $ writeTVar (cStake ci) $ deleteStakeCache cred stakeCache'
186
- pure $ Right addrId
187
- _other -> do
188
- liftIO $ atomically $ writeTVar (cStake ci) stakeCache'
189
- pure $ Right addrId
190
- Nothing -> do
191
- queryRes <- mapLeft (,bs) <$> resolveStakeAddress bs
192
- liftIO $ missCreds (cStats ci)
193
- case queryRes of
194
- Left _ -> pure queryRes
195
- Right stakeAddrsId -> do
196
- let ! stakeCache' = case cacheUA of
197
- UpdateCache -> stakeCache {scLruCache = LRU. insert cred stakeAddrsId (scLruCache stakeCache)}
198
- UpdateCacheStrong -> stakeCache {scStableCache = Map. insert cred stakeAddrsId (scStableCache stakeCache)}
199
- _otherwise -> stakeCache
200
- liftIO $
201
- atomically $
202
- writeTVar (cStake ci) stakeCache'
203
- pure $ Right stakeAddrsId
204
- where
205
- rsStkAdrrs bs = mapLeft (,bs) <$> resolveStakeAddress bs
206
-
207
- -- | True if it was found in LRU
208
- queryStakeCache :: StakeCred -> StakeCache -> Maybe (DB. StakeAddressId , StakeCache )
209
- queryStakeCache scred scache = case Map. lookup scred (scStableCache scache) of
210
- Just addrId -> Just (addrId, scache)
211
- Nothing -> case LRU. lookup scred (scLruCache scache) of
212
- Just (addrId, lru') -> Just (addrId, scache {scLruCache = lru'})
213
- Nothing -> Nothing
214
-
215
- deleteStakeCache :: StakeCred -> StakeCache -> StakeCache
216
- deleteStakeCache scred scache =
217
- scache {scStableCache = Map. delete scred (scStableCache scache)}
218
-
219
110
queryPoolKeyWithCache ::
220
111
MonadIO m =>
221
112
CacheStatus ->
@@ -352,14 +243,13 @@ insertPoolKeyWithCache cache cacheUA pHash =
352
243
353
244
queryPoolKeyOrInsert ::
354
245
(MonadBaseControl IO m , MonadIO m ) =>
246
+ SyncEnv ->
355
247
Text ->
356
- Trace IO Text ->
357
- CacheStatus ->
358
248
CacheAction ->
359
249
Bool ->
360
250
PoolKeyHash ->
361
251
ReaderT SqlBackend m DB. PoolHashId
362
- queryPoolKeyOrInsert txt trce cache cacheUA logsWarning hsh = do
252
+ queryPoolKeyOrInsert syncEnv txt cacheUA logsWarning hsh = do
363
253
pk <- queryPoolKeyWithCache cache cacheUA hsh
364
254
case pk of
365
255
Right poolHashId -> pure poolHashId
@@ -377,6 +267,9 @@ queryPoolKeyOrInsert txt trce cache cacheUA logsWarning hsh = do
377
267
, " . We will assume that the pool exists and move on."
378
268
]
379
269
insertPoolKeyWithCache cache cacheUA hsh
270
+ where
271
+ trce = getTrace syncEnv
272
+ cache = envCache syncEnv
380
273
381
274
queryMAWithCache ::
382
275
MonadIO m =>
@@ -544,27 +437,6 @@ insertDatumAndCache cache hsh dt = do
544
437
LRU. insert hsh datumId
545
438
pure datumId
546
439
547
- withCacheOptimisationCheck ::
548
- MonadIO m =>
549
- CacheInternal ->
550
- m a -> -- Action to perform if cache is optimised
551
- m a -> -- Action to perform if cache is not optimised
552
- m a
553
- withCacheOptimisationCheck ci ifOptimised ifNotOptimised = do
554
- isCachedOptimised <- liftIO $ readTVarIO (cIsCacheOptimised ci)
555
- if isCachedOptimised
556
- then ifOptimised
557
- else ifNotOptimised
558
-
559
- -- Stakes
560
- hitCreds :: StrictTVar IO CacheStatistics -> IO ()
561
- hitCreds ref =
562
- atomically $ modifyTVar ref (\ cs -> cs {credsHits = 1 + credsHits cs, credsQueries = 1 + credsQueries cs})
563
-
564
- missCreds :: StrictTVar IO CacheStatistics -> IO ()
565
- missCreds ref =
566
- atomically $ modifyTVar ref (\ cs -> cs {credsQueries = 1 + credsQueries cs})
567
-
568
440
-- Pools
569
441
hitPools :: StrictTVar IO CacheStatistics -> IO ()
570
442
hitPools ref =
0 commit comments