Skip to content
Snippets Groups Projects
Commit e6be6a09 authored by Éloïs's avatar Éloïs
Browse files

ref(runtime): more generic IdtyData (!84)

* ref(runtime): more generic IdtyData
parent f31e3083
Branches
Tags
1 merge request!84ref(runtime): more generic IdtyData
......@@ -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(),
......
......@@ -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")]
......
......@@ -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> {
......
......@@ -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,
......
......@@ -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
......
......@@ -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;
......
......@@ -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
}
......
......@@ -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))
......
......@@ -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};
......
......@@ -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;
......
......@@ -210,7 +210,7 @@ impl ExtBuilder {
owner_key: owner_key.clone(),
removable_on: 0,
status: IdtyStatus::Validated,
data: IdtyData::min(),
data: IdtyData::new(),
},
})
.collect(),
......
......@@ -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};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment