Skip to content

Commit b69942b

Browse files
ryanschneiderfjl
authored andcommitted
core, internal/ethapi: add and use LRU cache for receipts (#17610)
1 parent 86ec213 commit b69942b

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

‎core/blockchain.go‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var (
5555
const (
5656
bodyCacheLimit = 256
5757
blockCacheLimit = 256
58+
receiptsCacheLimit = 32
5859
maxFutureBlocks = 256
5960
maxTimeFutureBlocks = 30
6061
badBlockLimit = 10
@@ -111,11 +112,12 @@ type BlockChain struct {
111112
currentBlock atomic.Value // Current head of the block chain
112113
currentFastBlock atomic.Value // Current head of the fast-sync chain (may be above the block chain!)
113114

114-
stateCache state.Database // State database to reuse between imports (contains state cache)
115-
bodyCache *lru.Cache // Cache for the most recent block bodies
116-
bodyRLPCache *lru.Cache // Cache for the most recent block bodies in RLP encoded format
117-
blockCache *lru.Cache // Cache for the most recent entire blocks
118-
futureBlocks *lru.Cache // future blocks are blocks added for later processing
115+
stateCache state.Database // State database to reuse between imports (contains state cache)
116+
bodyCache *lru.Cache // Cache for the most recent block bodies
117+
bodyRLPCache *lru.Cache // Cache for the most recent block bodies in RLP encoded format
118+
receiptsCache *lru.Cache // Cache for the most recent receipts per block
119+
blockCache *lru.Cache // Cache for the most recent entire blocks
120+
futureBlocks *lru.Cache // future blocks are blocks added for later processing
119121

120122
quit chan struct{} // blockchain quit channel
121123
running int32 // running must be called atomically
@@ -144,6 +146,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
144146
}
145147
bodyCache, _ := lru.New(bodyCacheLimit)
146148
bodyRLPCache, _ := lru.New(bodyCacheLimit)
149+
receiptsCache, _ := lru.New(receiptsCacheLimit)
147150
blockCache, _ := lru.New(blockCacheLimit)
148151
futureBlocks, _ := lru.New(maxFutureBlocks)
149152
badBlocks, _ := lru.New(badBlockLimit)
@@ -158,6 +161,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
158161
shouldPreserve: shouldPreserve,
159162
bodyCache: bodyCache,
160163
bodyRLPCache: bodyRLPCache,
164+
receiptsCache: receiptsCache,
161165
blockCache: blockCache,
162166
futureBlocks: futureBlocks,
163167
engine: engine,
@@ -280,6 +284,7 @@ func (bc *BlockChain) SetHead(head uint64) error {
280284
// Clear out any stale content from the caches
281285
bc.bodyCache.Purge()
282286
bc.bodyRLPCache.Purge()
287+
bc.receiptsCache.Purge()
283288
bc.blockCache.Purge()
284289
bc.futureBlocks.Purge()
285290

@@ -603,11 +608,18 @@ func (bc *BlockChain) GetBlockByNumber(number uint64) *types.Block {
603608

604609
// GetReceiptsByHash retrieves the receipts for all transactions in a given block.
605610
func (bc *BlockChain) GetReceiptsByHash(hash common.Hash) types.Receipts {
611+
if receipts, ok := bc.receiptsCache.Get(hash); ok {
612+
return receipts.(types.Receipts)
613+
}
614+
606615
number := rawdb.ReadHeaderNumber(bc.db, hash)
607616
if number == nil {
608617
return nil
609618
}
610-
return rawdb.ReadReceipts(bc.db, hash, *number)
619+
620+
receipts := rawdb.ReadReceipts(bc.db, hash, *number)
621+
bc.receiptsCache.Add(hash, receipts)
622+
return receipts
611623
}
612624

613625
// GetBlocksFromHash returns the block corresponding to hash and up to n-1 ancestors.

‎eth/api_backend.go‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/ethereum/go-ethereum/common/math"
2626
"github.com/ethereum/go-ethereum/core"
2727
"github.com/ethereum/go-ethereum/core/bloombits"
28-
"github.com/ethereum/go-ethereum/core/rawdb"
2928
"github.com/ethereum/go-ethereum/core/state"
3029
"github.com/ethereum/go-ethereum/core/types"
3130
"github.com/ethereum/go-ethereum/core/vm"
@@ -107,18 +106,11 @@ func (b *EthAPIBackend) GetBlock(ctx context.Context, hash common.Hash) (*types.
107106
}
108107

109108
func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
110-
if number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash); number != nil {
111-
return rawdb.ReadReceipts(b.eth.chainDb, hash, *number), nil
112-
}
113-
return nil, nil
109+
return b.eth.blockchain.GetReceiptsByHash(hash), nil
114110
}
115111

116112
func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
117-
number := rawdb.ReadHeaderNumber(b.eth.chainDb, hash)
118-
if number == nil {
119-
return nil, nil
120-
}
121-
receipts := rawdb.ReadReceipts(b.eth.chainDb, hash, *number)
113+
receipts := b.eth.blockchain.GetReceiptsByHash(hash)
122114
if receipts == nil {
123115
return nil, nil
124116
}

0 commit comments

Comments
 (0)