Collation protocol: stricter validators (#2810)

* guide: declare one para as a collator

* add ParaId to Declare messages and clean up

* fix build

* fix the testerinos

* begin adding keystore to collator-protocol

* remove request_x_ctx

* add core_for_group

* add bump_rotation

* add some more helpers to subsystem-util

* change signing_key API to take ref

* determine current and next para assignments

* disconnect collators who are not on current or next para

* add collator peer count metric

* notes for later

* some fixes

* add data & keystore to test state

* add a test utility for answering runtime API requests

* fix existing collator tests

* add new tests

* remove sc_keystore

* update cargo lock

Co-authored-by: Andronik Ordian <write@reusable.software>
This commit is contained in:
Robert Habermeier
2021-04-03 21:48:58 +02:00
committed by GitHub
parent 94b0ccc8f1
commit 11b8e4c821
22 changed files with 1064 additions and 334 deletions
+53
View File
@@ -613,9 +613,42 @@ impl GroupRotationInfo {
let blocks_since_start = self.now.saturating_sub(self.session_start_block);
let rotations = blocks_since_start / self.group_rotation_frequency;
// g = c + r mod cores
let idx = (core_index.0 as usize + rotations as usize) % cores;
GroupIndex(idx as u32)
}
/// Returns the index of the group assigned to the given core. This does no checking or
/// whether the group index is in-bounds.
///
/// `core_index` should be less than `cores`, which is capped at u32::max().
pub fn core_for_group(&self, group_index: GroupIndex, cores: usize) -> CoreIndex {
if self.group_rotation_frequency == 0 { return CoreIndex(group_index.0) }
if cores == 0 { return CoreIndex(0) }
let cores = sp_std::cmp::min(cores, u32::max_value() as usize);
let blocks_since_start = self.now.saturating_sub(self.session_start_block);
let rotations = blocks_since_start / self.group_rotation_frequency;
let rotations = rotations % cores as u32;
// g = c + r mod cores
// c = g - r mod cores
// x = x + cores mod cores
// c = (g + cores) - r mod cores
let idx = (group_index.0 as usize + cores - rotations as usize) % cores;
CoreIndex(idx as u32)
}
/// Create a new `GroupRotationInfo` with one further rotation applied.
pub fn bump_rotation(&self) -> Self {
GroupRotationInfo {
session_start_block: self.session_start_block,
group_rotation_frequency: self.group_rotation_frequency,
now: self.next_rotation_at(),
}
}
}
impl<N: Saturating + BaseArithmetic + Copy> GroupRotationInfo<N> {
@@ -1107,6 +1140,26 @@ mod tests {
assert_eq!(info.last_rotation_at(), 15);
}
#[test]
fn group_for_core_is_core_for_group() {
for cores in 1..=256 {
for rotations in 0..(cores * 2) {
let info = GroupRotationInfo {
session_start_block: 0u32,
now: rotations,
group_rotation_frequency: 1,
};
for core in 0..cores {
let group = info.group_for_core(CoreIndex(core), cores as usize);
assert_eq!(info.core_for_group(group, cores as usize).0, core);
}
}
}
}
#[test]
fn collator_signature_payload_is_valid() {
// if this fails, collator signature verification code has to be updated.