state.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // Copyright 2015 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/logger"
  21. "github.com/ethereum/go-ethereum/logger/glog"
  22. "golang.org/x/net/context"
  23. )
  24. // StartingNonce determines the default nonce when new accounts are being created.
  25. var StartingNonce uint64
  26. // LightState is a memory representation of a state.
  27. // This version is ODR capable, caching only the already accessed part of the
  28. // state, retrieving unknown parts on-demand from the ODR backend. Changes are
  29. // never stored in the local database, only in the memory objects.
  30. type LightState struct {
  31. odr OdrBackend
  32. trie *LightTrie
  33. stateObjects map[string]*StateObject
  34. }
  35. // NewLightState creates a new LightState with the specified root.
  36. // Note that the creation of a light state is always successful, even if the
  37. // root is non-existent. In that case, ODR retrieval will always be unsuccessful
  38. // and every operation will return with an error or wait for the context to be
  39. // cancelled.
  40. func NewLightState(root common.Hash, odr OdrBackend) *LightState {
  41. tr := NewLightTrie(root, odr, true)
  42. return &LightState{
  43. odr: odr,
  44. trie: tr,
  45. stateObjects: make(map[string]*StateObject),
  46. }
  47. }
  48. // HasAccount returns true if an account exists at the given address
  49. func (self *LightState) HasAccount(ctx context.Context, addr common.Address) (bool, error) {
  50. so, err := self.GetStateObject(ctx, addr)
  51. return so != nil, err
  52. }
  53. // GetBalance retrieves the balance from the given address or 0 if the account does
  54. // not exist
  55. func (self *LightState) GetBalance(ctx context.Context, addr common.Address) (*big.Int, error) {
  56. stateObject, err := self.GetStateObject(ctx, addr)
  57. if err != nil {
  58. return common.Big0, err
  59. }
  60. if stateObject != nil {
  61. return stateObject.balance, nil
  62. }
  63. return common.Big0, nil
  64. }
  65. // GetNonce returns the nonce at the given address or 0 if the account does
  66. // not exist
  67. func (self *LightState) GetNonce(ctx context.Context, addr common.Address) (uint64, error) {
  68. stateObject, err := self.GetStateObject(ctx, addr)
  69. if err != nil {
  70. return 0, err
  71. }
  72. if stateObject != nil {
  73. return stateObject.nonce, nil
  74. }
  75. return 0, nil
  76. }
  77. // GetCode returns the contract code at the given address or nil if the account
  78. // does not exist
  79. func (self *LightState) GetCode(ctx context.Context, addr common.Address) ([]byte, error) {
  80. stateObject, err := self.GetStateObject(ctx, addr)
  81. if err != nil {
  82. return nil, err
  83. }
  84. if stateObject != nil {
  85. return stateObject.code, nil
  86. }
  87. return nil, nil
  88. }
  89. // GetState returns the contract storage value at storage address b from the
  90. // contract address a or common.Hash{} if the account does not exist
  91. func (self *LightState) GetState(ctx context.Context, a common.Address, b common.Hash) (common.Hash, error) {
  92. stateObject, err := self.GetStateObject(ctx, a)
  93. if err == nil && stateObject != nil {
  94. return stateObject.GetState(ctx, b)
  95. }
  96. return common.Hash{}, err
  97. }
  98. // IsDeleted returns true if the given account has been marked for deletion
  99. // or false if the account does not exist
  100. func (self *LightState) IsDeleted(ctx context.Context, addr common.Address) (bool, error) {
  101. stateObject, err := self.GetStateObject(ctx, addr)
  102. if err == nil && stateObject != nil {
  103. return stateObject.remove, nil
  104. }
  105. return false, err
  106. }
  107. /*
  108. * SETTERS
  109. */
  110. // AddBalance adds the given amount to the balance of the specified account
  111. func (self *LightState) AddBalance(ctx context.Context, addr common.Address, amount *big.Int) error {
  112. stateObject, err := self.GetOrNewStateObject(ctx, addr)
  113. if err == nil && stateObject != nil {
  114. stateObject.AddBalance(amount)
  115. }
  116. return err
  117. }
  118. // SetNonce sets the nonce of the specified account
  119. func (self *LightState) SetNonce(ctx context.Context, addr common.Address, nonce uint64) error {
  120. stateObject, err := self.GetOrNewStateObject(ctx, addr)
  121. if err == nil && stateObject != nil {
  122. stateObject.SetNonce(nonce)
  123. }
  124. return err
  125. }
  126. // SetCode sets the contract code at the specified account
  127. func (self *LightState) SetCode(ctx context.Context, addr common.Address, code []byte) error {
  128. stateObject, err := self.GetOrNewStateObject(ctx, addr)
  129. if err == nil && stateObject != nil {
  130. stateObject.SetCode(code)
  131. }
  132. return err
  133. }
  134. // SetState sets the storage value at storage address key of the account addr
  135. func (self *LightState) SetState(ctx context.Context, addr common.Address, key common.Hash, value common.Hash) error {
  136. stateObject, err := self.GetOrNewStateObject(ctx, addr)
  137. if err == nil && stateObject != nil {
  138. stateObject.SetState(key, value)
  139. }
  140. return err
  141. }
  142. // Delete marks an account to be removed and clears its balance
  143. func (self *LightState) Delete(ctx context.Context, addr common.Address) (bool, error) {
  144. stateObject, err := self.GetOrNewStateObject(ctx, addr)
  145. if err == nil && stateObject != nil {
  146. stateObject.MarkForDeletion()
  147. stateObject.balance = new(big.Int)
  148. return true, nil
  149. }
  150. return false, err
  151. }
  152. //
  153. // Get, set, new state object methods
  154. //
  155. // GetStateObject returns the state object of the given account or nil if the
  156. // account does not exist
  157. func (self *LightState) GetStateObject(ctx context.Context, addr common.Address) (stateObject *StateObject, err error) {
  158. stateObject = self.stateObjects[addr.Str()]
  159. if stateObject != nil {
  160. if stateObject.deleted {
  161. stateObject = nil
  162. }
  163. return stateObject, nil
  164. }
  165. data, err := self.trie.Get(ctx, addr[:])
  166. if err != nil {
  167. return nil, err
  168. }
  169. if len(data) == 0 {
  170. return nil, nil
  171. }
  172. stateObject, err = DecodeObject(ctx, addr, self.odr, []byte(data))
  173. if err != nil {
  174. return nil, err
  175. }
  176. self.SetStateObject(stateObject)
  177. return stateObject, nil
  178. }
  179. // SetStateObject sets the state object of the given account
  180. func (self *LightState) SetStateObject(object *StateObject) {
  181. self.stateObjects[object.Address().Str()] = object
  182. }
  183. // GetOrNewStateObject returns the state object of the given account or creates a
  184. // new one if the account does not exist
  185. func (self *LightState) GetOrNewStateObject(ctx context.Context, addr common.Address) (*StateObject, error) {
  186. stateObject, err := self.GetStateObject(ctx, addr)
  187. if err == nil && (stateObject == nil || stateObject.deleted) {
  188. stateObject, err = self.CreateStateObject(ctx, addr)
  189. }
  190. return stateObject, err
  191. }
  192. // newStateObject creates a state object whether it exists in the state or not
  193. func (self *LightState) newStateObject(addr common.Address) *StateObject {
  194. if glog.V(logger.Core) {
  195. glog.Infof("(+) %x\n", addr)
  196. }
  197. stateObject := NewStateObject(addr, self.odr)
  198. stateObject.SetNonce(StartingNonce)
  199. self.stateObjects[addr.Str()] = stateObject
  200. return stateObject
  201. }
  202. // CreateStateObject creates creates a new state object and takes ownership.
  203. // This is different from "NewStateObject"
  204. func (self *LightState) CreateStateObject(ctx context.Context, addr common.Address) (*StateObject, error) {
  205. // Get previous (if any)
  206. so, err := self.GetStateObject(ctx, addr)
  207. if err != nil {
  208. return nil, err
  209. }
  210. // Create a new one
  211. newSo := self.newStateObject(addr)
  212. // If it existed set the balance to the new account
  213. if so != nil {
  214. newSo.balance = so.balance
  215. }
  216. return newSo, nil
  217. }
  218. //
  219. // Setting, copying of the state methods
  220. //
  221. // Copy creates a copy of the state
  222. func (self *LightState) Copy() *LightState {
  223. // ignore error - we assume state-to-be-copied always exists
  224. state := NewLightState(common.Hash{}, self.odr)
  225. state.trie = self.trie
  226. for k, stateObject := range self.stateObjects {
  227. state.stateObjects[k] = stateObject.Copy()
  228. }
  229. return state
  230. }
  231. // Set copies the contents of the given state onto this state, overwriting
  232. // its contents
  233. func (self *LightState) Set(state *LightState) {
  234. self.trie = state.trie
  235. self.stateObjects = state.stateObjects
  236. }