vm_env.go 2.4 KB

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