Skip to content

Commit 247b930

Browse files
authored
Merge pull request #317 from OffchainLabs/wasmstore
Separate Wasmstore from normal db: initial
2 parents 8512c12 + 1ffc2da commit 247b930

File tree

13 files changed

+81
-22
lines changed

13 files changed

+81
-22
lines changed

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()))
279273
var prevRoot common.Hash
280274
if lastBlockHeader != nil {
281275
prevRoot = lastBlockHeader.Root

core/rawdb/database.go

Lines changed: 32 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 {
47+
return frdb
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,39 @@ 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 {
175+
return db
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+
}
188+
189+
func (db *dbWithWasmEntry) WasmDataBase() ethdb.KeyValueStore {
190+
return db.wasmDb
191+
}
192+
193+
func (db *dbWithWasmEntry) Close() error {
194+
dbErr := db.Database.Close()
195+
wasmErr := db.wasmDb.Close()
196+
if dbErr != nil {
197+
return dbErr
198+
}
199+
return wasmErr
200+
}
201+
202+
func WrapDatabaseWithWasm(db ethdb.Database, wasm ethdb.KeyValueStore) ethdb.Database {
203+
return &dbWithWasmEntry{db, wasm}
204+
}
205+
174206
// resolveChainFreezerDir is a helper function which resolves the absolute path
175207
// of chain freezer by considering backward compatibility.
176208
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 {
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ 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
5758

5859
// OpenTrie opens the main account trie.
5960
OpenTrie(root common.Hash) (Trie, error)
@@ -164,6 +165,7 @@ func NewDatabaseWithConfig(db ethdb.Database, config *trie.Config) Database {
164165
activatedModuleCache: lru.NewSizeConstrainedCache[common.Hash, []byte](activatedWasmCacheSize),
165166

166167
disk: db,
168+
wasmdb: db.WasmDataBase(),
167169
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
168170
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
169171
triedb: trie.NewDatabase(db, config),
@@ -179,6 +181,7 @@ func NewDatabaseWithNodeDB(db ethdb.Database, triedb *trie.Database) Database {
179181
activatedModuleCache: lru.NewSizeConstrainedCache[common.Hash, []byte](activatedWasmCacheSize),
180182

181183
disk: db,
184+
wasmdb: db.WasmDataBase(),
182185
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
183186
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
184187
triedb: triedb,
@@ -192,11 +195,16 @@ type cachingDB struct {
192195
activatedModuleCache *lru.SizeConstrainedCache[common.Hash, []byte]
193196

194197
disk ethdb.KeyValueStore
198+
wasmdb ethdb.KeyValueStore
195199
codeSizeCache *lru.Cache[common.Hash, int]
196200
codeCache *lru.SizeConstrainedCache[common.Hash, []byte]
197201
triedb *trie.Database
198202
}
199203

204+
func (db *cachingDB) WasmStore() ethdb.KeyValueStore {
205+
return db.wasmdb
206+
}
207+
200208
// OpenTrie opens the main account trie at a specific root hash.
201209
func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
202210
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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ type EvictWasm struct {
4141
}
4242

4343
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)
44+
asm, err := s.TryGetActivatedAsm(ch.ModuleHash) // only happens in native mode
45+
if err == nil && len(asm) != 0 {
46+
//if we failed to get it - it's not in the current rust cache
47+
CacheWasmRust(asm, ch.ModuleHash, ch.Version, ch.Debug)
48+
}
4649
}
4750

4851
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)

ethdb/database.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ type AncientStore interface {
178178
io.Closer
179179
}
180180

181+
type WasmDataBaseRetriever interface {
182+
WasmDataBase() KeyValueStore
183+
}
184+
181185
// Database contains all the methods required by the high level database to not
182186
// only access the key-value data store but also the chain freezer.
183187
type Database interface {
@@ -189,4 +193,5 @@ type Database interface {
189193
Compacter
190194
Snapshotter
191195
io.Closer
196+
WasmDataBaseRetriever
192197
}

0 commit comments

Comments
 (0)