env.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2015 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 runtime
  17. import (
  18. "math/big"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/core"
  21. "github.com/ethereum/go-ethereum/core/state"
  22. "github.com/ethereum/go-ethereum/core/vm"
  23. )
  24. // Env is a basic runtime environment required for running the EVM.
  25. type Env struct {
  26. ruleSet vm.RuleSet
  27. depth int
  28. state *state.StateDB
  29. origin common.Address
  30. coinbase common.Address
  31. number *big.Int
  32. time *big.Int
  33. difficulty *big.Int
  34. gasLimit *big.Int
  35. logs []vm.StructLog
  36. getHashFn func(uint64) common.Hash
  37. evm *vm.EVM
  38. }
  39. // NewEnv returns a new vm.Environment
  40. func NewEnv(cfg *Config, state *state.StateDB) vm.Environment {
  41. env := &Env{
  42. ruleSet: cfg.RuleSet,
  43. state: state,
  44. origin: cfg.Origin,
  45. coinbase: cfg.Coinbase,
  46. number: cfg.BlockNumber,
  47. time: cfg.Time,
  48. difficulty: cfg.Difficulty,
  49. gasLimit: cfg.GasLimit,
  50. }
  51. env.evm = vm.New(env, vm.Config{
  52. Debug: cfg.Debug,
  53. EnableJit: !cfg.DisableJit,
  54. ForceJit: !cfg.DisableJit,
  55. Logger: vm.LogConfig{
  56. Collector: env,
  57. },
  58. })
  59. return env
  60. }
  61. func (self *Env) StructLogs() []vm.StructLog {
  62. return self.logs
  63. }
  64. func (self *Env) AddStructLog(log vm.StructLog) {
  65. self.logs = append(self.logs, log)
  66. }
  67. func (self *Env) MarkCodeHash(hash common.Hash) {}
  68. func (self *Env) RuleSet() vm.RuleSet { return self.ruleSet }
  69. func (self *Env) Vm() vm.Vm { return self.evm }
  70. func (self *Env) Origin() common.Address { return self.origin }
  71. func (self *Env) BlockNumber() *big.Int { return self.number }
  72. func (self *Env) Coinbase() common.Address { return self.coinbase }
  73. func (self *Env) Time() *big.Int { return self.time }
  74. func (self *Env) Difficulty() *big.Int { return self.difficulty }
  75. func (self *Env) Db() vm.Database { return self.state }
  76. func (self *Env) GasLimit() *big.Int { return self.gasLimit }
  77. func (self *Env) VmType() vm.Type { return vm.StdVmTy }
  78. func (self *Env) GetHash(n uint64) common.Hash {
  79. return self.getHashFn(n)
  80. }
  81. func (self *Env) AddLog(log *vm.Log) {
  82. self.state.AddLog(log)
  83. }
  84. func (self *Env) Depth() int { return self.depth }
  85. func (self *Env) SetDepth(i int) { self.depth = i }
  86. func (self *Env) CanTransfer(from common.Address, balance *big.Int) bool {
  87. return self.state.GetBalance(from).Cmp(balance) >= 0
  88. }
  89. func (self *Env) MakeSnapshot() vm.Database {
  90. return self.state.Copy()
  91. }
  92. func (self *Env) SetSnapshot(copy vm.Database) {
  93. self.state.Set(copy.(*state.StateDB))
  94. }
  95. func (self *Env) Transfer(from, to vm.Account, amount *big.Int) {
  96. core.Transfer(from, to, amount)
  97. }
  98. func (self *Env) Call(caller vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  99. return core.Call(self, caller, addr, data, gas, price, value)
  100. }
  101. func (self *Env) CallCode(caller vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  102. return core.CallCode(self, caller, addr, data, gas, price, value)
  103. }
  104. func (self *Env) DelegateCall(me vm.ContractRef, addr common.Address, data []byte, gas, price *big.Int) ([]byte, error) {
  105. return core.DelegateCall(self, me, addr, data, gas, price)
  106. }
  107. func (self *Env) Create(caller vm.ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error) {
  108. return core.Create(self, caller, data, gas, price, value)
  109. }