execution.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package vm
  2. import (
  3. "fmt"
  4. "math/big"
  5. "github.com/ethereum/go-ethereum/ethutil"
  6. "github.com/ethereum/go-ethereum/state"
  7. )
  8. type Execution struct {
  9. vm VirtualMachine
  10. address, input []byte
  11. Gas, price, value *big.Int
  12. object *state.StateObject
  13. SkipTransfer bool
  14. }
  15. func NewExecution(vm VirtualMachine, address, input []byte, gas, gasPrice, value *big.Int) *Execution {
  16. return &Execution{vm: vm, 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) Exec(codeAddr []byte, caller ClosureRef) ([]byte, error) {
  22. // Retrieve the executing code
  23. code := self.vm.Env().State().GetCode(codeAddr)
  24. return self.exec(code, codeAddr, caller)
  25. }
  26. func (self *Execution) exec(code, caddr []byte, caller ClosureRef) (ret []byte, err error) {
  27. env := self.vm.Env()
  28. vmlogger.Debugf("pre state %x\n", env.State().Root())
  29. snapshot := env.State().Copy()
  30. defer func() {
  31. if IsDepthErr(err) || IsOOGErr(err) {
  32. env.State().Set(snapshot)
  33. }
  34. vmlogger.Debugf("post state %x\n", env.State().Root())
  35. }()
  36. msg := env.State().Manifest().AddMessage(&state.Message{
  37. To: self.address, From: caller.Address(),
  38. Input: self.input,
  39. Origin: env.Origin(),
  40. Block: env.BlockHash(), Timestamp: env.Time(), Coinbase: env.Coinbase(), Number: env.BlockNumber(),
  41. Value: self.value,
  42. })
  43. from, to := caller.Object(), env.State().GetOrNewStateObject(self.address)
  44. // Skipping transfer is used on testing for the initial call
  45. if !self.SkipTransfer {
  46. err = env.Transfer(from, to, self.value)
  47. }
  48. if err != nil {
  49. caller.ReturnGas(self.Gas, self.price)
  50. err = fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, from.Balance)
  51. } else {
  52. self.object = to
  53. // Pre-compiled contracts (address.go) 1, 2 & 3.
  54. naddr := ethutil.BigD(caddr).Uint64()
  55. if p := Precompiled[naddr]; p != nil {
  56. if self.Gas.Cmp(p.Gas) >= 0 {
  57. ret = p.Call(self.input)
  58. self.vm.Printf("NATIVE_FUNC(%x) => %x", naddr, ret)
  59. self.vm.Endl()
  60. }
  61. } else {
  62. // Create a new callable closure
  63. c := NewClosure(msg, caller, to, code, self.Gas, self.price)
  64. c.exe = self
  65. if self.vm.Depth() == MaxCallDepth {
  66. c.UseGas(self.Gas)
  67. return c.Return(nil), DepthError{}
  68. }
  69. // Executer the closure and get the return value (if any)
  70. ret, _, err = c.Call(self.vm, self.input)
  71. msg.Output = ret
  72. }
  73. }
  74. return
  75. }
  76. func (self *Execution) Create(caller []byte) (ret []byte, err error) {
  77. return self.exec(self.input, nil, caller)
  78. }