vm_env.go 3.5 KB
Newer Older
F
Felix Lange 已提交
1
// Copyright 2014 The go-ethereum Authors
2
// This file is part of the go-ethereum library.
F
Felix Lange 已提交
3
//
4
// The go-ethereum library is free software: you can redistribute it and/or modify
F
Felix Lange 已提交
5 6 7 8
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
9
// The go-ethereum library is distributed in the hope that it will be useful,
F
Felix Lange 已提交
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
F
Felix Lange 已提交
12 13 14
// GNU Lesser General Public License for more details.
//
// 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/>.
F
Felix Lange 已提交
16

O
obscuren 已提交
17
package core
O
obscuren 已提交
18 19 20

import (
	"math/big"
O
obscuren 已提交
21

O
obscuren 已提交
22
	"github.com/ethereum/go-ethereum/common"
O
obscuren 已提交
23
	"github.com/ethereum/go-ethereum/core/state"
O
obscuren 已提交
24
	"github.com/ethereum/go-ethereum/core/types"
O
obscuren 已提交
25
	"github.com/ethereum/go-ethereum/core/vm"
O
obscuren 已提交
26 27 28
)

type VMEnv struct {
F
Felix Lange 已提交
29 30 31 32
	state  *state.StateDB
	header *types.Header
	msg    Message
	depth  int
33
	chain  *BlockChain
F
Felix Lange 已提交
34
	typ    vm.Type
35 36
	// structured logging
	logs []vm.StructLog
O
obscuren 已提交
37 38
}

39
func NewEnv(state *state.StateDB, chain *BlockChain, msg Message, header *types.Header) *VMEnv {
O
obscuren 已提交
40
	return &VMEnv{
F
Felix Lange 已提交
41 42 43 44 45
		chain:  chain,
		state:  state,
		header: header,
		msg:    msg,
		typ:    vm.StdVmTy,
O
obscuren 已提交
46 47 48
	}
}

49
func (self *VMEnv) Origin() common.Address   { f, _ := self.msg.From(); return f }
F
Felix Lange 已提交
50 51
func (self *VMEnv) BlockNumber() *big.Int    { return self.header.Number }
func (self *VMEnv) Coinbase() common.Address { return self.header.Coinbase }
52
func (self *VMEnv) Time() *big.Int           { return self.header.Time }
F
Felix Lange 已提交
53 54
func (self *VMEnv) Difficulty() *big.Int     { return self.header.Difficulty }
func (self *VMEnv) GasLimit() *big.Int       { return self.header.GasLimit }
O
obscuren 已提交
55
func (self *VMEnv) Value() *big.Int          { return self.msg.Value() }
56
func (self *VMEnv) Db() vm.Database          { return self.state }
O
obscuren 已提交
57 58 59 60 61
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 {
62 63 64 65
	if block := self.chain.GetBlockByNumber(n); block != nil {
		return block.Hash()
	}

O
obscuren 已提交
66
	return common.Hash{}
67
}
68

69
func (self *VMEnv) AddLog(log *vm.Log) {
70
	self.state.AddLog(log)
O
obscuren 已提交
71
}
72 73 74 75 76 77 78 79 80 81
func (self *VMEnv) CanTransfer(from common.Address, balance *big.Int) bool {
	return self.state.GetBalance(from).Cmp(balance) >= 0
}

func (self *VMEnv) MakeSnapshot() vm.Database {
	return self.state.Copy()
}

func (self *VMEnv) SetSnapshot(copy vm.Database) {
	self.state.Set(copy.(*state.StateDB))
82 83
}

84 85
func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) {
	Transfer(from, to, amount)
O
obscuren 已提交
86
}
O
obscuren 已提交
87

88 89
func (self *VMEnv) Call(me vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
	return Call(self, me, addr, data, gas, price, value)
O
obscuren 已提交
90
}
91 92
func (self *VMEnv) CallCode(me vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
	return CallCode(self, me, addr, data, gas, price, value)
O
obscuren 已提交
93 94
}

95 96
func (self *VMEnv) Create(me vm.ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error) {
	return Create(self, me, data, gas, price, value)
O
obscuren 已提交
97
}
98 99 100 101 102 103 104 105

func (self *VMEnv) StructLogs() []vm.StructLog {
	return self.logs
}

func (self *VMEnv) AddStructLog(log vm.StructLog) {
	self.logs = append(self.logs, log)
}