execution.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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, env.Db().GetCodeHash(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, env.Db().GetCodeHash(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, env.Db().GetCodeHash(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, crypto.Keccak256Hash(code), 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, codeHash common.Hash, 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. snapshotPreTransfer := env.MakeSnapshot()
  76. var (
  77. from = env.Db().GetAccount(caller.Address())
  78. to vm.Account
  79. )
  80. if createAccount {
  81. to = env.Db().CreateAccount(*address)
  82. } else {
  83. if !env.Db().Exist(*address) {
  84. to = env.Db().CreateAccount(*address)
  85. } else {
  86. to = env.Db().GetAccount(*address)
  87. }
  88. }
  89. env.Transfer(from, to, value)
  90. // initialise a new contract and set the code that is to be used by the
  91. // EVM. The contract is a scoped environment for this execution context
  92. // only.
  93. contract := vm.NewContract(caller, to, value, gas, gasPrice)
  94. contract.SetCallCode(codeAddr, codeHash, code)
  95. defer contract.Finalise()
  96. ret, err = evm.Run(contract, input)
  97. // if the contract creation ran successfully and no errors were returned
  98. // calculate the gas required to store the code. If the code could not
  99. // be stored due to not enough gas set an error and let it be handled
  100. // by the error checking condition below.
  101. if err == nil && createAccount {
  102. dataGas := big.NewInt(int64(len(ret)))
  103. dataGas.Mul(dataGas, params.CreateDataGas)
  104. if contract.UseGas(dataGas) {
  105. env.Db().SetCode(*address, ret)
  106. } else {
  107. err = vm.CodeStoreOutOfGasError
  108. }
  109. }
  110. // When an error was returned by the EVM or when setting the creation code
  111. // above we revert to the snapshot and consume any gas remaining. Additionally
  112. // when we're in homestead this also counts for code storage gas errors.
  113. if err != nil && (env.RuleSet().IsHomestead(env.BlockNumber()) || err != vm.CodeStoreOutOfGasError) {
  114. contract.UseGas(contract.Gas)
  115. env.SetSnapshot(snapshotPreTransfer)
  116. }
  117. return ret, addr, err
  118. }
  119. func execDelegateCall(env vm.Environment, caller vm.ContractRef, originAddr, toAddr, codeAddr *common.Address, codeHash common.Hash, input, code []byte, gas, gasPrice, value *big.Int) (ret []byte, addr common.Address, err error) {
  120. evm := env.Vm()
  121. // Depth check execution. Fail if we're trying to execute above the
  122. // limit.
  123. if env.Depth() > int(params.CallCreateDepth.Int64()) {
  124. caller.ReturnGas(gas, gasPrice)
  125. return nil, common.Address{}, vm.DepthError
  126. }
  127. snapshot := env.MakeSnapshot()
  128. var to vm.Account
  129. if !env.Db().Exist(*toAddr) {
  130. to = env.Db().CreateAccount(*toAddr)
  131. } else {
  132. to = env.Db().GetAccount(*toAddr)
  133. }
  134. // Iinitialise a new contract and make initialise the delegate values
  135. contract := vm.NewContract(caller, to, value, gas, gasPrice).AsDelegate()
  136. contract.SetCallCode(codeAddr, codeHash, code)
  137. defer contract.Finalise()
  138. ret, err = evm.Run(contract, input)
  139. if err != nil {
  140. contract.UseGas(contract.Gas)
  141. env.SetSnapshot(snapshot)
  142. }
  143. return ret, addr, err
  144. }
  145. // generic transfer method
  146. func Transfer(from, to vm.Account, amount *big.Int) {
  147. from.SubBalance(amount)
  148. to.AddBalance(amount)
  149. }