@@ -12,6 +12,7 @@ import (
12
12
"github.com/oasisprotocol/oasis-core/go/roothash/api/block"
13
13
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/client"
14
14
15
+ "github.com/oasisprotocol/emerald-web3-gateway/conf"
15
16
"github.com/oasisprotocol/emerald-web3-gateway/db/model"
16
17
"github.com/oasisprotocol/emerald-web3-gateway/storage"
17
18
)
@@ -378,22 +379,37 @@ func (cb *cachingBackend) onRejectOrEvictBlock(
378
379
func newCachingBackend (
379
380
ctx context.Context ,
380
381
backend Backend ,
382
+ cfg * conf.CacheConfig ,
381
383
) (Backend , error ) {
382
384
const (
383
- blockCacheSize = 1024 // In blocks.
384
- txCacheSize = 1024 * 1024 * 1024 // In bytes.
385
- receiptCacheSize = txCacheSize // In bytes.
386
- bufferItems = 64 // Per documentation
385
+ defaultBlockCacheSize = 1024 // In blocks
386
+ defaultTxCacheSize = 1024 * 1024 * 1024 // In bytes
387
+ defaultReceiptCacheSize = defaultTxCacheSize // In bytes
388
+
389
+ bufferItems = 64 // Per documentation
387
390
)
388
391
392
+ if cfg == nil {
393
+ cfg = new (conf.CacheConfig )
394
+ }
395
+ if cfg .BlockSize == 0 {
396
+ cfg .BlockSize = defaultBlockCacheSize
397
+ }
398
+ if cfg .TxSize == 0 {
399
+ cfg .TxSize = defaultTxCacheSize
400
+ }
401
+ if cfg .TxReceiptSize == 0 {
402
+ cfg .TxReceiptSize = defaultReceiptCacheSize
403
+ }
404
+
389
405
cb := & cachingBackend {
390
406
inner : backend ,
391
407
}
392
408
393
409
// Block cache (by block number aka round), accounting in blocks.
394
410
blockByNumber , err := ristretto .NewCache (& ristretto.Config {
395
- NumCounters : blockCacheSize * 10 , // 10x MaxCost
396
- MaxCost : blockCacheSize ,
411
+ NumCounters : int64 ( cfg . BlockSize * 10 ) , // 10x MaxCost
412
+ MaxCost : int64 ( cfg . BlockSize ) ,
397
413
BufferItems : bufferItems ,
398
414
OnEvict : cb .onRejectOrEvictBlock ,
399
415
OnReject : cb .onRejectOrEvictBlock ,
@@ -406,8 +422,8 @@ func newCachingBackend(
406
422
407
423
// Transaction cache (by transaction hash in hex), accounting in bytes.
408
424
txByHashHex , err := ristretto .NewCache (& ristretto.Config {
409
- NumCounters : txCacheSize * 10 ,
410
- MaxCost : txCacheSize ,
425
+ NumCounters : int64 ( cfg . TxSize * 10 ) ,
426
+ MaxCost : int64 ( cfg . TxSize ) ,
411
427
BufferItems : bufferItems ,
412
428
})
413
429
if err != nil {
@@ -417,8 +433,8 @@ func newCachingBackend(
417
433
418
434
// Transaction receipt cache (by transaction hash in hex), accounting in bytes.
419
435
receiptByTxHashHex , err := ristretto .NewCache (& ristretto.Config {
420
- NumCounters : receiptCacheSize * 10 ,
421
- MaxCost : receiptCacheSize ,
436
+ NumCounters : int64 ( cfg . TxReceiptSize * 10 ) ,
437
+ MaxCost : int64 ( cfg . TxReceiptSize ) ,
422
438
BufferItems : bufferItems ,
423
439
})
424
440
if err != nil {
@@ -431,7 +447,7 @@ func newCachingBackend(
431
447
// Try to initialize the last indexed/retained rounds. Failures
432
448
// are ok.
433
449
cb .lastIndexedRound , _ = backend .QueryLastIndexedRound (ctx )
434
- if cb .lastRetainedRound , _ = backend .QueryLastRetainedRound (ctx ); cb . lastRetainedRound != 0 {
450
+ if cb .lastRetainedRound , err = backend .QueryLastRetainedRound (ctx ); err == nil {
435
451
cb .lastRetainedRoundValid = 1
436
452
}
437
453
0 commit comments