execution.go 3.9 KB

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