vm_env.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package core
  2. import (
  3. "math/big"
  4. "github.com/ethereum/go-ethereum/core/types"
  5. "github.com/ethereum/go-ethereum/state"
  6. "github.com/ethereum/go-ethereum/vm"
  7. )
  8. type VMEnv struct {
  9. state *state.State
  10. block *types.Block
  11. tx *types.Transaction
  12. depth int
  13. }
  14. func NewEnv(state *state.State, tx *types.Transaction, block *types.Block) *VMEnv {
  15. return &VMEnv{
  16. state: state,
  17. block: block,
  18. tx: tx,
  19. }
  20. }
  21. func (self *VMEnv) Origin() []byte { return self.tx.Sender() }
  22. func (self *VMEnv) BlockNumber() *big.Int { return self.block.Number }
  23. func (self *VMEnv) PrevHash() []byte { return self.block.PrevHash }
  24. func (self *VMEnv) Coinbase() []byte { return self.block.Coinbase }
  25. func (self *VMEnv) Time() int64 { return self.block.Time }
  26. func (self *VMEnv) Difficulty() *big.Int { return self.block.Difficulty }
  27. func (self *VMEnv) BlockHash() []byte { return self.block.Hash() }
  28. func (self *VMEnv) Value() *big.Int { return self.tx.Value }
  29. func (self *VMEnv) State() *state.State { return self.state }
  30. func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit }
  31. func (self *VMEnv) Depth() int { return self.depth }
  32. func (self *VMEnv) SetDepth(i int) { self.depth = i }
  33. func (self *VMEnv) AddLog(log *state.Log) {
  34. self.state.AddLog(log)
  35. }
  36. func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
  37. return vm.Transfer(from, to, amount)
  38. }
  39. func (self *VMEnv) vm(addr, data []byte, gas, price, value *big.Int) *Execution {
  40. evm := vm.New(self, vm.DebugVmTy)
  41. return NewExecution(evm, addr, data, gas, price, value)
  42. }
  43. func (self *VMEnv) Call(me vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
  44. exe := self.vm(addr, data, gas, price, value)
  45. return exe.Call(addr, me)
  46. }
  47. func (self *VMEnv) CallCode(me vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
  48. exe := self.vm(me.Address(), data, gas, price, value)
  49. return exe.Call(addr, me)
  50. }
  51. func (self *VMEnv) Create(me vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ClosureRef) {
  52. exe := self.vm(addr, data, gas, price, value)
  53. return exe.Create(me)
  54. }