This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
statedb: allow longer state pruning history #11980
Merged
paritytech-processbot
merged 21 commits into
paritytech:master
from
NingLin-P:state-pruning
Sep 1, 2022
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ff20ef7
introduce DbBackedQueue for the state pruning window
NingLin-P a0280fa
avoid cloning for next_hash
NingLin-P b05f699
add tests
NingLin-P e8d57d0
make clippy happy
NingLin-P f048c31
impl have_block by checking block number
NingLin-P 206582e
refactor
NingLin-P bc3e31c
fix tests & add test for init db-backed queue
NingLin-P 1944c5c
update comment
NingLin-P d042666
add check for have_state_at
NingLin-P 869eb5d
address comment
NingLin-P d0c7710
renanme unload_blocks to uncached_blocks
NingLin-P abf1354
address comment
NingLin-P 0c0a8eb
Merge branch 'master' of github.com:paritytech/substrate into state-p…
NingLin-P 16969d1
fix syncs_state test
NingLin-P 7c886f7
address comment
NingLin-P acfa7ef
revert change to make_test_db to add test cases
NingLin-P ba46914
Merge branch 'master' of github.com:paritytech/substrate into state-p…
NingLin-P 77c830a
do not prune unavailable block & add tests
NingLin-P 30a1f1e
Update client/state-db/src/lib.rs
917e26e
Update client/state-db/src/pruning.rs
469b0b2
address comment
NingLin-P 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,7 +63,7 @@ use sc_client_api::{ | |
| utils::is_descendent_of, | ||
| IoInfo, MemoryInfo, MemorySize, UsageInfo, | ||
| }; | ||
| use sc_state_db::StateDb; | ||
| use sc_state_db::{IsPruned, StateDb}; | ||
| use sp_arithmetic::traits::Saturating; | ||
| use sp_blockchain::{ | ||
| well_known_cache_keys, Backend as _, CachedHeaderMetadata, Error as ClientError, HeaderBackend, | ||
|
|
@@ -426,9 +426,10 @@ struct PendingBlock<Block: BlockT> { | |
| } | ||
|
|
||
| // wrapper that implements trait required for state_db | ||
| struct StateMetaDb<'a>(&'a dyn Database<DbHash>); | ||
| #[derive(Clone)] | ||
| struct StateMetaDb(Arc<dyn Database<DbHash>>); | ||
|
|
||
| impl<'a> sc_state_db::MetaDb for StateMetaDb<'a> { | ||
| impl sc_state_db::MetaDb for StateMetaDb { | ||
| type Error = sp_database::error::DatabaseError; | ||
|
|
||
| fn get_meta(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> { | ||
|
|
@@ -899,7 +900,7 @@ impl<Block: BlockT> sc_client_api::backend::BlockImportOperation<Block> | |
|
|
||
| struct StorageDb<Block: BlockT> { | ||
| pub db: Arc<dyn Database<DbHash>>, | ||
| pub state_db: StateDb<Block::Hash, Vec<u8>>, | ||
| pub state_db: StateDb<Block::Hash, Vec<u8>, StateMetaDb>, | ||
| prefix_keys: bool, | ||
| } | ||
|
|
||
|
|
@@ -1089,11 +1090,11 @@ impl<Block: BlockT> Backend<Block> { | |
| let mut db_init_transaction = Transaction::new(); | ||
|
|
||
| let requested_state_pruning = config.state_pruning.clone(); | ||
| let state_meta_db = StateMetaDb(db.as_ref()); | ||
| let state_meta_db = StateMetaDb(db.clone()); | ||
| let map_e = sp_blockchain::Error::from_state_db; | ||
|
|
||
| let (state_db_init_commit_set, state_db) = StateDb::open( | ||
| &state_meta_db, | ||
| state_meta_db, | ||
| requested_state_pruning, | ||
| !db.supports_ref_counting(), | ||
| should_init, | ||
|
|
@@ -1303,10 +1304,11 @@ impl<Block: BlockT> Backend<Block> { | |
| } | ||
|
|
||
| trace!(target: "db", "Canonicalize block #{} ({:?})", new_canonical, hash); | ||
| let commit = | ||
| self.storage.state_db.canonicalize_block(&hash).map_err( | ||
| sp_blockchain::Error::from_state_db::<sc_state_db::Error<io::Error>>, | ||
| )?; | ||
| let commit = self.storage.state_db.canonicalize_block(&hash).map_err( | ||
| sp_blockchain::Error::from_state_db::< | ||
| sc_state_db::Error<sp_database::error::DatabaseError>, | ||
| >, | ||
| )?; | ||
| apply_state_commit(transaction, commit); | ||
| } | ||
| Ok(()) | ||
|
|
@@ -1459,14 +1461,16 @@ impl<Block: BlockT> Backend<Block> { | |
| .storage | ||
| .state_db | ||
| .insert_block(&hash, number_u64, pending_block.header.parent_hash(), changeset) | ||
| .map_err(|e: sc_state_db::Error<io::Error>| { | ||
| .map_err(|e: sc_state_db::Error<sp_database::error::DatabaseError>| { | ||
| sp_blockchain::Error::from_state_db(e) | ||
| })?; | ||
| apply_state_commit(&mut transaction, commit); | ||
| if number <= last_finalized_num { | ||
| // Canonicalize in the db when re-importing existing blocks with state. | ||
| let commit = self.storage.state_db.canonicalize_block(&hash).map_err( | ||
| sp_blockchain::Error::from_state_db::<sc_state_db::Error<io::Error>>, | ||
| sp_blockchain::Error::from_state_db::< | ||
| sc_state_db::Error<sp_database::error::DatabaseError>, | ||
| >, | ||
| )?; | ||
| apply_state_commit(&mut transaction, commit); | ||
| meta_updates.push(MetaUpdate { | ||
|
|
@@ -1675,10 +1679,11 @@ impl<Block: BlockT> Backend<Block> { | |
| .map(|c| f_num.saturated_into::<u64>() > c) | ||
| .unwrap_or(true) | ||
| { | ||
| let commit = | ||
| self.storage.state_db.canonicalize_block(&f_hash).map_err( | ||
| sp_blockchain::Error::from_state_db::<sc_state_db::Error<io::Error>>, | ||
| )?; | ||
| let commit = self.storage.state_db.canonicalize_block(&f_hash).map_err( | ||
| sp_blockchain::Error::from_state_db::< | ||
| sc_state_db::Error<sp_database::error::DatabaseError>, | ||
| >, | ||
| )?; | ||
| apply_state_commit(transaction, commit); | ||
| } | ||
|
|
||
|
|
@@ -2270,13 +2275,14 @@ impl<Block: BlockT> sc_client_api::backend::Backend<Block> for Backend<Block> { | |
|
|
||
| match self.blockchain.header_metadata(hash) { | ||
| Ok(ref hdr) => { | ||
| if !self.have_state_at(&hash, hdr.number) { | ||
| return Err(sp_blockchain::Error::UnknownBlock(format!( | ||
| "State already discarded for {:?}", | ||
| block | ||
| ))) | ||
| } | ||
| if let Ok(()) = self.storage.state_db.pin(&hash) { | ||
| let hint = || { | ||
| sc_state_db::NodeDb::get(self.storage.as_ref(), hdr.state_root.as_ref()) | ||
| .unwrap_or(None) | ||
| .is_some() | ||
| }; | ||
| if let Ok(()) = | ||
| self.storage.state_db.pin(&hash, hdr.number.saturated_into::<u64>(), hint) | ||
| { | ||
| let root = hdr.state_root; | ||
| let db_state = DbState::<Block>::new(self.storage.clone(), root); | ||
| let state = RefTrackingState::new(db_state, self.storage.clone(), Some(hash)); | ||
|
|
@@ -2312,7 +2318,20 @@ impl<Block: BlockT> sc_client_api::backend::Backend<Block> for Backend<Block> { | |
| _ => false, | ||
| } | ||
| } else { | ||
| !self.storage.state_db.is_pruned(hash, number.saturated_into::<u64>()) | ||
| match self.storage.state_db.is_pruned(hash, number.saturated_into::<u64>()) { | ||
| IsPruned::Pruned => false, | ||
| IsPruned::NotPruned => true, | ||
| IsPruned::MaybePruned => match self.blockchain.header_metadata(*hash) { | ||
| Ok(header) => sp_state_machine::Storage::get( | ||
| self.storage.as_ref(), | ||
| &header.state_root, | ||
| (&[], None), | ||
| ) | ||
| .unwrap_or(None) | ||
| .is_some(), | ||
| _ => false, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I think it would be better to return a ClientError (have_state_at seems to be only call by function how return it).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing the return type here will also require changing the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I vote for changing it (client db is quite internal to substrate I think). But let's wait for second feed back. |
||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
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.