state_object.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. // Cached storage (flushed when updated)
  70. storage Storage
  71. // Mark for deletion
  72. // When an object is marked for deletion it will be delete from the trie
  73. // during the "update" phase of the state transition
  74. remove bool
  75. deleted bool
  76. dirty bool
  77. }
  78. // NewStateObject creates a new StateObject of the specified account address
  79. func NewStateObject(address common.Address, odr OdrBackend) *StateObject {
  80. object := &StateObject{
  81. odr: odr,
  82. address: address,
  83. balance: new(big.Int),
  84. dirty: true,
  85. codeHash: emptyCodeHash,
  86. storage: make(Storage),
  87. }
  88. object.trie = NewLightTrie(common.Hash{}, odr, true)
  89. return object
  90. }
  91. // MarkForDeletion marks an account to be removed
  92. func (self *StateObject) MarkForDeletion() {
  93. self.remove = true
  94. self.dirty = true
  95. if glog.V(logger.Core) {
  96. glog.Infof("%x: #%d %v X\n", self.Address(), self.nonce, self.balance)
  97. }
  98. }
  99. // getAddr gets the storage value at the given address from the trie
  100. func (c *StateObject) getAddr(ctx context.Context, addr common.Hash) (common.Hash, error) {
  101. var ret []byte
  102. val, err := c.trie.Get(ctx, addr[:])
  103. if err != nil {
  104. return common.Hash{}, err
  105. }
  106. rlp.DecodeBytes(val, &ret)
  107. return common.BytesToHash(ret), nil
  108. }
  109. // Storage returns the storage cache object of the account
  110. func (self *StateObject) Storage() Storage {
  111. return self.storage
  112. }
  113. // GetState returns the storage value at the given address from either the cache
  114. // or the trie
  115. func (self *StateObject) GetState(ctx context.Context, key common.Hash) (common.Hash, error) {
  116. strkey := key.Str()
  117. value, exists := self.storage[strkey]
  118. if !exists {
  119. var err error
  120. value, err = self.getAddr(ctx, key)
  121. if err != nil {
  122. return common.Hash{}, err
  123. }
  124. if (value != common.Hash{}) {
  125. self.storage[strkey] = value
  126. }
  127. }
  128. return value, nil
  129. }
  130. // SetState sets the storage value at the given address
  131. func (self *StateObject) SetState(k, value common.Hash) {
  132. self.storage[k.Str()] = value
  133. self.dirty = true
  134. }
  135. // AddBalance adds the given amount to the account balance
  136. func (c *StateObject) AddBalance(amount *big.Int) {
  137. c.SetBalance(new(big.Int).Add(c.balance, amount))
  138. if glog.V(logger.Core) {
  139. glog.Infof("%x: #%d %v (+ %v)\n", c.Address(), c.nonce, c.balance, amount)
  140. }
  141. }
  142. // SubBalance subtracts the given amount from the account balance
  143. func (c *StateObject) SubBalance(amount *big.Int) {
  144. c.SetBalance(new(big.Int).Sub(c.balance, amount))
  145. if glog.V(logger.Core) {
  146. glog.Infof("%x: #%d %v (- %v)\n", c.Address(), c.nonce, c.balance, amount)
  147. }
  148. }
  149. // SetBalance sets the account balance to the given amount
  150. func (c *StateObject) SetBalance(amount *big.Int) {
  151. c.balance = amount
  152. c.dirty = true
  153. }
  154. // Copy creates a copy of the state object
  155. func (self *StateObject) Copy() *StateObject {
  156. stateObject := NewStateObject(self.Address(), self.odr)
  157. stateObject.balance.Set(self.balance)
  158. stateObject.codeHash = common.CopyBytes(self.codeHash)
  159. stateObject.nonce = self.nonce
  160. stateObject.trie = self.trie
  161. stateObject.code = self.code
  162. stateObject.storage = self.storage.Copy()
  163. stateObject.remove = self.remove
  164. stateObject.dirty = self.dirty
  165. stateObject.deleted = self.deleted
  166. return stateObject
  167. }
  168. //
  169. // Attribute accessors
  170. //
  171. // Balance returns the account balance
  172. func (self *StateObject) Balance() *big.Int {
  173. return self.balance
  174. }
  175. // Address returns the address of the contract/account
  176. func (c *StateObject) Address() common.Address {
  177. return c.address
  178. }
  179. // Code returns the contract code
  180. func (self *StateObject) Code() []byte {
  181. return self.code
  182. }
  183. // SetCode sets the contract code
  184. func (self *StateObject) SetCode(code []byte) {
  185. self.code = code
  186. self.codeHash = crypto.Keccak256(code)
  187. self.dirty = true
  188. }
  189. // SetNonce sets the account nonce
  190. func (self *StateObject) SetNonce(nonce uint64) {
  191. self.nonce = nonce
  192. self.dirty = true
  193. }
  194. // Nonce returns the account nonce
  195. func (self *StateObject) Nonce() uint64 {
  196. return self.nonce
  197. }
  198. // Encoding
  199. type extStateObject struct {
  200. Nonce uint64
  201. Balance *big.Int
  202. Root common.Hash
  203. CodeHash []byte
  204. }
  205. // DecodeObject decodes an RLP-encoded state object.
  206. func DecodeObject(ctx context.Context, address common.Address, odr OdrBackend, data []byte) (*StateObject, error) {
  207. var (
  208. obj = &StateObject{address: address, odr: odr, storage: make(Storage)}
  209. ext extStateObject
  210. err error
  211. )
  212. if err = rlp.DecodeBytes(data, &ext); err != nil {
  213. return nil, err
  214. }
  215. obj.trie = NewLightTrie(ext.Root, odr, true)
  216. if !bytes.Equal(ext.CodeHash, emptyCodeHash) {
  217. if obj.code, err = retrieveNodeData(ctx, obj.odr, common.BytesToHash(ext.CodeHash)); err != nil {
  218. return nil, fmt.Errorf("can't find code for hash %x: %v", ext.CodeHash, err)
  219. }
  220. }
  221. obj.nonce = ext.Nonce
  222. obj.balance = ext.Balance
  223. obj.codeHash = ext.CodeHash
  224. return obj, nil
  225. }