Skip to content
Snippets Groups Projects
Commit 3db8ead4 authored by Hugo Trentesaux's avatar Hugo Trentesaux Committed by Hugo Trentesaux
Browse files

replace convert by idtyattr

parent 52a27c65
No related branches found
No related tags found
1 merge request!262Fix #179
Showing with 41 additions and 48 deletions
......@@ -6532,6 +6532,7 @@ dependencies = [
name = "pallet-membership"
version = "1.0.0"
dependencies = [
"duniter-primitives",
"frame-benchmarking",
"frame-support",
"frame-system",
......@@ -6716,6 +6717,7 @@ dependencies = [
name = "pallet-smith-members"
version = "1.0.0"
dependencies = [
"duniter-primitives",
"frame-benchmarking",
"frame-support",
"frame-system",
......@@ -6834,6 +6836,7 @@ dependencies = [
name = "pallet-universal-dividend"
version = "1.0.0"
dependencies = [
"duniter-primitives",
"frame-benchmarking",
"frame-support",
"frame-system",
......
......@@ -30,15 +30,6 @@ use std::collections::BTreeMap;
type AccountId = u64;
type Block = frame_system::mocking::MockBlock<Test>;
pub struct IdentityIndexOf<T: pallet_identity::Config>(PhantomData<T>);
impl<T: pallet_identity::Config> sp_runtime::traits::Convert<T::AccountId, Option<T::IdtyIndex>>
for IdentityIndexOf<T>
{
fn convert(account_id: T::AccountId) -> Option<T::IdtyIndex> {
pallet_identity::Pallet::<T>::identity_index_of(account_id)
}
}
// Configure a mock runtime to test the pallet.
frame_support::construct_runtime!(
pub enum Test {
......@@ -145,12 +136,11 @@ parameter_types! {
}
impl pallet_membership::Config for Test {
type AccountIdOf = ();
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkSetupHandler = ();
type CheckMembershipOpAllowed = DuniterWot;
type IdtyAttr = Identity;
type IdtyId = IdtyIndex;
type IdtyIdOf = IdentityIndexOf<Self>;
type MembershipPeriod = MembershipPeriod;
type MembershipRenewalPeriod = MembershipRenewalPeriod;
type OnNewMembership = DuniterWot;
......
......@@ -40,6 +40,7 @@ try-runtime = [
targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
duniter-primitives = { workspace = true }
codec = { workspace = true, features = ["derive"] }
frame-benchmarking = { workspace = true, optional = true }
frame-support = { workspace = true }
......
......@@ -69,7 +69,6 @@ pub mod pallet {
use super::*;
use frame_support::traits::StorageVersion;
use frame_system::pallet_prelude::*;
use sp_runtime::traits::Convert;
/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
......@@ -87,10 +86,8 @@ pub mod pallet {
type CheckMembershipOpAllowed: CheckMembershipOpAllowed<Self::IdtyId>;
/// Something that identifies an identity
type IdtyId: Copy + MaybeSerializeDeserialize + Parameter + Ord;
/// Something that gives the IdtyId of an AccountId
type IdtyIdOf: Convert<Self::AccountId, Option<Self::IdtyId>>;
/// Something that gives the AccountId of an IdtyId
type AccountIdOf: Convert<Self::IdtyId, Option<Self::AccountId>>;
/// Something that gives the IdtyId of an AccountId and reverse
type IdtyAttr: duniter_primitives::Idty<Self::IdtyId, Self::AccountId>;
/// Maximum life span of a single membership (in number of blocks)
// (this could be renamed "validity" or "duration")
#[pallet::constant]
......
......@@ -22,7 +22,7 @@ use frame_support::{
use frame_system as system;
use sp_core::H256;
use sp_runtime::{
traits::{BlakeTwo256, ConvertInto, IdentityLookup},
traits::{BlakeTwo256, IdentityLookup},
BuildStorage,
};
......@@ -82,12 +82,11 @@ parameter_types! {
}
impl pallet_membership::Config for Test {
type AccountIdOf = ConvertInto;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkSetupHandler = ();
type CheckMembershipOpAllowed = ();
type IdtyAttr = ();
type IdtyId = IdtyId;
type IdtyIdOf = ConvertInto;
type MembershipPeriod = MembershipPeriod;
type MembershipRenewalPeriod = MembershipRenewalPeriod;
type OnNewMembership = ();
......
......@@ -13,6 +13,7 @@ version.workspace = true
targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
duniter-primitives = { workspace = true }
codec = { workspace = true, features = ["derive"] }
frame-benchmarking = { workspace = true, optional = true }
frame-support = { workspace = true }
......
......@@ -38,7 +38,7 @@ mod benchmarks {
#[benchmark]
fn invite_smith() {
let issuer: T::IdtyIndex = 1.into();
let caller: T::AccountId = T::OwnerKeyOf::convert(issuer).unwrap();
let caller: T::AccountId = T::IdtyAttr::owner_key(issuer).unwrap();
Pallet::<T>::on_smith_goes_online(1.into());
let receiver: T::IdtyIndex = 4.into();
......@@ -51,14 +51,14 @@ mod benchmarks {
#[benchmark]
fn accept_invitation() -> Result<(), BenchmarkError> {
let issuer: T::IdtyIndex = 1.into();
let caller: T::AccountId = T::OwnerKeyOf::convert(issuer).unwrap();
let caller: T::AccountId = T::IdtyAttr::owner_key(issuer).unwrap();
Pallet::<T>::on_smith_goes_online(1.into());
let caller_origin: <T as frame_system::Config>::RuntimeOrigin =
RawOrigin::Signed(caller.clone()).into();
let receiver: T::IdtyIndex = 4.into();
Pallet::<T>::invite_smith(caller_origin, receiver)?;
let issuer: T::IdtyIndex = 4.into();
let caller: T::AccountId = T::OwnerKeyOf::convert(issuer).unwrap();
let caller: T::AccountId = T::IdtyAttr::owner_key(issuer).unwrap();
#[extrinsic_call]
_(RawOrigin::Signed(caller));
......@@ -75,19 +75,19 @@ mod benchmarks {
#[benchmark]
fn certify_smith() -> Result<(), BenchmarkError> {
let issuer: T::IdtyIndex = 1.into();
let caller: T::AccountId = T::OwnerKeyOf::convert(issuer).unwrap();
let caller: T::AccountId = T::IdtyAttr::owner_key(issuer).unwrap();
Pallet::<T>::on_smith_goes_online(1.into());
let caller_origin: <T as frame_system::Config>::RuntimeOrigin =
RawOrigin::Signed(caller.clone()).into();
let receiver: T::IdtyIndex = 4.into();
Pallet::<T>::invite_smith(caller_origin, receiver)?;
let issuer: T::IdtyIndex = receiver;
let caller: T::AccountId = T::OwnerKeyOf::convert(issuer).unwrap();
let caller: T::AccountId = T::IdtyAttr::owner_key(issuer).unwrap();
let caller_origin: <T as frame_system::Config>::RuntimeOrigin =
RawOrigin::Signed(caller.clone()).into();
Pallet::<T>::accept_invitation(caller_origin)?;
let issuer: T::IdtyIndex = 1.into();
let caller: T::AccountId = T::OwnerKeyOf::convert(issuer).unwrap();
let caller: T::AccountId = T::IdtyAttr::owner_key(issuer).unwrap();
#[extrinsic_call]
_(RawOrigin::Signed(caller), receiver);
......
......@@ -31,6 +31,7 @@ pub mod weights;
mod benchmarking;
use codec::{Codec, Decode, Encode};
use duniter_primitives::Idty;
use frame_support::dispatch::DispatchResultWithPostInfo;
use frame_support::ensure;
use frame_support::pallet_prelude::Get;
......@@ -108,11 +109,9 @@ pub mod pallet {
+ MaxEncodedLen;
/// Identifier for an authority-member
type MemberId: Copy + Ord + MaybeSerializeDeserialize + Parameter;
/// Something that gives the IdtyId of an AccountId
type IdtyIdOf: Convert<Self::AccountId, Option<Self::IdtyIndex>>;
/// Something that give the owner key of an identity
type OwnerKeyOf: Convert<Self::IdtyIndex, Option<Self::AccountId>>;
/// Something that gives the IdtyId of an AccountId
/// Something that gives the IdtyId of an AccountId and reverse
type IdtyAttr: duniter_primitives::Idty<Self::IdtyIndex, Self::AccountId>;
// /// Something that give the owner key of an identity
type IdtyIdOfAuthorityId: Convert<Self::MemberId, Option<Self::IdtyIndex>>;
/// Maximum number of active certifications by issuer
#[pallet::constant]
......@@ -291,7 +290,7 @@ pub mod pallet {
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin.clone())?;
let issuer =
T::IdtyIdOf::convert(who.clone()).ok_or(Error::<T>::OriginMustHaveAnIdentity)?;
T::IdtyAttr::idty_index(who.clone()).ok_or(Error::<T>::OriginMustHaveAnIdentity)?;
Self::check_invite_smith(issuer, receiver)?;
Self::do_invite_smith(issuer, receiver);
Ok(().into())
......@@ -303,7 +302,7 @@ pub mod pallet {
pub fn accept_invitation(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin.clone())?;
let receiver =
T::IdtyIdOf::convert(who.clone()).ok_or(Error::<T>::OriginMustHaveAnIdentity)?;
T::IdtyAttr::idty_index(who.clone()).ok_or(Error::<T>::OriginMustHaveAnIdentity)?;
Self::check_accept_invitation(receiver)?;
Self::do_accept_invitation(receiver)?;
Ok(().into())
......@@ -318,7 +317,7 @@ pub mod pallet {
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
let issuer =
T::IdtyIdOf::convert(who.clone()).ok_or(Error::<T>::OriginMustHaveAnIdentity)?;
T::IdtyAttr::idty_index(who.clone()).ok_or(Error::<T>::OriginMustHaveAnIdentity)?;
Self::check_certify_smith(issuer, receiver)?;
Self::do_certify_smith(receiver, issuer);
Ok(().into())
......
......@@ -84,7 +84,7 @@ impl IsMember<u64> for EveryoneExceptIdZero {
}
impl pallet_smith_members::Config for Runtime {
type IdtyIdOf = ConvertInto;
type IdtyAttr = ();
type IdtyIdOfAuthorityId = ConvertInto;
type IdtyIndex = u64;
type IsWoTMember = EveryoneExceptIdZero;
......@@ -92,7 +92,6 @@ impl pallet_smith_members::Config for Runtime {
type MemberId = u64;
type MinCertForMembership = ConstU32<2>;
type OnSmithDelete = ();
type OwnerKeyOf = ConvertInto;
type RuntimeEvent = RuntimeEvent;
type SmithInactivityMaxDuration = ConstU32<5>;
type WeightInfo = ();
......
......@@ -64,4 +64,5 @@ sp-runtime = { workspace = true }
sp-std = { workspace = true }
[dev-dependencies]
duniter-primitives = { workspace = true }
sp-core = { workspace = true }
......@@ -50,7 +50,7 @@ mod benchmarks {
// Benchmark `transfer_ud` extrinsic with the worst possible conditions:
// * Transfer will kill the sender account.
// * Transfer will create the recipient account.
let caller: T::AccountId = T::AccountIdOf::convert(1).unwrap();
let caller: T::AccountId = T::IdtyAttr::owner_key(1).unwrap();
CurrentUdIndex::<T>::put(2054u16);
T::MembersStorage::insert(
&caller,
......@@ -135,7 +135,7 @@ mod benchmarks {
#[benchmark]
fn on_removed_member(i: Linear<1, { T::MaxPastReeval::get() }>) -> Result<(), BenchmarkError> {
let caller: T::AccountId = T::AccountIdOf::convert(1).unwrap();
let caller: T::AccountId = T::IdtyAttr::owner_key(1).unwrap();
CurrentUdIndex::<T>::put(2054u16);
T::MembersStorage::insert(
&caller,
......
......@@ -18,12 +18,16 @@
mod benchmarking;
mod compute_claim_uds;
mod types;
mod weights;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
mod types;
mod weights;
#[cfg(feature = "runtime-benchmarks")]
use duniter_primitives::Idty;
pub use pallet::*;
pub use types::*;
......@@ -86,8 +90,9 @@ pub mod pallet {
type UnitsPerUd: Get<BalanceOf<Self>>;
/// Pallet weights info
type WeightInfo: WeightInfo;
/// Something that gives the IdtyId of an AccountId and reverse
#[cfg(feature = "runtime-benchmarks")]
type AccountIdOf: Convert<u32, Option<Self::AccountId>>;
type IdtyAttr: duniter_primitives::Idty<u32, Self::AccountId>;
}
// STORAGE //
......
......@@ -142,9 +142,9 @@ impl frame_support::traits::StoredMap<u32, FirstEligibleUd> for TestMembersStora
}
impl pallet_universal_dividend::Config for Test {
#[cfg(feature = "runtime-benchmarks")]
type AccountIdOf = ConvertInto;
type Currency = pallet_balances::Pallet<Test>;
#[cfg(feature = "runtime-benchmarks")]
type IdtyAttr = ();
type MaxPastReeval = frame_support::traits::ConstU32<2>;
type MembersCount = MembersCount;
type MembersStorage = TestMembersStorage;
......
......@@ -29,6 +29,7 @@ pub fn validate_idty_name(idty_name: &[u8]) -> bool {
}
/// trait used to go from index to owner key and reverse
// replaces less explicit "Convert" implementations
pub trait Idty<IdtyIndex, AccountId> {
fn owner_key(index: IdtyIndex) -> Option<AccountId>;
fn idty_index(owner_key: AccountId) -> Option<IdtyIndex>;
......
......@@ -457,7 +457,7 @@ type RuntimeFreezeReason = ();
type UnitsPerUd = frame_support::traits::ConstU64<1_000>;
type WeightInfo = common_runtime::weights::pallet_universal_dividend::WeightInfo<Runtime>;
#[cfg(feature = "runtime-benchmarks")]
type AccountIdOf = common_runtime::providers::IdentityAccountIdProvider<Self>;
type IdtyAttr = Identity;
}
// WEB OF TRUST //
......@@ -501,8 +501,7 @@ type RuntimeFreezeReason = ();
impl pallet_membership::Config for Runtime {
type CheckMembershipOpAllowed = Wot;
type IdtyId = IdtyIndex;
type IdtyIdOf = common_runtime::providers::IdentityIndexOf<Self>;
type AccountIdOf = common_runtime::providers::IdentityAccountIdProvider<Self>;
type IdtyAttr = Identity;
type MembershipPeriod = MembershipPeriod;
type MembershipRenewalPeriod = MembershipRenewalPeriod;
type OnNewMembership = OnNewMembershipHandler<Runtime>;
......@@ -546,7 +545,7 @@ type RuntimeFreezeReason = ();
type RuntimeEvent = RuntimeEvent;
type IdtyIndex = IdtyIndex;
type IsWoTMember = common_runtime::providers::IsWoTMemberProvider<Runtime>;
type IdtyIdOf = common_runtime::providers::IdentityIndexOf<Self>;
type IdtyAttr = Identity;
type MinCertForMembership = SmithWotMinCertForMembership;
type MaxByIssuer = SmithMaxByIssuer;
type SmithInactivityMaxDuration = SmithInactivityMaxDuration;
......@@ -554,8 +553,6 @@ type RuntimeFreezeReason = ();
type IdtyIdOfAuthorityId = sp_runtime::traits::ConvertInto;
type MemberId = IdtyIndex;
type WeightInfo = common_runtime::weights::pallet_smith_members::WeightInfo<Runtime>;
// TODO: remove as it is only used for benchmarking
type OwnerKeyOf = Identity;
}
pub struct TechnicalCommitteeDefaultVote;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment