vm_env.go 2.6 KB

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