execution.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. 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. type Execution struct {
  26. env vm.Environment
  27. address *common.Address
  28. input []byte
  29. evm vm.VirtualMachine
  30. Gas, price, value *big.Int
  31. }
  32. func NewExecution(env vm.Environment, address *common.Address, input []byte, gas, gasPrice, value *big.Int) *Execution {
  33. exe := &Execution{env: env, address: address, input: input, Gas: gas, price: gasPrice, value: value}
  34. exe.evm = vm.NewVm(env)
  35. return exe
  36. }
  37. func (self *Execution) Call(codeAddr common.Address, caller vm.ContextRef) ([]byte, error) {
  38. // Retrieve the executing code
  39. code := self.env.State().GetCode(codeAddr)
  40. return self.exec(&codeAddr, code, caller)
  41. }
  42. func (self *Execution) Create(caller vm.ContextRef) (ret []byte, err error, account *state.StateObject) {
  43. // Input must be nil for create
  44. code := self.input
  45. self.input = nil
  46. ret, err = self.exec(nil, code, caller)
  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, err, nil
  52. }
  53. account = self.env.State().GetStateObject(*self.address)
  54. return
  55. }
  56. func (self *Execution) exec(contextAddr *common.Address, code []byte, caller vm.ContextRef) (ret []byte, err error) {
  57. env := self.env
  58. evm := self.evm
  59. if env.Depth() > int(params.CallCreateDepth.Int64()) {
  60. caller.ReturnGas(self.Gas, self.price)
  61. return nil, vm.DepthError
  62. }
  63. vsnapshot := env.State().Copy()
  64. var createAccount bool
  65. if self.address == nil {
  66. // Generate a new address
  67. nonce := env.State().GetNonce(caller.Address())
  68. env.State().SetNonce(caller.Address(), nonce+1)
  69. addr := crypto.CreateAddress(caller.Address(), nonce)
  70. self.address = &addr
  71. createAccount = true
  72. }
  73. snapshot := env.State().Copy()
  74. var (
  75. from = env.State().GetStateObject(caller.Address())
  76. to *state.StateObject
  77. )
  78. if createAccount {
  79. to = env.State().CreateAccount(*self.address)
  80. } else {
  81. to = env.State().GetOrNewStateObject(*self.address)
  82. }
  83. err = env.Transfer(from, to, self.value)
  84. if err != nil {
  85. env.State().Set(vsnapshot)
  86. caller.ReturnGas(self.Gas, self.price)
  87. return nil, ValueTransferErr("insufficient funds to transfer value. Req %v, has %v", self.value, from.Balance())
  88. }
  89. context := vm.NewContext(caller, to, self.value, self.Gas, self.price)
  90. context.SetCallCode(contextAddr, code)
  91. ret, err = evm.Run(context, self.input)
  92. if err != nil {
  93. env.State().Set(snapshot)
  94. }
  95. return
  96. }