disklayer.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. "github.com/allegro/bigcache"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/core/rawdb"
  21. "github.com/ethereum/go-ethereum/ethdb"
  22. "github.com/ethereum/go-ethereum/rlp"
  23. )
  24. // diskLayer is a low level persistent snapshot built on top of a key-value store.
  25. type diskLayer struct {
  26. journal string // Path of the snapshot journal to use on shutdown
  27. db ethdb.KeyValueStore // Key-value store containing the base snapshot
  28. cache *bigcache.BigCache // Cache to avoid hitting the disk for direct access
  29. number uint64 // Block number of the base snapshot
  30. root common.Hash // Root hash of the base snapshot
  31. }
  32. // Info returns the block number and root hash for which this snapshot was made.
  33. func (dl *diskLayer) Info() (uint64, common.Hash) {
  34. return dl.number, dl.root
  35. }
  36. // Account directly retrieves the account associated with a particular hash in
  37. // the snapshot slim data format.
  38. func (dl *diskLayer) Account(hash common.Hash) *Account {
  39. data := dl.AccountRLP(hash)
  40. if len(data) == 0 { // can be both nil and []byte{}
  41. return nil
  42. }
  43. account := new(Account)
  44. if err := rlp.DecodeBytes(data, account); err != nil {
  45. panic(err)
  46. }
  47. return account
  48. }
  49. // AccountRLP directly retrieves the account RLP associated with a particular
  50. // hash in the snapshot slim data format.
  51. func (dl *diskLayer) AccountRLP(hash common.Hash) []byte {
  52. key := string(hash[:])
  53. // Try to retrieve the account from the memory cache
  54. if blob, err := dl.cache.Get(key); err == nil {
  55. snapshotCleanHitMeter.Mark(1)
  56. snapshotCleanReadMeter.Mark(int64(len(blob)))
  57. return blob
  58. }
  59. // Cache doesn't contain account, pull from disk and cache for later
  60. blob := rawdb.ReadAccountSnapshot(dl.db, hash)
  61. dl.cache.Set(key, blob)
  62. snapshotCleanMissMeter.Mark(1)
  63. snapshotCleanWriteMeter.Mark(int64(len(blob)))
  64. return blob
  65. }
  66. // Storage directly retrieves the storage data associated with a particular hash,
  67. // within a particular account.
  68. func (dl *diskLayer) Storage(accountHash, storageHash common.Hash) []byte {
  69. key := string(append(accountHash[:], storageHash[:]...))
  70. // Try to retrieve the storage slot from the memory cache
  71. if blob, err := dl.cache.Get(key); err == nil {
  72. snapshotCleanHitMeter.Mark(1)
  73. snapshotCleanReadMeter.Mark(int64(len(blob)))
  74. return blob
  75. }
  76. // Cache doesn't contain storage slot, pull from disk and cache for later
  77. blob := rawdb.ReadStorageSnapshot(dl.db, accountHash, storageHash)
  78. dl.cache.Set(key, blob)
  79. snapshotCleanMissMeter.Mark(1)
  80. snapshotCleanWriteMeter.Mark(int64(len(blob)))
  81. return blob
  82. }
  83. // Update creates a new layer on top of the existing snapshot diff tree with
  84. // the specified data items. Note, the maps are retained by the method to avoid
  85. // copying everything.
  86. func (dl *diskLayer) Update(blockHash common.Hash, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) *diffLayer {
  87. return newDiffLayer(dl, dl.number+1, blockHash, accounts, storage)
  88. }
  89. // Cap traverses downwards the diff tree until the number of allowed layers are
  90. // crossed. All diffs beyond the permitted number are flattened downwards.
  91. func (dl *diskLayer) Cap(layers int, memory uint64) (uint64, uint64) {
  92. return dl.number, dl.number
  93. }
  94. // Journal commits an entire diff hierarchy to disk into a single journal file.
  95. func (dl *diskLayer) Journal() error {
  96. // There's no journalling a disk layer
  97. return nil
  98. }