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

remove serialize/deserialize for accesslist #1701

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions x/evm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (server msgServer) EVMTransaction(goCtx context.Context, msg *types.MsgEVMT
gp := server.GetGasPool()

defer func() {
defer stateDB.Cleanup()
if pe := recover(); pe != nil {
// there is not supposed to be any panic
debug.PrintStack()
Expand Down
73 changes: 40 additions & 33 deletions x/evm/state/accesslist.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,61 @@
package state

import (
"encoding/json"

"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
)

// all custom precompiles have an address greater than or equal to this address
var CustomPrecompileStartingAddr = common.HexToAddress("0x0000000000000000000000000000000000001001")

// Forked from go-ethereum, except journaling logic which is unnecessary with cacheKV

type accessList struct {
Addresses map[common.Address]int `json:"addresses"`
Slots []map[common.Hash]struct{} `json:"slots"`
Addresses map[common.Address]int
Slots []map[common.Hash]struct{}
}

func (s *DBImpl) AddressInAccessList(addr common.Address) bool {
s.k.PrepareReplayedAddr(s.ctx, addr)
_, ok := s.getAccessList().Addresses[addr]
return ok
_, ok := s.getCurrentAccessList().Addresses[addr]
if ok {
return true
}
for _, ts := range s.tempStatesHist {
if _, ok := ts.transientAccessLists.Addresses[addr]; ok {
return true
}

Check warning on line 28 in x/evm/state/accesslist.go

View check run for this annotation

Codecov / codecov/patch

x/evm/state/accesslist.go#L27-L28

Added lines #L27 - L28 were not covered by tests
}
return false
}

func (s *DBImpl) SlotInAccessList(addr common.Address, slot common.Hash) (addressOk bool, slotOk bool) {
s.k.PrepareReplayedAddr(s.ctx, addr)
al := s.getAccessList()
idx, ok := al.Addresses[addr]
if ok && idx != -1 {
al := s.getCurrentAccessList()
idx, addrOk := al.Addresses[addr]
if addrOk && idx != -1 {
_, slotOk := al.Slots[idx][slot]
return ok, slotOk
if slotOk {
return true, true
}
}
for _, ts := range s.tempStatesHist {
idx, ok := ts.transientAccessLists.Addresses[addr]
addrOk = addrOk || ok
if ok && idx != -1 {
_, slotOk := ts.transientAccessLists.Slots[idx][slot]
if slotOk {
return true, true
}

Check warning on line 50 in x/evm/state/accesslist.go

View check run for this annotation

Codecov / codecov/patch

x/evm/state/accesslist.go#L47-L50

Added lines #L47 - L50 were not covered by tests
}
}
return ok, false
return addrOk, false
}

func (s *DBImpl) AddAddressToAccessList(addr common.Address) {
s.k.PrepareReplayedAddr(s.ctx, addr)
al := s.getAccessList()
defer s.saveAccessList(al)
al := s.getCurrentAccessList()
if _, present := al.Addresses[addr]; present {
return
}
Expand All @@ -44,8 +64,7 @@

func (s *DBImpl) AddSlotToAccessList(addr common.Address, slot common.Hash) {
s.k.PrepareReplayedAddr(s.ctx, addr)
al := s.getAccessList()
defer s.saveAccessList(al)
al := s.getCurrentAccessList()
idx, addrPresent := al.Addresses[addr]
if !addrPresent || idx == -1 {
// Address not present, or addr present but no slots there
Expand Down Expand Up @@ -74,6 +93,10 @@
// If it's a create-tx, the destination will be added inside evm.create
}
for _, addr := range precompiles {
// skip any custom precompile
if addr.Cmp(CustomPrecompileStartingAddr) >= 0 {
continue

Check warning on line 98 in x/evm/state/accesslist.go

View check run for this annotation

Codecov / codecov/patch

x/evm/state/accesslist.go#L98

Added line #L98 was not covered by tests
}
s.AddAddressToAccessList(addr)
}
for _, el := range txAccesses {
Expand All @@ -85,22 +108,6 @@
s.AddAddressToAccessList(coinbase)
}

func (s *DBImpl) getAccessList() *accessList {
bz, found := s.getTransientModule(AccessListKey)
al := accessList{Addresses: make(map[common.Address]int)}
if !found || bz == nil {
return &al
}
if err := json.Unmarshal(bz, &al); err != nil {
panic(err)
}
return &al
}

func (s *DBImpl) saveAccessList(al *accessList) {
albz, err := json.Marshal(al)
if err != nil {
panic(err)
}
s.tempStateCurrent.transientModuleStates[string(AccessListKey)] = albz
func (s *DBImpl) getCurrentAccessList() *accessList {
return s.tempStateCurrent.transientAccessLists
}
5 changes: 2 additions & 3 deletions x/evm/state/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ var (
// If evm module balance is higher than this value at the end of
// the transaction, we need to burn from module balance in order
// for this number to align.
GasRefundKey = []byte{0x01}
LogsKey = []byte{0x02}
AccessListKey = []byte{0x03}
GasRefundKey = []byte{0x01}
LogsKey = []byte{0x02}
)

/*
Expand Down
9 changes: 9 additions & 0 deletions x/evm/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@
// to the database.
func (s *DBImpl) AddPreimage(_ common.Hash, _ []byte) {}

func (s *DBImpl) Cleanup() {
s.tempStateCurrent = nil
s.tempStatesHist = []*TemporaryState{}
s.logger = nil
s.snapshottedCtxs = nil

Check warning on line 88 in x/evm/state/statedb.go

View check run for this annotation

Codecov / codecov/patch

x/evm/state/statedb.go#L84-L88

Added lines #L84 - L88 were not covered by tests
}

func (s *DBImpl) Finalize() (surplus sdk.Int, err error) {
if s.simulation {
panic("should never call finalize on a simulation DB")
Expand Down Expand Up @@ -199,6 +206,7 @@
transientStates map[string]map[string]common.Hash
transientAccounts map[string][]byte
transientModuleStates map[string][]byte
transientAccessLists *accessList
surplus sdk.Int // in wei
}

Expand All @@ -208,6 +216,7 @@
transientStates: make(map[string]map[string]common.Hash),
transientAccounts: make(map[string][]byte),
transientModuleStates: make(map[string][]byte),
transientAccessLists: &accessList{Addresses: make(map[common.Address]int), Slots: []map[common.Hash]struct{}{}},
surplus: utils.Sdk0,
}
}
Loading