Skip to content

Commit

Permalink
Revert "disable accesslist"
Browse files Browse the repository at this point in the history
This reverts commit 4fc5cc7.
  • Loading branch information
codchen committed May 27, 2024
1 parent 4fc5cc7 commit 0b63b4d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 11 deletions.
87 changes: 82 additions & 5 deletions x/evm/state/accesslist.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,73 @@ import (
"github.com/ethereum/go-ethereum/params"
)

// Always returns true for access list check
// 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
}
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{}{}
}
}
return newAl
}

func (s *DBImpl) AddressInAccessList(addr common.Address) bool {
return true
s.k.PrepareReplayedAddr(s.ctx, addr)
_, ok := s.getAccessList().Addresses[addr]
return ok
}

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

func (s *DBImpl) AddAddressToAccessList(addr common.Address) {}
func (s *DBImpl) AddAddressToAccessList(addr common.Address) {
s.k.PrepareReplayedAddr(s.ctx, addr)
al := s.getAccessList()
defer s.saveAccessList(al)
if _, present := al.Addresses[addr]; present {
return
}
al.Addresses[addr] = -1
}

func (s *DBImpl) AddSlotToAccessList(addr common.Address, slot common.Hash) {}
func (s *DBImpl) AddSlotToAccessList(addr common.Address, slot common.Hash) {
s.k.PrepareReplayedAddr(s.ctx, addr)
al := s.getAccessList()
defer s.saveAccessList(al)
idx, addrPresent := al.Addresses[addr]
if !addrPresent || idx == -1 {
// Address not present, or addr present but no slots there
al.Addresses[addr] = len(al.Slots)
slotmap := map[common.Hash]struct{}{slot: {}}
al.Slots = append(al.Slots, slotmap)
return
}
// There is already an (address,slot) mapping
slotmap := al.Slots[idx]
if _, ok := slotmap[slot]; !ok {
slotmap[slot] = struct{}{}
}
}

func (s *DBImpl) Prepare(_ params.Rules, sender, coinbase common.Address, dest *common.Address, precompiles []common.Address, txAccesses ethtypes.AccessList) {
s.k.PrepareReplayedAddr(s.ctx, sender)
Expand All @@ -27,4 +81,27 @@ func (s *DBImpl) Prepare(_ params.Rules, sender, coinbase common.Address, dest *
s.k.PrepareReplayedAddr(s.ctx, *dest)
}
s.Snapshot()
s.AddAddressToAccessList(sender)
if dest != nil {
s.AddAddressToAccessList(*dest)
// If it's a create-tx, the destination will be added inside evm.create
}
for _, addr := range precompiles {
s.AddAddressToAccessList(addr)
}
for _, el := range txAccesses {
s.AddAddressToAccessList(el.Address)
for _, key := range el.StorageKeys {
s.AddSlotToAccessList(el.Address, key)
}
}
s.AddAddressToAccessList(coinbase)
}

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

func (s *DBImpl) saveAccessList(al *accessList) {
s.tempStateCurrent.transientAccessLists = al
}
5 changes: 3 additions & 2 deletions x/evm/state/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ 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}
GasRefundKey = []byte{0x01}
LogsKey = []byte{0x02}
AccessListKey = []byte{0x03}
)

/*
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 = NewTemporaryState(s.tempStateCurrent.transientAccessLists.Copy())
return len(s.snapshottedCtxs) - 1
}

Expand Down
8 changes: 5 additions & 3 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(),
tempStateCurrent: NewTemporaryState(&accessList{Addresses: make(map[common.Address]int), Slots: []map[common.Hash]struct{}{}}),
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(),
tempStateCurrent: NewTemporaryState(s.tempStateCurrent.transientAccessLists.Copy()),
tempStatesHist: append(s.tempStatesHist, s.tempStateCurrent),
k: s.k,
coinbaseAddress: s.coinbaseAddress,
Expand Down Expand Up @@ -199,15 +199,17 @@ type TemporaryState struct {
transientStates map[string]map[string]common.Hash
transientAccounts map[string][]byte
transientModuleStates map[string][]byte
transientAccessLists *accessList
surplus sdk.Int // in wei
}

func NewTemporaryState() *TemporaryState {
func NewTemporaryState(al *accessList) *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,
surplus: utils.Sdk0,
}
}

0 comments on commit 0b63b4d

Please sign in to comment.