5555const (
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.
605610func (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.
0 commit comments