vm_env.go 6.1 KB

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