vm_env.go 6.0 KB

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