New Pallet: Root offences (#11943)

* root-offences pallet

* fix errors

* cleaned up a bit

* remove unwrap()

* new pallet is getting compiled

* remove unnecessary type annotations

* remove more unnecessary type annotations

* addidtional cleaning

* commit

* cleaned up

* fix in logic

* add event

* removed Clone trait from AccountId

* test module

* remove unused imports

* fmt

* fix

* separate into functions, still messy

* test

* first test

* fmt

* cleaned up a bit

* separate into mock.rs and tests.rs

* basic docs for now

* pallet_staking GenesisiConfig

* fix

* added start_session

* passing tests

* impl GenesisConfig for pallet_session

* updated event

* Update frame/root-offences/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update frame/root-offences/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* remove <T: Config

* specifying trait bounds inside Config

* commit

* active era increases correctly :)

* ExtBuilder

* slashing works

* new test

* additional test

* commit

* order

* fix?

* fix in logic

* remove unnecessary

* wrap comment at 100

* fmt

* merge fixes

* Update frame/root-offences/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update frame/root-offences/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* docs

* Update frame/root-offences/README.md

Co-authored-by: Andronik <write@reusable.software>

* Update frame/root-offences/Cargo.toml

Co-authored-by: Andronik <write@reusable.software>

* license header

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: Andronik <write@reusable.software>
This commit is contained in:
Sergej Sakac
2022-09-28 22:04:14 +02:00
committed by GitHub
parent bcf38c89a2
commit ea43466879
8 changed files with 661 additions and 1 deletions
@@ -0,0 +1,94 @@
// This file is part of Substrate.
// Copyright (C) 2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use super::*;
use frame_support::{assert_err, assert_ok};
use mock::{active_era, start_session, Balances, ExtBuilder, RootOffences, RuntimeOrigin, System};
#[test]
fn create_offence_fails_given_signed_origin() {
use sp_runtime::traits::BadOrigin;
ExtBuilder::default().build_and_execute(|| {
let offenders = (&[]).to_vec();
assert_err!(RootOffences::create_offence(RuntimeOrigin::signed(1), offenders), BadOrigin);
})
}
#[test]
fn create_offence_works_given_root_origin() {
ExtBuilder::default().build_and_execute(|| {
start_session(1);
assert_eq!(active_era(), 0);
assert_eq!(Balances::free_balance(11), 1000);
let offenders = [(11, Perbill::from_percent(50))].to_vec();
assert_ok!(RootOffences::create_offence(RuntimeOrigin::root(), offenders.clone()));
System::assert_last_event(Event::OffenceCreated { offenders }.into());
// the slash should be applied right away.
assert_eq!(Balances::free_balance(11), 500);
// the other validator should keep his balance, because we only created
// an offences for the first validator.
assert_eq!(Balances::free_balance(21), 1000);
})
}
#[test]
fn create_offence_wont_slash_non_active_validators() {
ExtBuilder::default().build_and_execute(|| {
start_session(1);
assert_eq!(active_era(), 0);
// 31 is not an active validator.
assert_eq!(Balances::free_balance(31), 500);
let offenders = [(31, Perbill::from_percent(20)), (11, Perbill::from_percent(20))].to_vec();
assert_ok!(RootOffences::create_offence(RuntimeOrigin::root(), offenders.clone()));
System::assert_last_event(Event::OffenceCreated { offenders }.into());
// so 31 didn't get slashed.
assert_eq!(Balances::free_balance(31), 500);
// but 11 is an active validator so he got slashed.
assert_eq!(Balances::free_balance(11), 800);
})
}
#[test]
fn create_offence_wont_slash_idle() {
ExtBuilder::default().build_and_execute(|| {
start_session(1);
assert_eq!(active_era(), 0);
// 41 is idle.
assert_eq!(Balances::free_balance(41), 1000);
let offenders = [(41, Perbill::from_percent(50))].to_vec();
assert_ok!(RootOffences::create_offence(RuntimeOrigin::root(), offenders.clone()));
System::assert_last_event(Event::OffenceCreated { offenders }.into());
// 41 didn't get slashed.
assert_eq!(Balances::free_balance(41), 1000);
})
}