accessors_snapshot.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 rawdb
  17. import (
  18. "encoding/binary"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/ethdb"
  21. "github.com/ethereum/go-ethereum/log"
  22. )
  23. // ReadSnapshotDisabled retrieves if the snapshot maintenance is disabled.
  24. func ReadSnapshotDisabled(db ethdb.KeyValueReader) bool {
  25. disabled, _ := db.Has(snapshotDisabledKey)
  26. return disabled
  27. }
  28. // WriteSnapshotDisabled stores the snapshot pause flag.
  29. func WriteSnapshotDisabled(db ethdb.KeyValueWriter) {
  30. if err := db.Put(snapshotDisabledKey, []byte("42")); err != nil {
  31. log.Crit("Failed to store snapshot disabled flag", "err", err)
  32. }
  33. }
  34. // DeleteSnapshotDisabled deletes the flag keeping the snapshot maintenance disabled.
  35. func DeleteSnapshotDisabled(db ethdb.KeyValueWriter) {
  36. if err := db.Delete(snapshotDisabledKey); err != nil {
  37. log.Crit("Failed to remove snapshot disabled flag", "err", err)
  38. }
  39. }
  40. // ReadSnapshotRoot retrieves the root of the block whose state is contained in
  41. // the persisted snapshot.
  42. func ReadSnapshotRoot(db ethdb.KeyValueReader) common.Hash {
  43. data, _ := db.Get(snapshotRootKey)
  44. if len(data) != common.HashLength {
  45. return common.Hash{}
  46. }
  47. return common.BytesToHash(data)
  48. }
  49. // WriteSnapshotRoot stores the root of the block whose state is contained in
  50. // the persisted snapshot.
  51. func WriteSnapshotRoot(db ethdb.KeyValueWriter, root common.Hash) {
  52. if err := db.Put(snapshotRootKey, root[:]); err != nil {
  53. log.Crit("Failed to store snapshot root", "err", err)
  54. }
  55. }
  56. // DeleteSnapshotRoot deletes the hash of the block whose state is contained in
  57. // the persisted snapshot. Since snapshots are not immutable, this method can
  58. // be used during updates, so a crash or failure will mark the entire snapshot
  59. // invalid.
  60. func DeleteSnapshotRoot(db ethdb.KeyValueWriter) {
  61. if err := db.Delete(snapshotRootKey); err != nil {
  62. log.Crit("Failed to remove snapshot root", "err", err)
  63. }
  64. }
  65. // ReadAccountSnapshot retrieves the snapshot entry of an account trie leaf.
  66. func ReadAccountSnapshot(db ethdb.KeyValueReader, hash common.Hash) []byte {
  67. data, _ := db.Get(accountSnapshotKey(hash))
  68. return data
  69. }
  70. // WriteAccountSnapshot stores the snapshot entry of an account trie leaf.
  71. func WriteAccountSnapshot(db ethdb.KeyValueWriter, hash common.Hash, entry []byte) {
  72. if err := db.Put(accountSnapshotKey(hash), entry); err != nil {
  73. log.Crit("Failed to store account snapshot", "err", err)
  74. }
  75. }
  76. // DeleteAccountSnapshot removes the snapshot entry of an account trie leaf.
  77. func DeleteAccountSnapshot(db ethdb.KeyValueWriter, hash common.Hash) {
  78. if err := db.Delete(accountSnapshotKey(hash)); err != nil {
  79. log.Crit("Failed to delete account snapshot", "err", err)
  80. }
  81. }
  82. // ReadStorageSnapshot retrieves the snapshot entry of an storage trie leaf.
  83. func ReadStorageSnapshot(db ethdb.KeyValueReader, accountHash, storageHash common.Hash) []byte {
  84. data, _ := db.Get(storageSnapshotKey(accountHash, storageHash))
  85. return data
  86. }
  87. // WriteStorageSnapshot stores the snapshot entry of an storage trie leaf.
  88. func WriteStorageSnapshot(db ethdb.KeyValueWriter, accountHash, storageHash common.Hash, entry []byte) {
  89. if err := db.Put(storageSnapshotKey(accountHash, storageHash), entry); err != nil {
  90. log.Crit("Failed to store storage snapshot", "err", err)
  91. }
  92. }
  93. // DeleteStorageSnapshot removes the snapshot entry of an storage trie leaf.
  94. func DeleteStorageSnapshot(db ethdb.KeyValueWriter, accountHash, storageHash common.Hash) {
  95. if err := db.Delete(storageSnapshotKey(accountHash, storageHash)); err != nil {
  96. log.Crit("Failed to delete storage snapshot", "err", err)
  97. }
  98. }
  99. // IterateStorageSnapshots returns an iterator for walking the entire storage
  100. // space of a specific account.
  101. func IterateStorageSnapshots(db ethdb.Iteratee, accountHash common.Hash) ethdb.Iterator {
  102. return db.NewIterator(storageSnapshotsKey(accountHash), nil)
  103. }
  104. // ReadSnapshotJournal retrieves the serialized in-memory diff layers saved at
  105. // the last shutdown. The blob is expected to be max a few 10s of megabytes.
  106. func ReadSnapshotJournal(db ethdb.KeyValueReader) []byte {
  107. data, _ := db.Get(snapshotJournalKey)
  108. return data
  109. }
  110. // WriteSnapshotJournal stores the serialized in-memory diff layers to save at
  111. // shutdown. The blob is expected to be max a few 10s of megabytes.
  112. func WriteSnapshotJournal(db ethdb.KeyValueWriter, journal []byte) {
  113. if err := db.Put(snapshotJournalKey, journal); err != nil {
  114. log.Crit("Failed to store snapshot journal", "err", err)
  115. }
  116. }
  117. // DeleteSnapshotJournal deletes the serialized in-memory diff layers saved at
  118. // the last shutdown
  119. func DeleteSnapshotJournal(db ethdb.KeyValueWriter) {
  120. if err := db.Delete(snapshotJournalKey); err != nil {
  121. log.Crit("Failed to remove snapshot journal", "err", err)
  122. }
  123. }
  124. // ReadSnapshotGenerator retrieves the serialized snapshot generator saved at
  125. // the last shutdown.
  126. func ReadSnapshotGenerator(db ethdb.KeyValueReader) []byte {
  127. data, _ := db.Get(snapshotGeneratorKey)
  128. return data
  129. }
  130. // WriteSnapshotGenerator stores the serialized snapshot generator to save at
  131. // shutdown.
  132. func WriteSnapshotGenerator(db ethdb.KeyValueWriter, generator []byte) {
  133. if err := db.Put(snapshotGeneratorKey, generator); err != nil {
  134. log.Crit("Failed to store snapshot generator", "err", err)
  135. }
  136. }
  137. // DeleteSnapshotGenerator deletes the serialized snapshot generator saved at
  138. // the last shutdown
  139. func DeleteSnapshotGenerator(db ethdb.KeyValueWriter) {
  140. if err := db.Delete(snapshotGeneratorKey); err != nil {
  141. log.Crit("Failed to remove snapshot generator", "err", err)
  142. }
  143. }
  144. // ReadSnapshotRecoveryNumber retrieves the block number of the last persisted
  145. // snapshot layer.
  146. func ReadSnapshotRecoveryNumber(db ethdb.KeyValueReader) *uint64 {
  147. data, _ := db.Get(snapshotRecoveryKey)
  148. if len(data) == 0 {
  149. return nil
  150. }
  151. if len(data) != 8 {
  152. return nil
  153. }
  154. number := binary.BigEndian.Uint64(data)
  155. return &number
  156. }
  157. // WriteSnapshotRecoveryNumber stores the block number of the last persisted
  158. // snapshot layer.
  159. func WriteSnapshotRecoveryNumber(db ethdb.KeyValueWriter, number uint64) {
  160. var buf [8]byte
  161. binary.BigEndian.PutUint64(buf[:], number)
  162. if err := db.Put(snapshotRecoveryKey, buf[:]); err != nil {
  163. log.Crit("Failed to store snapshot recovery number", "err", err)
  164. }
  165. }
  166. // DeleteSnapshotRecoveryNumber deletes the block number of the last persisted
  167. // snapshot layer.
  168. func DeleteSnapshotRecoveryNumber(db ethdb.KeyValueWriter) {
  169. if err := db.Delete(snapshotRecoveryKey); err != nil {
  170. log.Crit("Failed to remove snapshot recovery number", "err", err)
  171. }
  172. }
  173. // ReadSnapshotSyncStatus retrieves the serialized sync status saved at shutdown.
  174. func ReadSnapshotSyncStatus(db ethdb.KeyValueReader) []byte {
  175. data, err := db.Get(snapshotSyncStatusKey)
  176. if err != nil {
  177. log.Error("Error reading snapshot sync status", "err", err)
  178. }
  179. return data
  180. }
  181. // WriteSnapshotSyncStatus stores the serialized sync status to save at shutdown.
  182. func WriteSnapshotSyncStatus(db ethdb.KeyValueWriter, status []byte) {
  183. if err := db.Put(snapshotSyncStatusKey, status); err != nil {
  184. log.Crit("Failed to store snapshot sync status", "err", err)
  185. }
  186. }
  187. // DeleteSnapshotSyncStatus deletes the serialized sync status saved at the last
  188. // shutdown
  189. func DeleteSnapshotSyncStatus(db ethdb.KeyValueWriter) {
  190. if err := db.Delete(snapshotSyncStatusKey); err != nil {
  191. log.Crit("Failed to remove snapshot sync status", "err", err)
  192. }
  193. }