2016-04-14 19:18:24 +03:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-10-09 15:56:12 +03:00
|
|
|
// 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 runtime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2016-10-20 14:36:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2015-10-09 15:56:12 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Env is a basic runtime environment required for running the EVM.
|
|
|
|
type Env struct {
|
2016-10-20 14:36:29 +03:00
|
|
|
chainConfig *params.ChainConfig
|
|
|
|
depth int
|
|
|
|
state *state.StateDB
|
2015-10-09 15:56:12 +03:00
|
|
|
|
|
|
|
origin common.Address
|
|
|
|
coinbase common.Address
|
|
|
|
|
|
|
|
number *big.Int
|
|
|
|
time *big.Int
|
|
|
|
difficulty *big.Int
|
|
|
|
gasLimit *big.Int
|
|
|
|
|
|
|
|
getHashFn func(uint64) common.Hash
|
2016-01-21 16:29:58 +02:00
|
|
|
|
2016-02-04 00:46:27 +02:00
|
|
|
evm *vm.EVM
|
2015-10-09 15:56:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewEnv returns a new vm.Environment
|
|
|
|
func NewEnv(cfg *Config, state *state.StateDB) vm.Environment {
|
2016-01-21 16:29:58 +02:00
|
|
|
env := &Env{
|
2016-10-20 14:36:29 +03:00
|
|
|
chainConfig: cfg.ChainConfig,
|
|
|
|
state: state,
|
|
|
|
origin: cfg.Origin,
|
|
|
|
coinbase: cfg.Coinbase,
|
|
|
|
number: cfg.BlockNumber,
|
|
|
|
time: cfg.Time,
|
|
|
|
difficulty: cfg.Difficulty,
|
|
|
|
gasLimit: cfg.GasLimit,
|
2015-10-09 15:56:12 +03:00
|
|
|
}
|
2016-03-02 00:32:43 +02:00
|
|
|
env.evm = vm.New(env, vm.Config{
|
2016-02-04 00:46:27 +02:00
|
|
|
Debug: cfg.Debug,
|
|
|
|
EnableJit: !cfg.DisableJit,
|
|
|
|
ForceJit: !cfg.DisableJit,
|
|
|
|
})
|
2016-01-21 16:29:58 +02:00
|
|
|
|
|
|
|
return env
|
2015-10-09 15:56:12 +03:00
|
|
|
}
|
|
|
|
|
2016-10-20 14:36:29 +03:00
|
|
|
func (self *Env) ChainConfig() *params.ChainConfig { return self.chainConfig }
|
|
|
|
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 }
|
|
|
|
func (self *Env) Time() *big.Int { return self.time }
|
|
|
|
func (self *Env) Difficulty() *big.Int { return self.difficulty }
|
|
|
|
func (self *Env) Db() vm.Database { return self.state }
|
|
|
|
func (self *Env) GasLimit() *big.Int { return self.gasLimit }
|
|
|
|
func (self *Env) VmType() vm.Type { return vm.StdVmTy }
|
2015-10-09 15:56:12 +03:00
|
|
|
func (self *Env) GetHash(n uint64) common.Hash {
|
|
|
|
return self.getHashFn(n)
|
|
|
|
}
|
|
|
|
func (self *Env) AddLog(log *vm.Log) {
|
|
|
|
self.state.AddLog(log)
|
|
|
|
}
|
|
|
|
func (self *Env) Depth() int { return self.depth }
|
|
|
|
func (self *Env) SetDepth(i int) { self.depth = i }
|
|
|
|
func (self *Env) CanTransfer(from common.Address, balance *big.Int) bool {
|
|
|
|
return self.state.GetBalance(from).Cmp(balance) >= 0
|
|
|
|
}
|
2016-10-04 13:36:02 +03:00
|
|
|
func (self *Env) SnapshotDatabase() int {
|
|
|
|
return self.state.Snapshot()
|
2015-10-09 15:56:12 +03:00
|
|
|
}
|
2016-10-04 13:36:02 +03:00
|
|
|
func (self *Env) RevertToSnapshot(snapshot int) {
|
|
|
|
self.state.RevertToSnapshot(snapshot)
|
2015-10-09 15:56:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Env) Transfer(from, to vm.Account, amount *big.Int) {
|
|
|
|
core.Transfer(from, to, amount)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Env) Call(caller vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
|
|
|
|
return core.Call(self, caller, addr, data, gas, price, value)
|
|
|
|
}
|
|
|
|
func (self *Env) CallCode(caller vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
|
|
|
|
return core.CallCode(self, caller, addr, data, gas, price, value)
|
|
|
|
}
|
|
|
|
|
2015-11-27 16:40:29 +02:00
|
|
|
func (self *Env) DelegateCall(me vm.ContractRef, addr common.Address, data []byte, gas, price *big.Int) ([]byte, error) {
|
|
|
|
return core.DelegateCall(self, me, addr, data, gas, price)
|
|
|
|
}
|
|
|
|
|
2015-10-09 15:56:12 +03:00
|
|
|
func (self *Env) Create(caller vm.ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error) {
|
|
|
|
return core.Create(self, caller, data, gas, price, value)
|
|
|
|
}
|