generate.go 11 KB

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