environment.go 2.4 KB
Newer Older
F
Felix Lange 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright 2014 The go-ethereum Authors
// 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 Lesser 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with go-ethereum.  If not, see <http://www.gnu.org/licenses/>.

O
obscuren 已提交
17
package vm
18 19

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

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

27 28
// Environment is is required by the virtual machine to get information from
// it's own isolated environment. For an example see `core.VMEnv`
29
type Environment interface {
O
obscuren 已提交
30
	State() *state.StateDB
31

O
obscuren 已提交
32
	Origin() common.Address
33
	BlockNumber() *big.Int
O
obscuren 已提交
34 35
	GetHash(n uint64) common.Hash
	Coinbase() common.Address
36
	Time() uint64
37
	Difficulty() *big.Int
O
obscuren 已提交
38
	GasLimit() *big.Int
O
obscuren 已提交
39
	Transfer(from, to Account, amount *big.Int) error
O
obscuren 已提交
40
	AddLog(*state.Log)
41 42
	AddStructLog(StructLog)
	StructLogs() []StructLog
O
obscuren 已提交
43

44 45
	VmType() Type

O
obscuren 已提交
46 47 48
	Depth() int
	SetDepth(i int)

O
obscuren 已提交
49 50
	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 已提交
51
	Create(me ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, ContextRef)
52 53
}

54 55
// StructLog is emited to the Environment each cycle and lists information about the curent internal state
// prior to the execution of the statement.
56
type StructLog struct {
57 58 59
	Pc      uint64
	Op      OpCode
	Gas     *big.Int
60
	GasCost *big.Int
61 62 63
	Memory  []byte
	Stack   []*big.Int
	Storage map[common.Hash][]byte
64
	Err     error
65 66
}

O
obscuren 已提交
67 68 69 70
type Account interface {
	SubBalance(amount *big.Int)
	AddBalance(amount *big.Int)
	Balance() *big.Int
O
obscuren 已提交
71
	Address() common.Address
O
obscuren 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84
}

// 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
}