vm_env.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.StateDB
  10. block *types.Block
  11. msg Message
  12. depth int
  13. chain *ChainManager
  14. typ vm.Type
  15. }
  16. func NewEnv(state *state.StateDB, chain *ChainManager, msg Message, block *types.Block) *VMEnv {
  17. return &VMEnv{
  18. chain: chain,
  19. state: state,
  20. block: block,
  21. msg: msg,
  22. typ: vm.StdVmTy,
  23. }
  24. }
  25. func (self *VMEnv) Origin() []byte { return self.msg.From() }
  26. func (self *VMEnv) BlockNumber() *big.Int { return self.block.Number() }
  27. func (self *VMEnv) Coinbase() []byte { return self.block.Coinbase() }
  28. func (self *VMEnv) Time() int64 { return self.block.Time() }
  29. func (self *VMEnv) Difficulty() *big.Int { return self.block.Difficulty() }
  30. func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit() }
  31. func (self *VMEnv) Value() *big.Int { return self.msg.Value() }
  32. func (self *VMEnv) State() *state.StateDB { return self.state }
  33. func (self *VMEnv) Depth() int { return self.depth }
  34. func (self *VMEnv) SetDepth(i int) { self.depth = i }
  35. func (self *VMEnv) VmType() vm.Type { return self.typ }
  36. func (self *VMEnv) SetVmType(t vm.Type) { self.typ = t }
  37. func (self *VMEnv) GetHash(n uint64) []byte {
  38. if block := self.chain.GetBlockByNumber(n); block != nil {
  39. return block.Hash()
  40. }
  41. return nil
  42. }
  43. func (self *VMEnv) AddLog(log state.Log) {
  44. self.state.AddLog(log)
  45. }
  46. func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
  47. return vm.Transfer(from, to, amount)
  48. }
  49. func (self *VMEnv) vm(addr, data []byte, gas, price, value *big.Int) *Execution {
  50. return NewExecution(self, addr, data, gas, price, value)
  51. }
  52. func (self *VMEnv) Call(me vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
  53. exe := self.vm(addr, data, gas, price, value)
  54. return exe.Call(addr, me)
  55. }
  56. func (self *VMEnv) CallCode(me vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
  57. exe := self.vm(me.Address(), data, gas, price, value)
  58. return exe.Call(addr, me)
  59. }
  60. func (self *VMEnv) Create(me vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) {
  61. exe := self.vm(addr, data, gas, price, value)
  62. return exe.Create(me)
  63. }