Skip to content

Commit

Permalink
Merge pull request #498 from oasisprotocol/ptrus/feature/reduce-allocs
Browse files Browse the repository at this point in the history
indexer: Fix block cache pruning
  • Loading branch information
ptrus authored Dec 27, 2023
2 parents cd074b5 + 66a796d commit d1b0e67
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
14 changes: 9 additions & 5 deletions indexer/backend_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,20 +427,19 @@ func (cb *cachingBackend) pruneCache(
panic("indexer: cached entry block height != queried height")
}

cb.blockByHashHex.Delete(bd.Block.Hash)
cb.logsByBlockNumber.Delete(bd.Block.Round)

// Note: Load followed by delete is safe since the only routine that
// mutates those caches is the caller.
// Prune transaction cache.
for i := range bd.UniqueTxes {
txHash := bd.UniqueTxes[i].Hash
// Note: Load followed by delete is safe since the only routine that
// mutates those caches is the caller.
if untypedTxEntry, ok := cb.txByHashHex.Load(txHash); ok {
txEntry := untypedTxEntry.(*txCacheEntry)
if txEntry.blockNumber == blockNumber {
cb.txByHashHex.Delete(txHash)
}
}
}
// Prune receipts cache.
for i := range bd.Receipts {
txHash := bd.Receipts[i].TransactionHash
if untypedReceiptEntry, ok := cb.receiptByTxHashHex.Load(txHash); ok {
Expand All @@ -451,6 +450,11 @@ func (cb *cachingBackend) pruneCache(
}
}

// Prune block caches.
cb.blockByHashHex.Delete(bd.Block.Hash)
cb.logsByBlockNumber.Delete(bd.Block.Round)
cb.blockDataByNumber.Delete(blockNumber)

cb.cacheSize--
}

Expand Down
18 changes: 9 additions & 9 deletions indexer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ func blockToModels(
bshash := block.Header.StateRoot.Hex()
logsBloom := ethtypes.BytesToBloom(ethtypes.LogsBloom(logs))
bloomData, _ := logsBloom.MarshalText()
bloomHex := hex.EncodeToString(bloomData)
baseFee := big.NewInt(10)
number := big.NewInt(0)
number.SetUint64(block.Header.Round)
var btxHash string
if len(transactions) == 0 {
btxHash = ethtypes.EmptyRootHash.Hex()
Expand All @@ -123,7 +122,7 @@ func blockToModels(
ReceiptHash: ethtypes.EmptyRootHash.Hex(),
Bloom: string(bloomData),
Difficulty: big.NewInt(0).String(),
Number: number.Uint64(),
Number: block.Header.Round,
GasLimit: blockGasLimit,
Time: uint64(block.Header.Timestamp),
Extra: "",
Expand All @@ -136,6 +135,7 @@ func blockToModels(
innerReceipts := make([]*model.Receipt, 0, len(transactions))
cumulativeGasUsed := uint64(0)
for idx, ethTx := range transactions {
ethTxHex := ethTx.Hash().Hex()
v, r, s := ethTx.RawSignatureValues()
signer := ethtypes.LatestSignerForChainID(ethTx.ChainId())
from, _ := signer.Sender(ethTx)
Expand All @@ -153,12 +153,12 @@ func blockToModels(
accList = append(accList, acc)
}
tx := &model.Transaction{
Hash: ethTx.Hash().Hex(),
Hash: ethTxHex,
Type: ethTx.Type(),
ChainID: ethTx.ChainId().String(),
Status: uint(txsStatus[idx]),
BlockHash: bhash,
Round: number.Uint64(),
Round: block.Header.Round,
Index: uint32(idx),
Gas: ethTx.Gas(),
GasPrice: ethTx.GasPrice().String(),
Expand Down Expand Up @@ -186,9 +186,9 @@ func blockToModels(
receipt := &model.Receipt{
Status: uint(txsStatus[idx]),
CumulativeGasUsed: cumulativeGasUsed,
Logs: txLogs[ethTx.Hash().Hex()],
LogsBloom: hex.EncodeToString(bloomData),
TransactionHash: ethTx.Hash().Hex(),
Logs: txLogs[ethTxHex],
LogsBloom: bloomHex,
TransactionHash: ethTxHex,
BlockHash: bhash,
GasUsed: txsGasUsed[idx],
Type: uint(ethTx.Type()),
Expand All @@ -213,7 +213,7 @@ func blockToModels(

innerBlock := &model.Block{
Hash: bhash,
Round: number.Uint64(),
Round: block.Header.Round,
Header: innerHeader,
Transactions: innerTxs,
}
Expand Down

0 comments on commit d1b0e67

Please sign in to comment.