github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/vm/environment.go (about)

     1  package vm
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"math/big"
     7  
     8  	"github.com/jonasnick/go-ethereum/ethutil"
     9  	"github.com/jonasnick/go-ethereum/state"
    10  )
    11  
    12  type Environment interface {
    13  	State() *state.StateDB
    14  
    15  	Origin() []byte
    16  	BlockNumber() *big.Int
    17  	GetHash(n uint64) []byte
    18  	Coinbase() []byte
    19  	Time() int64
    20  	Difficulty() *big.Int
    21  	GasLimit() *big.Int
    22  	Transfer(from, to Account, amount *big.Int) error
    23  	AddLog(state.Log)
    24  
    25  	VmType() Type
    26  
    27  	Depth() int
    28  	SetDepth(i int)
    29  
    30  	Call(me ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error)
    31  	CallCode(me ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error)
    32  	Create(me ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, ContextRef)
    33  }
    34  
    35  type Account interface {
    36  	SubBalance(amount *big.Int)
    37  	AddBalance(amount *big.Int)
    38  	Balance() *big.Int
    39  }
    40  
    41  // generic transfer method
    42  func Transfer(from, to Account, amount *big.Int) error {
    43  	if from.Balance().Cmp(amount) < 0 {
    44  		return errors.New("Insufficient balance in account")
    45  	}
    46  
    47  	from.SubBalance(amount)
    48  	to.AddBalance(amount)
    49  
    50  	return nil
    51  }
    52  
    53  type Log struct {
    54  	address []byte
    55  	topics  [][]byte
    56  	data    []byte
    57  }
    58  
    59  func (self *Log) Address() []byte {
    60  	return self.address
    61  }
    62  
    63  func (self *Log) Topics() [][]byte {
    64  	return self.topics
    65  }
    66  
    67  func (self *Log) Data() []byte {
    68  	return self.data
    69  }
    70  
    71  func (self *Log) RlpData() interface{} {
    72  	return []interface{}{self.address, ethutil.ByteSliceToInterface(self.topics), self.data}
    73  }
    74  
    75  func (self *Log) String() string {
    76  	return fmt.Sprintf("[A=%x T=%x D=%x]", self.address, self.topics, self.data)
    77  }