Skip to content

Commit

Permalink
op-node: calc receipts cache volume
Browse files Browse the repository at this point in the history
  • Loading branch information
bendanzhentan committed Dec 25, 2023
1 parent 40dd44d commit 49a8c69
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
31 changes: 17 additions & 14 deletions op-node/sources/caching/blob_lru.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package caching

Check failure on line 1 in op-node/sources/caching/blob_lru.go

View workflow job for this annotation

GitHub Actions / op-node-lint

: # github.com/ethereum-optimism/optimism/op-node/sources/caching

import (
lru "github.com/hashicorp/golang-lru/v2"
"math"
"sync"

lru "github.com/hashicorp/golang-lru/v2"
)

type SizeFn func(value any) int
Expand Down Expand Up @@ -45,21 +46,23 @@ func (c *SizeConstrainedCache[K, V]) Add(key K, value V) (evicted bool) {
c.lock.Lock()
defer c.lock.Unlock()

// Unless it is already present, might need to evict something.
// OBS: If it is present, we still call Add internally to bump the recentness.
if !c.lru.Contains(key) {
targetSize := c.size + c.sizeFn(value)
for targetSize > c.maxSize {
evicted = true
_, v, ok := c.lru.RemoveOldest()
if !ok {
// list is now empty. Break
break
}
targetSize -= c.sizeFn(v)
// Remove old duplicate item
if oldValue, ok := c.lru.Get(key); ok && oldValue != nil {
c.lru.Remove(key)
c.size -= c.sizeFn(oldValue)
}

targetSize := c.size + c.sizeFn(value)
for targetSize > c.maxSize {
evicted = true
_, v, ok := c.lru.RemoveOldest()
if !ok {
// list is now empty. Break
break
}
c.size = targetSize
targetSize -= c.sizeFn(v)
}
c.size = targetSize

c.lru.Add(key, value)
if c.m != nil {
Expand Down
6 changes: 2 additions & 4 deletions op-node/sources/eth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,6 @@ func (s *EthClient) FetchReceipts(ctx context.Context, blockHash common.Hash) (e
} else {
txHashes := eth.TransactionsToHashes(txs)
job = NewReceiptsFetchingJob(s, s.client, s.maxBatchSize, eth.ToBlockID(info), info.ReceiptHash(), txHashes)
_, err := job.Fetch(ctx)
if err != nil {
return nil, nil, err
}
s.receiptsCache.Add(blockHash, job)
}

Expand All @@ -396,6 +392,8 @@ func (s *EthClient) FetchReceipts(ctx context.Context, blockHash common.Hash) (e
return nil, nil, err
}

s.receiptsCache.Add(blockHash, job)

return info, receipts, nil
}

Expand Down

0 comments on commit 49a8c69

Please sign in to comment.