conversion.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // Copyright 2020 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"
  23. "runtime"
  24. "sync"
  25. "time"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/core/rawdb"
  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. // trieKV represents a trie key-value pair
  34. type trieKV struct {
  35. key common.Hash
  36. value []byte
  37. }
  38. type (
  39. // trieGeneratorFn is the interface of trie generation which can
  40. // be implemented by different trie algorithm.
  41. trieGeneratorFn func(db ethdb.KeyValueWriter, in chan (trieKV), out chan (common.Hash))
  42. // leafCallbackFn is the callback invoked at the leaves of the trie,
  43. // returns the subtrie root with the specified subtrie identifier.
  44. leafCallbackFn func(db ethdb.KeyValueWriter, accountHash, codeHash common.Hash, stat *generateStats) (common.Hash, error)
  45. )
  46. // GenerateAccountTrieRoot takes an account iterator and reproduces the root hash.
  47. func GenerateAccountTrieRoot(it AccountIterator) (common.Hash, error) {
  48. return generateTrieRoot(nil, it, common.Hash{}, stackTrieGenerate, nil, newGenerateStats(), true)
  49. }
  50. // GenerateStorageTrieRoot takes a storage iterator and reproduces the root hash.
  51. func GenerateStorageTrieRoot(account common.Hash, it StorageIterator) (common.Hash, error) {
  52. return generateTrieRoot(nil, it, account, stackTrieGenerate, nil, newGenerateStats(), true)
  53. }
  54. // GenerateTrie takes the whole snapshot tree as the input, traverses all the
  55. // accounts as well as the corresponding storages and regenerate the whole state
  56. // (account trie + all storage tries).
  57. func GenerateTrie(snaptree *Tree, root common.Hash, src ethdb.Database, dst ethdb.KeyValueWriter) error {
  58. // Traverse all state by snapshot, re-generate the whole state trie
  59. acctIt, err := snaptree.AccountIterator(root, common.Hash{})
  60. if err != nil {
  61. return err // The required snapshot might not exist.
  62. }
  63. defer acctIt.Release()
  64. got, err := generateTrieRoot(dst, acctIt, common.Hash{}, stackTrieGenerate, func(dst ethdb.KeyValueWriter, accountHash, codeHash common.Hash, stat *generateStats) (common.Hash, error) {
  65. // Migrate the code first, commit the contract code into the tmp db.
  66. if codeHash != emptyCode {
  67. code := rawdb.ReadCode(src, codeHash)
  68. if len(code) == 0 {
  69. return common.Hash{}, errors.New("failed to read contract code")
  70. }
  71. rawdb.WriteCode(dst, codeHash, code)
  72. }
  73. // Then migrate all storage trie nodes into the tmp db.
  74. storageIt, err := snaptree.StorageIterator(root, accountHash, common.Hash{})
  75. if err != nil {
  76. return common.Hash{}, err
  77. }
  78. defer storageIt.Release()
  79. hash, err := generateTrieRoot(dst, storageIt, accountHash, stackTrieGenerate, nil, stat, false)
  80. if err != nil {
  81. return common.Hash{}, err
  82. }
  83. return hash, nil
  84. }, newGenerateStats(), true)
  85. if err != nil {
  86. return err
  87. }
  88. if got != root {
  89. return fmt.Errorf("state root hash mismatch: got %x, want %x", got, root)
  90. }
  91. return nil
  92. }
  93. // generateStats is a collection of statistics gathered by the trie generator
  94. // for logging purposes.
  95. type generateStats struct {
  96. head common.Hash
  97. start time.Time
  98. accounts uint64 // Number of accounts done (including those being crawled)
  99. slots uint64 // Number of storage slots done (including those being crawled)
  100. slotsStart map[common.Hash]time.Time // Start time for account slot crawling
  101. slotsHead map[common.Hash]common.Hash // Slot head for accounts being crawled
  102. lock sync.RWMutex
  103. }
  104. // newGenerateStats creates a new generator stats.
  105. func newGenerateStats() *generateStats {
  106. return &generateStats{
  107. slotsStart: make(map[common.Hash]time.Time),
  108. slotsHead: make(map[common.Hash]common.Hash),
  109. start: time.Now(),
  110. }
  111. }
  112. // progressAccounts updates the generator stats for the account range.
  113. func (stat *generateStats) progressAccounts(account common.Hash, done uint64) {
  114. stat.lock.Lock()
  115. defer stat.lock.Unlock()
  116. stat.accounts += done
  117. stat.head = account
  118. }
  119. // finishAccounts updates the gemerator stats for the finished account range.
  120. func (stat *generateStats) finishAccounts(done uint64) {
  121. stat.lock.Lock()
  122. defer stat.lock.Unlock()
  123. stat.accounts += done
  124. }
  125. // progressContract updates the generator stats for a specific in-progress contract.
  126. func (stat *generateStats) progressContract(account common.Hash, slot common.Hash, done uint64) {
  127. stat.lock.Lock()
  128. defer stat.lock.Unlock()
  129. stat.slots += done
  130. stat.slotsHead[account] = slot
  131. if _, ok := stat.slotsStart[account]; !ok {
  132. stat.slotsStart[account] = time.Now()
  133. }
  134. }
  135. // finishContract updates the generator stats for a specific just-finished contract.
  136. func (stat *generateStats) finishContract(account common.Hash, done uint64) {
  137. stat.lock.Lock()
  138. defer stat.lock.Unlock()
  139. stat.slots += done
  140. delete(stat.slotsHead, account)
  141. delete(stat.slotsStart, account)
  142. }
  143. // report prints the cumulative progress statistic smartly.
  144. func (stat *generateStats) report() {
  145. stat.lock.RLock()
  146. defer stat.lock.RUnlock()
  147. ctx := []interface{}{
  148. "accounts", stat.accounts,
  149. "slots", stat.slots,
  150. "elapsed", common.PrettyDuration(time.Since(stat.start)),
  151. }
  152. if stat.accounts > 0 {
  153. // If there's progress on the account trie, estimate the time to finish crawling it
  154. if done := binary.BigEndian.Uint64(stat.head[:8]) / stat.accounts; done > 0 {
  155. var (
  156. left = (math.MaxUint64 - binary.BigEndian.Uint64(stat.head[:8])) / stat.accounts
  157. speed = done/uint64(time.Since(stat.start)/time.Millisecond+1) + 1 // +1s to avoid division by zero
  158. eta = time.Duration(left/speed) * time.Millisecond
  159. )
  160. // If there are large contract crawls in progress, estimate their finish time
  161. for acc, head := range stat.slotsHead {
  162. start := stat.slotsStart[acc]
  163. if done := binary.BigEndian.Uint64(head[:8]); done > 0 {
  164. var (
  165. left = math.MaxUint64 - binary.BigEndian.Uint64(head[:8])
  166. speed = done/uint64(time.Since(start)/time.Millisecond+1) + 1 // +1s to avoid division by zero
  167. )
  168. // Override the ETA if larger than the largest until now
  169. if slotETA := time.Duration(left/speed) * time.Millisecond; eta < slotETA {
  170. eta = slotETA
  171. }
  172. }
  173. }
  174. ctx = append(ctx, []interface{}{
  175. "eta", common.PrettyDuration(eta),
  176. }...)
  177. }
  178. }
  179. log.Info("Iterating state snapshot", ctx...)
  180. }
  181. // reportDone prints the last log when the whole generation is finished.
  182. func (stat *generateStats) reportDone() {
  183. stat.lock.RLock()
  184. defer stat.lock.RUnlock()
  185. var ctx []interface{}
  186. ctx = append(ctx, []interface{}{"accounts", stat.accounts}...)
  187. if stat.slots != 0 {
  188. ctx = append(ctx, []interface{}{"slots", stat.slots}...)
  189. }
  190. ctx = append(ctx, []interface{}{"elapsed", common.PrettyDuration(time.Since(stat.start))}...)
  191. log.Info("Iterated snapshot", ctx...)
  192. }
  193. // runReport periodically prints the progress information.
  194. func runReport(stats *generateStats, stop chan bool) {
  195. timer := time.NewTimer(0)
  196. defer timer.Stop()
  197. for {
  198. select {
  199. case <-timer.C:
  200. stats.report()
  201. timer.Reset(time.Second * 8)
  202. case success := <-stop:
  203. if success {
  204. stats.reportDone()
  205. }
  206. return
  207. }
  208. }
  209. }
  210. // generateTrieRoot generates the trie hash based on the snapshot iterator.
  211. // It can be used for generating account trie, storage trie or even the
  212. // whole state which connects the accounts and the corresponding storages.
  213. func generateTrieRoot(db ethdb.KeyValueWriter, it Iterator, account common.Hash, generatorFn trieGeneratorFn, leafCallback leafCallbackFn, stats *generateStats, report bool) (common.Hash, error) {
  214. var (
  215. in = make(chan trieKV) // chan to pass leaves
  216. out = make(chan common.Hash, 1) // chan to collect result
  217. stoplog = make(chan bool, 1) // 1-size buffer, works when logging is not enabled
  218. wg sync.WaitGroup
  219. )
  220. // Spin up a go-routine for trie hash re-generation
  221. wg.Add(1)
  222. go func() {
  223. defer wg.Done()
  224. generatorFn(db, in, out)
  225. }()
  226. // Spin up a go-routine for progress logging
  227. if report && stats != nil {
  228. wg.Add(1)
  229. go func() {
  230. defer wg.Done()
  231. runReport(stats, stoplog)
  232. }()
  233. }
  234. // Create a semaphore to assign tasks and collect results through. We'll pre-
  235. // fill it with nils, thus using the same channel for both limiting concurrent
  236. // processing and gathering results.
  237. threads := runtime.NumCPU()
  238. results := make(chan error, threads)
  239. for i := 0; i < threads; i++ {
  240. results <- nil // fill the semaphore
  241. }
  242. // stop is a helper function to shutdown the background threads
  243. // and return the re-generated trie hash.
  244. stop := func(fail error) (common.Hash, error) {
  245. close(in)
  246. result := <-out
  247. for i := 0; i < threads; i++ {
  248. if err := <-results; err != nil && fail == nil {
  249. fail = err
  250. }
  251. }
  252. stoplog <- fail == nil
  253. wg.Wait()
  254. return result, fail
  255. }
  256. var (
  257. logged = time.Now()
  258. processed = uint64(0)
  259. leaf trieKV
  260. )
  261. // Start to feed leaves
  262. for it.Next() {
  263. if account == (common.Hash{}) {
  264. var (
  265. err error
  266. fullData []byte
  267. )
  268. if leafCallback == nil {
  269. fullData, err = FullAccountRLP(it.(AccountIterator).Account())
  270. if err != nil {
  271. return stop(err)
  272. }
  273. } else {
  274. // Wait until the semaphore allows us to continue, aborting if
  275. // a sub-task failed
  276. if err := <-results; err != nil {
  277. results <- nil // stop will drain the results, add a noop back for this error we just consumed
  278. return stop(err)
  279. }
  280. // Fetch the next account and process it concurrently
  281. account, err := FullAccount(it.(AccountIterator).Account())
  282. if err != nil {
  283. return stop(err)
  284. }
  285. go func(hash common.Hash) {
  286. subroot, err := leafCallback(db, hash, common.BytesToHash(account.CodeHash), stats)
  287. if err != nil {
  288. results <- err
  289. return
  290. }
  291. if !bytes.Equal(account.Root, subroot.Bytes()) {
  292. results <- fmt.Errorf("invalid subroot(path %x), want %x, have %x", hash, account.Root, subroot)
  293. return
  294. }
  295. results <- nil
  296. }(it.Hash())
  297. fullData, err = rlp.EncodeToBytes(account)
  298. if err != nil {
  299. return stop(err)
  300. }
  301. }
  302. leaf = trieKV{it.Hash(), fullData}
  303. } else {
  304. leaf = trieKV{it.Hash(), common.CopyBytes(it.(StorageIterator).Slot())}
  305. }
  306. in <- leaf
  307. // Accumulate the generation statistic if it's required.
  308. processed++
  309. if time.Since(logged) > 3*time.Second && stats != nil {
  310. if account == (common.Hash{}) {
  311. stats.progressAccounts(it.Hash(), processed)
  312. } else {
  313. stats.progressContract(account, it.Hash(), processed)
  314. }
  315. logged, processed = time.Now(), 0
  316. }
  317. }
  318. // Commit the last part statistic.
  319. if processed > 0 && stats != nil {
  320. if account == (common.Hash{}) {
  321. stats.finishAccounts(processed)
  322. } else {
  323. stats.finishContract(account, processed)
  324. }
  325. }
  326. return stop(nil)
  327. }
  328. func stackTrieGenerate(db ethdb.KeyValueWriter, in chan trieKV, out chan common.Hash) {
  329. t := trie.NewStackTrie(db)
  330. for leaf := range in {
  331. t.TryUpdate(leaf.key[:], leaf.value)
  332. }
  333. var root common.Hash
  334. if db == nil {
  335. root = t.Hash()
  336. } else {
  337. root, _ = t.Commit()
  338. }
  339. out <- root
  340. }