contract.go 4.9 KB
Newer Older
1
// Copyright 2015 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

import (
obscuren's avatar
obscuren committed
20 21
	"math/big"

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

25 26
// ContractRef is a reference to the contract's backing object
type ContractRef interface {
27
	ReturnGas(*big.Int, *big.Int)
obscuren's avatar
obscuren committed
28
	Address() common.Address
29
	Value() *big.Int
30
	SetCode(common.Hash, []byte)
31
	ForEachStorage(callback func(key, value common.Hash) bool)
32 33
}

34
// Contract represents an ethereum contract in the state database. It contains
35
// the the contract code, calling arguments. Contract implements ContractRef
36
type Contract struct {
37 38 39 40 41 42
	// CallerAddress is the result of the caller which initialised this
	// contract. However when the "call method" is delegated this value
	// needs to be initialised to that of the caller's caller.
	CallerAddress common.Address
	caller        ContractRef
	self          ContractRef
43

44 45
	jumpdests destinations // result of JUMPDEST analysis.

46
	Code     []byte
47
	CodeHash common.Hash
obscuren's avatar
obscuren committed
48
	CodeAddr *common.Address
49
	Input    []byte
50 51

	value, Gas, UsedGas, Price *big.Int
52 53

	Args []byte
54 55

	DelegateCall bool
56 57
}

58
// NewContract returns a new contract environment for the execution of EVM.
59
func NewContract(caller ContractRef, object ContractRef, value, gas, price *big.Int) *Contract {
60
	c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object, Args: nil}
61

62
	if parent, ok := caller.(*Contract); ok {
63 64 65 66 67 68
		// Reuse JUMPDEST analysis from parent context if available.
		c.jumpdests = parent.jumpdests
	} else {
		c.jumpdests = make(destinations)
	}

69 70 71
	// Gas should be a pointer so it can safely be reduced through the run
	// This pointer will be off the state transition
	c.Gas = gas //new(big.Int).Set(gas)
72
	c.value = new(big.Int).Set(value)
73 74 75 76 77 78 79 80
	// In most cases price and value are pointers to transaction objects
	// and we don't want the transaction's values to change.
	c.Price = new(big.Int).Set(price)
	c.UsedGas = new(big.Int)

	return c
}

81 82 83 84 85 86 87 88 89 90
// AsDelegate sets the contract to be a delegate call and returns the current
// contract (for chaining calls)
func (c *Contract) AsDelegate() *Contract {
	c.DelegateCall = true
	// NOTE: caller must, at all times be a contract. It should never happen
	// that caller is something other than a Contract.
	c.CallerAddress = c.caller.(*Contract).CallerAddress
	return c
}

91 92
// GetOp returns the n'th element in the contract's byte array
func (c *Contract) GetOp(n uint64) OpCode {
obscuren's avatar
obscuren committed
93
	return OpCode(c.GetByte(n))
94 95
}

96 97
// GetByte returns the n'th byte in the contract's byte array
func (c *Contract) GetByte(n uint64) byte {
98 99
	if n < uint64(len(c.Code)) {
		return c.Code[n]
100 101 102 103 104
	}

	return 0
}

105 106 107 108 109 110 111 112 113 114 115
// Caller returns the caller of the contract.
//
// Caller will recursively call caller when the contract is a delegate
// call, including that of caller's caller.
func (c *Contract) Caller() common.Address {
	return c.CallerAddress
}

// Finalise finalises the contract and returning any remaining gas to the original
// caller.
func (c *Contract) Finalise() {
116 117 118 119
	// Return the remaining gas to the caller
	c.caller.ReturnGas(c.Gas, c.Price)
}

120 121 122
// UseGas attempts the use gas and subtracts it and returns true on success
func (c *Contract) UseGas(gas *big.Int) (ok bool) {
	ok = useGas(c.Gas, gas)
obscuren's avatar
obscuren committed
123 124
	if ok {
		c.UsedGas.Add(c.UsedGas, gas)
125
	}
obscuren's avatar
obscuren committed
126
	return
127 128
}

129 130
// ReturnGas adds the given gas back to itself.
func (c *Contract) ReturnGas(gas, price *big.Int) {
obscuren's avatar
obscuren committed
131
	// Return the gas to the context
132 133 134 135
	c.Gas.Add(c.Gas, gas)
	c.UsedGas.Sub(c.UsedGas, gas)
}

136 137
// Address returns the contracts address
func (c *Contract) Address() common.Address {
138
	return c.self.Address()
139 140
}

141 142 143 144 145
// Value returns the contracts value (sent to it from it's caller)
func (c *Contract) Value() *big.Int {
	return c.value
}

146
// SetCode sets the code to the contract
147
func (self *Contract) SetCode(hash common.Hash, code []byte) {
148
	self.Code = code
149
	self.CodeHash = hash
150
}
151

152 153
// SetCallCode sets the code of the contract and address of the backing data
// object
154
func (self *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) {
155
	self.Code = code
156
	self.CodeHash = hash
157 158
	self.CodeAddr = addr
}
159 160 161

// EachStorage iterates the contract's storage and calls a method for every key
// value pair.
162 163
func (self *Contract) ForEachStorage(cb func(key, value common.Hash) bool) {
	self.caller.ForEachStorage(cb)
164
}