past-session validator discovery APIs (#2009)

* guide: fix formatting for SessionInfo module

* primitives: SessionInfo type

* punt on approval keys

* ah, revert the type alias

* session info runtime module skeleton

* update the guide

* runtime/configuration: sync with the guide

* runtime/configuration: setters for newly added fields

* runtime/configuration: set codec indexes

* runtime/configuration: update test

* primitives: fix SessionInfo definition

* runtime/session_info: initial impl

* runtime/session_info: use initializer for session handling (wip)

* runtime/session_info: mock authority discovery trait

* guide: update the initializer's order

* runtime/session_info: tests skeleton

* runtime/session_info: store n_delay_tranches in Configuration

* runtime/session_info: punt on approval keys

* runtime/session_info: add some basic tests

* Update primitives/src/v1.rs

* small fixes

* remove codec index annotation on structs

* fix off-by-one error

* validator_discovery: accept a session index

* runtime: replace validator_discovery api with session_info

* Update runtime/parachains/src/session_info.rs

Co-authored-by: Sergei Shulepov <sergei@parity.io>

* runtime/session_info: add a comment about missing entries

* runtime/session_info: define the keys

* util: expose connect_to_past_session_validators

* util: allow session_info requests for jobs

* runtime-api: add mock test for session_info

* collator-protocol: add session_index to test state

* util: fix error message for runtime error

* fix compilation

* fix tests after merge with master

Co-authored-by: Sergei Shulepov <sergei@parity.io>
This commit is contained in:
Andronik Ordian
2020-11-26 12:02:50 +01:00
committed by GitHub
parent 4ce744818c
commit 39a12b68f6
25 changed files with 696 additions and 213 deletions
@@ -23,12 +23,12 @@ use primitives::v1::{
ValidatorId, ValidatorIndex, GroupRotationInfo, CoreState, ValidationData,
Id as ParaId, OccupiedCoreAssumption, SessionIndex, ValidationCode,
CommittedCandidateReceipt, ScheduledCore, OccupiedCore, CoreOccupied, CoreIndex,
GroupIndex, CandidateEvent, PersistedValidationData, AuthorityDiscoveryId,
GroupIndex, CandidateEvent, PersistedValidationData, SessionInfo,
InboundDownwardMessage, InboundHrmpMessage,
};
use sp_runtime::traits::Zero;
use frame_support::debug;
use crate::{initializer, inclusion, scheduler, configuration, paras, dmp, hrmp};
use crate::{initializer, inclusion, scheduler, configuration, paras, session_info, dmp, hrmp};
/// Implementation for the `validators` function of the runtime API.
pub fn validators<T: initializer::Trait>() -> Vec<ValidatorId> {
@@ -285,28 +285,9 @@ where
.collect()
}
/// Get the `AuthorityDiscoveryId`s corresponding to the given `ValidatorId`s.
/// Currently this request is limited to validators in the current session.
///
/// We assume that every validator runs authority discovery,
/// which would allow us to establish point-to-point connection to given validators.
// FIXME: handle previous sessions:
// https://github.com/paritytech/polkadot/issues/1461
pub fn validator_discovery<T>(validators: Vec<ValidatorId>) -> Vec<Option<AuthorityDiscoveryId>>
where
T: initializer::Trait + pallet_authority_discovery::Trait,
{
// FIXME: the mapping might be invalid if a session change happens in between the calls
// use SessionInfo from https://github.com/paritytech/polkadot/pull/1691
let current_validators = <inclusion::Module<T>>::validators();
let authorities = <pallet_authority_discovery::Module<T>>::authorities();
// We assume the same ordering in authorities as in validators so we can do an index search
validators.iter().map(|id| {
// FIXME: linear search is slow O(n^2)
// use SessionInfo from https://github.com/paritytech/polkadot/pull/1691
let validator_index = current_validators.iter().position(|v| v == id);
validator_index.and_then(|i| authorities.get(i).cloned())
}).collect()
/// Get the session info for the given session, if stored.
pub fn session_info<T: session_info::Trait>(index: SessionIndex) -> Option<SessionInfo> {
<session_info::Module<T>>::session_info(index)
}
/// Implementation for the `dmq_contents` function of the runtime API.