From 6d6a089f52c17805e2b0c9b7f54116f343d79f01 Mon Sep 17 00:00:00 2001
From: Hugo Trentesaux <hugo.trentesaux@lilo.org>
Date: Tue, 14 May 2024 15:58:15 +0200
Subject: [PATCH] Fix #179 (nodes/rust/duniter-v2s!262)

* clippy with feature runtime-benchmarks

* replace convert by idtyattr
---
 Cargo.lock                                     |  3 +++
 pallets/duniter-wot/src/mock.rs                | 12 +-----------
 pallets/membership/Cargo.toml                  |  1 +
 pallets/membership/src/lib.rs                  |  7 ++-----
 pallets/membership/src/mock.rs                 |  5 ++---
 pallets/smith-members/Cargo.toml               |  1 +
 pallets/smith-members/src/benchmarking.rs      | 13 ++++++-------
 pallets/smith-members/src/lib.rs               | 15 +++++++--------
 pallets/smith-members/src/mock.rs              |  3 +--
 pallets/universal-dividend/Cargo.toml          |  1 +
 pallets/universal-dividend/src/benchmarking.rs |  5 ++---
 pallets/universal-dividend/src/lib.rs          | 11 ++++++++---
 pallets/universal-dividend/src/mock.rs         |  7 ++-----
 primitives/duniter/src/lib.rs                  |  1 +
 runtime/common/src/pallets_config.rs           |  9 +++------
 15 files changed, 41 insertions(+), 53 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index ca9a14877..45df9912b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -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",
diff --git a/pallets/duniter-wot/src/mock.rs b/pallets/duniter-wot/src/mock.rs
index d9ebcb180..f73248cc6 100644
--- a/pallets/duniter-wot/src/mock.rs
+++ b/pallets/duniter-wot/src/mock.rs
@@ -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;
diff --git a/pallets/membership/Cargo.toml b/pallets/membership/Cargo.toml
index e33045ca9..b43852aa0 100644
--- a/pallets/membership/Cargo.toml
+++ b/pallets/membership/Cargo.toml
@@ -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 }
diff --git a/pallets/membership/src/lib.rs b/pallets/membership/src/lib.rs
index 92a82428c..4119fcb9c 100644
--- a/pallets/membership/src/lib.rs
+++ b/pallets/membership/src/lib.rs
@@ -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]
diff --git a/pallets/membership/src/mock.rs b/pallets/membership/src/mock.rs
index 2d27a0d79..6269ed615 100644
--- a/pallets/membership/src/mock.rs
+++ b/pallets/membership/src/mock.rs
@@ -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 = ();
diff --git a/pallets/smith-members/Cargo.toml b/pallets/smith-members/Cargo.toml
index 651a62a8a..d18fc3cfb 100644
--- a/pallets/smith-members/Cargo.toml
+++ b/pallets/smith-members/Cargo.toml
@@ -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 }
diff --git a/pallets/smith-members/src/benchmarking.rs b/pallets/smith-members/src/benchmarking.rs
index 3539c9ed8..0381e177c 100644
--- a/pallets/smith-members/src/benchmarking.rs
+++ b/pallets/smith-members/src/benchmarking.rs
@@ -20,7 +20,6 @@ use super::*;
 
 use frame_benchmarking::v2::*;
 use frame_system::RawOrigin;
-use sp_runtime::traits::Convert;
 
 use crate::Pallet;
 
@@ -38,7 +37,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 +50,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 +74,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);
diff --git a/pallets/smith-members/src/lib.rs b/pallets/smith-members/src/lib.rs
index 9b33a6b56..61edcd6f6 100644
--- a/pallets/smith-members/src/lib.rs
+++ b/pallets/smith-members/src/lib.rs
@@ -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())
diff --git a/pallets/smith-members/src/mock.rs b/pallets/smith-members/src/mock.rs
index 3669161b5..804b7d49f 100644
--- a/pallets/smith-members/src/mock.rs
+++ b/pallets/smith-members/src/mock.rs
@@ -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 = ();
diff --git a/pallets/universal-dividend/Cargo.toml b/pallets/universal-dividend/Cargo.toml
index 5fdfc69f5..a859b2f97 100644
--- a/pallets/universal-dividend/Cargo.toml
+++ b/pallets/universal-dividend/Cargo.toml
@@ -49,6 +49,7 @@ default-features = false
 targets = ["x86_64-unknown-linux-gnu"]
 
 [dependencies]
+duniter-primitives = { workspace = true }
 codec = { workspace = true, features = ["derive", "max-encoded-len"] }
 frame-benchmarking = { workspace = true, optional = true }
 frame-support = { workspace = true }
diff --git a/pallets/universal-dividend/src/benchmarking.rs b/pallets/universal-dividend/src/benchmarking.rs
index 8614441b3..58c4302d8 100644
--- a/pallets/universal-dividend/src/benchmarking.rs
+++ b/pallets/universal-dividend/src/benchmarking.rs
@@ -27,7 +27,6 @@ use frame_support::traits::StoredMap;
 use frame_system::RawOrigin;
 use pallet_balances::Pallet as Balances;
 use sp_runtime::traits::Bounded;
-use sp_runtime::traits::Convert;
 
 use crate::Pallet;
 
@@ -50,7 +49,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 +134,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,
diff --git a/pallets/universal-dividend/src/lib.rs b/pallets/universal-dividend/src/lib.rs
index 9eb12eb1b..6c6f60da2 100644
--- a/pallets/universal-dividend/src/lib.rs
+++ b/pallets/universal-dividend/src/lib.rs
@@ -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 //
diff --git a/pallets/universal-dividend/src/mock.rs b/pallets/universal-dividend/src/mock.rs
index dbe776de2..b1397ffe5 100644
--- a/pallets/universal-dividend/src/mock.rs
+++ b/pallets/universal-dividend/src/mock.rs
@@ -27,9 +27,6 @@ use sp_runtime::{
     BuildStorage,
 };
 
-#[cfg(feature = "runtime-benchmarks")]
-use sp_runtime::traits::ConvertInto;
-
 pub const BLOCK_TIME: u64 = 6_000;
 
 type Balance = u64;
@@ -142,9 +139,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;
diff --git a/primitives/duniter/src/lib.rs b/primitives/duniter/src/lib.rs
index 44c1257da..1d71c0a59 100644
--- a/primitives/duniter/src/lib.rs
+++ b/primitives/duniter/src/lib.rs
@@ -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>;
diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs
index 09156ab08..6cbe9d6e9 100644
--- a/runtime/common/src/pallets_config.rs
+++ b/runtime/common/src/pallets_config.rs
@@ -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;
-- 
GitLab