Skip to content

Commit 2ea7976

Browse files
committed
fixup! feat(indexer): Add a tx and tx receipt cache
1 parent 8d8cff3 commit 2ea7976

File tree

7 files changed

+53
-14
lines changed

7 files changed

+53
-14
lines changed

conf/config.go

+11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Config struct {
2525
IndexingStart uint64 `koanf:"indexing_start"`
2626

2727
Log *LogConfig `koanf:"log"`
28+
Cache *CacheConfig `koanf:"cache"`
2829
Database *DatabaseConfig `koanf:"database"`
2930
Gateway *GatewayConfig `koanf:"gateway"`
3031
}
@@ -74,6 +75,16 @@ func (cfg *LogConfig) Validate() error {
7475
return level.Set(cfg.Level)
7576
}
7677

78+
// CacheConfig contains the cache configuration.
79+
type CacheConfig struct {
80+
// BlockSize is the size of the block cache in BLOCKS.
81+
BlockSize uint64 `koanf:"block_size"`
82+
// TxSize is the size of the transaction cache in bytes.
83+
TxSize uint64 `koanf:"tx_size"`
84+
// TxReceiptSize is the size of the transaction receipt cache in bytes.
85+
TxReceiptSize uint64 `koanf:"tx_receipt_size"`
86+
}
87+
7788
// DatabaseConfig is the postgresql database configuration.
7889
type DatabaseConfig struct {
7990
Host string `koanf:"host"`

conf/server.yml

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ log:
88
level: debug
99
format: json
1010

11+
cache:
12+
block_size: 1024
13+
tx_size: 1073741824
14+
tx_receipt_size: 1073741824
15+
1116
database:
1217
host: "127.0.0.1"
1318
port: 5432

conf/tests.yml

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ log:
55
level: debug
66
format: json
77

8+
cache:
9+
block_size: 10
10+
tx_size: 10485760
11+
tx_receipt_size: 10485760
12+
813
database:
914
host: "127.0.0.1"
1015
port: 5432

indexer/backend_cache.go

+27-11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/oasisprotocol/oasis-core/go/roothash/api/block"
1313
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/client"
1414

15+
"github.com/oasisprotocol/emerald-web3-gateway/conf"
1516
"github.com/oasisprotocol/emerald-web3-gateway/db/model"
1617
"github.com/oasisprotocol/emerald-web3-gateway/storage"
1718
)
@@ -378,22 +379,37 @@ func (cb *cachingBackend) onRejectOrEvictBlock(
378379
func newCachingBackend(
379380
ctx context.Context,
380381
backend Backend,
382+
cfg *conf.CacheConfig,
381383
) (Backend, error) {
382384
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
387390
)
388391

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+
389405
cb := &cachingBackend{
390406
inner: backend,
391407
}
392408

393409
// Block cache (by block number aka round), accounting in blocks.
394410
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),
397413
BufferItems: bufferItems,
398414
OnEvict: cb.onRejectOrEvictBlock,
399415
OnReject: cb.onRejectOrEvictBlock,
@@ -406,8 +422,8 @@ func newCachingBackend(
406422

407423
// Transaction cache (by transaction hash in hex), accounting in bytes.
408424
txByHashHex, err := ristretto.NewCache(&ristretto.Config{
409-
NumCounters: txCacheSize * 10,
410-
MaxCost: txCacheSize,
425+
NumCounters: int64(cfg.TxSize * 10),
426+
MaxCost: int64(cfg.TxSize),
411427
BufferItems: bufferItems,
412428
})
413429
if err != nil {
@@ -417,8 +433,8 @@ func newCachingBackend(
417433

418434
// Transaction receipt cache (by transaction hash in hex), accounting in bytes.
419435
receiptByTxHashHex, err := ristretto.NewCache(&ristretto.Config{
420-
NumCounters: receiptCacheSize * 10,
421-
MaxCost: receiptCacheSize,
436+
NumCounters: int64(cfg.TxReceiptSize * 10),
437+
MaxCost: int64(cfg.TxReceiptSize),
422438
BufferItems: bufferItems,
423439
})
424440
if err != nil {
@@ -431,7 +447,7 @@ func newCachingBackend(
431447
// Try to initialize the last indexed/retained rounds. Failures
432448
// are ok.
433449
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 {
435451
cb.lastRetainedRoundValid = 1
436452
}
437453

indexer/indexer.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/client"
1414
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/core"
1515

16+
"github.com/oasisprotocol/emerald-web3-gateway/conf"
1617
"github.com/oasisprotocol/emerald-web3-gateway/storage"
1718
)
1819

@@ -313,9 +314,10 @@ func New(
313314
enablePruning bool,
314315
pruningStep uint64,
315316
indexingStart uint64,
317+
cacheCfg *conf.CacheConfig,
316318
) (*Service, Backend, error) {
317319
ctx, cancelCtx := context.WithCancel(ctxBackend)
318-
cachingBackend, err := newCachingBackend(ctx, backend)
320+
cachingBackend, err := newCachingBackend(ctx, backend, cacheCfg)
319321
if err != nil {
320322
cancelCtx()
321323
return nil, nil, err

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func runRoot() error {
215215
return err
216216
}
217217
backend := indexer.NewIndexBackend(runtimeID, db, subBackend)
218-
indx, backend, err := indexer.New(ctx, backend, rc, runtimeID, db, cfg.EnablePruning, cfg.PruningStep, cfg.IndexingStart)
218+
indx, backend, err := indexer.New(ctx, backend, rc, runtimeID, db, cfg.EnablePruning, cfg.PruningStep, cfg.IndexingStart, cfg.Cache)
219219
if err != nil {
220220
logger.Error("failed to create indexer", err)
221221
return err

tests/rpc/utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func Setup() error {
149149
return err
150150
}
151151
backend := indexer.NewIndexBackend(runtimeID, db, subBackend)
152-
indx, backend, err := indexer.New(ctx, backend, rc, runtimeID, db, tests.TestsConfig.EnablePruning, tests.TestsConfig.PruningStep, tests.TestsConfig.IndexingStart)
152+
indx, backend, err := indexer.New(ctx, backend, rc, runtimeID, db, tests.TestsConfig.EnablePruning, tests.TestsConfig.PruningStep, tests.TestsConfig.IndexingStart, tests.TestsConfig.Cache)
153153
if err != nil {
154154
return err
155155
}

0 commit comments

Comments
 (0)