execution.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. prev := caller.Address()
  32. ret, _, err = exec(env, caller, &prev, &addr, input, env.Db().GetCode(addr), gas, gasPrice, value)
  33. return ret, err
  34. }
  35. // Create creates a new contract with the given code
  36. func Create(env vm.Environment, caller vm.ContractRef, code []byte, gas, gasPrice, value *big.Int) (ret []byte, address common.Address, err error) {
  37. ret, address, err = exec(env, caller, nil, nil, nil, code, gas, gasPrice, value)
  38. // Here we get an error if we run into maximum stack depth,
  39. // See: https://github.com/ethereum/yellowpaper/pull/131
  40. // and YP definitions for CREATE instruction
  41. if err != nil {
  42. return nil, address, err
  43. }
  44. return ret, address, err
  45. }
  46. 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) {
  47. evm := vm.NewVm(env)
  48. // Depth check execution. Fail if we're trying to execute above the
  49. // limit.
  50. if env.Depth() > int(params.CallCreateDepth.Int64()) {
  51. caller.ReturnGas(gas, gasPrice)
  52. return nil, common.Address{}, vm.DepthError
  53. }
  54. if !env.CanTransfer(caller.Address(), value) {
  55. caller.ReturnGas(gas, gasPrice)
  56. return nil, common.Address{}, ValueTransferErr("insufficient funds to transfer value. Req %v, has %v", value, env.Db().GetBalance(caller.Address()))
  57. }
  58. var createAccount bool
  59. if address == nil {
  60. // Generate a new address
  61. nonce := env.Db().GetNonce(caller.Address())
  62. env.Db().SetNonce(caller.Address(), nonce+1)
  63. addr = crypto.CreateAddress(caller.Address(), nonce)
  64. address = &addr
  65. createAccount = true
  66. }
  67. snapshot := env.MakeSnapshot()
  68. var (
  69. from = env.Db().GetAccount(caller.Address())
  70. to vm.Account
  71. )
  72. if createAccount {
  73. to = env.Db().CreateAccount(*address)
  74. } else {
  75. if !env.Db().Exist(*address) {
  76. to = env.Db().CreateAccount(*address)
  77. } else {
  78. to = env.Db().GetAccount(*address)
  79. }
  80. }
  81. env.Transfer(from, to, value)
  82. contract := vm.NewContract(caller, to, value, gas, gasPrice)
  83. contract.SetCallCode(codeAddr, code)
  84. ret, err = evm.Run(contract, input)
  85. if err != nil {
  86. env.SetSnapshot(snapshot) //env.Db().Set(snapshot)
  87. }
  88. return ret, addr, err
  89. }
  90. // generic transfer method
  91. func Transfer(from, to vm.Account, amount *big.Int) {
  92. from.SubBalance(amount)
  93. to.AddBalance(amount)
  94. }