Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace MessageRunMode with a struct #373

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
11 changes: 1 addition & 10 deletions arbitrum/apibackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ var (
type APIBackend struct {
b *Backend

dbForAPICalls ethdb.Database

fallbackClient types.FallbackClient
sync SyncProgressBackend
}
Expand Down Expand Up @@ -103,15 +101,8 @@ func createRegisterAPIBackend(backend *Backend, filterConfig filters.Config, fal
if err != nil {
return nil, err
}
// discard stylus-tag on any call made from api database
dbForAPICalls := backend.chainDb
wasmStore, tag := backend.chainDb.WasmDataBase()
if tag != 0 || len(backend.chainDb.WasmTargets()) > 1 {
dbForAPICalls = rawdb.WrapDatabaseWithWasm(backend.chainDb, wasmStore, 0, []ethdb.WasmTarget{rawdb.LocalTarget()})
}
backend.apiBackend = &APIBackend{
b: backend,
dbForAPICalls: dbForAPICalls,
fallbackClient: fallbackClient,
}
filterSystem := filters.NewFilterSystem(backend.apiBackend, filterConfig)
Expand Down Expand Up @@ -331,7 +322,7 @@ func (a *APIBackend) BlobBaseFee(ctx context.Context) *big.Int {
}

func (a *APIBackend) ChainDb() ethdb.Database {
return a.dbForAPICalls
return a.b.chainDb
}

func (a *APIBackend) AccountManager() *accounts.Manager {
Expand Down
2 changes: 1 addition & 1 deletion arbitrum/recordingdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func (r *RecordingDatabase) PrepareRecording(ctx context.Context, lastBlockHeade
defer func() { r.Dereference(finalDereference) }()
recordingKeyValue := newRecordingKV(r.db.TrieDB(), r.db.DiskDB())

recordingStateDatabase := state.NewDatabase(rawdb.WrapDatabaseWithWasm(rawdb.NewDatabase(recordingKeyValue), r.db.WasmStore(), 0, r.db.WasmTargets()))
recordingStateDatabase := state.NewDatabase(rawdb.WrapDatabaseWithWasm(rawdb.NewDatabase(recordingKeyValue), r.db.WasmStore()))
var prevRoot common.Hash
if lastBlockHeader != nil {
prevRoot = lastBlockHeader.Root
Expand Down
2 changes: 1 addition & 1 deletion cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
rejectedTxs = append(rejectedTxs, &rejectedTx{i, errMsg})
continue
}
msg, err := core.TransactionToMessage(tx, signer, pre.Env.BaseFee, core.MessageCommitMode)
msg, err := core.TransactionToMessage(tx, signer, pre.Env.BaseFee, core.NewMessageCommitContext(nil)) // TODO: do we need to specify wasm targets here if chainConfig.IsArbitrum == false?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to specify wasm targets here if chainConfig.IsArbitrum == false?

if err != nil {
log.Warn("rejected tx", "index", i, "hash", tx.Hash(), "error", err)
rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()})
Expand Down
22 changes: 12 additions & 10 deletions core/rawdb/accessors_state_arbitrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ import (
"github.com/ethereum/go-ethereum/log"
)

type WasmTarget string

const (
TargetWavm ethdb.WasmTarget = "wavm"
TargetArm64 ethdb.WasmTarget = "arm64"
TargetAmd64 ethdb.WasmTarget = "amd64"
TargetHost ethdb.WasmTarget = "host"
TargetWavm WasmTarget = "wavm"
TargetArm64 WasmTarget = "arm64"
TargetAmd64 WasmTarget = "amd64"
TargetHost WasmTarget = "host"
)

func LocalTarget() ethdb.WasmTarget {
func LocalTarget() WasmTarget {
if runtime.GOOS == "linux" {
switch runtime.GOARCH {
case "arm64":
Expand All @@ -44,7 +46,7 @@ func LocalTarget() ethdb.WasmTarget {
return TargetHost
}

func activatedAsmKeyPrefix(target ethdb.WasmTarget) (WasmPrefix, error) {
func activatedAsmKeyPrefix(target WasmTarget) (WasmPrefix, error) {
var prefix WasmPrefix
switch target {
case TargetWavm:
Expand All @@ -61,19 +63,19 @@ func activatedAsmKeyPrefix(target ethdb.WasmTarget) (WasmPrefix, error) {
return prefix, nil
}

func IsSupportedWasmTarget(target ethdb.WasmTarget) bool {
func IsSupportedWasmTarget(target WasmTarget) bool {
_, err := activatedAsmKeyPrefix(target)
return err == nil
}

func WriteActivation(db ethdb.KeyValueWriter, moduleHash common.Hash, asmMap map[ethdb.WasmTarget][]byte) {
func WriteActivation(db ethdb.KeyValueWriter, moduleHash common.Hash, asmMap map[WasmTarget][]byte) {
for target, asm := range asmMap {
WriteActivatedAsm(db, target, moduleHash, asm)
}
}

// Stores the activated asm for a given moduleHash and target
func WriteActivatedAsm(db ethdb.KeyValueWriter, target ethdb.WasmTarget, moduleHash common.Hash, asm []byte) {
func WriteActivatedAsm(db ethdb.KeyValueWriter, target WasmTarget, moduleHash common.Hash, asm []byte) {
prefix, err := activatedAsmKeyPrefix(target)
if err != nil {
log.Crit("Failed to store activated wasm asm", "err", err)
Expand All @@ -85,7 +87,7 @@ func WriteActivatedAsm(db ethdb.KeyValueWriter, target ethdb.WasmTarget, moduleH
}

// Retrieves the activated asm for a given moduleHash and target
func ReadActivatedAsm(db ethdb.KeyValueReader, target ethdb.WasmTarget, moduleHash common.Hash) []byte {
func ReadActivatedAsm(db ethdb.KeyValueReader, target WasmTarget, moduleHash common.Hash) []byte {
prefix, err := activatedAsmKeyPrefix(target)
if err != nil {
log.Crit("Failed to read activated wasm asm", "err", err)
Expand Down
28 changes: 11 additions & 17 deletions core/rawdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ type freezerdb struct {
ethdb.AncientStore
}

func (frdb *freezerdb) WasmDataBase() (ethdb.KeyValueStore, uint32) {
return frdb, 0
func (frdb *freezerdb) WasmDataBase() ethdb.KeyValueStore {
return frdb
}

func (frdb *freezerdb) WasmTargets() []ethdb.WasmTarget {
func (frdb *freezerdb) WasmTargets() []WasmTarget {
return nil
}

Expand Down Expand Up @@ -166,11 +166,11 @@ func (db *nofreezedb) AncientDatadir() (string, error) {
return "", errNotSupported
}

func (db *nofreezedb) WasmDataBase() (ethdb.KeyValueStore, uint32) {
return db, 0
func (db *nofreezedb) WasmDataBase() ethdb.KeyValueStore {
return db
}

func (db *nofreezedb) WasmTargets() []ethdb.WasmTarget {
func (db *nofreezedb) WasmTargets() []WasmTarget {
return nil
}

Expand All @@ -182,17 +182,11 @@ func NewDatabase(db ethdb.KeyValueStore) ethdb.Database {

type dbWithWasmEntry struct {
ethdb.Database
wasmDb ethdb.KeyValueStore
wasmCacheTag uint32
wasmTargets []ethdb.WasmTarget
wasmDb ethdb.KeyValueStore
}

func (db *dbWithWasmEntry) WasmDataBase() (ethdb.KeyValueStore, uint32) {
return db.wasmDb, db.wasmCacheTag
}

func (db *dbWithWasmEntry) WasmTargets() []ethdb.WasmTarget {
return db.wasmTargets
func (db *dbWithWasmEntry) WasmDataBase() ethdb.KeyValueStore {
return db.wasmDb
}

func (db *dbWithWasmEntry) Close() error {
Expand All @@ -204,8 +198,8 @@ func (db *dbWithWasmEntry) Close() error {
return wasmErr
}

func WrapDatabaseWithWasm(db ethdb.Database, wasm ethdb.KeyValueStore, cacheTag uint32, targets []ethdb.WasmTarget) ethdb.Database {
return &dbWithWasmEntry{db, wasm, cacheTag, targets}
func WrapDatabaseWithWasm(db ethdb.Database, wasm ethdb.KeyValueStore) ethdb.Database {
return &dbWithWasmEntry{db, wasm}
}

// resolveChainFreezerDir is a helper function which resolves the absolute path
Expand Down
6 changes: 1 addition & 5 deletions core/rawdb/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,10 @@ func (t *table) Close() error {
return nil
}

func (t *table) WasmDataBase() (ethdb.KeyValueStore, uint32) {
func (t *table) WasmDataBase() ethdb.KeyValueStore {
return t.db.WasmDataBase()
}

func (t *table) WasmTargets() []ethdb.WasmTarget {
return t.db.WasmTargets()
}

// Has retrieves if a prefixed version of a key is present in the database.
func (t *table) Has(key []byte) (bool, error) {
return t.db.Has(append([]byte(t.prefix), key...))
Expand Down
32 changes: 7 additions & 25 deletions core/state/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ const (
// Database wraps access to tries and contract code.
type Database interface {
// Arbitrum: Read activated Stylus contracts
ActivatedAsm(target ethdb.WasmTarget, moduleHash common.Hash) (asm []byte, err error)
ActivatedAsm(target rawdb.WasmTarget, moduleHash common.Hash) (asm []byte, err error)
WasmStore() ethdb.KeyValueStore
WasmCacheTag() uint32
WasmTargets() []ethdb.WasmTarget

// OpenTrie opens the main account trie.
OpenTrie(root common.Hash) (Trie, error)
Expand Down Expand Up @@ -161,15 +159,12 @@ func NewDatabase(db ethdb.Database) Database {
// is safe for concurrent use and retains a lot of collapsed RLP trie nodes in a
// large memory cache.
func NewDatabaseWithConfig(db ethdb.Database, config *triedb.Config) Database {
wasmdb, wasmTag := db.WasmDataBase()
cdb := &cachingDB{
// Arbitrum only
activatedAsmCache: lru.NewSizeConstrainedCache[activatedAsmCacheKey, []byte](activatedWasmCacheSize),
wasmTag: wasmTag,
wasmDatabaseRetriever: db,
activatedAsmCache: lru.NewSizeConstrainedCache[activatedAsmCacheKey, []byte](activatedWasmCacheSize),

disk: db,
wasmdb: wasmdb,
wasmdb: db.WasmDataBase(),
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
triedb: triedb.NewDatabase(db, config),
Expand All @@ -179,15 +174,12 @@ func NewDatabaseWithConfig(db ethdb.Database, config *triedb.Config) Database {

// NewDatabaseWithNodeDB creates a state database with an already initialized node database.
func NewDatabaseWithNodeDB(db ethdb.Database, triedb *triedb.Database) Database {
wasmdb, wasmTag := db.WasmDataBase()
cdb := &cachingDB{
// Arbitrum only
activatedAsmCache: lru.NewSizeConstrainedCache[activatedAsmCacheKey, []byte](activatedWasmCacheSize),
wasmTag: wasmTag,
wasmDatabaseRetriever: db,
activatedAsmCache: lru.NewSizeConstrainedCache[activatedAsmCacheKey, []byte](activatedWasmCacheSize),

disk: db,
wasmdb: wasmdb,
wasmdb: db.WasmDataBase(),
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
triedb: triedb,
Expand All @@ -197,14 +189,12 @@ func NewDatabaseWithNodeDB(db ethdb.Database, triedb *triedb.Database) Database

type activatedAsmCacheKey struct {
moduleHash common.Hash
target ethdb.WasmTarget
target rawdb.WasmTarget
}

type cachingDB struct {
// Arbitrum
activatedAsmCache *lru.SizeConstrainedCache[activatedAsmCacheKey, []byte]
wasmTag uint32
wasmDatabaseRetriever ethdb.WasmDataBaseRetriever
activatedAsmCache *lru.SizeConstrainedCache[activatedAsmCacheKey, []byte]

disk ethdb.KeyValueStore
wasmdb ethdb.KeyValueStore
Expand All @@ -217,14 +207,6 @@ func (db *cachingDB) WasmStore() ethdb.KeyValueStore {
return db.wasmdb
}

func (db *cachingDB) WasmCacheTag() uint32 {
return db.wasmTag
}

func (db *cachingDB) WasmTargets() []ethdb.WasmTarget {
return db.wasmDatabaseRetriever.WasmTargets()
}

// OpenTrie opens the main account trie at a specific root hash.
func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
if db.triedb.IsVerkle() {
Expand Down
3 changes: 1 addition & 2 deletions core/state/database_arbitrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/ethdb"
)

func (db *cachingDB) ActivatedAsm(target ethdb.WasmTarget, moduleHash common.Hash) ([]byte, error) {
func (db *cachingDB) ActivatedAsm(target rawdb.WasmTarget, moduleHash common.Hash) ([]byte, error) {
cacheKey := activatedAsmCacheKey{moduleHash, target}
if asm, _ := db.activatedAsmCache.Get(cacheKey); len(asm) > 0 {
return asm, nil
Expand Down
14 changes: 7 additions & 7 deletions core/state/statedb_arbitrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/lru"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
Expand All @@ -50,7 +50,7 @@ var (
StylusDiscriminant = []byte{stylusEOFMagic, stylusEOFMagicSuffix, stylusEOFVersion}
)

type ActivatedWasm map[ethdb.WasmTarget][]byte
type ActivatedWasm map[rawdb.WasmTarget][]byte

// checks if a valid Stylus prefix is present
func IsStylusProgram(b []byte) bool {
Expand All @@ -74,7 +74,7 @@ func NewStylusPrefix(dictionary byte) []byte {
return append(prefix, dictionary)
}

func (s *StateDB) ActivateWasm(moduleHash common.Hash, asmMap map[ethdb.WasmTarget][]byte) {
func (s *StateDB) ActivateWasm(moduleHash common.Hash, asmMap map[rawdb.WasmTarget][]byte) {
_, exists := s.arbExtraData.activatedWasms[moduleHash]
if exists {
return
Expand All @@ -85,7 +85,7 @@ func (s *StateDB) ActivateWasm(moduleHash common.Hash, asmMap map[ethdb.WasmTarg
})
}

func (s *StateDB) TryGetActivatedAsm(target ethdb.WasmTarget, moduleHash common.Hash) ([]byte, error) {
func (s *StateDB) TryGetActivatedAsm(target rawdb.WasmTarget, moduleHash common.Hash) ([]byte, error) {
asmMap, exists := s.arbExtraData.activatedWasms[moduleHash]
if exists {
if asm, exists := asmMap[target]; exists {
Expand All @@ -95,7 +95,7 @@ func (s *StateDB) TryGetActivatedAsm(target ethdb.WasmTarget, moduleHash common.
return s.db.ActivatedAsm(target, moduleHash)
}

func (s *StateDB) TryGetActivatedAsmMap(targets []ethdb.WasmTarget, moduleHash common.Hash) (map[ethdb.WasmTarget][]byte, error) {
func (s *StateDB) TryGetActivatedAsmMap(targets []rawdb.WasmTarget, moduleHash common.Hash) (map[rawdb.WasmTarget][]byte, error) {
asmMap := s.arbExtraData.activatedWasms[moduleHash]
if asmMap != nil {
for _, target := range targets {
Expand All @@ -106,7 +106,7 @@ func (s *StateDB) TryGetActivatedAsmMap(targets []ethdb.WasmTarget, moduleHash c
return asmMap, nil
}
var err error
asmMap = make(map[ethdb.WasmTarget][]byte, len(targets))
asmMap = make(map[rawdb.WasmTarget][]byte, len(targets))
for _, target := range targets {
asm, dbErr := s.db.ActivatedAsm(target, moduleHash)
if dbErr == nil {
Expand Down Expand Up @@ -241,7 +241,7 @@ func (s *StateDB) StartRecording() {
s.arbExtraData.userWasms = make(UserWasms)
}

func (s *StateDB) RecordProgram(targets []ethdb.WasmTarget, moduleHash common.Hash) {
func (s *StateDB) RecordProgram(targets []rawdb.WasmTarget, moduleHash common.Hash) {
if len(targets) == 0 {
// nothing to record
return
Expand Down
3 changes: 2 additions & 1 deletion core/state_prefetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ func (p *statePrefetcher) Prefetch(block *types.Block, statedb *state.StateDB, c
)
// Iterate over and process the individual transactions
byzantium := p.config.IsByzantium(block.Number())
runCtx := NewMessageEthcallContext()
for i, tx := range block.Transactions() {
// If block precaching was interrupted, abort
if interrupt != nil && interrupt.Load() {
return
}
// Convert the transaction into an executable message and pre-cache its sender
msg, err := TransactionToMessage(tx, signer, header.BaseFee, MessageEthcallMode)
msg, err := TransactionToMessage(tx, signer, header.BaseFee, runCtx)
if err != nil {
return // Also invalid block, bail out
}
Expand Down
10 changes: 6 additions & 4 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/misc"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
Expand Down Expand Up @@ -79,9 +80,10 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
if beaconRoot := block.BeaconRoot(); beaconRoot != nil {
ProcessBeaconBlockRoot(*beaconRoot, vmenv, statedb)
}
runCtx := NewMessageReplayContext([]rawdb.WasmTarget{rawdb.LocalTarget()})
// Iterate over and process the individual transactions
for i, tx := range block.Transactions() {
msg, err := TransactionToMessage(tx, signer, header.BaseFee, MessageReplayMode)
msg, err := TransactionToMessage(tx, signer, header.BaseFee, runCtx)
if err != nil {
return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err)
}
Expand Down Expand Up @@ -179,11 +181,11 @@ func ApplyTransactionWithEVM(msg *Message, config *params.ChainConfig, gp *GasPo
// for the transaction, gas used and an error if the transaction failed,
// indicating the block was invalid.
func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, *ExecutionResult, error) {
return ApplyTransactionWithResultFilter(config, bc, author, gp, statedb, header, tx, usedGas, cfg, MessageReplayMode, nil)
return ApplyTransactionWithResultFilter(config, bc, author, gp, statedb, header, tx, usedGas, cfg, NewMessageReplayContext([]rawdb.WasmTarget{rawdb.LocalTarget()}), nil)
}

func ApplyTransactionWithResultFilter(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config, runMode MessageRunMode, resultFilter func(*ExecutionResult) error) (*types.Receipt, *ExecutionResult, error) {
msg, err := TransactionToMessage(tx, types.MakeSigner(config, header.Number, header.Time), header.BaseFee, runMode)
func ApplyTransactionWithResultFilter(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config, runCtx *MessageRunContext, resultFilter func(*ExecutionResult) error) (*types.Receipt, *ExecutionResult, error) {
msg, err := TransactionToMessage(tx, types.MakeSigner(config, header.Number, header.Time), header.BaseFee, runCtx)
if err != nil {
return nil, nil, err
}
Expand Down
Loading
Loading