execution.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright 2014 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 core
  17. import (
  18. "math/big"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/core/vm"
  21. "github.com/ethereum/go-ethereum/crypto"
  22. "github.com/ethereum/go-ethereum/params"
  23. )
  24. // Call executes within the given contract
  25. func Call(env vm.Environment, caller vm.ContractRef, addr common.Address, input []byte, gas, gasPrice, value *big.Int) (ret []byte, err error) {
  26. // Depth check execution. Fail if we're trying to execute above the
  27. // limit.
  28. if env.Depth() > int(params.CallCreateDepth.Int64()) {
  29. caller.ReturnGas(gas, gasPrice)
  30. return nil, vm.DepthError
  31. }
  32. if !env.CanTransfer(caller.Address(), value) {
  33. caller.ReturnGas(gas, gasPrice)
  34. return nil, ValueTransferErr("insufficient funds to transfer value. Req %v, has %v", value, env.Db().GetBalance(caller.Address()))
  35. }
  36. snapshotPreTransfer := env.SnapshotDatabase()
  37. var (
  38. from = env.Db().GetAccount(caller.Address())
  39. to vm.Account
  40. )
  41. if !env.Db().Exist(addr) {
  42. if vm.Precompiled[addr.Str()] == nil && env.ChainConfig().IsEIP158(env.BlockNumber()) && value.BitLen() == 0 {
  43. caller.ReturnGas(gas, gasPrice)
  44. return nil, nil
  45. }
  46. to = env.Db().CreateAccount(addr)
  47. } else {
  48. to = env.Db().GetAccount(addr)
  49. }
  50. env.Transfer(from, to, value)
  51. // initialise a new contract and set the code that is to be used by the
  52. // EVM. The contract is a scoped environment for this execution context
  53. // only.
  54. contract := vm.NewContract(caller, to, value, gas, gasPrice)
  55. contract.SetCallCode(&addr, env.Db().GetCodeHash(addr), env.Db().GetCode(addr))
  56. defer contract.Finalise()
  57. ret, err = env.Vm().Run(contract, input)
  58. // When an error was returned by the EVM or when setting the creation code
  59. // above we revert to the snapshot and consume any gas remaining. Additionally
  60. // when we're in homestead this also counts for code storage gas errors.
  61. if err != nil {
  62. contract.UseGas(contract.Gas)
  63. env.RevertToSnapshot(snapshotPreTransfer)
  64. }
  65. return ret, err
  66. }
  67. // CallCode executes the given address' code as the given contract address
  68. func CallCode(env vm.Environment, caller vm.ContractRef, addr common.Address, input []byte, gas, gasPrice, value *big.Int) (ret []byte, err error) {
  69. // Depth check execution. Fail if we're trying to execute above the
  70. // limit.
  71. if env.Depth() > int(params.CallCreateDepth.Int64()) {
  72. caller.ReturnGas(gas, gasPrice)
  73. return nil, vm.DepthError
  74. }
  75. if !env.CanTransfer(caller.Address(), value) {
  76. caller.ReturnGas(gas, gasPrice)
  77. return nil, ValueTransferErr("insufficient funds to transfer value. Req %v, has %v", value, env.Db().GetBalance(caller.Address()))
  78. }
  79. var (
  80. snapshotPreTransfer = env.SnapshotDatabase()
  81. to = env.Db().GetAccount(caller.Address())
  82. )
  83. // initialise a new contract and set the code that is to be used by the
  84. // EVM. The contract is a scoped environment for this execution context
  85. // only.
  86. contract := vm.NewContract(caller, to, value, gas, gasPrice)
  87. contract.SetCallCode(&addr, env.Db().GetCodeHash(addr), env.Db().GetCode(addr))
  88. defer contract.Finalise()
  89. ret, err = env.Vm().Run(contract, input)
  90. if err != nil {
  91. contract.UseGas(contract.Gas)
  92. env.RevertToSnapshot(snapshotPreTransfer)
  93. }
  94. return ret, err
  95. }
  96. // Create creates a new contract with the given code
  97. func Create(env vm.Environment, caller vm.ContractRef, code []byte, gas, gasPrice, value *big.Int) (ret []byte, address common.Address, err error) {
  98. // Depth check execution. Fail if we're trying to execute above the
  99. // limit.
  100. if env.Depth() > int(params.CallCreateDepth.Int64()) {
  101. caller.ReturnGas(gas, gasPrice)
  102. return nil, common.Address{}, vm.DepthError
  103. }
  104. if !env.CanTransfer(caller.Address(), value) {
  105. caller.ReturnGas(gas, gasPrice)
  106. return nil, common.Address{}, ValueTransferErr("insufficient funds to transfer value. Req %v, has %v", value, env.Db().GetBalance(caller.Address()))
  107. }
  108. // Create a new account on the state
  109. nonce := env.Db().GetNonce(caller.Address())
  110. env.Db().SetNonce(caller.Address(), nonce+1)
  111. snapshotPreTransfer := env.SnapshotDatabase()
  112. var (
  113. addr = crypto.CreateAddress(caller.Address(), nonce)
  114. from = env.Db().GetAccount(caller.Address())
  115. to = env.Db().CreateAccount(addr)
  116. )
  117. if env.ChainConfig().IsEIP158(env.BlockNumber()) {
  118. env.Db().SetNonce(addr, 1)
  119. }
  120. env.Transfer(from, to, value)
  121. // initialise a new contract and set the code that is to be used by the
  122. // EVM. The contract is a scoped environment for this execution context
  123. // only.
  124. contract := vm.NewContract(caller, to, value, gas, gasPrice)
  125. contract.SetCallCode(&addr, crypto.Keccak256Hash(code), code)
  126. defer contract.Finalise()
  127. ret, err = env.Vm().Run(contract, nil)
  128. // check whether the max code size has been exceeded
  129. maxCodeSizeExceeded := len(ret) > params.MaxCodeSize
  130. // if the contract creation ran successfully and no errors were returned
  131. // calculate the gas required to store the code. If the code could not
  132. // be stored due to not enough gas set an error and let it be handled
  133. // by the error checking condition below.
  134. if err == nil && !maxCodeSizeExceeded {
  135. dataGas := big.NewInt(int64(len(ret)))
  136. dataGas.Mul(dataGas, params.CreateDataGas)
  137. if contract.UseGas(dataGas) {
  138. env.Db().SetCode(addr, ret)
  139. } else {
  140. err = vm.CodeStoreOutOfGasError
  141. }
  142. }
  143. // When an error was returned by the EVM or when setting the creation code
  144. // above we revert to the snapshot and consume any gas remaining. Additionally
  145. // when we're in homestead this also counts for code storage gas errors.
  146. if maxCodeSizeExceeded ||
  147. (err != nil && (env.ChainConfig().IsHomestead(env.BlockNumber()) || err != vm.CodeStoreOutOfGasError)) {
  148. contract.UseGas(contract.Gas)
  149. env.RevertToSnapshot(snapshotPreTransfer)
  150. // Nothing should be returned when an error is thrown.
  151. return nil, addr, err
  152. }
  153. return ret, addr, err
  154. }
  155. // DelegateCall is equivalent to CallCode except that sender and value propagates from parent scope to child scope
  156. func DelegateCall(env vm.Environment, caller vm.ContractRef, addr common.Address, input []byte, gas, gasPrice *big.Int) (ret []byte, err error) {
  157. // Depth check execution. Fail if we're trying to execute above the
  158. // limit.
  159. if env.Depth() > int(params.CallCreateDepth.Int64()) {
  160. caller.ReturnGas(gas, gasPrice)
  161. return nil, vm.DepthError
  162. }
  163. var (
  164. snapshot = env.SnapshotDatabase()
  165. to = env.Db().GetAccount(caller.Address())
  166. )
  167. // Iinitialise a new contract and make initialise the delegate values
  168. contract := vm.NewContract(caller, to, caller.Value(), gas, gasPrice).AsDelegate()
  169. contract.SetCallCode(&addr, env.Db().GetCodeHash(addr), env.Db().GetCode(addr))
  170. defer contract.Finalise()
  171. ret, err = env.Vm().Run(contract, input)
  172. if err != nil {
  173. contract.UseGas(contract.Gas)
  174. env.RevertToSnapshot(snapshot)
  175. }
  176. return ret, err
  177. }
  178. // generic transfer method
  179. func Transfer(from, to vm.Account, amount *big.Int) {
  180. from.SubBalance(amount)
  181. to.AddBalance(amount)
  182. }