environment.go 1.9 KB
Newer Older
O
obscuren 已提交
1
package vm
2 3

import (
O
obscuren 已提交
4
	"errors"
5
	"fmt"
O
obscuren 已提交
6
	"io"
7 8
	"math/big"

O
obscuren 已提交
9
	"github.com/ethereum/go-ethereum/common"
O
obscuren 已提交
10
	"github.com/ethereum/go-ethereum/core/state"
O
obscuren 已提交
11
	"github.com/ethereum/go-ethereum/rlp"
12 13 14
)

type Environment interface {
O
obscuren 已提交
15
	State() *state.StateDB
16

O
obscuren 已提交
17
	Origin() common.Address
18
	BlockNumber() *big.Int
O
obscuren 已提交
19 20
	GetHash(n uint64) common.Hash
	Coinbase() common.Address
21 22
	Time() int64
	Difficulty() *big.Int
O
obscuren 已提交
23
	GasLimit() *big.Int
O
obscuren 已提交
24
	Transfer(from, to Account, amount *big.Int) error
O
obscuren 已提交
25
	AddLog(state.Log)
O
obscuren 已提交
26

27 28
	VmType() Type

O
obscuren 已提交
29 30 31
	Depth() int
	SetDepth(i int)

O
obscuren 已提交
32 33
	Call(me ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
	CallCode(me ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
O
obscuren 已提交
34
	Create(me ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, ContextRef)
35 36
}

O
obscuren 已提交
37 38 39 40
type Account interface {
	SubBalance(amount *big.Int)
	AddBalance(amount *big.Int)
	Balance() *big.Int
O
obscuren 已提交
41
	Address() common.Address
O
obscuren 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54
}

// generic transfer method
func Transfer(from, to Account, amount *big.Int) error {
	if from.Balance().Cmp(amount) < 0 {
		return errors.New("Insufficient balance in account")
	}

	from.SubBalance(amount)
	to.AddBalance(amount)

	return nil
}
O
obscuren 已提交
55 56

type Log struct {
O
obscuren 已提交
57
	address common.Address
O
obscuren 已提交
58
	topics  []common.Hash
O
obscuren 已提交
59
	data    []byte
O
obscuren 已提交
60
	log     uint64
O
obscuren 已提交
61 62
}

O
obscuren 已提交
63
func (self *Log) Address() common.Address {
O
obscuren 已提交
64 65 66
	return self.address
}

O
obscuren 已提交
67
func (self *Log) Topics() []common.Hash {
O
obscuren 已提交
68 69 70 71 72 73 74
	return self.topics
}

func (self *Log) Data() []byte {
	return self.data
}

O
obscuren 已提交
75 76 77 78
func (self *Log) Number() uint64 {
	return self.log
}

O
obscuren 已提交
79 80 81 82 83
func (self *Log) EncodeRLP(w io.Writer) error {
	return rlp.Encode(w, []interface{}{self.address, self.topics, self.data})
}

/*
O
obscuren 已提交
84
func (self *Log) RlpData() interface{} {
O
obscuren 已提交
85
	return []interface{}{self.address, common.ByteSliceToInterface(self.topics), self.data}
O
obscuren 已提交
86
}
O
obscuren 已提交
87
*/
88 89

func (self *Log) String() string {
O
bloom  
obscuren 已提交
90
	return fmt.Sprintf("{%x %x %x}", self.address, self.data, self.topics)
91
}