contract.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. // ContractRef is a reference to the contract's backing object
  22. type ContractRef interface {
  23. ReturnGas(*big.Int, *big.Int)
  24. Address() common.Address
  25. SetAddress(common.Address)
  26. Value() *big.Int
  27. SetCode([]byte)
  28. EachStorage(cb func(key, value []byte))
  29. }
  30. // Contract represents an ethereum contract in the state database. It contains
  31. // the the contract code, calling arguments. Contract implements ContractRef
  32. type Contract struct {
  33. caller ContractRef
  34. self ContractRef
  35. jumpdests destinations // result of JUMPDEST analysis.
  36. Code []byte
  37. Input []byte
  38. CodeAddr *common.Address
  39. value, Gas, UsedGas, Price *big.Int
  40. Args []byte
  41. DelegateCall bool
  42. }
  43. // Create a new context for the given data items.
  44. func NewContract(caller ContractRef, object ContractRef, value, gas, price *big.Int) *Contract {
  45. c := &Contract{caller: caller, self: object, Args: nil}
  46. if parent, ok := caller.(*Contract); ok {
  47. // Reuse JUMPDEST analysis from parent context if available.
  48. c.jumpdests = parent.jumpdests
  49. } else {
  50. c.jumpdests = make(destinations)
  51. }
  52. // Gas should be a pointer so it can safely be reduced through the run
  53. // This pointer will be off the state transition
  54. c.Gas = gas //new(big.Int).Set(gas)
  55. c.value = new(big.Int).Set(value)
  56. // In most cases price and value are pointers to transaction objects
  57. // and we don't want the transaction's values to change.
  58. c.Price = new(big.Int).Set(price)
  59. c.UsedGas = new(big.Int)
  60. return c
  61. }
  62. // GetOp returns the n'th element in the contract's byte array
  63. func (c *Contract) GetOp(n uint64) OpCode {
  64. return OpCode(c.GetByte(n))
  65. }
  66. // GetByte returns the n'th byte in the contract's byte array
  67. func (c *Contract) GetByte(n uint64) byte {
  68. if n < uint64(len(c.Code)) {
  69. return c.Code[n]
  70. }
  71. return 0
  72. }
  73. // Return returns the given ret argument and returns any remaining gas to the
  74. // caller
  75. func (c *Contract) Return(ret []byte) []byte {
  76. // Return the remaining gas to the caller
  77. c.caller.ReturnGas(c.Gas, c.Price)
  78. return ret
  79. }
  80. // UseGas attempts the use gas and subtracts it and returns true on success
  81. func (c *Contract) UseGas(gas *big.Int) (ok bool) {
  82. ok = useGas(c.Gas, gas)
  83. if ok {
  84. c.UsedGas.Add(c.UsedGas, gas)
  85. }
  86. return
  87. }
  88. // ReturnGas adds the given gas back to itself.
  89. func (c *Contract) ReturnGas(gas, price *big.Int) {
  90. // Return the gas to the context
  91. c.Gas.Add(c.Gas, gas)
  92. c.UsedGas.Sub(c.UsedGas, gas)
  93. }
  94. // Address returns the contracts address
  95. func (c *Contract) Address() common.Address {
  96. return c.self.Address()
  97. }
  98. // SetAddress sets the contracts address
  99. func (c *Contract) SetAddress(addr common.Address) {
  100. c.self.SetAddress(addr)
  101. }
  102. // Value returns the contracts value (sent to it from it's caller)
  103. func (c *Contract) Value() *big.Int {
  104. return c.value
  105. }
  106. // SetCode sets the code to the contract
  107. func (self *Contract) SetCode(code []byte) {
  108. self.Code = code
  109. }
  110. // SetCallCode sets the code of the contract and address of the backing data
  111. // object
  112. func (self *Contract) SetCallCode(addr *common.Address, code []byte) {
  113. self.Code = code
  114. self.CodeAddr = addr
  115. }
  116. // EachStorage iterates the contract's storage and calls a method for every key
  117. // value pair.
  118. func (self *Contract) EachStorage(cb func(key, value []byte)) {
  119. self.caller.EachStorage(cb)
  120. }