preimages.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2022 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 trie
  17. import (
  18. "sync"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/core/rawdb"
  21. "github.com/ethereum/go-ethereum/ethdb"
  22. )
  23. // preimageStore is the store for caching preimages of node key.
  24. type preimageStore struct {
  25. lock sync.RWMutex
  26. disk ethdb.KeyValueStore
  27. preimages map[common.Hash][]byte // Preimages of nodes from the secure trie
  28. preimagesSize common.StorageSize // Storage size of the preimages cache
  29. }
  30. // newPreimageStore initializes the store for caching preimages.
  31. func newPreimageStore(disk ethdb.KeyValueStore) *preimageStore {
  32. return &preimageStore{
  33. disk: disk,
  34. preimages: make(map[common.Hash][]byte),
  35. }
  36. }
  37. // insertPreimage writes a new trie node pre-image to the memory database if it's
  38. // yet unknown. The method will NOT make a copy of the slice, only use if the
  39. // preimage will NOT be changed later on.
  40. func (store *preimageStore) insertPreimage(preimages map[common.Hash][]byte) {
  41. store.lock.Lock()
  42. defer store.lock.Unlock()
  43. for hash, preimage := range preimages {
  44. if _, ok := store.preimages[hash]; ok {
  45. continue
  46. }
  47. store.preimages[hash] = preimage
  48. store.preimagesSize += common.StorageSize(common.HashLength + len(preimage))
  49. }
  50. }
  51. // preimage retrieves a cached trie node pre-image from memory. If it cannot be
  52. // found cached, the method queries the persistent database for the content.
  53. func (store *preimageStore) preimage(hash common.Hash) []byte {
  54. store.lock.RLock()
  55. preimage := store.preimages[hash]
  56. store.lock.RUnlock()
  57. if preimage != nil {
  58. return preimage
  59. }
  60. return rawdb.ReadPreimage(store.disk, hash)
  61. }
  62. // commit flushes the cached preimages into the disk.
  63. func (store *preimageStore) commit(force bool) error {
  64. store.lock.Lock()
  65. defer store.lock.Unlock()
  66. if store.preimagesSize <= 4*1024*1024 && !force {
  67. return nil
  68. }
  69. batch := store.disk.NewBatch()
  70. rawdb.WritePreimages(batch, store.preimages)
  71. if err := batch.Write(); err != nil {
  72. return err
  73. }
  74. store.preimages, store.preimagesSize = make(map[common.Hash][]byte), 0
  75. return nil
  76. }
  77. // size returns the current storage size of accumulated preimages.
  78. func (store *preimageStore) size() common.StorageSize {
  79. store.lock.RLock()
  80. defer store.lock.RUnlock()
  81. return store.preimagesSize
  82. }