vm_env.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/types"
  21. "github.com/ethereum/go-ethereum/core/vm"
  22. "github.com/ethereum/go-ethereum/crypto"
  23. "golang.org/x/net/context"
  24. )
  25. // VMState is a wrapper for the light state that holds the actual context and
  26. // passes it to any state operation that requires it.
  27. type VMState struct {
  28. ctx context.Context
  29. state *LightState
  30. snapshots []*LightState
  31. err error
  32. }
  33. func NewVMState(ctx context.Context, state *LightState) *VMState {
  34. return &VMState{ctx: ctx, state: state}
  35. }
  36. func (s *VMState) Error() error {
  37. return s.err
  38. }
  39. func (s *VMState) AddLog(log *types.Log) {}
  40. func (s *VMState) AddPreimage(hash common.Hash, preimage []byte) {}
  41. // errHandler handles and stores any state error that happens during execution.
  42. func (s *VMState) errHandler(err error) {
  43. if err != nil && s.err == nil {
  44. s.err = err
  45. }
  46. }
  47. func (self *VMState) Snapshot() int {
  48. self.snapshots = append(self.snapshots, self.state.Copy())
  49. return len(self.snapshots) - 1
  50. }
  51. func (self *VMState) RevertToSnapshot(idx int) {
  52. self.state.Set(self.snapshots[idx])
  53. self.snapshots = self.snapshots[:idx]
  54. }
  55. // GetAccount returns the account object of the given account or nil if the
  56. // account does not exist
  57. func (s *VMState) GetAccount(addr common.Address) vm.Account {
  58. so, err := s.state.GetStateObject(s.ctx, addr)
  59. s.errHandler(err)
  60. if err != nil {
  61. // return a dummy state object to avoid panics
  62. so = s.state.newStateObject(addr)
  63. }
  64. return so
  65. }
  66. // CreateAccount creates creates a new account object and takes ownership.
  67. func (s *VMState) CreateAccount(addr common.Address) vm.Account {
  68. so, err := s.state.CreateStateObject(s.ctx, addr)
  69. s.errHandler(err)
  70. if err != nil {
  71. // return a dummy state object to avoid panics
  72. so = s.state.newStateObject(addr)
  73. }
  74. return so
  75. }
  76. // AddBalance adds the given amount to the balance of the specified account
  77. func (s *VMState) AddBalance(addr common.Address, amount *big.Int) {
  78. err := s.state.AddBalance(s.ctx, addr, amount)
  79. s.errHandler(err)
  80. }
  81. // SubBalance adds the given amount to the balance of the specified account
  82. func (s *VMState) SubBalance(addr common.Address, amount *big.Int) {
  83. err := s.state.SubBalance(s.ctx, addr, amount)
  84. s.errHandler(err)
  85. }
  86. // GetBalance retrieves the balance from the given address or 0 if the account does
  87. // not exist
  88. func (s *VMState) GetBalance(addr common.Address) *big.Int {
  89. res, err := s.state.GetBalance(s.ctx, addr)
  90. s.errHandler(err)
  91. return res
  92. }
  93. // GetNonce returns the nonce at the given address or 0 if the account does
  94. // not exist
  95. func (s *VMState) GetNonce(addr common.Address) uint64 {
  96. res, err := s.state.GetNonce(s.ctx, addr)
  97. s.errHandler(err)
  98. return res
  99. }
  100. // SetNonce sets the nonce of the specified account
  101. func (s *VMState) SetNonce(addr common.Address, nonce uint64) {
  102. err := s.state.SetNonce(s.ctx, addr, nonce)
  103. s.errHandler(err)
  104. }
  105. // GetCode returns the contract code at the given address or nil if the account
  106. // does not exist
  107. func (s *VMState) GetCode(addr common.Address) []byte {
  108. res, err := s.state.GetCode(s.ctx, addr)
  109. s.errHandler(err)
  110. return res
  111. }
  112. // GetCodeHash returns the contract code hash at the given address
  113. func (s *VMState) GetCodeHash(addr common.Address) common.Hash {
  114. res, err := s.state.GetCode(s.ctx, addr)
  115. s.errHandler(err)
  116. return crypto.Keccak256Hash(res)
  117. }
  118. // GetCodeSize returns the contract code size at the given address
  119. func (s *VMState) GetCodeSize(addr common.Address) int {
  120. res, err := s.state.GetCode(s.ctx, addr)
  121. s.errHandler(err)
  122. return len(res)
  123. }
  124. // SetCode sets the contract code at the specified account
  125. func (s *VMState) SetCode(addr common.Address, code []byte) {
  126. err := s.state.SetCode(s.ctx, addr, code)
  127. s.errHandler(err)
  128. }
  129. // AddRefund adds an amount to the refund value collected during a vm execution
  130. func (s *VMState) AddRefund(gas *big.Int) {
  131. s.state.AddRefund(gas)
  132. }
  133. // GetRefund returns the refund value collected during a vm execution
  134. func (s *VMState) GetRefund() *big.Int {
  135. return s.state.GetRefund()
  136. }
  137. // GetState returns the contract storage value at storage address b from the
  138. // contract address a or common.Hash{} if the account does not exist
  139. func (s *VMState) GetState(a common.Address, b common.Hash) common.Hash {
  140. res, err := s.state.GetState(s.ctx, a, b)
  141. s.errHandler(err)
  142. return res
  143. }
  144. // SetState sets the storage value at storage address key of the account addr
  145. func (s *VMState) SetState(addr common.Address, key common.Hash, value common.Hash) {
  146. err := s.state.SetState(s.ctx, addr, key, value)
  147. s.errHandler(err)
  148. }
  149. // Suicide marks an account to be removed and clears its balance
  150. func (s *VMState) Suicide(addr common.Address) bool {
  151. res, err := s.state.Suicide(s.ctx, addr)
  152. s.errHandler(err)
  153. return res
  154. }
  155. // Exist returns true if an account exists at the given address
  156. func (s *VMState) Exist(addr common.Address) bool {
  157. res, err := s.state.HasAccount(s.ctx, addr)
  158. s.errHandler(err)
  159. return res
  160. }
  161. // Empty returns true if the account at the given address is considered empty
  162. func (s *VMState) Empty(addr common.Address) bool {
  163. so, err := s.state.GetStateObject(s.ctx, addr)
  164. s.errHandler(err)
  165. return so == nil || so.empty()
  166. }
  167. // HasSuicided returns true if the given account has been marked for deletion
  168. // or false if the account does not exist
  169. func (s *VMState) HasSuicided(addr common.Address) bool {
  170. res, err := s.state.HasSuicided(s.ctx, addr)
  171. s.errHandler(err)
  172. return res
  173. }