Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Override RuntimeApi::authorities() to include past sessions #2494

Merged
merged 5 commits into from
Feb 23, 2021

Conversation

ParthDesai
Copy link
Contributor

@ParthDesai ParthDesai commented Feb 22, 2021

Overrides RuntimeApi::Authorities() to include past sessions as well with the help of session_info module.

Fixes p.2 of #2460

cc: @ordian

Copy link
Member

@ordian ordian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking a stab at this.
After the comments are addresses, it would be fine to go in.

runtime/parachains/src/runtime_api_impl/v1.rs Outdated Show resolved Hide resolved
runtime/parachains/src/runtime_api_impl/v1.rs Outdated Show resolved Hide resolved
runtime/parachains/src/runtime_api_impl/v1.rs Outdated Show resolved Hide resolved
runtime/rococo/src/lib.rs Outdated Show resolved Hide resolved
Comment on lines +236 to +237
let current_session_index = session_index_for_child::<T>();
let earliest_stored_session = <session_info::Module<T>>::earliest_stored_session();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine this might be racy and error-prone at session boundaries, we can maybe iterate directly over the Sessions storage, but that's probably racy as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ordian At session boundary, If there is a race condition between getting session indexes above and loop execution, we might miss latest session's authority ids, which can break the semantics. So, to cover widest possible range here we can query session info from earliest..=current+1.

Copy link
Contributor

@rphmeier rphmeier Feb 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be fine at session boundaries as session changes are applied at the end of the parent block. The code comment in fn session_index_for_child is outdated and wrong and that threw me for a loop while investigating this comment.

Iterating up to current_session_index is fine but we also want to include the AuthorityDiscovery::next_authorities here. Although AuthorityDiscovery updates its current and next authorities at the beginning of the parent block, we are past the end of the parent block here and current should have made their way into SessionInfo.

runtime/parachains/src/runtime_api_impl/v1.rs Outdated Show resolved Hide resolved
@ordian ordian added A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. labels Feb 22, 2021
@ordian ordian requested a review from rphmeier February 22, 2021 15:22
ordian
ordian previously approved these changes Feb 22, 2021
Copy link
Member

@ordian ordian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

runtime/parachains/src/runtime_api_impl/v1.rs Outdated Show resolved Hide resolved
pub fn get_current_and_historical_authority_ids<T: initializer::Config>() -> Vec<AuthorityDiscoveryId> {
let current_session_index = session_index_for_child::<T>();
let earliest_stored_session = <session_info::Module<T>>::earliest_stored_session();
let mut authority_ids = vec![];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, we need to include authorities for the next session as well here
the rationale is outlined in paritytech/substrate#6788

@ordian ordian dismissed their stale review February 22, 2021 21:17

jumped the gun on the approval

@ParthDesai
Copy link
Contributor Author

ParthDesai commented Feb 23, 2021

@ordian Ok. I modified the code based on comments above. Now, I am only getting historical authority ids from session_info module and current and next authority ids from the AuthorityDiscovery::authorities() runtime call.

Reason behind this is, session_info currently does not store the next session's authority ids which are required as per semantics of the runtime call.

/// Gets current and historical authority ids using session_info module.
pub fn get_current_and_historical_authority_ids<T: initializer::Config>() -> Vec<AuthorityDiscoveryId> {
pub fn get_historical_authority_ids<T: initializer::Config>() -> Vec<AuthorityDiscoveryId> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API of this function has now changed and it can include duplicates.

This function should include the call to AuthorityDiscovery::next_authorities which can be achieved by adding an extra bound on T if necessary (although I believe it is implied by initializer::Config.

Copy link
Contributor Author

@ParthDesai ParthDesai Feb 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rphmeier I am slightly confused right now. Is session_info[current_session_index].discovery_keys is equivalent to AutorityDiscovery::next_authorities() in every case? (Based on your earlier comment above) In that case, I can directly do earliest_session..=current_session, instead of calling AuthorityDiscovery::next_authorities.

Copy link
Contributor

@rphmeier rphmeier Feb 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that's not what I said at all. Please re-review my comment! session_info[current_session_index].discovery_keys is the same as AuthorityDiscovery::current_authorities. Why would it be otherwise? This is the only thing that makes sense.

Iterating up to current_session_index is fine but we also want to include the AuthorityDiscovery::next_authorities here.

but != because.

Copy link
Contributor Author

@ParthDesai ParthDesai Feb 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rphmeier Ok. Makes sense. I have updated code to reflect this. cc: @ordian

runtime/rococo/src/lib.rs Outdated Show resolved Hide resolved
runtime/parachains/src/runtime_api_impl/v1.rs Outdated Show resolved Hide resolved
}
}

let mut next_authorities = <pallet_authority_discovery::Module<T>>::next_authorities();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

session_info module defines AuthorityDiscoveryConfig trait to help with mock testing,
we can add next_authorities method to it, this would avoid having the pallet_authority_discovery::Config bound here

and call it

<T as session_info::AuthorityDiscoveryConfig>::next_authorities()

also this can go into initialization, i.e. instead of

let mut authority_ids = vec![];

Copy link
Contributor Author

@ParthDesai ParthDesai Feb 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ordian IMHO I think it would be more elegant to have explicit trait bound rather than use the trait impl that is primarily used for testing. Also it will create shared dependency between mock testing and actual code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't agree it's primarily used for tests, rather it is to allow testing as implementing pallet_authority_discovery::Config manually is much harder. But it's also not that important.

@rphmeier rphmeier merged commit 175d707 into paritytech:master Feb 23, 2021
@parity2305 parity2305 deleted the return-past-authorityids branch February 28, 2021 13:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants