vm_env.go 2.35 KB
Newer Older
obscuren's avatar
obscuren committed
1
package core
2 3 4

import (
	"math/big"
obscuren's avatar
obscuren committed
5

obscuren's avatar
obscuren committed
6
	"github.com/ethereum/go-ethereum/common"
obscuren's avatar
obscuren committed
7
	"github.com/ethereum/go-ethereum/core/state"
obscuren's avatar
obscuren committed
8
	"github.com/ethereum/go-ethereum/core/types"
obscuren's avatar
obscuren committed
9
	"github.com/ethereum/go-ethereum/core/vm"
10 11 12
)

type VMEnv struct {
obscuren's avatar
obscuren committed
13
	state *state.StateDB
14
	block *types.Block
15
	msg   Message
16
	depth int
17
	chain *ChainManager
18
	typ   vm.Type
19 20
}

21
func NewEnv(state *state.StateDB, chain *ChainManager, msg Message, block *types.Block) *VMEnv {
22
	return &VMEnv{
23
		chain: chain,
24 25
		state: state,
		block: block,
26
		msg:   msg,
27
		typ:   vm.StdVmTy,
28 29 30
	}
}

31
func (self *VMEnv) Origin() common.Address   { f, _ := self.msg.From(); return f }
obscuren's avatar
obscuren committed
32 33 34 35 36 37 38 39 40 41 42 43
func (self *VMEnv) BlockNumber() *big.Int    { return self.block.Number() }
func (self *VMEnv) Coinbase() common.Address { return self.block.Coinbase() }
func (self *VMEnv) Time() int64              { return self.block.Time() }
func (self *VMEnv) Difficulty() *big.Int     { return self.block.Difficulty() }
func (self *VMEnv) GasLimit() *big.Int       { return self.block.GasLimit() }
func (self *VMEnv) Value() *big.Int          { return self.msg.Value() }
func (self *VMEnv) State() *state.StateDB    { return self.state }
func (self *VMEnv) Depth() int               { return self.depth }
func (self *VMEnv) SetDepth(i int)           { self.depth = i }
func (self *VMEnv) VmType() vm.Type          { return self.typ }
func (self *VMEnv) SetVmType(t vm.Type)      { self.typ = t }
func (self *VMEnv) GetHash(n uint64) common.Hash {
44 45 46 47
	if block := self.chain.GetBlockByNumber(n); block != nil {
		return block.Hash()
	}

obscuren's avatar
obscuren committed
48
	return common.Hash{}
49
}
obscuren's avatar
obscuren committed
50
func (self *VMEnv) AddLog(log state.Log) {
51
	self.state.AddLog(log)
obscuren's avatar
obscuren committed
52
}
obscuren's avatar
obscuren committed
53 54 55
func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
	return vm.Transfer(from, to, amount)
}
56

obscuren's avatar
obscuren committed
57
func (self *VMEnv) Call(me vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
obscuren's avatar
obscuren committed
58
	exe := NewExecution(self, &addr, data, gas, price, value)
59 60
	return exe.Call(addr, me)
}
obscuren's avatar
obscuren committed
61
func (self *VMEnv) CallCode(me vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
obscuren's avatar
obscuren committed
62
	maddr := me.Address()
obscuren's avatar
obscuren committed
63
	exe := NewExecution(self, &maddr, data, gas, price, value)
64 65 66
	return exe.Call(addr, me)
}

obscuren's avatar
obscuren committed
67
func (self *VMEnv) Create(me vm.ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) {
obscuren's avatar
obscuren committed
68
	exe := NewExecution(self, nil, data, gas, price, value)
69 70
	return exe.Create(me)
}