GRANDPA links set IDs to sessions. (#3472)

* introduce some type aliases for round and set-id

* overhaul session "changed" flag and document better

* do_initialize in BABE when getting new session

* grandpa module tracks set IDs

* update runtime versions

* doc comment -> comment

* Include docs fixes from Gav

Co-Authored-By: Gavin Wood <gavin@parity.io>

* some more review changes

* fix srml-grandpa compilation
This commit is contained in:
Robert Habermeier
2019-08-24 17:17:01 +02:00
committed by Gavin Wood
parent bdd6bba20a
commit b50596428e
12 changed files with 214 additions and 114 deletions
+19 -10
View File
@@ -44,6 +44,7 @@ impl_outer_origin! {
}
thread_local! {
pub static VALIDATORS: RefCell<Vec<u64>> = RefCell::new(vec![1, 2, 3]);
pub static NEXT_VALIDATORS: RefCell<Vec<u64>> = RefCell::new(vec![1, 2, 3]);
pub static AUTHORITIES: RefCell<Vec<UintAuthorityId>> =
RefCell::new(vec![UintAuthorityId(1), UintAuthorityId(2), UintAuthorityId(3)]);
@@ -51,6 +52,7 @@ thread_local! {
pub static SESSION_LENGTH: RefCell<u64> = RefCell::new(2);
pub static SESSION_CHANGED: RefCell<bool> = RefCell::new(false);
pub static TEST_SESSION_CHANGED: RefCell<bool> = RefCell::new(false);
pub static DISABLED: RefCell<bool> = RefCell::new(false);
}
pub struct TestShouldEndSession;
@@ -76,14 +78,24 @@ impl SessionHandler<u64> for TestSessionHandler {
.collect()
);
}
fn on_disabled(_validator_index: usize) {}
fn on_disabled(_validator_index: usize) {
DISABLED.with(|l| *l.borrow_mut() = true)
}
}
pub struct TestOnSessionEnding;
impl OnSessionEnding<u64> for TestOnSessionEnding {
fn on_session_ending(_: SessionIndex, _: SessionIndex) -> Option<Vec<u64>> {
if !TEST_SESSION_CHANGED.with(|l| *l.borrow()) {
Some(NEXT_VALIDATORS.with(|l| l.borrow().clone()))
VALIDATORS.with(|v| {
let mut v = v.borrow_mut();
*v = NEXT_VALIDATORS.with(|l| l.borrow().clone());
Some(v.clone())
})
} else if DISABLED.with(|l| std::mem::replace(&mut *l.borrow_mut(), false)) {
// If there was a disabled validator, underlying conditions have changed
// so we return `Some`.
Some(VALIDATORS.with(|v| v.borrow().clone()))
} else {
None
}
@@ -92,16 +104,13 @@ impl OnSessionEnding<u64> for TestOnSessionEnding {
#[cfg(feature = "historical")]
impl crate::historical::OnSessionEnding<u64, u64> for TestOnSessionEnding {
fn on_session_ending(_: SessionIndex, _: SessionIndex)
fn on_session_ending(ending_index: SessionIndex, will_apply_at: SessionIndex)
-> Option<(Vec<u64>, Vec<(u64, u64)>)>
{
if !TEST_SESSION_CHANGED.with(|l| *l.borrow()) {
let last_validators = Session::validators();
let last_identifications = last_validators.into_iter().map(|v| (v, v)).collect();
Some((NEXT_VALIDATORS.with(|l| l.borrow().clone()), last_identifications))
} else {
None
}
let pair_with_ids = |vals: &[u64]| vals.iter().map(|&v| (v, v)).collect::<Vec<_>>();
<Self as OnSessionEnding<_>>::on_session_ending(ending_index, will_apply_at)
.map(|vals| (pair_with_ids(&vals), vals))
.map(|(ids, vals)| (vals, ids))
}
}