From 64bc018b452f1cfbcea1de56b5a849e16e5eaf61 Mon Sep 17 00:00:00 2001 From: cgeek <cem.moreau@gmail.com> Date: Tue, 9 Jan 2024 13:22:42 +0100 Subject: [PATCH] fix(#148): remove AuthoritiesCounter --- pallets/authority-members/src/lib.rs | 22 +++++++--------------- resources/metadata.scale | Bin 132226 -> 132165 bytes 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/pallets/authority-members/src/lib.rs b/pallets/authority-members/src/lib.rs index 6858083e1..16ff0eb48 100644 --- a/pallets/authority-members/src/lib.rs +++ b/pallets/authority-members/src/lib.rs @@ -116,7 +116,6 @@ pub mod pallet { .collect::<Vec<T::MemberId>>(); members_ids.sort(); - AuthoritiesCounter::<T>::put(members_ids.len() as u32); OnlineAuthorities::<T>::put(members_ids.clone()); } } @@ -129,11 +128,6 @@ pub mod pallet { pub type AccountIdOf<T: Config> = StorageMap<_, Twox64Concat, T::MemberId, T::AccountId, OptionQuery>; - /// count the number of authorities - #[pallet::storage] - #[pallet::getter(fn authorities_counter)] - pub type AuthoritiesCounter<T: Config> = StorageValue<_, u32, ValueQuery>; - /// list incoming authorities #[pallet::storage] #[pallet::getter(fn incoming)] @@ -270,7 +264,7 @@ pub mod pallet { if Self::is_online(member_id) && !is_outgoing { return Err(Error::<T>::AlreadyOnline.into()); } - if AuthoritiesCounter::<T>::get() >= T::MaxAuthorities::get() { + if Self::authorities_counter() >= T::MaxAuthorities::get() { return Err(Error::<T>::TooManyAuthorities.into()); } @@ -377,6 +371,12 @@ pub mod pallet { Ok(().into()) } + + pub fn authorities_counter() -> u32 { + let count = OnlineAuthorities::<T>::get().len() + IncomingAuthorities::<T>::get().len() + - OutgoingAuthorities::<T>::get().len(); + count as u32 + } } // INTERNAL FUNCTIONS // @@ -420,7 +420,6 @@ pub mod pallet { } }); if not_already_inserted { - AuthoritiesCounter::<T>::mutate(|counter| *counter += 1); Self::deposit_event(Event::MemberGoOnline { member: member_id }); } not_already_inserted @@ -436,11 +435,6 @@ pub mod pallet { } }); if not_already_inserted { - AuthoritiesCounter::<T>::mutate(|counter| { - if *counter > 0 { - *counter -= 1 - } - }); Self::deposit_event(Event::MemberGoOffline { member: member_id }); } not_already_inserted @@ -469,7 +463,6 @@ pub mod pallet { } /// perform removal from incoming authorities fn remove_in(member_id: T::MemberId) { - AuthoritiesCounter::<T>::mutate(|counter| counter.saturating_sub(1)); IncomingAuthorities::<T>::mutate(|members_ids| { if let Ok(index) = members_ids.binary_search(&member_id) { members_ids.remove(index); @@ -478,7 +471,6 @@ pub mod pallet { } /// perform removal from online authorities fn remove_online(member_id: T::MemberId) { - AuthoritiesCounter::<T>::mutate(|counter| counter.saturating_add(1)); OnlineAuthorities::<T>::mutate(|members_ids| { if let Ok(index) = members_ids.binary_search(&member_id) { members_ids.remove(index); diff --git a/resources/metadata.scale b/resources/metadata.scale index 919f543f21a57970a0b67040b2ff16e6d54034ec..6f5b7249e7b9c59c5bc79fbd697f8dac01b34ef1 100644 GIT binary patch delta 33 pcmZqb<T%>F(XfTl=nSL8bmKFO#*-b@MW)N1W#rv{<P76Ja{$lF3|asH delta 90 zcmX@w!O_&o(XfTl=nSLGbmKFO#<m`gr6n2pMVTd;sm0FurFkW(MT`sr0t^fc3@i-_ k$siGhl8jV^ywcpH)FOraG=)TjhRKfVBHOFZFrGFC0CGJaMgRZ+ -- GitLab