contract.go 4.43 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 {
obscuren's avatar
obscuren committed
27
	Address() common.Address
28 29
}

30 31 32 33 34 35 36 37 38 39 40 41
// AccountRef implements ContractRef.
//
// Account references are used during EVM initialisation and
// it's primary use is to fetch addresses. Removing this object
// proves difficult because of the cached jump destinations which
// are fetched from the parent contract (i.e. the caller), which
// is a ContractRef.
type AccountRef common.Address

// Address casts AccountRef to a Address
func (ar AccountRef) Address() common.Address { return (common.Address)(ar) }

42
// Contract represents an ethereum contract in the state database. It contains
43
// the the contract code, calling arguments. Contract implements ContractRef
44
type Contract struct {
45 46 47 48 49 50
	// 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
51

52 53
	jumpdests destinations // result of JUMPDEST analysis.

54
	Code     []byte
55
	CodeHash common.Hash
obscuren's avatar
obscuren committed
56
	CodeAddr *common.Address
57
	Input    []byte
58

59 60
	Gas   uint64
	value *big.Int
61 62

	Args []byte
63 64

	DelegateCall bool
65 66
}

67
// NewContract returns a new contract environment for the execution of EVM.
68
func NewContract(caller ContractRef, object ContractRef, value *big.Int, gas uint64) *Contract {
69
	c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object, Args: nil}
70

71
	if parent, ok := caller.(*Contract); ok {
72 73 74 75 76 77
		// Reuse JUMPDEST analysis from parent context if available.
		c.jumpdests = parent.jumpdests
	} else {
		c.jumpdests = make(destinations)
	}

78 79
	// Gas should be a pointer so it can safely be reduced through the run
	// This pointer will be off the state transition
80
	c.Gas = gas
81 82
	// ensures a value is set
	c.value = value
83 84 85 86

	return c
}

87 88 89 90 91 92
// 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.
93 94 95 96
	parent := c.caller.(*Contract)
	c.CallerAddress = parent.CallerAddress
	c.value = parent.value

97 98 99
	return c
}

100 101
// GetOp returns the n'th element in the contract's byte array
func (c *Contract) GetOp(n uint64) OpCode {
obscuren's avatar
obscuren committed
102
	return OpCode(c.GetByte(n))
103 104
}

105 106
// GetByte returns the n'th byte in the contract's byte array
func (c *Contract) GetByte(n uint64) byte {
107 108
	if n < uint64(len(c.Code)) {
		return c.Code[n]
109 110 111 112 113
	}

	return 0
}

114 115 116 117 118 119 120 121
// 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
}

122
// UseGas attempts the use gas and subtracts it and returns true on success
123 124 125
func (c *Contract) UseGas(gas uint64) (ok bool) {
	if c.Gas < gas {
		return false
126
	}
127 128
	c.Gas -= gas
	return true
129 130
}

131 132
// Address returns the contracts address
func (c *Contract) Address() common.Address {
133
	return c.self.Address()
134 135
}

136 137 138 139 140
// Value returns the contracts value (sent to it from it's caller)
func (c *Contract) Value() *big.Int {
	return c.value
}

141
// SetCode sets the code to the contract
142 143 144
func (c *Contract) SetCode(hash common.Hash, code []byte) {
	c.Code = code
	c.CodeHash = hash
145
}
146

147 148
// SetCallCode sets the code of the contract and address of the backing data
// object
149 150 151 152
func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) {
	c.Code = code
	c.CodeHash = hash
	c.CodeAddr = addr
153
}