environment.go 3.7 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
// Environment is an EVM requirement and helper which allows access to outside
26
// information such as states.
27 28 29 30 31 32 33
type Environment interface {
	// The state database
	Db() Database
	// Creates a restorable snapshot
	MakeSnapshot() Database
	// Set database to previous snapshot
	SetSnapshot(Database)
L
Leif Jurvetson 已提交
34
	// Address of the original invoker (first occurrence of the VM invoker)
O
obscuren 已提交
35
	Origin() common.Address
L
Leif Jurvetson 已提交
36
	// The block number this VM is invoked on
37
	BlockNumber() *big.Int
38
	// The n'th hash ago from this block number
J
Jeffrey Wilcke 已提交
39
	GetHash(uint64) common.Hash
40
	// The handler's address
O
obscuren 已提交
41
	Coinbase() common.Address
42
	// The current time (block time)
43
	Time() *big.Int
44
	// Difficulty set on the current block
45
	Difficulty() *big.Int
46
	// The gas limit of the block
O
obscuren 已提交
47
	GasLimit() *big.Int
48 49
	// Determines whether it's possible to transact
	CanTransfer(from common.Address, balance *big.Int) bool
50
	// Transfers amount from one account to the other
51
	Transfer(from, to Account, amount *big.Int)
52 53 54
	// Adds a LOG to the state
	AddLog(*Log)
	// Type of the VM
55
	Vm() Vm
56
	// Current calling depth
O
obscuren 已提交
57 58 59
	Depth() int
	SetDepth(i int)

60 61 62 63
	// 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)
64 65
	// 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)
66 67 68 69
	// Create a new contract
	Create(me ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error)
}

70 71 72 73 74 75 76 77 78
// 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.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
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
101 102
}

103
// Account represents a contract or basic ethereum account.
O
obscuren 已提交
104 105 106
type Account interface {
	SubBalance(amount *big.Int)
	AddBalance(amount *big.Int)
107 108
	SetBalance(*big.Int)
	SetNonce(uint64)
O
obscuren 已提交
109
	Balance() *big.Int
O
obscuren 已提交
110
	Address() common.Address
111 112
	ReturnGas(*big.Int, *big.Int)
	SetCode([]byte)
113
	ForEachStorage(cb func(key, value common.Hash) bool)
114
	Value() *big.Int
O
obscuren 已提交
115
}