Fix tracking validator set in ImOnline (#3596)

* Use session::validators instead of staking::current_elected

* Basic test framework.

* Initialize validators, attempt to heartbeat.

* Use dummy crypto for im-online testing.

* Remove printlns.

* Finish test, make it invalid.

* Add reporting test.

* Finalize the test.

* Remove dumbness.

* Updates.

* Update AuRa

* Update srml/im-online/src/tests.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Derive Ord

* Add some more tests.

* Remove stray todo.

* Bump runtime version.

* Bump impl-trait-for-tuples.

* Enforce new version of trait-for-tuples.
This commit is contained in:
Tomasz Drwięga
2019-09-13 14:55:33 +02:00
committed by Gavin Wood
parent a7f35680b4
commit b7c6bc1ed5
26 changed files with 493 additions and 133 deletions
+31 -49
View File
@@ -67,7 +67,10 @@
// Ensure we're `no_std` when compiling for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]
use app_crypto::{AppPublic, RuntimeAppPublic};
mod mock;
mod tests;
use app_crypto::RuntimeAppPublic;
use codec::{Encode, Decode};
use primitives::offchain::{OpaqueNetworkState, StorageKind};
use rstd::prelude::*;
@@ -80,7 +83,7 @@ use sr_primitives::{
},
};
use sr_staking_primitives::{
SessionIndex, CurrentElectedSet,
SessionIndex,
offence::{ReportOffence, Offence, Kind},
};
use support::{
@@ -136,15 +139,15 @@ pub mod ed25519 {
pub type AuthorityId = app_ed25519::Public;
}
// The local storage database key under which the worker progress status
// is tracked.
/// The local storage database key under which the worker progress status
/// is tracked.
const DB_KEY: &[u8] = b"srml/im-online-worker-status";
// It's important to persist the worker state, since e.g. the
// server could be restarted while starting the gossip process, but before
// finishing it. With every execution of the off-chain worker we check
// if we need to recover and resume gossipping or if there is already
// another off-chain worker in the process of gossipping.
/// It's important to persist the worker state, since e.g. the
/// server could be restarted while starting the gossip process, but before
/// finishing it. With every execution of the off-chain worker we check
/// if we need to recover and resume gossipping or if there is already
/// another off-chain worker in the process of gossipping.
#[derive(Encode, Decode, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug))]
struct WorkerStatus<BlockNumber> {
@@ -152,7 +155,8 @@ struct WorkerStatus<BlockNumber> {
gossipping_at: BlockNumber,
}
// Error which may occur while executing the off-chain code.
/// Error which may occur while executing the off-chain code.
#[cfg_attr(feature = "std", derive(Debug))]
enum OffchainErr {
DecodeWorkerStatus,
FailedSigning,
@@ -187,7 +191,7 @@ pub struct Heartbeat<BlockNumber>
pub trait Trait: system::Trait + session::historical::Trait {
/// The identifier type for an authority.
type AuthorityId: Member + Parameter + AppPublic + RuntimeAppPublic + Default;
type AuthorityId: Member + Parameter + RuntimeAppPublic + Default + Ord;
/// The overarching event type.
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
@@ -205,9 +209,6 @@ pub trait Trait: system::Trait + session::historical::Trait {
IdentificationTuple<Self>,
UnresponsivenessOffence<IdentificationTuple<Self>>,
>;
/// A type that returns a validator id from the current elected set of the era.
type CurrentElectedSet: CurrentElectedSet<<Self as session::Trait>::ValidatorId>;
}
decl_event!(
@@ -272,6 +273,10 @@ decl_module! {
&heartbeat.authority_index,
&network_state
);
} else if exists {
Err("Duplicated heartbeat.")?
} else {
Err("Non existent public key.")?
}
}
@@ -293,7 +298,7 @@ impl<T: Trait> Module<T> {
<ReceivedHeartbeats>::exists(&current_session, &authority_index)
}
fn offchain(now: T::BlockNumber) {
pub(crate) fn offchain(now: T::BlockNumber) {
let next_gossip = <GossipAt<T>>::get();
let check = Self::check_not_yet_gossipped(now, next_gossip);
let (curr_worker_status, not_yet_gossipped) = match check {
@@ -450,19 +455,16 @@ impl<T: Trait> session::OneSessionHandler<T::AccountId> for Module<T> {
let current_session = <session::Module<T>>::current_index();
let keys = Keys::<T>::get();
let current_elected = T::CurrentElectedSet::current_elected_set();
let current_validators = <session::Module<T>>::validators();
// The invariant is that these two are of the same length.
// TODO: What to do: Uncomment, ignore, a third option?
// assert_eq!(keys.len(), current_elected.len());
for (auth_idx, validator_id) in current_elected.into_iter().enumerate() {
for (auth_idx, validator_id) in current_validators.into_iter().enumerate() {
let auth_idx = auth_idx as u32;
if !<ReceivedHeartbeats>::exists(&current_session, &auth_idx) {
let exists = <ReceivedHeartbeats>::exists(&current_session, &auth_idx);
if !exists {
let full_identification = T::FullIdentificationOf::convert(validator_id.clone())
.expect(
"we got the validator_id from current_elected;
current_elected is set of currently elected validators;
"we got the validator_id from current_validators;
current_validators is set of currently acting validators;
the mapping between the validator id and its full identification should be valid;
thus `FullIdentificationOf::convert` can't return `None`;
qed",
@@ -472,6 +474,10 @@ impl<T: Trait> session::OneSessionHandler<T::AccountId> for Module<T> {
}
}
if unresponsive.is_empty() {
return;
}
let validator_set_count = keys.len() as u32;
let offence = UnresponsivenessOffence {
session_index: current_session,
@@ -533,6 +539,7 @@ impl<T: Trait> support::unsigned::ValidateUnsigned for Module<T> {
}
/// An offence that is filed if a validator didn't send a heartbeat message.
#[cfg_attr(feature = "std", derive(Clone, Debug, PartialEq, Eq))]
pub struct UnresponsivenessOffence<Offender> {
/// The current session index in which we report the unresponsive validators.
///
@@ -577,28 +584,3 @@ impl<Offender: Clone> Offence<Offender> for UnresponsivenessOffence<Offender> {
Perbill::from_parts(p as u32)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_unresponsiveness_slash_fraction() {
// A single case of unresponsiveness is not slashed.
assert_eq!(
UnresponsivenessOffence::<()>::slash_fraction(1, 50),
Perbill::zero(),
);
assert_eq!(
UnresponsivenessOffence::<()>::slash_fraction(3, 50),
Perbill::from_parts(6000000), // 0.6%
);
// One third offline should be punished around 5%.
assert_eq!(
UnresponsivenessOffence::<()>::slash_fraction(17, 50),
Perbill::from_parts(48000000), // 4.8%
);
}
}
+162
View File
@@ -0,0 +1,162 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Test utilities
#![cfg(test)]
use std::cell::RefCell;
use crate::{Module, Trait};
use sr_primitives::Perbill;
use sr_staking_primitives::{SessionIndex, offence::ReportOffence};
use sr_primitives::testing::{Header, UintAuthorityId, TestXt};
use sr_primitives::traits::{IdentityLookup, BlakeTwo256, ConvertInto};
use primitives::{H256, Blake2Hasher};
use support::{impl_outer_origin, impl_outer_dispatch, parameter_types};
use {runtime_io, system};
impl_outer_origin!{
pub enum Origin for Runtime {}
}
impl_outer_dispatch! {
pub enum Call for Runtime where origin: Origin {
imonline::ImOnline,
}
}
thread_local! {
pub static VALIDATORS: RefCell<Option<Vec<u64>>> = RefCell::new(Some(vec![1, 2, 3]));
}
pub struct TestOnSessionEnding;
impl session::OnSessionEnding<u64> for TestOnSessionEnding {
fn on_session_ending(_ending_index: SessionIndex, _will_apply_at: SessionIndex)
-> Option<Vec<u64>>
{
VALIDATORS.with(|l| l.borrow_mut().take())
}
}
impl session::historical::OnSessionEnding<u64, u64> for TestOnSessionEnding {
fn on_session_ending(_ending_index: SessionIndex, _will_apply_at: SessionIndex)
-> Option<(Vec<u64>, Vec<(u64, u64)>)>
{
VALIDATORS.with(|l| l
.borrow_mut()
.take()
.map(|validators| {
let full_identification = validators.iter().map(|v| (*v, *v)).collect();
(validators, full_identification)
})
)
}
}
/// An extrinsic type used for tests.
pub type Extrinsic = TestXt<Call, ()>;
type SubmitTransaction = system::offchain::TransactionSubmitter<(), Call, Extrinsic>;
type IdentificationTuple = (u64, u64);
type Offence = crate::UnresponsivenessOffence<IdentificationTuple>;
thread_local! {
pub static OFFENCES: RefCell<Vec<(Vec<u64>, Offence)>> = RefCell::new(vec![]);
}
/// A mock offence report handler.
pub struct OffenceHandler;
impl ReportOffence<u64, IdentificationTuple, Offence> for OffenceHandler {
fn report_offence(reporters: Vec<u64>, offence: Offence) {
OFFENCES.with(|l| l.borrow_mut().push((reporters, offence)));
}
}
pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
let t = system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
t.into()
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Runtime;
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const MaximumBlockWeight: u32 = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one();
}
impl system::Trait for Runtime {
type Origin = Origin;
type Index = u64;
type BlockNumber = u64;
type Call = Call;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type WeightMultiplierUpdate = ();
type Event = ();
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
type Version = ();
}
parameter_types! {
pub const Period: u64 = 1;
pub const Offset: u64 = 0;
}
impl session::Trait for Runtime {
type ShouldEndSession = session::PeriodicSessions<Period, Offset>;
type OnSessionEnding = session::historical::NoteHistoricalRoot<Runtime, TestOnSessionEnding>;
type SessionHandler = (ImOnline, );
type ValidatorId = u64;
type ValidatorIdOf = ConvertInto;
type Keys = UintAuthorityId;
type Event = ();
type SelectInitialValidators = ();
}
impl session::historical::Trait for Runtime {
type FullIdentification = u64;
type FullIdentificationOf = ConvertInto;
}
impl Trait for Runtime {
type AuthorityId = UintAuthorityId;
type Event = ();
type Call = Call;
type SubmitTransaction = SubmitTransaction;
type ReportUnresponsiveness = OffenceHandler;
}
/// Im Online module.
pub type ImOnline = Module<Runtime>;
pub type System = system::Module<Runtime>;
pub type Session = session::Module<Runtime>;
pub fn advance_session() {
let now = System::block_number();
System::set_block_number(now + 1);
Session::rotate_session();
assert_eq!(Session::current_index(), (now / Period::get()) as u32);
}
+218
View File
@@ -0,0 +1,218 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Tests for the im-online module.
#![cfg(test)]
use super::*;
use crate::mock::*;
use offchain::testing::TestOffchainExt;
use primitives::offchain::OpaquePeerId;
use runtime_io::with_externalities;
use support::{dispatch, assert_noop};
use sr_primitives::testing::UintAuthorityId;
#[test]
fn test_unresponsiveness_slash_fraction() {
// A single case of unresponsiveness is not slashed.
assert_eq!(
UnresponsivenessOffence::<()>::slash_fraction(1, 50),
Perbill::zero(),
);
assert_eq!(
UnresponsivenessOffence::<()>::slash_fraction(3, 50),
Perbill::from_parts(6000000), // 0.6%
);
// One third offline should be punished around 5%.
assert_eq!(
UnresponsivenessOffence::<()>::slash_fraction(17, 50),
Perbill::from_parts(48000000), // 4.8%
);
}
#[test]
fn should_report_offline_validators() {
with_externalities(&mut new_test_ext(), || {
// given
let block = 1;
System::set_block_number(block);
// buffer new validators
Session::rotate_session();
// enact the change and buffer another one
let validators = vec![1, 2, 3, 4, 5, 6];
VALIDATORS.with(|l| *l.borrow_mut() = Some(validators.clone()));
Session::rotate_session();
// when
// we end current session and start the next one
Session::rotate_session();
// then
let offences = OFFENCES.with(|l| l.replace(vec![]));
assert_eq!(offences, vec![
(vec![], UnresponsivenessOffence {
session_index: 2,
validator_set_count: 3,
offenders: vec![
(1, 1),
(2, 2),
(3, 3),
],
})
]);
// should not report when heartbeat is sent
for (idx, v) in validators.into_iter().take(4).enumerate() {
let _ = heartbeat(block, 3, idx as u32, v.into()).unwrap();
}
Session::rotate_session();
// then
let offences = OFFENCES.with(|l| l.replace(vec![]));
assert_eq!(offences, vec![
(vec![], UnresponsivenessOffence {
session_index: 3,
validator_set_count: 6,
offenders: vec![
(5, 5),
(6, 6),
],
})
]);
});
}
fn heartbeat(
block_number: u64,
session_index: u32,
authority_index: u32,
id: UintAuthorityId,
) -> dispatch::Result {
let heartbeat = Heartbeat {
block_number,
network_state: OpaqueNetworkState {
peer_id: OpaquePeerId(vec![1]),
external_addresses: vec![],
},
session_index,
authority_index,
};
let signature = id.sign(&heartbeat.encode()).unwrap();
ImOnline::heartbeat(
Origin::system(system::RawOrigin::None),
heartbeat,
signature
)
}
#[test]
fn should_mark_online_validator_when_heartbeat_is_received() {
with_externalities(&mut new_test_ext(), || {
advance_session();
// given
VALIDATORS.with(|l| *l.borrow_mut() = Some(vec![1, 2, 3, 4, 5, 6]));
assert_eq!(Session::validators(), Vec::<u64>::new());
// enact the change and buffer another one
advance_session();
assert_eq!(Session::current_index(), 2);
assert_eq!(Session::validators(), vec![1, 2, 3]);
assert!(!ImOnline::is_online_in_current_session(0));
assert!(!ImOnline::is_online_in_current_session(1));
assert!(!ImOnline::is_online_in_current_session(2));
// when
let _ = heartbeat(1, 2, 0, 1.into()).unwrap();
// then
assert!(ImOnline::is_online_in_current_session(0));
assert!(!ImOnline::is_online_in_current_session(1));
assert!(!ImOnline::is_online_in_current_session(2));
// and when
let _ = heartbeat(1, 2, 2, 3.into()).unwrap();
// then
assert!(ImOnline::is_online_in_current_session(0));
assert!(!ImOnline::is_online_in_current_session(1));
assert!(ImOnline::is_online_in_current_session(2));
});
}
#[test]
fn late_heartbeat_should_fail() {
with_externalities(&mut new_test_ext(), || {
advance_session();
// given
VALIDATORS.with(|l| *l.borrow_mut() = Some(vec![1, 2, 4, 4, 5, 6]));
assert_eq!(Session::validators(), Vec::<u64>::new());
// enact the change and buffer another one
advance_session();
assert_eq!(Session::current_index(), 2);
assert_eq!(Session::validators(), vec![1, 2, 3]);
// when
assert_noop!(heartbeat(1, 3, 0, 1.into()), "Outdated heartbeat received.");
assert_noop!(heartbeat(1, 1, 0, 1.into()), "Outdated heartbeat received.");
});
}
#[test]
fn should_generate_heartbeats() {
let mut ext = new_test_ext();
let (offchain, state) = TestOffchainExt::new();
ext.set_offchain_externalities(offchain);
with_externalities(&mut ext, || {
// given
let block = 1;
System::set_block_number(block);
// buffer new validators
Session::rotate_session();
// enact the change and buffer another one
VALIDATORS.with(|l| *l.borrow_mut() = Some(vec![1, 2, 3, 4, 5, 6]));
Session::rotate_session();
// when
UintAuthorityId::set_all_keys(vec![0, 1, 2]);
ImOnline::offchain(2);
// then
let transaction = state.write().transactions.pop().unwrap();
// All validators have `0` as their session key, so we generate 3 transactions.
assert_eq!(state.read().transactions.len(), 2);
// check stuff about the transaction.
let ex: Extrinsic = Decode::decode(&mut &*transaction).unwrap();
let heartbeat = match ex.1 {
crate::mock::Call::ImOnline(crate::Call::heartbeat(h, _)) => h,
e => panic!("Unexpected call: {:?}", e),
};
assert_eq!(heartbeat, Heartbeat {
block_number: 2,
network_state: runtime_io::network_state().unwrap(),
session_index: 2,
authority_index: 2,
});
});
}