vm_env.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 light
  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/types"
  22. "github.com/ethereum/go-ethereum/core/vm"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. "golang.org/x/net/context"
  25. )
  26. // VMEnv is the light client version of the vm execution environment.
  27. // Unlike other structures, VMEnv holds a context that is applied by state
  28. // retrieval requests through the entire execution. If any state operation
  29. // returns an error, the execution fails.
  30. type VMEnv struct {
  31. vm.Environment
  32. ctx context.Context
  33. chainConfig *core.ChainConfig
  34. evm *vm.EVM
  35. state *VMState
  36. header *types.Header
  37. msg core.Message
  38. depth int
  39. chain *LightChain
  40. err error
  41. }
  42. // NewEnv creates a new execution environment based on an ODR capable light state
  43. func NewEnv(ctx context.Context, state *LightState, chainConfig *core.ChainConfig, chain *LightChain, msg core.Message, header *types.Header, cfg vm.Config) *VMEnv {
  44. env := &VMEnv{
  45. chainConfig: chainConfig,
  46. chain: chain,
  47. header: header,
  48. msg: msg,
  49. }
  50. env.state = &VMState{ctx: ctx, state: state, env: env}
  51. env.evm = vm.New(env, cfg)
  52. return env
  53. }
  54. func (self *VMEnv) RuleSet() vm.RuleSet { return self.chainConfig }
  55. func (self *VMEnv) Vm() vm.Vm { return self.evm }
  56. func (self *VMEnv) Origin() common.Address { f, _ := self.msg.From(); return f }
  57. func (self *VMEnv) BlockNumber() *big.Int { return self.header.Number }
  58. func (self *VMEnv) Coinbase() common.Address { return self.header.Coinbase }
  59. func (self *VMEnv) Time() *big.Int { return self.header.Time }
  60. func (self *VMEnv) Difficulty() *big.Int { return self.header.Difficulty }
  61. func (self *VMEnv) GasLimit() *big.Int { return self.header.GasLimit }
  62. func (self *VMEnv) Db() vm.Database { return self.state }
  63. func (self *VMEnv) Depth() int { return self.depth }
  64. func (self *VMEnv) SetDepth(i int) { self.depth = i }
  65. func (self *VMEnv) GetHash(n uint64) common.Hash {
  66. for header := self.chain.GetHeader(self.header.ParentHash, self.header.Number.Uint64()-1); header != nil; header = self.chain.GetHeader(header.ParentHash, header.Number.Uint64()-1) {
  67. if header.Number.Uint64() == n {
  68. return header.Hash()
  69. }
  70. }
  71. return common.Hash{}
  72. }
  73. func (self *VMEnv) AddLog(log *vm.Log) {
  74. //self.state.AddLog(log)
  75. }
  76. func (self *VMEnv) CanTransfer(from common.Address, balance *big.Int) bool {
  77. return self.state.GetBalance(from).Cmp(balance) >= 0
  78. }
  79. func (self *VMEnv) SnapshotDatabase() int {
  80. return self.state.SnapshotDatabase()
  81. }
  82. func (self *VMEnv) RevertToSnapshot(idx int) {
  83. self.state.RevertToSnapshot(idx)
  84. }
  85. func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) {
  86. core.Transfer(from, to, amount)
  87. }
  88. func (self *VMEnv) Call(me vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  89. return core.Call(self, me, addr, data, gas, price, value)
  90. }
  91. func (self *VMEnv) CallCode(me vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  92. return core.CallCode(self, me, addr, data, gas, price, value)
  93. }
  94. func (self *VMEnv) DelegateCall(me vm.ContractRef, addr common.Address, data []byte, gas, price *big.Int) ([]byte, error) {
  95. return core.DelegateCall(self, me, addr, data, gas, price)
  96. }
  97. func (self *VMEnv) Create(me vm.ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error) {
  98. return core.Create(self, me, data, gas, price, value)
  99. }
  100. // Error returns the error (if any) that happened during execution.
  101. func (self *VMEnv) Error() error {
  102. return self.err
  103. }
  104. // VMState is a wrapper for the light state that holds the actual context and
  105. // passes it to any state operation that requires it.
  106. type VMState struct {
  107. vm.Database
  108. ctx context.Context
  109. state *LightState
  110. snapshots []*LightState
  111. env *VMEnv
  112. }
  113. // errHandler handles and stores any state error that happens during execution.
  114. func (s *VMState) errHandler(err error) {
  115. if err != nil && s.env.err == nil {
  116. s.env.err = err
  117. }
  118. }
  119. func (self *VMState) SnapshotDatabase() int {
  120. self.snapshots = append(self.snapshots, self.state.Copy())
  121. return len(self.snapshots) - 1
  122. }
  123. func (self *VMState) RevertToSnapshot(idx int) {
  124. self.state.Set(self.snapshots[idx])
  125. self.snapshots = self.snapshots[:idx]
  126. }
  127. // GetAccount returns the account object of the given account or nil if the
  128. // account does not exist
  129. func (s *VMState) GetAccount(addr common.Address) vm.Account {
  130. so, err := s.state.GetStateObject(s.ctx, addr)
  131. s.errHandler(err)
  132. if err != nil {
  133. // return a dummy state object to avoid panics
  134. so = s.state.newStateObject(addr)
  135. }
  136. return so
  137. }
  138. // CreateAccount creates creates a new account object and takes ownership.
  139. func (s *VMState) CreateAccount(addr common.Address) vm.Account {
  140. so, err := s.state.CreateStateObject(s.ctx, addr)
  141. s.errHandler(err)
  142. if err != nil {
  143. // return a dummy state object to avoid panics
  144. so = s.state.newStateObject(addr)
  145. }
  146. return so
  147. }
  148. // AddBalance adds the given amount to the balance of the specified account
  149. func (s *VMState) AddBalance(addr common.Address, amount *big.Int) {
  150. err := s.state.AddBalance(s.ctx, addr, amount)
  151. s.errHandler(err)
  152. }
  153. // GetBalance retrieves the balance from the given address or 0 if the account does
  154. // not exist
  155. func (s *VMState) GetBalance(addr common.Address) *big.Int {
  156. res, err := s.state.GetBalance(s.ctx, addr)
  157. s.errHandler(err)
  158. return res
  159. }
  160. // GetNonce returns the nonce at the given address or 0 if the account does
  161. // not exist
  162. func (s *VMState) GetNonce(addr common.Address) uint64 {
  163. res, err := s.state.GetNonce(s.ctx, addr)
  164. s.errHandler(err)
  165. return res
  166. }
  167. // SetNonce sets the nonce of the specified account
  168. func (s *VMState) SetNonce(addr common.Address, nonce uint64) {
  169. err := s.state.SetNonce(s.ctx, addr, nonce)
  170. s.errHandler(err)
  171. }
  172. // GetCode returns the contract code at the given address or nil if the account
  173. // does not exist
  174. func (s *VMState) GetCode(addr common.Address) []byte {
  175. res, err := s.state.GetCode(s.ctx, addr)
  176. s.errHandler(err)
  177. return res
  178. }
  179. // GetCodeHash returns the contract code hash at the given address
  180. func (s *VMState) GetCodeHash(addr common.Address) common.Hash {
  181. res, err := s.state.GetCode(s.ctx, addr)
  182. s.errHandler(err)
  183. return crypto.Keccak256Hash(res)
  184. }
  185. // GetCodeSize returns the contract code size at the given address
  186. func (s *VMState) GetCodeSize(addr common.Address) int {
  187. res, err := s.state.GetCode(s.ctx, addr)
  188. s.errHandler(err)
  189. return len(res)
  190. }
  191. // SetCode sets the contract code at the specified account
  192. func (s *VMState) SetCode(addr common.Address, code []byte) {
  193. err := s.state.SetCode(s.ctx, addr, code)
  194. s.errHandler(err)
  195. }
  196. // AddRefund adds an amount to the refund value collected during a vm execution
  197. func (s *VMState) AddRefund(gas *big.Int) {
  198. s.state.AddRefund(gas)
  199. }
  200. // GetRefund returns the refund value collected during a vm execution
  201. func (s *VMState) GetRefund() *big.Int {
  202. return s.state.GetRefund()
  203. }
  204. // GetState returns the contract storage value at storage address b from the
  205. // contract address a or common.Hash{} if the account does not exist
  206. func (s *VMState) GetState(a common.Address, b common.Hash) common.Hash {
  207. res, err := s.state.GetState(s.ctx, a, b)
  208. s.errHandler(err)
  209. return res
  210. }
  211. // SetState sets the storage value at storage address key of the account addr
  212. func (s *VMState) SetState(addr common.Address, key common.Hash, value common.Hash) {
  213. err := s.state.SetState(s.ctx, addr, key, value)
  214. s.errHandler(err)
  215. }
  216. // Suicide marks an account to be removed and clears its balance
  217. func (s *VMState) Suicide(addr common.Address) bool {
  218. res, err := s.state.Suicide(s.ctx, addr)
  219. s.errHandler(err)
  220. return res
  221. }
  222. // Exist returns true if an account exists at the given address
  223. func (s *VMState) Exist(addr common.Address) bool {
  224. res, err := s.state.HasAccount(s.ctx, addr)
  225. s.errHandler(err)
  226. return res
  227. }
  228. // HasSuicided returns true if the given account has been marked for deletion
  229. // or false if the account does not exist
  230. func (s *VMState) HasSuicided(addr common.Address) bool {
  231. res, err := s.state.HasSuicided(s.ctx, addr)
  232. s.errHandler(err)
  233. return res
  234. }