Skip to content

Commit 248de73

Browse files
authored
Merge pull request #1994 from CortexFoundation/dev
block lookup cache api used
2 parents 85ff0eb + 93c1a6e commit 248de73

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

core/blockchain.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ var defaultCacheConfig = &CacheConfig{
151151
SnapshotWait: true,
152152
}
153153

154+
// txLookup is wrapper over transaction lookup along with the corresponding
155+
// transaction object.
156+
type txLookup struct {
157+
lookup *rawdb.LegacyTxLookupEntry
158+
transaction *types.Transaction
159+
}
160+
154161
// BlockChain represents the canonical chain given a database with a genesis
155162
// block. The Blockchain manages chain imports, reverts, chain reorganisations.
156163
//
@@ -205,10 +212,9 @@ type BlockChain struct {
205212
bodyRLPCache *lru.Cache[common.Hash, rlp.RawValue]
206213
receiptsCache *lru.Cache[common.Hash, []*types.Receipt]
207214
blockCache *lru.Cache[common.Hash, *types.Block]
208-
txLookupCache *lru.Cache[common.Hash, *rawdb.LegacyTxLookupEntry]
215+
txLookupCache *lru.Cache[common.Hash, txLookup]
209216

210217
txLookupLock sync.RWMutex
211-
//txLookupCache *lru.Cache[common.Hash, txLookup]
212218

213219
// future blocks are blocks added for later processing
214220
futureBlocks *lru.Cache[common.Hash, *types.Block]
@@ -257,7 +263,7 @@ func NewBlockChain(db ctxcdb.Database, cacheConfig *CacheConfig, chainConfig *pa
257263
bodyRLPCache: lru.NewCache[common.Hash, rlp.RawValue](bodyCacheLimit),
258264
receiptsCache: lru.NewCache[common.Hash, []*types.Receipt](receiptsCacheLimit),
259265
blockCache: lru.NewCache[common.Hash, *types.Block](blockCacheLimit),
260-
txLookupCache: lru.NewCache[common.Hash, *rawdb.LegacyTxLookupEntry](txLookupCacheLimit),
266+
txLookupCache: lru.NewCache[common.Hash, txLookup](txLookupCacheLimit),
261267
futureBlocks: lru.NewCache[common.Hash, *types.Block](maxFutureBlocks),
262268
engine: engine,
263269
vmConfig: vmConfig,

core/blockchain_reader.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,21 +263,30 @@ func (bc *BlockChain) GetAncestor(hash common.Hash, number, ancestor uint64, max
263263
// A null will be returned in the transaction is not found and background
264264
// transaction indexing is already finished. The transaction is not existent
265265
// from the node's perspective
266-
func (bc *BlockChain) GetTransactionLookup(hash common.Hash) *rawdb.LegacyTxLookupEntry {
266+
func (bc *BlockChain) GetTransactionLookup(hash common.Hash) (*rawdb.LegacyTxLookupEntry, *types.Transaction, error) {
267267
bc.txLookupLock.RLock()
268268
defer bc.txLookupLock.RUnlock()
269269

270270
// Short circuit if the txlookup already in the cache, retrieve otherwise
271-
if lookup, exist := bc.txLookupCache.Get(hash); exist {
272-
return lookup
271+
if item, exist := bc.txLookupCache.Get(hash); exist {
272+
return item.lookup, item.transaction, nil
273273
}
274+
274275
tx, blockHash, blockNumber, txIndex := rawdb.ReadTransaction(bc.db, hash)
275276
if tx == nil {
276-
return nil
277+
return nil, nil, nil
278+
}
279+
280+
lookup := &rawdb.LegacyTxLookupEntry{
281+
BlockHash: blockHash,
282+
BlockIndex: blockNumber,
283+
Index: txIndex,
277284
}
278-
lookup := &rawdb.LegacyTxLookupEntry{BlockHash: blockHash, BlockIndex: blockNumber, Index: txIndex}
279-
bc.txLookupCache.Add(hash, lookup)
280-
return lookup
285+
bc.txLookupCache.Add(hash, txLookup{
286+
lookup: lookup,
287+
transaction: tx,
288+
})
289+
return lookup, tx, nil
281290
}
282291

283292
// GetTd retrieves a block's total difficulty in the canonical chain from the

ctxc/api_backend.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,14 @@ func (b *CortexAPIBackend) GetPoolTransaction(hash common.Hash) *types.Transacti
279279
}
280280

281281
func (b *CortexAPIBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
282-
tx, blockHash, blockNumber, index := rawdb.ReadTransaction(b.ctxc.ChainDb(), txHash)
283-
return tx, blockHash, blockNumber, index, nil
282+
lookup, tx, err := b.ctxc.blockchain.GetTransactionLookup(txHash)
283+
if err != nil {
284+
return nil, common.Hash{}, 0, 0, err
285+
}
286+
if lookup == nil || tx == nil {
287+
return nil, common.Hash{}, 0, 0, nil
288+
}
289+
return tx, lookup.BlockHash, lookup.BlockIndex, lookup.Index, nil
284290
}
285291

286292
func (b *CortexAPIBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {

0 commit comments

Comments
 (0)