statedb.go 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  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. "runtime"
  23. "sort"
  24. "sync"
  25. "time"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/common/gopool"
  28. "github.com/ethereum/go-ethereum/core/rawdb"
  29. "github.com/ethereum/go-ethereum/core/state/snapshot"
  30. "github.com/ethereum/go-ethereum/core/types"
  31. "github.com/ethereum/go-ethereum/crypto"
  32. "github.com/ethereum/go-ethereum/ethdb"
  33. "github.com/ethereum/go-ethereum/log"
  34. "github.com/ethereum/go-ethereum/metrics"
  35. "github.com/ethereum/go-ethereum/rlp"
  36. "github.com/ethereum/go-ethereum/trie"
  37. )
  38. const (
  39. preLoadLimit = 128
  40. defaultNumOfSlots = 100
  41. )
  42. type revision struct {
  43. id int
  44. journalIndex int
  45. }
  46. var (
  47. // emptyRoot is the known root hash of an empty trie.
  48. emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
  49. emptyAddr = crypto.Keccak256Hash(common.Address{}.Bytes())
  50. )
  51. type proofList [][]byte
  52. func (n *proofList) Put(key []byte, value []byte) error {
  53. *n = append(*n, value)
  54. return nil
  55. }
  56. func (n *proofList) Delete(key []byte) error {
  57. panic("not supported")
  58. }
  59. // StateDB structs within the ethereum protocol are used to store anything
  60. // within the merkle trie. StateDBs take care of caching and storing
  61. // nested states. It's the general query interface to retrieve:
  62. // * Contracts
  63. // * Accounts
  64. type StateDB struct {
  65. db Database
  66. prefetcher *triePrefetcher
  67. originalRoot common.Hash // The pre-state root, before any changes were made
  68. trie Trie
  69. hasher crypto.KeccakState
  70. diffLayer *types.DiffLayer
  71. diffTries map[common.Address]Trie
  72. diffCode map[common.Hash][]byte
  73. lightProcessed bool
  74. snapMux sync.Mutex
  75. snaps *snapshot.Tree
  76. snap snapshot.Snapshot
  77. snapDestructs map[common.Address]struct{}
  78. snapAccounts map[common.Address][]byte
  79. snapStorage map[common.Address]map[string][]byte
  80. // This map holds 'live' objects, which will get modified while processing a state transition.
  81. stateObjects map[common.Address]*StateObject
  82. stateObjectsPending map[common.Address]struct{} // State objects finalized but not yet written to the trie
  83. stateObjectsDirty map[common.Address]struct{} // State objects modified in the current execution
  84. // DB error.
  85. // State objects are used by the consensus core and VM which are
  86. // unable to deal with database-level errors. Any error that occurs
  87. // during a database read is memoized here and will eventually be returned
  88. // by StateDB.Commit.
  89. dbErr error
  90. // The refund counter, also used by state transitioning.
  91. refund uint64
  92. thash, bhash common.Hash
  93. txIndex int
  94. logs map[common.Hash][]*types.Log
  95. logSize uint
  96. preimages map[common.Hash][]byte
  97. // Per-transaction access list
  98. accessList *accessList
  99. // Journal of state modifications. This is the backbone of
  100. // Snapshot and RevertToSnapshot.
  101. journal *journal
  102. validRevisions []revision
  103. nextRevisionId int
  104. // Measurements gathered during execution for debugging purposes
  105. MetricsMux sync.Mutex
  106. AccountReads time.Duration
  107. AccountHashes time.Duration
  108. AccountUpdates time.Duration
  109. AccountCommits time.Duration
  110. StorageReads time.Duration
  111. StorageHashes time.Duration
  112. StorageUpdates time.Duration
  113. StorageCommits time.Duration
  114. SnapshotAccountReads time.Duration
  115. SnapshotStorageReads time.Duration
  116. SnapshotCommits time.Duration
  117. }
  118. // New creates a new state from a given trie.
  119. func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) {
  120. return newStateDB(root, db, snaps)
  121. }
  122. func newStateDB(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) {
  123. sdb := &StateDB{
  124. db: db,
  125. originalRoot: root,
  126. snaps: snaps,
  127. stateObjects: make(map[common.Address]*StateObject, defaultNumOfSlots),
  128. stateObjectsPending: make(map[common.Address]struct{}, defaultNumOfSlots),
  129. stateObjectsDirty: make(map[common.Address]struct{}, defaultNumOfSlots),
  130. logs: make(map[common.Hash][]*types.Log, defaultNumOfSlots),
  131. preimages: make(map[common.Hash][]byte),
  132. journal: newJournal(),
  133. hasher: crypto.NewKeccakState(),
  134. }
  135. tr, err := db.OpenTrie(root)
  136. if err != nil {
  137. return nil, err
  138. }
  139. sdb.trie = tr
  140. if sdb.snaps != nil {
  141. if sdb.snap = sdb.snaps.Snapshot(root); sdb.snap != nil {
  142. sdb.snapDestructs = make(map[common.Address]struct{})
  143. sdb.snapAccounts = make(map[common.Address][]byte)
  144. sdb.snapStorage = make(map[common.Address]map[string][]byte)
  145. }
  146. }
  147. return sdb, nil
  148. }
  149. // StartPrefetcher initializes a new trie prefetcher to pull in nodes from the
  150. // state trie concurrently while the state is mutated so that when we reach the
  151. // commit phase, most of the needed data is already hot.
  152. func (s *StateDB) StartPrefetcher(namespace string) {
  153. if s.prefetcher != nil {
  154. s.prefetcher.close()
  155. s.prefetcher = nil
  156. }
  157. if s.snap != nil {
  158. s.prefetcher = newTriePrefetcher(s.db, s.originalRoot, namespace)
  159. }
  160. }
  161. // StopPrefetcher terminates a running prefetcher and reports any leftover stats
  162. // from the gathered metrics.
  163. func (s *StateDB) StopPrefetcher() {
  164. if s.prefetcher != nil {
  165. s.prefetcher.close()
  166. s.prefetcher = nil
  167. }
  168. }
  169. // Mark that the block is processed by diff layer
  170. func (s *StateDB) MarkLightProcessed() {
  171. s.lightProcessed = true
  172. }
  173. func (s *StateDB) IsLightProcessed() bool {
  174. return s.lightProcessed
  175. }
  176. // setError remembers the first non-nil error it is called with.
  177. func (s *StateDB) setError(err error) {
  178. if s.dbErr == nil {
  179. s.dbErr = err
  180. }
  181. }
  182. func (s *StateDB) Error() error {
  183. return s.dbErr
  184. }
  185. func (s *StateDB) Trie() Trie {
  186. return s.trie
  187. }
  188. func (s *StateDB) SetDiff(diffLayer *types.DiffLayer, diffTries map[common.Address]Trie, diffCode map[common.Hash][]byte) {
  189. s.diffLayer, s.diffTries, s.diffCode = diffLayer, diffTries, diffCode
  190. }
  191. func (s *StateDB) SetSnapData(snapDestructs map[common.Address]struct{}, snapAccounts map[common.Address][]byte,
  192. snapStorage map[common.Address]map[string][]byte) {
  193. s.snapDestructs, s.snapAccounts, s.snapStorage = snapDestructs, snapAccounts, snapStorage
  194. }
  195. func (s *StateDB) AddLog(log *types.Log) {
  196. s.journal.append(addLogChange{txhash: s.thash})
  197. log.TxHash = s.thash
  198. log.BlockHash = s.bhash
  199. log.TxIndex = uint(s.txIndex)
  200. log.Index = s.logSize
  201. s.logs[s.thash] = append(s.logs[s.thash], log)
  202. s.logSize++
  203. }
  204. func (s *StateDB) GetLogs(hash common.Hash) []*types.Log {
  205. return s.logs[hash]
  206. }
  207. func (s *StateDB) Logs() []*types.Log {
  208. var logs []*types.Log
  209. for _, lgs := range s.logs {
  210. logs = append(logs, lgs...)
  211. }
  212. return logs
  213. }
  214. // AddPreimage records a SHA3 preimage seen by the VM.
  215. func (s *StateDB) AddPreimage(hash common.Hash, preimage []byte) {
  216. if _, ok := s.preimages[hash]; !ok {
  217. s.journal.append(addPreimageChange{hash: hash})
  218. pi := make([]byte, len(preimage))
  219. copy(pi, preimage)
  220. s.preimages[hash] = pi
  221. }
  222. }
  223. // Preimages returns a list of SHA3 preimages that have been submitted.
  224. func (s *StateDB) Preimages() map[common.Hash][]byte {
  225. return s.preimages
  226. }
  227. // AddRefund adds gas to the refund counter
  228. func (s *StateDB) AddRefund(gas uint64) {
  229. s.journal.append(refundChange{prev: s.refund})
  230. s.refund += gas
  231. }
  232. // SubRefund removes gas from the refund counter.
  233. // This method will panic if the refund counter goes below zero
  234. func (s *StateDB) SubRefund(gas uint64) {
  235. s.journal.append(refundChange{prev: s.refund})
  236. if gas > s.refund {
  237. panic(fmt.Sprintf("Refund counter below zero (gas: %d > refund: %d)", gas, s.refund))
  238. }
  239. s.refund -= gas
  240. }
  241. // Exist reports whether the given account address exists in the state.
  242. // Notably this also returns true for suicided accounts.
  243. func (s *StateDB) Exist(addr common.Address) bool {
  244. return s.getStateObject(addr) != nil
  245. }
  246. // Empty returns whether the state object is either non-existent
  247. // or empty according to the EIP161 specification (balance = nonce = code = 0)
  248. func (s *StateDB) Empty(addr common.Address) bool {
  249. so := s.getStateObject(addr)
  250. return so == nil || so.empty()
  251. }
  252. // GetBalance retrieves the balance from the given address or 0 if object not found
  253. func (s *StateDB) GetBalance(addr common.Address) *big.Int {
  254. stateObject := s.getStateObject(addr)
  255. if stateObject != nil {
  256. return stateObject.Balance()
  257. }
  258. return common.Big0
  259. }
  260. func (s *StateDB) GetNonce(addr common.Address) uint64 {
  261. stateObject := s.getStateObject(addr)
  262. if stateObject != nil {
  263. return stateObject.Nonce()
  264. }
  265. return 0
  266. }
  267. // TxIndex returns the current transaction index set by Prepare.
  268. func (s *StateDB) TxIndex() int {
  269. return s.txIndex
  270. }
  271. // BlockHash returns the current block hash set by Prepare.
  272. func (s *StateDB) BlockHash() common.Hash {
  273. return s.bhash
  274. }
  275. func (s *StateDB) GetCode(addr common.Address) []byte {
  276. stateObject := s.getStateObject(addr)
  277. if stateObject != nil {
  278. return stateObject.Code(s.db)
  279. }
  280. return nil
  281. }
  282. func (s *StateDB) GetCodeSize(addr common.Address) int {
  283. stateObject := s.getStateObject(addr)
  284. if stateObject != nil {
  285. return stateObject.CodeSize(s.db)
  286. }
  287. return 0
  288. }
  289. func (s *StateDB) GetCodeHash(addr common.Address) common.Hash {
  290. stateObject := s.getStateObject(addr)
  291. if stateObject == nil {
  292. return common.Hash{}
  293. }
  294. return common.BytesToHash(stateObject.CodeHash())
  295. }
  296. // GetState retrieves a value from the given account's storage trie.
  297. func (s *StateDB) GetState(addr common.Address, hash common.Hash) common.Hash {
  298. stateObject := s.getStateObject(addr)
  299. if stateObject != nil {
  300. return stateObject.GetState(s.db, hash)
  301. }
  302. return common.Hash{}
  303. }
  304. // GetProof returns the Merkle proof for a given account.
  305. func (s *StateDB) GetProof(addr common.Address) ([][]byte, error) {
  306. return s.GetProofByHash(crypto.Keccak256Hash(addr.Bytes()))
  307. }
  308. // GetProofByHash returns the Merkle proof for a given account.
  309. func (s *StateDB) GetProofByHash(addrHash common.Hash) ([][]byte, error) {
  310. var proof proofList
  311. err := s.trie.Prove(addrHash[:], 0, &proof)
  312. return proof, err
  313. }
  314. // GetStorageProof returns the Merkle proof for given storage slot.
  315. func (s *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byte, error) {
  316. var proof proofList
  317. trie := s.StorageTrie(a)
  318. if trie == nil {
  319. return proof, errors.New("storage trie for requested address does not exist")
  320. }
  321. err := trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof)
  322. return proof, err
  323. }
  324. // GetStorageProofByHash returns the Merkle proof for given storage slot.
  325. func (s *StateDB) GetStorageProofByHash(a common.Address, key common.Hash) ([][]byte, error) {
  326. var proof proofList
  327. trie := s.StorageTrie(a)
  328. if trie == nil {
  329. return proof, errors.New("storage trie for requested address does not exist")
  330. }
  331. err := trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof)
  332. return proof, err
  333. }
  334. // GetCommittedState retrieves a value from the given account's committed storage trie.
  335. func (s *StateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash {
  336. stateObject := s.getStateObject(addr)
  337. if stateObject != nil {
  338. return stateObject.GetCommittedState(s.db, hash)
  339. }
  340. return common.Hash{}
  341. }
  342. // Database retrieves the low level database supporting the lower level trie ops.
  343. func (s *StateDB) Database() Database {
  344. return s.db
  345. }
  346. // StorageTrie returns the storage trie of an account.
  347. // The return value is a copy and is nil for non-existent accounts.
  348. func (s *StateDB) StorageTrie(addr common.Address) Trie {
  349. stateObject := s.getStateObject(addr)
  350. if stateObject == nil {
  351. return nil
  352. }
  353. cpy := stateObject.deepCopy(s)
  354. cpy.updateTrie(s.db)
  355. return cpy.getTrie(s.db)
  356. }
  357. func (s *StateDB) HasSuicided(addr common.Address) bool {
  358. stateObject := s.getStateObject(addr)
  359. if stateObject != nil {
  360. return stateObject.suicided
  361. }
  362. return false
  363. }
  364. /*
  365. * SETTERS
  366. */
  367. // AddBalance adds amount to the account associated with addr.
  368. func (s *StateDB) AddBalance(addr common.Address, amount *big.Int) {
  369. stateObject := s.GetOrNewStateObject(addr)
  370. if stateObject != nil {
  371. stateObject.AddBalance(amount)
  372. }
  373. }
  374. // SubBalance subtracts amount from the account associated with addr.
  375. func (s *StateDB) SubBalance(addr common.Address, amount *big.Int) {
  376. stateObject := s.GetOrNewStateObject(addr)
  377. if stateObject != nil {
  378. stateObject.SubBalance(amount)
  379. }
  380. }
  381. func (s *StateDB) SetBalance(addr common.Address, amount *big.Int) {
  382. stateObject := s.GetOrNewStateObject(addr)
  383. if stateObject != nil {
  384. stateObject.SetBalance(amount)
  385. }
  386. }
  387. func (s *StateDB) SetNonce(addr common.Address, nonce uint64) {
  388. stateObject := s.GetOrNewStateObject(addr)
  389. if stateObject != nil {
  390. stateObject.SetNonce(nonce)
  391. }
  392. }
  393. func (s *StateDB) SetCode(addr common.Address, code []byte) {
  394. stateObject := s.GetOrNewStateObject(addr)
  395. if stateObject != nil {
  396. stateObject.SetCode(crypto.Keccak256Hash(code), code)
  397. }
  398. }
  399. func (s *StateDB) SetState(addr common.Address, key, value common.Hash) {
  400. stateObject := s.GetOrNewStateObject(addr)
  401. if stateObject != nil {
  402. stateObject.SetState(s.db, key, value)
  403. }
  404. }
  405. // SetStorage replaces the entire storage for the specified account with given
  406. // storage. This function should only be used for debugging.
  407. func (s *StateDB) SetStorage(addr common.Address, storage map[common.Hash]common.Hash) {
  408. stateObject := s.GetOrNewStateObject(addr)
  409. if stateObject != nil {
  410. stateObject.SetStorage(storage)
  411. }
  412. }
  413. // Suicide marks the given account as suicided.
  414. // This clears the account balance.
  415. //
  416. // The account's state object is still available until the state is committed,
  417. // getStateObject will return a non-nil account after Suicide.
  418. func (s *StateDB) Suicide(addr common.Address) bool {
  419. stateObject := s.getStateObject(addr)
  420. if stateObject == nil {
  421. return false
  422. }
  423. s.journal.append(suicideChange{
  424. account: &addr,
  425. prev: stateObject.suicided,
  426. prevbalance: new(big.Int).Set(stateObject.Balance()),
  427. })
  428. stateObject.markSuicided()
  429. stateObject.data.Balance = new(big.Int)
  430. return true
  431. }
  432. //
  433. // Setting, updating & deleting state object methods.
  434. //
  435. // updateStateObject writes the given object to the trie.
  436. func (s *StateDB) updateStateObject(obj *StateObject) {
  437. // Track the amount of time wasted on updating the account from the trie
  438. if metrics.EnabledExpensive {
  439. defer func(start time.Time) { s.AccountUpdates += time.Since(start) }(time.Now())
  440. }
  441. // Encode the account and update the account trie
  442. addr := obj.Address()
  443. data := obj.encodeData
  444. var err error
  445. if data == nil {
  446. data, err = rlp.EncodeToBytes(obj)
  447. if err != nil {
  448. panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err))
  449. }
  450. }
  451. if err = s.trie.TryUpdate(addr[:], data); err != nil {
  452. s.setError(fmt.Errorf("updateStateObject (%x) error: %v", addr[:], err))
  453. }
  454. }
  455. // deleteStateObject removes the given object from the state trie.
  456. func (s *StateDB) deleteStateObject(obj *StateObject) {
  457. // Track the amount of time wasted on deleting the account from the trie
  458. if metrics.EnabledExpensive {
  459. defer func(start time.Time) { s.AccountUpdates += time.Since(start) }(time.Now())
  460. }
  461. // Delete the account from the trie
  462. addr := obj.Address()
  463. if err := s.trie.TryDelete(addr[:]); err != nil {
  464. s.setError(fmt.Errorf("deleteStateObject (%x) error: %v", addr[:], err))
  465. }
  466. }
  467. // getStateObject retrieves a state object given by the address, returning nil if
  468. // the object is not found or was deleted in this execution context. If you need
  469. // to differentiate between non-existent/just-deleted, use getDeletedStateObject.
  470. func (s *StateDB) getStateObject(addr common.Address) *StateObject {
  471. if obj := s.getDeletedStateObject(addr); obj != nil && !obj.deleted {
  472. return obj
  473. }
  474. return nil
  475. }
  476. func (s *StateDB) TryPreload(block *types.Block, signer types.Signer) {
  477. accounts := make(map[common.Address]bool, block.Transactions().Len())
  478. accountsSlice := make([]common.Address, 0, block.Transactions().Len())
  479. for _, tx := range block.Transactions() {
  480. from, err := types.Sender(signer, tx)
  481. if err != nil {
  482. break
  483. }
  484. accounts[from] = true
  485. if tx.To() != nil {
  486. accounts[*tx.To()] = true
  487. }
  488. }
  489. for account := range accounts {
  490. accountsSlice = append(accountsSlice, account)
  491. }
  492. if len(accountsSlice) >= preLoadLimit && len(accountsSlice) > runtime.NumCPU() {
  493. objsChan := make(chan []*StateObject, runtime.NumCPU())
  494. for i := 0; i < runtime.NumCPU(); i++ {
  495. start := i * len(accountsSlice) / runtime.NumCPU()
  496. end := (i + 1) * len(accountsSlice) / runtime.NumCPU()
  497. if i+1 == runtime.NumCPU() {
  498. end = len(accountsSlice)
  499. }
  500. go func(start, end int) {
  501. objs := s.preloadStateObject(accountsSlice[start:end])
  502. objsChan <- objs
  503. }(start, end)
  504. }
  505. for i := 0; i < runtime.NumCPU(); i++ {
  506. objs := <-objsChan
  507. for _, obj := range objs {
  508. s.SetStateObject(obj)
  509. }
  510. }
  511. }
  512. }
  513. func (s *StateDB) preloadStateObject(address []common.Address) []*StateObject {
  514. // Prefer live objects if any is available
  515. if s.snap == nil {
  516. return nil
  517. }
  518. hasher := crypto.NewKeccakState()
  519. objs := make([]*StateObject, 0, len(address))
  520. for _, addr := range address {
  521. // If no live objects are available, attempt to use snapshots
  522. if acc, err := s.snap.Account(crypto.HashData(hasher, addr.Bytes())); err == nil {
  523. if acc == nil {
  524. continue
  525. }
  526. data := &Account{
  527. Nonce: acc.Nonce,
  528. Balance: acc.Balance,
  529. CodeHash: acc.CodeHash,
  530. Root: common.BytesToHash(acc.Root),
  531. }
  532. if len(data.CodeHash) == 0 {
  533. data.CodeHash = emptyCodeHash
  534. }
  535. if data.Root == (common.Hash{}) {
  536. data.Root = emptyRoot
  537. }
  538. // Insert into the live set
  539. obj := newObject(s, addr, *data)
  540. objs = append(objs, obj)
  541. }
  542. // Do not enable this feature when snapshot is not enabled.
  543. }
  544. return objs
  545. }
  546. // getDeletedStateObject is similar to getStateObject, but instead of returning
  547. // nil for a deleted state object, it returns the actual object with the deleted
  548. // flag set. This is needed by the state journal to revert to the correct s-
  549. // destructed object instead of wiping all knowledge about the state object.
  550. func (s *StateDB) getDeletedStateObject(addr common.Address) *StateObject {
  551. // Prefer live objects if any is available
  552. if obj := s.stateObjects[addr]; obj != nil {
  553. return obj
  554. }
  555. // If no live objects are available, attempt to use snapshots
  556. var (
  557. data *Account
  558. err error
  559. )
  560. if s.snap != nil {
  561. if metrics.EnabledExpensive {
  562. defer func(start time.Time) { s.SnapshotAccountReads += time.Since(start) }(time.Now())
  563. }
  564. var acc *snapshot.Account
  565. if acc, err = s.snap.Account(crypto.HashData(s.hasher, addr.Bytes())); err == nil {
  566. if acc == nil {
  567. return nil
  568. }
  569. data = &Account{
  570. Nonce: acc.Nonce,
  571. Balance: acc.Balance,
  572. CodeHash: acc.CodeHash,
  573. Root: common.BytesToHash(acc.Root),
  574. }
  575. if len(data.CodeHash) == 0 {
  576. data.CodeHash = emptyCodeHash
  577. }
  578. if data.Root == (common.Hash{}) {
  579. data.Root = emptyRoot
  580. }
  581. }
  582. }
  583. // If snapshot unavailable or reading from it failed, load from the database
  584. if s.snap == nil || err != nil {
  585. if s.trie == nil {
  586. tr, err := s.db.OpenTrie(s.originalRoot)
  587. if err != nil {
  588. s.setError(fmt.Errorf("failed to open trie tree"))
  589. return nil
  590. }
  591. s.trie = tr
  592. }
  593. if metrics.EnabledExpensive {
  594. defer func(start time.Time) { s.AccountReads += time.Since(start) }(time.Now())
  595. }
  596. enc, err := s.trie.TryGet(addr.Bytes())
  597. if err != nil {
  598. s.setError(fmt.Errorf("getDeleteStateObject (%x) error: %v", addr.Bytes(), err))
  599. return nil
  600. }
  601. if len(enc) == 0 {
  602. return nil
  603. }
  604. data = new(Account)
  605. if err := rlp.DecodeBytes(enc, data); err != nil {
  606. log.Error("Failed to decode state object", "addr", addr, "err", err)
  607. return nil
  608. }
  609. }
  610. // Insert into the live set
  611. obj := newObject(s, addr, *data)
  612. s.SetStateObject(obj)
  613. return obj
  614. }
  615. func (s *StateDB) SetStateObject(object *StateObject) {
  616. s.stateObjects[object.Address()] = object
  617. }
  618. // GetOrNewStateObject retrieves a state object or create a new state object if nil.
  619. func (s *StateDB) GetOrNewStateObject(addr common.Address) *StateObject {
  620. stateObject := s.getStateObject(addr)
  621. if stateObject == nil {
  622. stateObject, _ = s.createObject(addr)
  623. }
  624. return stateObject
  625. }
  626. // createObject creates a new state object. If there is an existing account with
  627. // the given address, it is overwritten and returned as the second return value.
  628. func (s *StateDB) createObject(addr common.Address) (newobj, prev *StateObject) {
  629. prev = s.getDeletedStateObject(addr) // Note, prev might have been deleted, we need that!
  630. var prevdestruct bool
  631. if s.snap != nil && prev != nil {
  632. _, prevdestruct = s.snapDestructs[prev.address]
  633. if !prevdestruct {
  634. s.snapDestructs[prev.address] = struct{}{}
  635. }
  636. }
  637. newobj = newObject(s, addr, Account{})
  638. newobj.setNonce(0) // sets the object to dirty
  639. if prev == nil {
  640. s.journal.append(createObjectChange{account: &addr})
  641. } else {
  642. s.journal.append(resetObjectChange{prev: prev, prevdestruct: prevdestruct})
  643. }
  644. s.SetStateObject(newobj)
  645. if prev != nil && !prev.deleted {
  646. return newobj, prev
  647. }
  648. return newobj, nil
  649. }
  650. // CreateAccount explicitly creates a state object. If a state object with the address
  651. // already exists the balance is carried over to the new account.
  652. //
  653. // CreateAccount is called during the EVM CREATE operation. The situation might arise that
  654. // a contract does the following:
  655. //
  656. // 1. sends funds to sha(account ++ (nonce + 1))
  657. // 2. tx_create(sha(account ++ nonce)) (note that this gets the address of 1)
  658. //
  659. // Carrying over the balance ensures that Ether doesn't disappear.
  660. func (s *StateDB) CreateAccount(addr common.Address) {
  661. newObj, prev := s.createObject(addr)
  662. if prev != nil {
  663. newObj.setBalance(prev.data.Balance)
  664. }
  665. }
  666. func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) error {
  667. so := db.getStateObject(addr)
  668. if so == nil {
  669. return nil
  670. }
  671. it := trie.NewIterator(so.getTrie(db.db).NodeIterator(nil))
  672. for it.Next() {
  673. key := common.BytesToHash(db.trie.GetKey(it.Key))
  674. if value, dirty := so.dirtyStorage[key]; dirty {
  675. if !cb(key, value) {
  676. return nil
  677. }
  678. continue
  679. }
  680. if len(it.Value) > 0 {
  681. _, content, _, err := rlp.Split(it.Value)
  682. if err != nil {
  683. return err
  684. }
  685. if !cb(key, common.BytesToHash(content)) {
  686. return nil
  687. }
  688. }
  689. }
  690. return nil
  691. }
  692. // Copy creates a deep, independent copy of the state.
  693. // Snapshots of the copied state cannot be applied to the copy.
  694. func (s *StateDB) Copy() *StateDB {
  695. // Copy all the basic fields, initialize the memory ones
  696. state := &StateDB{
  697. db: s.db,
  698. trie: s.db.CopyTrie(s.trie),
  699. stateObjects: make(map[common.Address]*StateObject, len(s.journal.dirties)),
  700. stateObjectsPending: make(map[common.Address]struct{}, len(s.stateObjectsPending)),
  701. stateObjectsDirty: make(map[common.Address]struct{}, len(s.journal.dirties)),
  702. refund: s.refund,
  703. logs: make(map[common.Hash][]*types.Log, len(s.logs)),
  704. logSize: s.logSize,
  705. preimages: make(map[common.Hash][]byte, len(s.preimages)),
  706. journal: newJournal(),
  707. hasher: crypto.NewKeccakState(),
  708. }
  709. // Copy the dirty states, logs, and preimages
  710. for addr := range s.journal.dirties {
  711. // As documented [here](https://github.com/ethereum/go-ethereum/pull/16485#issuecomment-380438527),
  712. // and in the Finalise-method, there is a case where an object is in the journal but not
  713. // in the stateObjects: OOG after touch on ripeMD prior to Byzantium. Thus, we need to check for
  714. // nil
  715. if object, exist := s.stateObjects[addr]; exist {
  716. // Even though the original object is dirty, we are not copying the journal,
  717. // so we need to make sure that anyside effect the journal would have caused
  718. // during a commit (or similar op) is already applied to the copy.
  719. state.stateObjects[addr] = object.deepCopy(state)
  720. state.stateObjectsDirty[addr] = struct{}{} // Mark the copy dirty to force internal (code/state) commits
  721. state.stateObjectsPending[addr] = struct{}{} // Mark the copy pending to force external (account) commits
  722. }
  723. }
  724. // Above, we don't copy the actual journal. This means that if the copy is copied, the
  725. // loop above will be a no-op, since the copy's journal is empty.
  726. // Thus, here we iterate over stateObjects, to enable copies of copies
  727. for addr := range s.stateObjectsPending {
  728. if _, exist := state.stateObjects[addr]; !exist {
  729. state.stateObjects[addr] = s.stateObjects[addr].deepCopy(state)
  730. }
  731. state.stateObjectsPending[addr] = struct{}{}
  732. }
  733. for addr := range s.stateObjectsDirty {
  734. if _, exist := state.stateObjects[addr]; !exist {
  735. state.stateObjects[addr] = s.stateObjects[addr].deepCopy(state)
  736. }
  737. state.stateObjectsDirty[addr] = struct{}{}
  738. }
  739. for hash, logs := range s.logs {
  740. cpy := make([]*types.Log, len(logs))
  741. for i, l := range logs {
  742. cpy[i] = new(types.Log)
  743. *cpy[i] = *l
  744. }
  745. state.logs[hash] = cpy
  746. }
  747. for hash, preimage := range s.preimages {
  748. state.preimages[hash] = preimage
  749. }
  750. // Do we need to copy the access list? In practice: No. At the start of a
  751. // transaction, the access list is empty. In practice, we only ever copy state
  752. // _between_ transactions/blocks, never in the middle of a transaction.
  753. // However, it doesn't cost us much to copy an empty list, so we do it anyway
  754. // to not blow up if we ever decide copy it in the middle of a transaction
  755. if s.accessList != nil {
  756. state.accessList = s.accessList.Copy()
  757. }
  758. // If there's a prefetcher running, make an inactive copy of it that can
  759. // only access data but does not actively preload (since the user will not
  760. // know that they need to explicitly terminate an active copy).
  761. if s.prefetcher != nil {
  762. state.prefetcher = s.prefetcher.copy()
  763. }
  764. if s.snaps != nil {
  765. // In order for the miner to be able to use and make additions
  766. // to the snapshot tree, we need to copy that aswell.
  767. // Otherwise, any block mined by ourselves will cause gaps in the tree,
  768. // and force the miner to operate trie-backed only
  769. state.snaps = s.snaps
  770. state.snap = s.snap
  771. // deep copy needed
  772. state.snapDestructs = make(map[common.Address]struct{})
  773. for k, v := range s.snapDestructs {
  774. state.snapDestructs[k] = v
  775. }
  776. state.snapAccounts = make(map[common.Address][]byte)
  777. for k, v := range s.snapAccounts {
  778. state.snapAccounts[k] = v
  779. }
  780. state.snapStorage = make(map[common.Address]map[string][]byte)
  781. for k, v := range s.snapStorage {
  782. temp := make(map[string][]byte)
  783. for kk, vv := range v {
  784. temp[kk] = vv
  785. }
  786. state.snapStorage[k] = temp
  787. }
  788. }
  789. return state
  790. }
  791. // Snapshot returns an identifier for the current revision of the state.
  792. func (s *StateDB) Snapshot() int {
  793. id := s.nextRevisionId
  794. s.nextRevisionId++
  795. s.validRevisions = append(s.validRevisions, revision{id, s.journal.length()})
  796. return id
  797. }
  798. // RevertToSnapshot reverts all state changes made since the given revision.
  799. func (s *StateDB) RevertToSnapshot(revid int) {
  800. // Find the snapshot in the stack of valid snapshots.
  801. idx := sort.Search(len(s.validRevisions), func(i int) bool {
  802. return s.validRevisions[i].id >= revid
  803. })
  804. if idx == len(s.validRevisions) || s.validRevisions[idx].id != revid {
  805. panic(fmt.Errorf("revision id %v cannot be reverted", revid))
  806. }
  807. snapshot := s.validRevisions[idx].journalIndex
  808. // Replay the journal to undo changes and remove invalidated snapshots
  809. s.journal.revert(s, snapshot)
  810. s.validRevisions = s.validRevisions[:idx]
  811. }
  812. // GetRefund returns the current value of the refund counter.
  813. func (s *StateDB) GetRefund() uint64 {
  814. return s.refund
  815. }
  816. // Finalise finalises the state by removing the s destructed objects and clears
  817. // the journal as well as the refunds. Finalise, however, will not push any updates
  818. // into the tries just yet. Only IntermediateRoot or Commit will do that.
  819. func (s *StateDB) Finalise(deleteEmptyObjects bool) {
  820. addressesToPrefetch := make([][]byte, 0, len(s.journal.dirties))
  821. for addr := range s.journal.dirties {
  822. obj, exist := s.stateObjects[addr]
  823. if !exist {
  824. // ripeMD is 'touched' at block 1714175, in tx 0x1237f737031e40bcde4a8b7e717b2d15e3ecadfe49bb1bbc71ee9deb09c6fcf2
  825. // That tx goes out of gas, and although the notion of 'touched' does not exist there, the
  826. // touch-event will still be recorded in the journal. Since ripeMD is a special snowflake,
  827. // it will persist in the journal even though the journal is reverted. In this special circumstance,
  828. // it may exist in `s.journal.dirties` but not in `s.stateObjects`.
  829. // Thus, we can safely ignore it here
  830. continue
  831. }
  832. if obj.suicided || (deleteEmptyObjects && obj.empty()) {
  833. obj.deleted = true
  834. // If state snapshotting is active, also mark the destruction there.
  835. // Note, we can't do this only at the end of a block because multiple
  836. // transactions within the same block might self destruct and then
  837. // ressurrect an account; but the snapshotter needs both events.
  838. if s.snap != nil {
  839. s.snapDestructs[obj.address] = struct{}{} // We need to maintain account deletions explicitly (will remain set indefinitely)
  840. delete(s.snapAccounts, obj.address) // Clear out any previously updated account data (may be recreated via a ressurrect)
  841. delete(s.snapStorage, obj.address) // Clear out any previously updated storage data (may be recreated via a ressurrect)
  842. }
  843. } else {
  844. obj.finalise(true) // Prefetch slots in the background
  845. }
  846. if _, exist := s.stateObjectsPending[addr]; !exist {
  847. s.stateObjectsPending[addr] = struct{}{}
  848. }
  849. if _, exist := s.stateObjectsDirty[addr]; !exist {
  850. s.stateObjectsDirty[addr] = struct{}{}
  851. // At this point, also ship the address off to the precacher. The precacher
  852. // will start loading tries, and when the change is eventually committed,
  853. // the commit-phase will be a lot faster
  854. addressesToPrefetch = append(addressesToPrefetch, common.CopyBytes(addr[:])) // Copy needed for closure
  855. }
  856. }
  857. if s.prefetcher != nil && len(addressesToPrefetch) > 0 {
  858. s.prefetcher.prefetch(s.originalRoot, addressesToPrefetch, emptyAddr)
  859. }
  860. // Invalidate journal because reverting across transactions is not allowed.
  861. s.clearJournalAndRefund()
  862. }
  863. // IntermediateRoot computes the current root hash of the state trie.
  864. // It is called in between transactions to get the root hash that
  865. // goes into transaction receipts.
  866. func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
  867. if s.lightProcessed {
  868. return s.trie.Hash()
  869. }
  870. // Finalise all the dirty storage states and write them into the tries
  871. s.Finalise(deleteEmptyObjects)
  872. // If there was a trie prefetcher operating, it gets aborted and irrevocably
  873. // modified after we start retrieving tries. Remove it from the statedb after
  874. // this round of use.
  875. //
  876. // This is weird pre-byzantium since the first tx runs with a prefetcher and
  877. // the remainder without, but pre-byzantium even the initial prefetcher is
  878. // useless, so no sleep lost.
  879. prefetcher := s.prefetcher
  880. if s.prefetcher != nil {
  881. defer func() {
  882. s.prefetcher.close()
  883. s.prefetcher = nil
  884. }()
  885. }
  886. tasks := make(chan func())
  887. finishCh := make(chan struct{})
  888. defer close(finishCh)
  889. wg := sync.WaitGroup{}
  890. for i := 0; i < runtime.NumCPU(); i++ {
  891. go func() {
  892. for {
  893. select {
  894. case task := <-tasks:
  895. task()
  896. case <-finishCh:
  897. return
  898. }
  899. }
  900. }()
  901. }
  902. // Although naively it makes sense to retrieve the account trie and then do
  903. // the contract storage and account updates sequentially, that short circuits
  904. // the account prefetcher. Instead, let's process all the storage updates
  905. // first, giving the account prefeches just a few more milliseconds of time
  906. // to pull useful data from disk.
  907. for addr := range s.stateObjectsPending {
  908. if obj := s.stateObjects[addr]; !obj.deleted {
  909. wg.Add(1)
  910. tasks <- func() {
  911. obj.updateRoot(s.db)
  912. // If state snapshotting is active, cache the data til commit. Note, this
  913. // update mechanism is not symmetric to the deletion, because whereas it is
  914. // enough to track account updates at commit time, deletions need tracking
  915. // at transaction boundary level to ensure we capture state clearing.
  916. if s.snap != nil && !obj.deleted {
  917. s.snapMux.Lock()
  918. // It is possible to add unnecessary change, but it is fine.
  919. s.snapAccounts[obj.address] = snapshot.SlimAccountRLP(obj.data.Nonce, obj.data.Balance, obj.data.Root, obj.data.CodeHash)
  920. s.snapMux.Unlock()
  921. }
  922. data, err := rlp.EncodeToBytes(obj)
  923. if err != nil {
  924. panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err))
  925. }
  926. obj.encodeData = data
  927. wg.Done()
  928. }
  929. }
  930. }
  931. wg.Wait()
  932. // Now we're about to start to write changes to the trie. The trie is so far
  933. // _untouched_. We can check with the prefetcher, if it can give us a trie
  934. // which has the same root, but also has some content loaded into it.
  935. if prefetcher != nil {
  936. if trie := prefetcher.trie(s.originalRoot); trie != nil {
  937. s.trie = trie
  938. }
  939. }
  940. if s.trie == nil {
  941. tr, err := s.db.OpenTrie(s.originalRoot)
  942. if err != nil {
  943. panic("Failed to open trie tree")
  944. }
  945. s.trie = tr
  946. }
  947. usedAddrs := make([][]byte, 0, len(s.stateObjectsPending))
  948. for addr := range s.stateObjectsPending {
  949. if obj := s.stateObjects[addr]; obj.deleted {
  950. s.deleteStateObject(obj)
  951. } else {
  952. s.updateStateObject(obj)
  953. }
  954. usedAddrs = append(usedAddrs, common.CopyBytes(addr[:])) // Copy needed for closure
  955. }
  956. if prefetcher != nil {
  957. prefetcher.used(s.originalRoot, usedAddrs)
  958. }
  959. if len(s.stateObjectsPending) > 0 {
  960. s.stateObjectsPending = make(map[common.Address]struct{})
  961. }
  962. // Track the amount of time wasted on hashing the account trie
  963. if metrics.EnabledExpensive {
  964. defer func(start time.Time) { s.AccountHashes += time.Since(start) }(time.Now())
  965. }
  966. root := s.trie.Hash()
  967. return root
  968. }
  969. // Prepare sets the current transaction hash and index and block hash which is
  970. // used when the EVM emits new state logs.
  971. func (s *StateDB) Prepare(thash, bhash common.Hash, ti int) {
  972. s.thash = thash
  973. s.bhash = bhash
  974. s.txIndex = ti
  975. s.accessList = nil
  976. }
  977. func (s *StateDB) clearJournalAndRefund() {
  978. if len(s.journal.entries) > 0 {
  979. s.journal = newJournal()
  980. s.refund = 0
  981. }
  982. s.validRevisions = s.validRevisions[:0] // Snapshots can be created without journal entires
  983. }
  984. func (s *StateDB) LightCommit(root common.Hash) (common.Hash, *types.DiffLayer, error) {
  985. codeWriter := s.db.TrieDB().DiskDB().NewBatch()
  986. commitFuncs := []func() error{
  987. func() error {
  988. for codeHash, code := range s.diffCode {
  989. rawdb.WriteCode(codeWriter, codeHash, code)
  990. if codeWriter.ValueSize() >= ethdb.IdealBatchSize {
  991. if err := codeWriter.Write(); err != nil {
  992. return err
  993. }
  994. codeWriter.Reset()
  995. }
  996. }
  997. if codeWriter.ValueSize() > 0 {
  998. if err := codeWriter.Write(); err != nil {
  999. return err
  1000. }
  1001. }
  1002. return nil
  1003. },
  1004. func() error {
  1005. tasks := make(chan func())
  1006. taskResults := make(chan error, len(s.diffTries))
  1007. tasksNum := 0
  1008. finishCh := make(chan struct{})
  1009. defer close(finishCh)
  1010. threads := gopool.Threads(len(s.diffTries))
  1011. for i := 0; i < threads; i++ {
  1012. go func() {
  1013. for {
  1014. select {
  1015. case task := <-tasks:
  1016. task()
  1017. case <-finishCh:
  1018. return
  1019. }
  1020. }
  1021. }()
  1022. }
  1023. for account, diff := range s.diffTries {
  1024. tmpAccount := account
  1025. tmpDiff := diff
  1026. tasks <- func() {
  1027. root, err := tmpDiff.Commit(nil)
  1028. if err != nil {
  1029. taskResults <- err
  1030. return
  1031. }
  1032. s.db.CacheStorage(crypto.Keccak256Hash(tmpAccount[:]), root, tmpDiff)
  1033. taskResults <- nil
  1034. }
  1035. tasksNum++
  1036. }
  1037. for i := 0; i < tasksNum; i++ {
  1038. err := <-taskResults
  1039. if err != nil {
  1040. return err
  1041. }
  1042. }
  1043. // commit account trie
  1044. var account Account
  1045. root, err := s.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
  1046. if err := rlp.DecodeBytes(leaf, &account); err != nil {
  1047. return nil
  1048. }
  1049. if account.Root != emptyRoot {
  1050. s.db.TrieDB().Reference(account.Root, parent)
  1051. }
  1052. return nil
  1053. })
  1054. if err != nil {
  1055. return err
  1056. }
  1057. if root != emptyRoot {
  1058. s.db.CacheAccount(root, s.trie)
  1059. }
  1060. return nil
  1061. },
  1062. func() error {
  1063. if s.snap != nil {
  1064. if metrics.EnabledExpensive {
  1065. defer func(start time.Time) { s.SnapshotCommits += time.Since(start) }(time.Now())
  1066. }
  1067. // Only update if there's a state transition (skip empty Clique blocks)
  1068. if parent := s.snap.Root(); parent != root {
  1069. if err := s.snaps.Update(root, parent, s.snapDestructs, s.snapAccounts, s.snapStorage); err != nil {
  1070. log.Warn("Failed to update snapshot tree", "from", parent, "to", root, "err", err)
  1071. }
  1072. // Keep n diff layers in the memory
  1073. // - head layer is paired with HEAD state
  1074. // - head-1 layer is paired with HEAD-1 state
  1075. // - head-(n-1) layer(bottom-most diff layer) is paired with HEAD-(n-1)state
  1076. if err := s.snaps.Cap(root, s.snaps.CapLimit()); err != nil {
  1077. log.Warn("Failed to cap snapshot tree", "root", root, "layers", s.snaps.CapLimit(), "err", err)
  1078. }
  1079. }
  1080. }
  1081. return nil
  1082. },
  1083. }
  1084. commitRes := make(chan error, len(commitFuncs))
  1085. for _, f := range commitFuncs {
  1086. tmpFunc := f
  1087. go func() {
  1088. commitRes <- tmpFunc()
  1089. }()
  1090. }
  1091. for i := 0; i < len(commitFuncs); i++ {
  1092. r := <-commitRes
  1093. if r != nil {
  1094. return common.Hash{}, nil, r
  1095. }
  1096. }
  1097. s.snap, s.snapDestructs, s.snapAccounts, s.snapStorage = nil, nil, nil, nil
  1098. s.diffTries, s.diffCode = nil, nil
  1099. return root, s.diffLayer, nil
  1100. }
  1101. // Commit writes the state to the underlying in-memory trie database.
  1102. func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, *types.DiffLayer, error) {
  1103. if s.dbErr != nil {
  1104. return common.Hash{}, nil, fmt.Errorf("commit aborted due to earlier error: %v", s.dbErr)
  1105. }
  1106. // Finalize any pending changes and merge everything into the tries
  1107. root := s.IntermediateRoot(deleteEmptyObjects)
  1108. if s.lightProcessed {
  1109. return s.LightCommit(root)
  1110. }
  1111. var diffLayer *types.DiffLayer
  1112. if s.snap != nil {
  1113. diffLayer = &types.DiffLayer{}
  1114. }
  1115. commitFuncs := []func() error{
  1116. func() error {
  1117. // Commit objects to the trie, measuring the elapsed time
  1118. tasks := make(chan func(batch ethdb.KeyValueWriter))
  1119. taskResults := make(chan error, len(s.stateObjectsDirty))
  1120. tasksNum := 0
  1121. finishCh := make(chan struct{})
  1122. threads := gopool.Threads(len(s.stateObjectsDirty))
  1123. wg := sync.WaitGroup{}
  1124. for i := 0; i < threads; i++ {
  1125. wg.Add(1)
  1126. go func() {
  1127. defer wg.Done()
  1128. codeWriter := s.db.TrieDB().DiskDB().NewBatch()
  1129. for {
  1130. select {
  1131. case task := <-tasks:
  1132. task(codeWriter)
  1133. case <-finishCh:
  1134. if codeWriter.ValueSize() > 0 {
  1135. if err := codeWriter.Write(); err != nil {
  1136. log.Crit("Failed to commit dirty codes", "error", err)
  1137. }
  1138. }
  1139. return
  1140. }
  1141. }
  1142. }()
  1143. }
  1144. if s.snap != nil {
  1145. for addr := range s.stateObjectsDirty {
  1146. if obj := s.stateObjects[addr]; !obj.deleted {
  1147. if obj.code != nil && obj.dirtyCode {
  1148. diffLayer.Codes = append(diffLayer.Codes, types.DiffCode{
  1149. Hash: common.BytesToHash(obj.CodeHash()),
  1150. Code: obj.code,
  1151. })
  1152. }
  1153. }
  1154. }
  1155. }
  1156. for addr := range s.stateObjectsDirty {
  1157. if obj := s.stateObjects[addr]; !obj.deleted {
  1158. // Write any contract code associated with the state object
  1159. tasks <- func(codeWriter ethdb.KeyValueWriter) {
  1160. if obj.code != nil && obj.dirtyCode {
  1161. rawdb.WriteCode(codeWriter, common.BytesToHash(obj.CodeHash()), obj.code)
  1162. obj.dirtyCode = false
  1163. }
  1164. // Write any storage changes in the state object to its storage trie
  1165. if err := obj.CommitTrie(s.db); err != nil {
  1166. taskResults <- err
  1167. }
  1168. taskResults <- nil
  1169. }
  1170. tasksNum++
  1171. }
  1172. }
  1173. for i := 0; i < tasksNum; i++ {
  1174. err := <-taskResults
  1175. if err != nil {
  1176. close(finishCh)
  1177. return err
  1178. }
  1179. }
  1180. close(finishCh)
  1181. if len(s.stateObjectsDirty) > 0 {
  1182. s.stateObjectsDirty = make(map[common.Address]struct{}, len(s.stateObjectsDirty)/2)
  1183. }
  1184. // Write the account trie changes, measuing the amount of wasted time
  1185. var start time.Time
  1186. if metrics.EnabledExpensive {
  1187. start = time.Now()
  1188. }
  1189. // The onleaf func is called _serially_, so we can reuse the same account
  1190. // for unmarshalling every time.
  1191. var account Account
  1192. root, err := s.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
  1193. if err := rlp.DecodeBytes(leaf, &account); err != nil {
  1194. return nil
  1195. }
  1196. if account.Root != emptyRoot {
  1197. s.db.TrieDB().Reference(account.Root, parent)
  1198. }
  1199. return nil
  1200. })
  1201. if err != nil {
  1202. return err
  1203. }
  1204. if metrics.EnabledExpensive {
  1205. s.AccountCommits += time.Since(start)
  1206. }
  1207. if root != emptyRoot {
  1208. s.db.CacheAccount(root, s.trie)
  1209. }
  1210. wg.Wait()
  1211. return nil
  1212. },
  1213. func() error {
  1214. // If snapshotting is enabled, update the snapshot tree with this new version
  1215. if s.snap != nil {
  1216. if metrics.EnabledExpensive {
  1217. defer func(start time.Time) { s.SnapshotCommits += time.Since(start) }(time.Now())
  1218. }
  1219. // Only update if there's a state transition (skip empty Clique blocks)
  1220. if parent := s.snap.Root(); parent != root {
  1221. if err := s.snaps.Update(root, parent, s.snapDestructs, s.snapAccounts, s.snapStorage); err != nil {
  1222. log.Warn("Failed to update snapshot tree", "from", parent, "to", root, "err", err)
  1223. }
  1224. // Keep n diff layers in the memory
  1225. // - head layer is paired with HEAD state
  1226. // - head-1 layer is paired with HEAD-1 state
  1227. // - head-(n-1) layer(bottom-most diff layer) is paired with HEAD-(n-1)state
  1228. if err := s.snaps.Cap(root, s.snaps.CapLimit()); err != nil {
  1229. log.Warn("Failed to cap snapshot tree", "root", root, "layers", s.snaps.CapLimit(), "err", err)
  1230. }
  1231. }
  1232. }
  1233. return nil
  1234. },
  1235. func() error {
  1236. if s.snap != nil {
  1237. diffLayer.Destructs, diffLayer.Accounts, diffLayer.Storages = s.SnapToDiffLayer()
  1238. }
  1239. return nil
  1240. },
  1241. }
  1242. commitRes := make(chan error, len(commitFuncs))
  1243. for _, f := range commitFuncs {
  1244. tmpFunc := f
  1245. go func() {
  1246. commitRes <- tmpFunc()
  1247. }()
  1248. }
  1249. for i := 0; i < len(commitFuncs); i++ {
  1250. r := <-commitRes
  1251. if r != nil {
  1252. return common.Hash{}, nil, r
  1253. }
  1254. }
  1255. s.snap, s.snapDestructs, s.snapAccounts, s.snapStorage = nil, nil, nil, nil
  1256. return root, diffLayer, nil
  1257. }
  1258. func (s *StateDB) DiffLayerToSnap(diffLayer *types.DiffLayer) (map[common.Address]struct{}, map[common.Address][]byte, map[common.Address]map[string][]byte, error) {
  1259. snapDestructs := make(map[common.Address]struct{})
  1260. snapAccounts := make(map[common.Address][]byte)
  1261. snapStorage := make(map[common.Address]map[string][]byte)
  1262. for _, des := range diffLayer.Destructs {
  1263. snapDestructs[des] = struct{}{}
  1264. }
  1265. for _, account := range diffLayer.Accounts {
  1266. snapAccounts[account.Account] = account.Blob
  1267. }
  1268. for _, storage := range diffLayer.Storages {
  1269. // should never happen
  1270. if len(storage.Keys) != len(storage.Vals) {
  1271. return nil, nil, nil, errors.New("invalid diffLayer: length of keys and values mismatch")
  1272. }
  1273. snapStorage[storage.Account] = make(map[string][]byte, len(storage.Keys))
  1274. n := len(storage.Keys)
  1275. for i := 0; i < n; i++ {
  1276. snapStorage[storage.Account][storage.Keys[i]] = storage.Vals[i]
  1277. }
  1278. }
  1279. return snapDestructs, snapAccounts, snapStorage, nil
  1280. }
  1281. func (s *StateDB) SnapToDiffLayer() ([]common.Address, []types.DiffAccount, []types.DiffStorage) {
  1282. destructs := make([]common.Address, 0, len(s.snapDestructs))
  1283. for account := range s.snapDestructs {
  1284. destructs = append(destructs, account)
  1285. }
  1286. accounts := make([]types.DiffAccount, 0, len(s.snapAccounts))
  1287. for accountHash, account := range s.snapAccounts {
  1288. accounts = append(accounts, types.DiffAccount{
  1289. Account: accountHash,
  1290. Blob: account,
  1291. })
  1292. }
  1293. storages := make([]types.DiffStorage, 0, len(s.snapStorage))
  1294. for accountHash, storage := range s.snapStorage {
  1295. keys := make([]string, 0, len(storage))
  1296. values := make([][]byte, 0, len(storage))
  1297. for k, v := range storage {
  1298. keys = append(keys, k)
  1299. values = append(values, v)
  1300. }
  1301. storages = append(storages, types.DiffStorage{
  1302. Account: accountHash,
  1303. Keys: keys,
  1304. Vals: values,
  1305. })
  1306. }
  1307. return destructs, accounts, storages
  1308. }
  1309. // PrepareAccessList handles the preparatory steps for executing a state transition with
  1310. // regards to both EIP-2929 and EIP-2930:
  1311. //
  1312. // - Add sender to access list (2929)
  1313. // - Add destination to access list (2929)
  1314. // - Add precompiles to access list (2929)
  1315. // - Add the contents of the optional tx access list (2930)
  1316. //
  1317. // This method should only be called if Yolov3/Berlin/2929+2930 is applicable at the current number.
  1318. func (s *StateDB) PrepareAccessList(sender common.Address, dst *common.Address, precompiles []common.Address, list types.AccessList) {
  1319. s.AddAddressToAccessList(sender)
  1320. if dst != nil {
  1321. s.AddAddressToAccessList(*dst)
  1322. // If it's a create-tx, the destination will be added inside evm.create
  1323. }
  1324. for _, addr := range precompiles {
  1325. s.AddAddressToAccessList(addr)
  1326. }
  1327. for _, el := range list {
  1328. s.AddAddressToAccessList(el.Address)
  1329. for _, key := range el.StorageKeys {
  1330. s.AddSlotToAccessList(el.Address, key)
  1331. }
  1332. }
  1333. }
  1334. // AddAddressToAccessList adds the given address to the access list
  1335. func (s *StateDB) AddAddressToAccessList(addr common.Address) {
  1336. if s.accessList == nil {
  1337. s.accessList = newAccessList()
  1338. }
  1339. if s.accessList.AddAddress(addr) {
  1340. s.journal.append(accessListAddAccountChange{&addr})
  1341. }
  1342. }
  1343. // AddSlotToAccessList adds the given (address, slot)-tuple to the access list
  1344. func (s *StateDB) AddSlotToAccessList(addr common.Address, slot common.Hash) {
  1345. if s.accessList == nil {
  1346. s.accessList = newAccessList()
  1347. }
  1348. addrMod, slotMod := s.accessList.AddSlot(addr, slot)
  1349. if addrMod {
  1350. // In practice, this should not happen, since there is no way to enter the
  1351. // scope of 'address' without having the 'address' become already added
  1352. // to the access list (via call-variant, create, etc).
  1353. // Better safe than sorry, though
  1354. s.journal.append(accessListAddAccountChange{&addr})
  1355. }
  1356. if slotMod {
  1357. s.journal.append(accessListAddSlotChange{
  1358. address: &addr,
  1359. slot: &slot,
  1360. })
  1361. }
  1362. }
  1363. // AddressInAccessList returns true if the given address is in the access list.
  1364. func (s *StateDB) AddressInAccessList(addr common.Address) bool {
  1365. if s.accessList == nil {
  1366. return false
  1367. }
  1368. return s.accessList.ContainsAddress(addr)
  1369. }
  1370. // SlotInAccessList returns true if the given (address, slot)-tuple is in the access list.
  1371. func (s *StateDB) SlotInAccessList(addr common.Address, slot common.Hash) (addressPresent bool, slotPresent bool) {
  1372. if s.accessList == nil {
  1373. return false, false
  1374. }
  1375. return s.accessList.Contains(addr, slot)
  1376. }