state.go 9.0 KB

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