Skip to content

Commit

Permalink
fix: cache should be updated when key exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Welkin committed Jan 26, 2024
1 parent 277234f commit e852c3e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 2 additions & 1 deletion op-node/sources/caching/pre_fetch_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func (v *PreFetchCache[V]) AddIfNotFull(key uint64, value V) (success bool, isFu
defer v.lock.Unlock()
v.lock.Lock()
if _, ok := v.inner[key]; ok {
return false, false
v.inner[key] = value
return true, false
}
if v.queue.Size() >= v.maxSize {
return false, true
Expand Down
7 changes: 6 additions & 1 deletion op-node/sources/l1_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func TestGoOrUpdatePreFetchReceipts(t *testing.T) {
}).Return([]error{nil})
}
var lastParentHeader common.Hash
var real100Hash common.Hash
for i := 76; i <= 100; i++ {
currentHead := &rpcHeader{
ParentHash: lastParentHeader,
Expand All @@ -135,6 +136,9 @@ func TestGoOrUpdatePreFetchReceipts(t *testing.T) {
WithdrawalsRoot: nil,
Hash: randHash(),
}
if i == 100 {
real100Hash = currentHead.Hash
}
lastParentHeader = currentHead.Hash
m.On("CallContext", ctx, new(*rpcHeader),
"eth_getBlockByNumber", []any{numberID(i).Arg(), false}).Once().Run(func(args mock.Arguments) {
Expand All @@ -154,8 +158,9 @@ func TestGoOrUpdatePreFetchReceipts(t *testing.T) {
err2 := s.GoOrUpdatePreFetchReceipts(ctx, 81)
require.NoError(t, err2)
time.Sleep(1 * time.Second)
_, ok := s.receiptsCache.Get(100)
pair, ok := s.receiptsCache.Get(100)
require.True(t, ok, "100 cache miss")
require.Equal(t, real100Hash, pair.blockHash, "block 100 hash is different,want:%s,but:%s", real100Hash, pair.blockHash)
_, ok2 := s.receiptsCache.Get(76)
require.True(t, ok2, "76 cache miss")
})
Expand Down

0 comments on commit e852c3e

Please sign in to comment.