Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache tx granularity #13134

Merged
merged 4 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions turbo/jsonrpc/eth_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func (api *BaseAPI) getReceipt(ctx context.Context, cc *chain.Config, tx kv.Temp
return api.receiptsGenerator.GetReceipt(ctx, cc, tx, block, index, txNum)
}

func (api *BaseAPI) getCachedReceipt(ctx context.Context, hash common.Hash) (*types.Receipt, bool) {
return api.receiptsGenerator.GetCachedReceipt(ctx, hash)
}

func (api *BaseAPI) getCachedReceipts(ctx context.Context, hash common.Hash) (types.Receipts, bool) {
return api.receiptsGenerator.GetCachedReceipts(ctx, hash)
}
Expand Down Expand Up @@ -463,7 +467,7 @@ func (api *APIImpl) GetTransactionReceipt(ctx context.Context, txnHash common.Ha
}
}

if txn == nil && chainConfig.Bor != nil {
if txn == nil && chainConfig.Bor != nil { //TODO: add tx gran here to.
receipts, err := api.getReceipts(ctx, tx, block)
if err != nil {
return nil, fmt.Errorf("getReceipts error: %w", err)
Expand Down Expand Up @@ -501,7 +505,6 @@ func (api *APIImpl) GetBlockReceipts(ctx context.Context, numberOrHash rpc.Block
return nil, err
}
defer tx.Rollback()

blockNum, blockHash, _, err := rpchelper.GetBlockNumber(ctx, numberOrHash, tx, api._blockReader, api.filters)
if err != nil {
return nil, err
Expand Down
28 changes: 25 additions & 3 deletions turbo/jsonrpc/receipts/receipts_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import (

type Generator struct {
receiptsCache *lru.Cache[common.Hash, types.Receipts]
receiptCache *lru.Cache[common.Hash, *types.Receipt]
receiptsCacheTrace bool
receiptCacheTrace bool

blockReader services.FullBlockReader
engine consensus.EngineReader
Expand All @@ -46,7 +48,12 @@ var (
)

func NewGenerator(blockReader services.FullBlockReader, engine consensus.EngineReader) *Generator {
receiptsCache, err := lru.New[common.Hash, types.Receipts](receiptsCacheLimit)
receiptsCache, err := lru.New[common.Hash, types.Receipts](receiptsCacheLimit) //TODO: is handling both of them a good idea though...?
if err != nil {
panic(err)
}

receiptCache, err := lru.New[common.Hash, *types.Receipt](receiptsCacheLimit * 1000) // think they should be connected in some of that way
if err != nil {
panic(err)
}
Expand All @@ -56,6 +63,8 @@ func NewGenerator(blockReader services.FullBlockReader, engine consensus.EngineR
blockReader: blockReader,
engine: engine,
receiptsCacheTrace: receiptsCacheTrace,
receiptCacheTrace: receiptsCacheTrace,
receiptCache: receiptCache,
}
}

Expand All @@ -71,6 +80,10 @@ func (g *Generator) GetCachedReceipts(ctx context.Context, blockHash common.Hash
return g.receiptsCache.Get(blockHash)
}

func (g *Generator) GetCachedReceipt(ctx context.Context, hash common.Hash) (*types.Receipt, bool) {
return g.receiptCache.Get(hash)
}

func (g *Generator) PrepareEnv(ctx context.Context, block *types.Block, cfg *chain.Config, tx kv.TemporalTx, txIndex int) (*ReceiptEnv, error) {
txNumsReader := rawdbv3.TxNums.WithCustomReadTxNumFunc(freezeblocks.ReadTxNumFuncFromBlockReader(ctx, g.blockReader))
ibs, _, _, _, _, err := transactions.ComputeBlockContext(ctx, g.engine, block.HeaderNoCopy(), cfg, g.blockReader, txNumsReader, tx, txIndex)
Expand Down Expand Up @@ -103,11 +116,19 @@ func (g *Generator) PrepareEnv(ctx context.Context, block *types.Block, cfg *cha
}, nil
}

func (g *Generator) addToCache(header *types.Header, receipts types.Receipts) {
func (g *Generator) addToCacheReceipts(header *types.Header, receipts types.Receipts) {
g.receiptsCache.Add(header.Hash(), receipts.Copy()) // .Copy() helps pprof to attribute memory to cache - instead of evm (where it was allocated).
}

func (g *Generator) addToCacheReceipt(hash common.Hash, receipt *types.Receipt) {
g.receiptCache.Add(hash, receipt.Copy()) // .Copy() helps pprof to attribute memory to cache - instead of evm (where it was allocated).
}

func (g *Generator) GetReceipt(ctx context.Context, cfg *chain.Config, tx kv.TemporalTx, block *types.Block, index int, txNum uint64) (*types.Receipt, error) {
if receipt, ok := g.receiptCache.Get(block.Transactions()[index].Hash()); ok {
return receipt, nil
}

if receipts, ok := g.receiptsCache.Get(block.Hash()); ok && len(receipts) > index {
return receipts[index], nil
}
Expand Down Expand Up @@ -137,6 +158,7 @@ func (g *Generator) GetReceipt(ctx context.Context, cfg *chain.Config, tx kv.Tem
receipt.Logs[i].Index = uint(firstLogIndex + uint32(i))
}

g.addToCacheReceipt(receipt.TxHash, receipt)
return receipt, nil
}

Expand All @@ -162,6 +184,6 @@ func (g *Generator) GetReceipts(ctx context.Context, cfg *chain.Config, tx kv.Te
receipts[i] = receipt
}

g.addToCache(block.HeaderNoCopy(), receipts)
g.addToCacheReceipts(block.HeaderNoCopy(), receipts)
return receipts, nil
}
Loading