github.com/immesys/bw2bc@v1.1.0/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/ethereum/go-ethereum/common"
    23  	"github.com/ethereum/go-ethereum/core/state"
    24  	"github.com/ethereum/go-ethereum/core/types"
    25  	"github.com/ethereum/go-ethereum/core/vm"
    26  )
    27  
    28  type VMEnv struct {
    29  	state  *state.StateDB
    30  	header *types.Header
    31  	msg    Message
    32  	depth  int
    33  	chain  *ChainManager
    34  	typ    vm.Type
    35  	// structured logging
    36  	logs []vm.StructLog
    37  }
    38  
    39  func NewEnv(state *state.StateDB, chain *ChainManager, msg Message, header *types.Header) *VMEnv {
    40  	return &VMEnv{
    41  		chain:  chain,
    42  		state:  state,
    43  		header: header,
    44  		msg:    msg,
    45  		typ:    vm.StdVmTy,
    46  	}
    47  }
    48  
    49  func (self *VMEnv) Origin() common.Address   { f, _ := self.msg.From(); return f }
    50  func (self *VMEnv) BlockNumber() *big.Int    { return self.header.Number }
    51  func (self *VMEnv) Coinbase() common.Address { return self.header.Coinbase }
    52  func (self *VMEnv) Time() *big.Int           { return self.header.Time }
    53  func (self *VMEnv) Difficulty() *big.Int     { return self.header.Difficulty }
    54  func (self *VMEnv) GasLimit() *big.Int       { return self.header.GasLimit }
    55  func (self *VMEnv) Value() *big.Int          { return self.msg.Value() }
    56  func (self *VMEnv) State() *state.StateDB    { return self.state }
    57  func (self *VMEnv) Depth() int               { return self.depth }
    58  func (self *VMEnv) SetDepth(i int)           { self.depth = i }
    59  func (self *VMEnv) VmType() vm.Type          { return self.typ }
    60  func (self *VMEnv) SetVmType(t vm.Type)      { self.typ = t }
    61  func (self *VMEnv) GetHash(n uint64) common.Hash {
    62  	if block := self.chain.GetBlockByNumber(n); block != nil {
    63  		return block.Hash()
    64  	}
    65  
    66  	return common.Hash{}
    67  }
    68  
    69  func (self *VMEnv) AddLog(log *state.Log) {
    70  	self.state.AddLog(log)
    71  }
    72  func (self *VMEnv) CanTransfer(from vm.Account, balance *big.Int) bool {
    73  	return from.Balance().Cmp(balance) >= 0
    74  }
    75  
    76  func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
    77  	return vm.Transfer(from, to, amount)
    78  }
    79  
    80  func (self *VMEnv) Call(me vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
    81  	exe := NewExecution(self, &addr, data, gas, price, value)
    82  	return exe.Call(addr, me)
    83  }
    84  func (self *VMEnv) CallCode(me vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
    85  	maddr := me.Address()
    86  	exe := NewExecution(self, &maddr, data, gas, price, value)
    87  	return exe.Call(addr, me)
    88  }
    89  
    90  func (self *VMEnv) Create(me vm.ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) {
    91  	exe := NewExecution(self, nil, data, gas, price, value)
    92  	return exe.Create(me)
    93  }
    94  
    95  func (self *VMEnv) StructLogs() []vm.StructLog {
    96  	return self.logs
    97  }
    98  
    99  func (self *VMEnv) AddStructLog(log vm.StructLog) {
   100  	self.logs = append(self.logs, log)
   101  }