disklayer.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 snapshot
  17. import (
  18. "sync"
  19. "github.com/VictoriaMetrics/fastcache"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/rawdb"
  22. "github.com/ethereum/go-ethereum/ethdb"
  23. "github.com/ethereum/go-ethereum/rlp"
  24. )
  25. // diskLayer is a low level persistent snapshot built on top of a key-value store.
  26. type diskLayer struct {
  27. journal string // Path of the snapshot journal to use on shutdown
  28. db ethdb.KeyValueStore // Key-value store containing the base snapshot
  29. cache *fastcache.Cache // Cache to avoid hitting the disk for direct access
  30. root common.Hash // Root hash of the base snapshot
  31. stale bool // Signals that the layer became stale (state progressed)
  32. lock sync.RWMutex
  33. }
  34. // Root returns root hash for which this snapshot was made.
  35. func (dl *diskLayer) Root() common.Hash {
  36. return dl.root
  37. }
  38. // Stale return whether this layer has become stale (was flattened across) or if
  39. // it's still live.
  40. func (dl *diskLayer) Stale() bool {
  41. dl.lock.RLock()
  42. defer dl.lock.RUnlock()
  43. return dl.stale
  44. }
  45. // Account directly retrieves the account associated with a particular hash in
  46. // the snapshot slim data format.
  47. func (dl *diskLayer) Account(hash common.Hash) (*Account, error) {
  48. data, err := dl.AccountRLP(hash)
  49. if err != nil {
  50. return nil, err
  51. }
  52. if len(data) == 0 { // can be both nil and []byte{}
  53. return nil, nil
  54. }
  55. account := new(Account)
  56. if err := rlp.DecodeBytes(data, account); err != nil {
  57. panic(err)
  58. }
  59. return account, nil
  60. }
  61. // AccountRLP directly retrieves the account RLP associated with a particular
  62. // hash in the snapshot slim data format.
  63. func (dl *diskLayer) AccountRLP(hash common.Hash) ([]byte, error) {
  64. dl.lock.RLock()
  65. defer dl.lock.RUnlock()
  66. // If the layer was flattened into, consider it invalid (any live reference to
  67. // the original should be marked as unusable).
  68. if dl.stale {
  69. return nil, ErrSnapshotStale
  70. }
  71. // Try to retrieve the account from the memory cache
  72. if blob := dl.cache.Get(nil, hash[:]); blob != nil {
  73. snapshotCleanHitMeter.Mark(1)
  74. snapshotCleanReadMeter.Mark(int64(len(blob)))
  75. return blob, nil
  76. }
  77. // Cache doesn't contain account, pull from disk and cache for later
  78. blob := rawdb.ReadAccountSnapshot(dl.db, hash)
  79. dl.cache.Set(hash[:], blob)
  80. snapshotCleanMissMeter.Mark(1)
  81. snapshotCleanWriteMeter.Mark(int64(len(blob)))
  82. return blob, nil
  83. }
  84. // Storage directly retrieves the storage data associated with a particular hash,
  85. // within a particular account.
  86. func (dl *diskLayer) Storage(accountHash, storageHash common.Hash) ([]byte, error) {
  87. dl.lock.RLock()
  88. defer dl.lock.RUnlock()
  89. // If the layer was flattened into, consider it invalid (any live reference to
  90. // the original should be marked as unusable).
  91. if dl.stale {
  92. return nil, ErrSnapshotStale
  93. }
  94. key := append(accountHash[:], storageHash[:]...)
  95. // Try to retrieve the storage slot from the memory cache
  96. if blob := dl.cache.Get(nil, key); blob != nil {
  97. snapshotCleanHitMeter.Mark(1)
  98. snapshotCleanReadMeter.Mark(int64(len(blob)))
  99. return blob, nil
  100. }
  101. // Cache doesn't contain storage slot, pull from disk and cache for later
  102. blob := rawdb.ReadStorageSnapshot(dl.db, accountHash, storageHash)
  103. dl.cache.Set(key, blob)
  104. snapshotCleanMissMeter.Mark(1)
  105. snapshotCleanWriteMeter.Mark(int64(len(blob)))
  106. return blob, nil
  107. }
  108. // Update creates a new layer on top of the existing snapshot diff tree with
  109. // the specified data items. Note, the maps are retained by the method to avoid
  110. // copying everything.
  111. func (dl *diskLayer) Update(blockHash common.Hash, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) *diffLayer {
  112. return newDiffLayer(dl, blockHash, accounts, storage)
  113. }
  114. // Journal commits an entire diff hierarchy to disk into a single journal file.
  115. func (dl *diskLayer) Journal() error {
  116. // There's no journalling a disk layer
  117. return nil
  118. }