Skip to content

Commit

Permalink
Fix round changes (#867)
Browse files Browse the repository at this point in the history
* fix round changes

Signed-off-by: Adam Reif <Garandor@manta.network>

* fix clippy

Signed-off-by: Adam Reif <Garandor@manta.network>

Signed-off-by: Adam Reif <Garandor@manta.network>
Signed-off-by: zqhxuyuan <zqhxuyuan@gmail.com>
  • Loading branch information
Adam Reif authored and zqhxuyuan committed Dec 7, 2022
1 parent 1c0895c commit f9c70ef
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions runtime/calamari/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,10 +1023,33 @@ impl_runtime_apis! {

impl nimbus_primitives::NimbusApi<Block> for Runtime {
fn can_author(author: NimbusId, relay_parent: u32, parent_header: &<Block as BlockT>::Header) -> bool {
System::initialize(&(parent_header.number + 1), &parent_header.hash(), &parent_header.digest);

// And now the actual prediction call
<AuthorInherent as nimbus_primitives::CanAuthor<_>>::can_author(&author, &relay_parent)
let next_block_number = parent_header.number + 1;
let slot = relay_parent;
// Because the staking solution calculates the next staking set at the beginning
// of the first block in the new round, the only way to accurately predict the
// authors is to compute the selection during prediction.
// NOTE: This logic must manually be kept in sync with the nimbus filter pipeline
if pallet_parachain_staking::Pallet::<Self>::round().should_update(next_block_number)
{
// lookup account from nimbusId
// mirrors logic in `pallet_author_inherent`
use nimbus_primitives::AccountLookup;
let account = match manta_collator_selection::Pallet::<Self>::lookup_account(&author) {
Some(account) => account,
// Authors whose account lookups fail will not be eligible
None => {
return false;
}
};
// manually check aura eligibility (in the new round)
// mirrors logic in `aura_style_filter`
let truncated_half_slot = (slot >> 1) as usize;
let active: Vec<AccountId> = pallet_parachain_staking::Pallet::<Self>::compute_top_candidates();
account == active[truncated_half_slot % active.len()]
} else {
// We're not changing rounds, `PotentialAuthors` is not changing, just use can_author
<AuthorInherent as nimbus_primitives::CanAuthor<_>>::can_author(&author, &relay_parent)
}
}
}

Expand Down

0 comments on commit f9c70ef

Please sign in to comment.