contract.go 4.9 KB

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