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.6k
[Light] synchronize cht pruning with babe pruning #6851
Closed
Closed
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
9778963
synch babe headers need with cht pruning
cheme 8fe1bea
Put derive under comment blocks.
cheme 8c24de8
Merge branch 'master' into synch_babe_cht
cheme cfea1f1
Safer api (no more optional component, option could be remove from babe
cheme 4ad7e18
unneeded change
cheme c3b6ef0
switch to array of range to single range.
cheme bf40274
Only log error on pruning failure in babe.
cheme 0a8f0bf
Revert "Only log error on pruning failure in babe."
cheme fcd1410
alternative implementation, HeaderLookup trait need to be redefine to
cheme 4ebf7e3
Merge branch 'master' into synch_babe_cht
cheme e9255a2
temporary code
cheme 34712ab
Revert "temporary code"
cheme cbad9b0
Add missing check
cheme 8701952
temporary code
cheme 689fb6e
Revert "temporary code"
cheme 2e0d122
Move HeaderLookupBackend methods to HeaderBackend
cheme ec15ed9
Rename methods.
cheme 07cf6dd
Factor duplicated code.
cheme 2e44ebe
Merge branch 'master' into synch_babe_cht
cheme 079eecd
Break some long lines.
cheme cb0130d
Merge branch 'master' into synch_babe_cht
cheme 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
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -536,3 +536,148 @@ pub fn changes_tries_state_at_block<'a, Block: BlockT>( | |||||||||
| None => Ok(None), | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| #[derive(Clone)] | ||||||||||
| /// Pruning requirement to share between multiple client component. | ||||||||||
| /// | ||||||||||
| /// This allows pruning related synchronisation. For instance in light | ||||||||||
| /// client we need to synchronize header pruning from CHT (every N blocks) | ||||||||||
| /// with the pruning from consensus used (babe for instance require that | ||||||||||
| /// its epoch headers are not pruned which works as long as the slot length | ||||||||||
| /// is less than the CHT pruning window. | ||||||||||
| /// Each compenent register at a given index (call are done by this order). | ||||||||||
| /// | ||||||||||
| /// Note that this struct could be split in two different struct (provider without | ||||||||||
| /// component and Component), depending on future usage of this shared info. | ||||||||||
| pub struct SharedPruningRequirements<Block: BlockT> { | ||||||||||
| shared: Arc<RwLock<(Vec<ComponentPruningRequirements<Block>>, usize)>>, | ||||||||||
| component: Option<usize>, | ||||||||||
| } | ||||||||||
|
|
||||||||||
| impl<Block: BlockT> Default for SharedPruningRequirements<Block> { | ||||||||||
| fn default() -> Self { | ||||||||||
| SharedPruningRequirements { | ||||||||||
| shared: Arc::new(RwLock::new((Vec::new(), 0))), | ||||||||||
| component: None, | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| impl<Block: BlockT> SharedPruningRequirements<Block> { | ||||||||||
| /// Add a following requirement to apply. | ||||||||||
| /// Returns the shared requirement to use from this component. | ||||||||||
| pub fn next_instance( | ||||||||||
| &self, | ||||||||||
| ) -> SharedPruningRequirements<Block> { | ||||||||||
| let req = ComponentPruningRequirements::default(); | ||||||||||
| let index = { | ||||||||||
| let mut shared = self.shared.write(); | ||||||||||
| let index = shared.0.len(); | ||||||||||
| shared.0.push(req); | ||||||||||
| shared.1 += 1; | ||||||||||
| if shared.1 == usize::max_value() { | ||||||||||
| shared.1 = 1; | ||||||||||
| // marking all as changed is the safest | ||||||||||
| for component in shared.0.iter_mut() { | ||||||||||
| component.last_modified_check = 0; | ||||||||||
| } | ||||||||||
| } | ||||||||||
| index | ||||||||||
| }; | ||||||||||
| let mut result = self.clone(); | ||||||||||
| result.component = Some(index); | ||||||||||
| result | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /// Check if some content was modified after last check. | ||||||||||
| pub fn modification_check(&self) -> bool { | ||||||||||
| if let Some(index) = self.component { | ||||||||||
| let modified = { | ||||||||||
| let read = self.shared.read(); | ||||||||||
| if read.0[index].last_modified_check < read.1 { | ||||||||||
| Some(read.1.clone()) | ||||||||||
| } else { | ||||||||||
| None | ||||||||||
| } | ||||||||||
| }; | ||||||||||
| if let Some(modified) = modified { | ||||||||||
| self.shared.write().0[index].last_modified_check = modified; | ||||||||||
| true | ||||||||||
| } else { | ||||||||||
| false | ||||||||||
| } | ||||||||||
| } else { | ||||||||||
| false | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /// Resolve a finalized block headers to keep. | ||||||||||
| pub fn finalized_headers_needed(&self) -> PruningLimit<NumberFor<Block>> { | ||||||||||
| let mut result = PruningLimit::None; | ||||||||||
| for req in self.shared.read().0.iter() { | ||||||||||
| match &req.requires_finalized_header_up_to { | ||||||||||
| PruningLimit::Locked => return PruningLimit::Locked, | ||||||||||
| PruningLimit::None => (), | ||||||||||
| PruningLimit::Some(n) => { | ||||||||||
| if let PruningLimit::Some(p) = result { | ||||||||||
| if &p < n { | ||||||||||
| result = PruningLimit::Some(p); | ||||||||||
| continue; | ||||||||||
| } | ||||||||||
| } | ||||||||||
| result = PruningLimit::Some(n.clone()); | ||||||||||
| }, | ||||||||||
| } | ||||||||||
| } | ||||||||||
| result | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /// Set new requirement on finalized headers. | ||||||||||
| /// Returns false if we do not have a handle on the shared requirement. | ||||||||||
| pub fn set_finalized_headers_needed(&self, limit: PruningLimit<NumberFor<Block>>) -> bool { | ||||||||||
| if let Some(index) = self.component { | ||||||||||
| let mut shared = self.shared.write(); | ||||||||||
| if shared.0[index].requires_finalized_header_up_to != limit { | ||||||||||
| shared.0[index].requires_finalized_header_up_to = limit; | ||||||||||
| shared.1 += 1; | ||||||||||
| if shared.1 == usize::max_value() { | ||||||||||
| shared.1 = 1; | ||||||||||
| // marking all as changed is the safest | ||||||||||
| for component in shared.0.iter_mut() { | ||||||||||
| component.last_modified_check = 0; | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
| true | ||||||||||
| } else { | ||||||||||
| false | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /// Individual pruning requirement for any substrate component. | ||||||||||
| struct ComponentPruningRequirements<Block: BlockT> { | ||||||||||
| requires_finalized_header_up_to: PruningLimit<NumberFor<Block>>, | ||||||||||
| last_modified_check: usize, | ||||||||||
| } | ||||||||||
|
|
||||||||||
| impl<Block: BlockT> Default for ComponentPruningRequirements<Block> { | ||||||||||
| fn default() -> Self { | ||||||||||
| ComponentPruningRequirements { | ||||||||||
| requires_finalized_header_up_to: PruningLimit::None, | ||||||||||
| last_modified_check: 0, | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| #[derive(Eq, PartialEq)] | ||||||||||
| /// Define a block number limit to apply. | ||||||||||
|
Member
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.
Suggested change
|
||||||||||
| pub enum PruningLimit<N> { | ||||||||||
| /// Ignore. | ||||||||||
| None, | ||||||||||
| /// The component require at least this number | ||||||||||
|
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. Iiuc from the code, |
||||||||||
| /// of unpruned elements. | ||||||||||
| Some(N), | ||||||||||
| /// We lock all pruning. | ||||||||||
| Locked, | ||||||||||
| } | ||||||||||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.