aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/blockchain.go2
-rw-r--r--core/blockchain_test.go4
-rw-r--r--core/chain_makers.go2
-rw-r--r--core/state/state_object.go33
-rw-r--r--core/state_processor.go8
-rw-r--r--core/state_transition.go6
-rw-r--r--core/types.go2
-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
-rw-r--r--core/vm_env.go41
20 files changed, 385 insertions, 194 deletions
diff --git a/core/blockchain.go b/core/blockchain.go
index 534318ecd..cecb914a8 100644
--- a/core/blockchain.go
+++ b/core/blockchain.go
@@ -891,7 +891,7 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
return i, err
}
// Process block using the parent state as reference point.
- receipts, logs, usedGas, err := self.processor.Process(block, statedb)
+ receipts, logs, usedGas, err := self.processor.Process(block, statedb, nil)
if err != nil {
reportBlock(block, err)
return i, err
diff --git a/core/blockchain_test.go b/core/blockchain_test.go
index df979578e..d7cd24fa8 100644
--- a/core/blockchain_test.go
+++ b/core/blockchain_test.go
@@ -141,7 +141,7 @@ func testBlockChainImport(chain types.Blocks, blockchain *BlockChain) error {
if err != nil {
return err
}
- receipts, _, usedGas, err := blockchain.Processor().Process(block, statedb)
+ receipts, _, usedGas, err := blockchain.Processor().Process(block, statedb, nil)
if err != nil {
reportBlock(block, err)
return err
@@ -435,7 +435,7 @@ func (bproc) ValidateHeader(*types.Header, *types.Header, bool) error { return n
func (bproc) ValidateState(block, parent *types.Block, state *state.StateDB, receipts types.Receipts, usedGas *big.Int) error {
return nil
}
-func (bproc) Process(block *types.Block, statedb *state.StateDB) (types.Receipts, vm.Logs, *big.Int, error) {
+func (bproc) Process(block *types.Block, statedb *state.StateDB, cfg *vm.Config) (types.Receipts, vm.Logs, *big.Int, error) {
return nil, nil, nil, nil
}
diff --git a/core/chain_makers.go b/core/chain_makers.go
index 0e1ca5fff..7ae3c98b0 100644
--- a/core/chain_makers.go
+++ b/core/chain_makers.go
@@ -91,7 +91,7 @@ func (b *BlockGen) AddTx(tx *types.Transaction) {
b.SetCoinbase(common.Address{})
}
b.statedb.StartRecord(tx.Hash(), common.Hash{}, len(b.txs))
- receipt, _, _, err := ApplyTransaction(nil, b.gasPool, b.statedb, b.header, tx, b.header.GasUsed)
+ receipt, _, _, err := ApplyTransaction(nil, b.gasPool, b.statedb, b.header, tx, b.header.GasUsed, nil)
if err != nil {
panic(err)
}
diff --git a/core/state/state_object.go b/core/state/state_object.go
index 0f86325c6..326708118 100644
--- a/core/state/state_object.go
+++ b/core/state/state_object.go
@@ -38,7 +38,7 @@ func (self Code) String() string {
return string(self) //strings.Join(Disassemble(self), " ")
}
-type Storage map[string]common.Hash
+type Storage map[common.Hash]common.Hash
func (self Storage) String() (str string) {
for key, value := range self {
@@ -112,13 +112,13 @@ func (c *StateObject) getAddr(addr common.Hash) common.Hash {
return common.BytesToHash(ret)
}
-func (c *StateObject) setAddr(addr []byte, value common.Hash) {
+func (c *StateObject) setAddr(addr, value common.Hash) {
v, err := rlp.EncodeToBytes(bytes.TrimLeft(value[:], "\x00"))
if err != nil {
// if RLPing failed we better panic and not fail silently. This would be considered a consensus issue
panic(err)
}
- c.trie.Update(addr, v)
+ c.trie.Update(addr[:], v)
}
func (self *StateObject) Storage() Storage {
@@ -126,20 +126,19 @@ func (self *StateObject) Storage() Storage {
}
func (self *StateObject) GetState(key common.Hash) common.Hash {
- strkey := key.Str()
- value, exists := self.storage[strkey]
+ value, exists := self.storage[key]
if !exists {
value = self.getAddr(key)
if (value != common.Hash{}) {
- self.storage[strkey] = value
+ self.storage[key] = value
}
}
return value
}
-func (self *StateObject) SetState(k, value common.Hash) {
- self.storage[k.Str()] = value
+func (self *StateObject) SetState(key, value common.Hash) {
+ self.storage[key] = value
self.dirty = true
}
@@ -147,10 +146,10 @@ func (self *StateObject) SetState(k, value common.Hash) {
func (self *StateObject) Update() {
for key, value := range self.storage {
if (value == common.Hash{}) {
- self.trie.Delete([]byte(key))
+ self.trie.Delete(key[:])
continue
}
- self.setAddr([]byte(key), value)
+ self.setAddr(key, value)
}
}
@@ -245,24 +244,22 @@ func (self *StateObject) Value() *big.Int {
panic("Value on StateObject should never be called")
}
-func (self *StateObject) EachStorage(cb func(key, value []byte)) {
+func (self *StateObject) ForEachStorage(cb func(key, value common.Hash) bool) {
// When iterating over the storage check the cache first
- for h, v := range self.storage {
- cb([]byte(h), v.Bytes())
+ for h, value := range self.storage {
+ cb(h, value)
}
it := self.trie.Iterator()
for it.Next() {
// ignore cached values
- key := self.trie.GetKey(it.Key)
- if _, ok := self.storage[string(key)]; !ok {
- cb(key, it.Value)
+ key := common.BytesToHash(self.trie.GetKey(it.Key))
+ if _, ok := self.storage[key]; !ok {
+ cb(key, common.BytesToHash(it.Value))
}
}
}
-// Encoding
-
type extStateObject struct {
Nonce uint64
Balance *big.Int
diff --git a/core/state_processor.go b/core/state_processor.go
index 3ca36a43a..38cd0e675 100644
--- a/core/state_processor.go
+++ b/core/state_processor.go
@@ -31,7 +31,7 @@ func NewStateProcessor(bc *BlockChain) *StateProcessor {
// Process returns the receipts and logs accumulated during the process and
// returns the amount of gas that was used in the process. If any of the
// transactions failed to execute due to insufficient gas it will return an error.
-func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB) (types.Receipts, vm.Logs, *big.Int, error) {
+func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg *vm.Config) (types.Receipts, vm.Logs, *big.Int, error) {
var (
receipts types.Receipts
totalUsedGas = big.NewInt(0)
@@ -43,7 +43,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB) (ty
for i, tx := range block.Transactions() {
statedb.StartRecord(tx.Hash(), block.Hash(), i)
- receipt, logs, _, err := ApplyTransaction(p.bc, gp, statedb, header, tx, totalUsedGas)
+ receipt, logs, _, err := ApplyTransaction(p.bc, gp, statedb, header, tx, totalUsedGas, cfg)
if err != nil {
return nil, nil, totalUsedGas, err
}
@@ -60,8 +60,8 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB) (ty
//
// ApplyTransactions returns the generated receipts and vm logs during the
// execution of the state transition phase.
-func ApplyTransaction(bc *BlockChain, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *big.Int) (*types.Receipt, vm.Logs, *big.Int, error) {
- _, gas, err := ApplyMessage(NewEnv(statedb, bc, tx, header), tx, gp)
+func ApplyTransaction(bc *BlockChain, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *big.Int, cfg *vm.Config) (*types.Receipt, vm.Logs, *big.Int, error) {
+ _, gas, err := ApplyMessage(NewEnv(statedb, bc, tx, header, cfg), tx, gp)
if err != nil {
return nil, nil, nil, err
}
diff --git a/core/state_transition.go b/core/state_transition.go
index 2887f6228..cc357aaca 100644
--- a/core/state_transition.go
+++ b/core/state_transition.go
@@ -137,6 +137,7 @@ func (self *StateTransition) from() (vm.Account, error) {
}
return self.state.GetAccount(f), nil
}
+
func (self *StateTransition) to() vm.Account {
if self.msg == nil {
return nil
@@ -193,7 +194,6 @@ func (self *StateTransition) preCheck() (err error) {
}
// Make sure this transaction's nonce is correct
- //if sender.Nonce() != msg.Nonce() {
if n := self.state.GetNonce(sender.Address()); n != msg.Nonce() {
return NonceError(msg.Nonce(), n)
}
@@ -253,10 +253,6 @@ func (self *StateTransition) transitionDb() (ret []byte, usedGas *big.Int, err e
err = nil
}
- if vm.Debug {
- vm.StdErrFormat(vmenv.StructLogs())
- }
-
self.refundGas()
self.state.AddBalance(self.env.Coinbase(), new(big.Int).Mul(self.gasUsed(), self.gasPrice))
diff --git a/core/types.go b/core/types.go
index 022528374..af9bc567b 100644
--- a/core/types.go
+++ b/core/types.go
@@ -61,7 +61,7 @@ type HeaderValidator interface {
// of gas used in the process and return an error if any of the internal rules
// failed.
type Processor interface {
- Process(block *types.Block, statedb *state.StateDB) (types.Receipts, vm.Logs, *big.Int, error)
+ Process(block *types.Block, statedb *state.StateDB, cfg *vm.Config) (types.Receipts, vm.Logs, *big.Int, error)
}
// Backend is an interface defining the basic functionality for an operable node
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)
}
diff --git a/core/vm_env.go b/core/vm_env.go
index 0fab4a090..880baa7b0 100644
--- a/core/vm_env.go
+++ b/core/vm_env.go
@@ -41,33 +41,42 @@ func GetHashFn(ref common.Hash, chain *BlockChain) func(n uint64) common.Hash {
}
type VMEnv struct {
- state *state.StateDB
- header *types.Header
- msg Message
- depth int
- chain *BlockChain
- typ vm.Type
-
- getHashFn func(uint64) common.Hash
- // structured logging
- logs []vm.StructLog
- evm *vm.Vm
+ state *state.StateDB // State to use for executing
+ evm *vm.EVM // The Ethereum Virtual Machine
+ depth int // Current execution depth
+ msg Message // Message appliod
+
+ header *types.Header // Header information
+ chain *BlockChain // Blockchain handle
+ logs []vm.StructLog // Logs for the custom structured logger
+ getHashFn func(uint64) common.Hash // getHashFn callback is used to retrieve block hashes
+
}
-func NewEnv(state *state.StateDB, chain *BlockChain, msg Message, header *types.Header) *VMEnv {
+func NewEnv(state *state.StateDB, chain *BlockChain, msg Message, header *types.Header, cfg *vm.Config) *VMEnv {
env := &VMEnv{
chain: chain,
state: state,
header: header,
msg: msg,
- typ: vm.StdVmTy,
getHashFn: GetHashFn(header.ParentHash, chain),
}
- env.evm = vm.EVM(env)
+
+ // initialise a default config if none present
+ if cfg == nil {
+ cfg = new(vm.Config)
+ }
+
+ // if no log collector is present set self as the collector
+ if cfg.Logger.Collector == nil {
+ cfg.Logger.Collector = env
+ }
+
+ env.evm = vm.New(env, cfg)
return env
}
-func (self *VMEnv) Vm() *vm.Vm { return self.evm }
+func (self *VMEnv) Vm() vm.Vm { return self.evm }
func (self *VMEnv) Origin() common.Address { f, _ := self.msg.From(); return f }
func (self *VMEnv) BlockNumber() *big.Int { return self.header.Number }
func (self *VMEnv) Coinbase() common.Address { return self.header.Coinbase }
@@ -78,8 +87,6 @@ func (self *VMEnv) Value() *big.Int { return self.msg.Value() }
func (self *VMEnv) Db() vm.Database { return self.state }
func (self *VMEnv) Depth() int { return self.depth }
func (self *VMEnv) SetDepth(i int) { self.depth = i }
-func (self *VMEnv) VmType() vm.Type { return self.typ }
-func (self *VMEnv) SetVmType(t vm.Type) { self.typ = t }
func (self *VMEnv) GetHash(n uint64) common.Hash {
return self.getHashFn(n)
}