vm_env.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library 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. // The go-ethereum library 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 the go-ethereum library. 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 *BlockChain
  30. typ vm.Type
  31. // structured logging
  32. logs []vm.StructLog
  33. }
  34. func NewEnv(state *state.StateDB, chain *BlockChain, 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() *big.Int { 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) Db() vm.Database { 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. for block := self.chain.GetBlock(self.header.ParentHash); block != nil; block = self.chain.GetBlock(block.ParentHash()) {
  57. if block.NumberU64() == n {
  58. return block.Hash()
  59. }
  60. }
  61. return common.Hash{}
  62. }
  63. func (self *VMEnv) AddLog(log *vm.Log) {
  64. self.state.AddLog(log)
  65. }
  66. func (self *VMEnv) CanTransfer(from common.Address, balance *big.Int) bool {
  67. return self.state.GetBalance(from).Cmp(balance) >= 0
  68. }
  69. func (self *VMEnv) MakeSnapshot() vm.Database {
  70. return self.state.Copy()
  71. }
  72. func (self *VMEnv) SetSnapshot(copy vm.Database) {
  73. self.state.Set(copy.(*state.StateDB))
  74. }
  75. func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) {
  76. Transfer(from, to, amount)
  77. }
  78. func (self *VMEnv) Call(me vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  79. return Call(self, me, addr, data, gas, price, value)
  80. }
  81. func (self *VMEnv) CallCode(me vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  82. return CallCode(self, me, addr, data, gas, price, value)
  83. }
  84. func (self *VMEnv) Create(me vm.ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error) {
  85. return Create(self, me, data, gas, price, value)
  86. }
  87. func (self *VMEnv) StructLogs() []vm.StructLog {
  88. return self.logs
  89. }
  90. func (self *VMEnv) AddStructLog(log vm.StructLog) {
  91. self.logs = append(self.logs, log)
  92. }