contract.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. "github.com/holiman/uint256"
  21. )
  22. // ContractRef is a reference to the contract's backing object
  23. type ContractRef interface {
  24. Address() common.Address
  25. }
  26. // AccountRef implements ContractRef.
  27. //
  28. // Account references are used during EVM initialisation and
  29. // it's primary use is to fetch addresses. Removing this object
  30. // proves difficult because of the cached jump destinations which
  31. // are fetched from the parent contract (i.e. the caller), which
  32. // is a ContractRef.
  33. type AccountRef common.Address
  34. // Address casts AccountRef to a Address
  35. func (ar AccountRef) Address() common.Address { return (common.Address)(ar) }
  36. // Contract represents an ethereum contract in the state database. It contains
  37. // the contract code, calling arguments. Contract implements ContractRef
  38. type Contract struct {
  39. // CallerAddress is the result of the caller which initialised this
  40. // contract. However when the "call method" is delegated this value
  41. // needs to be initialised to that of the caller's caller.
  42. CallerAddress common.Address
  43. caller ContractRef
  44. self ContractRef
  45. jumpdests map[common.Hash]bitvec // Aggregated result of JUMPDEST analysis.
  46. analysis bitvec // Locally cached result of JUMPDEST analysis
  47. Code []byte
  48. CodeHash common.Hash
  49. CodeAddr *common.Address
  50. Input []byte
  51. Gas uint64
  52. value *big.Int
  53. }
  54. // NewContract returns a new contract environment for the execution of EVM.
  55. func NewContract(caller ContractRef, object ContractRef, value *big.Int, gas uint64) *Contract {
  56. c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object}
  57. if parent, ok := caller.(*Contract); ok {
  58. // Reuse JUMPDEST analysis from parent context if available.
  59. c.jumpdests = parent.jumpdests
  60. } else {
  61. c.jumpdests = make(map[common.Hash]bitvec)
  62. }
  63. // Gas should be a pointer so it can safely be reduced through the run
  64. // This pointer will be off the state transition
  65. c.Gas = gas
  66. // ensures a value is set
  67. c.value = value
  68. return c
  69. }
  70. func (c *Contract) validJumpdest(dest *uint256.Int) bool {
  71. udest, overflow := dest.Uint64WithOverflow()
  72. // PC cannot go beyond len(code) and certainly can't be bigger than 63bits.
  73. // Don't bother checking for JUMPDEST in that case.
  74. if overflow || udest >= uint64(len(c.Code)) {
  75. return false
  76. }
  77. // Only JUMPDESTs allowed for destinations
  78. if OpCode(c.Code[udest]) != JUMPDEST {
  79. return false
  80. }
  81. return c.isCode(udest)
  82. }
  83. // isCode returns true if the provided PC location is an actual opcode, as
  84. // opposed to a data-segment following a PUSHN operation.
  85. func (c *Contract) isCode(udest uint64) bool {
  86. // Do we already have an analysis laying around?
  87. if c.analysis != nil {
  88. return c.analysis.codeSegment(udest)
  89. }
  90. // Do we have a contract hash already?
  91. // If we do have a hash, that means it's a 'regular' contract. For regular
  92. // contracts ( not temporary initcode), we store the analysis in a map
  93. if c.CodeHash != (common.Hash{}) {
  94. // Does parent context have the analysis?
  95. analysis, exist := c.jumpdests[c.CodeHash]
  96. if !exist {
  97. // Do the analysis and save in parent context
  98. // We do not need to store it in c.analysis
  99. analysis = codeBitmap(c.Code)
  100. c.jumpdests[c.CodeHash] = analysis
  101. }
  102. // Also stash it in current contract for faster access
  103. c.analysis = analysis
  104. return analysis.codeSegment(udest)
  105. }
  106. // We don't have the code hash, most likely a piece of initcode not already
  107. // in state trie. In that case, we do an analysis, and save it locally, so
  108. // we don't have to recalculate it for every JUMP instruction in the execution
  109. // However, we don't save it within the parent context
  110. if c.analysis == nil {
  111. c.analysis = codeBitmap(c.Code)
  112. }
  113. return c.analysis.codeSegment(udest)
  114. }
  115. // AsDelegate sets the contract to be a delegate call and returns the current
  116. // contract (for chaining calls)
  117. func (c *Contract) AsDelegate() *Contract {
  118. // NOTE: caller must, at all times be a contract. It should never happen
  119. // that caller is something other than a Contract.
  120. parent := c.caller.(*Contract)
  121. c.CallerAddress = parent.CallerAddress
  122. c.value = parent.value
  123. return c
  124. }
  125. // GetOp returns the n'th element in the contract's byte array
  126. func (c *Contract) GetOp(n uint64) OpCode {
  127. return OpCode(c.GetByte(n))
  128. }
  129. // GetByte returns the n'th byte in the contract's byte array
  130. func (c *Contract) GetByte(n uint64) byte {
  131. if n < uint64(len(c.Code)) {
  132. return c.Code[n]
  133. }
  134. return 0
  135. }
  136. // Caller returns the caller of the contract.
  137. //
  138. // Caller will recursively call caller when the contract is a delegate
  139. // call, including that of caller's caller.
  140. func (c *Contract) Caller() common.Address {
  141. return c.CallerAddress
  142. }
  143. // UseGas attempts the use gas and subtracts it and returns true on success
  144. func (c *Contract) UseGas(gas uint64) (ok bool) {
  145. if c.Gas < gas {
  146. return false
  147. }
  148. c.Gas -= gas
  149. return true
  150. }
  151. // Address returns the contracts address
  152. func (c *Contract) Address() common.Address {
  153. return c.self.Address()
  154. }
  155. // Value returns the contract's value (sent to it from it's caller)
  156. func (c *Contract) Value() *big.Int {
  157. return c.value
  158. }
  159. // SetCallCode sets the code of the contract and address of the backing data
  160. // object
  161. func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) {
  162. c.Code = code
  163. c.CodeHash = hash
  164. c.CodeAddr = addr
  165. }
  166. // SetCodeOptionalHash can be used to provide code, but it's optional to provide hash.
  167. // In case hash is not provided, the jumpdest analysis will not be saved to the parent context
  168. func (c *Contract) SetCodeOptionalHash(addr *common.Address, codeAndHash *codeAndHash) {
  169. c.Code = codeAndHash.code
  170. c.CodeHash = codeAndHash.hash
  171. c.CodeAddr = addr
  172. }