github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/core/vm/context.go (about) 1 // Copyright 2014 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 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 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 // GNU Lesser General Public License for more details. 13 // 14 // 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 17 package vm 18 19 import ( 20 "math/big" 21 22 "github.com/ethereum/go-ethereum/common" 23 ) 24 25 type ContextRef interface { 26 ReturnGas(*big.Int, *big.Int) 27 Address() common.Address 28 SetCode([]byte) 29 } 30 31 type Context struct { 32 caller ContextRef 33 self ContextRef 34 35 jumpdests destinations // result of JUMPDEST analysis. 36 37 Code []byte 38 Input []byte 39 CodeAddr *common.Address 40 41 value, Gas, UsedGas, Price *big.Int 42 43 Args []byte 44 } 45 46 // Create a new context for the given data items. 47 func NewContext(caller ContextRef, object ContextRef, value, gas, price *big.Int) *Context { 48 c := &Context{caller: caller, self: object, Args: nil} 49 50 if parent, ok := caller.(*Context); ok { 51 // Reuse JUMPDEST analysis from parent context if available. 52 c.jumpdests = parent.jumpdests 53 } else { 54 c.jumpdests = make(destinations) 55 } 56 57 // Gas should be a pointer so it can safely be reduced through the run 58 // This pointer will be off the state transition 59 c.Gas = gas //new(big.Int).Set(gas) 60 c.value = new(big.Int).Set(value) 61 // In most cases price and value are pointers to transaction objects 62 // and we don't want the transaction's values to change. 63 c.Price = new(big.Int).Set(price) 64 c.UsedGas = new(big.Int) 65 66 return c 67 } 68 69 func (c *Context) GetOp(n uint64) OpCode { 70 return OpCode(c.GetByte(n)) 71 } 72 73 func (c *Context) GetByte(n uint64) byte { 74 if n < uint64(len(c.Code)) { 75 return c.Code[n] 76 } 77 78 return 0 79 } 80 81 func (c *Context) Return(ret []byte) []byte { 82 // Return the remaining gas to the caller 83 c.caller.ReturnGas(c.Gas, c.Price) 84 85 return ret 86 } 87 88 /* 89 * Gas functions 90 */ 91 func (c *Context) UseGas(gas *big.Int) (ok bool) { 92 ok = UseGas(c.Gas, gas) 93 if ok { 94 c.UsedGas.Add(c.UsedGas, gas) 95 } 96 return 97 } 98 99 // Implement the caller interface 100 func (c *Context) ReturnGas(gas, price *big.Int) { 101 // Return the gas to the context 102 c.Gas.Add(c.Gas, gas) 103 c.UsedGas.Sub(c.UsedGas, gas) 104 } 105 106 /* 107 * Set / Get 108 */ 109 func (c *Context) Address() common.Address { 110 return c.self.Address() 111 } 112 113 func (self *Context) SetCode(code []byte) { 114 self.Code = code 115 } 116 117 func (self *Context) SetCallCode(addr *common.Address, code []byte) { 118 self.Code = code 119 self.CodeAddr = addr 120 }