accessors_snapshot.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "github.com/ethereum/go-ethereum/common"
  19. "github.com/ethereum/go-ethereum/ethdb"
  20. "github.com/ethereum/go-ethereum/log"
  21. )
  22. // ReadSnapshotRoot retrieves the root of the block whose state is contained in
  23. // the persisted snapshot.
  24. func ReadSnapshotRoot(db ethdb.KeyValueReader) common.Hash {
  25. data, _ := db.Get(snapshotRootKey)
  26. if len(data) != common.HashLength {
  27. return common.Hash{}
  28. }
  29. return common.BytesToHash(data)
  30. }
  31. // WriteSnapshotRoot stores the root of the block whose state is contained in
  32. // the persisted snapshot.
  33. func WriteSnapshotRoot(db ethdb.KeyValueWriter, root common.Hash) {
  34. if err := db.Put(snapshotRootKey, root[:]); err != nil {
  35. log.Crit("Failed to store snapshot root", "err", err)
  36. }
  37. }
  38. // DeleteSnapshotRoot deletes the hash of the block whose state is contained in
  39. // the persisted snapshot. Since snapshots are not immutable, this method can
  40. // be used during updates, so a crash or failure will mark the entire snapshot
  41. // invalid.
  42. func DeleteSnapshotRoot(db ethdb.KeyValueWriter) {
  43. if err := db.Delete(snapshotRootKey); err != nil {
  44. log.Crit("Failed to remove snapshot root", "err", err)
  45. }
  46. }
  47. // ReadAccountSnapshot retrieves the snapshot entry of an account trie leaf.
  48. func ReadAccountSnapshot(db ethdb.KeyValueReader, hash common.Hash) []byte {
  49. data, _ := db.Get(accountSnapshotKey(hash))
  50. return data
  51. }
  52. // WriteAccountSnapshot stores the snapshot entry of an account trie leaf.
  53. func WriteAccountSnapshot(db ethdb.KeyValueWriter, hash common.Hash, entry []byte) {
  54. if err := db.Put(accountSnapshotKey(hash), entry); err != nil {
  55. log.Crit("Failed to store account snapshot", "err", err)
  56. }
  57. }
  58. // DeleteAccountSnapshot removes the snapshot entry of an account trie leaf.
  59. func DeleteAccountSnapshot(db ethdb.KeyValueWriter, hash common.Hash) {
  60. if err := db.Delete(accountSnapshotKey(hash)); err != nil {
  61. log.Crit("Failed to delete account snapshot", "err", err)
  62. }
  63. }
  64. // ReadStorageSnapshot retrieves the snapshot entry of an storage trie leaf.
  65. func ReadStorageSnapshot(db ethdb.KeyValueReader, accountHash, storageHash common.Hash) []byte {
  66. data, _ := db.Get(storageSnapshotKey(accountHash, storageHash))
  67. return data
  68. }
  69. // WriteStorageSnapshot stores the snapshot entry of an storage trie leaf.
  70. func WriteStorageSnapshot(db ethdb.KeyValueWriter, accountHash, storageHash common.Hash, entry []byte) {
  71. if err := db.Put(storageSnapshotKey(accountHash, storageHash), entry); err != nil {
  72. log.Crit("Failed to store storage snapshot", "err", err)
  73. }
  74. }
  75. // DeleteStorageSnapshot removes the snapshot entry of an storage trie leaf.
  76. func DeleteStorageSnapshot(db ethdb.KeyValueWriter, accountHash, storageHash common.Hash) {
  77. if err := db.Delete(storageSnapshotKey(accountHash, storageHash)); err != nil {
  78. log.Crit("Failed to delete storage snapshot", "err", err)
  79. }
  80. }
  81. // IterateStorageSnapshots returns an iterator for walking the entire storage
  82. // space of a specific account.
  83. func IterateStorageSnapshots(db ethdb.Iteratee, accountHash common.Hash) ethdb.Iterator {
  84. return db.NewIterator(storageSnapshotsKey(accountHash), nil)
  85. }
  86. // ReadSnapshotJournal retrieves the serialized in-memory diff layers saved at
  87. // the last shutdown. The blob is expected to be max a few 10s of megabytes.
  88. func ReadSnapshotJournal(db ethdb.KeyValueReader) []byte {
  89. data, _ := db.Get(snapshotJournalKey)
  90. return data
  91. }
  92. // WriteSnapshotJournal stores the serialized in-memory diff layers to save at
  93. // shutdown. The blob is expected to be max a few 10s of megabytes.
  94. func WriteSnapshotJournal(db ethdb.KeyValueWriter, journal []byte) {
  95. if err := db.Put(snapshotJournalKey, journal); err != nil {
  96. log.Crit("Failed to store snapshot journal", "err", err)
  97. }
  98. }
  99. // DeleteSnapshotJournal deletes the serialized in-memory diff layers saved at
  100. // the last shutdown
  101. func DeleteSnapshotJournal(db ethdb.KeyValueWriter) {
  102. if err := db.Delete(snapshotJournalKey); err != nil {
  103. log.Crit("Failed to remove snapshot journal", "err", err)
  104. }
  105. }