Lines
100 %
Functions
Branches
// Copyright (C) Moondance Labs Ltd.
// This file is part of Tanssi.
// Tanssi 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.
// Tanssi 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 Tanssi. If not, see <http://www.gnu.org/licenses/>
use crate::OfflineStatus;
use {super::*, crate::EnableMarkingOffline};
fn make_collator_inactive(collator: AccountId) {
run_to_block(u64::from(get_max_inactive_sessions()) * 5u64);
for session_index in 0..get_max_inactive_sessions() {
InactiveCollators::<Test>::insert(session_index, get_collator_set(vec![collator]));
}
#[test]
fn enabling_and_disabling_offline_marking_works() {
ExtBuilder.build().execute_with(|| {
assert!(!EnableMarkingOffline::<Test>::get());
assert_ok!(Pallet::<Test>::enable_offline_marking(
RuntimeOrigin::root(),
true
));
assert!(EnableMarkingOffline::<Test>::get());
false
});
fn enabling_and_disabling_offline_marking_fails_for_non_root() {
assert_noop!(
Pallet::<Test>::enable_offline_marking(RuntimeOrigin::signed(COLLATOR_1), true),
BadOrigin
);
fn set_offline_works() {
assert!(OfflineCollators::<Test>::get(COLLATOR_1).is_none());
assert_ok!(Pallet::<Test>::set_offline(RuntimeOrigin::signed(
COLLATOR_1
)));
System::assert_last_event(
Event::CollatorStatusUpdated {
collator: COLLATOR_1,
is_offline: true,
.into(),
assert!(OfflineCollators::<Test>::get(COLLATOR_1).is_some());
assert_eq!(
OfflineCollators::<Test>::get(COLLATOR_1),
Some(OfflineStatus::Disabled)
fn set_offline_fails_if_offline_marking_is_not_enabled() {
Pallet::<Test>::set_offline(RuntimeOrigin::signed(COLLATOR_1)),
Error::<Test>::MarkingOfflineNotEnabled
fn set_offline_fails_if_collator_is_not_in_eligible_candidates() {
Pallet::<Test>::set_offline(RuntimeOrigin::signed(COLLATOR_3)),
Error::<Test>::CollatorNotEligibleCandidate
fn set_offline_fails_for_offline_collators() {
OfflineCollators::<Test>::insert(COLLATOR_1, OfflineStatus::Disabled);
Error::<Test>::CollatorNotOnline
OfflineCollators::<Test>::insert(
COLLATOR_1,
OfflineStatus::Notified { cooldown_end: 0u32 },
fn set_offline_fails_if_collator_is_invulnerable() {
Pallet::<Test>::set_offline(RuntimeOrigin::signed(COLLATOR_2)),
Error::<Test>::MarkingInvulnerableOfflineInvalid
fn set_online_works_for_notified_offline_collator() {
// We need to advance to at least session 1 so that the cooldown period has passed
run_to_block(5u64);
assert_ok!(Pallet::<Test>::set_online(RuntimeOrigin::signed(
is_offline: false,
fn set_online_works_for_disabled_offline_collator() {
fn set_online_fails_for_online_collators() {
Pallet::<Test>::set_online(RuntimeOrigin::signed(COLLATOR_1)),
Error::<Test>::CollatorNotOffline
fn set_online_fails_for_offline_collator_within_cooldown_period() {
OfflineStatus::Notified { cooldown_end: 1u32 },
Error::<Test>::CollatorNotReadyToBeOnline
fn notify_inactive_collator_works() {
make_collator_inactive(COLLATOR_1);
assert_eq!(CurrentSessionIndexGetter::session_index(), 2);
assert_ok!(Pallet::<Test>::notify_inactive_collator(
RuntimeOrigin::signed(COLLATOR_3),
// Since we are currently in session 2 and the cooldown period is 1 session
// the collator should be marked as offline util session 3
Some(OfflineStatus::Notified { cooldown_end: 3u32 })
fn notify_inactive_collator_fails_if_collator_is_active() {
Pallet::<Test>::notify_inactive_collator(RuntimeOrigin::signed(COLLATOR_3), COLLATOR_1),
Error::<Test>::CollatorCannotBeNotifiedAsInactive
fn notify_inactive_collator_fails_if_offline_marking_is_not_enabled() {
fn notify_inactive_collator_fails_for_collator_not_in_sorted_eligible_collators() {
make_collator_inactive(COLLATOR_3);
Pallet::<Test>::notify_inactive_collator(RuntimeOrigin::signed(COLLATOR_2), COLLATOR_3),
fn notify_inactive_collator_fails_for_notified_offline_collators() {
Error::<Test>::CollatorAlreadyNotifiedOffline
fn notify_inactive_collator_fails_if_collator_is_invulnerable() {
make_collator_inactive(COLLATOR_2);
Pallet::<Test>::notify_inactive_collator(RuntimeOrigin::signed(COLLATOR_3), COLLATOR_2),
fn calling_set_online_after_set_offline_works() {
fn calling_set_online_after_notify_inactive_collator_fails() {
fn notify_inactive_collator_overrides_disabled_offline_collator_record() {