context.go 2.27 KB
Newer Older
obscuren's avatar
obscuren committed
1
package vm
2 3

import (
4
	"math"
obscuren's avatar
obscuren committed
5 6
	"math/big"

obscuren's avatar
obscuren committed
7
	"github.com/ethereum/go-ethereum/common"
8 9
)

obscuren's avatar
obscuren committed
10
type ContextRef interface {
11 12
	ReturnGas(*big.Int, *big.Int)
	Address() []byte
13
	SetCode([]byte)
14 15
}

obscuren's avatar
obscuren committed
16
type Context struct {
obscuren's avatar
obscuren committed
17
	caller ContextRef
18
	self   ContextRef
19

20 21 22 23
	Code     []byte
	CodeAddr []byte

	value, Gas, UsedGas, Price *big.Int
24 25 26 27

	Args []byte
}

obscuren's avatar
obscuren committed
28
// Create a new context for the given data items
29 30
func NewContext(caller ContextRef, object ContextRef, value, gas, price *big.Int) *Context {
	c := &Context{caller: caller, self: object, Args: nil}
31 32 33 34

	// 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)
35
	c.value = new(big.Int).Set(value)
36 37 38 39 40 41 42 43
	// 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
}

obscuren's avatar
obscuren committed
44 45
func (c *Context) GetOp(n uint64) OpCode {
	return OpCode(c.GetByte(n))
46 47
}

obscuren's avatar
obscuren committed
48 49 50
func (c *Context) GetByte(n uint64) byte {
	if n < uint64(len(c.Code)) {
		return c.Code[n]
51 52 53 54 55
	}

	return 0
}

obscuren's avatar
obscuren committed
56
func (c *Context) GetBytes(x, y int) []byte {
57
	return c.GetRangeValue(uint64(x), uint64(y))
58 59
}

obscuren's avatar
obscuren committed
60
func (c *Context) GetRangeValue(x, size uint64) []byte {
61 62
	x = uint64(math.Min(float64(x), float64(len(c.Code))))
	y := uint64(math.Min(float64(x+size), float64(len(c.Code))))
63

obscuren's avatar
obscuren committed
64
	return common.RightPadBytes(c.Code[x:y], int(size))
65 66
}

obscuren's avatar
obscuren committed
67
func (c *Context) GetCode(x, size uint64) []byte {
68
	return getCode(c.Code, x, size)
obscuren's avatar
obscuren committed
69 70
}

obscuren's avatar
obscuren committed
71
func (c *Context) Return(ret []byte) []byte {
72 73 74 75 76 77
	// Return the remaining gas to the caller
	c.caller.ReturnGas(c.Gas, c.Price)

	return ret
}

78 79 80
/*
 * Gas functions
 */
obscuren's avatar
obscuren committed
81
func (c *Context) UseGas(gas *big.Int) bool {
82 83 84 85 86 87 88 89 90 91 92 93
	if c.Gas.Cmp(gas) < 0 {
		return false
	}

	// Sub the amount of gas from the remaining
	c.Gas.Sub(c.Gas, gas)
	c.UsedGas.Add(c.UsedGas, gas)

	return true
}

// Implement the caller interface
obscuren's avatar
obscuren committed
94 95
func (c *Context) ReturnGas(gas, price *big.Int) {
	// Return the gas to the context
96 97 98 99
	c.Gas.Add(c.Gas, gas)
	c.UsedGas.Sub(c.UsedGas, gas)
}

100 101 102
/*
 * Set / Get
 */
obscuren's avatar
obscuren committed
103
func (c *Context) Address() []byte {
104
	return c.self.Address()
105 106
}

obscuren's avatar
obscuren committed
107
func (self *Context) SetCode(code []byte) {
108 109
	self.Code = code
}
110 111 112 113 114

func (self *Context) SetCallCode(addr, code []byte) {
	self.Code = code
	self.CodeAddr = addr
}