disklayer.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. func (dl *diskLayer) WaitAndGetVerifyRes() bool {
  44. return true
  45. }
  46. func (dl *diskLayer) MarkValid() {}
  47. func (dl *diskLayer) Verified() bool {
  48. return true
  49. }
  50. // Parent always returns nil as there's no layer below the disk.
  51. func (dl *diskLayer) Parent() snapshot {
  52. return nil
  53. }
  54. // Stale return whether this layer has become stale (was flattened across) or if
  55. // it's still live.
  56. func (dl *diskLayer) Stale() bool {
  57. dl.lock.RLock()
  58. defer dl.lock.RUnlock()
  59. return dl.stale
  60. }
  61. // Account directly retrieves the account associated with a particular hash in
  62. // the snapshot slim data format.
  63. func (dl *diskLayer) Account(hash common.Hash) (*Account, error) {
  64. data, err := dl.AccountRLP(hash)
  65. if err != nil {
  66. return nil, err
  67. }
  68. if len(data) == 0 { // can be both nil and []byte{}
  69. return nil, nil
  70. }
  71. account := new(Account)
  72. if err := rlp.DecodeBytes(data, account); err != nil {
  73. panic(err)
  74. }
  75. return account, nil
  76. }
  77. // AccountRLP directly retrieves the account RLP associated with a particular
  78. // hash in the snapshot slim data format.
  79. func (dl *diskLayer) AccountRLP(hash common.Hash) ([]byte, error) {
  80. dl.lock.RLock()
  81. defer dl.lock.RUnlock()
  82. // If the layer was flattened into, consider it invalid (any live reference to
  83. // the original should be marked as unusable).
  84. if dl.stale {
  85. return nil, ErrSnapshotStale
  86. }
  87. // If the layer is being generated, ensure the requested hash has already been
  88. // covered by the generator.
  89. if dl.genMarker != nil && bytes.Compare(hash[:], dl.genMarker) > 0 {
  90. return nil, ErrNotCoveredYet
  91. }
  92. // If we're in the disk layer, all diff layers missed
  93. snapshotDirtyAccountMissMeter.Mark(1)
  94. // Try to retrieve the account from the memory cache
  95. if blob, found := dl.cache.HasGet(nil, hash[:]); found {
  96. snapshotCleanAccountHitMeter.Mark(1)
  97. snapshotCleanAccountReadMeter.Mark(int64(len(blob)))
  98. return blob, nil
  99. }
  100. // Cache doesn't contain account, pull from disk and cache for later
  101. blob := rawdb.ReadAccountSnapshot(dl.diskdb, hash)
  102. dl.cache.Set(hash[:], blob)
  103. snapshotCleanAccountMissMeter.Mark(1)
  104. if n := len(blob); n > 0 {
  105. snapshotCleanAccountWriteMeter.Mark(int64(n))
  106. } else {
  107. snapshotCleanAccountInexMeter.Mark(1)
  108. }
  109. return blob, nil
  110. }
  111. // Storage directly retrieves the storage data associated with a particular hash,
  112. // within a particular account.
  113. func (dl *diskLayer) Storage(accountHash, storageHash common.Hash) ([]byte, error) {
  114. dl.lock.RLock()
  115. defer dl.lock.RUnlock()
  116. // If the layer was flattened into, consider it invalid (any live reference to
  117. // the original should be marked as unusable).
  118. if dl.stale {
  119. return nil, ErrSnapshotStale
  120. }
  121. key := append(accountHash[:], storageHash[:]...)
  122. // If the layer is being generated, ensure the requested hash has already been
  123. // covered by the generator.
  124. if dl.genMarker != nil && bytes.Compare(key, dl.genMarker) > 0 {
  125. return nil, ErrNotCoveredYet
  126. }
  127. // If we're in the disk layer, all diff layers missed
  128. snapshotDirtyStorageMissMeter.Mark(1)
  129. // Try to retrieve the storage slot from the memory cache
  130. if blob, found := dl.cache.HasGet(nil, key); found {
  131. snapshotCleanStorageHitMeter.Mark(1)
  132. snapshotCleanStorageReadMeter.Mark(int64(len(blob)))
  133. return blob, nil
  134. }
  135. // Cache doesn't contain storage slot, pull from disk and cache for later
  136. blob := rawdb.ReadStorageSnapshot(dl.diskdb, accountHash, storageHash)
  137. dl.cache.Set(key, blob)
  138. snapshotCleanStorageMissMeter.Mark(1)
  139. if n := len(blob); n > 0 {
  140. snapshotCleanStorageWriteMeter.Mark(int64(n))
  141. } else {
  142. snapshotCleanStorageInexMeter.Mark(1)
  143. }
  144. return blob, nil
  145. }
  146. // Update creates a new layer on top of the existing snapshot diff tree with
  147. // the specified data items. Note, the maps are retained by the method to avoid
  148. // copying everything.
  149. 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, verified chan struct{}) *diffLayer {
  150. return newDiffLayer(dl, blockHash, destructs, accounts, storage, verified)
  151. }