environment.go 4.0 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 vm
18 19 20 21

import (
	"math/big"

O
obscuren 已提交
22
	"github.com/ethereum/go-ethereum/common"
23 24
)

25 26 27 28 29 30
// RuleSet is an interface that defines the current rule set during the
// execution of the EVM instructions (e.g. whether it's homestead)
type RuleSet interface {
	IsHomestead(*big.Int) bool
}

31
// Environment is an EVM requirement and helper which allows access to outside
32
// information such as states.
33
type Environment interface {
34 35
	// The current ruleset
	RuleSet() RuleSet
36 37 38 39 40 41
	// The state database
	Db() Database
	// Creates a restorable snapshot
	MakeSnapshot() Database
	// Set database to previous snapshot
	SetSnapshot(Database)
L
Leif Jurvetson 已提交
42
	// Address of the original invoker (first occurrence of the VM invoker)
O
obscuren 已提交
43
	Origin() common.Address
L
Leif Jurvetson 已提交
44
	// The block number this VM is invoked on
45
	BlockNumber() *big.Int
46
	// The n'th hash ago from this block number
J
Jeffrey Wilcke 已提交
47
	GetHash(uint64) common.Hash
48
	// The handler's address
O
obscuren 已提交
49
	Coinbase() common.Address
50
	// The current time (block time)
51
	Time() *big.Int
52
	// Difficulty set on the current block
53
	Difficulty() *big.Int
54
	// The gas limit of the block
O
obscuren 已提交
55
	GasLimit() *big.Int
56 57
	// Determines whether it's possible to transact
	CanTransfer(from common.Address, balance *big.Int) bool
58
	// Transfers amount from one account to the other
59
	Transfer(from, to Account, amount *big.Int)
60 61 62
	// Adds a LOG to the state
	AddLog(*Log)
	// Type of the VM
63
	Vm() Vm
64
	// Get the curret calling depth
O
obscuren 已提交
65
	Depth() int
66
	// Set the current calling depth
O
obscuren 已提交
67
	SetDepth(i int)
68 69 70 71
	// 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)
72 73
	// Same as CallCode except sender and value is propagated from parent to child scope
	DelegateCall(me ContractRef, addr common.Address, data []byte, gas, price *big.Int) ([]byte, error)
74 75 76 77
	// Create a new contract
	Create(me ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error)
}

78 79 80 81 82 83 84 85 86
// Vm is the basic interface for an implementation of the EVM.
type Vm interface {
	// Run should execute the given contract with the input given in in
	// and return the contract execution return bytes or an error if it
	// failed.
	Run(c *Contract, in []byte) ([]byte, error)
}

// Database is a EVM database for full state querying.
87 88 89 90 91 92 93 94 95 96
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)

97
	GetCodeSize(common.Address) int
98 99 100 101 102 103 104 105 106 107 108 109
	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
110 111
}

112
// Account represents a contract or basic ethereum account.
O
obscuren 已提交
113 114 115
type Account interface {
	SubBalance(amount *big.Int)
	AddBalance(amount *big.Int)
116 117
	SetBalance(*big.Int)
	SetNonce(uint64)
O
obscuren 已提交
118
	Balance() *big.Int
O
obscuren 已提交
119
	Address() common.Address
120 121
	ReturnGas(*big.Int, *big.Int)
	SetCode([]byte)
122
	ForEachStorage(cb func(key, value common.Hash) bool)
123
	Value() *big.Int
O
obscuren 已提交
124
}