environment.go 3.69 KB
Newer Older
1
// Copyright 2014 The go-ethereum Authors
2
// This file is part of the go-ethereum library.
3
//
4
// The go-ethereum library is free software: you can redistribute it and/or modify
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,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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/>.
16

obscuren's avatar
obscuren committed
17
package vm
18 19 20 21

import (
	"math/big"

obscuren's avatar
obscuren committed
22
	"github.com/ethereum/go-ethereum/common"
23 24
)

25
// Environment is is required by the virtual machine to get information from
26
// it's own isolated environment.
27

28
// Environment is an EVM requirement and helper which allows access to outside
29
// information such as states.
30 31 32 33 34 35 36 37
type Environment interface {
	// The state database
	Db() Database
	// Creates a restorable snapshot
	MakeSnapshot() Database
	// Set database to previous snapshot
	SetSnapshot(Database)
	// Address of the original invoker (first occurance of the VM invoker)
obscuren's avatar
obscuren committed
38
	Origin() common.Address
39
	// The block number this VM is invoken on
40
	BlockNumber() *big.Int
41
	// The n'th hash ago from this block number
42
	GetHash(uint64) common.Hash
43
	// The handler's address
obscuren's avatar
obscuren committed
44
	Coinbase() common.Address
45
	// The current time (block time)
46
	Time() *big.Int
47
	// Difficulty set on the current block
48
	Difficulty() *big.Int
49
	// The gas limit of the block
obscuren's avatar
obscuren committed
50
	GasLimit() *big.Int
51 52
	// Determines whether it's possible to transact
	CanTransfer(from common.Address, balance *big.Int) bool
53
	// Transfers amount from one account to the other
54
	Transfer(from, to Account, amount *big.Int)
55 56 57
	// Adds a LOG to the state
	AddLog(*Log)
	// Adds a structured log to the env
58
	AddStructLog(StructLog)
59
	// Returns all coalesced structured logs
60
	StructLogs() []StructLog
61

62
	// Type of the VM
63 64
	VmType() Type

65
	// Current calling depth
66 67 68
	Depth() int
	SetDepth(i int)

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	// Call another contract
	Call(me ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
	// Take another's contract code and execute within our own context
	CallCode(me ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error)
	// Create a new contract
	Create(me ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error)
}

// Database is a EVM database for full state querying
type Database interface {
	GetAccount(common.Address) Account
	CreateAccount(common.Address) Account

	AddBalance(common.Address, *big.Int)
	GetBalance(common.Address) *big.Int

	GetNonce(common.Address) uint64
	SetNonce(common.Address, uint64)

	GetCode(common.Address) []byte
	SetCode(common.Address, []byte)

	AddRefund(*big.Int)
	GetRefund() *big.Int

	GetState(common.Address, common.Hash) common.Hash
	SetState(common.Address, common.Hash, common.Hash)

	Delete(common.Address) bool
	Exist(common.Address) bool
	IsDeleted(common.Address) bool
100 101
}

102 103
// StructLog is emited to the Environment each cycle and lists information about the curent internal state
// prior to the execution of the statement.
104
type StructLog struct {
105 106 107
	Pc      uint64
	Op      OpCode
	Gas     *big.Int
108
	GasCost *big.Int
109 110 111
	Memory  []byte
	Stack   []*big.Int
	Storage map[common.Hash][]byte
112
	Err     error
113 114
}

115 116 117
type Account interface {
	SubBalance(amount *big.Int)
	AddBalance(amount *big.Int)
118 119
	SetBalance(*big.Int)
	SetNonce(uint64)
120
	Balance() *big.Int
obscuren's avatar
obscuren committed
121
	Address() common.Address
122 123
	ReturnGas(*big.Int, *big.Int)
	SetCode([]byte)
124
}