vm_env.go 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package core
  17. import (
  18. "math/big"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/core/state"
  21. "github.com/ethereum/go-ethereum/core/types"
  22. "github.com/ethereum/go-ethereum/core/vm"
  23. )
  24. type VMEnv struct {
  25. state *state.StateDB
  26. header *types.Header
  27. msg Message
  28. depth int
  29. chain *ChainManager
  30. typ vm.Type
  31. // structured logging
  32. logs []vm.StructLog
  33. }
  34. func NewEnv(state *state.StateDB, chain *ChainManager, msg Message, header *types.Header) *VMEnv {
  35. return &VMEnv{
  36. chain: chain,
  37. state: state,
  38. header: header,
  39. msg: msg,
  40. typ: vm.StdVmTy,
  41. }
  42. }
  43. func (self *VMEnv) Origin() common.Address { f, _ := self.msg.From(); return f }
  44. func (self *VMEnv) BlockNumber() *big.Int { return self.header.Number }
  45. func (self *VMEnv) Coinbase() common.Address { return self.header.Coinbase }
  46. func (self *VMEnv) Time() uint64 { return self.header.Time }
  47. func (self *VMEnv) Difficulty() *big.Int { return self.header.Difficulty }
  48. func (self *VMEnv) GasLimit() *big.Int { return self.header.GasLimit }
  49. func (self *VMEnv) Value() *big.Int { return self.msg.Value() }
  50. func (self *VMEnv) State() *state.StateDB { return self.state }
  51. func (self *VMEnv) Depth() int { return self.depth }
  52. func (self *VMEnv) SetDepth(i int) { self.depth = i }
  53. func (self *VMEnv) VmType() vm.Type { return self.typ }
  54. func (self *VMEnv) SetVmType(t vm.Type) { self.typ = t }
  55. func (self *VMEnv) GetHash(n uint64) common.Hash {
  56. if block := self.chain.GetBlockByNumber(n); block != nil {
  57. return block.Hash()
  58. }
  59. return common.Hash{}
  60. }
  61. func (self *VMEnv) AddLog(log *state.Log) {
  62. self.state.AddLog(log)
  63. }
  64. func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
  65. return vm.Transfer(from, to, amount)
  66. }
  67. func (self *VMEnv) Call(me vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  68. exe := NewExecution(self, &addr, data, gas, price, value)
  69. return exe.Call(addr, me)
  70. }
  71. func (self *VMEnv) CallCode(me vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  72. maddr := me.Address()
  73. exe := NewExecution(self, &maddr, data, gas, price, value)
  74. return exe.Call(addr, me)
  75. }
  76. func (self *VMEnv) Create(me vm.ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) {
  77. exe := NewExecution(self, nil, data, gas, price, value)
  78. return exe.Create(me)
  79. }
  80. func (self *VMEnv) StructLogs() []vm.StructLog {
  81. return self.logs
  82. }
  83. func (self *VMEnv) AddStructLog(log vm.StructLog) {
  84. self.logs = append(self.logs, log)
  85. }