forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 284
core, trie, eth, cmd: rework preimage store #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0af92e3
core, trie, eth, cmd: rework preimage store
rjl493456442 a667c1d
fix ci
mask-pp 5d29cf5
fix ci
mask-pp d649356
Update zk_trie_database.go
0xmountaintop eff662f
Merge branch 'staging' into fix/preimages-data-race_3
0xmountaintop 5b89484
Merge branch 'staging' into fix/preimages-data-race_3
0xmountaintop 4aa5093
Merge branch 'staging' into fix/preimages-data-race_3
0xmountaintop 0fc2e2c
Merge branch 'staging' into fix/preimages-data-race_3
0xmountaintop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // Copyright 2022 The go-ethereum Authors | ||
| // This file is part of the go-ethereum library. | ||
| // | ||
| // The go-ethereum library is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Lesser General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // The go-ethereum library is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Lesser General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Lesser General Public License | ||
| // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| package trie | ||
|
|
||
| import ( | ||
| "sync" | ||
|
|
||
| "github.com/scroll-tech/go-ethereum/common" | ||
| "github.com/scroll-tech/go-ethereum/core/rawdb" | ||
| "github.com/scroll-tech/go-ethereum/ethdb" | ||
| ) | ||
|
|
||
| // preimageStore is the store for caching preimages of node key. | ||
| type preimageStore struct { | ||
| lock sync.RWMutex | ||
| disk ethdb.KeyValueStore | ||
| preimages map[common.Hash][]byte // Preimages of nodes from the secure trie | ||
| preimagesSize common.StorageSize // Storage size of the preimages cache | ||
| } | ||
|
|
||
| // newPreimageStore initializes the store for caching preimages. | ||
| func newPreimageStore(disk ethdb.KeyValueStore) *preimageStore { | ||
| return &preimageStore{ | ||
| disk: disk, | ||
| preimages: make(map[common.Hash][]byte), | ||
| } | ||
| } | ||
|
|
||
| // insertPreimage writes a new trie node pre-image to the memory database if it's | ||
| // yet unknown. The method will NOT make a copy of the slice, only use if the | ||
| // preimage will NOT be changed later on. | ||
| func (store *preimageStore) insertPreimage(preimages map[common.Hash][]byte) { | ||
| store.lock.Lock() | ||
| defer store.lock.Unlock() | ||
|
|
||
| for hash, preimage := range preimages { | ||
| if _, ok := store.preimages[hash]; ok { | ||
| continue | ||
| } | ||
| store.preimages[hash] = preimage | ||
| store.preimagesSize += common.StorageSize(common.HashLength + len(preimage)) | ||
| } | ||
| } | ||
|
|
||
| // preimage retrieves a cached trie node pre-image from memory. If it cannot be | ||
| // found cached, the method queries the persistent database for the content. | ||
| func (store *preimageStore) preimage(hash common.Hash) []byte { | ||
| store.lock.RLock() | ||
| preimage := store.preimages[hash] | ||
| store.lock.RUnlock() | ||
|
|
||
| if preimage != nil { | ||
| return preimage | ||
| } | ||
| return rawdb.ReadPreimage(store.disk, hash) | ||
| } | ||
|
|
||
| // commit flushes the cached preimages into the disk. | ||
| func (store *preimageStore) commit(force bool) error { | ||
| store.lock.Lock() | ||
| defer store.lock.Unlock() | ||
|
|
||
| if store.preimagesSize <= 4*1024*1024 && !force { | ||
| return nil | ||
| } | ||
| batch := store.disk.NewBatch() | ||
| rawdb.WritePreimages(batch, store.preimages) | ||
| if err := batch.Write(); err != nil { | ||
| return err | ||
| } | ||
| store.preimages, store.preimagesSize = make(map[common.Hash][]byte), 0 | ||
| return nil | ||
| } | ||
|
|
||
| // size returns the current storage size of accumulated preimages. | ||
| func (store *preimageStore) size() common.StorageSize { | ||
| store.lock.RLock() | ||
| defer store.lock.RUnlock() | ||
|
|
||
| return store.preimagesSize | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.