Skip to content

Commit 342ae7c

Browse files
committed
core, core/vm, tests: changed the initialisation behaviour of the EVM
The EVM was previously initialised and created for every CALL, CALLCODE, DELEGATECALL and CREATE. This PR changes this behaviour so that the same EVM can be used through the session and beyond as long as the Environment sticks around.
1 parent 2855a93 commit 342ae7c

13 files changed

Lines changed: 49 additions & 44 deletions

File tree

‎cmd/evm/main.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/ethereum/go-ethereum/core/vm"
3434
"github.com/ethereum/go-ethereum/ethdb"
3535
"github.com/ethereum/go-ethereum/logger/glog"
36+
"github.com/ethereum/go-ethereum/params"
3637
)
3738

3839
var (
@@ -174,17 +175,23 @@ type VMEnv struct {
174175
Gas *big.Int
175176
time *big.Int
176177
logs []vm.StructLog
178+
179+
evm *vm.Vm
177180
}
178181

179182
func NewEnv(state *state.StateDB, transactor common.Address, value *big.Int) *VMEnv {
180-
return &VMEnv{
183+
params.HomesteadBlock = new(big.Int)
184+
env := &VMEnv{
181185
state: state,
182186
transactor: &transactor,
183187
value: value,
184188
time: big.NewInt(time.Now().Unix()),
185189
}
190+
env.evm = vm.EVM(env)
191+
return env
186192
}
187193

194+
func (self *VMEnv) Vm() *vm.Vm { return self.evm }
188195
func (self *VMEnv) Db() vm.Database { return self.state }
189196
func (self *VMEnv) MakeSnapshot() vm.Database { return self.state.Copy() }
190197
func (self *VMEnv) SetSnapshot(db vm.Database) { self.state.Set(db.(*state.StateDB)) }

‎core/execution.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func Create(env vm.Environment, caller vm.ContractRef, code []byte, gas, gasPric
6060
}
6161

6262
func exec(env vm.Environment, caller vm.ContractRef, address, codeAddr *common.Address, input, code []byte, gas, gasPrice, value *big.Int) (ret []byte, addr common.Address, err error) {
63-
evm := vm.NewVm(env)
63+
evm := env.Vm()
6464
// Depth check execution. Fail if we're trying to execute above the
6565
// limit.
6666
if env.Depth() > int(params.CallCreateDepth.Int64()) {
@@ -136,7 +136,7 @@ func exec(env vm.Environment, caller vm.ContractRef, address, codeAddr *common.A
136136
}
137137

138138
func execDelegateCall(env vm.Environment, caller vm.ContractRef, originAddr, toAddr, codeAddr *common.Address, input, code []byte, gas, gasPrice, value *big.Int) (ret []byte, addr common.Address, err error) {
139-
evm := vm.NewVm(env)
139+
evm := env.Vm()
140140
// Depth check execution. Fail if we're trying to execute above the
141141
// limit.
142142
if env.Depth() > int(params.CallCreateDepth.Int64()) {

‎core/vm/common.go‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"math/big"
2222

2323
"github.com/ethereum/go-ethereum/common"
24-
"github.com/ethereum/go-ethereum/logger/glog"
2524
"github.com/ethereum/go-ethereum/params"
2625
)
2726

@@ -51,19 +50,6 @@ var (
5150
max = big.NewInt(math.MaxInt64) // Maximum 64 bit integer
5251
)
5352

54-
// NewVm returns a new VM based on the Environment
55-
func NewVm(env Environment) VirtualMachine {
56-
switch env.VmType() {
57-
case JitVmTy:
58-
return NewJitVm(env)
59-
default:
60-
glog.V(0).Infoln("unsupported vm type %d", env.VmType())
61-
fallthrough
62-
case StdVmTy:
63-
return New(env)
64-
}
65-
}
66-
6753
// calculates the memory size required for a step
6854
func calcMemSize(off, l *big.Int) *big.Int {
6955
if l.Cmp(common.Big0) == 0 {

‎core/vm/environment.go‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,8 @@ type Environment interface {
5858
AddStructLog(StructLog)
5959
// Returns all coalesced structured logs
6060
StructLogs() []StructLog
61-
6261
// Type of the VM
63-
VmType() Type
64-
62+
Vm() *Vm
6563
// Current calling depth
6664
Depth() int
6765
SetDepth(i int)

‎core/vm/instructions.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,6 @@ func opDelegateCall(instr instruction, pc *uint64, env Environment, contract *Co
597597
toAddr := common.BigToAddress(to)
598598
args := memory.Get(inOffset.Int64(), inSize.Int64())
599599
ret, err := env.DelegateCall(contract, toAddr, args, gas, contract.Price)
600-
601600
if err != nil {
602601
stack.push(new(big.Int))
603602
} else {

‎core/vm/jit_test.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func runVmBench(test vmBench, b *testing.B) {
154154
context := NewContract(sender, sender, big.NewInt(100), big.NewInt(10000), big.NewInt(0))
155155
context.Code = test.code
156156
context.CodeAddr = &common.Address{}
157-
_, err := New(env).Run(context, test.input)
157+
_, err := env.Vm().Run(context, test.input)
158158
if err != nil {
159159
b.Error(err)
160160
b.FailNow()
@@ -165,12 +165,16 @@ func runVmBench(test vmBench, b *testing.B) {
165165
type Env struct {
166166
gasLimit *big.Int
167167
depth int
168+
evm *Vm
168169
}
169170

170171
func NewEnv() *Env {
171-
return &Env{big.NewInt(10000), 0}
172+
env := &Env{gasLimit: big.NewInt(10000), depth: 0}
173+
env.evm = EVM(env)
174+
return env
172175
}
173176

177+
func (self *Env) Vm() *Vm { return self.evm }
174178
func (self *Env) Origin() common.Address { return common.Address{} }
175179
func (self *Env) BlockNumber() *big.Int { return big.NewInt(0) }
176180
func (self *Env) AddStructLog(log StructLog) {

‎core/vm/jump_table.go‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,15 @@ type jumpPtr struct {
1313

1414
type vmJumpTable [256]jumpPtr
1515

16-
func (jt vmJumpTable) init(blockNumber *big.Int) {
16+
func newJumpTable(blockNumber *big.Int) vmJumpTable {
17+
var jumpTable vmJumpTable
18+
1719
// when initialising a new VM execution we must first check the homestead
1820
// changes.
1921
if params.IsHomestead(blockNumber) {
2022
jumpTable[DELEGATECALL] = jumpPtr{opDelegateCall, true}
21-
} else {
22-
jumpTable[DELEGATECALL] = jumpPtr{nil, false}
2323
}
24-
}
2524

26-
var jumpTable vmJumpTable
27-
28-
func init() {
2925
jumpTable[ADD] = jumpPtr{opAdd, true}
3026
jumpTable[SUB] = jumpPtr{opSub, true}
3127
jumpTable[MUL] = jumpPtr{opMul, true}
@@ -156,4 +152,6 @@ func init() {
156152
jumpTable[JUMP] = jumpPtr{nil, true}
157153
jumpTable[JUMPI] = jumpPtr{nil, true}
158154
jumpTable[STOP] = jumpPtr{nil, true}
155+
156+
return jumpTable
159157
}

‎core/vm/jump_table_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import (
1010
func TestInit(t *testing.T) {
1111
params.HomesteadBlock = big.NewInt(1)
1212

13-
jumpTable.init(big.NewInt(0))
13+
jumpTable := newJumpTable(big.NewInt(0))
1414
if jumpTable[DELEGATECALL].valid {
1515
t.Error("Expected DELEGATECALL not to be present")
1616
}
1717

1818
for _, n := range []int64{1, 2, 100} {
19-
jumpTable.init(big.NewInt(n))
19+
jumpTable := newJumpTable(big.NewInt(n))
2020
if !jumpTable[DELEGATECALL].valid {
2121
t.Error("Expected DELEGATECALL to be present for block", n)
2222
}

‎core/vm/runtime/env.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ type Env struct {
4141
logs []vm.StructLog
4242

4343
getHashFn func(uint64) common.Hash
44+
45+
evm *vm.Vm
4446
}
4547

4648
// NewEnv returns a new vm.Environment
4749
func NewEnv(cfg *Config, state *state.StateDB) vm.Environment {
48-
return &Env{
50+
env := &Env{
4951
state: state,
5052
origin: cfg.Origin,
5153
coinbase: cfg.Coinbase,
@@ -54,6 +56,9 @@ func NewEnv(cfg *Config, state *state.StateDB) vm.Environment {
5456
difficulty: cfg.Difficulty,
5557
gasLimit: cfg.GasLimit,
5658
}
59+
env.evm = vm.EVM(env)
60+
61+
return env
5762
}
5863

5964
func (self *Env) StructLogs() []vm.StructLog {
@@ -64,6 +69,7 @@ func (self *Env) AddStructLog(log vm.StructLog) {
6469
self.logs = append(self.logs, log)
6570
}
6671

72+
func (self *Env) Vm() *vm.Vm { return self.evm }
6773
func (self *Env) Origin() common.Address { return self.origin }
6874
func (self *Env) BlockNumber() *big.Int { return self.number }
6975
func (self *Env) Coinbase() common.Address { return self.coinbase }

‎core/vm/vm.go‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,12 @@ import (
3030

3131
// Vm is an EVM and implements VirtualMachine
3232
type Vm struct {
33-
env Environment
33+
env Environment
34+
jumpTable vmJumpTable
3435
}
3536

36-
// New returns a new Vm
37-
func New(env Environment) *Vm {
38-
// init the jump table. Also prepares the homestead changes
39-
jumpTable.init(env.BlockNumber())
40-
41-
return &Vm{env: env}
37+
func EVM(env Environment) *Vm {
38+
return &Vm{env: env, jumpTable: newJumpTable(env.BlockNumber())}
4239
}
4340

4441
// Run loops and evaluates the contract's code with the given input data
@@ -169,7 +166,7 @@ func (self *Vm) Run(contract *Contract, input []byte) (ret []byte, err error) {
169166
mem.Resize(newMemSize.Uint64())
170167
// Add a log message
171168
self.log(pc, op, contract.Gas, cost, mem, stack, contract, nil)
172-
if opPtr := jumpTable[op]; opPtr.valid {
169+
if opPtr := self.jumpTable[op]; opPtr.valid {
173170
if opPtr.fn != nil {
174171
opPtr.fn(instruction{}, &pc, self.env, contract, mem, stack)
175172
} else {

0 commit comments

Comments
 (0)