github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/core/vm/runtime/env.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package runtime 18 19 import ( 20 "math/big" 21 22 "github.com/ethereumproject/go-ethereum/common" 23 "github.com/ethereumproject/go-ethereum/core" 24 "github.com/ethereumproject/go-ethereum/core/state" 25 "github.com/ethereumproject/go-ethereum/core/vm" 26 ) 27 28 // Env is a basic runtime environment required for running the EVM. 29 type Env struct { 30 ruleSet vm.RuleSet 31 depth int 32 state *state.StateDB 33 34 origin common.Address 35 coinbase common.Address 36 37 number *big.Int 38 time *big.Int 39 difficulty *big.Int 40 gasLimit *big.Int 41 42 getHashFn func(uint64) common.Hash 43 44 evm *vm.EVM 45 } 46 47 // NewEnv returns a new vm.Environment 48 func NewEnv(cfg *Config, state *state.StateDB) vm.Environment { 49 env := &Env{ 50 ruleSet: cfg.RuleSet, 51 state: state, 52 origin: cfg.Origin, 53 coinbase: cfg.Coinbase, 54 number: cfg.BlockNumber, 55 time: cfg.Time, 56 difficulty: cfg.Difficulty, 57 gasLimit: cfg.GasLimit, 58 } 59 env.evm = vm.New(env) 60 61 return env 62 } 63 64 func (self *Env) RuleSet() vm.RuleSet { return self.ruleSet } 65 func (self *Env) Vm() vm.Vm { return self.evm } 66 func (self *Env) Origin() common.Address { return self.origin } 67 func (self *Env) BlockNumber() *big.Int { return self.number } 68 func (self *Env) Coinbase() common.Address { return self.coinbase } 69 func (self *Env) Time() *big.Int { return self.time } 70 func (self *Env) Difficulty() *big.Int { return self.difficulty } 71 func (self *Env) Db() vm.Database { return self.state } 72 func (self *Env) GasLimit() *big.Int { return self.gasLimit } 73 func (self *Env) VmType() vm.Type { return vm.StdVmTy } 74 func (self *Env) GetHash(n uint64) common.Hash { 75 return self.getHashFn(n) 76 } 77 func (self *Env) AddLog(log *vm.Log) { 78 self.state.AddLog(*log) 79 } 80 func (self *Env) Depth() int { return self.depth } 81 func (self *Env) SetDepth(i int) { self.depth = i } 82 func (self *Env) CanTransfer(from common.Address, balance *big.Int) bool { 83 return self.state.GetBalance(from).Cmp(balance) >= 0 84 } 85 func (self *Env) SnapshotDatabase() int { 86 return self.state.Snapshot() 87 } 88 func (self *Env) RevertToSnapshot(snapshot int) { 89 self.state.RevertToSnapshot(snapshot) 90 } 91 92 func (self *Env) Transfer(from, to vm.Account, amount *big.Int) { 93 core.Transfer(from, to, amount) 94 } 95 96 func (self *Env) Call(caller vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) { 97 return core.Call(self, caller, addr, data, gas, price, value) 98 } 99 func (self *Env) CallCode(caller vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) { 100 return core.CallCode(self, caller, addr, data, gas, price, value) 101 } 102 103 func (self *Env) DelegateCall(me vm.ContractRef, addr common.Address, data []byte, gas, price *big.Int) ([]byte, error) { 104 return core.DelegateCall(self, me, addr, data, gas, price) 105 } 106 107 func (self *Env) Create(caller vm.ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error) { 108 return core.Create(self, caller, data, gas, price, value) 109 }