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 2 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, txNum uint64) (*types.Receipt, bool) {
return api.receiptsGenerator.GetCachedReceipt(ctx, txNum)
}

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
24 changes: 21 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 @@

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

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

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...?

Check failure on line 51 in turbo/jsonrpc/receipts/receipts_generator.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)
receiptCache, err := lru.New[uint64, *types.Receipt](receiptsCacheLimit * 1000) // think they should be connected in some of that way
if err != nil {
panic(err)
}
Expand All @@ -56,6 +59,8 @@
blockReader: blockReader,
engine: engine,
receiptsCacheTrace: receiptsCacheTrace,
receiptCacheTrace: receiptsCacheTrace,
receiptCache: receiptCache,
}
}

Expand All @@ -71,6 +76,10 @@
return g.receiptsCache.Get(blockHash)
}

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

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 +112,19 @@
}, 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(txNum uint64, receipt *types.Receipt) {
g.receiptCache.Add(txNum, 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(txNum); ok {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

txNum is not reorg-resistant

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

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 +154,7 @@
receipt.Logs[i].Index = uint(firstLogIndex + uint32(i))
}

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

Expand All @@ -162,6 +180,6 @@
receipts[i] = receipt
}

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