github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/core/vm/environment.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 vm
    18  
    19  import (
    20  	"errors"
    21  	"math/big"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/core/state"
    25  )
    26  
    27  // Environment is is required by the virtual machine to get information from
    28  // it's own isolated environment. For an example see `core.VMEnv`
    29  type Environment interface {
    30  	State() *state.StateDB
    31  
    32  	Origin() common.Address
    33  	BlockNumber() *big.Int
    34  	GetHash(n uint64) common.Hash
    35  	Coinbase() common.Address
    36  	Time() *big.Int
    37  	Difficulty() *big.Int
    38  	GasLimit() *big.Int
    39  	CanTransfer(from Account, balance *big.Int) bool
    40  	Transfer(from, to Account, amount *big.Int) error
    41  	AddLog(*state.Log)
    42  	AddStructLog(StructLog)
    43  	StructLogs() []StructLog
    44  
    45  	VmType() Type
    46  
    47  	Depth() int
    48  	SetDepth(i int)
    49  
    50  	Call(me ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
    51  	CallCode(me ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
    52  	Create(me ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, ContextRef)
    53  }
    54  
    55  // StructLog is emited to the Environment each cycle and lists information about the curent internal state
    56  // prior to the execution of the statement.
    57  type StructLog struct {
    58  	Pc      uint64
    59  	Op      OpCode
    60  	Gas     *big.Int
    61  	GasCost *big.Int
    62  	Memory  []byte
    63  	Stack   []*big.Int
    64  	Storage map[common.Hash][]byte
    65  	Err     error
    66  }
    67  
    68  type Account interface {
    69  	SubBalance(amount *big.Int)
    70  	AddBalance(amount *big.Int)
    71  	Balance() *big.Int
    72  	Address() common.Address
    73  }
    74  
    75  // generic transfer method
    76  func Transfer(from, to Account, amount *big.Int) error {
    77  	if from.Balance().Cmp(amount) < 0 {
    78  		return errors.New("Insufficient balance in account")
    79  	}
    80  
    81  	from.SubBalance(amount)
    82  	to.AddBalance(amount)
    83  
    84  	return nil
    85  }