aboutsummaryrefslogtreecommitdiffstats
path: root/core/vm
diff options
context:
space:
mode:
authorJeffrey Wilcke <geffobscura@gmail.com>2016-02-04 06:46:27 +0800
committerJeffrey Wilcke <jeffrey@ethereum.org>2016-03-24 06:02:42 +0800
commit14013372aeca2d7f1d8c3a87b7df7c27868314be (patch)
treed4951dbe1d7f1b25b4bbde786b14b5b0b6107bcc /core/vm
parent342ae7ce7dfd7a0eab2dd06bfa65199825279f05 (diff)
downloaddexon-14013372aeca2d7f1d8c3a87b7df7c27868314be.tar
dexon-14013372aeca2d7f1d8c3a87b7df7c27868314be.tar.gz
dexon-14013372aeca2d7f1d8c3a87b7df7c27868314be.tar.bz2
dexon-14013372aeca2d7f1d8c3a87b7df7c27868314be.tar.lz
dexon-14013372aeca2d7f1d8c3a87b7df7c27868314be.tar.xz
dexon-14013372aeca2d7f1d8c3a87b7df7c27868314be.tar.zst
dexon-14013372aeca2d7f1d8c3a87b7df7c27868314be.zip
core: Added EVM configuration options
The EVM is now initialised with an additional configured object that allows you to turn on debugging options.
Diffstat (limited to 'core/vm')
-rw-r--r--core/vm/common.go5
-rw-r--r--core/vm/contract.go6
-rw-r--r--core/vm/environment.go35
-rw-r--r--core/vm/jit_test.go28
-rw-r--r--core/vm/logger.go133
-rw-r--r--core/vm/logger_test.go104
-rw-r--r--core/vm/runtime/env.go14
-rw-r--r--core/vm/runtime/runtime.go29
-rw-r--r--core/vm/runtime/runtime_test.go15
-rw-r--r--core/vm/virtual_machine.go1
-rw-r--r--core/vm/vm.go109
-rw-r--r--core/vm/vm_jit_fake.go2
12 files changed, 336 insertions, 145 deletions
diff --git a/core/vm/common.go b/core/vm/common.go
index f73bc1527..2878b92d2 100644
--- a/core/vm/common.go
+++ b/core/vm/common.go
@@ -24,11 +24,6 @@ import (
"github.com/ethereum/go-ethereum/params"
)
-// Global Debug flag indicating Debug VM (full logging)
-var Debug bool
-
-var GenerateStructLogs bool = false
-
// Type is the VM type accepted by **NewVm**
type Type byte
diff --git a/core/vm/contract.go b/core/vm/contract.go
index d23995218..59e8f1aa6 100644
--- a/core/vm/contract.go
+++ b/core/vm/contract.go
@@ -28,7 +28,7 @@ type ContractRef interface {
Address() common.Address
Value() *big.Int
SetCode([]byte)
- EachStorage(cb func(key, value []byte))
+ ForEachStorage(callback func(key, value common.Hash) bool)
}
// Contract represents an ethereum contract in the state database. It contains
@@ -156,6 +156,6 @@ func (self *Contract) SetCallCode(addr *common.Address, code []byte) {
// EachStorage iterates the contract's storage and calls a method for every key
// value pair.
-func (self *Contract) EachStorage(cb func(key, value []byte)) {
- self.caller.EachStorage(cb)
+func (self *Contract) ForEachStorage(cb func(key, value common.Hash) bool) {
+ self.caller.ForEachStorage(cb)
}
diff --git a/core/vm/environment.go b/core/vm/environment.go
index 3c530962b..568218edd 100644
--- a/core/vm/environment.go
+++ b/core/vm/environment.go
@@ -22,9 +22,6 @@ import (
"github.com/ethereum/go-ethereum/common"
)
-// Environment is is required by the virtual machine to get information from
-// it's own isolated environment.
-
// Environment is an EVM requirement and helper which allows access to outside
// information such as states.
type Environment interface {
@@ -54,12 +51,8 @@ type Environment interface {
Transfer(from, to Account, amount *big.Int)
// Adds a LOG to the state
AddLog(*Log)
- // Adds a structured log to the env
- AddStructLog(StructLog)
- // Returns all coalesced structured logs
- StructLogs() []StructLog
// Type of the VM
- Vm() *Vm
+ Vm() Vm
// Current calling depth
Depth() int
SetDepth(i int)
@@ -74,7 +67,15 @@ type Environment interface {
Create(me ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error)
}
-// Database is a EVM database for full state querying
+// Vm is the basic interface for an implementation of the EVM.
+type Vm interface {
+ // Run should execute the given contract with the input given in in
+ // and return the contract execution return bytes or an error if it
+ // failed.
+ Run(c *Contract, in []byte) ([]byte, error)
+}
+
+// Database is a EVM database for full state querying.
type Database interface {
GetAccount(common.Address) Account
CreateAccount(common.Address) Account
@@ -99,19 +100,7 @@ type Database interface {
IsDeleted(common.Address) bool
}
-// StructLog is emitted to the Environment each cycle and lists information about the current internal state
-// prior to the execution of the statement.
-type StructLog struct {
- Pc uint64
- Op OpCode
- Gas *big.Int
- GasCost *big.Int
- Memory []byte
- Stack []*big.Int
- Storage map[common.Hash][]byte
- Err error
-}
-
+// Account represents a contract or basic ethereum account.
type Account interface {
SubBalance(amount *big.Int)
AddBalance(amount *big.Int)
@@ -121,6 +110,6 @@ type Account interface {
Address() common.Address
ReturnGas(*big.Int, *big.Int)
SetCode([]byte)
- EachStorage(cb func(key, value []byte))
+ ForEachStorage(cb func(key, value common.Hash) bool)
Value() *big.Int
}
diff --git a/core/vm/jit_test.go b/core/vm/jit_test.go
index 5fac0156f..43b1dee2a 100644
--- a/core/vm/jit_test.go
+++ b/core/vm/jit_test.go
@@ -125,17 +125,17 @@ type vmBench struct {
type account struct{}
-func (account) SubBalance(amount *big.Int) {}
-func (account) AddBalance(amount *big.Int) {}
-func (account) SetAddress(common.Address) {}
-func (account) Value() *big.Int { return nil }
-func (account) SetBalance(*big.Int) {}
-func (account) SetNonce(uint64) {}
-func (account) Balance() *big.Int { return nil }
-func (account) Address() common.Address { return common.Address{} }
-func (account) ReturnGas(*big.Int, *big.Int) {}
-func (account) SetCode([]byte) {}
-func (account) EachStorage(cb func(key, value []byte)) {}
+func (account) SubBalance(amount *big.Int) {}
+func (account) AddBalance(amount *big.Int) {}
+func (account) SetAddress(common.Address) {}
+func (account) Value() *big.Int { return nil }
+func (account) SetBalance(*big.Int) {}
+func (account) SetNonce(uint64) {}
+func (account) Balance() *big.Int { return nil }
+func (account) Address() common.Address { return common.Address{} }
+func (account) ReturnGas(*big.Int, *big.Int) {}
+func (account) SetCode([]byte) {}
+func (account) ForEachStorage(cb func(key, value common.Hash) bool) {}
func runVmBench(test vmBench, b *testing.B) {
var sender account
@@ -165,16 +165,16 @@ func runVmBench(test vmBench, b *testing.B) {
type Env struct {
gasLimit *big.Int
depth int
- evm *Vm
+ evm *EVM
}
func NewEnv() *Env {
env := &Env{gasLimit: big.NewInt(10000), depth: 0}
- env.evm = EVM(env)
+ env.evm = New(env, nil)
return env
}
-func (self *Env) Vm() *Vm { return self.evm }
+func (self *Env) Vm() Vm { return self.evm }
func (self *Env) Origin() common.Address { return common.Address{} }
func (self *Env) BlockNumber() *big.Int { return big.NewInt(0) }
func (self *Env) AddStructLog(log StructLog) {
diff --git a/core/vm/logger.go b/core/vm/logger.go
index 2bd02319f..8d333dfd2 100644
--- a/core/vm/logger.go
+++ b/core/vm/logger.go
@@ -18,12 +18,143 @@ package vm
import (
"fmt"
+ "math/big"
"os"
"unicode"
"github.com/ethereum/go-ethereum/common"
)
+type Storage map[common.Hash]common.Hash
+
+func (self Storage) Copy() Storage {
+ cpy := make(Storage)
+ for key, value := range self {
+ cpy[key] = value
+ }
+
+ return cpy
+}
+
+// StructLogCollector is the basic interface to capture emited logs by the EVM logger.
+type StructLogCollector interface {
+ // Adds the structured log to the collector.
+ AddStructLog(StructLog)
+}
+
+// LogConfig are the configuration options for structured logger the EVM
+type LogConfig struct {
+ DisableMemory bool // disable memory capture
+ DisableStack bool // disable stack capture
+ DisableStorage bool // disable storage capture
+ FullStorage bool // show full storage (slow)
+ Collector StructLogCollector // the log collector
+}
+
+// StructLog is emitted to the Environment each cycle and lists information about the current internal state
+// prior to the execution of the statement.
+type StructLog struct {
+ Pc uint64
+ Op OpCode
+ Gas *big.Int
+ GasCost *big.Int
+ Memory []byte
+ Stack []*big.Int
+ Storage map[common.Hash]common.Hash
+ Depth int
+ Err error
+}
+
+// Logger is an EVM state logger and implements VmLogger.
+//
+// Logger can capture state based on the given Log configuration and also keeps
+// a track record of modified storage which is used in reporting snapshots of the
+// contract their storage.
+type Logger struct {
+ cfg LogConfig
+
+ env Environment
+ changedValues map[common.Address]Storage
+}
+
+// newLogger returns a new logger
+func newLogger(cfg LogConfig, env Environment) *Logger {
+ return &Logger{
+ cfg: cfg,
+ env: env,
+ changedValues: make(map[common.Address]Storage),
+ }
+}
+
+// captureState logs a new structured log message and pushes it out to the environment
+//
+// captureState also tracks SSTORE ops to track dirty values.
+func (l *Logger) captureState(pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, stack *stack, contract *Contract, err error) {
+ // short circuit if no log collector is present
+ if l.cfg.Collector == nil {
+ return
+ }
+
+ // initialise new changed values storage container for this contract
+ // if not present.
+ if l.changedValues[contract.Address()] == nil {
+ l.changedValues[contract.Address()] = make(Storage)
+ }
+
+ // capture SSTORE opcodes and determine the changed value and store
+ // it in the local storage container. NOTE: we do not need to do any
+ // range checks here because that's already handler prior to calling
+ // this function.
+ switch op {
+ case SSTORE:
+ var (
+ value = common.BigToHash(stack.data[stack.len()-2])
+ address = common.BigToHash(stack.data[stack.len()-1])
+ )
+ l.changedValues[contract.Address()][address] = value
+ }
+
+ // copy a snapstot of the current memory state to a new buffer
+ var mem []byte
+ if !l.cfg.DisableMemory {
+ mem = make([]byte, len(memory.Data()))
+ copy(mem, memory.Data())
+ }
+
+ // copy a snapshot of the current stack state to a new buffer
+ var stck []*big.Int
+ if !l.cfg.DisableStack {
+ stck = make([]*big.Int, len(stack.Data()))
+ for i, item := range stack.Data() {
+ stck[i] = new(big.Int).Set(item)
+ }
+ }
+
+ // Copy the storage based on the settings specified in the log config. If full storage
+ // is disabled (default) we can use the simple Storage.Copy method, otherwise we use
+ // the state object to query for all values (slow process).
+ var storage Storage
+ if !l.cfg.DisableStorage {
+ if l.cfg.FullStorage {
+ storage = make(Storage)
+ // Get the contract account and loop over each storage entry. This may involve looping over
+ // the trie and is a very expensive process.
+ l.env.Db().GetAccount(contract.Address()).ForEachStorage(func(key, value common.Hash) bool {
+ storage[key] = value
+ // Return true, indicating we'd like to continue.
+ return true
+ })
+ } else {
+ // copy a snapshot of the current storage to a new container.
+ storage = l.changedValues[contract.Address()].Copy()
+ }
+ }
+ // create a new snaptshot of the EVM.
+ log := StructLog{pc, op, new(big.Int).Set(gas), cost, mem, stck, storage, l.env.Depth(), err}
+ // Add the log to the collector
+ l.cfg.Collector.AddStructLog(log)
+}
+
// StdErrFormat formats a slice of StructLogs to human readable format
func StdErrFormat(logs []StructLog) {
fmt.Fprintf(os.Stderr, "VM STAT %d OPs\n", len(logs))
@@ -61,7 +192,7 @@ func StdErrFormat(logs []StructLog) {
fmt.Fprintln(os.Stderr, "STORAGE =", len(log.Storage))
for h, item := range log.Storage {
- fmt.Fprintf(os.Stderr, "%x: %x\n", h, common.LeftPadBytes(item, 32))
+ fmt.Fprintf(os.Stderr, "%x: %x\n", h, item)
}
fmt.Fprintln(os.Stderr)
}
diff --git a/core/vm/logger_test.go b/core/vm/logger_test.go
new file mode 100644
index 000000000..77fee2c64
--- /dev/null
+++ b/core/vm/logger_test.go
@@ -0,0 +1,104 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "math/big"
+ "testing"
+
+ "github.com/ethereum/go-ethereum/common"
+)
+
+type dummyContractRef struct {
+ calledForEach bool
+}
+
+func (dummyContractRef) ReturnGas(*big.Int, *big.Int) {}
+func (dummyContractRef) Address() common.Address { return common.Address{} }
+func (dummyContractRef) Value() *big.Int { return new(big.Int) }
+func (dummyContractRef) SetCode([]byte) {}
+func (d *dummyContractRef) ForEachStorage(callback func(key, value common.Hash) bool) {
+ d.calledForEach = true
+}
+func (d *dummyContractRef) SubBalance(amount *big.Int) {}
+func (d *dummyContractRef) AddBalance(amount *big.Int) {}
+func (d *dummyContractRef) SetBalance(*big.Int) {}
+func (d *dummyContractRef) SetNonce(uint64) {}
+func (d *dummyContractRef) Balance() *big.Int { return new(big.Int) }
+
+type dummyEnv struct {
+ *Env
+ ref *dummyContractRef
+}
+
+func newDummyEnv(ref *dummyContractRef) *dummyEnv {
+ return &dummyEnv{
+ Env: NewEnv(),
+ ref: ref,
+ }
+}
+func (d dummyEnv) GetAccount(common.Address) Account {
+ return d.ref
+}
+func (d dummyEnv) AddStructLog(StructLog) {}
+
+func TestStoreCapture(t *testing.T) {
+ var (
+ env = NewEnv()
+ logger = newLogger(LogConfig{Collector: env}, env)
+ mem = NewMemory()
+ stack = newstack()
+ contract = NewContract(&dummyContractRef{}, &dummyContractRef{}, new(big.Int), new(big.Int), new(big.Int))
+ )
+ stack.push(big.NewInt(1))
+ stack.push(big.NewInt(0))
+
+ var index common.Hash
+
+ logger.captureState(0, SSTORE, new(big.Int), new(big.Int), mem, stack, contract, nil)
+ if len(logger.changedValues[contract.Address()]) == 0 {
+ t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(), len(logger.changedValues[contract.Address()]))
+ }
+
+ exp := common.BigToHash(big.NewInt(1))
+ if logger.changedValues[contract.Address()][index] != exp {
+ t.Errorf("expected %x, got %x", exp, logger.changedValues[contract.Address()][index])
+ }
+}
+
+func TestStorageCapture(t *testing.T) {
+ t.Skip("implementing this function is difficult. it requires all sort of interfaces to be implemented which isn't trivial. The value (the actual test) isn't worth it")
+ var (
+ ref = &dummyContractRef{}
+ contract = NewContract(ref, ref, new(big.Int), new(big.Int), new(big.Int))
+ env = newDummyEnv(ref)
+ logger = newLogger(LogConfig{Collector: env}, env)
+ mem = NewMemory()
+ stack = newstack()
+ )
+
+ logger.captureState(0, STOP, new(big.Int), new(big.Int), mem, stack, contract, nil)
+ if ref.calledForEach {
+ t.Error("didn't expect for each to be called")
+ }
+
+ logger = newLogger(LogConfig{Collector: env, FullStorage: true}, env)
+ logger.captureState(0, STOP, new(big.Int), new(big.Int), mem, stack, contract, nil)
+ if !ref.calledForEach {
+ t.Error("expected for each to be called")
+ }
+}
diff --git a/core/vm/runtime/env.go b/core/vm/runtime/env.go
index e9bf828ea..ce64d7117 100644
--- a/core/vm/runtime/env.go
+++ b/core/vm/runtime/env.go
@@ -42,7 +42,7 @@ type Env struct {
getHashFn func(uint64) common.Hash
- evm *vm.Vm
+ evm *vm.EVM
}
// NewEnv returns a new vm.Environment
@@ -56,7 +56,15 @@ func NewEnv(cfg *Config, state *state.StateDB) vm.Environment {
difficulty: cfg.Difficulty,
gasLimit: cfg.GasLimit,
}
- env.evm = vm.EVM(env)
+ env.evm = vm.New(env, &vm.Config{
+ Debug: cfg.Debug,
+ EnableJit: !cfg.DisableJit,
+ ForceJit: !cfg.DisableJit,
+
+ Logger: vm.LogConfig{
+ Collector: env,
+ },
+ })
return env
}
@@ -69,7 +77,7 @@ func (self *Env) AddStructLog(log vm.StructLog) {
self.logs = append(self.logs, log)
}
-func (self *Env) Vm() *vm.Vm { return self.evm }
+func (self *Env) Vm() vm.Vm { return self.evm }
func (self *Env) Origin() common.Address { return self.origin }
func (self *Env) BlockNumber() *big.Int { return self.number }
func (self *Env) Coinbase() common.Address { return self.coinbase }
diff --git a/core/vm/runtime/runtime.go b/core/vm/runtime/runtime.go
index 3e6057142..f88a20170 100644
--- a/core/vm/runtime/runtime.go
+++ b/core/vm/runtime/runtime.go
@@ -22,7 +22,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
- "github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
)
@@ -84,17 +83,6 @@ func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
}
setDefaults(cfg)
- // defer the call to setting back the original values
- defer func(debug, forceJit, enableJit bool) {
- vm.Debug = debug
- vm.ForceJit = forceJit
- vm.EnableJit = enableJit
- }(vm.Debug, vm.ForceJit, vm.EnableJit)
-
- vm.ForceJit = !cfg.DisableJit
- vm.EnableJit = !cfg.DisableJit
- vm.Debug = cfg.Debug
-
if cfg.State == nil {
db, _ := ethdb.NewMemDatabase()
cfg.State, _ = state.New(common.Hash{}, db)
@@ -117,9 +105,6 @@ func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
cfg.Value,
)
- if cfg.Debug {
- vm.StdErrFormat(vmenv.StructLogs())
- }
return ret, cfg.State, err
}
@@ -131,17 +116,6 @@ func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
func Call(address common.Address, input []byte, cfg *Config) ([]byte, error) {
setDefaults(cfg)
- // defer the call to setting back the original values
- defer func(debug, forceJit, enableJit bool) {
- vm.Debug = debug
- vm.ForceJit = forceJit
- vm.EnableJit = enableJit
- }(vm.Debug, vm.ForceJit, vm.EnableJit)
-
- vm.ForceJit = !cfg.DisableJit
- vm.EnableJit = !cfg.DisableJit
- vm.Debug = cfg.Debug
-
vmenv := NewEnv(cfg, cfg.State)
sender := cfg.State.GetOrNewStateObject(cfg.Origin)
@@ -155,8 +129,5 @@ func Call(address common.Address, input []byte, cfg *Config) ([]byte, error) {
cfg.Value,
)
- if cfg.Debug {
- vm.StdErrFormat(vmenv.StructLogs())
- }
return ret, err
}
diff --git a/core/vm/runtime/runtime_test.go b/core/vm/runtime/runtime_test.go
index e5183052f..88c76c731 100644
--- a/core/vm/runtime/runtime_test.go
+++ b/core/vm/runtime/runtime_test.go
@@ -117,21 +117,6 @@ func TestCall(t *testing.T) {
}
}
-func TestRestoreDefaults(t *testing.T) {
- Execute(nil, nil, &Config{Debug: true})
- if vm.ForceJit {
- t.Error("expected force jit to be disabled")
- }
-
- if vm.Debug {
- t.Error("expected debug to be disabled")
- }
-
- if vm.EnableJit {
- t.Error("expected jit to be disabled")
- }
-}
-
func BenchmarkCall(b *testing.B) {
var definition = `[{"constant":true,"inputs":[],"name":"seller","outputs":[{"name":"","type":"address"}],"type":"function"},{"constant":false,"inputs":[],"name":"abort","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"value","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[],"name":"refund","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"buyer","outputs":[{"name":"","type":"address"}],"type":"function"},{"constant":false,"inputs":[],"name":"confirmReceived","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"state","outputs":[{"name":"","type":"uint8"}],"type":"function"},{"constant":false,"inputs":[],"name":"confirmPurchase","outputs":[],"type":"function"},{"inputs":[],"type":"constructor"},{"anonymous":false,"inputs":[],"name":"Aborted","type":"event"},{"anonymous":false,"inputs":[],"name":"PurchaseConfirmed","type":"event"},{"anonymous":false,"inputs":[],"name":"ItemReceived","type":"event"},{"anonymous":false,"inputs":[],"name":"Refunded","type":"event"}]`
diff --git a/core/vm/virtual_machine.go b/core/vm/virtual_machine.go
index 9b3340bb2..629108884 100644
--- a/core/vm/virtual_machine.go
+++ b/core/vm/virtual_machine.go
@@ -18,6 +18,5 @@ package vm
// VirtualMachine is an EVM interface
type VirtualMachine interface {
- Env() Environment
Run(*Contract, []byte) ([]byte, error)
}
diff --git a/core/vm/vm.go b/core/vm/vm.go
index 26df8aef4..f72c853a2 100644
--- a/core/vm/vm.go
+++ b/core/vm/vm.go
@@ -28,24 +28,54 @@ import (
"github.com/ethereum/go-ethereum/params"
)
-// Vm is an EVM and implements VirtualMachine
-type Vm struct {
+// Config are the configuration options for the EVM
+type Config struct {
+ Debug bool
+ EnableJit bool
+ ForceJit bool
+ Logger LogConfig
+}
+
+// EVM is used to run Ethereum based contracts and will utilise the
+// passed environment to query external sources for state information.
+// The EVM will run the byte code VM or JIT VM based on the passed
+// configuration.
+type EVM struct {
env Environment
jumpTable vmJumpTable
+ cfg *Config
+
+ logger *Logger
}
-func EVM(env Environment) *Vm {
- return &Vm{env: env, jumpTable: newJumpTable(env.BlockNumber())}
+// New returns a new instance of the EVM.
+func New(env Environment, cfg *Config) *EVM {
+ // initialise a default config if none is present
+ if cfg == nil {
+ cfg = new(Config)
+ }
+
+ var logger *Logger
+ if cfg.Debug {
+ logger = newLogger(cfg.Logger, env)
+ }
+
+ return &EVM{
+ env: env,
+ jumpTable: newJumpTable(env.BlockNumber()),
+ cfg: cfg,
+ logger: logger,
+ }
}
// Run loops and evaluates the contract's code with the given input data
-func (self *Vm) Run(contract *Contract, input []byte) (ret []byte, err error) {
- self.env.SetDepth(self.env.Depth() + 1)
- defer self.env.SetDepth(self.env.Depth() - 1)
+func (evm *EVM) Run(contract *Contract, input []byte) (ret []byte, err error) {
+ evm.env.SetDepth(evm.env.Depth() + 1)
+ defer evm.env.SetDepth(evm.env.Depth() - 1)
if contract.CodeAddr != nil {
if p := Precompiled[contract.CodeAddr.Str()]; p != nil {
- return self.RunPrecompiled(p, input, contract)
+ return evm.RunPrecompiled(p, input, contract)
}
}
@@ -58,21 +88,21 @@ func (self *Vm) Run(contract *Contract, input []byte) (ret []byte, err error) {
codehash = crypto.Keccak256Hash(contract.Code) // codehash is used when doing jump dest caching
program *Program
)
- if EnableJit {
+ if evm.cfg.EnableJit {
// If the JIT is enabled check the status of the JIT program,
// if it doesn't exist compile a new program in a separate
// goroutine or wait for compilation to finish if the JIT is
// forced.
switch GetProgramStatus(codehash) {
case progReady:
- return RunProgram(GetProgram(codehash), self.env, contract, input)
+ return RunProgram(GetProgram(codehash), evm.env, contract, input)
case progUnknown:
- if ForceJit {
+ if evm.cfg.ForceJit {
// Create and compile program
program = NewProgram(contract.Code)
perr := CompileProgram(program)
if perr == nil {
- return RunProgram(program, self.env, contract, input)
+ return RunProgram(program, evm.env, contract, input)
}
glog.V(logger.Info).Infoln("error compiling program", err)
} else {
@@ -95,10 +125,10 @@ func (self *Vm) Run(contract *Contract, input []byte) (ret []byte, err error) {
code = contract.Code
instrCount = 0
- op OpCode // current opcode
- mem = NewMemory() // bound memory
- stack = newstack() // local stack
- statedb = self.env.Db() // current state
+ op OpCode // current opcode
+ mem = NewMemory() // bound memory
+ stack = newstack() // local stack
+ statedb = evm.env.Db() // current state
// For optimisation reason we're using uint64 as the program counter.
// It's theoretically possible to go above 2^64. The YP defines the PC to be uint256. Practically much less so feasible.
pc = uint64(0) // program counter
@@ -123,8 +153,8 @@ func (self *Vm) Run(contract *Contract, input []byte) (ret []byte, err error) {
// User defer pattern to check for an error and, based on the error being nil or not, use all gas and return.
defer func() {
- if err != nil {
- self.log(pc, op, contract.Gas, cost, mem, stack, contract, err)
+ if err != nil && evm.cfg.Debug {
+ evm.logger.captureState(pc, op, contract.Gas, cost, mem, stack, contract, err)
}
}()
@@ -143,7 +173,7 @@ func (self *Vm) Run(contract *Contract, input []byte) (ret []byte, err error) {
// move execution
fmt.Println("moved", it)
glog.V(logger.Info).Infoln("Moved execution to JIT")
- return runProgram(program, pc, mem, stack, self.env, contract, input)
+ return runProgram(program, pc, mem, stack, evm.env, contract, input)
}
}
*/
@@ -151,7 +181,7 @@ func (self *Vm) Run(contract *Contract, input []byte) (ret []byte, err error) {
// Get the memory location of pc
op = contract.GetOp(pc)
// calculate the new memory size and gas price for the current executing opcode
- newMemSize, cost, err = calculateGasAndSize(self.env, contract, caller, op, statedb, mem, stack)
+ newMemSize, cost, err = calculateGasAndSize(evm.env, contract, caller, op, statedb, mem, stack)
if err != nil {
return nil, err
}
@@ -165,14 +195,17 @@ func (self *Vm) Run(contract *Contract, input []byte) (ret []byte, err error) {
// Resize the memory calculated previously
mem.Resize(newMemSize.Uint64())
// Add a log message
- self.log(pc, op, contract.Gas, cost, mem, stack, contract, nil)
- if opPtr := self.jumpTable[op]; opPtr.valid {
+ if evm.cfg.Debug {
+ evm.logger.captureState(pc, op, contract.Gas, cost, mem, stack, contract, nil)
+ }
+
+ if opPtr := evm.jumpTable[op]; opPtr.valid {
if opPtr.fn != nil {
- opPtr.fn(instruction{}, &pc, self.env, contract, mem, stack)
+ opPtr.fn(instruction{}, &pc, evm.env, contract, mem, stack)
} else {
switch op {
case PC:
- opPc(instruction{data: new(big.Int).SetUint64(pc)}, &pc, self.env, contract, mem, stack)
+ opPc(instruction{data: new(big.Int).SetUint64(pc)}, &pc, evm.env, contract, mem, stack)
case JUMP:
if err := jump(pc, stack.pop()); err != nil {
return nil, err
@@ -195,7 +228,7 @@ func (self *Vm) Run(contract *Contract, input []byte) (ret []byte, err error) {
return ret, nil
case SUICIDE:
- opSuicide(instruction{}, nil, self.env, contract, mem, stack)
+ opSuicide(instruction{}, nil, evm.env, contract, mem, stack)
fallthrough
case STOP: // Stop the contract
@@ -347,7 +380,7 @@ func calculateGasAndSize(env Environment, contract *Contract, caller ContractRef
}
// RunPrecompile runs and evaluate the output of a precompiled contract defined in contracts.go
-func (self *Vm) RunPrecompiled(p *PrecompiledAccount, input []byte, contract *Contract) (ret []byte, err error) {
+func (evm *EVM) RunPrecompiled(p *PrecompiledAccount, input []byte, contract *Contract) (ret []byte, err error) {
gas := p.Gas(len(input))
if contract.UseGas(gas) {
ret = p.Call(input)
@@ -357,27 +390,3 @@ func (self *Vm) RunPrecompiled(p *PrecompiledAccount, input []byte, contract *Co
return nil, OutOfGasError
}
}
-
-// log emits a log event to the environment for each opcode encountered. This is not to be confused with the
-// LOG* opcode.
-func (self *Vm) log(pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, stack *stack, contract *Contract, err error) {
- if Debug || GenerateStructLogs {
- mem := make([]byte, len(memory.Data()))
- copy(mem, memory.Data())
-
- stck := make([]*big.Int, len(stack.Data()))
- for i, item := range stack.Data() {
- stck[i] = new(big.Int).Set(item)
- }
- storage := make(map[common.Hash][]byte)
- contract.self.EachStorage(func(k, v []byte) {
- storage[common.BytesToHash(k)] = v
- })
- self.env.AddStructLog(StructLog{pc, op, new(big.Int).Set(gas), cost, mem, stck, storage, err})
- }
-}
-
-// Environment returns the current workable state of the VM
-func (self *Vm) Env() Environment {
- return self.env
-}
diff --git a/core/vm/vm_jit_fake.go b/core/vm/vm_jit_fake.go
index 192f3615d..b26cf1ad0 100644
--- a/core/vm/vm_jit_fake.go
+++ b/core/vm/vm_jit_fake.go
@@ -22,5 +22,5 @@ import "fmt"
func NewJitVm(env Environment) VirtualMachine {
fmt.Printf("Warning! EVM JIT not enabled.\n")
- return EVM(env)
+ return New(env, nil)
}