contract.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2015 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. Value() *big.Int
  26. SetCode([]byte)
  27. ForEachStorage(callback func(key, value common.Hash) bool)
  28. }
  29. // Contract represents an ethereum contract in the state database. It contains
  30. // the the contract code, calling arguments. Contract implements ContractRef
  31. type Contract struct {
  32. // CallerAddress is the result of the caller which initialised this
  33. // contract. However when the "call method" is delegated this value
  34. // needs to be initialised to that of the caller's caller.
  35. CallerAddress common.Address
  36. caller ContractRef
  37. self ContractRef
  38. jumpdests destinations // result of JUMPDEST analysis.
  39. Code []byte
  40. Input []byte
  41. CodeAddr *common.Address
  42. value, Gas, UsedGas, Price *big.Int
  43. Args []byte
  44. DelegateCall bool
  45. }
  46. // NewContract returns a new contract environment for the execution of EVM.
  47. func NewContract(caller ContractRef, object ContractRef, value, gas, price *big.Int) *Contract {
  48. c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object, Args: nil}
  49. if parent, ok := caller.(*Contract); ok {
  50. // Reuse JUMPDEST analysis from parent context if available.
  51. c.jumpdests = parent.jumpdests
  52. } else {
  53. c.jumpdests = make(destinations)
  54. }
  55. // Gas should be a pointer so it can safely be reduced through the run
  56. // This pointer will be off the state transition
  57. c.Gas = gas //new(big.Int).Set(gas)
  58. c.value = new(big.Int).Set(value)
  59. // In most cases price and value are pointers to transaction objects
  60. // and we don't want the transaction's values to change.
  61. c.Price = new(big.Int).Set(price)
  62. c.UsedGas = new(big.Int)
  63. return c
  64. }
  65. // AsDelegate sets the contract to be a delegate call and returns the current
  66. // contract (for chaining calls)
  67. func (c *Contract) AsDelegate() *Contract {
  68. c.DelegateCall = true
  69. // NOTE: caller must, at all times be a contract. It should never happen
  70. // that caller is something other than a Contract.
  71. c.CallerAddress = c.caller.(*Contract).CallerAddress
  72. return c
  73. }
  74. // GetOp returns the n'th element in the contract's byte array
  75. func (c *Contract) GetOp(n uint64) OpCode {
  76. return OpCode(c.GetByte(n))
  77. }
  78. // GetByte returns the n'th byte in the contract's byte array
  79. func (c *Contract) GetByte(n uint64) byte {
  80. if n < uint64(len(c.Code)) {
  81. return c.Code[n]
  82. }
  83. return 0
  84. }
  85. // Caller returns the caller of the contract.
  86. //
  87. // Caller will recursively call caller when the contract is a delegate
  88. // call, including that of caller's caller.
  89. func (c *Contract) Caller() common.Address {
  90. return c.CallerAddress
  91. }
  92. // Finalise finalises the contract and returning any remaining gas to the original
  93. // caller.
  94. func (c *Contract) Finalise() {
  95. // Return the remaining gas to the caller
  96. c.caller.ReturnGas(c.Gas, c.Price)
  97. }
  98. // UseGas attempts the use gas and subtracts it and returns true on success
  99. func (c *Contract) UseGas(gas *big.Int) (ok bool) {
  100. ok = useGas(c.Gas, gas)
  101. if ok {
  102. c.UsedGas.Add(c.UsedGas, gas)
  103. }
  104. return
  105. }
  106. // ReturnGas adds the given gas back to itself.
  107. func (c *Contract) ReturnGas(gas, price *big.Int) {
  108. // Return the gas to the context
  109. c.Gas.Add(c.Gas, gas)
  110. c.UsedGas.Sub(c.UsedGas, gas)
  111. }
  112. // Address returns the contracts address
  113. func (c *Contract) Address() common.Address {
  114. return c.self.Address()
  115. }
  116. // Value returns the contracts value (sent to it from it's caller)
  117. func (c *Contract) Value() *big.Int {
  118. return c.value
  119. }
  120. // SetCode sets the code to the contract
  121. func (self *Contract) SetCode(code []byte) {
  122. self.Code = code
  123. }
  124. // SetCallCode sets the code of the contract and address of the backing data
  125. // object
  126. func (self *Contract) SetCallCode(addr *common.Address, code []byte) {
  127. self.Code = code
  128. self.CodeAddr = addr
  129. }
  130. // EachStorage iterates the contract's storage and calls a method for every key
  131. // value pair.
  132. func (self *Contract) ForEachStorage(cb func(key, value common.Hash) bool) {
  133. self.caller.ForEachStorage(cb)
  134. }