execution.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. caller.SetAddress(callerAddr)
  42. return ret, err
  43. }
  44. // Create creates a new contract with the given code
  45. func Create(env vm.Environment, caller vm.ContractRef, code []byte, gas, gasPrice, value *big.Int) (ret []byte, address common.Address, err error) {
  46. ret, address, err = exec(env, caller, nil, nil, nil, code, gas, gasPrice, value)
  47. // Here we get an error if we run into maximum stack depth,
  48. // See: https://github.com/ethereum/yellowpaper/pull/131
  49. // and YP definitions for CREATE instruction
  50. if err != nil {
  51. return nil, address, err
  52. }
  53. return ret, address, err
  54. }
  55. 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) {
  56. evm := vm.NewVm(env)
  57. // Depth check execution. Fail if we're trying to execute above the
  58. // limit.
  59. if env.Depth() > int(params.CallCreateDepth.Int64()) {
  60. caller.ReturnGas(gas, gasPrice)
  61. return nil, common.Address{}, vm.DepthError
  62. }
  63. if !env.CanTransfer(caller.Address(), value) {
  64. caller.ReturnGas(gas, gasPrice)
  65. return nil, common.Address{}, ValueTransferErr("insufficient funds to transfer value. Req %v, has %v", value, env.Db().GetBalance(caller.Address()))
  66. }
  67. var createAccount bool
  68. if address == nil {
  69. // Generate a new address
  70. nonce := env.Db().GetNonce(caller.Address())
  71. env.Db().SetNonce(caller.Address(), nonce+1)
  72. addr = crypto.CreateAddress(caller.Address(), nonce)
  73. address = &addr
  74. createAccount = true
  75. }
  76. snapshotPreTransfer := env.MakeSnapshot()
  77. var (
  78. from = env.Db().GetAccount(caller.Address())
  79. to vm.Account
  80. )
  81. if createAccount {
  82. to = env.Db().CreateAccount(*address)
  83. } else {
  84. if !env.Db().Exist(*address) {
  85. to = env.Db().CreateAccount(*address)
  86. } else {
  87. to = env.Db().GetAccount(*address)
  88. }
  89. }
  90. env.Transfer(from, to, value)
  91. snapshotPostTransfer := env.MakeSnapshot()
  92. contract := vm.NewContract(caller, to, value, gas, gasPrice)
  93. contract.SetCallCode(codeAddr, code)
  94. ret, err = evm.Run(contract, input)
  95. if err != nil {
  96. if err == vm.CodeStoreOutOfGasError {
  97. // TODO: this is rather hacky, restore all state changes
  98. // except sender's account nonce increment
  99. toNonce := env.Db().GetNonce(to.Address())
  100. env.SetSnapshot(snapshotPostTransfer)
  101. env.Db().SetNonce(to.Address(), toNonce)
  102. } else {
  103. env.SetSnapshot(snapshotPreTransfer) //env.Db().Set(snapshot)
  104. }
  105. }
  106. return ret, addr, err
  107. }
  108. 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) {
  109. evm := vm.NewVm(env)
  110. // Depth check execution. Fail if we're trying to execute above the
  111. // limit.
  112. if env.Depth() > int(params.CallCreateDepth.Int64()) {
  113. caller.ReturnGas(gas, gasPrice)
  114. return nil, common.Address{}, vm.DepthError
  115. }
  116. if !env.CanTransfer(*originAddr, value) {
  117. caller.ReturnGas(gas, gasPrice)
  118. return nil, common.Address{}, ValueTransferErr("insufficient funds to transfer value. Req %v, has %v", value, env.Db().GetBalance(caller.Address()))
  119. }
  120. snapshot := env.MakeSnapshot()
  121. var (
  122. //from = env.Db().GetAccount(*originAddr)
  123. to vm.Account
  124. )
  125. if !env.Db().Exist(*toAddr) {
  126. to = env.Db().CreateAccount(*toAddr)
  127. } else {
  128. to = env.Db().GetAccount(*toAddr)
  129. }
  130. contract := vm.NewContract(caller, to, value, gas, gasPrice)
  131. contract.SetCallCode(codeAddr, code)
  132. contract.DelegateCall = true
  133. ret, err = evm.Run(contract, input)
  134. if err != nil {
  135. env.SetSnapshot(snapshot) //env.Db().Set(snapshot)
  136. }
  137. return ret, addr, err
  138. }
  139. // generic transfer method
  140. func Transfer(from, to vm.Account, amount *big.Int) {
  141. from.SubBalance(amount)
  142. to.AddBalance(amount)
  143. }