generate.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. // Copyright 2019 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 snapshot
  17. import (
  18. "bytes"
  19. "encoding/binary"
  20. "errors"
  21. "fmt"
  22. "math/big"
  23. "time"
  24. "github.com/VictoriaMetrics/fastcache"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/common/hexutil"
  27. "github.com/ethereum/go-ethereum/common/math"
  28. "github.com/ethereum/go-ethereum/core/rawdb"
  29. "github.com/ethereum/go-ethereum/crypto"
  30. "github.com/ethereum/go-ethereum/ethdb"
  31. "github.com/ethereum/go-ethereum/ethdb/memorydb"
  32. "github.com/ethereum/go-ethereum/log"
  33. "github.com/ethereum/go-ethereum/metrics"
  34. "github.com/ethereum/go-ethereum/rlp"
  35. "github.com/ethereum/go-ethereum/trie"
  36. )
  37. var (
  38. // emptyRoot is the known root hash of an empty trie.
  39. emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
  40. // emptyCode is the known hash of the empty EVM bytecode.
  41. emptyCode = crypto.Keccak256Hash(nil)
  42. // accountCheckRange is the upper limit of the number of accounts involved in
  43. // each range check. This is a value estimated based on experience. If this
  44. // value is too large, the failure rate of range prove will increase. Otherwise
  45. // the the value is too small, the efficiency of the state recovery will decrease.
  46. accountCheckRange = 128
  47. // storageCheckRange is the upper limit of the number of storage slots involved
  48. // in each range check. This is a value estimated based on experience. If this
  49. // value is too large, the failure rate of range prove will increase. Otherwise
  50. // the the value is too small, the efficiency of the state recovery will decrease.
  51. storageCheckRange = 1024
  52. // errMissingTrie is returned if the target trie is missing while the generation
  53. // is running. In this case the generation is aborted and wait the new signal.
  54. errMissingTrie = errors.New("missing trie")
  55. )
  56. // Metrics in generation
  57. var (
  58. snapGeneratedAccountMeter = metrics.NewRegisteredMeter("state/snapshot/generation/account/generated", nil)
  59. snapRecoveredAccountMeter = metrics.NewRegisteredMeter("state/snapshot/generation/account/recovered", nil)
  60. snapWipedAccountMeter = metrics.NewRegisteredMeter("state/snapshot/generation/account/wiped", nil)
  61. snapMissallAccountMeter = metrics.NewRegisteredMeter("state/snapshot/generation/account/missall", nil)
  62. snapGeneratedStorageMeter = metrics.NewRegisteredMeter("state/snapshot/generation/storage/generated", nil)
  63. snapRecoveredStorageMeter = metrics.NewRegisteredMeter("state/snapshot/generation/storage/recovered", nil)
  64. snapWipedStorageMeter = metrics.NewRegisteredMeter("state/snapshot/generation/storage/wiped", nil)
  65. snapMissallStorageMeter = metrics.NewRegisteredMeter("state/snapshot/generation/storage/missall", nil)
  66. snapSuccessfulRangeProofMeter = metrics.NewRegisteredMeter("state/snapshot/generation/proof/success", nil)
  67. snapFailedRangeProofMeter = metrics.NewRegisteredMeter("state/snapshot/generation/proof/failure", nil)
  68. // snapAccountProveCounter measures time spent on the account proving
  69. snapAccountProveCounter = metrics.NewRegisteredCounter("state/snapshot/generation/duration/account/prove", nil)
  70. // snapAccountTrieReadCounter measures time spent on the account trie iteration
  71. snapAccountTrieReadCounter = metrics.NewRegisteredCounter("state/snapshot/generation/duration/account/trieread", nil)
  72. // snapAccountSnapReadCounter measues time spent on the snapshot account iteration
  73. snapAccountSnapReadCounter = metrics.NewRegisteredCounter("state/snapshot/generation/duration/account/snapread", nil)
  74. // snapAccountWriteCounter measures time spent on writing/updating/deleting accounts
  75. snapAccountWriteCounter = metrics.NewRegisteredCounter("state/snapshot/generation/duration/account/write", nil)
  76. // snapStorageProveCounter measures time spent on storage proving
  77. snapStorageProveCounter = metrics.NewRegisteredCounter("state/snapshot/generation/duration/storage/prove", nil)
  78. // snapStorageTrieReadCounter measures time spent on the storage trie iteration
  79. snapStorageTrieReadCounter = metrics.NewRegisteredCounter("state/snapshot/generation/duration/storage/trieread", nil)
  80. // snapStorageSnapReadCounter measures time spent on the snapshot storage iteration
  81. snapStorageSnapReadCounter = metrics.NewRegisteredCounter("state/snapshot/generation/duration/storage/snapread", nil)
  82. // snapStorageWriteCounter measures time spent on writing/updating/deleting storages
  83. snapStorageWriteCounter = metrics.NewRegisteredCounter("state/snapshot/generation/duration/storage/write", nil)
  84. )
  85. // generatorStats is a collection of statistics gathered by the snapshot generator
  86. // for logging purposes.
  87. type generatorStats struct {
  88. origin uint64 // Origin prefix where generation started
  89. start time.Time // Timestamp when generation started
  90. accounts uint64 // Number of accounts indexed(generated or recovered)
  91. slots uint64 // Number of storage slots indexed(generated or recovered)
  92. storage common.StorageSize // Total account and storage slot size(generation or recovery)
  93. }
  94. // Log creates an contextual log with the given message and the context pulled
  95. // from the internally maintained statistics.
  96. func (gs *generatorStats) Log(msg string, root common.Hash, marker []byte) {
  97. var ctx []interface{}
  98. if root != (common.Hash{}) {
  99. ctx = append(ctx, []interface{}{"root", root}...)
  100. }
  101. // Figure out whether we're after or within an account
  102. switch len(marker) {
  103. case common.HashLength:
  104. ctx = append(ctx, []interface{}{"at", common.BytesToHash(marker)}...)
  105. case 2 * common.HashLength:
  106. ctx = append(ctx, []interface{}{
  107. "in", common.BytesToHash(marker[:common.HashLength]),
  108. "at", common.BytesToHash(marker[common.HashLength:]),
  109. }...)
  110. }
  111. // Add the usual measurements
  112. ctx = append(ctx, []interface{}{
  113. "accounts", gs.accounts,
  114. "slots", gs.slots,
  115. "storage", gs.storage,
  116. "elapsed", common.PrettyDuration(time.Since(gs.start)),
  117. }...)
  118. // Calculate the estimated indexing time based on current stats
  119. if len(marker) > 0 {
  120. if done := binary.BigEndian.Uint64(marker[:8]) - gs.origin; done > 0 {
  121. left := math.MaxUint64 - binary.BigEndian.Uint64(marker[:8])
  122. speed := done/uint64(time.Since(gs.start)/time.Millisecond+1) + 1 // +1s to avoid division by zero
  123. ctx = append(ctx, []interface{}{
  124. "eta", common.PrettyDuration(time.Duration(left/speed) * time.Millisecond),
  125. }...)
  126. }
  127. }
  128. log.Info(msg, ctx...)
  129. }
  130. // ClearSnapshotMarker sets the snapshot marker to zero, meaning that snapshots
  131. // are not usable.
  132. func ClearSnapshotMarker(diskdb ethdb.KeyValueStore) {
  133. batch := diskdb.NewBatch()
  134. journalProgress(batch, []byte{}, nil)
  135. if err := batch.Write(); err != nil {
  136. log.Crit("Failed to write initialized state marker", "err", err)
  137. }
  138. }
  139. // generateSnapshot regenerates a brand new snapshot based on an existing state
  140. // database and head block asynchronously. The snapshot is returned immediately
  141. // and generation is continued in the background until done.
  142. func generateSnapshot(diskdb ethdb.KeyValueStore, triedb *trie.Database, cache int, root common.Hash) *diskLayer {
  143. // Create a new disk layer with an initialized state marker at zero
  144. var (
  145. stats = &generatorStats{start: time.Now()}
  146. batch = diskdb.NewBatch()
  147. genMarker = []byte{} // Initialized but empty!
  148. )
  149. rawdb.WriteSnapshotRoot(batch, root)
  150. journalProgress(batch, genMarker, stats)
  151. if err := batch.Write(); err != nil {
  152. log.Crit("Failed to write initialized state marker", "err", err)
  153. }
  154. base := &diskLayer{
  155. diskdb: diskdb,
  156. triedb: triedb,
  157. root: root,
  158. cache: fastcache.New(cache * 1024 * 1024),
  159. genMarker: genMarker,
  160. genPending: make(chan struct{}),
  161. genAbort: make(chan chan *generatorStats),
  162. }
  163. go base.generate(stats)
  164. log.Debug("Start snapshot generation", "root", root)
  165. return base
  166. }
  167. // journalProgress persists the generator stats into the database to resume later.
  168. func journalProgress(db ethdb.KeyValueWriter, marker []byte, stats *generatorStats) {
  169. // Write out the generator marker. Note it's a standalone disk layer generator
  170. // which is not mixed with journal. It's ok if the generator is persisted while
  171. // journal is not.
  172. entry := journalGenerator{
  173. Done: marker == nil,
  174. Marker: marker,
  175. }
  176. if stats != nil {
  177. entry.Accounts = stats.accounts
  178. entry.Slots = stats.slots
  179. entry.Storage = uint64(stats.storage)
  180. }
  181. blob, err := rlp.EncodeToBytes(entry)
  182. if err != nil {
  183. panic(err) // Cannot happen, here to catch dev errors
  184. }
  185. var logstr string
  186. switch {
  187. case marker == nil:
  188. logstr = "done"
  189. case bytes.Equal(marker, []byte{}):
  190. logstr = "empty"
  191. case len(marker) == common.HashLength:
  192. logstr = fmt.Sprintf("%#x", marker)
  193. default:
  194. logstr = fmt.Sprintf("%#x:%#x", marker[:common.HashLength], marker[common.HashLength:])
  195. }
  196. log.Debug("Journalled generator progress", "progress", logstr)
  197. rawdb.WriteSnapshotGenerator(db, blob)
  198. }
  199. // proofResult contains the output of range proving which can be used
  200. // for further processing regardless if it is successful or not.
  201. type proofResult struct {
  202. keys [][]byte // The key set of all elements being iterated, even proving is failed
  203. vals [][]byte // The val set of all elements being iterated, even proving is failed
  204. diskMore bool // Set when the database has extra snapshot states since last iteration
  205. trieMore bool // Set when the trie has extra snapshot states(only meaningful for successful proving)
  206. proofErr error // Indicator whether the given state range is valid or not
  207. tr *trie.Trie // The trie, in case the trie was resolved by the prover (may be nil)
  208. }
  209. // valid returns the indicator that range proof is successful or not.
  210. func (result *proofResult) valid() bool {
  211. return result.proofErr == nil
  212. }
  213. // last returns the last verified element key regardless of whether the range proof is
  214. // successful or not. Nil is returned if nothing involved in the proving.
  215. func (result *proofResult) last() []byte {
  216. var last []byte
  217. if len(result.keys) > 0 {
  218. last = result.keys[len(result.keys)-1]
  219. }
  220. return last
  221. }
  222. // forEach iterates all the visited elements and applies the given callback on them.
  223. // The iteration is aborted if the callback returns non-nil error.
  224. func (result *proofResult) forEach(callback func(key []byte, val []byte) error) error {
  225. for i := 0; i < len(result.keys); i++ {
  226. key, val := result.keys[i], result.vals[i]
  227. if err := callback(key, val); err != nil {
  228. return err
  229. }
  230. }
  231. return nil
  232. }
  233. // proveRange proves the snapshot segment with particular prefix is "valid".
  234. // The iteration start point will be assigned if the iterator is restored from
  235. // the last interruption. Max will be assigned in order to limit the maximum
  236. // amount of data involved in each iteration.
  237. //
  238. // The proof result will be returned if the range proving is finished, otherwise
  239. // the error will be returned to abort the entire procedure.
  240. func (dl *diskLayer) proveRange(stats *generatorStats, root common.Hash, prefix []byte, kind string, origin []byte, max int, valueConvertFn func([]byte) ([]byte, error)) (*proofResult, error) {
  241. var (
  242. keys [][]byte
  243. vals [][]byte
  244. proof = rawdb.NewMemoryDatabase()
  245. diskMore = false
  246. )
  247. iter := dl.diskdb.NewIterator(prefix, origin)
  248. defer iter.Release()
  249. var start = time.Now()
  250. for iter.Next() {
  251. key := iter.Key()
  252. if len(key) != len(prefix)+common.HashLength {
  253. continue
  254. }
  255. if len(keys) == max {
  256. // Break if we've reached the max size, and signal that we're not
  257. // done yet.
  258. diskMore = true
  259. break
  260. }
  261. keys = append(keys, common.CopyBytes(key[len(prefix):]))
  262. if valueConvertFn == nil {
  263. vals = append(vals, common.CopyBytes(iter.Value()))
  264. } else {
  265. val, err := valueConvertFn(iter.Value())
  266. if err != nil {
  267. // Special case, the state data is corrupted (invalid slim-format account),
  268. // don't abort the entire procedure directly. Instead, let the fallback
  269. // generation to heal the invalid data.
  270. //
  271. // Here append the original value to ensure that the number of key and
  272. // value are the same.
  273. vals = append(vals, common.CopyBytes(iter.Value()))
  274. log.Error("Failed to convert account state data", "err", err)
  275. } else {
  276. vals = append(vals, val)
  277. }
  278. }
  279. }
  280. // Update metrics for database iteration and merkle proving
  281. if kind == "storage" {
  282. snapStorageSnapReadCounter.Inc(time.Since(start).Nanoseconds())
  283. } else {
  284. snapAccountSnapReadCounter.Inc(time.Since(start).Nanoseconds())
  285. }
  286. defer func(start time.Time) {
  287. if kind == "storage" {
  288. snapStorageProveCounter.Inc(time.Since(start).Nanoseconds())
  289. } else {
  290. snapAccountProveCounter.Inc(time.Since(start).Nanoseconds())
  291. }
  292. }(time.Now())
  293. // The snap state is exhausted, pass the entire key/val set for verification
  294. if origin == nil && !diskMore {
  295. stackTr := trie.NewStackTrie(nil)
  296. for i, key := range keys {
  297. stackTr.TryUpdate(key, vals[i])
  298. }
  299. if gotRoot := stackTr.Hash(); gotRoot != root {
  300. return &proofResult{
  301. keys: keys,
  302. vals: vals,
  303. proofErr: fmt.Errorf("wrong root: have %#x want %#x", gotRoot, root),
  304. }, nil
  305. }
  306. return &proofResult{keys: keys, vals: vals}, nil
  307. }
  308. // Snap state is chunked, generate edge proofs for verification.
  309. tr, err := trie.New(root, dl.triedb)
  310. if err != nil {
  311. stats.Log("Trie missing, state snapshotting paused", dl.root, dl.genMarker)
  312. return nil, errMissingTrie
  313. }
  314. // Firstly find out the key of last iterated element.
  315. var last []byte
  316. if len(keys) > 0 {
  317. last = keys[len(keys)-1]
  318. }
  319. // Generate the Merkle proofs for the first and last element
  320. if origin == nil {
  321. origin = common.Hash{}.Bytes()
  322. }
  323. if err := tr.Prove(origin, 0, proof); err != nil {
  324. log.Debug("Failed to prove range", "kind", kind, "origin", origin, "err", err)
  325. return &proofResult{
  326. keys: keys,
  327. vals: vals,
  328. diskMore: diskMore,
  329. proofErr: err,
  330. tr: tr,
  331. }, nil
  332. }
  333. if last != nil {
  334. if err := tr.Prove(last, 0, proof); err != nil {
  335. log.Debug("Failed to prove range", "kind", kind, "last", last, "err", err)
  336. return &proofResult{
  337. keys: keys,
  338. vals: vals,
  339. diskMore: diskMore,
  340. proofErr: err,
  341. tr: tr,
  342. }, nil
  343. }
  344. }
  345. // Verify the snapshot segment with range prover, ensure that all flat states
  346. // in this range correspond to merkle trie.
  347. _, _, _, cont, err := trie.VerifyRangeProof(root, origin, last, keys, vals, proof)
  348. return &proofResult{
  349. keys: keys,
  350. vals: vals,
  351. diskMore: diskMore,
  352. trieMore: cont,
  353. proofErr: err,
  354. tr: tr},
  355. nil
  356. }
  357. // onStateCallback is a function that is called by generateRange, when processing a range of
  358. // accounts or storage slots. For each element, the callback is invoked.
  359. // If 'delete' is true, then this element (and potential slots) needs to be deleted from the snapshot.
  360. // If 'write' is true, then this element needs to be updated with the 'val'.
  361. // If 'write' is false, then this element is already correct, and needs no update. However,
  362. // for accounts, the storage trie of the account needs to be checked.
  363. // The 'val' is the canonical encoding of the value (not the slim format for accounts)
  364. type onStateCallback func(key []byte, val []byte, write bool, delete bool) error
  365. // generateRange generates the state segment with particular prefix. Generation can
  366. // either verify the correctness of existing state through rangeproof and skip
  367. // generation, or iterate trie to regenerate state on demand.
  368. func (dl *diskLayer) generateRange(root common.Hash, prefix []byte, kind string, origin []byte, max int, stats *generatorStats, onState onStateCallback, valueConvertFn func([]byte) ([]byte, error)) (bool, []byte, error) {
  369. // Use range prover to check the validity of the flat state in the range
  370. result, err := dl.proveRange(stats, root, prefix, kind, origin, max, valueConvertFn)
  371. if err != nil {
  372. return false, nil, err
  373. }
  374. last := result.last()
  375. // Construct contextual logger
  376. logCtx := []interface{}{"kind", kind, "prefix", hexutil.Encode(prefix)}
  377. if len(origin) > 0 {
  378. logCtx = append(logCtx, "origin", hexutil.Encode(origin))
  379. }
  380. logger := log.New(logCtx...)
  381. // The range prover says the range is correct, skip trie iteration
  382. if result.valid() {
  383. snapSuccessfulRangeProofMeter.Mark(1)
  384. logger.Trace("Proved state range", "last", hexutil.Encode(last))
  385. // The verification is passed, process each state with the given
  386. // callback function. If this state represents a contract, the
  387. // corresponding storage check will be performed in the callback
  388. if err := result.forEach(func(key []byte, val []byte) error { return onState(key, val, false, false) }); err != nil {
  389. return false, nil, err
  390. }
  391. // Only abort the iteration when both database and trie are exhausted
  392. return !result.diskMore && !result.trieMore, last, nil
  393. }
  394. logger.Trace("Detected outdated state range", "last", hexutil.Encode(last), "err", result.proofErr)
  395. snapFailedRangeProofMeter.Mark(1)
  396. // Special case, the entire trie is missing. In the original trie scheme,
  397. // all the duplicated subtries will be filter out(only one copy of data
  398. // will be stored). While in the snapshot model, all the storage tries
  399. // belong to different contracts will be kept even they are duplicated.
  400. // Track it to a certain extent remove the noise data used for statistics.
  401. if origin == nil && last == nil {
  402. meter := snapMissallAccountMeter
  403. if kind == "storage" {
  404. meter = snapMissallStorageMeter
  405. }
  406. meter.Mark(1)
  407. }
  408. // We use the snap data to build up a cache which can be used by the
  409. // main account trie as a primary lookup when resolving hashes
  410. var snapNodeCache ethdb.KeyValueStore
  411. if len(result.keys) > 0 {
  412. snapNodeCache = memorydb.New()
  413. snapTrieDb := trie.NewDatabase(snapNodeCache)
  414. snapTrie, _ := trie.New(common.Hash{}, snapTrieDb)
  415. for i, key := range result.keys {
  416. snapTrie.Update(key, result.vals[i])
  417. }
  418. root, _ := snapTrie.Commit(nil)
  419. snapTrieDb.Commit(root, false, nil)
  420. }
  421. tr := result.tr
  422. if tr == nil {
  423. tr, err = trie.New(root, dl.triedb)
  424. if err != nil {
  425. stats.Log("Trie missing, state snapshotting paused", dl.root, dl.genMarker)
  426. return false, nil, errMissingTrie
  427. }
  428. }
  429. var (
  430. trieMore bool
  431. nodeIt = tr.NodeIterator(origin)
  432. iter = trie.NewIterator(nodeIt)
  433. kvkeys, kvvals = result.keys, result.vals
  434. // counters
  435. count = 0 // number of states delivered by iterator
  436. created = 0 // states created from the trie
  437. updated = 0 // states updated from the trie
  438. deleted = 0 // states not in trie, but were in snapshot
  439. untouched = 0 // states already correct
  440. // timers
  441. start = time.Now()
  442. internal time.Duration
  443. )
  444. nodeIt.AddResolver(snapNodeCache)
  445. for iter.Next() {
  446. if last != nil && bytes.Compare(iter.Key, last) > 0 {
  447. trieMore = true
  448. break
  449. }
  450. count++
  451. write := true
  452. created++
  453. for len(kvkeys) > 0 {
  454. if cmp := bytes.Compare(kvkeys[0], iter.Key); cmp < 0 {
  455. // delete the key
  456. istart := time.Now()
  457. if err := onState(kvkeys[0], nil, false, true); err != nil {
  458. return false, nil, err
  459. }
  460. kvkeys = kvkeys[1:]
  461. kvvals = kvvals[1:]
  462. deleted++
  463. internal += time.Since(istart)
  464. continue
  465. } else if cmp == 0 {
  466. // the snapshot key can be overwritten
  467. created--
  468. if write = !bytes.Equal(kvvals[0], iter.Value); write {
  469. updated++
  470. } else {
  471. untouched++
  472. }
  473. kvkeys = kvkeys[1:]
  474. kvvals = kvvals[1:]
  475. }
  476. break
  477. }
  478. istart := time.Now()
  479. if err := onState(iter.Key, iter.Value, write, false); err != nil {
  480. return false, nil, err
  481. }
  482. internal += time.Since(istart)
  483. }
  484. if iter.Err != nil {
  485. return false, nil, iter.Err
  486. }
  487. // Delete all stale snapshot states remaining
  488. istart := time.Now()
  489. for _, key := range kvkeys {
  490. if err := onState(key, nil, false, true); err != nil {
  491. return false, nil, err
  492. }
  493. deleted += 1
  494. }
  495. internal += time.Since(istart)
  496. // Update metrics for counting trie iteration
  497. if kind == "storage" {
  498. snapStorageTrieReadCounter.Inc((time.Since(start) - internal).Nanoseconds())
  499. } else {
  500. snapAccountTrieReadCounter.Inc((time.Since(start) - internal).Nanoseconds())
  501. }
  502. logger.Debug("Regenerated state range", "root", root, "last", hexutil.Encode(last),
  503. "count", count, "created", created, "updated", updated, "untouched", untouched, "deleted", deleted)
  504. // If there are either more trie items, or there are more snap items
  505. // (in the next segment), then we need to keep working
  506. return !trieMore && !result.diskMore, last, nil
  507. }
  508. // generate is a background thread that iterates over the state and storage tries,
  509. // constructing the state snapshot. All the arguments are purely for statistics
  510. // gathering and logging, since the method surfs the blocks as they arrive, often
  511. // being restarted.
  512. func (dl *diskLayer) generate(stats *generatorStats) {
  513. var (
  514. accMarker []byte
  515. accountRange = accountCheckRange
  516. )
  517. if len(dl.genMarker) > 0 { // []byte{} is the start, use nil for that
  518. // Always reset the initial account range as 1
  519. // whenever recover from the interruption.
  520. accMarker, accountRange = dl.genMarker[:common.HashLength], 1
  521. }
  522. var (
  523. batch = dl.diskdb.NewBatch()
  524. logged = time.Now()
  525. accOrigin = common.CopyBytes(accMarker)
  526. abort chan *generatorStats
  527. )
  528. stats.Log("Resuming state snapshot generation", dl.root, dl.genMarker)
  529. checkAndFlush := func(currentLocation []byte) error {
  530. select {
  531. case abort = <-dl.genAbort:
  532. default:
  533. }
  534. if batch.ValueSize() > ethdb.IdealBatchSize || abort != nil {
  535. // Flush out the batch anyway no matter it's empty or not.
  536. // It's possible that all the states are recovered and the
  537. // generation indeed makes progress.
  538. journalProgress(batch, currentLocation, stats)
  539. if err := batch.Write(); err != nil {
  540. return err
  541. }
  542. batch.Reset()
  543. dl.lock.Lock()
  544. dl.genMarker = currentLocation
  545. dl.lock.Unlock()
  546. if abort != nil {
  547. stats.Log("Aborting state snapshot generation", dl.root, currentLocation)
  548. return errors.New("aborted")
  549. }
  550. }
  551. if time.Since(logged) > 8*time.Second {
  552. stats.Log("Generating state snapshot", dl.root, currentLocation)
  553. logged = time.Now()
  554. }
  555. return nil
  556. }
  557. onAccount := func(key []byte, val []byte, write bool, delete bool) error {
  558. var (
  559. start = time.Now()
  560. accountHash = common.BytesToHash(key)
  561. )
  562. if delete {
  563. rawdb.DeleteAccountSnapshot(batch, accountHash)
  564. snapWipedAccountMeter.Mark(1)
  565. // Ensure that any previous snapshot storage values are cleared
  566. prefix := append(rawdb.SnapshotStoragePrefix, accountHash.Bytes()...)
  567. keyLen := len(rawdb.SnapshotStoragePrefix) + 2*common.HashLength
  568. if err := wipeKeyRange(dl.diskdb, "storage", prefix, nil, nil, keyLen, snapWipedStorageMeter, false); err != nil {
  569. return err
  570. }
  571. snapAccountWriteCounter.Inc(time.Since(start).Nanoseconds())
  572. return nil
  573. }
  574. // Retrieve the current account and flatten it into the internal format
  575. var acc struct {
  576. Nonce uint64
  577. Balance *big.Int
  578. Root common.Hash
  579. CodeHash []byte
  580. }
  581. if err := rlp.DecodeBytes(val, &acc); err != nil {
  582. log.Crit("Invalid account encountered during snapshot creation", "err", err)
  583. }
  584. // If the account is not yet in-progress, write it out
  585. if accMarker == nil || !bytes.Equal(accountHash[:], accMarker) {
  586. dataLen := len(val) // Approximate size, saves us a round of RLP-encoding
  587. if !write {
  588. if bytes.Equal(acc.CodeHash, emptyCode[:]) {
  589. dataLen -= 32
  590. }
  591. if acc.Root == emptyRoot {
  592. dataLen -= 32
  593. }
  594. snapRecoveredAccountMeter.Mark(1)
  595. } else {
  596. data := SlimAccountRLP(acc.Nonce, acc.Balance, acc.Root, acc.CodeHash)
  597. dataLen = len(data)
  598. rawdb.WriteAccountSnapshot(batch, accountHash, data)
  599. snapGeneratedAccountMeter.Mark(1)
  600. }
  601. stats.storage += common.StorageSize(1 + common.HashLength + dataLen)
  602. stats.accounts++
  603. }
  604. // If we've exceeded our batch allowance or termination was requested, flush to disk
  605. if err := checkAndFlush(accountHash[:]); err != nil {
  606. return err
  607. }
  608. // If the iterated account is the contract, create a further loop to
  609. // verify or regenerate the contract storage.
  610. if acc.Root == emptyRoot {
  611. // If the root is empty, we still need to ensure that any previous snapshot
  612. // storage values are cleared
  613. // TODO: investigate if this can be avoided, this will be very costly since it
  614. // affects every single EOA account
  615. // - Perhaps we can avoid if where codeHash is emptyCode
  616. prefix := append(rawdb.SnapshotStoragePrefix, accountHash.Bytes()...)
  617. keyLen := len(rawdb.SnapshotStoragePrefix) + 2*common.HashLength
  618. if err := wipeKeyRange(dl.diskdb, "storage", prefix, nil, nil, keyLen, snapWipedStorageMeter, false); err != nil {
  619. return err
  620. }
  621. snapAccountWriteCounter.Inc(time.Since(start).Nanoseconds())
  622. } else {
  623. snapAccountWriteCounter.Inc(time.Since(start).Nanoseconds())
  624. var storeMarker []byte
  625. if accMarker != nil && bytes.Equal(accountHash[:], accMarker) && len(dl.genMarker) > common.HashLength {
  626. storeMarker = dl.genMarker[common.HashLength:]
  627. }
  628. onStorage := func(key []byte, val []byte, write bool, delete bool) error {
  629. defer func(start time.Time) {
  630. snapStorageWriteCounter.Inc(time.Since(start).Nanoseconds())
  631. }(time.Now())
  632. if delete {
  633. rawdb.DeleteStorageSnapshot(batch, accountHash, common.BytesToHash(key))
  634. snapWipedStorageMeter.Mark(1)
  635. return nil
  636. }
  637. if write {
  638. rawdb.WriteStorageSnapshot(batch, accountHash, common.BytesToHash(key), val)
  639. snapGeneratedStorageMeter.Mark(1)
  640. } else {
  641. snapRecoveredStorageMeter.Mark(1)
  642. }
  643. stats.storage += common.StorageSize(1 + 2*common.HashLength + len(val))
  644. stats.slots++
  645. // If we've exceeded our batch allowance or termination was requested, flush to disk
  646. if err := checkAndFlush(append(accountHash[:], key...)); err != nil {
  647. return err
  648. }
  649. return nil
  650. }
  651. var storeOrigin = common.CopyBytes(storeMarker)
  652. for {
  653. exhausted, last, err := dl.generateRange(acc.Root, append(rawdb.SnapshotStoragePrefix, accountHash.Bytes()...), "storage", storeOrigin, storageCheckRange, stats, onStorage, nil)
  654. if err != nil {
  655. return err
  656. }
  657. if exhausted {
  658. break
  659. }
  660. if storeOrigin = increaseKey(last); storeOrigin == nil {
  661. break // special case, the last is 0xffffffff...fff
  662. }
  663. }
  664. }
  665. // Some account processed, unmark the marker
  666. accMarker = nil
  667. return nil
  668. }
  669. // Global loop for regerating the entire state trie + all layered storage tries.
  670. for {
  671. exhausted, last, err := dl.generateRange(dl.root, rawdb.SnapshotAccountPrefix, "account", accOrigin, accountRange, stats, onAccount, FullAccountRLP)
  672. // The procedure it aborted, either by external signal or internal error
  673. if err != nil {
  674. if abort == nil { // aborted by internal error, wait the signal
  675. abort = <-dl.genAbort
  676. }
  677. abort <- stats
  678. return
  679. }
  680. // Abort the procedure if the entire snapshot is generated
  681. if exhausted {
  682. break
  683. }
  684. if accOrigin = increaseKey(last); accOrigin == nil {
  685. break // special case, the last is 0xffffffff...fff
  686. }
  687. accountRange = accountCheckRange
  688. }
  689. // Snapshot fully generated, set the marker to nil.
  690. // Note even there is nothing to commit, persist the
  691. // generator anyway to mark the snapshot is complete.
  692. journalProgress(batch, nil, stats)
  693. if err := batch.Write(); err != nil {
  694. log.Error("Failed to flush batch", "err", err)
  695. abort = <-dl.genAbort
  696. abort <- stats
  697. return
  698. }
  699. batch.Reset()
  700. log.Info("Generated state snapshot", "accounts", stats.accounts, "slots", stats.slots,
  701. "storage", stats.storage, "elapsed", common.PrettyDuration(time.Since(stats.start)))
  702. dl.lock.Lock()
  703. dl.genMarker = nil
  704. close(dl.genPending)
  705. dl.lock.Unlock()
  706. // Someone will be looking for us, wait it out
  707. abort = <-dl.genAbort
  708. abort <- nil
  709. }
  710. // increaseKey increase the input key by one bit. Return nil if the entire
  711. // addition operation overflows,
  712. func increaseKey(key []byte) []byte {
  713. for i := len(key) - 1; i >= 0; i-- {
  714. key[i]++
  715. if key[i] != 0x0 {
  716. return key
  717. }
  718. }
  719. return nil
  720. }