state.go 8.8 KB

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