statedb.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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 provides a caching layer atop the Ethereum state trie.
  17. package state
  18. import (
  19. "errors"
  20. "fmt"
  21. "math/big"
  22. "sort"
  23. "sync"
  24. "time"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/core/state/snapshot"
  27. "github.com/ethereum/go-ethereum/core/types"
  28. "github.com/ethereum/go-ethereum/crypto"
  29. "github.com/ethereum/go-ethereum/log"
  30. "github.com/ethereum/go-ethereum/metrics"
  31. "github.com/ethereum/go-ethereum/rlp"
  32. "github.com/ethereum/go-ethereum/trie"
  33. )
  34. type revision struct {
  35. id int
  36. journalIndex int
  37. }
  38. var (
  39. // emptyRoot is the known root hash of an empty trie.
  40. emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
  41. // emptyCode is the known hash of the empty EVM bytecode.
  42. emptyCode = crypto.Keccak256Hash(nil)
  43. )
  44. type proofList [][]byte
  45. func (n *proofList) Put(key []byte, value []byte) error {
  46. *n = append(*n, value)
  47. return nil
  48. }
  49. func (n *proofList) Delete(key []byte) error {
  50. panic("not supported")
  51. }
  52. // StateDBs within the ethereum protocol are used to store anything
  53. // within the merkle trie. StateDBs take care of caching and storing
  54. // nested states. It's the general query interface to retrieve:
  55. // * Contracts
  56. // * Accounts
  57. type StateDB struct {
  58. db Database
  59. trie Trie
  60. snaps *snapshot.Tree
  61. snap snapshot.Snapshot
  62. snapAccounts map[common.Hash][]byte
  63. snapStorage map[common.Hash]map[common.Hash][]byte
  64. snapLock sync.RWMutex // Lock for the concurrent storage updaters
  65. // This map holds 'live' objects, which will get modified while processing a state transition.
  66. stateObjects map[common.Address]*stateObject
  67. stateObjectsPending map[common.Address]struct{} // State objects finalized but not yet written to the trie
  68. stateObjectsDirty map[common.Address]struct{} // State objects modified in the current execution
  69. // DB error.
  70. // State objects are used by the consensus core and VM which are
  71. // unable to deal with database-level errors. Any error that occurs
  72. // during a database read is memoized here and will eventually be returned
  73. // by StateDB.Commit.
  74. dbErr error
  75. // The refund counter, also used by state transitioning.
  76. refund uint64
  77. thash, bhash common.Hash
  78. txIndex int
  79. logs map[common.Hash][]*types.Log
  80. logSize uint
  81. preimages map[common.Hash][]byte
  82. // Journal of state modifications. This is the backbone of
  83. // Snapshot and RevertToSnapshot.
  84. journal *journal
  85. validRevisions []revision
  86. nextRevisionId int
  87. // Measurements gathered during execution for debugging purposes
  88. AccountReads time.Duration
  89. AccountHashes time.Duration
  90. AccountUpdates time.Duration
  91. AccountCommits time.Duration
  92. StorageReads time.Duration
  93. StorageHashes time.Duration
  94. StorageUpdates time.Duration
  95. StorageCommits time.Duration
  96. SnapshotAccountReads time.Duration
  97. SnapshotStorageReads time.Duration
  98. SnapshotCommits time.Duration
  99. }
  100. // Create a new state from a given trie.
  101. func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) {
  102. tr, err := db.OpenTrie(root)
  103. if err != nil {
  104. return nil, err
  105. }
  106. sdb := &StateDB{
  107. db: db,
  108. trie: tr,
  109. snaps: snaps,
  110. stateObjects: make(map[common.Address]*stateObject),
  111. stateObjectsPending: make(map[common.Address]struct{}),
  112. stateObjectsDirty: make(map[common.Address]struct{}),
  113. logs: make(map[common.Hash][]*types.Log),
  114. preimages: make(map[common.Hash][]byte),
  115. journal: newJournal(),
  116. }
  117. if sdb.snaps != nil {
  118. if sdb.snap = sdb.snaps.Snapshot(root); sdb.snap != nil {
  119. sdb.snapAccounts = make(map[common.Hash][]byte)
  120. sdb.snapStorage = make(map[common.Hash]map[common.Hash][]byte)
  121. }
  122. }
  123. return sdb, nil
  124. }
  125. // setError remembers the first non-nil error it is called with.
  126. func (s *StateDB) setError(err error) {
  127. if s.dbErr == nil {
  128. s.dbErr = err
  129. }
  130. }
  131. func (s *StateDB) Error() error {
  132. return s.dbErr
  133. }
  134. // Reset clears out all ephemeral state objects from the state db, but keeps
  135. // the underlying state trie to avoid reloading data for the next operations.
  136. func (s *StateDB) Reset(root common.Hash) error {
  137. tr, err := s.db.OpenTrie(root)
  138. if err != nil {
  139. return err
  140. }
  141. s.trie = tr
  142. s.stateObjects = make(map[common.Address]*stateObject)
  143. s.stateObjectsPending = make(map[common.Address]struct{})
  144. s.stateObjectsDirty = make(map[common.Address]struct{})
  145. s.thash = common.Hash{}
  146. s.bhash = common.Hash{}
  147. s.txIndex = 0
  148. s.logs = make(map[common.Hash][]*types.Log)
  149. s.logSize = 0
  150. s.preimages = make(map[common.Hash][]byte)
  151. s.clearJournalAndRefund()
  152. if s.snaps != nil {
  153. s.snapAccounts, s.snapStorage = nil, nil
  154. if s.snap = s.snaps.Snapshot(root); s.snap != nil {
  155. s.snapAccounts = make(map[common.Hash][]byte)
  156. s.snapStorage = make(map[common.Hash]map[common.Hash][]byte)
  157. }
  158. }
  159. return nil
  160. }
  161. func (s *StateDB) AddLog(log *types.Log) {
  162. s.journal.append(addLogChange{txhash: s.thash})
  163. log.TxHash = s.thash
  164. log.BlockHash = s.bhash
  165. log.TxIndex = uint(s.txIndex)
  166. log.Index = s.logSize
  167. s.logs[s.thash] = append(s.logs[s.thash], log)
  168. s.logSize++
  169. }
  170. func (s *StateDB) GetLogs(hash common.Hash) []*types.Log {
  171. return s.logs[hash]
  172. }
  173. func (s *StateDB) Logs() []*types.Log {
  174. var logs []*types.Log
  175. for _, lgs := range s.logs {
  176. logs = append(logs, lgs...)
  177. }
  178. return logs
  179. }
  180. // AddPreimage records a SHA3 preimage seen by the VM.
  181. func (s *StateDB) AddPreimage(hash common.Hash, preimage []byte) {
  182. if _, ok := s.preimages[hash]; !ok {
  183. s.journal.append(addPreimageChange{hash: hash})
  184. pi := make([]byte, len(preimage))
  185. copy(pi, preimage)
  186. s.preimages[hash] = pi
  187. }
  188. }
  189. // Preimages returns a list of SHA3 preimages that have been submitted.
  190. func (s *StateDB) Preimages() map[common.Hash][]byte {
  191. return s.preimages
  192. }
  193. // AddRefund adds gas to the refund counter
  194. func (s *StateDB) AddRefund(gas uint64) {
  195. s.journal.append(refundChange{prev: s.refund})
  196. s.refund += gas
  197. }
  198. // SubRefund removes gas from the refund counter.
  199. // This method will panic if the refund counter goes below zero
  200. func (s *StateDB) SubRefund(gas uint64) {
  201. s.journal.append(refundChange{prev: s.refund})
  202. if gas > s.refund {
  203. panic(fmt.Sprintf("Refund counter below zero (gas: %d > refund: %d)", gas, s.refund))
  204. }
  205. s.refund -= gas
  206. }
  207. // Exist reports whether the given account address exists in the state.
  208. // Notably this also returns true for suicided accounts.
  209. func (s *StateDB) Exist(addr common.Address) bool {
  210. return s.getStateObject(addr) != nil
  211. }
  212. // Empty returns whether the state object is either non-existent
  213. // or empty according to the EIP161 specification (balance = nonce = code = 0)
  214. func (s *StateDB) Empty(addr common.Address) bool {
  215. so := s.getStateObject(addr)
  216. return so == nil || so.empty()
  217. }
  218. // Retrieve the balance from the given address or 0 if object not found
  219. func (s *StateDB) GetBalance(addr common.Address) *big.Int {
  220. stateObject := s.getStateObject(addr)
  221. if stateObject != nil {
  222. return stateObject.Balance()
  223. }
  224. return common.Big0
  225. }
  226. func (s *StateDB) GetNonce(addr common.Address) uint64 {
  227. stateObject := s.getStateObject(addr)
  228. if stateObject != nil {
  229. return stateObject.Nonce()
  230. }
  231. return 0
  232. }
  233. // TxIndex returns the current transaction index set by Prepare.
  234. func (s *StateDB) TxIndex() int {
  235. return s.txIndex
  236. }
  237. // BlockHash returns the current block hash set by Prepare.
  238. func (s *StateDB) BlockHash() common.Hash {
  239. return s.bhash
  240. }
  241. func (s *StateDB) GetCode(addr common.Address) []byte {
  242. stateObject := s.getStateObject(addr)
  243. if stateObject != nil {
  244. return stateObject.Code(s.db)
  245. }
  246. return nil
  247. }
  248. func (s *StateDB) GetCodeSize(addr common.Address) int {
  249. stateObject := s.getStateObject(addr)
  250. if stateObject == nil {
  251. return 0
  252. }
  253. if stateObject.code != nil {
  254. return len(stateObject.code)
  255. }
  256. size, err := s.db.ContractCodeSize(stateObject.addrHash, common.BytesToHash(stateObject.CodeHash()))
  257. if err != nil {
  258. s.setError(err)
  259. }
  260. return size
  261. }
  262. func (s *StateDB) GetCodeHash(addr common.Address) common.Hash {
  263. stateObject := s.getStateObject(addr)
  264. if stateObject == nil {
  265. return common.Hash{}
  266. }
  267. return common.BytesToHash(stateObject.CodeHash())
  268. }
  269. // GetState retrieves a value from the given account's storage trie.
  270. func (s *StateDB) GetState(addr common.Address, hash common.Hash) common.Hash {
  271. stateObject := s.getStateObject(addr)
  272. if stateObject != nil {
  273. return stateObject.GetState(s.db, hash)
  274. }
  275. return common.Hash{}
  276. }
  277. // GetProof returns the MerkleProof for a given Account
  278. func (s *StateDB) GetProof(a common.Address) ([][]byte, error) {
  279. var proof proofList
  280. err := s.trie.Prove(crypto.Keccak256(a.Bytes()), 0, &proof)
  281. return [][]byte(proof), err
  282. }
  283. // GetProof returns the StorageProof for given key
  284. func (s *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byte, error) {
  285. var proof proofList
  286. trie := s.StorageTrie(a)
  287. if trie == nil {
  288. return proof, errors.New("storage trie for requested address does not exist")
  289. }
  290. err := trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof)
  291. return [][]byte(proof), err
  292. }
  293. // GetCommittedState retrieves a value from the given account's committed storage trie.
  294. func (s *StateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash {
  295. stateObject := s.getStateObject(addr)
  296. if stateObject != nil {
  297. return stateObject.GetCommittedState(s.db, hash)
  298. }
  299. return common.Hash{}
  300. }
  301. // Database retrieves the low level database supporting the lower level trie ops.
  302. func (s *StateDB) Database() Database {
  303. return s.db
  304. }
  305. // StorageTrie returns the storage trie of an account.
  306. // The return value is a copy and is nil for non-existent accounts.
  307. func (s *StateDB) StorageTrie(addr common.Address) Trie {
  308. stateObject := s.getStateObject(addr)
  309. if stateObject == nil {
  310. return nil
  311. }
  312. cpy := stateObject.deepCopy(s)
  313. cpy.updateTrie(s.db)
  314. return cpy.getTrie(s.db)
  315. }
  316. func (s *StateDB) HasSuicided(addr common.Address) bool {
  317. stateObject := s.getStateObject(addr)
  318. if stateObject != nil {
  319. return stateObject.suicided
  320. }
  321. return false
  322. }
  323. /*
  324. * SETTERS
  325. */
  326. // AddBalance adds amount to the account associated with addr.
  327. func (s *StateDB) AddBalance(addr common.Address, amount *big.Int) {
  328. stateObject := s.GetOrNewStateObject(addr)
  329. if stateObject != nil {
  330. stateObject.AddBalance(amount)
  331. }
  332. }
  333. // SubBalance subtracts amount from the account associated with addr.
  334. func (s *StateDB) SubBalance(addr common.Address, amount *big.Int) {
  335. stateObject := s.GetOrNewStateObject(addr)
  336. if stateObject != nil {
  337. stateObject.SubBalance(amount)
  338. }
  339. }
  340. func (s *StateDB) SetBalance(addr common.Address, amount *big.Int) {
  341. stateObject := s.GetOrNewStateObject(addr)
  342. if stateObject != nil {
  343. stateObject.SetBalance(amount)
  344. }
  345. }
  346. func (s *StateDB) SetNonce(addr common.Address, nonce uint64) {
  347. stateObject := s.GetOrNewStateObject(addr)
  348. if stateObject != nil {
  349. stateObject.SetNonce(nonce)
  350. }
  351. }
  352. func (s *StateDB) SetCode(addr common.Address, code []byte) {
  353. stateObject := s.GetOrNewStateObject(addr)
  354. if stateObject != nil {
  355. stateObject.SetCode(crypto.Keccak256Hash(code), code)
  356. }
  357. }
  358. func (s *StateDB) SetState(addr common.Address, key, value common.Hash) {
  359. stateObject := s.GetOrNewStateObject(addr)
  360. if stateObject != nil {
  361. stateObject.SetState(s.db, key, value)
  362. }
  363. }
  364. // SetStorage replaces the entire storage for the specified account with given
  365. // storage. This function should only be used for debugging.
  366. func (s *StateDB) SetStorage(addr common.Address, storage map[common.Hash]common.Hash) {
  367. stateObject := s.GetOrNewStateObject(addr)
  368. if stateObject != nil {
  369. stateObject.SetStorage(storage)
  370. }
  371. }
  372. // Suicide marks the given account as suicided.
  373. // This clears the account balance.
  374. //
  375. // The account's state object is still available until the state is committed,
  376. // getStateObject will return a non-nil account after Suicide.
  377. func (s *StateDB) Suicide(addr common.Address) bool {
  378. stateObject := s.getStateObject(addr)
  379. if stateObject == nil {
  380. return false
  381. }
  382. s.journal.append(suicideChange{
  383. account: &addr,
  384. prev: stateObject.suicided,
  385. prevbalance: new(big.Int).Set(stateObject.Balance()),
  386. })
  387. stateObject.markSuicided()
  388. stateObject.data.Balance = new(big.Int)
  389. return true
  390. }
  391. //
  392. // Setting, updating & deleting state object methods.
  393. //
  394. // updateStateObject writes the given object to the trie.
  395. func (s *StateDB) updateStateObject(obj *stateObject) {
  396. // Track the amount of time wasted on updating the account from the trie
  397. if metrics.EnabledExpensive {
  398. defer func(start time.Time) { s.AccountUpdates += time.Since(start) }(time.Now())
  399. }
  400. // Encode the account and update the account trie
  401. addr := obj.Address()
  402. data, err := rlp.EncodeToBytes(obj)
  403. if err != nil {
  404. panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err))
  405. }
  406. s.setError(s.trie.TryUpdate(addr[:], data))
  407. // If state snapshotting is active, cache the data til commit
  408. if s.snap != nil {
  409. s.snapAccounts[obj.addrHash] = snapshot.AccountRLP(obj.data.Nonce, obj.data.Balance, obj.data.Root, obj.data.CodeHash)
  410. }
  411. }
  412. // deleteStateObject removes the given object from the state trie.
  413. func (s *StateDB) deleteStateObject(obj *stateObject) {
  414. // Track the amount of time wasted on deleting the account from the trie
  415. if metrics.EnabledExpensive {
  416. defer func(start time.Time) { s.AccountUpdates += time.Since(start) }(time.Now())
  417. }
  418. // Delete the account from the trie
  419. addr := obj.Address()
  420. s.setError(s.trie.TryDelete(addr[:]))
  421. // If state snapshotting is active, cache the data til commit
  422. if s.snap != nil {
  423. s.snapLock.Lock()
  424. s.snapAccounts[obj.addrHash] = nil // We need to maintain account deletions explicitly
  425. s.snapStorage[obj.addrHash] = nil // We need to maintain storage deletions explicitly
  426. s.snapLock.Unlock()
  427. }
  428. }
  429. // getStateObject retrieves a state object given by the address, returning nil if
  430. // the object is not found or was deleted in this execution context. If you need
  431. // to differentiate between non-existent/just-deleted, use getDeletedStateObject.
  432. func (s *StateDB) getStateObject(addr common.Address) *stateObject {
  433. if obj := s.getDeletedStateObject(addr); obj != nil && !obj.deleted {
  434. return obj
  435. }
  436. return nil
  437. }
  438. // getDeletedStateObject is similar to getStateObject, but instead of returning
  439. // nil for a deleted state object, it returns the actual object with the deleted
  440. // flag set. This is needed by the state journal to revert to the correct s-
  441. // destructed object instead of wiping all knowledge about the state object.
  442. func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject {
  443. // Prefer live objects if any is available
  444. if obj := s.stateObjects[addr]; obj != nil {
  445. return obj
  446. }
  447. // If no live objects are available, attempt to use snapshots
  448. var (
  449. data Account
  450. err error
  451. )
  452. if s.snap != nil {
  453. if metrics.EnabledExpensive {
  454. defer func(start time.Time) { s.SnapshotAccountReads += time.Since(start) }(time.Now())
  455. }
  456. var acc *snapshot.Account
  457. if acc, err = s.snap.Account(crypto.Keccak256Hash(addr[:])); err == nil {
  458. if acc == nil {
  459. return nil
  460. }
  461. data.Nonce, data.Balance, data.CodeHash = acc.Nonce, acc.Balance, acc.CodeHash
  462. if len(data.CodeHash) == 0 {
  463. data.CodeHash = emptyCodeHash
  464. }
  465. data.Root = common.BytesToHash(acc.Root)
  466. if data.Root == (common.Hash{}) {
  467. data.Root = emptyRoot
  468. }
  469. }
  470. }
  471. // If snapshot unavailable or reading from it failed, load from the database
  472. if s.snap == nil || err != nil {
  473. if metrics.EnabledExpensive {
  474. defer func(start time.Time) { s.AccountReads += time.Since(start) }(time.Now())
  475. }
  476. enc, err := s.trie.TryGet(addr[:])
  477. if len(enc) == 0 {
  478. s.setError(err)
  479. return nil
  480. }
  481. if err := rlp.DecodeBytes(enc, &data); err != nil {
  482. log.Error("Failed to decode state object", "addr", addr, "err", err)
  483. return nil
  484. }
  485. }
  486. // Insert into the live set
  487. obj := newObject(s, addr, data)
  488. s.setStateObject(obj)
  489. return obj
  490. }
  491. func (s *StateDB) setStateObject(object *stateObject) {
  492. s.stateObjects[object.Address()] = object
  493. }
  494. // Retrieve a state object or create a new state object if nil.
  495. func (s *StateDB) GetOrNewStateObject(addr common.Address) *stateObject {
  496. stateObject := s.getStateObject(addr)
  497. if stateObject == nil {
  498. stateObject, _ = s.createObject(addr)
  499. }
  500. return stateObject
  501. }
  502. // createObject creates a new state object. If there is an existing account with
  503. // the given address, it is overwritten and returned as the second return value.
  504. func (s *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) {
  505. prev = s.getDeletedStateObject(addr) // Note, prev might have been deleted, we need that!
  506. newobj = newObject(s, addr, Account{})
  507. newobj.setNonce(0) // sets the object to dirty
  508. if prev == nil {
  509. s.journal.append(createObjectChange{account: &addr})
  510. } else {
  511. s.journal.append(resetObjectChange{prev: prev})
  512. }
  513. s.setStateObject(newobj)
  514. return newobj, prev
  515. }
  516. // CreateAccount explicitly creates a state object. If a state object with the address
  517. // already exists the balance is carried over to the new account.
  518. //
  519. // CreateAccount is called during the EVM CREATE operation. The situation might arise that
  520. // a contract does the following:
  521. //
  522. // 1. sends funds to sha(account ++ (nonce + 1))
  523. // 2. tx_create(sha(account ++ nonce)) (note that this gets the address of 1)
  524. //
  525. // Carrying over the balance ensures that Ether doesn't disappear.
  526. func (s *StateDB) CreateAccount(addr common.Address) {
  527. newObj, prev := s.createObject(addr)
  528. if prev != nil {
  529. newObj.setBalance(prev.data.Balance)
  530. }
  531. }
  532. func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) error {
  533. so := db.getStateObject(addr)
  534. if so == nil {
  535. return nil
  536. }
  537. it := trie.NewIterator(so.getTrie(db.db).NodeIterator(nil))
  538. for it.Next() {
  539. key := common.BytesToHash(db.trie.GetKey(it.Key))
  540. if value, dirty := so.dirtyStorage[key]; dirty {
  541. if !cb(key, value) {
  542. return nil
  543. }
  544. continue
  545. }
  546. if len(it.Value) > 0 {
  547. _, content, _, err := rlp.Split(it.Value)
  548. if err != nil {
  549. return err
  550. }
  551. if !cb(key, common.BytesToHash(content)) {
  552. return nil
  553. }
  554. }
  555. }
  556. return nil
  557. }
  558. // Copy creates a deep, independent copy of the state.
  559. // Snapshots of the copied state cannot be applied to the copy.
  560. func (s *StateDB) Copy() *StateDB {
  561. // Copy all the basic fields, initialize the memory ones
  562. state := &StateDB{
  563. db: s.db,
  564. trie: s.db.CopyTrie(s.trie),
  565. stateObjects: make(map[common.Address]*stateObject, len(s.journal.dirties)),
  566. stateObjectsPending: make(map[common.Address]struct{}, len(s.stateObjectsPending)),
  567. stateObjectsDirty: make(map[common.Address]struct{}, len(s.journal.dirties)),
  568. refund: s.refund,
  569. logs: make(map[common.Hash][]*types.Log, len(s.logs)),
  570. logSize: s.logSize,
  571. preimages: make(map[common.Hash][]byte, len(s.preimages)),
  572. journal: newJournal(),
  573. }
  574. // Copy the dirty states, logs, and preimages
  575. for addr := range s.journal.dirties {
  576. // As documented [here](https://github.com/ethereum/go-ethereum/pull/16485#issuecomment-380438527),
  577. // and in the Finalise-method, there is a case where an object is in the journal but not
  578. // in the stateObjects: OOG after touch on ripeMD prior to Byzantium. Thus, we need to check for
  579. // nil
  580. if object, exist := s.stateObjects[addr]; exist {
  581. // Even though the original object is dirty, we are not copying the journal,
  582. // so we need to make sure that anyside effect the journal would have caused
  583. // during a commit (or similar op) is already applied to the copy.
  584. state.stateObjects[addr] = object.deepCopy(state)
  585. state.stateObjectsDirty[addr] = struct{}{} // Mark the copy dirty to force internal (code/state) commits
  586. state.stateObjectsPending[addr] = struct{}{} // Mark the copy pending to force external (account) commits
  587. }
  588. }
  589. // Above, we don't copy the actual journal. This means that if the copy is copied, the
  590. // loop above will be a no-op, since the copy's journal is empty.
  591. // Thus, here we iterate over stateObjects, to enable copies of copies
  592. for addr := range s.stateObjectsPending {
  593. if _, exist := state.stateObjects[addr]; !exist {
  594. state.stateObjects[addr] = s.stateObjects[addr].deepCopy(state)
  595. }
  596. state.stateObjectsPending[addr] = struct{}{}
  597. }
  598. for addr := range s.stateObjectsDirty {
  599. if _, exist := state.stateObjects[addr]; !exist {
  600. state.stateObjects[addr] = s.stateObjects[addr].deepCopy(state)
  601. }
  602. state.stateObjectsDirty[addr] = struct{}{}
  603. }
  604. for hash, logs := range s.logs {
  605. cpy := make([]*types.Log, len(logs))
  606. for i, l := range logs {
  607. cpy[i] = new(types.Log)
  608. *cpy[i] = *l
  609. }
  610. state.logs[hash] = cpy
  611. }
  612. for hash, preimage := range s.preimages {
  613. state.preimages[hash] = preimage
  614. }
  615. return state
  616. }
  617. // Snapshot returns an identifier for the current revision of the state.
  618. func (s *StateDB) Snapshot() int {
  619. id := s.nextRevisionId
  620. s.nextRevisionId++
  621. s.validRevisions = append(s.validRevisions, revision{id, s.journal.length()})
  622. return id
  623. }
  624. // RevertToSnapshot reverts all state changes made since the given revision.
  625. func (s *StateDB) RevertToSnapshot(revid int) {
  626. // Find the snapshot in the stack of valid snapshots.
  627. idx := sort.Search(len(s.validRevisions), func(i int) bool {
  628. return s.validRevisions[i].id >= revid
  629. })
  630. if idx == len(s.validRevisions) || s.validRevisions[idx].id != revid {
  631. panic(fmt.Errorf("revision id %v cannot be reverted", revid))
  632. }
  633. snapshot := s.validRevisions[idx].journalIndex
  634. // Replay the journal to undo changes and remove invalidated snapshots
  635. s.journal.revert(s, snapshot)
  636. s.validRevisions = s.validRevisions[:idx]
  637. }
  638. // GetRefund returns the current value of the refund counter.
  639. func (s *StateDB) GetRefund() uint64 {
  640. return s.refund
  641. }
  642. // Finalise finalises the state by removing the s destructed objects and clears
  643. // the journal as well as the refunds. Finalise, however, will not push any updates
  644. // into the tries just yet. Only IntermediateRoot or Commit will do that.
  645. func (s *StateDB) Finalise(deleteEmptyObjects bool) {
  646. for addr := range s.journal.dirties {
  647. obj, exist := s.stateObjects[addr]
  648. if !exist {
  649. // ripeMD is 'touched' at block 1714175, in tx 0x1237f737031e40bcde4a8b7e717b2d15e3ecadfe49bb1bbc71ee9deb09c6fcf2
  650. // That tx goes out of gas, and although the notion of 'touched' does not exist there, the
  651. // touch-event will still be recorded in the journal. Since ripeMD is a special snowflake,
  652. // it will persist in the journal even though the journal is reverted. In this special circumstance,
  653. // it may exist in `s.journal.dirties` but not in `s.stateObjects`.
  654. // Thus, we can safely ignore it here
  655. continue
  656. }
  657. if obj.suicided || (deleteEmptyObjects && obj.empty()) {
  658. obj.deleted = true
  659. } else {
  660. obj.finalise()
  661. }
  662. s.stateObjectsPending[addr] = struct{}{}
  663. s.stateObjectsDirty[addr] = struct{}{}
  664. }
  665. // Invalidate journal because reverting across transactions is not allowed.
  666. s.clearJournalAndRefund()
  667. }
  668. // IntermediateRoot computes the current root hash of the state trie.
  669. // It is called in between transactions to get the root hash that
  670. // goes into transaction receipts.
  671. func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
  672. // Finalise all the dirty storage states and write them into the tries
  673. s.Finalise(deleteEmptyObjects)
  674. for addr := range s.stateObjectsPending {
  675. obj := s.stateObjects[addr]
  676. if obj.deleted {
  677. s.deleteStateObject(obj)
  678. } else {
  679. obj.updateRoot(s.db)
  680. s.updateStateObject(obj)
  681. }
  682. }
  683. if len(s.stateObjectsPending) > 0 {
  684. s.stateObjectsPending = make(map[common.Address]struct{})
  685. }
  686. // Track the amount of time wasted on hashing the account trie
  687. if metrics.EnabledExpensive {
  688. defer func(start time.Time) { s.AccountHashes += time.Since(start) }(time.Now())
  689. }
  690. return s.trie.Hash()
  691. }
  692. // Prepare sets the current transaction hash and index and block hash which is
  693. // used when the EVM emits new state logs.
  694. func (s *StateDB) Prepare(thash, bhash common.Hash, ti int) {
  695. s.thash = thash
  696. s.bhash = bhash
  697. s.txIndex = ti
  698. }
  699. func (s *StateDB) clearJournalAndRefund() {
  700. if len(s.journal.entries) > 0 {
  701. s.journal = newJournal()
  702. s.refund = 0
  703. }
  704. s.validRevisions = s.validRevisions[:0] // Snapshots can be created without journal entires
  705. }
  706. // Commit writes the state to the underlying in-memory trie database.
  707. func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) {
  708. // Finalize any pending changes and merge everything into the tries
  709. s.IntermediateRoot(deleteEmptyObjects)
  710. // Commit objects to the trie, measuring the elapsed time
  711. for addr := range s.stateObjectsDirty {
  712. if obj := s.stateObjects[addr]; !obj.deleted {
  713. // Write any contract code associated with the state object
  714. if obj.code != nil && obj.dirtyCode {
  715. s.db.TrieDB().InsertBlob(common.BytesToHash(obj.CodeHash()), obj.code)
  716. obj.dirtyCode = false
  717. }
  718. // Write any storage changes in the state object to its storage trie
  719. if err := obj.CommitTrie(s.db); err != nil {
  720. return common.Hash{}, err
  721. }
  722. }
  723. }
  724. if len(s.stateObjectsDirty) > 0 {
  725. s.stateObjectsDirty = make(map[common.Address]struct{})
  726. }
  727. // Write the account trie changes, measuing the amount of wasted time
  728. var start time.Time
  729. if metrics.EnabledExpensive {
  730. start = time.Now()
  731. }
  732. // The onleaf func is called _serially_, so we can reuse the same account
  733. // for unmarshalling every time.
  734. var account Account
  735. root, err := s.trie.Commit(func(leaf []byte, parent common.Hash) error {
  736. if err := rlp.DecodeBytes(leaf, &account); err != nil {
  737. return nil
  738. }
  739. if account.Root != emptyRoot {
  740. s.db.TrieDB().Reference(account.Root, parent)
  741. }
  742. code := common.BytesToHash(account.CodeHash)
  743. if code != emptyCode {
  744. s.db.TrieDB().Reference(code, parent)
  745. }
  746. return nil
  747. })
  748. if metrics.EnabledExpensive {
  749. s.AccountCommits += time.Since(start)
  750. }
  751. // If snapshotting is enabled, update the snapshot tree with this new version
  752. if s.snap != nil {
  753. if metrics.EnabledExpensive {
  754. defer func(start time.Time) { s.SnapshotCommits += time.Since(start) }(time.Now())
  755. }
  756. // Only update if there's a state transition (skip empty Clique blocks)
  757. if parent := s.snap.Root(); parent != root {
  758. if err := s.snaps.Update(root, parent, s.snapAccounts, s.snapStorage); err != nil {
  759. log.Warn("Failed to update snapshot tree", "from", parent, "to", root, "err", err)
  760. }
  761. if err := s.snaps.Cap(root, 128); err != nil {
  762. log.Warn("Failed to cap snapshot tree", "root", root, "layers", 128, "err", err)
  763. }
  764. }
  765. s.snap, s.snapAccounts, s.snapStorage = nil, nil, nil
  766. }
  767. return root, err
  768. }