execution.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/state"
  21. "github.com/ethereum/go-ethereum/core/vm"
  22. "github.com/ethereum/go-ethereum/crypto"
  23. "github.com/ethereum/go-ethereum/params"
  24. )
  25. // Execution is the execution environment for the given call or create action.
  26. type Execution struct {
  27. env vm.Environment
  28. address *common.Address
  29. input []byte
  30. evm vm.VirtualMachine
  31. Gas, price, value *big.Int
  32. }
  33. // NewExecution returns a new execution environment that handles all calling
  34. // and creation logic defined by the YP.
  35. func NewExecution(env vm.Environment, address *common.Address, input []byte, gas, gasPrice, value *big.Int) *Execution {
  36. exe := &Execution{env: env, address: address, input: input, Gas: gas, price: gasPrice, value: value}
  37. exe.evm = vm.NewVm(env)
  38. return exe
  39. }
  40. // Call executes within the given context
  41. func (self *Execution) Call(codeAddr common.Address, caller vm.ContextRef) ([]byte, error) {
  42. // Retrieve the executing code
  43. code := self.env.State().GetCode(codeAddr)
  44. return self.exec(&codeAddr, code, caller)
  45. }
  46. // Create creates a new contract and runs the initialisation procedure of the
  47. // contract. This returns the returned code for the contract and is stored
  48. // elsewhere.
  49. func (self *Execution) Create(caller vm.ContextRef) (ret []byte, err error, account *state.StateObject) {
  50. // Input must be nil for create
  51. code := self.input
  52. self.input = nil
  53. ret, err = self.exec(nil, code, caller)
  54. // Here we get an error if we run into maximum stack depth,
  55. // See: https://github.com/ethereum/yellowpaper/pull/131
  56. // and YP definitions for CREATE instruction
  57. if err != nil {
  58. return nil, err, nil
  59. }
  60. account = self.env.State().GetStateObject(*self.address)
  61. return
  62. }
  63. // exec executes the given code and executes within the contextAddr context.
  64. func (self *Execution) exec(contextAddr *common.Address, code []byte, caller vm.ContextRef) (ret []byte, err error) {
  65. env := self.env
  66. evm := self.evm
  67. // Depth check execution. Fail if we're trying to execute above the
  68. // limit.
  69. if env.Depth() > int(params.CallCreateDepth.Int64()) {
  70. caller.ReturnGas(self.Gas, self.price)
  71. return nil, vm.DepthError
  72. }
  73. if !env.CanTransfer(env.State().GetStateObject(caller.Address()), self.value) {
  74. caller.ReturnGas(self.Gas, self.price)
  75. return nil, ValueTransferErr("insufficient funds to transfer value. Req %v, has %v", self.value, env.State().GetBalance(caller.Address()))
  76. }
  77. var createAccount bool
  78. if self.address == nil {
  79. // Generate a new address
  80. nonce := env.State().GetNonce(caller.Address())
  81. env.State().SetNonce(caller.Address(), nonce+1)
  82. addr := crypto.CreateAddress(caller.Address(), nonce)
  83. self.address = &addr
  84. createAccount = true
  85. }
  86. snapshot := env.State().Copy()
  87. var (
  88. from = env.State().GetStateObject(caller.Address())
  89. to *state.StateObject
  90. )
  91. if createAccount {
  92. to = env.State().CreateAccount(*self.address)
  93. } else {
  94. to = env.State().GetOrNewStateObject(*self.address)
  95. }
  96. vm.Transfer(from, to, self.value)
  97. context := vm.NewContext(caller, to, self.value, self.Gas, self.price)
  98. context.SetCallCode(contextAddr, code)
  99. ret, err = evm.Run(context, self.input)
  100. if err != nil {
  101. env.State().Set(snapshot)
  102. }
  103. return
  104. }