Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Nathy-bajo
Copy link
Contributor

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.

@Nathy-bajo
Copy link
Contributor Author

I'm not done with the implementation and I am yet to test it.

@Nathy-bajo
Copy link
Contributor Author

Please review @bkchr

Comment on lines +1659 to +1693
// 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
}
))
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistent fork-choice: Validator sometimes chooses own secondary block over others primary
2 participants