accessors_snapshot.go 7.2 KB

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