state.go 9.5 KB

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