Skip to content

Commit 1eed72d

Browse files
committed
optimize
1 parent 35f459b commit 1eed72d

File tree

4 files changed

+47
-32
lines changed

4 files changed

+47
-32
lines changed

x/evm/state/accesslist.go

+36-24
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,56 @@ import (
66
"github.com/ethereum/go-ethereum/params"
77
)
88

9+
// all custom precompiles have an address greater than or equal to this address
10+
var CustomPrecompileStartingAddr = common.HexToAddress("0x0000000000000000000000000000000000001001")
11+
912
// Forked from go-ethereum, except journaling logic which is unnecessary with cacheKV
1013

1114
type accessList struct {
1215
Addresses map[common.Address]int
1316
Slots []map[common.Hash]struct{}
1417
}
1518

16-
// deep copy so that changes to a new snapshot won't affect older ones
17-
func (al *accessList) Copy() *accessList {
18-
newAl := &accessList{Addresses: make(map[common.Address]int, len(al.Addresses)), Slots: make([]map[common.Hash]struct{}, 0, len(al.Slots))}
19-
for a, i := range al.Addresses {
20-
newAl.Addresses[a] = i
19+
func (s *DBImpl) AddressInAccessList(addr common.Address) bool {
20+
s.k.PrepareReplayedAddr(s.ctx, addr)
21+
_, ok := s.getCurrentAccessList().Addresses[addr]
22+
if ok {
23+
return true
2124
}
22-
for i, slot := range al.Slots {
23-
newAl.Slots = append(newAl.Slots, make(map[common.Hash]struct{}, len(slot)))
24-
for h := range slot {
25-
newAl.Slots[i][h] = struct{}{}
25+
for _, ts := range s.tempStatesHist {
26+
if _, ok := ts.transientAccessLists.Addresses[addr]; ok {
27+
return true
2628
}
2729
}
28-
return newAl
29-
}
30-
31-
func (s *DBImpl) AddressInAccessList(addr common.Address) bool {
32-
s.k.PrepareReplayedAddr(s.ctx, addr)
33-
_, ok := s.getAccessList().Addresses[addr]
34-
return ok
30+
return false
3531
}
3632

3733
func (s *DBImpl) SlotInAccessList(addr common.Address, slot common.Hash) (addressOk bool, slotOk bool) {
3834
s.k.PrepareReplayedAddr(s.ctx, addr)
39-
al := s.getAccessList()
40-
idx, ok := al.Addresses[addr]
41-
if ok && idx != -1 {
35+
al := s.getCurrentAccessList()
36+
idx, addrOk := al.Addresses[addr]
37+
if addrOk && idx != -1 {
4238
_, slotOk := al.Slots[idx][slot]
43-
return ok, slotOk
39+
if slotOk {
40+
return true, true
41+
}
4442
}
45-
return ok, false
43+
for _, ts := range s.tempStatesHist {
44+
idx, ok := ts.transientAccessLists.Addresses[addr]
45+
addrOk = addrOk || ok
46+
if ok && idx != -1 {
47+
_, slotOk := ts.transientAccessLists.Slots[idx][slot]
48+
if slotOk {
49+
return true, true
50+
}
51+
}
52+
}
53+
return addrOk, false
4654
}
4755

4856
func (s *DBImpl) AddAddressToAccessList(addr common.Address) {
4957
s.k.PrepareReplayedAddr(s.ctx, addr)
50-
al := s.getAccessList()
58+
al := s.getCurrentAccessList()
5159
defer s.saveAccessList(al)
5260
if _, present := al.Addresses[addr]; present {
5361
return
@@ -57,7 +65,7 @@ func (s *DBImpl) AddAddressToAccessList(addr common.Address) {
5765

5866
func (s *DBImpl) AddSlotToAccessList(addr common.Address, slot common.Hash) {
5967
s.k.PrepareReplayedAddr(s.ctx, addr)
60-
al := s.getAccessList()
68+
al := s.getCurrentAccessList()
6169
defer s.saveAccessList(al)
6270
idx, addrPresent := al.Addresses[addr]
6371
if !addrPresent || idx == -1 {
@@ -87,6 +95,10 @@ func (s *DBImpl) Prepare(_ params.Rules, sender, coinbase common.Address, dest *
8795
// If it's a create-tx, the destination will be added inside evm.create
8896
}
8997
for _, addr := range precompiles {
98+
// skip any custom precompile
99+
if addr.Cmp(CustomPrecompileStartingAddr) >= 0 {
100+
continue
101+
}
90102
s.AddAddressToAccessList(addr)
91103
}
92104
for _, el := range txAccesses {
@@ -98,7 +110,7 @@ func (s *DBImpl) Prepare(_ params.Rules, sender, coinbase common.Address, dest *
98110
s.AddAddressToAccessList(coinbase)
99111
}
100112

101-
func (s *DBImpl) getAccessList() *accessList {
113+
func (s *DBImpl) getCurrentAccessList() *accessList {
102114
return s.tempStateCurrent.transientAccessLists
103115
}
104116

x/evm/state/keys.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ var (
99
// If evm module balance is higher than this value at the end of
1010
// the transaction, we need to burn from module balance in order
1111
// for this number to align.
12-
GasRefundKey = []byte{0x01}
13-
LogsKey = []byte{0x02}
14-
AccessListKey = []byte{0x03}
12+
GasRefundKey = []byte{0x01}
13+
LogsKey = []byte{0x02}
1514
)
1615

1716
/*

x/evm/state/state.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (s *DBImpl) Snapshot() int {
9797
s.snapshottedCtxs = append(s.snapshottedCtxs, s.ctx)
9898
s.ctx = newCtx
9999
s.tempStatesHist = append(s.tempStatesHist, s.tempStateCurrent)
100-
s.tempStateCurrent = NewTemporaryState(s.tempStateCurrent.transientAccessLists.Copy())
100+
s.tempStateCurrent = NewTemporaryState()
101101
return len(s.snapshottedCtxs) - 1
102102
}
103103

x/evm/state/statedb.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func NewDBImpl(ctx sdk.Context, k EVMKeeper, simulation bool) *DBImpl {
4646
snapshottedCtxs: []sdk.Context{},
4747
coinbaseAddress: GetCoinbaseAddress(ctx.TxIndex()),
4848
simulation: simulation,
49-
tempStateCurrent: NewTemporaryState(&accessList{Addresses: make(map[common.Address]int), Slots: []map[common.Hash]struct{}{}}),
49+
tempStateCurrent: NewTemporaryState(),
5050
coinbaseEvmAddress: feeCollector,
5151
}
5252
s.Snapshot() // take an initial snapshot for GetCommitted
@@ -85,6 +85,10 @@ func (s *DBImpl) Finalize() (surplus sdk.Int, err error) {
8585
if s.simulation {
8686
panic("should never call finalize on a simulation DB")
8787
}
88+
defer func() {
89+
s.tempStateCurrent = nil
90+
s.tempStatesHist = []*TemporaryState{}
91+
}()
8892
if s.err != nil {
8993
err = s.err
9094
return
@@ -131,7 +135,7 @@ func (s *DBImpl) Copy() vm.StateDB {
131135
return &DBImpl{
132136
ctx: newCtx,
133137
snapshottedCtxs: append(s.snapshottedCtxs, s.ctx),
134-
tempStateCurrent: NewTemporaryState(s.tempStateCurrent.transientAccessLists.Copy()),
138+
tempStateCurrent: NewTemporaryState(),
135139
tempStatesHist: append(s.tempStatesHist, s.tempStateCurrent),
136140
k: s.k,
137141
coinbaseAddress: s.coinbaseAddress,
@@ -203,13 +207,13 @@ type TemporaryState struct {
203207
surplus sdk.Int // in wei
204208
}
205209

206-
func NewTemporaryState(al *accessList) *TemporaryState {
210+
func NewTemporaryState() *TemporaryState {
207211
return &TemporaryState{
208212
logs: []*ethtypes.Log{},
209213
transientStates: make(map[string]map[string]common.Hash),
210214
transientAccounts: make(map[string][]byte),
211215
transientModuleStates: make(map[string][]byte),
212-
transientAccessLists: al,
216+
transientAccessLists: &accessList{Addresses: make(map[common.Address]int), Slots: []map[common.Hash]struct{}{}},
213217
surplus: utils.Sdk0,
214218
}
215219
}

0 commit comments

Comments
 (0)