vm_env.go 9.2 KB

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