state_object.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. "bytes"
  19. "fmt"
  20. "math/big"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/crypto"
  23. "github.com/ethereum/go-ethereum/logger"
  24. "github.com/ethereum/go-ethereum/logger/glog"
  25. "github.com/ethereum/go-ethereum/rlp"
  26. "golang.org/x/net/context"
  27. )
  28. var emptyCodeHash = crypto.Keccak256(nil)
  29. // Code represents a contract code in binary form
  30. type Code []byte
  31. // String returns a string representation of the code
  32. func (self Code) String() string {
  33. return string(self) //strings.Join(Disassemble(self), " ")
  34. }
  35. // Storage is a memory map cache of a contract storage
  36. type Storage map[string]common.Hash
  37. // String returns a string representation of the storage cache
  38. func (self Storage) String() (str string) {
  39. for key, value := range self {
  40. str += fmt.Sprintf("%X : %X\n", key, value)
  41. }
  42. return
  43. }
  44. // Copy copies the contents of a storage cache
  45. func (self Storage) Copy() Storage {
  46. cpy := make(Storage)
  47. for key, value := range self {
  48. cpy[key] = value
  49. }
  50. return cpy
  51. }
  52. // StateObject is a memory representation of an account or contract and its storage.
  53. // This version is ODR capable, caching only the already accessed part of the
  54. // storage, retrieving unknown parts on-demand from the ODR backend. Changes are
  55. // never stored in the local database, only in the memory objects.
  56. type StateObject struct {
  57. odr OdrBackend
  58. trie *LightTrie
  59. // Address belonging to this account
  60. address common.Address
  61. // The balance of the account
  62. balance *big.Int
  63. // The nonce of the account
  64. nonce uint64
  65. // The code hash if code is present (i.e. a contract)
  66. codeHash []byte
  67. // The code for this account
  68. code Code
  69. // Temporarily initialisation code
  70. initCode Code
  71. // Cached storage (flushed when updated)
  72. storage Storage
  73. // Mark for deletion
  74. // When an object is marked for deletion it will be delete from the trie
  75. // during the "update" phase of the state transition
  76. remove bool
  77. deleted bool
  78. dirty bool
  79. }
  80. // NewStateObject creates a new StateObject of the specified account address
  81. func NewStateObject(address common.Address, odr OdrBackend) *StateObject {
  82. object := &StateObject{
  83. odr: odr,
  84. address: address,
  85. balance: new(big.Int),
  86. dirty: true,
  87. codeHash: emptyCodeHash,
  88. storage: make(Storage),
  89. }
  90. object.trie = NewLightTrie(common.Hash{}, odr, true)
  91. return object
  92. }
  93. // MarkForDeletion marks an account to be removed
  94. func (self *StateObject) MarkForDeletion() {
  95. self.remove = true
  96. self.dirty = true
  97. if glog.V(logger.Core) {
  98. glog.Infof("%x: #%d %v X\n", self.Address(), self.nonce, self.balance)
  99. }
  100. }
  101. // getAddr gets the storage value at the given address from the trie
  102. func (c *StateObject) getAddr(ctx context.Context, addr common.Hash) (common.Hash, error) {
  103. var ret []byte
  104. val, err := c.trie.Get(ctx, addr[:])
  105. if err != nil {
  106. return common.Hash{}, err
  107. }
  108. rlp.DecodeBytes(val, &ret)
  109. return common.BytesToHash(ret), nil
  110. }
  111. // Storage returns the storage cache object of the account
  112. func (self *StateObject) Storage() Storage {
  113. return self.storage
  114. }
  115. // GetState returns the storage value at the given address from either the cache
  116. // or the trie
  117. func (self *StateObject) GetState(ctx context.Context, key common.Hash) (common.Hash, error) {
  118. strkey := key.Str()
  119. value, exists := self.storage[strkey]
  120. if !exists {
  121. var err error
  122. value, err = self.getAddr(ctx, key)
  123. if err != nil {
  124. return common.Hash{}, err
  125. }
  126. if (value != common.Hash{}) {
  127. self.storage[strkey] = value
  128. }
  129. }
  130. return value, nil
  131. }
  132. // SetState sets the storage value at the given address
  133. func (self *StateObject) SetState(k, value common.Hash) {
  134. self.storage[k.Str()] = value
  135. self.dirty = true
  136. }
  137. // AddBalance adds the given amount to the account balance
  138. func (c *StateObject) AddBalance(amount *big.Int) {
  139. c.SetBalance(new(big.Int).Add(c.balance, amount))
  140. if glog.V(logger.Core) {
  141. glog.Infof("%x: #%d %v (+ %v)\n", c.Address(), c.nonce, c.balance, amount)
  142. }
  143. }
  144. // SubBalance subtracts the given amount from the account balance
  145. func (c *StateObject) SubBalance(amount *big.Int) {
  146. c.SetBalance(new(big.Int).Sub(c.balance, amount))
  147. if glog.V(logger.Core) {
  148. glog.Infof("%x: #%d %v (- %v)\n", c.Address(), c.nonce, c.balance, amount)
  149. }
  150. }
  151. // SetBalance sets the account balance to the given amount
  152. func (c *StateObject) SetBalance(amount *big.Int) {
  153. c.balance = amount
  154. c.dirty = true
  155. }
  156. // Copy creates a copy of the state object
  157. func (self *StateObject) Copy() *StateObject {
  158. stateObject := NewStateObject(self.Address(), self.odr)
  159. stateObject.balance.Set(self.balance)
  160. stateObject.codeHash = common.CopyBytes(self.codeHash)
  161. stateObject.nonce = self.nonce
  162. stateObject.trie = self.trie
  163. stateObject.code = common.CopyBytes(self.code)
  164. stateObject.initCode = common.CopyBytes(self.initCode)
  165. stateObject.storage = self.storage.Copy()
  166. stateObject.remove = self.remove
  167. stateObject.dirty = self.dirty
  168. stateObject.deleted = self.deleted
  169. return stateObject
  170. }
  171. //
  172. // Attribute accessors
  173. //
  174. // Balance returns the account balance
  175. func (self *StateObject) Balance() *big.Int {
  176. return self.balance
  177. }
  178. // Address returns the address of the contract/account
  179. func (c *StateObject) Address() common.Address {
  180. return c.address
  181. }
  182. // Code returns the contract code
  183. func (self *StateObject) Code() []byte {
  184. return self.code
  185. }
  186. // SetCode sets the contract code
  187. func (self *StateObject) SetCode(code []byte) {
  188. self.code = code
  189. self.codeHash = crypto.Keccak256(code)
  190. self.dirty = true
  191. }
  192. // SetNonce sets the account nonce
  193. func (self *StateObject) SetNonce(nonce uint64) {
  194. self.nonce = nonce
  195. self.dirty = true
  196. }
  197. // Nonce returns the account nonce
  198. func (self *StateObject) Nonce() uint64 {
  199. return self.nonce
  200. }
  201. // Encoding
  202. type extStateObject struct {
  203. Nonce uint64
  204. Balance *big.Int
  205. Root common.Hash
  206. CodeHash []byte
  207. }
  208. // DecodeObject decodes an RLP-encoded state object.
  209. func DecodeObject(ctx context.Context, address common.Address, odr OdrBackend, data []byte) (*StateObject, error) {
  210. var (
  211. obj = &StateObject{address: address, odr: odr, storage: make(Storage)}
  212. ext extStateObject
  213. err error
  214. )
  215. if err = rlp.DecodeBytes(data, &ext); err != nil {
  216. return nil, err
  217. }
  218. obj.trie = NewLightTrie(ext.Root, odr, true)
  219. if !bytes.Equal(ext.CodeHash, emptyCodeHash) {
  220. if obj.code, err = retrieveNodeData(ctx, obj.odr, common.BytesToHash(ext.CodeHash)); err != nil {
  221. return nil, fmt.Errorf("can't find code for hash %x: %v", ext.CodeHash, err)
  222. }
  223. }
  224. obj.nonce = ext.Nonce
  225. obj.balance = ext.Balance
  226. obj.codeHash = ext.CodeHash
  227. return obj, nil
  228. }