context.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. package vm
  17. import (
  18. "math/big"
  19. "github.com/ethereum/go-ethereum/common"
  20. )
  21. type ContextRef interface {
  22. ReturnGas(*big.Int, *big.Int)
  23. Address() common.Address
  24. SetCode([]byte)
  25. }
  26. type Context struct {
  27. caller ContextRef
  28. self ContextRef
  29. jumpdests destinations // result of JUMPDEST analysis.
  30. Code []byte
  31. Input []byte
  32. CodeAddr *common.Address
  33. value, Gas, UsedGas, Price *big.Int
  34. Args []byte
  35. }
  36. // Create a new context for the given data items.
  37. func NewContext(caller ContextRef, object ContextRef, value, gas, price *big.Int) *Context {
  38. c := &Context{caller: caller, self: object, Args: nil}
  39. if parent, ok := caller.(*Context); ok {
  40. // Reuse JUMPDEST analysis from parent context if available.
  41. c.jumpdests = parent.jumpdests
  42. } else {
  43. c.jumpdests = make(destinations)
  44. }
  45. // Gas should be a pointer so it can safely be reduced through the run
  46. // This pointer will be off the state transition
  47. c.Gas = gas //new(big.Int).Set(gas)
  48. c.value = new(big.Int).Set(value)
  49. // In most cases price and value are pointers to transaction objects
  50. // and we don't want the transaction's values to change.
  51. c.Price = new(big.Int).Set(price)
  52. c.UsedGas = new(big.Int)
  53. return c
  54. }
  55. func (c *Context) GetOp(n uint64) OpCode {
  56. return OpCode(c.GetByte(n))
  57. }
  58. func (c *Context) GetByte(n uint64) byte {
  59. if n < uint64(len(c.Code)) {
  60. return c.Code[n]
  61. }
  62. return 0
  63. }
  64. func (c *Context) Return(ret []byte) []byte {
  65. // Return the remaining gas to the caller
  66. c.caller.ReturnGas(c.Gas, c.Price)
  67. return ret
  68. }
  69. /*
  70. * Gas functions
  71. */
  72. func (c *Context) UseGas(gas *big.Int) (ok bool) {
  73. ok = UseGas(c.Gas, gas)
  74. if ok {
  75. c.UsedGas.Add(c.UsedGas, gas)
  76. }
  77. return
  78. }
  79. // Implement the caller interface
  80. func (c *Context) ReturnGas(gas, price *big.Int) {
  81. // Return the gas to the context
  82. c.Gas.Add(c.Gas, gas)
  83. c.UsedGas.Sub(c.UsedGas, gas)
  84. }
  85. /*
  86. * Set / Get
  87. */
  88. func (c *Context) Address() common.Address {
  89. return c.self.Address()
  90. }
  91. func (self *Context) SetCode(code []byte) {
  92. self.Code = code
  93. }
  94. func (self *Context) SetCallCode(addr *common.Address, code []byte) {
  95. self.Code = code
  96. self.CodeAddr = addr
  97. }