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"

7
	"github.com/ethereum/go-ethereum/ethutil"
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 18 19
	caller ContextRef
	object ContextRef
	Code   []byte
20 21 22 23 24 25

	Gas, UsedGas, Price *big.Int

	Args []byte
}

obscuren's avatar
obscuren committed
26
// Create a new context for the given data items
obscuren's avatar
obscuren committed
27 28
func NewContext(caller ContextRef, object ContextRef, code []byte, gas, price *big.Int) *Context {
	c := &Context{caller: caller, object: object, Code: code, Args: nil}
29 30 31 32 33 34 35 36 37 38 39 40

	// 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)
	// 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
41 42
func (c *Context) GetOp(n uint64) OpCode {
	return OpCode(c.GetByte(n))
43 44
}

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

	return 0
}

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

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

obscuren's avatar
obscuren committed
61
	return ethutil.RightPadBytes(c.Code[x:y], int(size))
62 63
}

obscuren's avatar
obscuren committed
64 65 66 67 68 69 70
func (c *Context) GetCode(x, size uint64) []byte {
	x = uint64(math.Min(float64(x), float64(len(c.Code))))
	y := uint64(math.Min(float64(x+size), float64(len(c.Code))))

	return ethutil.RightPadBytes(c.Code[x:y], int(size))
}

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 105 106
	return c.object.Address()
}

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