execution.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. ret, _, err = exec(env, caller, &addr, &addr, input, env.Db().GetCode(addr), gas, gasPrice, value)
  27. return ret, err
  28. }
  29. // CallCode executes the given address' code as the given contract address
  30. func CallCode(env vm.Environment, caller vm.ContractRef, addr common.Address, input []byte, gas, gasPrice, value *big.Int) (ret []byte, err error) {
  31. callerAddr := caller.Address()
  32. ret, _, err = exec(env, caller, &callerAddr, &addr, input, env.Db().GetCode(addr), gas, gasPrice, value)
  33. return ret, err
  34. }
  35. // DelegateCall is equivalent to CallCode except that sender and value propagates from parent scope to child scope
  36. func DelegateCall(env vm.Environment, caller vm.ContractRef, addr common.Address, input []byte, gas, gasPrice *big.Int) (ret []byte, err error) {
  37. callerAddr := caller.Address()
  38. originAddr := env.Origin()
  39. callerValue := caller.Value()
  40. ret, _, err = execDelegateCall(env, caller, &originAddr, &callerAddr, &addr, input, env.Db().GetCode(addr), gas, gasPrice, callerValue)
  41. return ret, err
  42. }
  43. // Create creates a new contract with the given code
  44. func Create(env vm.Environment, caller vm.ContractRef, code []byte, gas, gasPrice, value *big.Int) (ret []byte, address common.Address, err error) {
  45. ret, address, err = exec(env, caller, nil, nil, nil, code, gas, gasPrice, value)
  46. // Here we get an error if we run into maximum stack depth,
  47. // See: https://github.com/ethereum/yellowpaper/pull/131
  48. // and YP definitions for CREATE instruction
  49. if err != nil {
  50. return nil, address, err
  51. }
  52. return ret, address, err
  53. }
  54. func exec(env vm.Environment, caller vm.ContractRef, address, codeAddr *common.Address, input, code []byte, gas, gasPrice, value *big.Int) (ret []byte, addr common.Address, err error) {
  55. evm := env.Vm()
  56. // Depth check execution. Fail if we're trying to execute above the
  57. // limit.
  58. if env.Depth() > int(params.CallCreateDepth.Int64()) {
  59. caller.ReturnGas(gas, gasPrice)
  60. return nil, common.Address{}, vm.DepthError
  61. }
  62. if !env.CanTransfer(caller.Address(), value) {
  63. caller.ReturnGas(gas, gasPrice)
  64. return nil, common.Address{}, ValueTransferErr("insufficient funds to transfer value. Req %v, has %v", value, env.Db().GetBalance(caller.Address()))
  65. }
  66. var createAccount bool
  67. if address == nil {
  68. // Create a new account on the state
  69. nonce := env.Db().GetNonce(caller.Address())
  70. env.Db().SetNonce(caller.Address(), nonce+1)
  71. addr = crypto.CreateAddress(caller.Address(), nonce)
  72. address = &addr
  73. createAccount = true
  74. }
  75. // mark the code hash if the execution is a call, callcode or delegate.
  76. if value.Cmp(common.Big0) > 0 {
  77. env.MarkCodeHash(env.Db().GetCodeHash(caller.Address()))
  78. }
  79. snapshotPreTransfer := env.MakeSnapshot()
  80. var (
  81. from = env.Db().GetAccount(caller.Address())
  82. to vm.Account
  83. )
  84. if createAccount {
  85. to = env.Db().CreateAccount(*address)
  86. } else {
  87. if !env.Db().Exist(*address) {
  88. to = env.Db().CreateAccount(*address)
  89. } else {
  90. to = env.Db().GetAccount(*address)
  91. }
  92. }
  93. env.Transfer(from, to, value)
  94. // initialise a new contract and set the code that is to be used by the
  95. // EVM. The contract is a scoped environment for this execution context
  96. // only.
  97. contract := vm.NewContract(caller, to, value, gas, gasPrice)
  98. contract.SetCallCode(codeAddr, code)
  99. defer contract.Finalise()
  100. ret, err = evm.Run(contract, input)
  101. // if the contract creation ran successfully and no errors were returned
  102. // calculate the gas required to store the code. If the code could not
  103. // be stored due to not enough gas set an error and let it be handled
  104. // by the error checking condition below.
  105. if err == nil && createAccount {
  106. dataGas := big.NewInt(int64(len(ret)))
  107. dataGas.Mul(dataGas, params.CreateDataGas)
  108. if contract.UseGas(dataGas) {
  109. env.Db().SetCode(*address, ret)
  110. } else {
  111. err = vm.CodeStoreOutOfGasError
  112. }
  113. }
  114. // When an error was returned by the EVM or when setting the creation code
  115. // above we revert to the snapshot and consume any gas remaining. Additionally
  116. // when we're in homestead this also counts for code storage gas errors.
  117. if err != nil && (env.RuleSet().IsHomestead(env.BlockNumber()) || err != vm.CodeStoreOutOfGasError) {
  118. contract.UseGas(contract.Gas)
  119. env.SetSnapshot(snapshotPreTransfer)
  120. }
  121. return ret, addr, err
  122. }
  123. func execDelegateCall(env vm.Environment, caller vm.ContractRef, originAddr, toAddr, codeAddr *common.Address, input, code []byte, gas, gasPrice, value *big.Int) (ret []byte, addr common.Address, err error) {
  124. evm := env.Vm()
  125. // Depth check execution. Fail if we're trying to execute above the
  126. // limit.
  127. if env.Depth() > int(params.CallCreateDepth.Int64()) {
  128. caller.ReturnGas(gas, gasPrice)
  129. return nil, common.Address{}, vm.DepthError
  130. }
  131. if value.Cmp(common.Big0) > 0 {
  132. env.MarkCodeHash(env.Db().GetCodeHash(caller.Address()))
  133. }
  134. snapshot := env.MakeSnapshot()
  135. var to vm.Account
  136. if !env.Db().Exist(*toAddr) {
  137. to = env.Db().CreateAccount(*toAddr)
  138. } else {
  139. to = env.Db().GetAccount(*toAddr)
  140. }
  141. // Iinitialise a new contract and make initialise the delegate values
  142. contract := vm.NewContract(caller, to, value, gas, gasPrice).AsDelegate()
  143. contract.SetCallCode(codeAddr, code)
  144. defer contract.Finalise()
  145. ret, err = evm.Run(contract, input)
  146. if err != nil {
  147. contract.UseGas(contract.Gas)
  148. env.SetSnapshot(snapshot)
  149. }
  150. return ret, addr, err
  151. }
  152. // generic transfer method
  153. func Transfer(from, to vm.Account, amount *big.Int) {
  154. from.SubBalance(amount)
  155. to.AddBalance(amount)
  156. }