vm_env.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. // BlockedCodeHashes is a set of EVM code hashes that this node should block
  25. // sending funds from.
  26. var BlockedCodeHashes map[common.Hash]struct{}
  27. // GetHashFn returns a function for which the VM env can query block hashes through
  28. // up to the limit defined by the Yellow Paper and uses the given block chain
  29. // to query for information.
  30. func GetHashFn(ref common.Hash, chain *BlockChain) func(n uint64) common.Hash {
  31. return func(n uint64) common.Hash {
  32. for block := chain.GetBlockByHash(ref); block != nil; block = chain.GetBlock(block.ParentHash(), block.NumberU64()-1) {
  33. if block.NumberU64() == n {
  34. return block.Hash()
  35. }
  36. }
  37. return common.Hash{}
  38. }
  39. }
  40. type VMEnv struct {
  41. chainConfig *ChainConfig // Chain configuration
  42. state *state.StateDB // State to use for executing
  43. evm *vm.EVM // The Ethereum Virtual Machine
  44. depth int // Current execution depth
  45. msg Message // Message appliod
  46. codeHashes map[common.Hash]struct{} // code hashes collected during execution
  47. header *types.Header // Header information
  48. chain *BlockChain // Blockchain handle
  49. logs []vm.StructLog // Logs for the custom structured logger
  50. getHashFn func(uint64) common.Hash // getHashFn callback is used to retrieve block hashes
  51. }
  52. func NewEnv(state *state.StateDB, chainConfig *ChainConfig, chain *BlockChain, msg Message, header *types.Header, cfg vm.Config) *VMEnv {
  53. env := &VMEnv{
  54. chainConfig: chainConfig,
  55. codeHashes: make(map[common.Hash]struct{}),
  56. chain: chain,
  57. state: state,
  58. header: header,
  59. msg: msg,
  60. getHashFn: GetHashFn(header.ParentHash, chain),
  61. }
  62. // if no log collector is present set self as the collector
  63. if cfg.Logger.Collector == nil {
  64. cfg.Logger.Collector = env
  65. }
  66. env.evm = vm.New(env, cfg)
  67. return env
  68. }
  69. func (self *VMEnv) MarkCodeHash(hash common.Hash) { self.codeHashes[hash] = struct{}{} }
  70. func (self *VMEnv) GetMarkedCodeHashes() map[common.Hash]struct{} { return self.codeHashes }
  71. func (self *VMEnv) RuleSet() vm.RuleSet { return self.chainConfig }
  72. func (self *VMEnv) Vm() vm.Vm { return self.evm }
  73. func (self *VMEnv) Origin() common.Address { f, _ := self.msg.From(); return f }
  74. func (self *VMEnv) BlockNumber() *big.Int { return self.header.Number }
  75. func (self *VMEnv) Coinbase() common.Address { return self.header.Coinbase }
  76. func (self *VMEnv) Time() *big.Int { return self.header.Time }
  77. func (self *VMEnv) Difficulty() *big.Int { return self.header.Difficulty }
  78. func (self *VMEnv) GasLimit() *big.Int { return self.header.GasLimit }
  79. func (self *VMEnv) Value() *big.Int { return self.msg.Value() }
  80. func (self *VMEnv) Db() vm.Database { return self.state }
  81. func (self *VMEnv) Depth() int { return self.depth }
  82. func (self *VMEnv) SetDepth(i int) { self.depth = i }
  83. func (self *VMEnv) GetHash(n uint64) common.Hash {
  84. return self.getHashFn(n)
  85. }
  86. func (self *VMEnv) AddLog(log *vm.Log) {
  87. self.state.AddLog(log)
  88. }
  89. func (self *VMEnv) CanTransfer(from common.Address, balance *big.Int) bool {
  90. return self.state.GetBalance(from).Cmp(balance) >= 0
  91. }
  92. func (self *VMEnv) MakeSnapshot() vm.Database {
  93. return self.state.Copy()
  94. }
  95. func (self *VMEnv) SetSnapshot(copy vm.Database) {
  96. self.state.Set(copy.(*state.StateDB))
  97. }
  98. func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) {
  99. Transfer(from, to, amount)
  100. }
  101. func (self *VMEnv) Call(me vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  102. return Call(self, me, addr, data, gas, price, value)
  103. }
  104. func (self *VMEnv) CallCode(me vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  105. return CallCode(self, me, addr, data, gas, price, value)
  106. }
  107. func (self *VMEnv) DelegateCall(me vm.ContractRef, addr common.Address, data []byte, gas, price *big.Int) ([]byte, error) {
  108. return DelegateCall(self, me, addr, data, gas, price)
  109. }
  110. func (self *VMEnv) Create(me vm.ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error) {
  111. return Create(self, me, data, gas, price, value)
  112. }
  113. func (self *VMEnv) StructLogs() []vm.StructLog {
  114. return self.logs
  115. }
  116. func (self *VMEnv) AddStructLog(log vm.StructLog) {
  117. self.logs = append(self.logs, log)
  118. }