Skip to content

Commit 32c6aa8

Browse files
authored
core/vm: clean up EVM environmental structure (#31061)
This PR does a few things including: - Remove `ContractRef` interface - Remove `vm.AccountRef` which implements `ContractRef` interface - Maintain the `jumpDests` struct in EVM for sharing between call frames - Simplify the delegateCall context initialization
1 parent 7332a1b commit 32c6aa8

14 files changed

+109
-225
lines changed

core/state_processor.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func ProcessBeaconBlockRoot(beaconRoot common.Hash, evm *vm.EVM) {
229229
}
230230
evm.SetTxContext(NewEVMTxContext(msg))
231231
evm.StateDB.AddAddressToAccessList(params.BeaconRootsAddress)
232-
_, _, _ = evm.Call(vm.AccountRef(msg.From), *msg.To, msg.Data, 30_000_000, common.U2560)
232+
_, _, _ = evm.Call(msg.From, *msg.To, msg.Data, 30_000_000, common.U2560)
233233
evm.StateDB.Finalise(true)
234234
}
235235

@@ -253,7 +253,7 @@ func ProcessParentBlockHash(prevHash common.Hash, evm *vm.EVM) {
253253
}
254254
evm.SetTxContext(NewEVMTxContext(msg))
255255
evm.StateDB.AddAddressToAccessList(params.HistoryStorageAddress)
256-
_, _, err := evm.Call(vm.AccountRef(msg.From), *msg.To, msg.Data, 30_000_000, common.U2560)
256+
_, _, err := evm.Call(msg.From, *msg.To, msg.Data, 30_000_000, common.U2560)
257257
if err != nil {
258258
panic(err)
259259
}
@@ -292,7 +292,7 @@ func processRequestsSystemCall(requests *[][]byte, evm *vm.EVM, requestType byte
292292
}
293293
evm.SetTxContext(NewEVMTxContext(msg))
294294
evm.StateDB.AddAddressToAccessList(addr)
295-
ret, _, _ := evm.Call(vm.AccountRef(msg.From), *msg.To, msg.Data, 30_000_000, common.U2560)
295+
ret, _, _ := evm.Call(msg.From, *msg.To, msg.Data, 30_000_000, common.U2560)
296296
evm.StateDB.Finalise(true)
297297
if len(ret) == 0 {
298298
return // skip empty output

core/state_transition.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,6 @@ func (st *stateTransition) execute() (*ExecutionResult, error) {
423423

424424
var (
425425
msg = st.msg
426-
sender = vm.AccountRef(msg.From)
427426
rules = st.evm.ChainConfig().Rules(st.evm.Context.BlockNumber, st.evm.Context.Random != nil, st.evm.Context.Time)
428427
contractCreation = msg.To == nil
429428
floorDataGas uint64
@@ -484,7 +483,7 @@ func (st *stateTransition) execute() (*ExecutionResult, error) {
484483
vmerr error // vm errors do not effect consensus and are therefore not assigned to err
485484
)
486485
if contractCreation {
487-
ret, _, st.gasRemaining, vmerr = st.evm.Create(sender, msg.Data, st.gasRemaining, value)
486+
ret, _, st.gasRemaining, vmerr = st.evm.Create(msg.From, msg.Data, st.gasRemaining, value)
488487
} else {
489488
// Increment the nonce for the next transaction.
490489
st.state.SetNonce(msg.From, st.state.GetNonce(msg.From)+1, tracing.NonceChangeEoACall)
@@ -507,7 +506,7 @@ func (st *stateTransition) execute() (*ExecutionResult, error) {
507506
}
508507

509508
// Execute the transaction's call.
510-
ret, st.gasRemaining, vmerr = st.evm.Call(sender, st.to(), msg.Data, st.gasRemaining, value)
509+
ret, st.gasRemaining, vmerr = st.evm.Call(msg.From, st.to(), msg.Data, st.gasRemaining, value)
511510
}
512511

513512
// Compute refund counter, capped to a refund quotient.

core/vm/contract.go

+20-66
Original file line numberDiff line numberDiff line change
@@ -22,39 +22,20 @@ import (
2222
"github.com/holiman/uint256"
2323
)
2424

25-
// ContractRef is a reference to the contract's backing object
26-
type ContractRef interface {
27-
Address() common.Address
28-
}
29-
30-
// AccountRef implements ContractRef.
31-
//
32-
// Account references are used during EVM initialisation and
33-
// its primary use is to fetch addresses. Removing this object
34-
// proves difficult because of the cached jump destinations which
35-
// are fetched from the parent contract (i.e. the caller), which
36-
// is a ContractRef.
37-
type AccountRef common.Address
38-
39-
// Address casts AccountRef to an Address
40-
func (ar AccountRef) Address() common.Address { return (common.Address)(ar) }
41-
4225
// Contract represents an ethereum contract in the state database. It contains
4326
// the contract code, calling arguments. Contract implements ContractRef
4427
type Contract struct {
45-
// CallerAddress is the result of the caller which initialised this
46-
// contract. However when the "call method" is delegated this value
47-
// needs to be initialised to that of the caller's caller.
48-
CallerAddress common.Address
49-
caller ContractRef
50-
self ContractRef
28+
// caller is the result of the caller which initialised this
29+
// contract. However, when the "call method" is delegated this
30+
// value needs to be initialised to that of the caller's caller.
31+
caller common.Address
32+
address common.Address
5133

5234
jumpdests map[common.Hash]bitvec // Aggregated result of JUMPDEST analysis.
5335
analysis bitvec // Locally cached result of JUMPDEST analysis
5436

5537
Code []byte
5638
CodeHash common.Hash
57-
CodeAddr *common.Address
5839
Input []byte
5940

6041
// is the execution frame represented by this object a contract deployment
@@ -66,23 +47,18 @@ type Contract struct {
6647
}
6748

6849
// NewContract returns a new contract environment for the execution of EVM.
69-
func NewContract(caller ContractRef, object ContractRef, value *uint256.Int, gas uint64) *Contract {
70-
c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object}
71-
72-
if parent, ok := caller.(*Contract); ok {
73-
// Reuse JUMPDEST analysis from parent context if available.
74-
c.jumpdests = parent.jumpdests
75-
} else {
76-
c.jumpdests = make(map[common.Hash]bitvec)
50+
func NewContract(caller common.Address, address common.Address, value *uint256.Int, gas uint64, jumpDests map[common.Hash]bitvec) *Contract {
51+
// Initialize the jump analysis map if it's nil, mostly for tests
52+
if jumpDests == nil {
53+
jumpDests = make(map[common.Hash]bitvec)
54+
}
55+
return &Contract{
56+
caller: caller,
57+
address: address,
58+
jumpdests: jumpDests,
59+
Gas: gas,
60+
value: value,
7761
}
78-
79-
// Gas should be a pointer so it can safely be reduced through the run
80-
// This pointer will be off the state transition
81-
c.Gas = gas
82-
// ensures a value is set
83-
c.value = value
84-
85-
return c
8662
}
8763

8864
func (c *Contract) validJumpdest(dest *uint256.Int) bool {
@@ -132,18 +108,6 @@ func (c *Contract) isCode(udest uint64) bool {
132108
return c.analysis.codeSegment(udest)
133109
}
134110

135-
// AsDelegate sets the contract to be a delegate call and returns the current
136-
// contract (for chaining calls)
137-
func (c *Contract) AsDelegate() *Contract {
138-
// NOTE: caller must, at all times be a contract. It should never happen
139-
// that caller is something other than a Contract.
140-
parent := c.caller.(*Contract)
141-
c.CallerAddress = parent.CallerAddress
142-
c.value = parent.value
143-
144-
return c
145-
}
146-
147111
// GetOp returns the n'th element in the contract's byte array
148112
func (c *Contract) GetOp(n uint64) OpCode {
149113
if n < uint64(len(c.Code)) {
@@ -158,7 +122,7 @@ func (c *Contract) GetOp(n uint64) OpCode {
158122
// Caller will recursively call caller when the contract is a delegate
159123
// call, including that of caller's caller.
160124
func (c *Contract) Caller() common.Address {
161-
return c.CallerAddress
125+
return c.caller
162126
}
163127

164128
// UseGas attempts the use gas and subtracts it and returns true on success
@@ -186,26 +150,16 @@ func (c *Contract) RefundGas(gas uint64, logger *tracing.Hooks, reason tracing.G
186150

187151
// Address returns the contracts address
188152
func (c *Contract) Address() common.Address {
189-
return c.self.Address()
153+
return c.address
190154
}
191155

192156
// Value returns the contract's value (sent to it from it's caller)
193157
func (c *Contract) Value() *uint256.Int {
194158
return c.value
195159
}
196160

197-
// SetCallCode sets the code of the contract and address of the backing data
198-
// object
199-
func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) {
161+
// SetCallCode sets the code of the contract,
162+
func (c *Contract) SetCallCode(hash common.Hash, code []byte) {
200163
c.Code = code
201164
c.CodeHash = hash
202-
c.CodeAddr = addr
203-
}
204-
205-
// SetCodeOptionalHash can be used to provide code, but it's optional to provide hash.
206-
// In case hash is not provided, the jumpdest analysis will not be saved to the parent context
207-
func (c *Contract) SetCodeOptionalHash(addr *common.Address, codeAndHash *codeAndHash) {
208-
c.Code = codeAndHash.code
209-
c.CodeHash = codeAndHash.hash
210-
c.CodeAddr = addr
211165
}

core/vm/eips.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,9 @@ func opExtCodeCopyEIP4762(pc *uint64, interpreter *EVMInterpreter, scope *ScopeC
338338
}
339339
addr := common.Address(a.Bytes20())
340340
code := interpreter.evm.StateDB.GetCode(addr)
341-
contract := &Contract{
342-
Code: code,
343-
self: AccountRef(addr),
344-
}
345341
paddedCodeCopy, copyOffset, nonPaddedCopyLength := getDataAndAdjustedBounds(code, uint64CodeOffset, length.Uint64())
346-
if !contract.IsSystemCall {
347-
statelessGas := interpreter.evm.AccessEvents.CodeChunksRangeGas(addr, copyOffset, nonPaddedCopyLength, uint64(len(contract.Code)), false)
342+
if !scope.Contract.IsSystemCall {
343+
statelessGas := interpreter.evm.AccessEvents.CodeChunksRangeGas(addr, copyOffset, nonPaddedCopyLength, uint64(len(code)), false)
348344
if !scope.Contract.UseGas(statelessGas, interpreter.evm.Config.Tracer, tracing.GasChangeUnspecified) {
349345
scope.Contract.Gas = 0
350346
return nil, ErrOutOfGas

0 commit comments

Comments
 (0)