@@ -30,7 +30,7 @@ import Cardano.BM.Trace
30
30
import qualified Cardano.Db as DB
31
31
import Cardano.DbSync.Cache.Epoch (rollbackMapEpochInCache )
32
32
import qualified Cardano.DbSync.Cache.LRU as LRU
33
- import Cardano.DbSync.Cache.Types (CacheInternal (.. ), CacheStatistics (.. ), CacheStatus (.. ), CacheUpdateAction (.. ), StakeAddrCache , initCacheStatistics )
33
+ import Cardano.DbSync.Cache.Types (CacheAction (.. ), CacheInternal (.. ), CacheStatistics (.. ), CacheStatus (.. ), initCacheStatistics , isCacheActionUpdate )
34
34
import qualified Cardano.DbSync.Era.Shelley.Generic.Util as Generic
35
35
import Cardano.DbSync.Era.Shelley.Query
36
36
import Cardano.DbSync.Era.Util
@@ -85,47 +85,40 @@ queryOrInsertRewardAccount ::
85
85
(MonadBaseControl IO m , MonadIO m ) =>
86
86
Trace IO Text ->
87
87
CacheStatus ->
88
- CacheUpdateAction ->
88
+ CacheAction ->
89
89
Ledger. RewardAccount StandardCrypto ->
90
90
ReaderT SqlBackend m DB. StakeAddressId
91
- queryOrInsertRewardAccount trce cacheStatus cacheUA rewardAddr = do
92
- eiAddrId <- queryRewardAccountWithCacheRetBs trce cacheStatus rewardAddr
91
+ queryOrInsertRewardAccount trce cache cacheUA rewardAddr = do
92
+ eiAddrId <- queryRewardAccountWithCacheRetBs trce cache cacheUA rewardAddr
93
93
case eiAddrId of
94
- Left (_err, bs) -> insertStakeAddress cacheStatus rewardAddr (Just bs)
94
+ Left (_err, bs) -> insertStakeAddress rewardAddr (Just bs)
95
95
Right addrId -> pure addrId
96
96
97
97
queryOrInsertStakeAddress ::
98
98
(MonadBaseControl IO m , MonadIO m ) =>
99
99
Trace IO Text ->
100
100
CacheStatus ->
101
- CacheUpdateAction ->
101
+ CacheAction ->
102
102
Network ->
103
103
StakeCred ->
104
104
ReaderT SqlBackend m DB. StakeAddressId
105
- queryOrInsertStakeAddress trce cacheStatus cacheUA nw cred =
106
- queryOrInsertRewardAccount trce cacheStatus cacheUA $ Ledger. RewardAccount nw cred
105
+ queryOrInsertStakeAddress trce cache cacheUA nw cred =
106
+ queryOrInsertRewardAccount trce cache cacheUA $ Ledger. RewardAccount nw cred
107
107
108
108
-- If the address already exists in the table, it will not be inserted again (due to
109
109
-- the uniqueness constraint) but the function will return the 'StakeAddressId'.
110
110
insertStakeAddress ::
111
111
(MonadBaseControl IO m , MonadIO m ) =>
112
- CacheStatus ->
113
112
Ledger. RewardAccount StandardCrypto ->
114
113
Maybe ByteString ->
115
114
ReaderT SqlBackend m DB. StakeAddressId
116
- insertStakeAddress cacheStatus rewardAddr stakeCredBs = do
117
- addrId <- DB. insertStakeAddress $
118
- DB. StakeAddress
119
- { DB. stakeAddressHashRaw = addrBs
120
- , DB. stakeAddressView = Generic. renderRewardAccount rewardAddr
121
- , DB. stakeAddressScriptHash = Generic. getCredentialScriptHash $ Ledger. raCredential rewardAddr
122
- }
123
- case cacheStatus of
124
- NoCache -> pure addrId
125
- CacheActive ci -> do
126
- liftIO $ atomically $ modifyTVar (cStakeRawHashes ci) $
127
- LRU. insert addrBs addrId
128
- pure addrId
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
+ }
129
122
where
130
123
addrBs = fromMaybe (Ledger. serialiseRewardAccount rewardAddr) stakeCredBs
131
124
@@ -134,97 +127,83 @@ queryRewardAccountWithCacheRetBs ::
134
127
MonadIO m =>
135
128
Trace IO Text ->
136
129
CacheStatus ->
137
- CacheUpdateAction ->
130
+ CacheAction ->
138
131
Ledger. RewardAccount StandardCrypto ->
139
132
ReaderT SqlBackend m (Either (DB. LookupFail , ByteString ) DB. StakeAddressId )
140
- queryRewardAccountWithCacheRetBs trce cacheStatus cacheUA rwdAcc =
141
- queryStakeAddrWithCacheRetBs trce cacheStatus cacheUA (Ledger. raNetwork rwdAcc) (Ledger. raCredential rwdAcc)
133
+ queryRewardAccountWithCacheRetBs trce cache cacheUA rwdAcc =
134
+ queryStakeAddrWithCacheRetBs trce cache cacheUA (Ledger. raNetwork rwdAcc) (Ledger. raCredential rwdAcc)
142
135
143
136
queryStakeAddrWithCache ::
144
137
forall m .
145
138
MonadIO m =>
146
139
Trace IO Text ->
147
140
CacheStatus ->
148
- CacheUpdateAction ->
141
+ CacheAction ->
149
142
Network ->
150
143
StakeCred ->
151
144
ReaderT SqlBackend m (Either DB. LookupFail DB. StakeAddressId )
152
- queryStakeAddrWithCache trce cacheStatus cacheUA nw cred =
153
- mapLeft fst <$> queryStakeAddrWithCacheRetBs trce cacheStatus cacheUA nw cred
145
+ queryStakeAddrWithCache trce cache cacheUA nw cred =
146
+ mapLeft fst <$> queryStakeAddrWithCacheRetBs trce cache cacheUA nw cred
154
147
155
148
queryStakeAddrWithCacheRetBs ::
156
149
forall m .
157
150
MonadIO m =>
158
151
Trace IO Text ->
159
152
CacheStatus ->
160
- CacheUpdateAction ->
153
+ CacheAction ->
161
154
Network ->
162
155
StakeCred ->
163
156
ReaderT SqlBackend m (Either (DB. LookupFail , ByteString ) DB. StakeAddressId )
164
- queryStakeAddrWithCacheRetBs trce cacheStatus cacheUA nw cred = do
157
+ queryStakeAddrWithCacheRetBs trce cache cacheUA nw cred = do
165
158
let ! bs = Ledger. serialiseRewardAccount (Ledger. RewardAccount nw cred)
166
- case cacheStatus of
159
+ case cache of
167
160
NoCache -> do
168
161
mapLeft (,bs) <$> queryStakeAddress bs
169
- CacheActive ci -> do
170
- currentCache <- liftIO $ readTVarIO (cStakeRawHashes ci)
171
- let cacheSize = LRU. getSize currentCache
172
- newCache <-
173
- if cacheSize < 1
174
- then do
175
- liftIO $ logInfo trce " ----------------- Cache is empty. Querying all addresses. ---------"
176
- queryRes <- DB. queryLatestAddresses cacheSize
177
- pure $ LRU. fromList queryRes currentCache
178
- -- convert the results into the cache
179
- else pure currentCache
180
- case LRU. lookup bs newCache of
181
- Just (addrId, mp') -> do
162
+ ActiveCache ci -> do
163
+ prevCache <- liftIO $ readTVarIO (cStakeRawHashes ci)
164
+ let isNewCache = LRU. getSize prevCache < 1
165
+ -- populate from db if the cache is empty
166
+ currentCache <-
167
+ if isNewCache
168
+ then do
169
+ liftIO $ logInfo trce " Stake Raw Hashes cache is new and empty. Populating with addresses from db..."
170
+ queryRes <- DB. queryAddressWithReward (fromIntegral $ LRU. getCapacity prevCache)
171
+ liftIO $ atomically $ writeTVar (cStakeRawHashes ci) $ LRU. fromList queryRes prevCache
172
+ liftIO $ logInfo trce " Population of cache complete."
173
+ liftIO $ readTVarIO (cStakeRawHashes ci)
174
+ else pure prevCache
175
+
176
+ case LRU. lookup bs currentCache of
177
+ Just (addrId, lruCache) -> do
182
178
liftIO $ hitCreds (cStats ci)
183
- liftIO $ atomically $ writeTVar (cStakeRawHashes ci) mp'
184
- pure $ Right addrId
179
+ case cacheUA of
180
+ EvictAndUpdateCache -> do
181
+ liftIO $ atomically $ writeTVar (cStakeRawHashes ci) $ LRU. delete bs lruCache
182
+ pure $ Right addrId
183
+ _other -> do
184
+ liftIO $ atomically $ writeTVar (cStakeRawHashes ci) lruCache
185
+ pure $ Right addrId
185
186
Nothing -> do
186
- liftIO $ missCreds (cStats ci)
187
- liftIO $ atomically $ writeTVar (cStakeRawHashes ci) newCache
188
187
queryRes <- mapLeft (,bs) <$> queryStakeAddress bs
188
+ liftIO $ missCreds (cStats ci)
189
189
case queryRes of
190
190
Left _ -> pure queryRes
191
191
Right stakeAddrsId -> do
192
- liftIO $ atomically $ modifyTVar (cStakeRawHashes ci) $
193
- LRU. insert bs stakeAddrsId
192
+ when (isCacheActionUpdate cacheUA) $
193
+ liftIO $
194
+ atomically $
195
+ modifyTVar (cStakeRawHashes ci) $
196
+ LRU. insert bs stakeAddrsId
194
197
pure $ Right stakeAddrsId
195
198
196
- -- queryStakeAddrAux ::
197
- -- MonadIO m =>
198
- -- CacheNew ->
199
- -- StakeAddrCache ->
200
- -- StrictTVar IO CacheStatistics ->
201
- -- Network ->
202
- -- StakeCred ->
203
- -- ReaderT SqlBackend m (Either (DB.LookupFail, ByteString) DB.StakeAddressId, StakeAddrCache)
204
- -- queryStakeAddrAux cacheNew mp sts nw cred =
205
- -- case Map.lookup cred mp of
206
- -- Just addrId -> do
207
- -- liftIO $ hitCreds sts
208
- -- case cacheNew of
209
- -- EvictAndReturn -> pure (Right addrId, Map.delete cred mp)
210
- -- _ -> pure (Right addrId, mp)
211
- -- Nothing -> do
212
- -- liftIO $ missCreds sts
213
- -- let !bs = Ledger.serialiseRewardAccount (Ledger.RewardAccount nw cred)
214
- -- mAddrId <- mapLeft (,bs) <$> queryStakeAddress bs
215
- -- case (mAddrId, cacheNew) of
216
- -- (Right addrId, CacheNew) -> pure (Right addrId, Map.insert cred addrId mp)
217
- -- (Right addrId, _) -> pure (Right addrId, mp)
218
- -- (err, _) -> pure (err, mp)
219
-
220
199
queryPoolKeyWithCache ::
221
200
MonadIO m =>
222
201
CacheStatus ->
223
- CacheUpdateAction ->
202
+ CacheAction ->
224
203
PoolKeyHash ->
225
204
ReaderT SqlBackend m (Either DB. LookupFail DB. PoolHashId )
226
- queryPoolKeyWithCache cacheStatus cacheUA hsh =
227
- case cacheStatus of
205
+ queryPoolKeyWithCache cache cacheUA hsh =
206
+ case cache of
228
207
NoCache -> do
229
208
mPhId <- queryPoolHashId (Generic. unKeyHashRaw hsh)
230
209
case mPhId of
@@ -259,11 +238,11 @@ queryPoolKeyWithCache cacheStatus cacheUA hsh =
259
238
insertPoolKeyWithCache ::
260
239
(MonadBaseControl IO m , MonadIO m ) =>
261
240
CacheStatus ->
262
- CacheUpdateAction ->
241
+ CacheAction ->
263
242
PoolKeyHash ->
264
243
ReaderT SqlBackend m DB. PoolHashId
265
- insertPoolKeyWithCache cacheStatus cacheUA pHash =
266
- case cacheStatus of
244
+ insertPoolKeyWithCache cache cacheUA pHash =
245
+ case cache of
267
246
NoCache ->
268
247
DB. insertPoolHash $
269
248
DB. PoolHash
@@ -301,12 +280,12 @@ queryPoolKeyOrInsert ::
301
280
Text ->
302
281
Trace IO Text ->
303
282
CacheStatus ->
304
- CacheUpdateAction ->
283
+ CacheAction ->
305
284
Bool ->
306
285
PoolKeyHash ->
307
286
ReaderT SqlBackend m DB. PoolHashId
308
- queryPoolKeyOrInsert txt trce cacheStatus cacheUA logsWarning hsh = do
309
- pk <- queryPoolKeyWithCache cacheStatus cacheUA hsh
287
+ queryPoolKeyOrInsert txt trce cache cacheUA logsWarning hsh = do
288
+ pk <- queryPoolKeyWithCache cache cacheUA hsh
310
289
case pk of
311
290
Right poolHashId -> pure poolHashId
312
291
Left err -> do
@@ -322,16 +301,16 @@ queryPoolKeyOrInsert txt trce cacheStatus cacheUA logsWarning hsh = do
322
301
, txt
323
302
, " . We will assume that the pool exists and move on."
324
303
]
325
- insertPoolKeyWithCache cacheStatus cacheUA hsh
304
+ insertPoolKeyWithCache cache cacheUA hsh
326
305
327
306
queryMAWithCache ::
328
307
MonadIO m =>
329
308
CacheStatus ->
330
309
PolicyID StandardCrypto ->
331
310
AssetName ->
332
311
ReaderT SqlBackend m (Either (ByteString , ByteString ) DB. MultiAssetId )
333
- queryMAWithCache cacheStatus policyId asset =
334
- case cacheStatus of
312
+ queryMAWithCache cache policyId asset =
313
+ case cache of
335
314
NoCache -> do
336
315
let ! policyBs = Generic. unScriptHash $ policyID policyId
337
316
let ! assetNameBs = Generic. unAssetName asset
@@ -359,8 +338,8 @@ queryPrevBlockWithCache ::
359
338
CacheStatus ->
360
339
ByteString ->
361
340
ExceptT SyncNodeError (ReaderT SqlBackend m ) DB. BlockId
362
- queryPrevBlockWithCache msg cacheStatus hsh =
363
- case cacheStatus of
341
+ queryPrevBlockWithCache msg cache hsh =
342
+ case cache of
364
343
NoCache -> liftLookupFail msg $ DB. queryBlockId hsh
365
344
ActiveCache ci -> do
366
345
mCachedPrev <- liftIO $ readTVarIO (cPrevBlock ci)
@@ -387,8 +366,8 @@ insertBlockAndCache ::
387
366
CacheStatus ->
388
367
DB. Block ->
389
368
ReaderT SqlBackend m DB. BlockId
390
- insertBlockAndCache cacheStatus block =
391
- case cacheStatus of
369
+ insertBlockAndCache cache block =
370
+ case cache of
392
371
NoCache -> DB. insertBlock block
393
372
ActiveCache ci -> do
394
373
bid <- DB. insertBlock block
@@ -402,10 +381,10 @@ queryDatum ::
402
381
CacheStatus ->
403
382
DataHash ->
404
383
ReaderT SqlBackend m (Maybe DB. DatumId )
405
- queryDatum cacheStatus hsh = do
406
- case cacheStatus of
384
+ queryDatum cache hsh = do
385
+ case cache of
407
386
NoCache -> DB. queryDatum $ Generic. dataHashToBytes hsh
408
- CacheActive ci -> do
387
+ ActiveCache ci -> do
409
388
mp <- liftIO $ readTVarIO (cDatum ci)
410
389
case LRU. lookup hsh mp of
411
390
Just (datumId, mp') -> do
@@ -424,9 +403,9 @@ insertDatumAndCache ::
424
403
DataHash ->
425
404
DB. Datum ->
426
405
ReaderT SqlBackend m DB. DatumId
427
- insertDatumAndCache cacheStatus hsh dt = do
406
+ insertDatumAndCache cache hsh dt = do
428
407
datumId <- DB. insertDatum dt
429
- case cacheStatus of
408
+ case cache of
430
409
NoCache -> pure datumId
431
410
ActiveCache ci -> do
432
411
liftIO $
0 commit comments