execution.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package core
  2. import (
  3. "fmt"
  4. "math/big"
  5. "github.com/ethereum/go-ethereum/state"
  6. "github.com/ethereum/go-ethereum/vm"
  7. )
  8. type Execution struct {
  9. env vm.Environment
  10. address, input []byte
  11. Gas, price, value *big.Int
  12. object *state.StateObject
  13. SkipTransfer bool
  14. }
  15. func NewExecution(env vm.Environment, address, input []byte, gas, gasPrice, value *big.Int) *Execution {
  16. return &Execution{env: env, address: address, input: input, Gas: gas, price: gasPrice, value: value}
  17. }
  18. func (self *Execution) Addr() []byte {
  19. return self.address
  20. }
  21. func (self *Execution) Call(codeAddr []byte, caller vm.ClosureRef) ([]byte, error) {
  22. // Retrieve the executing code
  23. code := self.env.State().GetCode(codeAddr)
  24. return self.exec(code, codeAddr, caller)
  25. }
  26. func (self *Execution) exec(code, contextAddr []byte, caller vm.ClosureRef) (ret []byte, err error) {
  27. env := self.env
  28. evm := vm.New(env, vm.DebugVmTy)
  29. chainlogger.Debugf("pre state %x\n", env.State().Root())
  30. if env.Depth() == vm.MaxCallDepth {
  31. // Consume all gas (by not returning it) and return a depth error
  32. return nil, vm.DepthError{}
  33. }
  34. from, to := env.State().GetStateObject(caller.Address()), env.State().GetOrNewStateObject(self.address)
  35. // Skipping transfer is used on testing for the initial call
  36. if !self.SkipTransfer {
  37. err = env.Transfer(from, to, self.value)
  38. if err != nil {
  39. caller.ReturnGas(self.Gas, self.price)
  40. err = fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, from.Balance)
  41. return
  42. }
  43. }
  44. snapshot := env.State().Copy()
  45. defer func() {
  46. env.State().Set(snapshot)
  47. chainlogger.Debugf("post state %x\n", env.State().Root())
  48. }()
  49. self.object = to
  50. ret, err = evm.Run(to, caller, code, self.value, self.Gas, self.price, self.input)
  51. return
  52. }
  53. func (self *Execution) Create(caller vm.ClosureRef) (ret []byte, err error, account *state.StateObject) {
  54. ret, err = self.exec(self.input, nil, caller)
  55. account = self.env.State().GetStateObject(self.address)
  56. return
  57. }