Skip to content

Commit 07f6d7a

Browse files
authored
Merge branch 'master' into pebble-extra-options
2 parents 5b7b36a + 3ad1488 commit 07f6d7a

File tree

14 files changed

+107
-26
lines changed

14 files changed

+107
-26
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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ func (db *RecordingKV) Get(key []byte) ([]byte, error) {
5555
// Retrieving code
5656
copy(hash[:], key[len(rawdb.CodePrefix):])
5757
res, err = db.diskDb.Get(key)
58-
} else if ok, _ := rawdb.IsActivatedAsmKey(key); ok {
59-
// Arbitrum: the asm is non-consensus
60-
return db.diskDb.Get(key)
61-
} else if ok, _ := rawdb.IsActivatedModuleKey(key); ok {
62-
// Arbitrum: the module is non-consensus (only its hash is)
63-
return db.diskDb.Get(key)
6458
} else {
6559
err = fmt.Errorf("recording KV attempted to access non-hash key %v", hex.EncodeToString(key))
6660
}
@@ -275,7 +269,7 @@ func (r *RecordingDatabase) PrepareRecording(ctx context.Context, lastBlockHeade
275269
defer func() { r.Dereference(finalDereference) }()
276270
recordingKeyValue := newRecordingKV(r.db.TrieDB(), r.db.DiskDB())
277271

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

core/rawdb/database.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ type freezerdb struct {
4242
ethdb.AncientStore
4343
}
4444

45+
// AncientDatadir returns the path of root ancient directory.
46+
func (frdb *freezerdb) WasmDataBase() (ethdb.KeyValueStore, uint32) {
47+
return frdb, 0
48+
}
49+
4550
// AncientDatadir returns the path of root ancient directory.
4651
func (frdb *freezerdb) AncientDatadir() (string, error) {
4752
return frdb.ancientRoot, nil
@@ -165,12 +170,40 @@ func (db *nofreezedb) AncientDatadir() (string, error) {
165170
return "", errNotSupported
166171
}
167172

173+
// AncientDatadir returns the path of root ancient directory.
174+
func (db *nofreezedb) WasmDataBase() (ethdb.KeyValueStore, uint32) {
175+
return db, 0
176+
}
177+
168178
// NewDatabase creates a high level database on top of a given key-value data
169179
// store without a freezer moving immutable chain segments into cold storage.
170180
func NewDatabase(db ethdb.KeyValueStore) ethdb.Database {
171181
return &nofreezedb{KeyValueStore: db}
172182
}
173183

184+
type dbWithWasmEntry struct {
185+
ethdb.Database
186+
wasmDb ethdb.KeyValueStore
187+
wasmCacheTag uint32
188+
}
189+
190+
func (db *dbWithWasmEntry) WasmDataBase() (ethdb.KeyValueStore, uint32) {
191+
return db.wasmDb, db.wasmCacheTag
192+
}
193+
194+
func (db *dbWithWasmEntry) Close() error {
195+
dbErr := db.Database.Close()
196+
wasmErr := db.wasmDb.Close()
197+
if dbErr != nil {
198+
return dbErr
199+
}
200+
return wasmErr
201+
}
202+
203+
func WrapDatabaseWithWasm(db ethdb.Database, wasm ethdb.KeyValueStore, cacheTag uint32) ethdb.Database {
204+
return &dbWithWasmEntry{db, wasm, cacheTag}
205+
}
206+
174207
// resolveChainFreezerDir is a helper function which resolves the absolute path
175208
// of chain freezer by considering backward compatibility.
176209
func resolveChainFreezerDir(ancient string) string {

core/rawdb/table.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ func (t *table) Close() error {
4040
return nil
4141
}
4242

43+
func (t *table) WasmDataBase() (ethdb.KeyValueStore, uint32) {
44+
return t.db.WasmDataBase()
45+
}
46+
4347
// Has retrieves if a prefixed version of a key is present in the database.
4448
func (t *table) Has(key []byte) (bool, error) {
4549
return t.db.Has(append([]byte(t.prefix), key...))

core/state/database.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ type Database interface {
5454
// Arbitrum: Read activated Stylus contracts
5555
ActivatedAsm(moduleHash common.Hash) (asm []byte, err error)
5656
ActivatedModule(moduleHash common.Hash) (module []byte, err error)
57+
WasmStore() ethdb.KeyValueStore
58+
WasmCacheTag() uint32
5759

5860
// OpenTrie opens the main account trie.
5961
OpenTrie(root common.Hash) (Trie, error)
@@ -158,12 +160,15 @@ func NewDatabase(db ethdb.Database) Database {
158160
// is safe for concurrent use and retains a lot of collapsed RLP trie nodes in a
159161
// large memory cache.
160162
func NewDatabaseWithConfig(db ethdb.Database, config *trie.Config) Database {
163+
wasmdb, wasmTag := db.WasmDataBase()
161164
cdb := &cachingDB{
162165
// Arbitrum only
163166
activatedAsmCache: lru.NewSizeConstrainedCache[common.Hash, []byte](activatedWasmCacheSize),
164167
activatedModuleCache: lru.NewSizeConstrainedCache[common.Hash, []byte](activatedWasmCacheSize),
168+
wasmTag: wasmTag,
165169

166170
disk: db,
171+
wasmdb: wasmdb,
167172
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
168173
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
169174
triedb: trie.NewDatabase(db, config),
@@ -173,12 +178,15 @@ func NewDatabaseWithConfig(db ethdb.Database, config *trie.Config) Database {
173178

174179
// NewDatabaseWithNodeDB creates a state database with an already initialized node database.
175180
func NewDatabaseWithNodeDB(db ethdb.Database, triedb *trie.Database) Database {
181+
wasmdb, wasmTag := db.WasmDataBase()
176182
cdb := &cachingDB{
177183
// Arbitrum only
178184
activatedAsmCache: lru.NewSizeConstrainedCache[common.Hash, []byte](activatedWasmCacheSize),
179185
activatedModuleCache: lru.NewSizeConstrainedCache[common.Hash, []byte](activatedWasmCacheSize),
186+
wasmTag: wasmTag,
180187

181188
disk: db,
189+
wasmdb: wasmdb,
182190
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
183191
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
184192
triedb: triedb,
@@ -190,13 +198,23 @@ type cachingDB struct {
190198
// Arbitrum
191199
activatedAsmCache *lru.SizeConstrainedCache[common.Hash, []byte]
192200
activatedModuleCache *lru.SizeConstrainedCache[common.Hash, []byte]
201+
wasmTag uint32
193202

194203
disk ethdb.KeyValueStore
204+
wasmdb ethdb.KeyValueStore
195205
codeSizeCache *lru.Cache[common.Hash, int]
196206
codeCache *lru.SizeConstrainedCache[common.Hash, []byte]
197207
triedb *trie.Database
198208
}
199209

210+
func (db *cachingDB) WasmStore() ethdb.KeyValueStore {
211+
return db.wasmdb
212+
}
213+
214+
func (db *cachingDB) WasmCacheTag() uint32 {
215+
return db.wasmTag
216+
}
217+
200218
// OpenTrie opens the main account trie at a specific root hash.
201219
func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
202220
if db.triedb.IsVerkle() {

core/state/database_arbitrum.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func (db *cachingDB) ActivatedAsm(moduleHash common.Hash) ([]byte, error) {
1212
return asm, nil
1313
}
1414
wasmKey := rawdb.ActivatedAsmKey(moduleHash)
15-
asm, err := db.disk.Get(wasmKey[:])
15+
asm, err := db.wasmdb.Get(wasmKey[:])
1616
if err != nil {
1717
return nil, err
1818
}
@@ -28,7 +28,7 @@ func (db *cachingDB) ActivatedModule(moduleHash common.Hash) ([]byte, error) {
2828
return module, nil
2929
}
3030
wasmKey := rawdb.ActivatedModuleKey(moduleHash)
31-
module, err := db.disk.Get(wasmKey[:])
31+
module, err := db.wasmdb.Get(wasmKey[:])
3232
if err != nil {
3333
return nil, err
3434
}

core/state/journal_arbitrum.go

Lines changed: 10 additions & 5 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,12 +38,16 @@ 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) {
44-
asm := s.GetActivatedAsm(ch.ModuleHash) // only happens in native mode
45-
CacheWasmRust(asm, ch.ModuleHash, ch.Version, ch.Debug)
46+
asm, err := s.TryGetActivatedAsm(ch.ModuleHash) // only happens in native mode
47+
if err == nil && len(asm) != 0 {
48+
//if we failed to get it - it's not in the current rust cache
49+
CacheWasmRust(asm, ch.ModuleHash, ch.Version, ch.Tag, ch.Debug)
50+
}
4651
}
4752

4853
func (ch EvictWasm) dirtied() *common.Address {

core/state/statedb.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,7 @@ func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool) (common.Hash, er
12491249
storageTrieNodesDeleted int
12501250
nodes = trienode.NewMergedNodeSet()
12511251
codeWriter = s.db.DiskDB().NewBatch()
1252+
wasmCodeWriter = s.db.WasmStore().NewBatch()
12521253
)
12531254
// Handle all state deletions first
12541255
incomplete, err := s.handleDestruction(nodes)
@@ -1286,7 +1287,7 @@ func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool) (common.Hash, er
12861287

12871288
// Arbitrum: write Stylus programs to disk
12881289
for moduleHash, info := range s.arbExtraData.activatedWasms {
1289-
rawdb.WriteActivation(codeWriter, moduleHash, info.Asm, info.Module)
1290+
rawdb.WriteActivation(wasmCodeWriter, moduleHash, info.Asm, info.Module)
12901291
}
12911292
if len(s.arbExtraData.activatedWasms) > 0 {
12921293
s.arbExtraData.activatedWasms = make(map[common.Hash]*ActivatedWasm)
@@ -1297,6 +1298,11 @@ func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool) (common.Hash, er
12971298
log.Crit("Failed to commit dirty codes", "error", err)
12981299
}
12991300
}
1301+
if wasmCodeWriter.ValueSize() > 0 {
1302+
if err := wasmCodeWriter.Write(); err != nil {
1303+
log.Crit("Failed to commit dirty stylus codes", "error", err)
1304+
}
1305+
}
13001306
// Write the account trie changes, measuring the amount of wasted time
13011307
var start time.Time
13021308
if metrics.EnabledExpensive {

core/state/statedb_arbitrum.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/ethereum/go-ethereum/common"
2929
"github.com/ethereum/go-ethereum/common/lru"
3030
"github.com/ethereum/go-ethereum/core/types"
31+
"github.com/ethereum/go-ethereum/log"
3132
"github.com/ethereum/go-ethereum/rlp"
3233
"github.com/ethereum/go-ethereum/trie"
3334
)
@@ -89,16 +90,12 @@ func (s *StateDB) ActivateWasm(moduleHash common.Hash, asm, module []byte) {
8990
})
9091
}
9192

92-
func (s *StateDB) GetActivatedAsm(moduleHash common.Hash) []byte {
93+
func (s *StateDB) TryGetActivatedAsm(moduleHash common.Hash) ([]byte, error) {
9394
info, exists := s.arbExtraData.activatedWasms[moduleHash]
9495
if exists {
95-
return info.Asm
96+
return info.Asm, nil
9697
}
97-
asm, err := s.db.ActivatedAsm(moduleHash)
98-
if err != nil {
99-
s.setError(fmt.Errorf("failed to load asm for %x: %v", moduleHash, err))
100-
}
101-
return asm
98+
return s.db.ActivatedAsm(moduleHash)
10299
}
103100

104101
func (s *StateDB) GetActivatedModule(moduleHash common.Hash) []byte {
@@ -237,9 +234,13 @@ func (s *StateDB) StartRecording() {
237234
}
238235

239236
func (s *StateDB) RecordProgram(moduleHash common.Hash) {
237+
asm, err := s.TryGetActivatedAsm(moduleHash)
238+
if err != nil {
239+
log.Crit("can't find activated wasm while recording", "modulehash", moduleHash)
240+
}
240241
if s.arbExtraData.userWasms != nil {
241242
s.arbExtraData.userWasms[moduleHash] = ActivatedWasm{
242-
Asm: s.GetActivatedAsm(moduleHash),
243+
Asm: asm,
243244
Module: s.GetActivatedModule(moduleHash),
244245
}
245246
}

core/vm/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
type StateDB interface {
3131
// Arbitrum: manage Stylus wasms
3232
ActivateWasm(moduleHash common.Hash, asm, module []byte)
33-
GetActivatedAsm(moduleHash common.Hash) (asm []byte)
33+
TryGetActivatedAsm(moduleHash common.Hash) (asm []byte, err error)
3434
GetActivatedModule(moduleHash common.Hash) (module []byte)
3535
RecordCacheWasm(wasm state.CacheWasm)
3636
RecordEvictWasm(wasm state.EvictWasm)

0 commit comments

Comments
 (0)