vm_env.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/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 *types.Log) {}
  39. func (s *VMState) AddPreimage(hash common.Hash, preimage []byte) {}
  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. // CreateAccount creates creates a new account object and takes ownership.
  55. func (s *VMState) CreateAccount(addr common.Address) {
  56. _, err := s.state.CreateStateObject(s.ctx, addr)
  57. s.errHandler(err)
  58. }
  59. // AddBalance adds the given amount to the balance of the specified account
  60. func (s *VMState) AddBalance(addr common.Address, amount *big.Int) {
  61. err := s.state.AddBalance(s.ctx, addr, amount)
  62. s.errHandler(err)
  63. }
  64. // SubBalance adds the given amount to the balance of the specified account
  65. func (s *VMState) SubBalance(addr common.Address, amount *big.Int) {
  66. err := s.state.SubBalance(s.ctx, addr, amount)
  67. s.errHandler(err)
  68. }
  69. // ForEachStorage calls a callback function for every key/value pair found
  70. // in the local storage cache. Note that unlike core/state.StateObject,
  71. // light.StateObject only returns cached values and doesn't download the
  72. // entire storage tree.
  73. func (s *VMState) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) {
  74. err := s.state.ForEachStorage(s.ctx, addr, cb)
  75. s.errHandler(err)
  76. }
  77. // GetBalance retrieves the balance from the given address or 0 if the account does
  78. // not exist
  79. func (s *VMState) GetBalance(addr common.Address) *big.Int {
  80. res, err := s.state.GetBalance(s.ctx, addr)
  81. s.errHandler(err)
  82. return res
  83. }
  84. // GetNonce returns the nonce at the given address or 0 if the account does
  85. // not exist
  86. func (s *VMState) GetNonce(addr common.Address) uint64 {
  87. res, err := s.state.GetNonce(s.ctx, addr)
  88. s.errHandler(err)
  89. return res
  90. }
  91. // SetNonce sets the nonce of the specified account
  92. func (s *VMState) SetNonce(addr common.Address, nonce uint64) {
  93. err := s.state.SetNonce(s.ctx, addr, nonce)
  94. s.errHandler(err)
  95. }
  96. // GetCode returns the contract code at the given address or nil if the account
  97. // does not exist
  98. func (s *VMState) GetCode(addr common.Address) []byte {
  99. res, err := s.state.GetCode(s.ctx, addr)
  100. s.errHandler(err)
  101. return res
  102. }
  103. // GetCodeHash returns the contract code hash at the given address
  104. func (s *VMState) GetCodeHash(addr common.Address) common.Hash {
  105. res, err := s.state.GetCode(s.ctx, addr)
  106. s.errHandler(err)
  107. return crypto.Keccak256Hash(res)
  108. }
  109. // GetCodeSize returns the contract code size at the given address
  110. func (s *VMState) GetCodeSize(addr common.Address) int {
  111. res, err := s.state.GetCode(s.ctx, addr)
  112. s.errHandler(err)
  113. return len(res)
  114. }
  115. // SetCode sets the contract code at the specified account
  116. func (s *VMState) SetCode(addr common.Address, code []byte) {
  117. err := s.state.SetCode(s.ctx, addr, code)
  118. s.errHandler(err)
  119. }
  120. // AddRefund adds an amount to the refund value collected during a vm execution
  121. func (s *VMState) AddRefund(gas *big.Int) {
  122. s.state.AddRefund(gas)
  123. }
  124. // GetRefund returns the refund value collected during a vm execution
  125. func (s *VMState) GetRefund() *big.Int {
  126. return s.state.GetRefund()
  127. }
  128. // GetState returns the contract storage value at storage address b from the
  129. // contract address a or common.Hash{} if the account does not exist
  130. func (s *VMState) GetState(a common.Address, b common.Hash) common.Hash {
  131. res, err := s.state.GetState(s.ctx, a, b)
  132. s.errHandler(err)
  133. return res
  134. }
  135. // SetState sets the storage value at storage address key of the account addr
  136. func (s *VMState) SetState(addr common.Address, key common.Hash, value common.Hash) {
  137. err := s.state.SetState(s.ctx, addr, key, value)
  138. s.errHandler(err)
  139. }
  140. // Suicide marks an account to be removed and clears its balance
  141. func (s *VMState) Suicide(addr common.Address) bool {
  142. res, err := s.state.Suicide(s.ctx, addr)
  143. s.errHandler(err)
  144. return res
  145. }
  146. // Exist returns true if an account exists at the given address
  147. func (s *VMState) Exist(addr common.Address) bool {
  148. res, err := s.state.HasAccount(s.ctx, addr)
  149. s.errHandler(err)
  150. return res
  151. }
  152. // Empty returns true if the account at the given address is considered empty
  153. func (s *VMState) Empty(addr common.Address) bool {
  154. so, err := s.state.GetStateObject(s.ctx, addr)
  155. s.errHandler(err)
  156. return so == nil || so.empty()
  157. }
  158. // HasSuicided returns true if the given account has been marked for deletion
  159. // or false if the account does not exist
  160. func (s *VMState) HasSuicided(addr common.Address) bool {
  161. res, err := s.state.HasSuicided(s.ctx, addr)
  162. s.errHandler(err)
  163. return res
  164. }