generate.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. "math/big"
  21. "time"
  22. "github.com/VictoriaMetrics/fastcache"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/common/math"
  25. "github.com/ethereum/go-ethereum/core/rawdb"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. "github.com/ethereum/go-ethereum/ethdb"
  28. "github.com/ethereum/go-ethereum/log"
  29. "github.com/ethereum/go-ethereum/rlp"
  30. "github.com/ethereum/go-ethereum/trie"
  31. )
  32. var (
  33. // emptyRoot is the known root hash of an empty trie.
  34. emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
  35. // emptyCode is the known hash of the empty EVM bytecode.
  36. emptyCode = crypto.Keccak256Hash(nil)
  37. )
  38. // generatorStats is a collection of statistics gathered by the snapshot generator
  39. // for logging purposes.
  40. type generatorStats struct {
  41. wiping chan struct{} // Notification channel if wiping is in progress
  42. origin uint64 // Origin prefix where generation started
  43. start time.Time // Timestamp when generation started
  44. accounts uint64 // Number of accounts indexed
  45. slots uint64 // Number of storage slots indexed
  46. storage common.StorageSize // Account and storage slot size
  47. }
  48. // Log creates an contextual log with the given message and the context pulled
  49. // from the internally maintained statistics.
  50. func (gs *generatorStats) Log(msg string, marker []byte) {
  51. var ctx []interface{}
  52. // Figure out whether we're after or within an account
  53. switch len(marker) {
  54. case common.HashLength:
  55. ctx = append(ctx, []interface{}{"at", common.BytesToHash(marker)}...)
  56. case 2 * common.HashLength:
  57. ctx = append(ctx, []interface{}{
  58. "in", common.BytesToHash(marker[:common.HashLength]),
  59. "at", common.BytesToHash(marker[common.HashLength:]),
  60. }...)
  61. }
  62. // Add the usual measurements
  63. ctx = append(ctx, []interface{}{
  64. "accounts", gs.accounts,
  65. "slots", gs.slots,
  66. "storage", gs.storage,
  67. "elapsed", common.PrettyDuration(time.Since(gs.start)),
  68. }...)
  69. // Calculate the estimated indexing time based on current stats
  70. if len(marker) > 0 {
  71. if done := binary.BigEndian.Uint64(marker[:8]) - gs.origin; done > 0 {
  72. left := math.MaxUint64 - binary.BigEndian.Uint64(marker[:8])
  73. speed := done/uint64(time.Since(gs.start)/time.Millisecond+1) + 1 // +1s to avoid division by zero
  74. ctx = append(ctx, []interface{}{
  75. "eta", common.PrettyDuration(time.Duration(left/speed) * time.Millisecond),
  76. }...)
  77. }
  78. }
  79. log.Info(msg, ctx...)
  80. }
  81. // generateSnapshot regenerates a brand new snapshot based on an existing state
  82. // database and head block asynchronously. The snapshot is returned immediately
  83. // and generation is continued in the background until done.
  84. func generateSnapshot(diskdb ethdb.KeyValueStore, triedb *trie.Database, cache int, root common.Hash, wiper chan struct{}) *diskLayer {
  85. // Wipe any previously existing snapshot from the database if no wiper is
  86. // currenty in progress.
  87. if wiper == nil {
  88. wiper = wipeSnapshot(diskdb, true)
  89. }
  90. // Create a new disk layer with an initialized state marker at zero
  91. rawdb.WriteSnapshotRoot(diskdb, root)
  92. base := &diskLayer{
  93. diskdb: diskdb,
  94. triedb: triedb,
  95. root: root,
  96. cache: fastcache.New(cache * 1024 * 1024),
  97. genMarker: []byte{}, // Initialized but empty!
  98. genAbort: make(chan chan *generatorStats),
  99. }
  100. go base.generate(&generatorStats{wiping: wiper, start: time.Now()})
  101. return base
  102. }
  103. // generate is a background thread that iterates over the state and storage tries,
  104. // constructing the state snapshot. All the arguments are purely for statistics
  105. // gethering and logging, since the method surfs the blocks as they arrive, often
  106. // being restarted.
  107. func (dl *diskLayer) generate(stats *generatorStats) {
  108. // If a database wipe is in operation, wait until it's done
  109. if stats.wiping != nil {
  110. stats.Log("Wiper running, state snapshotting paused", dl.genMarker)
  111. select {
  112. // If wiper is done, resume normal mode of operation
  113. case <-stats.wiping:
  114. stats.wiping = nil
  115. stats.start = time.Now()
  116. // If generator was aboted during wipe, return
  117. case abort := <-dl.genAbort:
  118. abort <- stats
  119. return
  120. }
  121. }
  122. // Create an account and state iterator pointing to the current generator marker
  123. accTrie, err := trie.NewSecure(dl.root, dl.triedb)
  124. if err != nil {
  125. // The account trie is missing (GC), surf the chain until one becomes available
  126. stats.Log("Trie missing, state snapshotting paused", dl.genMarker)
  127. abort := <-dl.genAbort
  128. abort <- stats
  129. return
  130. }
  131. stats.Log("Resuming state snapshot generation", dl.genMarker)
  132. var accMarker []byte
  133. if len(dl.genMarker) > 0 { // []byte{} is the start, use nil for that
  134. accMarker = dl.genMarker[:common.HashLength]
  135. }
  136. accIt := trie.NewIterator(accTrie.NodeIterator(accMarker))
  137. batch := dl.diskdb.NewBatch()
  138. // Iterate from the previous marker and continue generating the state snapshot
  139. logged := time.Now()
  140. for accIt.Next() {
  141. // Retrieve the current account and flatten it into the internal format
  142. accountHash := common.BytesToHash(accIt.Key)
  143. var acc struct {
  144. Nonce uint64
  145. Balance *big.Int
  146. Root common.Hash
  147. CodeHash []byte
  148. }
  149. if err := rlp.DecodeBytes(accIt.Value, &acc); err != nil {
  150. log.Crit("Invalid account encountered during snapshot creation", "err", err)
  151. }
  152. data := AccountRLP(acc.Nonce, acc.Balance, acc.Root, acc.CodeHash)
  153. // If the account is not yet in-progress, write it out
  154. if accMarker == nil || !bytes.Equal(accountHash[:], accMarker) {
  155. rawdb.WriteAccountSnapshot(batch, accountHash, data)
  156. stats.storage += common.StorageSize(1 + common.HashLength + len(data))
  157. stats.accounts++
  158. }
  159. // If we've exceeded our batch allowance or termination was requested, flush to disk
  160. var abort chan *generatorStats
  161. select {
  162. case abort = <-dl.genAbort:
  163. default:
  164. }
  165. if batch.ValueSize() > ethdb.IdealBatchSize || abort != nil {
  166. // Only write and set the marker if we actually did something useful
  167. if batch.ValueSize() > 0 {
  168. batch.Write()
  169. batch.Reset()
  170. dl.lock.Lock()
  171. dl.genMarker = accountHash[:]
  172. dl.lock.Unlock()
  173. }
  174. if abort != nil {
  175. stats.Log("Aborting state snapshot generation", accountHash[:])
  176. abort <- stats
  177. return
  178. }
  179. }
  180. // If the account is in-progress, continue where we left off (otherwise iterate all)
  181. if acc.Root != emptyRoot {
  182. storeTrie, err := trie.NewSecure(acc.Root, dl.triedb)
  183. if err != nil {
  184. log.Crit("Storage trie inaccessible for snapshot generation", "err", err)
  185. }
  186. var storeMarker []byte
  187. if accMarker != nil && bytes.Equal(accountHash[:], accMarker) && len(dl.genMarker) > common.HashLength {
  188. storeMarker = dl.genMarker[common.HashLength:]
  189. }
  190. storeIt := trie.NewIterator(storeTrie.NodeIterator(storeMarker))
  191. for storeIt.Next() {
  192. rawdb.WriteStorageSnapshot(batch, accountHash, common.BytesToHash(storeIt.Key), storeIt.Value)
  193. stats.storage += common.StorageSize(1 + 2*common.HashLength + len(storeIt.Value))
  194. stats.slots++
  195. // If we've exceeded our batch allowance or termination was requested, flush to disk
  196. var abort chan *generatorStats
  197. select {
  198. case abort = <-dl.genAbort:
  199. default:
  200. }
  201. if batch.ValueSize() > ethdb.IdealBatchSize || abort != nil {
  202. // Only write and set the marker if we actually did something useful
  203. if batch.ValueSize() > 0 {
  204. batch.Write()
  205. batch.Reset()
  206. dl.lock.Lock()
  207. dl.genMarker = append(accountHash[:], storeIt.Key...)
  208. dl.lock.Unlock()
  209. }
  210. if abort != nil {
  211. stats.Log("Aborting state snapshot generation", append(accountHash[:], storeIt.Key...))
  212. abort <- stats
  213. return
  214. }
  215. }
  216. }
  217. }
  218. if time.Since(logged) > 8*time.Second {
  219. stats.Log("Generating state snapshot", accIt.Key)
  220. logged = time.Now()
  221. }
  222. // Some account processed, unmark the marker
  223. accMarker = nil
  224. }
  225. // Snapshot fully generated, set the marker to nil
  226. if batch.ValueSize() > 0 {
  227. batch.Write()
  228. }
  229. log.Info("Generated state snapshot", "accounts", stats.accounts, "slots", stats.slots,
  230. "storage", stats.storage, "elapsed", common.PrettyDuration(time.Since(stats.start)))
  231. dl.lock.Lock()
  232. dl.genMarker = nil
  233. dl.lock.Unlock()
  234. // Someone will be looking for us, wait it out
  235. abort := <-dl.genAbort
  236. abort <- nil
  237. }