disklayer.go 4.8 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/allegro/bigcache"
  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 *bigcache.BigCache // Cache to avoid hitting the disk for direct access
  30. number uint64 // Block number of the base snapshot
  31. root common.Hash // Root hash of the base snapshot
  32. stale bool // Signals that the layer became stale (state progressed)
  33. lock sync.RWMutex
  34. }
  35. // Info returns the block number and root hash for which this snapshot was made.
  36. func (dl *diskLayer) Info() (uint64, common.Hash) {
  37. return dl.number, dl.root
  38. }
  39. // Account directly retrieves the account associated with a particular hash in
  40. // the snapshot slim data format.
  41. func (dl *diskLayer) Account(hash common.Hash) (*Account, error) {
  42. data, err := dl.AccountRLP(hash)
  43. if err != nil {
  44. return nil, err
  45. }
  46. if len(data) == 0 { // can be both nil and []byte{}
  47. return nil, nil
  48. }
  49. account := new(Account)
  50. if err := rlp.DecodeBytes(data, account); err != nil {
  51. panic(err)
  52. }
  53. return account, nil
  54. }
  55. // AccountRLP directly retrieves the account RLP associated with a particular
  56. // hash in the snapshot slim data format.
  57. func (dl *diskLayer) AccountRLP(hash common.Hash) ([]byte, error) {
  58. dl.lock.RLock()
  59. defer dl.lock.RUnlock()
  60. // If the layer was flattened into, consider it invalid (any live reference to
  61. // the original should be marked as unusable).
  62. if dl.stale {
  63. return nil, ErrSnapshotStale
  64. }
  65. key := string(hash[:])
  66. // Try to retrieve the account from the memory cache
  67. if blob, err := dl.cache.Get(key); err == nil {
  68. snapshotCleanHitMeter.Mark(1)
  69. snapshotCleanReadMeter.Mark(int64(len(blob)))
  70. return blob, nil
  71. }
  72. // Cache doesn't contain account, pull from disk and cache for later
  73. blob := rawdb.ReadAccountSnapshot(dl.db, hash)
  74. dl.cache.Set(key, blob)
  75. snapshotCleanMissMeter.Mark(1)
  76. snapshotCleanWriteMeter.Mark(int64(len(blob)))
  77. return blob, nil
  78. }
  79. // Storage directly retrieves the storage data associated with a particular hash,
  80. // within a particular account.
  81. func (dl *diskLayer) Storage(accountHash, storageHash common.Hash) ([]byte, error) {
  82. dl.lock.RLock()
  83. defer dl.lock.RUnlock()
  84. // If the layer was flattened into, consider it invalid (any live reference to
  85. // the original should be marked as unusable).
  86. if dl.stale {
  87. return nil, ErrSnapshotStale
  88. }
  89. key := string(append(accountHash[:], storageHash[:]...))
  90. // Try to retrieve the storage slot from the memory cache
  91. if blob, err := dl.cache.Get(key); err == nil {
  92. snapshotCleanHitMeter.Mark(1)
  93. snapshotCleanReadMeter.Mark(int64(len(blob)))
  94. return blob, nil
  95. }
  96. // Cache doesn't contain storage slot, pull from disk and cache for later
  97. blob := rawdb.ReadStorageSnapshot(dl.db, accountHash, storageHash)
  98. dl.cache.Set(key, blob)
  99. snapshotCleanMissMeter.Mark(1)
  100. snapshotCleanWriteMeter.Mark(int64(len(blob)))
  101. return blob, nil
  102. }
  103. // Update creates a new layer on top of the existing snapshot diff tree with
  104. // the specified data items. Note, the maps are retained by the method to avoid
  105. // copying everything.
  106. func (dl *diskLayer) Update(blockHash common.Hash, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) *diffLayer {
  107. return newDiffLayer(dl, dl.number+1, blockHash, accounts, storage)
  108. }
  109. // Cap traverses downwards the diff tree until the number of allowed layers are
  110. // crossed. All diffs beyond the permitted number are flattened downwards.
  111. func (dl *diskLayer) Cap(layers int, memory uint64) (uint64, uint64) {
  112. return dl.number, dl.number
  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. }