statedb.go 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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. "time"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/core/rawdb"
  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. )
  42. type proofList [][]byte
  43. func (n *proofList) Put(key []byte, value []byte) error {
  44. *n = append(*n, value)
  45. return nil
  46. }
  47. func (n *proofList) Delete(key []byte) error {
  48. panic("not supported")
  49. }
  50. // StateDB structs within the ethereum protocol are used to store anything
  51. // within the merkle trie. StateDBs take care of caching and storing
  52. // nested states. It's the general query interface to retrieve:
  53. // * Contracts
  54. // * Accounts
  55. type StateDB struct {
  56. db Database
  57. prefetcher *triePrefetcher
  58. trie Trie
  59. hasher crypto.KeccakState
  60. // originalRoot is the pre-state root, before any changes were made.
  61. // It will be updated when the Commit is called.
  62. originalRoot common.Hash
  63. snaps *snapshot.Tree
  64. snap snapshot.Snapshot
  65. snapDestructs map[common.Hash]struct{}
  66. snapAccounts map[common.Hash][]byte
  67. snapStorage map[common.Hash]map[common.Hash][]byte
  68. // This map holds 'live' objects, which will get modified while processing a state transition.
  69. stateObjects map[common.Address]*stateObject
  70. stateObjectsPending map[common.Address]struct{} // State objects finalized but not yet written to the trie
  71. stateObjectsDirty map[common.Address]struct{} // State objects modified in the current execution
  72. // DB error.
  73. // State objects are used by the consensus core and VM which are
  74. // unable to deal with database-level errors. Any error that occurs
  75. // during a database read is memoized here and will eventually be returned
  76. // by StateDB.Commit.
  77. dbErr error
  78. // The refund counter, also used by state transitioning.
  79. refund uint64
  80. thash common.Hash
  81. txIndex int
  82. logs map[common.Hash][]*types.Log
  83. logSize uint
  84. preimages map[common.Hash][]byte
  85. // Per-transaction access list
  86. accessList *accessList
  87. // Journal of state modifications. This is the backbone of
  88. // Snapshot and RevertToSnapshot.
  89. journal *journal
  90. validRevisions []revision
  91. nextRevisionId int
  92. // Measurements gathered during execution for debugging purposes
  93. AccountReads time.Duration
  94. AccountHashes time.Duration
  95. AccountUpdates time.Duration
  96. AccountCommits time.Duration
  97. StorageReads time.Duration
  98. StorageHashes time.Duration
  99. StorageUpdates time.Duration
  100. StorageCommits time.Duration
  101. SnapshotAccountReads time.Duration
  102. SnapshotStorageReads time.Duration
  103. SnapshotCommits time.Duration
  104. AccountUpdated int
  105. StorageUpdated int
  106. AccountDeleted int
  107. StorageDeleted int
  108. }
  109. // New creates a new state from a given trie.
  110. func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) {
  111. tr, err := db.OpenTrie(root)
  112. if err != nil {
  113. return nil, err
  114. }
  115. sdb := &StateDB{
  116. db: db,
  117. trie: tr,
  118. originalRoot: root,
  119. snaps: snaps,
  120. stateObjects: make(map[common.Address]*stateObject),
  121. stateObjectsPending: make(map[common.Address]struct{}),
  122. stateObjectsDirty: make(map[common.Address]struct{}),
  123. logs: make(map[common.Hash][]*types.Log),
  124. preimages: make(map[common.Hash][]byte),
  125. journal: newJournal(),
  126. accessList: newAccessList(),
  127. hasher: crypto.NewKeccakState(),
  128. }
  129. if sdb.snaps != nil {
  130. if sdb.snap = sdb.snaps.Snapshot(root); sdb.snap != nil {
  131. sdb.snapDestructs = make(map[common.Hash]struct{})
  132. sdb.snapAccounts = make(map[common.Hash][]byte)
  133. sdb.snapStorage = make(map[common.Hash]map[common.Hash][]byte)
  134. }
  135. }
  136. return sdb, nil
  137. }
  138. // StartPrefetcher initializes a new trie prefetcher to pull in nodes from the
  139. // state trie concurrently while the state is mutated so that when we reach the
  140. // commit phase, most of the needed data is already hot.
  141. func (s *StateDB) StartPrefetcher(namespace string) {
  142. if s.prefetcher != nil {
  143. s.prefetcher.close()
  144. s.prefetcher = nil
  145. }
  146. if s.snap != nil {
  147. s.prefetcher = newTriePrefetcher(s.db, s.originalRoot, namespace)
  148. }
  149. }
  150. // StopPrefetcher terminates a running prefetcher and reports any leftover stats
  151. // from the gathered metrics.
  152. func (s *StateDB) StopPrefetcher() {
  153. if s.prefetcher != nil {
  154. s.prefetcher.close()
  155. s.prefetcher = nil
  156. }
  157. }
  158. // setError remembers the first non-nil error it is called with.
  159. func (s *StateDB) setError(err error) {
  160. if s.dbErr == nil {
  161. s.dbErr = err
  162. }
  163. }
  164. func (s *StateDB) Error() error {
  165. return s.dbErr
  166. }
  167. func (s *StateDB) AddLog(log *types.Log) {
  168. s.journal.append(addLogChange{txhash: s.thash})
  169. log.TxHash = s.thash
  170. log.TxIndex = uint(s.txIndex)
  171. log.Index = s.logSize
  172. s.logs[s.thash] = append(s.logs[s.thash], log)
  173. s.logSize++
  174. }
  175. func (s *StateDB) GetLogs(hash common.Hash, blockHash common.Hash) []*types.Log {
  176. logs := s.logs[hash]
  177. for _, l := range logs {
  178. l.BlockHash = blockHash
  179. }
  180. return logs
  181. }
  182. func (s *StateDB) Logs() []*types.Log {
  183. var logs []*types.Log
  184. for _, lgs := range s.logs {
  185. logs = append(logs, lgs...)
  186. }
  187. return logs
  188. }
  189. // AddPreimage records a SHA3 preimage seen by the VM.
  190. func (s *StateDB) AddPreimage(hash common.Hash, preimage []byte) {
  191. if _, ok := s.preimages[hash]; !ok {
  192. s.journal.append(addPreimageChange{hash: hash})
  193. pi := make([]byte, len(preimage))
  194. copy(pi, preimage)
  195. s.preimages[hash] = pi
  196. }
  197. }
  198. // Preimages returns a list of SHA3 preimages that have been submitted.
  199. func (s *StateDB) Preimages() map[common.Hash][]byte {
  200. return s.preimages
  201. }
  202. // AddRefund adds gas to the refund counter
  203. func (s *StateDB) AddRefund(gas uint64) {
  204. s.journal.append(refundChange{prev: s.refund})
  205. s.refund += gas
  206. }
  207. // SubRefund removes gas from the refund counter.
  208. // This method will panic if the refund counter goes below zero
  209. func (s *StateDB) SubRefund(gas uint64) {
  210. s.journal.append(refundChange{prev: s.refund})
  211. if gas > s.refund {
  212. panic(fmt.Sprintf("Refund counter below zero (gas: %d > refund: %d)", gas, s.refund))
  213. }
  214. s.refund -= gas
  215. }
  216. // Exist reports whether the given account address exists in the state.
  217. // Notably this also returns true for suicided accounts.
  218. func (s *StateDB) Exist(addr common.Address) bool {
  219. return s.getStateObject(addr) != nil
  220. }
  221. // Empty returns whether the state object is either non-existent
  222. // or empty according to the EIP161 specification (balance = nonce = code = 0)
  223. func (s *StateDB) Empty(addr common.Address) bool {
  224. so := s.getStateObject(addr)
  225. return so == nil || so.empty()
  226. }
  227. // GetBalance retrieves the balance from the given address or 0 if object not found
  228. func (s *StateDB) GetBalance(addr common.Address) *big.Int {
  229. stateObject := s.getStateObject(addr)
  230. if stateObject != nil {
  231. return stateObject.Balance()
  232. }
  233. return common.Big0
  234. }
  235. func (s *StateDB) GetNonce(addr common.Address) uint64 {
  236. stateObject := s.getStateObject(addr)
  237. if stateObject != nil {
  238. return stateObject.Nonce()
  239. }
  240. return 0
  241. }
  242. // TxIndex returns the current transaction index set by Prepare.
  243. func (s *StateDB) TxIndex() int {
  244. return s.txIndex
  245. }
  246. func (s *StateDB) GetCode(addr common.Address) []byte {
  247. stateObject := s.getStateObject(addr)
  248. if stateObject != nil {
  249. return stateObject.Code(s.db)
  250. }
  251. return nil
  252. }
  253. func (s *StateDB) GetCodeSize(addr common.Address) int {
  254. stateObject := s.getStateObject(addr)
  255. if stateObject != nil {
  256. return stateObject.CodeSize(s.db)
  257. }
  258. return 0
  259. }
  260. func (s *StateDB) GetCodeHash(addr common.Address) common.Hash {
  261. stateObject := s.getStateObject(addr)
  262. if stateObject == nil {
  263. return common.Hash{}
  264. }
  265. return common.BytesToHash(stateObject.CodeHash())
  266. }
  267. // GetState retrieves a value from the given account's storage trie.
  268. func (s *StateDB) GetState(addr common.Address, hash common.Hash) common.Hash {
  269. stateObject := s.getStateObject(addr)
  270. if stateObject != nil {
  271. return stateObject.GetState(s.db, hash)
  272. }
  273. return common.Hash{}
  274. }
  275. // GetProof returns the Merkle proof for a given account.
  276. func (s *StateDB) GetProof(addr common.Address) ([][]byte, error) {
  277. return s.GetProofByHash(crypto.Keccak256Hash(addr.Bytes()))
  278. }
  279. // GetProofByHash returns the Merkle proof for a given account.
  280. func (s *StateDB) GetProofByHash(addrHash common.Hash) ([][]byte, error) {
  281. var proof proofList
  282. err := s.trie.Prove(addrHash[:], 0, &proof)
  283. return proof, err
  284. }
  285. // GetStorageProof returns the Merkle proof for given storage slot.
  286. func (s *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byte, error) {
  287. var proof proofList
  288. trie := s.StorageTrie(a)
  289. if trie == nil {
  290. return proof, errors.New("storage trie for requested address does not exist")
  291. }
  292. err := trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof)
  293. return proof, err
  294. }
  295. // GetCommittedState retrieves a value from the given account's committed storage trie.
  296. func (s *StateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash {
  297. stateObject := s.getStateObject(addr)
  298. if stateObject != nil {
  299. return stateObject.GetCommittedState(s.db, hash)
  300. }
  301. return common.Hash{}
  302. }
  303. // Database retrieves the low level database supporting the lower level trie ops.
  304. func (s *StateDB) Database() Database {
  305. return s.db
  306. }
  307. // StorageTrie returns the storage trie of an account.
  308. // The return value is a copy and is nil for non-existent accounts.
  309. func (s *StateDB) StorageTrie(addr common.Address) Trie {
  310. stateObject := s.getStateObject(addr)
  311. if stateObject == nil {
  312. return nil
  313. }
  314. cpy := stateObject.deepCopy(s)
  315. cpy.updateTrie(s.db)
  316. return cpy.getTrie(s.db)
  317. }
  318. func (s *StateDB) HasSuicided(addr common.Address) bool {
  319. stateObject := s.getStateObject(addr)
  320. if stateObject != nil {
  321. return stateObject.suicided
  322. }
  323. return false
  324. }
  325. /*
  326. * SETTERS
  327. */
  328. // AddBalance adds amount to the account associated with addr.
  329. func (s *StateDB) AddBalance(addr common.Address, amount *big.Int) {
  330. stateObject := s.GetOrNewStateObject(addr)
  331. if stateObject != nil {
  332. stateObject.AddBalance(amount)
  333. }
  334. }
  335. // SubBalance subtracts amount from the account associated with addr.
  336. func (s *StateDB) SubBalance(addr common.Address, amount *big.Int) {
  337. stateObject := s.GetOrNewStateObject(addr)
  338. if stateObject != nil {
  339. stateObject.SubBalance(amount)
  340. }
  341. }
  342. func (s *StateDB) SetBalance(addr common.Address, amount *big.Int) {
  343. stateObject := s.GetOrNewStateObject(addr)
  344. if stateObject != nil {
  345. stateObject.SetBalance(amount)
  346. }
  347. }
  348. func (s *StateDB) SetNonce(addr common.Address, nonce uint64) {
  349. stateObject := s.GetOrNewStateObject(addr)
  350. if stateObject != nil {
  351. stateObject.SetNonce(nonce)
  352. }
  353. }
  354. func (s *StateDB) SetCode(addr common.Address, code []byte) {
  355. stateObject := s.GetOrNewStateObject(addr)
  356. if stateObject != nil {
  357. stateObject.SetCode(crypto.Keccak256Hash(code), code)
  358. }
  359. }
  360. func (s *StateDB) SetState(addr common.Address, key, value common.Hash) {
  361. stateObject := s.GetOrNewStateObject(addr)
  362. if stateObject != nil {
  363. stateObject.SetState(s.db, key, value)
  364. }
  365. }
  366. // SetStorage replaces the entire storage for the specified account with given
  367. // storage. This function should only be used for debugging.
  368. func (s *StateDB) SetStorage(addr common.Address, storage map[common.Hash]common.Hash) {
  369. stateObject := s.GetOrNewStateObject(addr)
  370. if stateObject != nil {
  371. stateObject.SetStorage(storage)
  372. }
  373. }
  374. // Suicide marks the given account as suicided.
  375. // This clears the account balance.
  376. //
  377. // The account's state object is still available until the state is committed,
  378. // getStateObject will return a non-nil account after Suicide.
  379. func (s *StateDB) Suicide(addr common.Address) bool {
  380. stateObject := s.getStateObject(addr)
  381. if stateObject == nil {
  382. return false
  383. }
  384. s.journal.append(suicideChange{
  385. account: &addr,
  386. prev: stateObject.suicided,
  387. prevbalance: new(big.Int).Set(stateObject.Balance()),
  388. })
  389. stateObject.markSuicided()
  390. stateObject.data.Balance = new(big.Int)
  391. return true
  392. }
  393. //
  394. // Setting, updating & deleting state object methods.
  395. //
  396. // updateStateObject writes the given object to the trie.
  397. func (s *StateDB) updateStateObject(obj *stateObject) {
  398. // Track the amount of time wasted on updating the account from the trie
  399. if metrics.EnabledExpensive {
  400. defer func(start time.Time) { s.AccountUpdates += time.Since(start) }(time.Now())
  401. }
  402. // Encode the account and update the account trie
  403. addr := obj.Address()
  404. if err := s.trie.TryUpdateAccount(addr[:], &obj.data); err != nil {
  405. s.setError(fmt.Errorf("updateStateObject (%x) error: %v", addr[:], err))
  406. }
  407. // If state snapshotting is active, cache the data til commit. Note, this
  408. // update mechanism is not symmetric to the deletion, because whereas it is
  409. // enough to track account updates at commit time, deletions need tracking
  410. // at transaction boundary level to ensure we capture state clearing.
  411. if s.snap != nil {
  412. s.snapAccounts[obj.addrHash] = snapshot.SlimAccountRLP(obj.data.Nonce, obj.data.Balance, obj.data.Root, obj.data.CodeHash)
  413. }
  414. }
  415. // deleteStateObject removes the given object from the state trie.
  416. func (s *StateDB) deleteStateObject(obj *stateObject) {
  417. // Track the amount of time wasted on deleting the account from the trie
  418. if metrics.EnabledExpensive {
  419. defer func(start time.Time) { s.AccountUpdates += time.Since(start) }(time.Now())
  420. }
  421. // Delete the account from the trie
  422. addr := obj.Address()
  423. if err := s.trie.TryDeleteAccount(addr[:]); err != nil {
  424. s.setError(fmt.Errorf("deleteStateObject (%x) error: %v", addr[:], err))
  425. }
  426. }
  427. // getStateObject retrieves a state object given by the address, returning nil if
  428. // the object is not found or was deleted in this execution context. If you need
  429. // to differentiate between non-existent/just-deleted, use getDeletedStateObject.
  430. func (s *StateDB) getStateObject(addr common.Address) *stateObject {
  431. if obj := s.getDeletedStateObject(addr); obj != nil && !obj.deleted {
  432. return obj
  433. }
  434. return nil
  435. }
  436. // getDeletedStateObject is similar to getStateObject, but instead of returning
  437. // nil for a deleted state object, it returns the actual object with the deleted
  438. // flag set. This is needed by the state journal to revert to the correct s-
  439. // destructed object instead of wiping all knowledge about the state object.
  440. func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject {
  441. // Prefer live objects if any is available
  442. if obj := s.stateObjects[addr]; obj != nil {
  443. return obj
  444. }
  445. // If no live objects are available, attempt to use snapshots
  446. var data *types.StateAccount
  447. if s.snap != nil {
  448. start := time.Now()
  449. acc, err := s.snap.Account(crypto.HashData(s.hasher, addr.Bytes()))
  450. if metrics.EnabledExpensive {
  451. s.SnapshotAccountReads += time.Since(start)
  452. }
  453. if err == nil {
  454. if acc == nil {
  455. return nil
  456. }
  457. data = &types.StateAccount{
  458. Nonce: acc.Nonce,
  459. Balance: acc.Balance,
  460. CodeHash: acc.CodeHash,
  461. Root: common.BytesToHash(acc.Root),
  462. }
  463. if len(data.CodeHash) == 0 {
  464. data.CodeHash = emptyCodeHash
  465. }
  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 data == nil {
  473. start := time.Now()
  474. var err error
  475. data, err = s.trie.TryGetAccount(addr.Bytes())
  476. if metrics.EnabledExpensive {
  477. s.AccountReads += time.Since(start)
  478. }
  479. if err != nil {
  480. s.setError(fmt.Errorf("getDeleteStateObject (%x) error: %w", addr.Bytes(), err))
  481. return nil
  482. }
  483. if data == nil {
  484. return nil
  485. }
  486. }
  487. // Insert into the live set
  488. obj := newObject(s, addr, *data)
  489. s.setStateObject(obj)
  490. return obj
  491. }
  492. func (s *StateDB) setStateObject(object *stateObject) {
  493. s.stateObjects[object.Address()] = object
  494. }
  495. // GetOrNewStateObject retrieves a state object or create a new state object if nil.
  496. func (s *StateDB) GetOrNewStateObject(addr common.Address) *stateObject {
  497. stateObject := s.getStateObject(addr)
  498. if stateObject == nil {
  499. stateObject, _ = s.createObject(addr)
  500. }
  501. return stateObject
  502. }
  503. // createObject creates a new state object. If there is an existing account with
  504. // the given address, it is overwritten and returned as the second return value.
  505. func (s *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) {
  506. prev = s.getDeletedStateObject(addr) // Note, prev might have been deleted, we need that!
  507. var prevdestruct bool
  508. if s.snap != nil && prev != nil {
  509. _, prevdestruct = s.snapDestructs[prev.addrHash]
  510. if !prevdestruct {
  511. s.snapDestructs[prev.addrHash] = struct{}{}
  512. }
  513. }
  514. newobj = newObject(s, addr, types.StateAccount{})
  515. if prev == nil {
  516. s.journal.append(createObjectChange{account: &addr})
  517. } else {
  518. s.journal.append(resetObjectChange{prev: prev, prevdestruct: prevdestruct})
  519. }
  520. s.setStateObject(newobj)
  521. if prev != nil && !prev.deleted {
  522. return newobj, prev
  523. }
  524. return newobj, nil
  525. }
  526. // CreateAccount explicitly creates a state object. If a state object with the address
  527. // already exists the balance is carried over to the new account.
  528. //
  529. // CreateAccount is called during the EVM CREATE operation. The situation might arise that
  530. // a contract does the following:
  531. //
  532. // 1. sends funds to sha(account ++ (nonce + 1))
  533. // 2. tx_create(sha(account ++ nonce)) (note that this gets the address of 1)
  534. //
  535. // Carrying over the balance ensures that Ether doesn't disappear.
  536. func (s *StateDB) CreateAccount(addr common.Address) {
  537. newObj, prev := s.createObject(addr)
  538. if prev != nil {
  539. newObj.setBalance(prev.data.Balance)
  540. }
  541. }
  542. func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) error {
  543. so := db.getStateObject(addr)
  544. if so == nil {
  545. return nil
  546. }
  547. it := trie.NewIterator(so.getTrie(db.db).NodeIterator(nil))
  548. for it.Next() {
  549. key := common.BytesToHash(db.trie.GetKey(it.Key))
  550. if value, dirty := so.dirtyStorage[key]; dirty {
  551. if !cb(key, value) {
  552. return nil
  553. }
  554. continue
  555. }
  556. if len(it.Value) > 0 {
  557. _, content, _, err := rlp.Split(it.Value)
  558. if err != nil {
  559. return err
  560. }
  561. if !cb(key, common.BytesToHash(content)) {
  562. return nil
  563. }
  564. }
  565. }
  566. return nil
  567. }
  568. // Copy creates a deep, independent copy of the state.
  569. // Snapshots of the copied state cannot be applied to the copy.
  570. func (s *StateDB) Copy() *StateDB {
  571. // Copy all the basic fields, initialize the memory ones
  572. state := &StateDB{
  573. db: s.db,
  574. trie: s.db.CopyTrie(s.trie),
  575. originalRoot: s.originalRoot,
  576. stateObjects: make(map[common.Address]*stateObject, len(s.journal.dirties)),
  577. stateObjectsPending: make(map[common.Address]struct{}, len(s.stateObjectsPending)),
  578. stateObjectsDirty: make(map[common.Address]struct{}, len(s.journal.dirties)),
  579. refund: s.refund,
  580. logs: make(map[common.Hash][]*types.Log, len(s.logs)),
  581. logSize: s.logSize,
  582. preimages: make(map[common.Hash][]byte, len(s.preimages)),
  583. journal: newJournal(),
  584. hasher: crypto.NewKeccakState(),
  585. }
  586. // Copy the dirty states, logs, and preimages
  587. for addr := range s.journal.dirties {
  588. // As documented [here](https://github.com/ethereum/go-ethereum/pull/16485#issuecomment-380438527),
  589. // and in the Finalise-method, there is a case where an object is in the journal but not
  590. // in the stateObjects: OOG after touch on ripeMD prior to Byzantium. Thus, we need to check for
  591. // nil
  592. if object, exist := s.stateObjects[addr]; exist {
  593. // Even though the original object is dirty, we are not copying the journal,
  594. // so we need to make sure that anyside effect the journal would have caused
  595. // during a commit (or similar op) is already applied to the copy.
  596. state.stateObjects[addr] = object.deepCopy(state)
  597. state.stateObjectsDirty[addr] = struct{}{} // Mark the copy dirty to force internal (code/state) commits
  598. state.stateObjectsPending[addr] = struct{}{} // Mark the copy pending to force external (account) commits
  599. }
  600. }
  601. // Above, we don't copy the actual journal. This means that if the copy is copied, the
  602. // loop above will be a no-op, since the copy's journal is empty.
  603. // Thus, here we iterate over stateObjects, to enable copies of copies
  604. for addr := range s.stateObjectsPending {
  605. if _, exist := state.stateObjects[addr]; !exist {
  606. state.stateObjects[addr] = s.stateObjects[addr].deepCopy(state)
  607. }
  608. state.stateObjectsPending[addr] = struct{}{}
  609. }
  610. for addr := range s.stateObjectsDirty {
  611. if _, exist := state.stateObjects[addr]; !exist {
  612. state.stateObjects[addr] = s.stateObjects[addr].deepCopy(state)
  613. }
  614. state.stateObjectsDirty[addr] = struct{}{}
  615. }
  616. for hash, logs := range s.logs {
  617. cpy := make([]*types.Log, len(logs))
  618. for i, l := range logs {
  619. cpy[i] = new(types.Log)
  620. *cpy[i] = *l
  621. }
  622. state.logs[hash] = cpy
  623. }
  624. for hash, preimage := range s.preimages {
  625. state.preimages[hash] = preimage
  626. }
  627. // Do we need to copy the access list? In practice: No. At the start of a
  628. // transaction, the access list is empty. In practice, we only ever copy state
  629. // _between_ transactions/blocks, never in the middle of a transaction.
  630. // However, it doesn't cost us much to copy an empty list, so we do it anyway
  631. // to not blow up if we ever decide copy it in the middle of a transaction
  632. state.accessList = s.accessList.Copy()
  633. // If there's a prefetcher running, make an inactive copy of it that can
  634. // only access data but does not actively preload (since the user will not
  635. // know that they need to explicitly terminate an active copy).
  636. if s.prefetcher != nil {
  637. state.prefetcher = s.prefetcher.copy()
  638. }
  639. if s.snaps != nil {
  640. // In order for the miner to be able to use and make additions
  641. // to the snapshot tree, we need to copy that aswell.
  642. // Otherwise, any block mined by ourselves will cause gaps in the tree,
  643. // and force the miner to operate trie-backed only
  644. state.snaps = s.snaps
  645. state.snap = s.snap
  646. // deep copy needed
  647. state.snapDestructs = make(map[common.Hash]struct{})
  648. for k, v := range s.snapDestructs {
  649. state.snapDestructs[k] = v
  650. }
  651. state.snapAccounts = make(map[common.Hash][]byte)
  652. for k, v := range s.snapAccounts {
  653. state.snapAccounts[k] = v
  654. }
  655. state.snapStorage = make(map[common.Hash]map[common.Hash][]byte)
  656. for k, v := range s.snapStorage {
  657. temp := make(map[common.Hash][]byte)
  658. for kk, vv := range v {
  659. temp[kk] = vv
  660. }
  661. state.snapStorage[k] = temp
  662. }
  663. }
  664. return state
  665. }
  666. // Snapshot returns an identifier for the current revision of the state.
  667. func (s *StateDB) Snapshot() int {
  668. id := s.nextRevisionId
  669. s.nextRevisionId++
  670. s.validRevisions = append(s.validRevisions, revision{id, s.journal.length()})
  671. return id
  672. }
  673. // RevertToSnapshot reverts all state changes made since the given revision.
  674. func (s *StateDB) RevertToSnapshot(revid int) {
  675. // Find the snapshot in the stack of valid snapshots.
  676. idx := sort.Search(len(s.validRevisions), func(i int) bool {
  677. return s.validRevisions[i].id >= revid
  678. })
  679. if idx == len(s.validRevisions) || s.validRevisions[idx].id != revid {
  680. panic(fmt.Errorf("revision id %v cannot be reverted", revid))
  681. }
  682. snapshot := s.validRevisions[idx].journalIndex
  683. // Replay the journal to undo changes and remove invalidated snapshots
  684. s.journal.revert(s, snapshot)
  685. s.validRevisions = s.validRevisions[:idx]
  686. }
  687. // GetRefund returns the current value of the refund counter.
  688. func (s *StateDB) GetRefund() uint64 {
  689. return s.refund
  690. }
  691. // Finalise finalises the state by removing the destructed objects and clears
  692. // the journal as well as the refunds. Finalise, however, will not push any updates
  693. // into the tries just yet. Only IntermediateRoot or Commit will do that.
  694. func (s *StateDB) Finalise(deleteEmptyObjects bool) {
  695. addressesToPrefetch := make([][]byte, 0, len(s.journal.dirties))
  696. for addr := range s.journal.dirties {
  697. obj, exist := s.stateObjects[addr]
  698. if !exist {
  699. // ripeMD is 'touched' at block 1714175, in tx 0x1237f737031e40bcde4a8b7e717b2d15e3ecadfe49bb1bbc71ee9deb09c6fcf2
  700. // That tx goes out of gas, and although the notion of 'touched' does not exist there, the
  701. // touch-event will still be recorded in the journal. Since ripeMD is a special snowflake,
  702. // it will persist in the journal even though the journal is reverted. In this special circumstance,
  703. // it may exist in `s.journal.dirties` but not in `s.stateObjects`.
  704. // Thus, we can safely ignore it here
  705. continue
  706. }
  707. if obj.suicided || (deleteEmptyObjects && obj.empty()) {
  708. obj.deleted = true
  709. // If state snapshotting is active, also mark the destruction there.
  710. // Note, we can't do this only at the end of a block because multiple
  711. // transactions within the same block might self destruct and then
  712. // resurrect an account; but the snapshotter needs both events.
  713. if s.snap != nil {
  714. s.snapDestructs[obj.addrHash] = struct{}{} // We need to maintain account deletions explicitly (will remain set indefinitely)
  715. delete(s.snapAccounts, obj.addrHash) // Clear out any previously updated account data (may be recreated via a ressurrect)
  716. delete(s.snapStorage, obj.addrHash) // Clear out any previously updated storage data (may be recreated via a ressurrect)
  717. }
  718. } else {
  719. obj.finalise(true) // Prefetch slots in the background
  720. }
  721. s.stateObjectsPending[addr] = struct{}{}
  722. s.stateObjectsDirty[addr] = struct{}{}
  723. // At this point, also ship the address off to the precacher. The precacher
  724. // will start loading tries, and when the change is eventually committed,
  725. // the commit-phase will be a lot faster
  726. addressesToPrefetch = append(addressesToPrefetch, common.CopyBytes(addr[:])) // Copy needed for closure
  727. }
  728. if s.prefetcher != nil && len(addressesToPrefetch) > 0 {
  729. s.prefetcher.prefetch(common.Hash{}, s.originalRoot, addressesToPrefetch)
  730. }
  731. // Invalidate journal because reverting across transactions is not allowed.
  732. s.clearJournalAndRefund()
  733. }
  734. // IntermediateRoot computes the current root hash of the state trie.
  735. // It is called in between transactions to get the root hash that
  736. // goes into transaction receipts.
  737. func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
  738. // Finalise all the dirty storage states and write them into the tries
  739. s.Finalise(deleteEmptyObjects)
  740. // If there was a trie prefetcher operating, it gets aborted and irrevocably
  741. // modified after we start retrieving tries. Remove it from the statedb after
  742. // this round of use.
  743. //
  744. // This is weird pre-byzantium since the first tx runs with a prefetcher and
  745. // the remainder without, but pre-byzantium even the initial prefetcher is
  746. // useless, so no sleep lost.
  747. prefetcher := s.prefetcher
  748. if s.prefetcher != nil {
  749. defer func() {
  750. s.prefetcher.close()
  751. s.prefetcher = nil
  752. }()
  753. }
  754. // Although naively it makes sense to retrieve the account trie and then do
  755. // the contract storage and account updates sequentially, that short circuits
  756. // the account prefetcher. Instead, let's process all the storage updates
  757. // first, giving the account prefetches just a few more milliseconds of time
  758. // to pull useful data from disk.
  759. for addr := range s.stateObjectsPending {
  760. if obj := s.stateObjects[addr]; !obj.deleted {
  761. obj.updateRoot(s.db)
  762. }
  763. }
  764. // Now we're about to start to write changes to the trie. The trie is so far
  765. // _untouched_. We can check with the prefetcher, if it can give us a trie
  766. // which has the same root, but also has some content loaded into it.
  767. if prefetcher != nil {
  768. if trie := prefetcher.trie(common.Hash{}, s.originalRoot); trie != nil {
  769. s.trie = trie
  770. }
  771. }
  772. usedAddrs := make([][]byte, 0, len(s.stateObjectsPending))
  773. for addr := range s.stateObjectsPending {
  774. if obj := s.stateObjects[addr]; obj.deleted {
  775. s.deleteStateObject(obj)
  776. s.AccountDeleted += 1
  777. } else {
  778. s.updateStateObject(obj)
  779. s.AccountUpdated += 1
  780. }
  781. usedAddrs = append(usedAddrs, common.CopyBytes(addr[:])) // Copy needed for closure
  782. }
  783. if prefetcher != nil {
  784. prefetcher.used(common.Hash{}, s.originalRoot, usedAddrs)
  785. }
  786. if len(s.stateObjectsPending) > 0 {
  787. s.stateObjectsPending = make(map[common.Address]struct{})
  788. }
  789. // Track the amount of time wasted on hashing the account trie
  790. if metrics.EnabledExpensive {
  791. defer func(start time.Time) { s.AccountHashes += time.Since(start) }(time.Now())
  792. }
  793. return s.trie.Hash()
  794. }
  795. // Prepare sets the current transaction hash and index which are
  796. // used when the EVM emits new state logs.
  797. func (s *StateDB) Prepare(thash common.Hash, ti int) {
  798. s.thash = thash
  799. s.txIndex = ti
  800. }
  801. func (s *StateDB) clearJournalAndRefund() {
  802. if len(s.journal.entries) > 0 {
  803. s.journal = newJournal()
  804. s.refund = 0
  805. }
  806. s.validRevisions = s.validRevisions[:0] // Snapshots can be created without journal entries
  807. }
  808. // Commit writes the state to the underlying in-memory trie database.
  809. func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) {
  810. if s.dbErr != nil {
  811. return common.Hash{}, fmt.Errorf("commit aborted due to earlier error: %v", s.dbErr)
  812. }
  813. // Finalize any pending changes and merge everything into the tries
  814. s.IntermediateRoot(deleteEmptyObjects)
  815. // Commit objects to the trie, measuring the elapsed time
  816. var (
  817. accountTrieNodes int
  818. storageTrieNodes int
  819. nodes = trie.NewMergedNodeSet()
  820. )
  821. codeWriter := s.db.TrieDB().DiskDB().NewBatch()
  822. for addr := range s.stateObjectsDirty {
  823. if obj := s.stateObjects[addr]; !obj.deleted {
  824. // Write any contract code associated with the state object
  825. if obj.code != nil && obj.dirtyCode {
  826. rawdb.WriteCode(codeWriter, common.BytesToHash(obj.CodeHash()), obj.code)
  827. obj.dirtyCode = false
  828. }
  829. // Write any storage changes in the state object to its storage trie
  830. set, err := obj.CommitTrie(s.db)
  831. if err != nil {
  832. return common.Hash{}, err
  833. }
  834. // Merge the dirty nodes of storage trie into global set
  835. if set != nil {
  836. if err := nodes.Merge(set); err != nil {
  837. return common.Hash{}, err
  838. }
  839. storageTrieNodes += set.Len()
  840. }
  841. }
  842. }
  843. if len(s.stateObjectsDirty) > 0 {
  844. s.stateObjectsDirty = make(map[common.Address]struct{})
  845. }
  846. if codeWriter.ValueSize() > 0 {
  847. if err := codeWriter.Write(); err != nil {
  848. log.Crit("Failed to commit dirty codes", "error", err)
  849. }
  850. }
  851. // Write the account trie changes, measuring the amount of wasted time
  852. var start time.Time
  853. if metrics.EnabledExpensive {
  854. start = time.Now()
  855. }
  856. root, set, err := s.trie.Commit(true)
  857. if err != nil {
  858. return common.Hash{}, err
  859. }
  860. // Merge the dirty nodes of account trie into global set
  861. if set != nil {
  862. if err := nodes.Merge(set); err != nil {
  863. return common.Hash{}, err
  864. }
  865. accountTrieNodes = set.Len()
  866. }
  867. if metrics.EnabledExpensive {
  868. s.AccountCommits += time.Since(start)
  869. accountUpdatedMeter.Mark(int64(s.AccountUpdated))
  870. storageUpdatedMeter.Mark(int64(s.StorageUpdated))
  871. accountDeletedMeter.Mark(int64(s.AccountDeleted))
  872. storageDeletedMeter.Mark(int64(s.StorageDeleted))
  873. accountTrieCommittedMeter.Mark(int64(accountTrieNodes))
  874. storageTriesCommittedMeter.Mark(int64(storageTrieNodes))
  875. s.AccountUpdated, s.AccountDeleted = 0, 0
  876. s.StorageUpdated, s.StorageDeleted = 0, 0
  877. }
  878. // If snapshotting is enabled, update the snapshot tree with this new version
  879. if s.snap != nil {
  880. if metrics.EnabledExpensive {
  881. defer func(start time.Time) { s.SnapshotCommits += time.Since(start) }(time.Now())
  882. }
  883. // Only update if there's a state transition (skip empty Clique blocks)
  884. if parent := s.snap.Root(); parent != root {
  885. if err := s.snaps.Update(root, parent, s.snapDestructs, s.snapAccounts, s.snapStorage); err != nil {
  886. log.Warn("Failed to update snapshot tree", "from", parent, "to", root, "err", err)
  887. }
  888. // Keep 128 diff layers in the memory, persistent layer is 129th.
  889. // - head layer is paired with HEAD state
  890. // - head-1 layer is paired with HEAD-1 state
  891. // - head-127 layer(bottom-most diff layer) is paired with HEAD-127 state
  892. if err := s.snaps.Cap(root, 128); err != nil {
  893. log.Warn("Failed to cap snapshot tree", "root", root, "layers", 128, "err", err)
  894. }
  895. }
  896. s.snap, s.snapDestructs, s.snapAccounts, s.snapStorage = nil, nil, nil, nil
  897. }
  898. if err := s.db.TrieDB().Update(nodes); err != nil {
  899. return common.Hash{}, err
  900. }
  901. s.originalRoot = root
  902. return root, err
  903. }
  904. // PrepareAccessList handles the preparatory steps for executing a state transition with
  905. // regards to both EIP-2929 and EIP-2930:
  906. //
  907. // - Add sender to access list (2929)
  908. // - Add destination to access list (2929)
  909. // - Add precompiles to access list (2929)
  910. // - Add the contents of the optional tx access list (2930)
  911. //
  912. // This method should only be called if Berlin/2929+2930 is applicable at the current number.
  913. func (s *StateDB) PrepareAccessList(sender common.Address, dst *common.Address, precompiles []common.Address, list types.AccessList) {
  914. // Clear out any leftover from previous executions
  915. s.accessList = newAccessList()
  916. s.AddAddressToAccessList(sender)
  917. if dst != nil {
  918. s.AddAddressToAccessList(*dst)
  919. // If it's a create-tx, the destination will be added inside evm.create
  920. }
  921. for _, addr := range precompiles {
  922. s.AddAddressToAccessList(addr)
  923. }
  924. for _, el := range list {
  925. s.AddAddressToAccessList(el.Address)
  926. for _, key := range el.StorageKeys {
  927. s.AddSlotToAccessList(el.Address, key)
  928. }
  929. }
  930. }
  931. // AddAddressToAccessList adds the given address to the access list
  932. func (s *StateDB) AddAddressToAccessList(addr common.Address) {
  933. if s.accessList.AddAddress(addr) {
  934. s.journal.append(accessListAddAccountChange{&addr})
  935. }
  936. }
  937. // AddSlotToAccessList adds the given (address, slot)-tuple to the access list
  938. func (s *StateDB) AddSlotToAccessList(addr common.Address, slot common.Hash) {
  939. addrMod, slotMod := s.accessList.AddSlot(addr, slot)
  940. if addrMod {
  941. // In practice, this should not happen, since there is no way to enter the
  942. // scope of 'address' without having the 'address' become already added
  943. // to the access list (via call-variant, create, etc).
  944. // Better safe than sorry, though
  945. s.journal.append(accessListAddAccountChange{&addr})
  946. }
  947. if slotMod {
  948. s.journal.append(accessListAddSlotChange{
  949. address: &addr,
  950. slot: &slot,
  951. })
  952. }
  953. }
  954. // AddressInAccessList returns true if the given address is in the access list.
  955. func (s *StateDB) AddressInAccessList(addr common.Address) bool {
  956. return s.accessList.ContainsAddress(addr)
  957. }
  958. // SlotInAccessList returns true if the given (address, slot)-tuple is in the access list.
  959. func (s *StateDB) SlotInAccessList(addr common.Address, slot common.Hash) (addressPresent bool, slotPresent bool) {
  960. return s.accessList.Contains(addr, slot)
  961. }