utils.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2022 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. "fmt"
  20. "time"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core/rawdb"
  23. "github.com/ethereum/go-ethereum/ethdb"
  24. "github.com/ethereum/go-ethereum/log"
  25. "github.com/ethereum/go-ethereum/rlp"
  26. )
  27. // CheckDanglingStorage iterates the snap storage data, and verifies that all
  28. // storage also has corresponding account data.
  29. func CheckDanglingStorage(chaindb ethdb.KeyValueStore) error {
  30. if err := checkDanglingDiskStorage(chaindb); err != nil {
  31. log.Error("Database check error", "err", err)
  32. }
  33. return checkDanglingMemStorage(chaindb)
  34. }
  35. // checkDanglingDiskStorage checks if there is any 'dangling' storage data in the
  36. // disk-backed snapshot layer.
  37. func checkDanglingDiskStorage(chaindb ethdb.KeyValueStore) error {
  38. var (
  39. lastReport = time.Now()
  40. start = time.Now()
  41. lastKey []byte
  42. it = rawdb.NewKeyLengthIterator(chaindb.NewIterator(rawdb.SnapshotStoragePrefix, nil), 1+2*common.HashLength)
  43. )
  44. log.Info("Checking dangling snapshot disk storage")
  45. defer it.Release()
  46. for it.Next() {
  47. k := it.Key()
  48. accKey := k[1:33]
  49. if bytes.Equal(accKey, lastKey) {
  50. // No need to look up for every slot
  51. continue
  52. }
  53. lastKey = common.CopyBytes(accKey)
  54. if time.Since(lastReport) > time.Second*8 {
  55. log.Info("Iterating snap storage", "at", fmt.Sprintf("%#x", accKey), "elapsed", common.PrettyDuration(time.Since(start)))
  56. lastReport = time.Now()
  57. }
  58. if data := rawdb.ReadAccountSnapshot(chaindb, common.BytesToHash(accKey)); len(data) == 0 {
  59. log.Warn("Dangling storage - missing account", "account", fmt.Sprintf("%#x", accKey), "storagekey", fmt.Sprintf("%#x", k))
  60. return fmt.Errorf("dangling snapshot storage account %#x", accKey)
  61. }
  62. }
  63. log.Info("Verified the snapshot disk storage", "time", common.PrettyDuration(time.Since(start)), "err", it.Error())
  64. return nil
  65. }
  66. // checkDanglingMemStorage checks if there is any 'dangling' storage in the journalled
  67. // snapshot difflayers.
  68. func checkDanglingMemStorage(db ethdb.KeyValueStore) error {
  69. start := time.Now()
  70. log.Info("Checking dangling journalled storage")
  71. err := iterateJournal(db, func(pRoot, root common.Hash, destructs map[common.Hash]struct{}, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) error {
  72. for accHash := range storage {
  73. if _, ok := accounts[accHash]; !ok {
  74. log.Error("Dangling storage - missing account", "account", fmt.Sprintf("%#x", accHash), "root", root)
  75. }
  76. }
  77. return nil
  78. })
  79. if err != nil {
  80. log.Info("Failed to resolve snapshot journal", "err", err)
  81. return err
  82. }
  83. log.Info("Verified the snapshot journalled storage", "time", common.PrettyDuration(time.Since(start)))
  84. return nil
  85. }
  86. // CheckJournalAccount shows information about an account, from the disk layer and
  87. // up through the diff layers.
  88. func CheckJournalAccount(db ethdb.KeyValueStore, hash common.Hash) error {
  89. // Look up the disk layer first
  90. baseRoot := rawdb.ReadSnapshotRoot(db)
  91. fmt.Printf("Disklayer: Root: %x\n", baseRoot)
  92. if data := rawdb.ReadAccountSnapshot(db, hash); data != nil {
  93. account := new(Account)
  94. if err := rlp.DecodeBytes(data, account); err != nil {
  95. panic(err)
  96. }
  97. fmt.Printf("\taccount.nonce: %d\n", account.Nonce)
  98. fmt.Printf("\taccount.balance: %x\n", account.Balance)
  99. fmt.Printf("\taccount.root: %x\n", account.Root)
  100. fmt.Printf("\taccount.codehash: %x\n", account.CodeHash)
  101. }
  102. // Check storage
  103. {
  104. it := rawdb.NewKeyLengthIterator(db.NewIterator(append(rawdb.SnapshotStoragePrefix, hash.Bytes()...), nil), 1+2*common.HashLength)
  105. fmt.Printf("\tStorage:\n")
  106. for it.Next() {
  107. slot := it.Key()[33:]
  108. fmt.Printf("\t\t%x: %x\n", slot, it.Value())
  109. }
  110. it.Release()
  111. }
  112. var depth = 0
  113. return iterateJournal(db, func(pRoot, root common.Hash, destructs map[common.Hash]struct{}, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) error {
  114. _, a := accounts[hash]
  115. _, b := destructs[hash]
  116. _, c := storage[hash]
  117. depth++
  118. if !a && !b && !c {
  119. return nil
  120. }
  121. fmt.Printf("Disklayer+%d: Root: %x, parent %x\n", depth, root, pRoot)
  122. if data, ok := accounts[hash]; ok {
  123. account := new(Account)
  124. if err := rlp.DecodeBytes(data, account); err != nil {
  125. panic(err)
  126. }
  127. fmt.Printf("\taccount.nonce: %d\n", account.Nonce)
  128. fmt.Printf("\taccount.balance: %x\n", account.Balance)
  129. fmt.Printf("\taccount.root: %x\n", account.Root)
  130. fmt.Printf("\taccount.codehash: %x\n", account.CodeHash)
  131. }
  132. if _, ok := destructs[hash]; ok {
  133. fmt.Printf("\t Destructed!")
  134. }
  135. if data, ok := storage[hash]; ok {
  136. fmt.Printf("\tStorage\n")
  137. for k, v := range data {
  138. fmt.Printf("\t\t%x: %x\n", k, v)
  139. }
  140. }
  141. return nil
  142. })
  143. }