vm_env.go 3.15 KB
Newer Older
obscuren's avatar
obscuren committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
	This file is part of go-ethereum

	go-ethereum is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	go-ethereum is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with go-ethereum.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * @authors
 * 	Jeffrey Wilcke <i@jev.io>
 */
21 22 23
package utils

import (
24
	"math/big"
25

obscuren's avatar
obscuren committed
26 27
	"github.com/ethereum/go-ethereum/core"
	"github.com/ethereum/go-ethereum/core/types"
obscuren's avatar
obscuren committed
28
	"github.com/ethereum/go-ethereum/state"
29
	"github.com/ethereum/go-ethereum/vm"
30 31 32
)

type VMEnv struct {
33
	chain *core.ChainManager
obscuren's avatar
obscuren committed
34
	state *state.StateDB
35
	block *types.Block
36 37 38

	transactor []byte
	value      *big.Int
39 40 41

	depth int
	Gas   *big.Int
42 43
}

44
func NewEnv(chain *core.ChainManager, state *state.StateDB, block *types.Block, transactor []byte, value *big.Int) *VMEnv {
45
	return &VMEnv{
46
		chain:      chain,
47 48 49 50 51 52 53
		state:      state,
		block:      block,
		transactor: transactor,
		value:      value,
	}
}

obscuren's avatar
obscuren committed
54
func (self *VMEnv) Origin() []byte        { return self.transactor }
55 56 57 58 59 60
func (self *VMEnv) BlockNumber() *big.Int { return self.block.Number() }
func (self *VMEnv) PrevHash() []byte      { return self.block.ParentHash() }
func (self *VMEnv) Coinbase() []byte      { 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() }
obscuren's avatar
obscuren committed
61
func (self *VMEnv) Value() *big.Int       { return self.value }
obscuren's avatar
obscuren committed
62
func (self *VMEnv) State() *state.StateDB { return self.state }
63 64
func (self *VMEnv) Depth() int            { return self.depth }
func (self *VMEnv) SetDepth(i int)        { self.depth = i }
65 66 67 68 69 70 71
func (self *VMEnv) GetHash(n uint64) []byte {
	if block := self.chain.GetBlockByNumber(n); block != nil {
		return block.Hash()
	}

	return nil
}
obscuren's avatar
obscuren committed
72
func (self *VMEnv) AddLog(log state.Log) {
73 74
	self.state.AddLog(log)
}
obscuren's avatar
obscuren committed
75 76 77
func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
	return vm.Transfer(from, to, amount)
}
78

obscuren's avatar
obscuren committed
79
func (self *VMEnv) vm(addr, data []byte, gas, price, value *big.Int) *core.Execution {
obscuren's avatar
obscuren committed
80
	return core.NewExecution(self, addr, data, gas, price, value)
81 82
}

obscuren's avatar
obscuren committed
83
func (self *VMEnv) Call(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
84 85 86 87 88 89
	exe := self.vm(addr, data, gas, price, value)
	ret, err := exe.Call(addr, caller)
	self.Gas = exe.Gas

	return ret, err
}
obscuren's avatar
obscuren committed
90
func (self *VMEnv) CallCode(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
91 92 93 94
	exe := self.vm(caller.Address(), data, gas, price, value)
	return exe.Call(addr, caller)
}

obscuren's avatar
obscuren committed
95
func (self *VMEnv) Create(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) {
96 97 98
	exe := self.vm(addr, data, gas, price, value)
	return exe.Create(caller)
}