disklayer.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. "bytes"
  19. "sync"
  20. "github.com/VictoriaMetrics/fastcache"
  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/rlp"
  25. "github.com/ethereum/go-ethereum/trie"
  26. )
  27. // diskLayer is a low level persistent snapshot built on top of a key-value store.
  28. type diskLayer struct {
  29. diskdb ethdb.KeyValueStore // Key-value store containing the base snapshot
  30. triedb *trie.Database // Trie node cache for reconstruction purposes
  31. cache *fastcache.Cache // Cache to avoid hitting the disk for direct access
  32. root common.Hash // Root hash of the base snapshot
  33. stale bool // Signals that the layer became stale (state progressed)
  34. genMarker []byte // Marker for the state that's indexed during initial layer generation
  35. genPending chan struct{} // Notification channel when generation is done (test synchronicity)
  36. genAbort chan chan *generatorStats // Notification channel to abort generating the snapshot in this layer
  37. lock sync.RWMutex
  38. }
  39. // Root returns root hash for which this snapshot was made.
  40. func (dl *diskLayer) Root() common.Hash {
  41. return dl.root
  42. }
  43. // Parent always returns nil as there's no layer below the disk.
  44. func (dl *diskLayer) Parent() snapshot {
  45. return nil
  46. }
  47. // Stale return whether this layer has become stale (was flattened across) or if
  48. // it's still live.
  49. func (dl *diskLayer) Stale() bool {
  50. dl.lock.RLock()
  51. defer dl.lock.RUnlock()
  52. return dl.stale
  53. }
  54. // Account directly retrieves the account associated with a particular hash in
  55. // the snapshot slim data format.
  56. func (dl *diskLayer) Account(hash common.Hash) (*Account, error) {
  57. data, err := dl.AccountRLP(hash)
  58. if err != nil {
  59. return nil, err
  60. }
  61. if len(data) == 0 { // can be both nil and []byte{}
  62. return nil, nil
  63. }
  64. account := new(Account)
  65. if err := rlp.DecodeBytes(data, account); err != nil {
  66. panic(err)
  67. }
  68. return account, nil
  69. }
  70. // AccountRLP directly retrieves the account RLP associated with a particular
  71. // hash in the snapshot slim data format.
  72. func (dl *diskLayer) AccountRLP(hash common.Hash) ([]byte, error) {
  73. dl.lock.RLock()
  74. defer dl.lock.RUnlock()
  75. // If the layer was flattened into, consider it invalid (any live reference to
  76. // the original should be marked as unusable).
  77. if dl.stale {
  78. return nil, ErrSnapshotStale
  79. }
  80. // If the layer is being generated, ensure the requested hash has already been
  81. // covered by the generator.
  82. if dl.genMarker != nil && bytes.Compare(hash[:], dl.genMarker) > 0 {
  83. return nil, ErrNotCoveredYet
  84. }
  85. // If we're in the disk layer, all diff layers missed
  86. snapshotDirtyAccountMissMeter.Mark(1)
  87. // Try to retrieve the account from the memory cache
  88. if blob, found := dl.cache.HasGet(nil, hash[:]); found {
  89. snapshotCleanAccountHitMeter.Mark(1)
  90. snapshotCleanAccountReadMeter.Mark(int64(len(blob)))
  91. return blob, nil
  92. }
  93. // Cache doesn't contain account, pull from disk and cache for later
  94. blob := rawdb.ReadAccountSnapshot(dl.diskdb, hash)
  95. dl.cache.Set(hash[:], blob)
  96. snapshotCleanAccountMissMeter.Mark(1)
  97. if n := len(blob); n > 0 {
  98. snapshotCleanAccountWriteMeter.Mark(int64(n))
  99. } else {
  100. snapshotCleanAccountInexMeter.Mark(1)
  101. }
  102. return blob, nil
  103. }
  104. // Storage directly retrieves the storage data associated with a particular hash,
  105. // within a particular account.
  106. func (dl *diskLayer) Storage(accountHash, storageHash common.Hash) ([]byte, error) {
  107. dl.lock.RLock()
  108. defer dl.lock.RUnlock()
  109. // If the layer was flattened into, consider it invalid (any live reference to
  110. // the original should be marked as unusable).
  111. if dl.stale {
  112. return nil, ErrSnapshotStale
  113. }
  114. key := append(accountHash[:], storageHash[:]...)
  115. // If the layer is being generated, ensure the requested hash has already been
  116. // covered by the generator.
  117. if dl.genMarker != nil && bytes.Compare(key, dl.genMarker) > 0 {
  118. return nil, ErrNotCoveredYet
  119. }
  120. // If we're in the disk layer, all diff layers missed
  121. snapshotDirtyStorageMissMeter.Mark(1)
  122. // Try to retrieve the storage slot from the memory cache
  123. if blob, found := dl.cache.HasGet(nil, key); found {
  124. snapshotCleanStorageHitMeter.Mark(1)
  125. snapshotCleanStorageReadMeter.Mark(int64(len(blob)))
  126. return blob, nil
  127. }
  128. // Cache doesn't contain storage slot, pull from disk and cache for later
  129. blob := rawdb.ReadStorageSnapshot(dl.diskdb, accountHash, storageHash)
  130. dl.cache.Set(key, blob)
  131. snapshotCleanStorageMissMeter.Mark(1)
  132. if n := len(blob); n > 0 {
  133. snapshotCleanStorageWriteMeter.Mark(int64(n))
  134. } else {
  135. snapshotCleanStorageInexMeter.Mark(1)
  136. }
  137. return blob, nil
  138. }
  139. // Update creates a new layer on top of the existing snapshot diff tree with
  140. // the specified data items. Note, the maps are retained by the method to avoid
  141. // copying everything.
  142. func (dl *diskLayer) Update(blockHash common.Hash, destructs map[common.Hash]struct{}, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) *diffLayer {
  143. return newDiffLayer(dl, blockHash, destructs, accounts, storage)
  144. }