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
Fix some problems with prove_warp_sync
#8037
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3e60871
Fix some problems with prove_warp_sync
expenses 38afb53
Update client/finality-grandpa/src/finality_proof.rs
expenses ab19577
Merge remote-tracking branch 'origin/master' into ashley-fix-prove-wa…
expenses ecaa8e1
Merge branch 'ashley-fix-prove-warp-sync-problems' of github.com:pari…
expenses 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 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 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 |
---|---|---|
|
@@ -277,9 +277,9 @@ pub fn prove_warp_sync<Block: BlockT, B: BlockchainBackend<Block>>( | |
// This operation is a costy and only for the delay corner case. | ||
while index > Zero::zero() { | ||
index = index - One::one(); | ||
if let Some((fragement, apply_block)) = get_warp_sync_proof_fragment(blockchain, index, &mut cache)? { | ||
if let Some((fragment, apply_block)) = get_warp_sync_proof_fragment(blockchain, index, &mut cache)? { | ||
if last_apply.map(|next| &next > header.number()).unwrap_or(false) { | ||
result.push(fragement); | ||
result.push(fragment); | ||
last_apply = Some(apply_block); | ||
} else { | ||
break; | ||
|
@@ -289,7 +289,7 @@ pub fn prove_warp_sync<Block: BlockT, B: BlockchainBackend<Block>>( | |
|
||
let mut index = *header.number(); | ||
while index <= end_number { | ||
if max_fragment_limit.map(|limit| result.len() <= limit).unwrap_or(false) { | ||
if max_fragment_limit.map(|limit| result.len() >= limit).unwrap_or(false) { | ||
break; | ||
} | ||
|
||
|
@@ -305,7 +305,9 @@ pub fn prove_warp_sync<Block: BlockT, B: BlockchainBackend<Block>>( | |
index = index + One::one(); | ||
} | ||
|
||
if result.last().as_ref().map(|head| head.header.number()) != Some(&end_number) { | ||
let at_limit = max_fragment_limit.map(|limit| result.len() >= limit).unwrap_or(false); | ||
|
||
if !at_limit && result.last().as_ref().map(|head| head.header.number()) != Some(&end_number) { | ||
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. 👍 when at_limit we probably had twice the fragment
expenses marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let header = blockchain.expect_header(end)?; | ||
if let Some(justification) = blockchain.justification(BlockId::Number(end_number.clone()))? { | ||
result.push(AuthoritySetProofFragment { | ||
|
@@ -328,7 +330,7 @@ fn get_warp_sync_proof_fragment<Block: BlockT, B: BlockchainBackend<Block>>( | |
) -> sp_blockchain::Result<Option<(AuthoritySetProofFragment<Block::Header>, NumberFor<Block>)>> { | ||
if let Some(cache) = cache.as_mut() { | ||
if let Some(result) = cache.get_item(index) { | ||
return Ok(result.clone()); | ||
return Ok(result); | ||
} | ||
} | ||
|
||
|
@@ -541,20 +543,20 @@ impl<Block: BlockT> BlockJustification<Block::Header> for GrandpaJustification<B | |
|
||
/// Simple cache for warp sync queries. | ||
pub struct WarpSyncFragmentCache<Header: HeaderT> { | ||
header_has_proof_fragment: std::collections::HashMap<Header::Number, bool>, | ||
cache: linked_hash_map::LinkedHashMap< | ||
Header::Number, | ||
Option<(AuthoritySetProofFragment<Header>, Header::Number)>, | ||
(AuthoritySetProofFragment<Header>, Header::Number), | ||
>, | ||
headers_with_justification: usize, | ||
limit: usize, | ||
} | ||
|
||
impl<Header: HeaderT> WarpSyncFragmentCache<Header> { | ||
/// Instantiate a new cache for the warp sync prover. | ||
pub fn new(size: usize) -> Self { | ||
WarpSyncFragmentCache { | ||
header_has_proof_fragment: Default::default(), | ||
cache: Default::default(), | ||
headers_with_justification: 0, | ||
limit: size, | ||
} | ||
} | ||
|
@@ -564,31 +566,32 @@ impl<Header: HeaderT> WarpSyncFragmentCache<Header> { | |
at: Header::Number, | ||
item: Option<(AuthoritySetProofFragment<Header>, Header::Number)>, | ||
) { | ||
if self.cache.len() == self.limit { | ||
self.pop_one(); | ||
} | ||
if item.is_some() { | ||
// we do not check previous value as cached value is always supposed to | ||
// be queried before calling 'new_item'. | ||
self.headers_with_justification += 1; | ||
self.header_has_proof_fragment.insert(at, item.is_some()); | ||
|
||
if let Some(item) = item { | ||
if self.cache.len() == self.limit { | ||
self.pop_one(); | ||
} | ||
|
||
self.cache.insert(at, item); | ||
} | ||
self.cache.insert(at, item); | ||
} | ||
|
||
fn pop_one(&mut self) { | ||
while let Some(v) = self.cache.pop_front() { | ||
if v.1.is_some() { | ||
self.headers_with_justification -= 1; | ||
break; | ||
} | ||
if let Some((header_number, _)) = self.cache.pop_front() { | ||
self.header_has_proof_fragment.remove(&header_number); | ||
} | ||
} | ||
|
||
fn get_item( | ||
&mut self, | ||
block: Header::Number, | ||
) -> Option<&mut Option<(AuthoritySetProofFragment<Header>, Header::Number)>> { | ||
self.cache.get_refresh(&block) | ||
) -> Option<Option<(AuthoritySetProofFragment<Header>, Header::Number)>> { | ||
match self.header_has_proof_fragment.get(&block) { | ||
Some(true) => Some(self.cache.get_refresh(&block).cloned()), | ||
cheme marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Some(false) => Some(None), | ||
None => None | ||
} | ||
} | ||
} | ||
|
||
|
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.
🤦 , sorry.