contract.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. Address() common.Address
  24. }
  25. // AccountRef implements ContractRef.
  26. //
  27. // Account references are used during EVM initialisation and
  28. // it's primary use is to fetch addresses. Removing this object
  29. // proves difficult because of the cached jump destinations which
  30. // are fetched from the parent contract (i.e. the caller), which
  31. // is a ContractRef.
  32. type AccountRef common.Address
  33. // Address casts AccountRef to a Address
  34. func (ar AccountRef) Address() common.Address { return (common.Address)(ar) }
  35. // Contract represents an ethereum contract in the state database. It contains
  36. // the the contract code, calling arguments. Contract implements ContractRef
  37. type Contract struct {
  38. // CallerAddress is the result of the caller which initialised this
  39. // contract. However when the "call method" is delegated this value
  40. // needs to be initialised to that of the caller's caller.
  41. CallerAddress common.Address
  42. caller ContractRef
  43. self ContractRef
  44. jumpdests destinations // result of JUMPDEST analysis.
  45. Code []byte
  46. CodeHash common.Hash
  47. CodeAddr *common.Address
  48. Input []byte
  49. Gas uint64
  50. value *big.Int
  51. Args []byte
  52. DelegateCall bool
  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, Args: nil}
  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(destinations)
  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. // AsDelegate sets the contract to be a delegate call and returns the current
  71. // contract (for chaining calls)
  72. func (c *Contract) AsDelegate() *Contract {
  73. c.DelegateCall = true
  74. // NOTE: caller must, at all times be a contract. It should never happen
  75. // that caller is something other than a Contract.
  76. parent := c.caller.(*Contract)
  77. c.CallerAddress = parent.CallerAddress
  78. c.value = parent.value
  79. return c
  80. }
  81. // GetOp returns the n'th element in the contract's byte array
  82. func (c *Contract) GetOp(n uint64) OpCode {
  83. return OpCode(c.GetByte(n))
  84. }
  85. // GetByte returns the n'th byte in the contract's byte array
  86. func (c *Contract) GetByte(n uint64) byte {
  87. if n < uint64(len(c.Code)) {
  88. return c.Code[n]
  89. }
  90. return 0
  91. }
  92. // Caller returns the caller of the contract.
  93. //
  94. // Caller will recursively call caller when the contract is a delegate
  95. // call, including that of caller's caller.
  96. func (c *Contract) Caller() common.Address {
  97. return c.CallerAddress
  98. }
  99. // UseGas attempts the use gas and subtracts it and returns true on success
  100. func (c *Contract) UseGas(gas uint64) (ok bool) {
  101. if c.Gas < gas {
  102. return false
  103. }
  104. c.Gas -= gas
  105. return true
  106. }
  107. // Address returns the contracts address
  108. func (c *Contract) Address() common.Address {
  109. return c.self.Address()
  110. }
  111. // Value returns the contracts value (sent to it from it's caller)
  112. func (c *Contract) Value() *big.Int {
  113. return c.value
  114. }
  115. // SetCode sets the code to the contract
  116. func (self *Contract) SetCode(hash common.Hash, code []byte) {
  117. self.Code = code
  118. self.CodeHash = hash
  119. }
  120. // SetCallCode sets the code of the contract and address of the backing data
  121. // object
  122. func (self *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) {
  123. self.Code = code
  124. self.CodeHash = hash
  125. self.CodeAddr = addr
  126. }