github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/core/vm_env.go (about) 1 // Copyright 2014 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 core 18 19 import ( 20 "math/big" 21 22 "github.com/ethereumproject/go-ethereum/common" 23 "github.com/ethereumproject/go-ethereum/core/state" 24 "github.com/ethereumproject/go-ethereum/core/types" 25 "github.com/ethereumproject/go-ethereum/core/vm" 26 ) 27 28 // GetHashFn returns a function for which the VM env can query block hashes through 29 // up to the limit defined by the Yellow Paper and uses the given block chain 30 // to query for information. 31 func GetHashFn(ref common.Hash, chain *BlockChain) func(n uint64) common.Hash { 32 return func(n uint64) common.Hash { 33 for block := chain.GetBlock(ref); block != nil; block = chain.GetBlock(block.ParentHash()) { 34 if block.NumberU64() == n { 35 return block.Hash() 36 } 37 } 38 39 return common.Hash{} 40 } 41 } 42 43 type VMEnv struct { 44 chainConfig *ChainConfig // Chain configuration 45 state *state.StateDB // State to use for executing 46 evm *vm.EVM // The Ethereum Virtual Machine 47 depth int // Current execution depth 48 msg Message // Message appliod 49 50 header *types.Header // Header information 51 chain *BlockChain // Blockchain handle 52 getHashFn func(uint64) common.Hash // getHashFn callback is used to retrieve block hashes 53 } 54 55 func NewEnv(state *state.StateDB, chainConfig *ChainConfig, chain *BlockChain, msg Message, header *types.Header) *VMEnv { 56 env := &VMEnv{ 57 chainConfig: chainConfig, 58 chain: chain, 59 state: state, 60 header: header, 61 msg: msg, 62 getHashFn: GetHashFn(header.ParentHash, chain), 63 } 64 65 env.evm = vm.New(env) 66 return env 67 } 68 69 func (self *VMEnv) RuleSet() vm.RuleSet { return self.chainConfig } 70 func (self *VMEnv) Vm() vm.Vm { return self.evm } 71 func (self *VMEnv) Origin() common.Address { f, _ := self.msg.From(); return f } 72 func (self *VMEnv) BlockNumber() *big.Int { return self.header.Number } 73 func (self *VMEnv) Coinbase() common.Address { return self.header.Coinbase } 74 func (self *VMEnv) Time() *big.Int { return self.header.Time } 75 func (self *VMEnv) Difficulty() *big.Int { return self.header.Difficulty } 76 func (self *VMEnv) GasLimit() *big.Int { return self.header.GasLimit } 77 func (self *VMEnv) Value() *big.Int { return self.msg.Value() } 78 func (self *VMEnv) Db() vm.Database { return self.state } 79 func (self *VMEnv) Depth() int { return self.depth } 80 func (self *VMEnv) SetDepth(i int) { self.depth = i } 81 func (self *VMEnv) GetHash(n uint64) common.Hash { 82 return self.getHashFn(n) 83 } 84 85 func (self *VMEnv) AddLog(log *vm.Log) { 86 self.state.AddLog(*log) 87 } 88 func (self *VMEnv) CanTransfer(from common.Address, balance *big.Int) bool { 89 return self.state.GetBalance(from).Cmp(balance) >= 0 90 } 91 92 func (self *VMEnv) SnapshotDatabase() int { 93 return self.state.Snapshot() 94 } 95 96 func (self *VMEnv) RevertToSnapshot(snapshot int) { 97 self.state.RevertToSnapshot(snapshot) 98 } 99 100 func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) { 101 Transfer(from, to, amount) 102 } 103 104 func (self *VMEnv) Call(me vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) { 105 return Call(self, me, addr, data, gas, price, value) 106 } 107 func (self *VMEnv) CallCode(me vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) { 108 return CallCode(self, me, addr, data, gas, price, value) 109 } 110 111 func (self *VMEnv) DelegateCall(me vm.ContractRef, addr common.Address, data []byte, gas, price *big.Int) ([]byte, error) { 112 return DelegateCall(self, me, addr, data, gas, price) 113 } 114 115 func (self *VMEnv) Create(me vm.ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error) { 116 return Create(self, me, data, gas, price, value) 117 }