contract.go 4.7 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)
  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 *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 *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. c.UsedGas = new(big.Int)
  61. return c
  62. }
  63. // AsDelegate sets the contract to be a delegate call and returns the current
  64. // contract (for chaining calls)
  65. func (c *Contract) AsDelegate() *Contract {
  66. c.DelegateCall = true
  67. // NOTE: caller must, at all times be a contract. It should never happen
  68. // that caller is something other than a Contract.
  69. c.CallerAddress = c.caller.(*Contract).CallerAddress
  70. return c
  71. }
  72. // GetOp returns the n'th element in the contract's byte array
  73. func (c *Contract) GetOp(n uint64) OpCode {
  74. return OpCode(c.GetByte(n))
  75. }
  76. // GetByte returns the n'th byte in the contract's byte array
  77. func (c *Contract) GetByte(n uint64) byte {
  78. if n < uint64(len(c.Code)) {
  79. return c.Code[n]
  80. }
  81. return 0
  82. }
  83. // Caller returns the caller of the contract.
  84. //
  85. // Caller will recursively call caller when the contract is a delegate
  86. // call, including that of caller's caller.
  87. func (c *Contract) Caller() common.Address {
  88. return c.CallerAddress
  89. }
  90. // Finalise finalises the contract and returning any remaining gas to the original
  91. // caller.
  92. func (c *Contract) Finalise() {
  93. // Return the remaining gas to the caller
  94. c.caller.ReturnGas(c.Gas)
  95. }
  96. // UseGas attempts the use gas and subtracts it and returns true on success
  97. func (c *Contract) UseGas(gas *big.Int) (ok bool) {
  98. ok = useGas(c.Gas, gas)
  99. if ok {
  100. c.UsedGas.Add(c.UsedGas, gas)
  101. }
  102. return
  103. }
  104. // ReturnGas adds the given gas back to itself.
  105. func (c *Contract) ReturnGas(gas *big.Int) {
  106. // Return the gas to the context
  107. c.Gas.Add(c.Gas, gas)
  108. c.UsedGas.Sub(c.UsedGas, gas)
  109. }
  110. // Address returns the contracts address
  111. func (c *Contract) Address() common.Address {
  112. return c.self.Address()
  113. }
  114. // Value returns the contracts value (sent to it from it's caller)
  115. func (c *Contract) Value() *big.Int {
  116. return c.value
  117. }
  118. // SetCode sets the code to the contract
  119. func (self *Contract) SetCode(hash common.Hash, code []byte) {
  120. self.Code = code
  121. self.CodeHash = hash
  122. }
  123. // SetCallCode sets the code of the contract and address of the backing data
  124. // object
  125. func (self *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) {
  126. self.Code = code
  127. self.CodeHash = hash
  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. }