Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed May 27, 2024
1 parent 0b63b4d commit 3d77fa8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 32 deletions.
60 changes: 36 additions & 24 deletions x/evm/state/accesslist.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,56 @@ import (
"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
Slots []map[common.Hash]struct{}
}

// deep copy so that changes to a new snapshot won't affect older ones
func (al *accessList) Copy() *accessList {
newAl := &accessList{Addresses: make(map[common.Address]int, len(al.Addresses)), Slots: make([]map[common.Hash]struct{}, 0, len(al.Slots))}
for a, i := range al.Addresses {
newAl.Addresses[a] = i
func (s *DBImpl) AddressInAccessList(addr common.Address) bool {
s.k.PrepareReplayedAddr(s.ctx, addr)
_, ok := s.getCurrentAccessList().Addresses[addr]
if ok {
return true
}
for i, slot := range al.Slots {
newAl.Slots = append(newAl.Slots, make(map[common.Hash]struct{}, len(slot)))
for h := range slot {
newAl.Slots[i][h] = struct{}{}
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 newAl
}

func (s *DBImpl) AddressInAccessList(addr common.Address) bool {
s.k.PrepareReplayedAddr(s.ctx, addr)
_, ok := s.getAccessList().Addresses[addr]
return ok
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
}
}
return ok, false
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 addrOk, false
}

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

func (s *DBImpl) AddSlotToAccessList(addr common.Address, slot common.Hash) {
s.k.PrepareReplayedAddr(s.ctx, addr)
al := s.getAccessList()
al := s.getCurrentAccessList()
defer s.saveAccessList(al)
idx, addrPresent := al.Addresses[addr]
if !addrPresent || idx == -1 {
Expand Down Expand Up @@ -87,6 +95,10 @@ func (s *DBImpl) Prepare(_ params.Rules, sender, coinbase common.Address, dest *
// 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 100 in x/evm/state/accesslist.go

View check run for this annotation

Codecov / codecov/patch

x/evm/state/accesslist.go#L100

Added line #L100 was not covered by tests
}
s.AddAddressToAccessList(addr)
}
for _, el := range txAccesses {
Expand All @@ -98,7 +110,7 @@ func (s *DBImpl) Prepare(_ params.Rules, sender, coinbase common.Address, dest *
s.AddAddressToAccessList(coinbase)
}

func (s *DBImpl) getAccessList() *accessList {
func (s *DBImpl) getCurrentAccessList() *accessList {
return s.tempStateCurrent.transientAccessLists
}

Expand Down
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
2 changes: 1 addition & 1 deletion x/evm/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (s *DBImpl) Snapshot() int {
s.snapshottedCtxs = append(s.snapshottedCtxs, s.ctx)
s.ctx = newCtx
s.tempStatesHist = append(s.tempStatesHist, s.tempStateCurrent)
s.tempStateCurrent = NewTemporaryState(s.tempStateCurrent.transientAccessLists.Copy())
s.tempStateCurrent = NewTemporaryState()
return len(s.snapshottedCtxs) - 1
}

Expand Down
8 changes: 4 additions & 4 deletions x/evm/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewDBImpl(ctx sdk.Context, k EVMKeeper, simulation bool) *DBImpl {
snapshottedCtxs: []sdk.Context{},
coinbaseAddress: GetCoinbaseAddress(ctx.TxIndex()),
simulation: simulation,
tempStateCurrent: NewTemporaryState(&accessList{Addresses: make(map[common.Address]int), Slots: []map[common.Hash]struct{}{}}),
tempStateCurrent: NewTemporaryState(),
coinbaseEvmAddress: feeCollector,
}
s.Snapshot() // take an initial snapshot for GetCommitted
Expand Down Expand Up @@ -131,7 +131,7 @@ func (s *DBImpl) Copy() vm.StateDB {
return &DBImpl{
ctx: newCtx,
snapshottedCtxs: append(s.snapshottedCtxs, s.ctx),
tempStateCurrent: NewTemporaryState(s.tempStateCurrent.transientAccessLists.Copy()),
tempStateCurrent: NewTemporaryState(),
tempStatesHist: append(s.tempStatesHist, s.tempStateCurrent),
k: s.k,
coinbaseAddress: s.coinbaseAddress,
Expand Down Expand Up @@ -203,13 +203,13 @@ type TemporaryState struct {
surplus sdk.Int // in wei
}

func NewTemporaryState(al *accessList) *TemporaryState {
func NewTemporaryState() *TemporaryState {
return &TemporaryState{
logs: []*ethtypes.Log{},
transientStates: make(map[string]map[string]common.Hash),
transientAccounts: make(map[string][]byte),
transientModuleStates: make(map[string][]byte),
transientAccessLists: al,
transientAccessLists: &accessList{Addresses: make(map[common.Address]int), Slots: []map[common.Hash]struct{}{}},
surplus: utils.Sdk0,
}
}

0 comments on commit 3d77fa8

Please sign in to comment.