-
Notifications
You must be signed in to change notification settings - Fork 819
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
Prioritize primary blocks over secondary blocks #6013 #7550
base: master
Are you sure you want to change the base?
Conversation
I'm not done with the implementation and I am yet to test it. |
Please review @bkchr |
// Determine if the current block is primary or secondary | ||
let is_primary = pre_digest.is_primary(); | ||
|
||
// Determine if the last best block is primary or secondary | ||
let last_best_is_primary = { | ||
let last_best_header = self.client.header(last_best) | ||
.map_err(|e| ConsensusError::ChainLookup(e.to_string()))? | ||
.ok_or_else(|| { | ||
ConsensusError::ChainLookup( | ||
"No header for last best block.".to_string(), | ||
) | ||
})?; | ||
let last_best_pre_digest = find_pre_digest::<Block>(&last_best_header).expect( | ||
"valid babe headers must contain a predigest; header has been already verified; qed", | ||
); | ||
last_best_pre_digest.is_primary() | ||
}; | ||
|
||
Some(ForkChoiceStrategy::Custom( | ||
if is_primary && !last_best_is_primary { | ||
// Always prefer primary blocks over secondary blocks | ||
true | ||
} else if !is_primary && last_best_is_primary { | ||
// Never prefer secondary blocks over primary blocks | ||
false | ||
} else if total_weight > last_best_weight { | ||
// If both are primary or both are secondary, prefer the heavier chain | ||
true | ||
} else if total_weight == last_best_weight { | ||
// If weights are equal, prefer the longer chain | ||
number > last_best_number | ||
} else { | ||
false | ||
} | ||
)) |
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 would be nice if you could follow my proposal here: #6013 (comment)
The weight needs to be stored in the shared object. Otherwise the issue will not be solved. The code you have written here still has the same issues, because the new best block was not yet imported.
Attempts to resolve #6013
Description
modified the fork-choice logic to explicitly prioritize primary blocks over secondary blocks. This should resolve the inconsistency in the fork-choice rule and ensure that the validator consistently chooses the primary block when available.