Skip to content
Snippets Groups Projects
Commit ce5f317f authored by Cédric Moreau's avatar Cédric Moreau
Browse files

feat(smith-members): clippy

parent cb6ef553
No related branches found
No related tags found
No related merge requests found
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
//! The offences are executed here based. The offenders are disconnected and //! The offences are executed here based. The offenders are disconnected and
//! can be added to a blacklist to avoid futur connection. //! can be added to a blacklist to avoid futur connection.
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::type_complexity)] #![allow(clippy::type_complexity)]
use super::pallet::*; use super::pallet::*;
...@@ -56,7 +55,7 @@ where ...@@ -56,7 +55,7 @@ where
SlashStrategy::Blacklist => { SlashStrategy::Blacklist => {
for offender in offenders { for offender in offenders {
if let Some(member_id) = T::MemberIdOf::convert(offender.offender.0.clone()) { if let Some(member_id) = T::MemberIdOf::convert(offender.offender.0.clone()) {
T::OnBlacklistedMember::on_blacklisted_member(member_id.clone()); T::OnBlacklistedMember::on_blacklisted_member(member_id);
Blacklist::<T>::mutate(|blacklist| { Blacklist::<T>::mutate(|blacklist| {
if !blacklist.contains(&member_id) { if !blacklist.contains(&member_id) {
blacklist.push(member_id); blacklist.push(member_id);
......
...@@ -50,6 +50,7 @@ pub mod pallet { ...@@ -50,6 +50,7 @@ pub mod pallet {
use frame_support::traits::{StorageVersion, UnfilteredDispatchable}; use frame_support::traits::{StorageVersion, UnfilteredDispatchable};
use frame_system::pallet_prelude::*; use frame_system::pallet_prelude::*;
use sp_runtime::traits::{Convert, IsMember}; use sp_runtime::traits::{Convert, IsMember};
use sp_std::collections::btree_map::BTreeMap;
use sp_std::vec; use sp_std::vec;
/// The current storage version. /// The current storage version.
...@@ -547,10 +548,10 @@ impl<T: Config> pallet_session::SessionManager<T::ValidatorId> for Pallet<T> { ...@@ -547,10 +548,10 @@ impl<T: Config> pallet_session::SessionManager<T::ValidatorId> for Pallet<T> {
} }
for member_id in members_ids_to_add.iter() { for member_id in members_ids_to_add.iter() {
T::OnIncomingMember::on_incoming_member(member_id.clone()); T::OnIncomingMember::on_incoming_member(*member_id);
} }
for member_id in members_ids_to_del.iter() { for member_id in members_ids_to_del.iter() {
T::OnOutgoingMember::on_outgoing_member(member_id.clone()); T::OnOutgoingMember::on_outgoing_member(*member_id);
} }
// updates the list of OnlineAuthorities and returns the list of their key // updates the list of OnlineAuthorities and returns the list of their key
......
...@@ -160,6 +160,9 @@ impl pallet_authority_members::Config for Test { ...@@ -160,6 +160,9 @@ impl pallet_authority_members::Config for Test {
type RemoveMemberOrigin = system::EnsureRoot<u64>; type RemoveMemberOrigin = system::EnsureRoot<u64>;
type RuntimeEvent = RuntimeEvent; type RuntimeEvent = RuntimeEvent;
type WeightInfo = (); type WeightInfo = ();
type OnBlacklistedMember = ();
type OnIncomingMember = ();
type OnOutgoingMember = ();
} }
// Build genesis storage according to the mock runtime. // Build genesis storage according to the mock runtime.
......
...@@ -189,6 +189,9 @@ impl pallet_authority_members::Config for Test { ...@@ -189,6 +189,9 @@ impl pallet_authority_members::Config for Test {
type RemoveMemberOrigin = system::EnsureRoot<AccountId>; type RemoveMemberOrigin = system::EnsureRoot<AccountId>;
type RuntimeEvent = RuntimeEvent; type RuntimeEvent = RuntimeEvent;
type WeightInfo = (); type WeightInfo = ();
type OnOutgoingMember = ();
type OnIncomingMember = ();
type OnBlacklistedMember = ();
} }
parameter_types! { parameter_types! {
......
use crate::{Config, CurrentSession, Pallet}; use crate::{Config, CurrentSession, Pallet};
use frame_support::StorageValue;
use pallet_authority_members::SessionIndex; use pallet_authority_members::SessionIndex;
use sp_runtime::traits::Convert; use sp_runtime::traits::Convert;
/// We want to remove a Smith when he is removed from the higher level set of "authorities". /// We want to remove a Smith when he is removed from the higher level set of "authorities".
impl<T: Config> pallet_authority_members::OnRemovedMember<T> for Pallet<T> { impl<T: Config> pallet_authority_members::OnRemovedMember<T> for Pallet<T> {
fn on_removed_member(member_id: T) { fn on_removed_member(_: T) {
todo!("Remove smith as well") todo!("Remove smith as well")
// TODO: not sure if we should listen authority-members or rather the initial pallet that called the removal // TODO: not sure if we should listen authority-members or rather the initial pallet that called the removal
} }
......
...@@ -32,7 +32,7 @@ use frame_support::pallet_prelude::Get; ...@@ -32,7 +32,7 @@ use frame_support::pallet_prelude::Get;
use frame_support::{ensure, RuntimeDebug}; use frame_support::{ensure, RuntimeDebug};
use frame_system::ensure_signed; use frame_system::ensure_signed;
use frame_system::pallet_prelude::OriginFor; use frame_system::pallet_prelude::OriginFor;
use sp_runtime::traits::{AtLeast32BitUnsigned, Convert}; use sp_runtime::traits::AtLeast32BitUnsigned;
use sp_std::fmt::Debug; use sp_std::fmt::Debug;
use sp_std::prelude::*; use sp_std::prelude::*;
...@@ -60,16 +60,11 @@ pub enum SmithStatus { ...@@ -60,16 +60,11 @@ pub enum SmithStatus {
Excluded, Excluded,
} }
#[frame_support::pallet] #[frame_support::pallet(dev_mode)]
pub mod pallet { pub mod pallet {
use super::*; use super::*;
use super::*;
use crate::Call::certify_smith;
use frame_support::pallet_prelude::*; use frame_support::pallet_prelude::*;
use frame_support::traits::StorageVersion; use frame_support::traits::StorageVersion;
use frame_support::{pallet_prelude::*, traits::ReservableCurrency};
use frame_system::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use pallet_authority_members::SessionIndex; use pallet_authority_members::SessionIndex;
use sp_runtime::traits::{Convert, IsMember}; use sp_runtime::traits::{Convert, IsMember};
use std::collections::BTreeMap; use std::collections::BTreeMap;
...@@ -122,16 +117,12 @@ pub mod pallet { ...@@ -122,16 +117,12 @@ pub mod pallet {
#[pallet::generate_deposit(pub(super) fn deposit_event)] #[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> { pub enum Event<T: Config> {
/// A smith gathered enough certifications to become an authority (can call `go_online()`). /// A smith gathered enough certifications to become an authority (can call `go_online()`).
SmithBecameAuthority { SmithBecameAuthority { idty_index: T::IdtyIndex },
idty_index: T::IdtyIndex,
owner_key: T::AccountId,
},
} }
#[pallet::genesis_config] #[pallet::genesis_config]
pub struct GenesisConfig<T: Config> { pub struct GenesisConfig<T: Config> {
pub certs_by_receiver: pub certs_by_receiver: BTreeMap<T::IdtyIndex, Vec<T::IdtyIndex>>,
BTreeMap<T::IdtyIndex, BTreeMap<T::IdtyIndex, Option<T::BlockNumber>>>,
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
...@@ -151,12 +142,12 @@ pub mod pallet { ...@@ -151,12 +142,12 @@ pub mod pallet {
for (receiver, issuers) in &self.certs_by_receiver { for (receiver, issuers) in &self.certs_by_receiver {
// Forbid self-cert // Forbid self-cert
assert!( assert!(
!issuers.contains_key(receiver), !issuers.contains(receiver),
"Identity cannot certify it-self." "Identity cannot certify it-self."
); );
let mut issuers_: Vec<_> = Vec::with_capacity(issuers.len()); let mut issuers_: Vec<_> = Vec::with_capacity(issuers.len());
for (issuer, maybe_removable_on) in issuers { for issuer in issuers {
// Count issued certs // Count issued certs
cert_meta_by_issuer cert_meta_by_issuer
.entry(*issuer) .entry(*issuer)
...@@ -397,6 +388,11 @@ impl<T: Config> Pallet<T> { ...@@ -397,6 +388,11 @@ impl<T: Config> Pallet<T> {
// expiry postponed // expiry postponed
let new_expires_on = CurrentSession::<T>::get() + T::InactivityMaxDuration::get(); let new_expires_on = CurrentSession::<T>::get() + T::InactivityMaxDuration::get();
maybe_smith_meta.expires_on = Some(new_expires_on); maybe_smith_meta.expires_on = Some(new_expires_on);
if maybe_smith_meta.status == SmithStatus::Smith {
Self::deposit_event(Event::<T>::SmithBecameAuthority {
idty_index: receiver,
});
}
// TODO: unschedule old expiry // TODO: unschedule old expiry
}); });
} }
...@@ -415,7 +411,7 @@ impl<T: Config> Pallet<T> { ...@@ -415,7 +411,7 @@ impl<T: Config> Pallet<T> {
maybe_smith_meta.expires_on = None; maybe_smith_meta.expires_on = None;
maybe_smith_meta.status = SmithStatus::Excluded; maybe_smith_meta.status = SmithStatus::Excluded;
for cert in &maybe_smith_meta.received_certs { for cert in &maybe_smith_meta.received_certs {
lost_certs.push(cert.clone()); lost_certs.push(*cert);
} }
maybe_smith_meta.received_certs = vec![]; maybe_smith_meta.received_certs = vec![];
// N.B.: the issued certs are kept in case the smith joins back // N.B.: the issued certs are kept in case the smith joins back
...@@ -476,7 +472,7 @@ impl<T: Config> Pallet<T> { ...@@ -476,7 +472,7 @@ impl<T: Config> Pallet<T> {
} }
// TODO: return what? // TODO: return what?
fn smith_goes_blacklisted(idty_index: T::IdtyIndex) { fn smith_goes_blacklisted(_idty_index: T::IdtyIndex) {
// TODO: for now, just let smith_goes_offline do the job // TODO: for now, just let smith_goes_offline do the job
// if let Some(_) = Smiths::<T>::get(idty_index) { // if let Some(_) = Smiths::<T>::get(idty_index) {
// Smiths::<T>::remove(idty_index); // Smiths::<T>::remove(idty_index);
...@@ -494,7 +490,7 @@ impl<T: Config> Pallet<T> { ...@@ -494,7 +490,7 @@ impl<T: Config> Pallet<T> {
for _ in smith.received_certs { for _ in smith.received_certs {
count += 1; count += 1;
} }
return count >= T::MinCertForMembership::get(); count >= T::MinCertForMembership::get()
} }
} }
......
...@@ -16,25 +16,20 @@ ...@@ -16,25 +16,20 @@
#![cfg(test)] #![cfg(test)]
use crate::traits::OnSmithDelete;
use crate::{self as pallet_smith_members}; use crate::{self as pallet_smith_members};
use frame_support::pallet_prelude::{GenesisBuild, Hooks};
use frame_support::{ use frame_support::{
parameter_types, parameter_types,
traits::{ConstU32, ConstU64}, traits::{ConstU32, ConstU64},
weights::{constants::RocksDbWeight, Weight}, weights::{constants::RocksDbWeight, Weight},
}; };
use pallet_balances::pallet;
use sp_core::H256; use sp_core::H256;
use sp_runtime::traits::{ConstBool, ConvertInto, IsMember}; use sp_runtime::traits::{ConvertInto, IsMember};
use sp_runtime::{ use sp_runtime::{
testing::Header, testing::Header,
traits::{BlakeTwo256, IdentityLookup}, traits::{BlakeTwo256, IdentityLookup},
BuildStorage, Perbill, BuildStorage, Perbill,
}; };
pub struct OnOffenceHandler;
parameter_types! { parameter_types! {
pub static OnOffencePerbill: Vec<Perbill> = Default::default(); pub static OnOffencePerbill: Vec<Perbill> = Default::default();
pub static OffenceWeight: Weight = Default::default(); pub static OffenceWeight: Weight = Default::default();
...@@ -94,7 +89,6 @@ impl pallet_smith_members::Config for Runtime { ...@@ -94,7 +89,6 @@ impl pallet_smith_members::Config for Runtime {
type IsWoTMember = EveryoneExceptIdZero; type IsWoTMember = EveryoneExceptIdZero;
type IdtyIdOf = ConvertInto; type IdtyIdOf = ConvertInto;
type MinCertForMembership = ConstU32<2>; type MinCertForMembership = ConstU32<2>;
type MinCertForCreateIdtyRight = ConstU32<3>;
type MaxByIssuer = ConstU32<3>; type MaxByIssuer = ConstU32<3>;
type InactivityMaxDuration = ConstU32<5>; type InactivityMaxDuration = ConstU32<5>;
type OnSmithDelete = (); type OnSmithDelete = ();
......
...@@ -28,17 +28,10 @@ use pallet_authority_members::OnNewSession; ...@@ -28,17 +28,10 @@ use pallet_authority_members::OnNewSession;
fn process_to_become_a_smith_and_lose_it() { fn process_to_become_a_smith_and_lose_it() {
new_test_ext(GenesisConfig { new_test_ext(GenesisConfig {
certs_by_receiver: btreemap![ certs_by_receiver: btreemap![
1 => btreemap![ 1 => vec![2, 3, 4],
2 => None, 2 => vec![3, 4],
3 => None, 3 => vec![],
4 => None, 4 => vec![],
],
2 => btreemap![
3 => None,
4 => None,
],
3 => btreemap![],
4 => btreemap![],
], ],
}) })
.execute_with(|| { .execute_with(|| {
...@@ -91,9 +84,9 @@ fn process_to_become_a_smith_and_lose_it() { ...@@ -91,9 +84,9 @@ fn process_to_become_a_smith_and_lose_it() {
// On session 4 everything if fine // On session 4 everything if fine
Pallet::<Runtime>::on_new_session(4); Pallet::<Runtime>::on_new_session(4);
assert_eq!(Smiths::<Runtime>::get(1).is_some(), true); assert!(Smiths::<Runtime>::get(1).is_some());
assert_eq!(Smiths::<Runtime>::get(2).is_some(), true); assert!(Smiths::<Runtime>::get(2).is_some());
assert_eq!(Smiths::<Runtime>::get(5).is_some(), true); assert!(Smiths::<Runtime>::get(5).is_some());
// On session 5 no more smiths because of lack of activity // On session 5 no more smiths because of lack of activity
Pallet::<Runtime>::on_new_session(5); Pallet::<Runtime>::on_new_session(5);
assert_eq!( assert_eq!(
...@@ -130,22 +123,10 @@ fn process_to_become_a_smith_and_lose_it() { ...@@ -130,22 +123,10 @@ fn process_to_become_a_smith_and_lose_it() {
fn should_have_checks_on_certify() { fn should_have_checks_on_certify() {
new_test_ext(GenesisConfig { new_test_ext(GenesisConfig {
certs_by_receiver: btreemap![ certs_by_receiver: btreemap![
1 => btreemap![ 1 => vec![2, 3, 4],
2 => None, 2 => vec![3, 4],
3 => None, 3 => vec![4],
4 => None, 4 => vec![1, 2],
],
2 => btreemap![
3 => None,
4 => None,
],
3 => btreemap![
4 => None,
],
4 => btreemap![
1 => None,
2 => None,
],
], ],
}) })
.execute_with(|| { .execute_with(|| {
...@@ -237,24 +218,17 @@ fn should_have_checks_on_certify() { ...@@ -237,24 +218,17 @@ fn should_have_checks_on_certify() {
fn smith_activity_postpones_expiration() { fn smith_activity_postpones_expiration() {
new_test_ext(GenesisConfig { new_test_ext(GenesisConfig {
certs_by_receiver: btreemap![ certs_by_receiver: btreemap![
1 => btreemap![ 1 => vec![2, 3, 4],
2 => None, 2 => vec![3, 4],
3 => None, 3 => vec![],
4 => None, 4 => vec![]
],
2 => btreemap![
3 => None,
4 => None,
],
3 => btreemap![],
4 => btreemap![]
], ],
}) })
.execute_with(|| { .execute_with(|| {
// On session 4 everything is fine // On session 4 everything is fine
Pallet::<Runtime>::on_new_session(4); Pallet::<Runtime>::on_new_session(4);
assert_eq!(Smiths::<Runtime>::get(1).is_some(), true); assert!(Smiths::<Runtime>::get(1).is_some());
assert_eq!(Smiths::<Runtime>::get(2).is_some(), true); assert!(Smiths::<Runtime>::get(2).is_some());
// Smith #2 is online but not #1 // Smith #2 is online but not #1
Pallet::<Runtime>::smith_goes_online(2); Pallet::<Runtime>::smith_goes_online(2);
...@@ -331,20 +305,10 @@ fn smith_activity_postpones_expiration() { ...@@ -331,20 +305,10 @@ fn smith_activity_postpones_expiration() {
fn smith_coming_back_recovers_its_issued_certs() { fn smith_coming_back_recovers_its_issued_certs() {
new_test_ext(GenesisConfig { new_test_ext(GenesisConfig {
certs_by_receiver: btreemap![ certs_by_receiver: btreemap![
1 => btreemap![ 1 => vec![2, 3, 4],
2 => None, 2 => vec![3, 4],
3 => None, 3 => vec![1, 4],
4 => None, 4 => vec![]
],
2 => btreemap![
3 => None,
4 => None,
],
3 => btreemap![
4 => None,
1 => None,
],
4 => btreemap![]
], ],
}) })
.execute_with(|| { .execute_with(|| {
......
...@@ -241,6 +241,9 @@ macro_rules! pallets_config { ...@@ -241,6 +241,9 @@ macro_rules! pallets_config {
type MaxAuthorities = MaxAuthorities; type MaxAuthorities = MaxAuthorities;
type RemoveMemberOrigin = EnsureRoot<Self::AccountId>; type RemoveMemberOrigin = EnsureRoot<Self::AccountId>;
type WeightInfo = common_runtime::weights::pallet_authority_members::WeightInfo<Runtime>; type WeightInfo = common_runtime::weights::pallet_authority_members::WeightInfo<Runtime>;
type OnBlacklistedMember = ();
type OnIncomingMember = ();
type OnOutgoingMember = ();
} }
impl pallet_authorship::Config for Runtime { impl pallet_authorship::Config for Runtime {
type EventHandler = ImOnline; type EventHandler = ImOnline;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment