Skip to content

Commit ae34a6a

Browse files
authored
Merge pull request #581 from OffchainLabs/Fix-RecentWasms-cache-bug-by-using-pointers-in-methods
core: Fix RecentWasms cache bug by using pointers in methods
2 parents 75f2cec + 937527a commit ae34a6a

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

core/state/statedb_arbitrum.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ func (s *StateDB) RecordEvictWasm(wasm EvictWasm) {
330330
s.journal.entries = append(s.journal.entries, wasm)
331331
}
332332

333-
func (s *StateDB) GetRecentWasms() RecentWasms {
334-
return s.arbExtraData.recentWasms
333+
func (s *StateDB) GetRecentWasms() *RecentWasms {
334+
return &s.arbExtraData.recentWasms
335335
}
336336

337337
// Type for managing recent program access.
@@ -346,7 +346,7 @@ func NewRecentWasms() RecentWasms {
346346
}
347347

348348
// Inserts a new item, returning true if already present.
349-
func (p RecentWasms) Insert(item common.Hash, retain uint16) bool {
349+
func (p *RecentWasms) Insert(item common.Hash, retain uint16) bool {
350350
if p.cache == nil {
351351
cache := lru.NewBasicLRU[common.Hash, struct{}](int(retain))
352352
p.cache = &cache
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package state
2+
3+
import (
4+
"testing"
5+
6+
"github.com/ethereum/go-ethereum/common"
7+
"github.com/ethereum/go-ethereum/core/types"
8+
)
9+
10+
func TestRecentWasmsInsertAndCopy(t *testing.T) {
11+
db := NewDatabaseForTesting()
12+
state, err := New(types.EmptyRootHash, db)
13+
if err != nil {
14+
t.Fatalf("failed to create state: %v", err)
15+
}
16+
17+
const retain = uint16(8)
18+
19+
hash1 := common.HexToHash("0x01")
20+
hash2 := common.HexToHash("0x02")
21+
hash3 := common.HexToHash("0x03")
22+
23+
if hit := state.GetRecentWasms().Insert(hash1, retain); hit {
24+
t.Fatalf("first insert of hash1 should be a miss")
25+
}
26+
27+
if hit := state.GetRecentWasms().Insert(hash1, retain); !hit {
28+
t.Fatalf("second insert of hash1 should be a hit (cache not persisting)")
29+
}
30+
31+
if hit := state.GetRecentWasms().Insert(hash2, retain); hit {
32+
t.Fatalf("first insert of hash2 should be a miss")
33+
}
34+
35+
copy := state.Copy()
36+
37+
if hit := copy.GetRecentWasms().Insert(hash1, retain); !hit {
38+
t.Fatalf("copy: expected hit for hash1 present before copy")
39+
}
40+
if hit := copy.GetRecentWasms().Insert(hash2, retain); !hit {
41+
t.Fatalf("copy: expected hit for hash2 present before copy")
42+
}
43+
44+
if hit := copy.GetRecentWasms().Insert(hash3, retain); hit {
45+
t.Fatalf("copy: first insert of hash3 should be a miss")
46+
}
47+
48+
if hit := state.GetRecentWasms().Insert(hash3, retain); hit {
49+
t.Fatalf("original: first insert of hash3 should be a miss (must be independent of copy)")
50+
}
51+
}

core/state/statedb_hooked.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ func (s *hookedStateDB) RecordEvictWasm(wasm EvictWasm) {
321321
s.inner.RecordEvictWasm(wasm)
322322
}
323323

324-
func (s *hookedStateDB) GetRecentWasms() RecentWasms {
324+
func (s *hookedStateDB) GetRecentWasms() *RecentWasms {
325325
return s.inner.GetRecentWasms()
326326
}
327327

core/vm/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type StateDB interface {
3838
ActivatedAsmMap(targets []rawdb.WasmTarget, moduleHash common.Hash) (asmMap map[rawdb.WasmTarget][]byte, missingTargets []rawdb.WasmTarget, err error)
3939
RecordCacheWasm(wasm state.CacheWasm)
4040
RecordEvictWasm(wasm state.EvictWasm)
41-
GetRecentWasms() state.RecentWasms
41+
GetRecentWasms() *state.RecentWasms
4242

4343
// Arbitrum: track stylus's memory footprint
4444
GetStylusPages() (uint16, uint16)

0 commit comments

Comments
 (0)