diff --git a/node/src/chain_spec/gdev.rs b/node/src/chain_spec/gdev.rs index 52bea218a5c48721aba9e036bd8989ea0413b950..6e51deef86e7768fa552b47957fbe5d1d84c5640 100644 --- a/node/src/chain_spec/gdev.rs +++ b/node/src/chain_spec/gdev.rs @@ -16,6 +16,7 @@ use super::*; use common_runtime::constants::*; +use common_runtime::entities::IdtyData; use common_runtime::*; use gdev_runtime::{ opaque::SessionKeys, AccountConfig, AccountId, AuthorityMembersConfig, BabeConfig, @@ -377,7 +378,7 @@ fn gen_genesis_for_local_chain( owner_key: owner_key.clone(), removable_on: 0, status: IdtyStatus::Validated, - data: IdtyData::min(), + data: IdtyData::new(), }, }) .collect(), @@ -503,7 +504,7 @@ fn genesis_data_to_gdev_genesis_conf( owner_key: pubkey, removable_on: 0, status: IdtyStatus::Validated, - data: IdtyData::min(), + data: IdtyData::new(), }, }) .collect(), diff --git a/pallets/universal-dividend/src/types.rs b/pallets/universal-dividend/src/types.rs index 837efa9dea44da5189c320189acb1212dc0cbabc..0f4564e65291f5200a2965797098350f246359ef 100644 --- a/pallets/universal-dividend/src/types.rs +++ b/pallets/universal-dividend/src/types.rs @@ -18,12 +18,13 @@ use codec::{Decode, Encode, Error, Input, MaxEncodedLen, Output}; use core::num::NonZeroU16; #[cfg(feature = "std")] use serde::{Deserialize, Serialize}; +use sp_runtime::RuntimeDebug; use sp_std::vec::Vec; pub type UdIndex = u16; #[cfg_attr(feature = "std", derive(Deserialize, Serialize))] -#[derive(Clone, Copy, Default, Eq, PartialEq)] +#[derive(Clone, Copy, Default, Eq, PartialEq, RuntimeDebug)] pub struct FirstEligibleUd(pub Option<NonZeroU16>); #[cfg(feature = "std")] diff --git a/runtime/common/src/entities.rs b/runtime/common/src/entities.rs index df1f72cd096ba743007662953314262290a27975..bcd294197a5a5c4218cbe4d57b72fa483741fa5c 100644 --- a/runtime/common/src/entities.rs +++ b/runtime/common/src/entities.rs @@ -55,6 +55,27 @@ macro_rules! declare_session_keys { } } +#[cfg_attr(feature = "std", derive(Deserialize, Serialize))] +#[derive(Clone, Encode, Decode, Default, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)] +pub struct IdtyData { + pub first_eligible_ud: pallet_universal_dividend::FirstEligibleUd, +} + +#[cfg(feature = "std")] +impl IdtyData { + pub fn new() -> Self { + Self { + first_eligible_ud: pallet_universal_dividend::FirstEligibleUd::min(), + } + } +} + +impl From<IdtyData> for pallet_universal_dividend::FirstEligibleUd { + fn from(idty_data: IdtyData) -> Self { + idty_data.first_eligible_ud + } +} + #[cfg_attr(feature = "std", derive(Deserialize, Serialize))] #[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo)] pub struct SmithsMembershipMetaData<SessionKeysWrapper> { diff --git a/runtime/common/src/handlers.rs b/runtime/common/src/handlers.rs index b5f407057c184bc03a84b2aa029be40594130ba4..92a897143d74e8bdbca0cfacec4e8d294d8da757 100644 --- a/runtime/common/src/handlers.rs +++ b/runtime/common/src/handlers.rs @@ -15,7 +15,7 @@ // along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>. use super::entities::*; -use super::{AccountId, IdtyData, IdtyIndex}; +use super::{AccountId, IdtyIndex}; use frame_support::dispatch::UnfilteredDispatchable; use frame_support::instances::{Instance1, Instance2}; use frame_support::pallet_prelude::Weight; @@ -52,15 +52,18 @@ impl< sp_membership::Event::MembershipAcquired(idty_index, _owner_key) => { pallet_identity::Identities::<Runtime>::mutate_exists(idty_index, |idty_val_opt| { if let Some(ref mut idty_val) = idty_val_opt { - idty_val.data = - pallet_universal_dividend::Pallet::<Runtime>::init_first_eligible_ud(); + idty_val.data = IdtyData { + first_eligible_ud: + pallet_universal_dividend::Pallet::<Runtime>::init_first_eligible_ud( + ), + } } }); Runtime::DbWeight::get().reads_writes(1, 1) } sp_membership::Event::MembershipRevoked(idty_index) => { if let Some(idty_value) = pallet_identity::Identities::<Runtime>::get(idty_index) { - if let Some(first_ud_index) = idty_value.data.into() { + if let Some(first_ud_index) = idty_value.data.first_eligible_ud.into() { pallet_universal_dividend::Pallet::<Runtime>::on_removed_member( first_ud_index, &idty_value.owner_key, diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 96daf2186c4e7e253b24d34ca0fc5c3ac4e168d3..c1d3becf48665f29a88b66b71b97516c030272a9 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -63,9 +63,6 @@ pub type Signature = sp_runtime::MultiSignature; /// Index of an identity pub type IdtyIndex = u32; -/// Identity data -pub type IdtyData = pallet_universal_dividend::FirstEligibleUd; - pub struct FullIdentificationOfImpl; impl sp_runtime::traits::Convert<AccountId, Option<entities::ValidatorFullIdentification>> for FullIdentificationOfImpl diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs index eb599c40ed5fdf81a091c236e8d1d4421f8466b0..93c7aece4cf69c8ae3527e0d6b8c8e213884efac 100644 --- a/runtime/common/src/pallets_config.rs +++ b/runtime/common/src/pallets_config.rs @@ -399,8 +399,8 @@ macro_rules! pallets_config { type Event = Event; type MaxPastReeval = frame_support::traits::ConstU32<4>; type MembersCount = MembersCount; - type MembersStorage = Identity; - type MembersStorageIter = common_runtime::providers::IdtyDataIter<Runtime>; + type MembersStorage = common_runtime::providers::UdMembersStorage<Runtime>; + type MembersStorageIter = common_runtime::providers::UdMembersStorageIter<Runtime>; type SquareMoneyGrowthRate = SquareMoneyGrowthRate; type UdCreationPeriod = UdCreationPeriod; type UdReevalPeriod = UdReevalPeriod; diff --git a/runtime/common/src/providers.rs b/runtime/common/src/providers.rs index 193a9bd98fc9c6d558d7c7f6748529fef3454243..3e32943062b3f258ff3796b490a83de9f7501edd 100644 --- a/runtime/common/src/providers.rs +++ b/runtime/common/src/providers.rs @@ -14,8 +14,9 @@ // You should have received a copy of the GNU Affero General Public License // along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>. -use crate::{AccountId, IdtyIndex}; +use crate::{entities::IdtyData, AccountId, IdtyIndex}; use core::marker::PhantomData; +use pallet_universal_dividend::FirstEligibleUd; use sp_std::boxed::Box; use sp_std::vec::Vec; @@ -32,13 +33,42 @@ impl< } } +pub struct UdMembersStorage<T: pallet_identity::Config>(PhantomData<T>); + +impl<T> frame_support::traits::StoredMap<AccountId, FirstEligibleUd> for UdMembersStorage<T> +where + T: frame_system::Config<AccountId = AccountId>, + T: pallet_identity::Config<IdtyData = IdtyData>, +{ + fn get(key: &T::AccountId) -> FirstEligibleUd { + pallet_identity::Pallet::<T>::get(key).first_eligible_ud + } + fn try_mutate_exists<R, E: From<sp_runtime::DispatchError>>( + key: &T::AccountId, + f: impl FnOnce(&mut Option<FirstEligibleUd>) -> Result<R, E>, + ) -> Result<R, E> { + pallet_identity::Pallet::<T>::try_mutate_exists(key, |maybe_idty_data| { + if let Some(ref mut idty_data) = maybe_idty_data { + let mut maybe_first_eligible_ud = Some(idty_data.first_eligible_ud); + let result = f(&mut maybe_first_eligible_ud)?; + if let Some(first_eligible_ud) = maybe_first_eligible_ud { + idty_data.first_eligible_ud = first_eligible_ud; + } + Ok(result) + } else { + f(&mut None) + } + }) + } +} + #[allow(clippy::type_complexity)] -pub struct IdtyDataIter<T: pallet_identity::Config>( +pub struct UdMembersStorageIter<T: pallet_identity::Config>( Box<dyn Iterator<Item = pallet_identity::IdtyValue<T::BlockNumber, T::AccountId, T::IdtyData>>>, PhantomData<T>, ); -impl<T: pallet_identity::Config> From<Option<Vec<u8>>> for IdtyDataIter<T> { +impl<T: pallet_identity::Config> From<Option<Vec<u8>>> for UdMembersStorageIter<T> { fn from(maybe_key: Option<Vec<u8>>) -> Self { let mut iter = pallet_identity::Identities::<T>::iter_values(); if let Some(key) = maybe_key { @@ -48,15 +78,19 @@ impl<T: pallet_identity::Config> From<Option<Vec<u8>>> for IdtyDataIter<T> { } } -impl<T: pallet_identity::Config> Iterator for IdtyDataIter<T> { - type Item = (T::AccountId, T::IdtyData); +impl<T> Iterator for UdMembersStorageIter<T> +where + T: pallet_identity::Config, + T::IdtyData: Into<FirstEligibleUd>, +{ + type Item = (T::AccountId, FirstEligibleUd); fn next(&mut self) -> Option<Self::Item> { if let Some(pallet_identity::IdtyValue { owner_key, data, .. }) = self.0.next() { - Some((owner_key, data)) + Some((owner_key, data.into())) } else { None } diff --git a/runtime/common/src/weights/pallet_universal_dividend.rs b/runtime/common/src/weights/pallet_universal_dividend.rs index 9a1b5c98006da1fe2eeb8effbe9708e209b6a882..9f196cb7f02e44fa0c782f7a5b7a3af48f0fab0c 100644 --- a/runtime/common/src/weights/pallet_universal_dividend.rs +++ b/runtime/common/src/weights/pallet_universal_dividend.rs @@ -74,7 +74,7 @@ impl<T: frame_system::Config> pallet_universal_dividend::WeightInfo for WeightIn // Storage: UniversalDividend CurrentUdIndex (r:1 w:0) // Storage: UniversalDividend PastReevals (r:1 w:0) fn claim_uds(n: u32) -> Weight { - (1_221_776_000 as Weight) + (1_228_876_000 as Weight) // Standard Error: 958_000 .saturating_add((7_709_000 as Weight).saturating_mul(n as Weight)) .saturating_add(T::DbWeight::get().reads(4 as Weight)) diff --git a/runtime/g1/src/lib.rs b/runtime/g1/src/lib.rs index 5a28e63f19bfd5b5a0c85a112298e1f936b9e47d..7e05a45b77e1e3b3971fdad75b231b4b790d6d4f 100644 --- a/runtime/g1/src/lib.rs +++ b/runtime/g1/src/lib.rs @@ -27,8 +27,7 @@ pub mod parameters; pub use self::parameters::*; pub use common_runtime::{ constants::*, entities::*, handlers::*, AccountId, Address, Balance, BlockNumber, - FullIdentificationOfImpl, GetCurrentEpochIndex, Hash, Header, IdtyData, IdtyIndex, Index, - Signature, + FullIdentificationOfImpl, GetCurrentEpochIndex, Hash, Header, IdtyIndex, Index, Signature, }; pub use pallet_balances::Call as BalancesCall; pub use pallet_identity::{IdtyStatus, IdtyValue}; diff --git a/runtime/gdev/src/lib.rs b/runtime/gdev/src/lib.rs index f350f9f2b0868164b940b5f48ee8268778bee9c3..99f886d165d862b0a652f53d2399bb63a71b7816 100644 --- a/runtime/gdev/src/lib.rs +++ b/runtime/gdev/src/lib.rs @@ -31,8 +31,7 @@ pub mod parameters; pub use self::parameters::*; pub use common_runtime::{ constants::*, entities::*, handlers::*, AccountId, Address, Balance, BlockNumber, - FullIdentificationOfImpl, GetCurrentEpochIndex, Hash, Header, IdtyData, IdtyIndex, Index, - Signature, + FullIdentificationOfImpl, GetCurrentEpochIndex, Hash, Header, IdtyIndex, Index, Signature, }; pub use pallet_balances::Call as BalancesCall; pub use pallet_duniter_test_parameters::Parameters as GenesisParameters; diff --git a/runtime/gdev/tests/common/mod.rs b/runtime/gdev/tests/common/mod.rs index 762e921fbbb85ad718d50f0e9c951d25d537a0c1..34db9d0df56b2a5209f3d9ae14cb277ce6a7f21d 100644 --- a/runtime/gdev/tests/common/mod.rs +++ b/runtime/gdev/tests/common/mod.rs @@ -210,7 +210,7 @@ impl ExtBuilder { owner_key: owner_key.clone(), removable_on: 0, status: IdtyStatus::Validated, - data: IdtyData::min(), + data: IdtyData::new(), }, }) .collect(), diff --git a/runtime/gtest/src/lib.rs b/runtime/gtest/src/lib.rs index 0c534e3a5b26c4fa7e8d62763621a1fec7afa3a7..2553564fc4a5f09871b0e586f62401e4db1fb564 100644 --- a/runtime/gtest/src/lib.rs +++ b/runtime/gtest/src/lib.rs @@ -27,8 +27,7 @@ pub mod parameters; pub use self::parameters::*; pub use common_runtime::{ constants::*, entities::*, handlers::*, AccountId, Address, Balance, BlockNumber, - FullIdentificationOfImpl, GetCurrentEpochIndex, Hash, Header, IdtyData, IdtyIndex, Index, - Signature, + FullIdentificationOfImpl, GetCurrentEpochIndex, Hash, Header, IdtyIndex, Index, Signature, }; pub use pallet_balances::Call as BalancesCall; pub use pallet_identity::{IdtyStatus, IdtyValue};