state_object.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. // Copyright 2014 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 state
  17. import (
  18. "bytes"
  19. "errors"
  20. "fmt"
  21. "io"
  22. "math/big"
  23. "sync"
  24. "time"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. "github.com/ethereum/go-ethereum/metrics"
  28. "github.com/ethereum/go-ethereum/rlp"
  29. )
  30. var emptyCodeHash = crypto.Keccak256(nil)
  31. type Code []byte
  32. func (c Code) String() string {
  33. return string(c) //strings.Join(Disassemble(c), " ")
  34. }
  35. type Storage map[common.Hash]common.Hash
  36. func (s Storage) String() (str string) {
  37. for key, value := range s {
  38. str += fmt.Sprintf("%X : %X\n", key, value)
  39. }
  40. return
  41. }
  42. func (s Storage) Copy() Storage {
  43. cpy := make(Storage)
  44. for key, value := range s {
  45. cpy[key] = value
  46. }
  47. return cpy
  48. }
  49. // stateObject represents an Ethereum account which is being modified.
  50. //
  51. // The usage pattern is as follows:
  52. // First you need to obtain a state object.
  53. // Account values can be accessed and modified through the object.
  54. // Finally, call CommitTrie to write the modified storage trie into a database.
  55. type stateObject struct {
  56. address common.Address
  57. addrHash common.Hash // hash of ethereum address of the account
  58. data Account
  59. db *StateDB
  60. // DB error.
  61. // State objects are used by the consensus core and VM which are
  62. // unable to deal with database-level errors. Any error that occurs
  63. // during a database read is memoized here and will eventually be returned
  64. // by StateDB.Commit.
  65. dbErr error
  66. // Write caches.
  67. trie Trie // storage trie, which becomes non-nil on first access
  68. code Code // contract bytecode, which gets set when code is loaded
  69. // Quorum
  70. // contains extra data that is linked to the account
  71. accountExtraData *AccountExtraData
  72. // as there are many fields in accountExtraData which might be concurrently changed
  73. // this is to make sure we can keep track of changes individually.
  74. accountExtraDataMutex sync.Mutex
  75. originStorage Storage // Storage cache of original entries to dedup rewrites, reset for every transaction
  76. pendingStorage Storage // Storage entries that need to be flushed to disk, at the end of an entire block
  77. dirtyStorage Storage // Storage entries that have been modified in the current transaction execution
  78. fakeStorage Storage // Fake storage which constructed by caller for debugging purpose.
  79. // Cache flags.
  80. // When an object is marked suicided it will be delete from the trie
  81. // during the "update" phase of the state transition.
  82. dirtyCode bool // true if the code was updated
  83. suicided bool
  84. deleted bool
  85. // Quorum
  86. // flag to track changes in AccountExtraData
  87. dirtyAccountExtraData bool
  88. mux sync.Mutex
  89. }
  90. // empty returns whether the account is considered empty.
  91. func (s *stateObject) empty() bool {
  92. return s.data.Nonce == 0 && s.data.Balance.Sign() == 0 && bytes.Equal(s.data.CodeHash, emptyCodeHash)
  93. }
  94. // Account is the Ethereum consensus representation of accounts.
  95. // These objects are stored in the main account trie.
  96. type Account struct {
  97. Nonce uint64
  98. Balance *big.Int
  99. Root common.Hash // merkle root of the storage trie
  100. CodeHash []byte
  101. }
  102. // newObject creates a state object.
  103. func newObject(db *StateDB, address common.Address, data Account) *stateObject {
  104. if data.Balance == nil {
  105. data.Balance = new(big.Int)
  106. }
  107. if data.CodeHash == nil {
  108. data.CodeHash = emptyCodeHash
  109. }
  110. if data.Root == (common.Hash{}) {
  111. data.Root = emptyRoot
  112. }
  113. return &stateObject{
  114. db: db,
  115. address: address,
  116. addrHash: crypto.Keccak256Hash(address[:]),
  117. data: data,
  118. originStorage: make(Storage),
  119. pendingStorage: make(Storage),
  120. dirtyStorage: make(Storage),
  121. }
  122. }
  123. // EncodeRLP implements rlp.Encoder.
  124. func (s *stateObject) EncodeRLP(w io.Writer) error {
  125. return rlp.Encode(w, s.data)
  126. }
  127. // setError remembers the first non-nil error it is called with.
  128. func (s *stateObject) setError(err error) {
  129. if s.dbErr == nil {
  130. s.dbErr = err
  131. }
  132. }
  133. func (s *stateObject) markSuicided() {
  134. s.suicided = true
  135. }
  136. func (s *stateObject) touch() {
  137. s.db.journal.append(touchChange{
  138. account: &s.address,
  139. })
  140. if s.address == ripemd {
  141. // Explicitly put it in the dirty-cache, which is otherwise generated from
  142. // flattened journals.
  143. s.db.journal.dirty(s.address)
  144. }
  145. }
  146. func (s *stateObject) getTrie(db Database) Trie {
  147. if s.trie == nil {
  148. // Try fetching from prefetcher first
  149. // We don't prefetch empty tries
  150. if s.data.Root != emptyRoot && s.db.prefetcher != nil {
  151. // When the miner is creating the pending state, there is no
  152. // prefetcher
  153. s.trie = s.db.prefetcher.trie(s.data.Root)
  154. }
  155. if s.trie == nil {
  156. var err error
  157. s.trie, err = db.OpenStorageTrie(s.addrHash, s.data.Root)
  158. if err != nil {
  159. s.trie, _ = db.OpenStorageTrie(s.addrHash, common.Hash{})
  160. s.setError(fmt.Errorf("can't create storage trie: %v", err))
  161. }
  162. }
  163. }
  164. return s.trie
  165. }
  166. func (so *stateObject) storageRoot(db Database) common.Hash {
  167. return so.getTrie(db).Hash()
  168. }
  169. // GetState retrieves a value from the account storage trie.
  170. func (s *stateObject) GetState(db Database, key common.Hash) common.Hash {
  171. // If the fake storage is set, only lookup the state here(in the debugging mode)
  172. if s.fakeStorage != nil {
  173. return s.fakeStorage[key]
  174. }
  175. // If we have a dirty value for this state entry, return it
  176. value, dirty := s.dirtyStorage[key]
  177. if dirty {
  178. return value
  179. }
  180. // Otherwise return the entry's original value
  181. return s.GetCommittedState(db, key)
  182. }
  183. // GetCommittedState retrieves a value from the committed account storage trie.
  184. func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Hash {
  185. // If the fake storage is set, only lookup the state here(in the debugging mode)
  186. if s.fakeStorage != nil {
  187. return s.fakeStorage[key]
  188. }
  189. // If we have a pending write or clean cached, return that
  190. if value, pending := s.pendingStorage[key]; pending {
  191. return value
  192. }
  193. if value, cached := s.originStorage[key]; cached {
  194. return value
  195. }
  196. // If no live objects are available, attempt to use snapshots
  197. var (
  198. enc []byte
  199. err error
  200. meter *time.Duration
  201. )
  202. readStart := time.Now()
  203. if metrics.EnabledExpensive {
  204. // If the snap is 'under construction', the first lookup may fail. If that
  205. // happens, we don't want to double-count the time elapsed. Thus this
  206. // dance with the metering.
  207. defer func() {
  208. if meter != nil {
  209. *meter += time.Since(readStart)
  210. }
  211. }()
  212. }
  213. if s.db.snap != nil {
  214. if metrics.EnabledExpensive {
  215. meter = &s.db.SnapshotStorageReads
  216. }
  217. // If the object was destructed in *this* block (and potentially resurrected),
  218. // the storage has been cleared out, and we should *not* consult the previous
  219. // snapshot about any storage values. The only possible alternatives are:
  220. // 1) resurrect happened, and new slot values were set -- those should
  221. // have been handles via pendingStorage above.
  222. // 2) we don't have new values, and can deliver empty response back
  223. if _, destructed := s.db.snapDestructs[s.addrHash]; destructed {
  224. return common.Hash{}
  225. }
  226. enc, err = s.db.snap.Storage(s.addrHash, crypto.Keccak256Hash(key.Bytes()))
  227. }
  228. // If snapshot unavailable or reading from it failed, load from the database
  229. if s.db.snap == nil || err != nil {
  230. if meter != nil {
  231. // If we already spent time checking the snapshot, account for it
  232. // and reset the readStart
  233. *meter += time.Since(readStart)
  234. readStart = time.Now()
  235. }
  236. if metrics.EnabledExpensive {
  237. meter = &s.db.StorageReads
  238. }
  239. if enc, err = s.getTrie(db).TryGet(key.Bytes()); err != nil {
  240. s.setError(err)
  241. return common.Hash{}
  242. }
  243. }
  244. var value common.Hash
  245. if len(enc) > 0 {
  246. _, content, _, err := rlp.Split(enc)
  247. if err != nil {
  248. s.setError(err)
  249. }
  250. value.SetBytes(content)
  251. }
  252. s.mux.Lock()
  253. defer s.mux.Unlock()
  254. s.originStorage[key] = value
  255. return value
  256. }
  257. // SetState updates a value in account storage.
  258. func (s *stateObject) SetState(db Database, key, value common.Hash) {
  259. // If the fake storage is set, put the temporary state update here.
  260. if s.fakeStorage != nil {
  261. s.fakeStorage[key] = value
  262. return
  263. }
  264. // If the new value is the same as old, don't set
  265. prev := s.GetState(db, key)
  266. if prev == value {
  267. return
  268. }
  269. // New value is different, update and journal the change
  270. s.db.journal.append(storageChange{
  271. account: &s.address,
  272. key: key,
  273. prevalue: prev,
  274. })
  275. s.setState(key, value)
  276. }
  277. // SetStorage replaces the entire state storage with the given one.
  278. //
  279. // After this function is called, all original state will be ignored and state
  280. // lookup only happens in the fake state storage.
  281. //
  282. // Note this function should only be used for debugging purpose.
  283. func (s *stateObject) SetStorage(storage map[common.Hash]common.Hash) {
  284. // Allocate fake storage if it's nil.
  285. if s.fakeStorage == nil {
  286. s.fakeStorage = make(Storage)
  287. }
  288. for key, value := range storage {
  289. s.fakeStorage[key] = value
  290. }
  291. // Don't bother journal since this function should only be used for
  292. // debugging and the `fake` storage won't be committed to database.
  293. }
  294. func (s *stateObject) setState(key, value common.Hash) {
  295. s.mux.Lock()
  296. defer s.mux.Unlock()
  297. s.dirtyStorage[key] = value
  298. }
  299. // finalise moves all dirty storage slots into the pending area to be hashed or
  300. // committed later. It is invoked at the end of every transaction.
  301. func (s *stateObject) finalise(prefetch bool) {
  302. s.mux.Lock()
  303. defer s.mux.Unlock()
  304. slotsToPrefetch := make([][]byte, 0, len(s.dirtyStorage))
  305. for key, value := range s.dirtyStorage {
  306. s.pendingStorage[key] = value
  307. if value != s.originStorage[key] {
  308. slotsToPrefetch = append(slotsToPrefetch, common.CopyBytes(key[:])) // Copy needed for closure
  309. }
  310. }
  311. if s.db.prefetcher != nil && prefetch && len(slotsToPrefetch) > 0 && s.data.Root != emptyRoot {
  312. s.db.prefetcher.prefetch(s.data.Root, slotsToPrefetch)
  313. }
  314. if len(s.dirtyStorage) > 0 {
  315. s.dirtyStorage = make(Storage)
  316. }
  317. }
  318. // updateTrie writes cached storage modifications into the object's storage trie.
  319. // It will return nil if the trie has not been loaded and no changes have been made
  320. func (s *stateObject) updateTrie(db Database) Trie {
  321. // Make sure all dirty slots are finalized into the pending storage area
  322. s.finalise(false) // Don't prefetch any more, pull directly if need be
  323. if len(s.pendingStorage) == 0 {
  324. return s.trie
  325. }
  326. // Track the amount of time wasted on updating the storage trie
  327. if metrics.EnabledExpensive {
  328. defer func(start time.Time) { s.db.StorageUpdates += time.Since(start) }(time.Now())
  329. }
  330. // The snapshot storage map for the object
  331. var storage map[common.Hash][]byte
  332. // Insert all the pending updates into the trie
  333. tr := s.getTrie(db)
  334. hasher := s.db.hasher
  335. s.mux.Lock()
  336. defer s.mux.Unlock()
  337. usedStorage := make([][]byte, 0, len(s.pendingStorage))
  338. for key, value := range s.pendingStorage {
  339. // Skip noop changes, persist actual changes
  340. if value == s.originStorage[key] {
  341. continue
  342. }
  343. s.originStorage[key] = value
  344. var v []byte
  345. if (value == common.Hash{}) {
  346. s.setError(tr.TryDelete(key[:]))
  347. } else {
  348. // Encoding []byte cannot fail, ok to ignore the error.
  349. v, _ = rlp.EncodeToBytes(common.TrimLeftZeroes(value[:]))
  350. s.setError(tr.TryUpdate(key[:], v))
  351. }
  352. // If state snapshotting is active, cache the data til commit
  353. if s.db.snap != nil {
  354. if storage == nil {
  355. // Retrieve the old storage map, if available, create a new one otherwise
  356. if storage = s.db.snapStorage[s.addrHash]; storage == nil {
  357. storage = make(map[common.Hash][]byte)
  358. s.db.snapStorage[s.addrHash] = storage
  359. }
  360. }
  361. storage[crypto.HashData(hasher, key[:])] = v // v will be nil if value is 0x00
  362. }
  363. usedStorage = append(usedStorage, common.CopyBytes(key[:])) // Copy needed for closure
  364. }
  365. if s.db.prefetcher != nil {
  366. s.db.prefetcher.used(s.data.Root, usedStorage)
  367. }
  368. if len(s.pendingStorage) > 0 {
  369. s.pendingStorage = make(Storage)
  370. }
  371. return tr
  372. }
  373. // UpdateRoot sets the trie root to the current root hash of
  374. func (s *stateObject) updateRoot(db Database) {
  375. // If nothing changed, don't bother with hashing anything
  376. if s.updateTrie(db) == nil {
  377. return
  378. }
  379. // Track the amount of time wasted on hashing the storage trie
  380. if metrics.EnabledExpensive {
  381. defer func(start time.Time) { s.db.StorageHashes += time.Since(start) }(time.Now())
  382. }
  383. s.data.Root = s.trie.Hash()
  384. }
  385. // CommitTrie the storage trie of the object to db.
  386. // This updates the trie root.
  387. func (s *stateObject) CommitTrie(db Database) error {
  388. // If nothing changed, don't bother with hashing anything
  389. if s.updateTrie(db) == nil {
  390. return nil
  391. }
  392. if s.dbErr != nil {
  393. return s.dbErr
  394. }
  395. // Track the amount of time wasted on committing the storage trie
  396. if metrics.EnabledExpensive {
  397. defer func(start time.Time) { s.db.StorageCommits += time.Since(start) }(time.Now())
  398. }
  399. root, err := s.trie.Commit(nil)
  400. if err == nil {
  401. s.data.Root = root
  402. }
  403. return err
  404. }
  405. // AddBalance adds amount to s's balance.
  406. // It is used to add funds to the destination account of a transfer.
  407. func (s *stateObject) AddBalance(amount *big.Int) {
  408. // EIP161: We must check emptiness for the objects such that the account
  409. // clearing (0,0,0 objects) can take effect.
  410. if amount.Sign() == 0 {
  411. if s.empty() {
  412. s.touch()
  413. }
  414. return
  415. }
  416. s.SetBalance(new(big.Int).Add(s.Balance(), amount))
  417. }
  418. // SubBalance removes amount from s's balance.
  419. // It is used to remove funds from the origin account of a transfer.
  420. func (s *stateObject) SubBalance(amount *big.Int) {
  421. if amount.Sign() == 0 {
  422. return
  423. }
  424. s.SetBalance(new(big.Int).Sub(s.Balance(), amount))
  425. }
  426. func (s *stateObject) SetBalance(amount *big.Int) {
  427. s.db.journal.append(balanceChange{
  428. account: &s.address,
  429. prev: new(big.Int).Set(s.data.Balance),
  430. })
  431. s.setBalance(amount)
  432. }
  433. func (s *stateObject) setBalance(amount *big.Int) {
  434. s.data.Balance = amount
  435. }
  436. // Return the gas back to the origin. Used by the Virtual machine or Closures
  437. func (s *stateObject) ReturnGas(gas *big.Int) {}
  438. func (s *stateObject) deepCopy(db *StateDB) *stateObject {
  439. s.mux.Lock()
  440. defer s.mux.Unlock()
  441. stateObject := newObject(db, s.address, s.data)
  442. if s.trie != nil {
  443. stateObject.trie = db.db.CopyTrie(s.trie)
  444. }
  445. stateObject.code = s.code
  446. stateObject.dirtyStorage = s.dirtyStorage.Copy()
  447. stateObject.originStorage = s.originStorage.Copy()
  448. stateObject.pendingStorage = s.pendingStorage.Copy()
  449. stateObject.suicided = s.suicided
  450. stateObject.dirtyCode = s.dirtyCode
  451. stateObject.deleted = s.deleted
  452. // Quorum - copy AccountExtraData
  453. stateObject.accountExtraData = s.accountExtraData
  454. stateObject.dirtyAccountExtraData = s.dirtyAccountExtraData
  455. return stateObject
  456. }
  457. //
  458. // Attribute accessors
  459. //
  460. // Returns the address of the contract/account
  461. func (s *stateObject) Address() common.Address {
  462. return s.address
  463. }
  464. // Code returns the contract code associated with this object, if any.
  465. func (s *stateObject) Code(db Database) []byte {
  466. if s.code != nil {
  467. return s.code
  468. }
  469. if bytes.Equal(s.CodeHash(), emptyCodeHash) {
  470. return nil
  471. }
  472. code, err := db.ContractCode(s.addrHash, common.BytesToHash(s.CodeHash()))
  473. if err != nil {
  474. s.setError(fmt.Errorf("can't load code hash %x: %v", s.CodeHash(), err))
  475. }
  476. s.code = code
  477. return code
  478. }
  479. // CodeSize returns the size of the contract code associated with this object,
  480. // or zero if none. This method is an almost mirror of Code, but uses a cache
  481. // inside the database to avoid loading codes seen recently.
  482. func (s *stateObject) CodeSize(db Database) int {
  483. if s.code != nil {
  484. return len(s.code)
  485. }
  486. if bytes.Equal(s.CodeHash(), emptyCodeHash) {
  487. return 0
  488. }
  489. size, err := db.ContractCodeSize(s.addrHash, common.BytesToHash(s.CodeHash()))
  490. if err != nil {
  491. s.setError(fmt.Errorf("can't load code size %x: %v", s.CodeHash(), err))
  492. }
  493. return size
  494. }
  495. func (s *stateObject) SetCode(codeHash common.Hash, code []byte) {
  496. prevcode := s.Code(s.db.db)
  497. s.db.journal.append(codeChange{
  498. account: &s.address,
  499. prevhash: s.CodeHash(),
  500. prevcode: prevcode,
  501. })
  502. s.setCode(codeHash, code)
  503. }
  504. func (s *stateObject) setCode(codeHash common.Hash, code []byte) {
  505. s.code = code
  506. s.data.CodeHash = codeHash[:]
  507. s.dirtyCode = true
  508. }
  509. func (s *stateObject) SetNonce(nonce uint64) {
  510. s.db.journal.append(nonceChange{
  511. account: &s.address,
  512. prev: s.data.Nonce,
  513. })
  514. s.setNonce(nonce)
  515. }
  516. func (s *stateObject) setNonce(nonce uint64) {
  517. s.data.Nonce = nonce
  518. }
  519. // Quorum
  520. // SetAccountExtraData modifies the AccountExtraData reference and journals it
  521. func (s *stateObject) SetAccountExtraData(extraData *AccountExtraData) {
  522. current, _ := s.AccountExtraData()
  523. s.db.journal.append(accountExtraDataChange{
  524. account: &s.address,
  525. prev: current,
  526. })
  527. s.setAccountExtraData(extraData)
  528. }
  529. // A new AccountExtraData will be created if not exists.
  530. // This must be called after successfully acquiring accountExtraDataMutex lock
  531. func (s *stateObject) journalAccountExtraData() *AccountExtraData {
  532. current, _ := s.AccountExtraData()
  533. s.db.journal.append(accountExtraDataChange{
  534. account: &s.address,
  535. prev: current.copy(),
  536. })
  537. if current == nil {
  538. current = &AccountExtraData{}
  539. }
  540. return current
  541. }
  542. // Quorum
  543. // SetStatePrivacyMetadata updates the PrivacyMetadata in AccountExtraData and journals it.
  544. func (s *stateObject) SetStatePrivacyMetadata(pm *PrivacyMetadata) {
  545. s.accountExtraDataMutex.Lock()
  546. defer s.accountExtraDataMutex.Unlock()
  547. newExtraData := s.journalAccountExtraData()
  548. newExtraData.PrivacyMetadata = pm
  549. s.setAccountExtraData(newExtraData)
  550. }
  551. // Quorum
  552. // SetStatePrivacyMetadata updates the PrivacyMetadata in AccountExtraData and journals it.
  553. func (s *stateObject) SetManagedParties(managedParties []string) {
  554. s.accountExtraDataMutex.Lock()
  555. defer s.accountExtraDataMutex.Unlock()
  556. newExtraData := s.journalAccountExtraData()
  557. newExtraData.ManagedParties = managedParties
  558. s.setAccountExtraData(newExtraData)
  559. }
  560. // Quorum
  561. // setAccountExtraData modifies the AccountExtraData reference in this state object
  562. func (s *stateObject) setAccountExtraData(extraData *AccountExtraData) {
  563. s.accountExtraData = extraData
  564. s.dirtyAccountExtraData = true
  565. }
  566. func (s *stateObject) CodeHash() []byte {
  567. return s.data.CodeHash
  568. }
  569. func (s *stateObject) Balance() *big.Int {
  570. return s.data.Balance
  571. }
  572. func (s *stateObject) Nonce() uint64 {
  573. return s.data.Nonce
  574. }
  575. // Quorum
  576. // AccountExtraData returns the extra data in this state object.
  577. // It will also update the reference by searching the accountExtraDataTrie.
  578. //
  579. // This method enforces on returning error and never returns (nil, nil).
  580. func (s *stateObject) AccountExtraData() (*AccountExtraData, error) {
  581. if s.accountExtraData != nil {
  582. return s.accountExtraData, nil
  583. }
  584. val, err := s.getCommittedAccountExtraData()
  585. if err != nil {
  586. return nil, err
  587. }
  588. s.accountExtraData = val
  589. return val, nil
  590. }
  591. // Quorum
  592. // getCommittedAccountExtraData looks for an entry in accountExtraDataTrie.
  593. //
  594. // This method enforces on returning error and never returns (nil, nil).
  595. func (s *stateObject) getCommittedAccountExtraData() (*AccountExtraData, error) {
  596. val, err := s.db.accountExtraDataTrie.TryGet(s.address.Bytes())
  597. if err != nil {
  598. return nil, fmt.Errorf("unable to retrieve data from the accountExtraDataTrie. Cause: %v", err)
  599. }
  600. if len(val) == 0 {
  601. return nil, fmt.Errorf("%s: %w", s.address.Hex(), common.ErrNoAccountExtraData)
  602. }
  603. var extraData AccountExtraData
  604. if err := rlp.DecodeBytes(val, &extraData); err != nil {
  605. return nil, fmt.Errorf("unable to decode to AccountExtraData. Cause: %v", err)
  606. }
  607. return &extraData, nil
  608. }
  609. // Quorum - Privacy Enhancements
  610. // PrivacyMetadata returns the reference to PrivacyMetadata.
  611. // It will returrn an error if no PrivacyMetadata is in the AccountExtraData.
  612. func (s *stateObject) PrivacyMetadata() (*PrivacyMetadata, error) {
  613. extraData, err := s.AccountExtraData()
  614. if err != nil {
  615. return nil, err
  616. }
  617. // extraData can't be nil. Refer to s.AccountExtraData()
  618. if extraData.PrivacyMetadata == nil {
  619. return nil, fmt.Errorf("no privacy metadata data for contract %s", s.address.Hex())
  620. }
  621. return extraData.PrivacyMetadata, nil
  622. }
  623. func (s *stateObject) GetCommittedPrivacyMetadata() (*PrivacyMetadata, error) {
  624. extraData, err := s.getCommittedAccountExtraData()
  625. if err != nil {
  626. return nil, err
  627. }
  628. if extraData == nil || extraData.PrivacyMetadata == nil {
  629. return nil, fmt.Errorf("The provided contract does not have privacy metadata: %x", s.address)
  630. }
  631. return extraData.PrivacyMetadata, nil
  632. }
  633. // End Quorum - Privacy Enhancements
  634. // ManagedParties will return empty if no account extra data found
  635. func (s *stateObject) ManagedParties() ([]string, error) {
  636. extraData, err := s.AccountExtraData()
  637. if errors.Is(err, common.ErrNoAccountExtraData) {
  638. return []string{}, nil
  639. }
  640. if err != nil {
  641. return nil, err
  642. }
  643. // extraData can't be nil. Refer to s.AccountExtraData()
  644. return extraData.ManagedParties, nil
  645. }
  646. // Never called, but must be present to allow stateObject to be used
  647. // as a vm.Account interface that also satisfies the vm.ContractRef
  648. // interface. Interfaces are awesome.
  649. func (s *stateObject) Value() *big.Int {
  650. panic("Value on stateObject should never be called")
  651. }