Skip to content

Commit 3ad1488

Browse files
authored
Merge pull request #319 from OffchainLabs/stylus_cache_tag
Stylus cache tag
2 parents 247b930 + b8d4ced commit 3ad1488

File tree

8 files changed

+43
-21
lines changed

8 files changed

+43
-21
lines changed

arbitrum/apibackend.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ var (
4545
type APIBackend struct {
4646
b *Backend
4747

48+
dbForAPICalls ethdb.Database
49+
4850
fallbackClient types.FallbackClient
4951
sync SyncProgressBackend
5052
}
@@ -101,8 +103,15 @@ func createRegisterAPIBackend(backend *Backend, filterConfig filters.Config, fal
101103
if err != nil {
102104
return nil, err
103105
}
106+
// discard stylus-tag on any call made from api database
107+
dbForAPICalls := backend.chainDb
108+
wasmStore, tag := backend.chainDb.WasmDataBase()
109+
if tag != 0 {
110+
dbForAPICalls = rawdb.WrapDatabaseWithWasm(backend.chainDb, wasmStore, 0)
111+
}
104112
backend.apiBackend = &APIBackend{
105113
b: backend,
114+
dbForAPICalls: dbForAPICalls,
106115
fallbackClient: fallbackClient,
107116
}
108117
filterSystem := filters.NewFilterSystem(backend.apiBackend, filterConfig)
@@ -314,7 +323,7 @@ func (a *APIBackend) FeeHistory(
314323
}
315324

316325
func (a *APIBackend) ChainDb() ethdb.Database {
317-
return a.b.chainDb
326+
return a.dbForAPICalls
318327
}
319328

320329
func (a *APIBackend) AccountManager() *accounts.Manager {

arbitrum/recordingdb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func (r *RecordingDatabase) PrepareRecording(ctx context.Context, lastBlockHeade
269269
defer func() { r.Dereference(finalDereference) }()
270270
recordingKeyValue := newRecordingKV(r.db.TrieDB(), r.db.DiskDB())
271271

272-
recordingStateDatabase := state.NewDatabase(rawdb.WrapDatabaseWithWasm(rawdb.NewDatabase(recordingKeyValue), r.db.WasmStore()))
272+
recordingStateDatabase := state.NewDatabase(rawdb.WrapDatabaseWithWasm(rawdb.NewDatabase(recordingKeyValue), r.db.WasmStore(), 0))
273273
var prevRoot common.Hash
274274
if lastBlockHeader != nil {
275275
prevRoot = lastBlockHeader.Root

core/rawdb/database.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ type freezerdb struct {
4343
}
4444

4545
// AncientDatadir returns the path of root ancient directory.
46-
func (frdb *freezerdb) WasmDataBase() ethdb.KeyValueStore {
47-
return frdb
46+
func (frdb *freezerdb) WasmDataBase() (ethdb.KeyValueStore, uint32) {
47+
return frdb, 0
4848
}
4949

5050
// AncientDatadir returns the path of root ancient directory.
@@ -171,8 +171,8 @@ func (db *nofreezedb) AncientDatadir() (string, error) {
171171
}
172172

173173
// AncientDatadir returns the path of root ancient directory.
174-
func (db *nofreezedb) WasmDataBase() ethdb.KeyValueStore {
175-
return db
174+
func (db *nofreezedb) WasmDataBase() (ethdb.KeyValueStore, uint32) {
175+
return db, 0
176176
}
177177

178178
// NewDatabase creates a high level database on top of a given key-value data
@@ -183,11 +183,12 @@ func NewDatabase(db ethdb.KeyValueStore) ethdb.Database {
183183

184184
type dbWithWasmEntry struct {
185185
ethdb.Database
186-
wasmDb ethdb.KeyValueStore
186+
wasmDb ethdb.KeyValueStore
187+
wasmCacheTag uint32
187188
}
188189

189-
func (db *dbWithWasmEntry) WasmDataBase() ethdb.KeyValueStore {
190-
return db.wasmDb
190+
func (db *dbWithWasmEntry) WasmDataBase() (ethdb.KeyValueStore, uint32) {
191+
return db.wasmDb, db.wasmCacheTag
191192
}
192193

193194
func (db *dbWithWasmEntry) Close() error {
@@ -199,8 +200,8 @@ func (db *dbWithWasmEntry) Close() error {
199200
return wasmErr
200201
}
201202

202-
func WrapDatabaseWithWasm(db ethdb.Database, wasm ethdb.KeyValueStore) ethdb.Database {
203-
return &dbWithWasmEntry{db, wasm}
203+
func WrapDatabaseWithWasm(db ethdb.Database, wasm ethdb.KeyValueStore, cacheTag uint32) ethdb.Database {
204+
return &dbWithWasmEntry{db, wasm, cacheTag}
204205
}
205206

206207
// resolveChainFreezerDir is a helper function which resolves the absolute path

core/rawdb/table.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (t *table) Close() error {
4040
return nil
4141
}
4242

43-
func (t *table) WasmDataBase() ethdb.KeyValueStore {
43+
func (t *table) WasmDataBase() (ethdb.KeyValueStore, uint32) {
4444
return t.db.WasmDataBase()
4545
}
4646

core/state/database.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type Database interface {
5555
ActivatedAsm(moduleHash common.Hash) (asm []byte, err error)
5656
ActivatedModule(moduleHash common.Hash) (module []byte, err error)
5757
WasmStore() ethdb.KeyValueStore
58+
WasmCacheTag() uint32
5859

5960
// OpenTrie opens the main account trie.
6061
OpenTrie(root common.Hash) (Trie, error)
@@ -159,13 +160,15 @@ func NewDatabase(db ethdb.Database) Database {
159160
// is safe for concurrent use and retains a lot of collapsed RLP trie nodes in a
160161
// large memory cache.
161162
func NewDatabaseWithConfig(db ethdb.Database, config *trie.Config) Database {
163+
wasmdb, wasmTag := db.WasmDataBase()
162164
cdb := &cachingDB{
163165
// Arbitrum only
164166
activatedAsmCache: lru.NewSizeConstrainedCache[common.Hash, []byte](activatedWasmCacheSize),
165167
activatedModuleCache: lru.NewSizeConstrainedCache[common.Hash, []byte](activatedWasmCacheSize),
168+
wasmTag: wasmTag,
166169

167170
disk: db,
168-
wasmdb: db.WasmDataBase(),
171+
wasmdb: wasmdb,
169172
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
170173
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
171174
triedb: trie.NewDatabase(db, config),
@@ -175,13 +178,15 @@ func NewDatabaseWithConfig(db ethdb.Database, config *trie.Config) Database {
175178

176179
// NewDatabaseWithNodeDB creates a state database with an already initialized node database.
177180
func NewDatabaseWithNodeDB(db ethdb.Database, triedb *trie.Database) Database {
181+
wasmdb, wasmTag := db.WasmDataBase()
178182
cdb := &cachingDB{
179183
// Arbitrum only
180184
activatedAsmCache: lru.NewSizeConstrainedCache[common.Hash, []byte](activatedWasmCacheSize),
181185
activatedModuleCache: lru.NewSizeConstrainedCache[common.Hash, []byte](activatedWasmCacheSize),
186+
wasmTag: wasmTag,
182187

183188
disk: db,
184-
wasmdb: db.WasmDataBase(),
189+
wasmdb: wasmdb,
185190
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
186191
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
187192
triedb: triedb,
@@ -193,6 +198,7 @@ type cachingDB struct {
193198
// Arbitrum
194199
activatedAsmCache *lru.SizeConstrainedCache[common.Hash, []byte]
195200
activatedModuleCache *lru.SizeConstrainedCache[common.Hash, []byte]
201+
wasmTag uint32
196202

197203
disk ethdb.KeyValueStore
198204
wasmdb ethdb.KeyValueStore
@@ -205,6 +211,10 @@ func (db *cachingDB) WasmStore() ethdb.KeyValueStore {
205211
return db.wasmdb
206212
}
207213

214+
func (db *cachingDB) WasmCacheTag() uint32 {
215+
return db.wasmTag
216+
}
217+
208218
// OpenTrie opens the main account trie at a specific root hash.
209219
func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
210220
if db.triedb.IsVerkle() {

core/state/journal_arbitrum.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@ func (ch wasmActivation) dirtied() *common.Address {
1717
}
1818

1919
// Updates the Rust-side recent program cache
20-
var CacheWasmRust func(asm []byte, moduleHash common.Hash, version uint16, debug bool) = func([]byte, common.Hash, uint16, bool) {}
21-
var EvictWasmRust func(moduleHash common.Hash, version uint16, debug bool) = func(common.Hash, uint16, bool) {}
20+
var CacheWasmRust func(asm []byte, moduleHash common.Hash, version uint16, tag uint32, debug bool) = func([]byte, common.Hash, uint16, uint32, bool) {}
21+
var EvictWasmRust func(moduleHash common.Hash, version uint16, tag uint32, debug bool) = func(common.Hash, uint16, uint32, bool) {}
2222

2323
type CacheWasm struct {
2424
ModuleHash common.Hash
2525
Version uint16
26+
Tag uint32
2627
Debug bool
2728
}
2829

2930
func (ch CacheWasm) revert(s *StateDB) {
30-
EvictWasmRust(ch.ModuleHash, ch.Version, ch.Debug)
31+
EvictWasmRust(ch.ModuleHash, ch.Version, ch.Tag, ch.Debug)
3132
}
3233

3334
func (ch CacheWasm) dirtied() *common.Address {
@@ -37,14 +38,15 @@ func (ch CacheWasm) dirtied() *common.Address {
3738
type EvictWasm struct {
3839
ModuleHash common.Hash
3940
Version uint16
41+
Tag uint32
4042
Debug bool
4143
}
4244

4345
func (ch EvictWasm) revert(s *StateDB) {
4446
asm, err := s.TryGetActivatedAsm(ch.ModuleHash) // only happens in native mode
4547
if err == nil && len(asm) != 0 {
4648
//if we failed to get it - it's not in the current rust cache
47-
CacheWasmRust(asm, ch.ModuleHash, ch.Version, ch.Debug)
49+
CacheWasmRust(asm, ch.ModuleHash, ch.Version, ch.Tag, ch.Debug)
4850
}
4951
}
5052

ethdb/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ type AncientStore interface {
179179
}
180180

181181
type WasmDataBaseRetriever interface {
182-
WasmDataBase() KeyValueStore
182+
WasmDataBase() (KeyValueStore, uint32)
183183
}
184184

185185
// Database contains all the methods required by the high level database to not

ethdb/remotedb/remotedb.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func (db *Database) Has(key []byte) (bool, error) {
3939
return true, nil
4040
}
4141

42-
func (t *Database) WasmDataBase() ethdb.KeyValueStore {
43-
return t
42+
func (t *Database) WasmDataBase() (ethdb.KeyValueStore, uint32) {
43+
return t, 0
4444
}
4545

4646
func (db *Database) Get(key []byte) ([]byte, error) {

0 commit comments

Comments
 (0)