mirror of https://github.com/status-im/op-geth.git
core: Added EVM configuration options
The EVM is now initialised with an additional configured object that allows you to turn on debugging options.
This commit is contained in:
parent
342ae7ce7d
commit
14013372ae
|
@ -26,7 +26,6 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/ethereum/go-ethereum/core/vm"
|
||||
"github.com/ethereum/go-ethereum/logger/glog"
|
||||
"github.com/ethereum/go-ethereum/tests"
|
||||
)
|
||||
|
@ -188,7 +187,6 @@ func setupApp(c *cli.Context) {
|
|||
continueOnError = c.GlobalBool(ContinueOnErrorFlag.Name)
|
||||
useStdIn := c.GlobalBool(ReadStdInFlag.Name)
|
||||
skipTests = strings.Split(c.GlobalString(SkipTestsFlag.Name), " ")
|
||||
vm.Debug = c.GlobalBool(TraceFlag.Name)
|
||||
|
||||
if !useStdIn {
|
||||
runSuite(flagTest, flagFile)
|
||||
|
|
|
@ -106,7 +106,6 @@ func init() {
|
|||
}
|
||||
|
||||
func run(ctx *cli.Context) {
|
||||
vm.Debug = ctx.GlobalBool(DebugFlag.Name)
|
||||
vm.ForceJit = ctx.GlobalBool(ForceJitFlag.Name)
|
||||
vm.EnableJit = !ctx.GlobalBool(DisableJitFlag.Name)
|
||||
|
||||
|
@ -119,7 +118,9 @@ func run(ctx *cli.Context) {
|
|||
receiver := statedb.CreateAccount(common.StringToAddress("receiver"))
|
||||
receiver.SetCode(common.Hex2Bytes(ctx.GlobalString(CodeFlag.Name)))
|
||||
|
||||
vmenv := NewEnv(statedb, common.StringToAddress("evmuser"), common.Big(ctx.GlobalString(ValueFlag.Name)))
|
||||
vmenv := NewEnv(statedb, common.StringToAddress("evmuser"), common.Big(ctx.GlobalString(ValueFlag.Name)), &vm.Config{
|
||||
Debug: ctx.GlobalBool(DebugFlag.Name),
|
||||
})
|
||||
|
||||
tstart := time.Now()
|
||||
ret, e := vmenv.Call(
|
||||
|
@ -176,10 +177,10 @@ type VMEnv struct {
|
|||
time *big.Int
|
||||
logs []vm.StructLog
|
||||
|
||||
evm *vm.Vm
|
||||
evm *vm.EVM
|
||||
}
|
||||
|
||||
func NewEnv(state *state.StateDB, transactor common.Address, value *big.Int) *VMEnv {
|
||||
func NewEnv(state *state.StateDB, transactor common.Address, value *big.Int, cfg *vm.Config) *VMEnv {
|
||||
params.HomesteadBlock = new(big.Int)
|
||||
env := &VMEnv{
|
||||
state: state,
|
||||
|
@ -187,11 +188,13 @@ func NewEnv(state *state.StateDB, transactor common.Address, value *big.Int) *VM
|
|||
value: value,
|
||||
time: big.NewInt(time.Now().Unix()),
|
||||
}
|
||||
env.evm = vm.EVM(env)
|
||||
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) Db() vm.Database { return self.state }
|
||||
func (self *VMEnv) MakeSnapshot() vm.Database { return self.state.Copy() }
|
||||
func (self *VMEnv) SetSnapshot(db vm.Database) { self.state.Set(db.(*state.StateDB)) }
|
||||
|
|
|
@ -321,7 +321,6 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
|
|||
utils.WhisperEnabledFlag,
|
||||
utils.DevModeFlag,
|
||||
utils.TestNetFlag,
|
||||
utils.VMDebugFlag,
|
||||
utils.VMForceJitFlag,
|
||||
utils.VMJitCacheFlag,
|
||||
utils.VMEnableJitFlag,
|
||||
|
|
|
@ -142,7 +142,6 @@ var AppHelpFlagGroups = []flagGroup{
|
|||
{
|
||||
Name: "VIRTUAL MACHINE",
|
||||
Flags: []cli.Flag{
|
||||
utils.VMDebugFlag,
|
||||
utils.VMEnableJitFlag,
|
||||
utils.VMForceJitFlag,
|
||||
utils.VMJitCacheFlag,
|
||||
|
|
|
@ -203,11 +203,6 @@ var (
|
|||
Value: "",
|
||||
}
|
||||
|
||||
// vm flags
|
||||
VMDebugFlag = cli.BoolFlag{
|
||||
Name: "vmdebug",
|
||||
Usage: "Virtual Machine debug output",
|
||||
}
|
||||
VMForceJitFlag = cli.BoolFlag{
|
||||
Name: "forcejit",
|
||||
Usage: "Force the JIT VM to take precedence",
|
||||
|
@ -728,9 +723,6 @@ func MakeSystemNode(name, version string, extra []byte, ctx *cli.Context) *node.
|
|||
if !ctx.GlobalIsSet(WhisperEnabledFlag.Name) {
|
||||
shhEnable = true
|
||||
}
|
||||
if !ctx.GlobalIsSet(VMDebugFlag.Name) {
|
||||
vm.Debug = true
|
||||
}
|
||||
ethConf.PowTest = true
|
||||
}
|
||||
// Assemble and return the protocol stack
|
||||
|
@ -771,9 +763,6 @@ func SetupVM(ctx *cli.Context) {
|
|||
vm.EnableJit = ctx.GlobalBool(VMEnableJitFlag.Name)
|
||||
vm.ForceJit = ctx.GlobalBool(VMForceJitFlag.Name)
|
||||
vm.SetJITCacheSize(ctx.GlobalInt(VMJitCacheFlag.Name))
|
||||
if ctx.GlobalIsSet(VMDebugFlag.Name) {
|
||||
vm.Debug = ctx.GlobalBool(VMDebugFlag.Name)
|
||||
}
|
||||
}
|
||||
|
||||
// MakeChain creates a chain manager from set command line flags.
|
||||
|
|
|
@ -179,7 +179,7 @@ func (be *registryAPIBackend) Call(fromStr, toStr, valueStr, gasStr, gasPriceStr
|
|||
}
|
||||
|
||||
header := be.bc.CurrentBlock().Header()
|
||||
vmenv := core.NewEnv(statedb, be.bc, msg, header)
|
||||
vmenv := core.NewEnv(statedb, be.bc, msg, header, nil)
|
||||
gp := new(core.GasPool).AddGas(common.MaxBig)
|
||||
res, gas, err := core.ApplyMessage(vmenv, msg, gp)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
|
@ -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 }
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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"}]`
|
||||
|
||||
|
|
|
@ -18,6 +18,5 @@ package vm
|
|||
|
||||
// VirtualMachine is an EVM interface
|
||||
type VirtualMachine interface {
|
||||
Env() Environment
|
||||
Run(*Contract, []byte) ([]byte, error)
|
||||
}
|
||||
|
|
113
core/vm/vm.go
113
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 {
|
||||
env Environment
|
||||
jumpTable vmJumpTable
|
||||
// Config are the configuration options for the EVM
|
||||
type Config struct {
|
||||
Debug bool
|
||||
EnableJit bool
|
||||
ForceJit bool
|
||||
Logger LogConfig
|
||||
}
|
||||
|
||||
func EVM(env Environment) *Vm {
|
||||
return &Vm{env: env, jumpTable: newJumpTable(env.BlockNumber())}
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
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
|
||||
|
||||
getHashFn func(uint64) common.Hash
|
||||
// structured logging
|
||||
logs []vm.StructLog
|
||||
evm *vm.Vm
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
|
11
eth/api.go
11
eth/api.go
|
@ -667,7 +667,7 @@ func (s *PublicBlockChainAPI) doCall(args CallArgs, blockNr rpc.BlockNumber) (st
|
|||
}
|
||||
|
||||
// Execute the call and return
|
||||
vmenv := core.NewEnv(stateDb, s.bc, msg, block.Header())
|
||||
vmenv := core.NewEnv(stateDb, s.bc, msg, block.Header(), nil)
|
||||
gp := new(core.GasPool).AddGas(common.MaxBig)
|
||||
|
||||
res, gas, err := core.ApplyMessage(vmenv, msg, gp)
|
||||
|
@ -1513,9 +1513,6 @@ func (api *PrivateDebugAPI) ProcessBlock(number uint64) (bool, error) {
|
|||
if block == nil {
|
||||
return false, fmt.Errorf("block #%d not found", number)
|
||||
}
|
||||
// Temporarily enable debugging
|
||||
defer func(old bool) { vm.Debug = old }(vm.Debug)
|
||||
vm.Debug = true
|
||||
|
||||
// Validate and reprocess the block
|
||||
var (
|
||||
|
@ -1530,7 +1527,7 @@ func (api *PrivateDebugAPI) ProcessBlock(number uint64) (bool, error) {
|
|||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
receipts, _, usedGas, err := processor.Process(block, statedb)
|
||||
receipts, _, usedGas, err := processor.Process(block, statedb, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
@ -1601,10 +1598,8 @@ func (s *PrivateDebugAPI) doReplayTransaction(txHash common.Hash) ([]vm.StructLo
|
|||
data: tx.Data(),
|
||||
}
|
||||
|
||||
vmenv := core.NewEnv(stateDb, s.eth.BlockChain(), msg, block.Header())
|
||||
vmenv := core.NewEnv(stateDb, s.eth.BlockChain(), msg, block.Header(), nil)
|
||||
gp := new(core.GasPool).AddGas(block.GasLimit())
|
||||
vm.GenerateStructLogs = true
|
||||
defer func() { vm.GenerateStructLogs = false }()
|
||||
|
||||
ret, gas, err := core.ApplyMessage(vmenv, msg, gp)
|
||||
if err != nil {
|
||||
|
|
|
@ -663,7 +663,7 @@ func (env *Work) commitTransactions(mux *event.TypeMux, transactions types.Trans
|
|||
|
||||
func (env *Work) commitTransaction(tx *types.Transaction, bc *core.BlockChain, gp *core.GasPool) (error, vm.Logs) {
|
||||
snap := env.state.Copy()
|
||||
receipt, logs, _, err := core.ApplyTransaction(bc, gp, env.state, env.header, tx, env.header.GasUsed)
|
||||
receipt, logs, _, err := core.ApplyTransaction(bc, gp, env.state, env.header, tx, env.header.GasUsed, nil)
|
||||
if err != nil {
|
||||
env.state.Set(snap)
|
||||
return err, nil
|
||||
|
|
|
@ -235,7 +235,6 @@ func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, vm.Log
|
|||
}
|
||||
// Set pre compiled contracts
|
||||
vm.Precompiled = vm.PrecompiledContracts()
|
||||
vm.Debug = false
|
||||
snapshot := statedb.Copy()
|
||||
gaspool := new(core.GasPool).AddGas(common.Big(env["currentGasLimit"]))
|
||||
|
||||
|
|
|
@ -28,8 +28,13 @@ import (
|
|||
"github.com/ethereum/go-ethereum/core/vm"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/ethereum/go-ethereum/logger/glog"
|
||||
)
|
||||
|
||||
func init() {
|
||||
glog.SetV(0)
|
||||
}
|
||||
|
||||
func checkLogs(tlog []Log, logs vm.Logs) error {
|
||||
|
||||
if len(tlog) != len(logs) {
|
||||
|
@ -144,7 +149,7 @@ type Env struct {
|
|||
|
||||
vmTest bool
|
||||
|
||||
evm *vm.Vm
|
||||
evm *vm.EVM
|
||||
}
|
||||
|
||||
func NewEnv(state *state.StateDB) *Env {
|
||||
|
@ -174,12 +179,12 @@ func NewEnvFromMap(state *state.StateDB, envValues map[string]string, exeValues
|
|||
env.gasLimit = common.Big(envValues["currentGasLimit"])
|
||||
env.Gas = new(big.Int)
|
||||
|
||||
env.evm = vm.EVM(env)
|
||||
env.evm = vm.New(env, nil)
|
||||
|
||||
return env
|
||||
}
|
||||
|
||||
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 }
|
||||
|
|
Loading…
Reference in New Issue