disklayer.go 5.3 KB

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