@@ -81,22 +81,24 @@ rollbackCache (ActiveCache _ cache) blockId = do
81
81
atomically $ modifyTVar (cTxIds cache) FIFO. cleanupCache
82
82
void $ rollbackMapEpochInCache cache blockId
83
83
84
- -- When syncing and we're close to the tip, we can optimise the caches.
84
+ -- When syncing and we get within 2 minutes of the tip, we can optimise the caches
85
+ -- and set the flag to True on ActiveCache.
85
86
optimiseCaches :: MonadIO m => CacheStatus -> ReaderT SqlBackend m CacheStatus
86
- optimiseCaches c@ (ActiveCache isCacheOptomised cache) = do
87
- if isCacheOptomised
88
- then liftIO $ do
89
- -- empty caches not to be used anymore
90
- atomically $ modifyTVar (cTxIds cache) FIFO. cleanupCache
91
- atomically $ writeTVar (cStake cache) (StakeCache Map. empty (LRU. empty 0 ))
92
- atomically $ modifyTVar (cDatum cache) (LRU. optimise 0 )
93
- -- empty then limit the capacity of the cache
94
- atomically $ writeTVar (cMultiAssets cache) (LRU. empty 50000 )
95
- -- leaving the following caches as they are:
96
- -- cPools, cPrevBlock, Cstats, cEpoch
97
- pure c
98
- else pure c
99
- optimiseCaches c = pure c
87
+ optimiseCaches cache =
88
+ case cache of
89
+ NoCache -> pure cache
90
+ ActiveCache True _ -> pure cache
91
+ ActiveCache False c -> do
92
+ liftIO $ do
93
+ -- empty caches not to be used anymore
94
+ atomically $ modifyTVar (cTxIds c) FIFO. cleanupCache
95
+ atomically $ writeTVar (cStake c) (StakeCache Map. empty (LRU. empty 0 ))
96
+ atomically $ modifyTVar (cDatum c) (LRU. optimise 0 )
97
+ -- empty then limit the capacity of the cache
98
+ atomically $ writeTVar (cMultiAssets c) (LRU. empty 50000 )
99
+ -- leaving the following caches as they are:
100
+ -- cPools, cPrevBlock, Cstats, cEpoch
101
+ pure $ ActiveCache True c
100
102
101
103
getCacheStatistics :: CacheStatus -> IO CacheStatistics
102
104
getCacheStatistics cs =
@@ -169,35 +171,33 @@ queryStakeAddrWithCacheRetBs _trce cache cacheUA ra@(Ledger.RewardAccount _ cred
169
171
let bs = Ledger. serialiseRewardAccount ra
170
172
case cache of
171
173
NoCache -> mapLeft (,bs) <$> resolveStakeAddress bs
172
- ActiveCache shouldOptomiseCache ci -> do
173
- if shouldOptomiseCache
174
- then mapLeft (,bs) <$> resolveStakeAddress bs
175
- else do
176
- stakeCache <- liftIO $ readTVarIO (cStake ci)
177
- case queryStakeCache cred stakeCache of
178
- Just (addrId, stakeCache') -> do
179
- liftIO $ hitCreds (cStats ci)
180
- case cacheUA of
181
- EvictAndUpdateCache -> do
182
- liftIO $ atomically $ writeTVar (cStake ci) $ deleteStakeCache cred stakeCache'
183
- pure $ Right addrId
184
- _other -> do
185
- liftIO $ atomically $ writeTVar (cStake ci) stakeCache'
186
- pure $ Right addrId
187
- Nothing -> do
188
- queryRes <- mapLeft (,bs) <$> resolveStakeAddress bs
189
- liftIO $ missCreds (cStats ci)
190
- case queryRes of
191
- Left _ -> pure queryRes
192
- Right stakeAddrsId -> do
193
- let ! stakeCache' = case cacheUA of
194
- UpdateCache -> stakeCache {scLruCache = LRU. insert cred stakeAddrsId (scLruCache stakeCache)}
195
- UpdateCacheStrong -> stakeCache {scStableCache = Map. insert cred stakeAddrsId (scStableCache stakeCache)}
196
- _ -> stakeCache
197
- liftIO $
198
- atomically $
199
- writeTVar (cStake ci) stakeCache'
200
- pure $ Right stakeAddrsId
174
+ ActiveCache True _ -> mapLeft (,bs) <$> resolveStakeAddress bs
175
+ ActiveCache False ci -> do
176
+ stakeCache <- liftIO $ readTVarIO (cStake ci)
177
+ case queryStakeCache cred stakeCache of
178
+ Just (addrId, stakeCache') -> do
179
+ liftIO $ hitCreds (cStats ci)
180
+ case cacheUA of
181
+ EvictAndUpdateCache -> do
182
+ liftIO $ atomically $ writeTVar (cStake ci) $ deleteStakeCache cred stakeCache'
183
+ pure $ Right addrId
184
+ _other -> do
185
+ liftIO $ atomically $ writeTVar (cStake ci) stakeCache'
186
+ pure $ Right addrId
187
+ Nothing -> do
188
+ queryRes <- mapLeft (,bs) <$> resolveStakeAddress bs
189
+ liftIO $ missCreds (cStats ci)
190
+ case queryRes of
191
+ Left _ -> pure queryRes
192
+ Right stakeAddrsId -> do
193
+ let ! stakeCache' = case cacheUA of
194
+ UpdateCache -> stakeCache {scLruCache = LRU. insert cred stakeAddrsId (scLruCache stakeCache)}
195
+ UpdateCacheStrong -> stakeCache {scStableCache = Map. insert cred stakeAddrsId (scStableCache stakeCache)}
196
+ _ -> stakeCache
197
+ liftIO $
198
+ atomically $
199
+ writeTVar (cStake ci) stakeCache'
200
+ pure $ Right stakeAddrsId
201
201
202
202
-- | True if it was found in LRU
203
203
queryStakeCache :: StakeCred -> StakeCache -> Maybe (DB. StakeAddressId , StakeCache )
@@ -327,25 +327,23 @@ queryMAWithCache ::
327
327
queryMAWithCache cache policyId asset =
328
328
case cache of
329
329
NoCache -> queryDb
330
- ActiveCache isCacheOptomised ci -> do
331
- if isCacheOptomised
332
- then queryDb
333
- else do
334
- mp <- liftIO $ readTVarIO (cMultiAssets ci)
335
- case LRU. lookup (policyId, asset) mp of
336
- Just (maId, mp') -> do
337
- liftIO $ hitMAssets (cStats ci)
338
- liftIO $ atomically $ writeTVar (cMultiAssets ci) mp'
339
- pure $ Right maId
340
- Nothing -> do
341
- liftIO $ missMAssets (cStats ci)
342
- -- miss. The lookup doesn't change the cache on a miss.
343
- let ! policyBs = Generic. unScriptHash $ policyID policyId
344
- let ! assetNameBs = Generic. unAssetName asset
345
- maId <- maybe (Left (policyBs, assetNameBs)) Right <$> DB. queryMultiAssetId policyBs assetNameBs
346
- whenRight maId $
347
- liftIO . atomically . modifyTVar (cMultiAssets ci) . LRU. insert (policyId, asset)
348
- pure maId
330
+ ActiveCache True _ -> queryDb
331
+ ActiveCache False ci -> do
332
+ mp <- liftIO $ readTVarIO (cMultiAssets ci)
333
+ case LRU. lookup (policyId, asset) mp of
334
+ Just (maId, mp') -> do
335
+ liftIO $ hitMAssets (cStats ci)
336
+ liftIO $ atomically $ writeTVar (cMultiAssets ci) mp'
337
+ pure $ Right maId
338
+ Nothing -> do
339
+ liftIO $ missMAssets (cStats ci)
340
+ -- miss. The lookup doesn't change the cache on a miss.
341
+ let ! policyBs = Generic. unScriptHash $ policyID policyId
342
+ let ! assetNameBs = Generic. unAssetName asset
343
+ maId <- maybe (Left (policyBs, assetNameBs)) Right <$> DB. queryMultiAssetId policyBs assetNameBs
344
+ whenRight maId $
345
+ liftIO . atomically . modifyTVar (cMultiAssets ci) . LRU. insert (policyId, asset)
346
+ pure maId
349
347
where
350
348
queryDb = do
351
349
let ! policyBs = Generic. unScriptHash $ policyID policyId
@@ -389,47 +387,42 @@ queryTxIdWithCache ::
389
387
queryTxIdWithCache cache txIdLedger = do
390
388
case cache of
391
389
-- Direct database query if no cache.
392
- NoCache -> DB. queryTxId txHash
393
- ActiveCache isCacheOptomised cacheInternal -> do
394
- if isCacheOptomised
395
- then DB. queryTxId txHash
396
- else do
397
- -- Read current cache state.
398
- cacheTx <- liftIO $ readTVarIO (cTxIds cacheInternal)
399
-
400
- case FIFO. lookup txIdLedger cacheTx of
401
- -- Cache hit, return the transaction ID.
402
- Just txId -> do
403
- liftIO $ hitTxIds (cStats cacheInternal)
390
+ NoCache -> qTxHash
391
+ ActiveCache True _ -> qTxHash
392
+ ActiveCache False cacheInternal -> do
393
+ -- Read current cache state.
394
+ cacheTx <- liftIO $ readTVarIO (cTxIds cacheInternal)
395
+
396
+ case FIFO. lookup txIdLedger cacheTx of
397
+ -- Cache hit, return the transaction ID.
398
+ Just txId -> do
399
+ liftIO $ hitTxIds (cStats cacheInternal)
400
+ pure $ Right txId
401
+ -- Cache miss.
402
+ Nothing -> do
403
+ eTxId <- qTxHash
404
+ liftIO $ missTxIds (cStats cacheInternal)
405
+ case eTxId of
406
+ Right txId -> do
407
+ -- Update cache.
408
+ liftIO $ atomically $ modifyTVar (cTxIds cacheInternal) $ FIFO. insert txIdLedger txId
409
+ -- Return ID after updating cache.
404
410
pure $ Right txId
405
- -- Cache miss.
406
- Nothing -> do
407
- eTxId <- DB. queryTxId txHash
408
- liftIO $ missTxIds (cStats cacheInternal)
409
- case eTxId of
410
- Right txId -> do
411
- -- Update cache.
412
- liftIO $ atomically $ modifyTVar (cTxIds cacheInternal) $ FIFO. insert txIdLedger txId
413
- -- Return ID after updating cache.
414
- pure $ Right txId
415
- -- Return lookup failure.
416
- Left _ -> pure $ Left $ DB. DbLookupTxHash txHash
411
+ -- Return lookup failure.
412
+ Left _ -> pure $ Left $ DB. DbLookupTxHash txHash
417
413
where
418
414
txHash = Generic. unTxHash txIdLedger
415
+ qTxHash = DB. queryTxId txHash
419
416
420
417
tryUpdateCacheTx ::
421
418
MonadIO m =>
422
419
CacheStatus ->
423
420
Ledger. TxId StandardCrypto ->
424
421
DB. TxId ->
425
422
m ()
426
- tryUpdateCacheTx cache ledgerTxId txId = do
427
- case cache of
428
- NoCache -> pure ()
429
- ActiveCache isCacheOptomised ci -> do
430
- if isCacheOptomised
431
- then pure ()
432
- else liftIO $ atomically $ modifyTVar (cTxIds ci) $ FIFO. insert ledgerTxId txId
423
+ tryUpdateCacheTx (ActiveCache False ci) ledgerTxId txId =
424
+ liftIO $ atomically $ modifyTVar (cTxIds ci) $ FIFO. insert ledgerTxId txId
425
+ tryUpdateCacheTx _ _ _ = pure ()
433
426
434
427
insertBlockAndCache ::
435
428
(MonadIO m , MonadBaseControl IO m ) =>
@@ -438,16 +431,16 @@ insertBlockAndCache ::
438
431
ReaderT SqlBackend m DB. BlockId
439
432
insertBlockAndCache cache block =
440
433
case cache of
441
- NoCache -> DB. insertBlock block
442
- ActiveCache isCacheOptomised ci ->
443
- if isCacheOptomised
444
- then DB. insertBlock block
445
- else do
446
- bid <- DB. insertBlock block
447
- liftIO $ do
448
- missPrevBlock (cStats ci)
449
- atomically $ writeTVar (cPrevBlock ci) $ Just (bid, DB. blockHash block)
450
- pure bid
434
+ NoCache -> insBlck
435
+ ActiveCache True _ -> insBlck
436
+ ActiveCache False ci -> do
437
+ bid <- insBlck
438
+ liftIO $ do
439
+ missPrevBlock (cStats ci)
440
+ atomically $ writeTVar (cPrevBlock ci) $ Just (bid, DB. blockHash block)
441
+ pure bid
442
+ where
443
+ insBlck = DB. insertBlock block
451
444
452
445
queryDatum ::
453
446
MonadIO m =>
@@ -456,21 +449,21 @@ queryDatum ::
456
449
ReaderT SqlBackend m (Maybe DB. DatumId )
457
450
queryDatum cache hsh = do
458
451
case cache of
459
- NoCache -> DB. queryDatum $ Generic. dataHashToBytes hsh
460
- ActiveCache isCacheOptomised ci -> do
461
- if isCacheOptomised
462
- then DB. queryDatum $ Generic. dataHashToBytes hsh
463
- else do
464
- mp <- liftIO $ readTVarIO (cDatum ci)
465
- case LRU. lookup hsh mp of
466
- Just (datumId, mp') -> do
467
- liftIO $ hitDatum (cStats ci)
468
- liftIO $ atomically $ writeTVar (cDatum ci) mp'
469
- pure $ Just datumId
470
- Nothing -> do
471
- liftIO $ missDatum (cStats ci)
472
- -- miss. The lookup doesn't change the cache on a miss.
473
- DB. queryDatum $ Generic. dataHashToBytes hsh
452
+ NoCache -> queryDtm
453
+ ActiveCache True _ -> queryDtm
454
+ ActiveCache False ci -> do
455
+ mp <- liftIO $ readTVarIO (cDatum ci)
456
+ case LRU. lookup hsh mp of
457
+ Just (datumId, mp') -> do
458
+ liftIO $ hitDatum (cStats ci)
459
+ liftIO $ atomically $ writeTVar (cDatum ci) mp'
460
+ pure $ Just datumId
461
+ Nothing -> do
462
+ liftIO $ missDatum (cStats ci)
463
+ -- miss. The lookup doesn't change the cache on a miss.
464
+ queryDtm
465
+ where
466
+ queryDtm = DB. queryDatum $ Generic. dataHashToBytes hsh
474
467
475
468
-- This assumes the entry is not cached.
476
469
insertDatumAndCache ::
@@ -483,15 +476,13 @@ insertDatumAndCache cache hsh dt = do
483
476
datumId <- DB. insertDatum dt
484
477
case cache of
485
478
NoCache -> pure datumId
486
- ActiveCache isCacheOptomised ci -> do
487
- if isCacheOptomised
488
- then pure datumId
489
- else do
490
- liftIO $
491
- atomically $
492
- modifyTVar (cDatum ci) $
493
- LRU. insert hsh datumId
494
- pure datumId
479
+ ActiveCache True _ -> pure datumId
480
+ ActiveCache False ci -> do
481
+ liftIO $
482
+ atomically $
483
+ modifyTVar (cDatum ci) $
484
+ LRU. insert hsh datumId
485
+ pure datumId
495
486
496
487
-- Stakes
497
488
hitCreds :: StrictTVar IO CacheStatistics -> IO ()
0 commit comments