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 1.6k
Ensure that we fetch another collation if the first collation was invalid #3362
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -480,12 +480,14 @@ type PendingCollationFetch = ( | |
); | ||
|
||
/// The status of the collations in [`CollationsPerRelayParent`]. | ||
#[derive(Debug)] | ||
#[derive(Debug, Clone, Copy)] | ||
enum CollationStatus { | ||
/// We are waiting for a collation to be advertised to us. | ||
Waiting, | ||
/// We are currently fetching a collation. | ||
Fetching, | ||
/// We are waiting that a collation is being validated. | ||
WaitingOnValidation, | ||
/// We have seconded a collation. | ||
Seconded, | ||
} | ||
|
@@ -496,6 +498,16 @@ impl Default for CollationStatus { | |
} | ||
} | ||
|
||
impl CollationStatus { | ||
/// Downgrades to `Waiting`, but only if `self != Seconded`. | ||
fn back_to_waiting(&mut self) { | ||
match self { | ||
Self::Seconded => {}, | ||
_ => *self = Self::Waiting, | ||
} | ||
} | ||
} | ||
|
||
/// Information about collations per relay parent. | ||
#[derive(Default)] | ||
struct CollationsPerRelayParent { | ||
|
@@ -505,6 +517,25 @@ struct CollationsPerRelayParent { | |
unfetched_collations: Vec<(PendingCollation, CollatorId)>, | ||
} | ||
|
||
impl CollationsPerRelayParent { | ||
/// Returns the next collation to fetch from the `unfetched_collations`. | ||
/// | ||
/// This will reset the status back to `Waiting` using [`CollationStatus::back_to_waiting`]. | ||
/// | ||
/// Returns `Some(_)` if there is any collation to fetch and the `status` is not `Seconded`. | ||
pub fn get_next_collation_to_fetch(&mut self) -> Option<(PendingCollation, CollatorId)> { | ||
self.status.back_to_waiting(); | ||
|
||
match self.status { | ||
// We don't need to fetch any other collation when we already have seconded one. | ||
CollationStatus::Seconded => None, | ||
CollationStatus::Waiting => self.unfetched_collations.pop(), | ||
CollationStatus::WaitingOnValidation | CollationStatus::Fetching => | ||
unreachable!("We have reset the status above!"), | ||
} | ||
} | ||
} | ||
|
||
/// All state relevant for the validator side of the protocol lives here. | ||
#[derive(Default)] | ||
struct State { | ||
|
@@ -812,7 +843,7 @@ where | |
let collations = state.collations_per_relay_parent.entry(relay_parent).or_default(); | ||
|
||
match collations.status { | ||
CollationStatus::Fetching => | ||
CollationStatus::Fetching | CollationStatus::WaitingOnValidation => | ||
collations.unfetched_collations.push((pending_collation, id)), | ||
CollationStatus::Waiting => { | ||
collations.status = CollationStatus::Fetching; | ||
|
@@ -1024,14 +1055,28 @@ where | |
} | ||
} | ||
Invalid(parent, candidate_receipt) => { | ||
if state.pending_candidates | ||
.get(&parent) | ||
.map(|e| e.1.commitments_hash == Some(candidate_receipt.commitments_hash)) | ||
.unwrap_or_default() | ||
{ | ||
if let Some((id, _)) = state.pending_candidates.remove(&parent) { | ||
report_collator(ctx, &state.peer_data, id).await; | ||
let id = match state.pending_candidates.entry(parent) { | ||
Entry::Occupied(entry) | ||
if entry.get().1.commitments_hash == Some(candidate_receipt.commitments_hash) => entry.remove().0, | ||
Entry::Occupied(_) => { | ||
tracing::error!( | ||
target: LOG_TARGET, | ||
relay_parent = ?parent, | ||
candidate = ?candidate_receipt.hash(), | ||
"Reported invalid candidate for unknown `pending_candidate`!", | ||
); | ||
return | ||
} | ||
Entry::Vacant(_) => return, | ||
}; | ||
|
||
report_collator(ctx, &state.peer_data, id).await; | ||
|
||
if let Some((next, id)) = state.collations_per_relay_parent | ||
.get_mut(&parent) | ||
.and_then(|c| c.get_next_collation_to_fetch()) | ||
{ | ||
fetch_collation(ctx, state, next, id).await; | ||
} | ||
} | ||
} | ||
|
@@ -1139,30 +1184,21 @@ async fn handle_collation_fetched_result( | |
"Failed to fetch collation.", | ||
); | ||
|
||
let (next_try, id) = if let Some(collations) = state.collations_per_relay_parent.get_mut(&relay_parent) { | ||
if let Some(next_try) = collations.unfetched_collations.pop() { | ||
next_try | ||
} else if matches!(collations.status, CollationStatus::Fetching) { | ||
collations.status = CollationStatus::Waiting; | ||
return | ||
} else { | ||
tracing::error!( | ||
target: LOG_TARGET, | ||
status = ?collations.status, | ||
"Expected status `CollationStatus::Fetching` but got unexpected status." | ||
); | ||
return | ||
} | ||
} else { | ||
return | ||
}; | ||
|
||
fetch_collation(ctx, state, next_try, id).await; | ||
if let Some((next, id)) = state.collations_per_relay_parent | ||
.get_mut(&relay_parent) | ||
.and_then(|c| c.get_next_collation_to_fetch()) | ||
{ | ||
fetch_collation(ctx, state, next, id).await; | ||
} | ||
|
||
return | ||
}, | ||
}; | ||
|
||
if let Some(collations) = state.collations_per_relay_parent.get_mut(&relay_parent) { | ||
collations.status = CollationStatus::WaitingOnValidation; | ||
} | ||
|
||
if let Entry::Vacant(entry) = state.pending_candidates.entry(relay_parent) { | ||
collation_event.1.commitments_hash = Some(candidate_receipt.commitments_hash); | ||
ctx.send_message( | ||
|
@@ -1174,6 +1210,13 @@ async fn handle_collation_fetched_result( | |
).await; | ||
|
||
entry.insert(collation_event); | ||
} else { | ||
tracing::error!( | ||
target: LOG_TARGET, | ||
?relay_parent, | ||
candidate = ?candidate_receipt.hash(), | ||
"Trying to insert a pending candidate failed, because there is already one!", | ||
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. this should be unreachable, right? 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. It should be unreachable, yes. I just added it to have some error if we fucked it up. |
||
) | ||
} | ||
} | ||
|
||
|
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
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.
how can it be
None
?can the status be
Seconded
?I feel like the state transition function could use more type-safety and checked assumptions/invariants built-in
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.
It can not be none, but I also don't want to use an expect here.
Not sure what you mean here.
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.
I don't have a suggestion how to refactor it yet, so nevermind.