contract.go 5.82 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 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 map[common.Hash]bitvec // Aggregated result of JUMPDEST analysis.
	analysis  bitvec                 // Locally cached result of JUMPDEST analysis
54

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

60 61
	Gas   uint64
	value *big.Int
62 63
}

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

68
	if parent, ok := caller.(*Contract); ok {
69 70 71
		// Reuse JUMPDEST analysis from parent context if available.
		c.jumpdests = parent.jumpdests
	} else {
72
		c.jumpdests = make(map[common.Hash]bitvec)
73 74
	}

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

	return c
}

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
func (c *Contract) validJumpdest(dest *big.Int) bool {
	udest := dest.Uint64()
	// PC cannot go beyond len(code) and certainly can't be bigger than 63bits.
	// Don't bother checking for JUMPDEST in that case.
	if dest.BitLen() >= 63 || udest >= uint64(len(c.Code)) {
		return false
	}
	// Only JUMPDESTs allowed for destinations
	if OpCode(c.Code[udest]) != JUMPDEST {
		return false
	}
	// Do we have a contract hash already?
	if c.CodeHash != (common.Hash{}) {
		// Does parent context have the analysis?
		analysis, exist := c.jumpdests[c.CodeHash]
		if !exist {
			// Do the analysis and save in parent context
			// We do not need to store it in c.analysis
			analysis = codeBitmap(c.Code)
			c.jumpdests[c.CodeHash] = analysis
		}
		return analysis.codeSegment(udest)
	}
	// We don't have the code hash, most likely a piece of initcode not already
	// in state trie. In that case, we do an analysis, and save it locally, so
	// we don't have to recalculate it for every JUMP instruction in the execution
	// However, we don't save it within the parent context
	if c.analysis == nil {
		c.analysis = codeBitmap(c.Code)
	}
	return c.analysis.codeSegment(udest)
}

117 118 119 120 121
// AsDelegate sets the contract to be a delegate call and returns the current
// contract (for chaining calls)
func (c *Contract) AsDelegate() *Contract {
	// NOTE: caller must, at all times be a contract. It should never happen
	// that caller is something other than a Contract.
122 123 124 125
	parent := c.caller.(*Contract)
	c.CallerAddress = parent.CallerAddress
	c.value = parent.value

126 127 128
	return c
}

129 130
// GetOp returns the n'th element in the contract's byte array
func (c *Contract) GetOp(n uint64) OpCode {
obscuren's avatar
obscuren committed
131
	return OpCode(c.GetByte(n))
132 133
}

134 135
// GetByte returns the n'th byte in the contract's byte array
func (c *Contract) GetByte(n uint64) byte {
136 137
	if n < uint64(len(c.Code)) {
		return c.Code[n]
138 139 140 141 142
	}

	return 0
}

143 144 145 146 147 148 149 150
// 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
}

151
// UseGas attempts the use gas and subtracts it and returns true on success
152 153 154
func (c *Contract) UseGas(gas uint64) (ok bool) {
	if c.Gas < gas {
		return false
155
	}
156 157
	c.Gas -= gas
	return true
158 159
}

160 161
// Address returns the contracts address
func (c *Contract) Address() common.Address {
162
	return c.self.Address()
163 164
}

165 166 167 168 169
// Value returns the contracts value (sent to it from it's caller)
func (c *Contract) Value() *big.Int {
	return c.value
}

170 171
// SetCallCode sets the code of the contract and address of the backing data
// object
172 173 174 175
func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) {
	c.Code = code
	c.CodeHash = hash
	c.CodeAddr = addr
176
}
177 178 179 180 181 182 183 184

// SetCodeOptionalHash can be used to provide code, but it's optional to provide hash.
// In case hash is not provided, the jumpdest analysis will not be saved to the parent context
func (c *Contract) SetCodeOptionalHash(addr *common.Address, codeAndHash *codeAndHash) {
	c.Code = codeAndHash.code
	c.CodeHash = codeAndHash.hash
	c.CodeAddr = addr
}