From e3e7300168ecff56ce32036d5310ef15b34d3bcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20Eng=C3=A9libert?= <tuxmain@zettascript.org> Date: Mon, 11 Sep 2023 19:25:26 +0200 Subject: [PATCH] Doc comments, make max size params const --- client/distance/src/lib.rs | 8 ++--- pallets/distance/src/lib.rs | 54 ++++++++++++++-------------- pallets/distance/src/types.rs | 39 ++++++++++++++------ runtime/common/src/pallets_config.rs | 2 -- 4 files changed, 57 insertions(+), 46 deletions(-) diff --git a/client/distance/src/lib.rs b/client/distance/src/lib.rs index a89ef6bc1..ea3451010 100644 --- a/client/distance/src/lib.rs +++ b/client/distance/src/lib.rs @@ -78,14 +78,12 @@ where ), )? .map_or_else(Default::default, |raw| { - pallet_distance::EvaluationPool::<AccountId32, IdtyIndex, ConstU32<100>, 100>::decode( - &mut &raw.0[..], - ) - .expect("cannot decode EvaluationPool") + pallet_distance::EvaluationPool::<AccountId32, IdtyIndex>::decode(&mut &raw.0[..]) + .expect("cannot decode EvaluationPool") }); // Have we already published a result for this session? - if published_results.1.contains(&owner_key) { + if published_results.evaluators.contains(&owner_key) { return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None)); } diff --git a/pallets/distance/src/lib.rs b/pallets/distance/src/lib.rs index 9f668c474..c56e35d60 100644 --- a/pallets/distance/src/lib.rs +++ b/pallets/distance/src/lib.rs @@ -32,6 +32,11 @@ use sp_std::convert::TryInto; type IdtyIndex = u32; +/// Maximum number of identities to be evaluated in a session +pub const MAX_EVALUATIONS_PER_SESSION: u32 = 600; +/// Maximum number of evaluators in a session +pub const MAX_EVALUATORS_PER_SESSION: u32 = 100; + #[frame_support::pallet] pub mod pallet { use super::*; @@ -61,11 +66,6 @@ pub mod pallet { type EvaluationPrice: Get< <Self::Currency as frame_support::traits::Currency<Self::AccountId>>::Balance, >; - /// Maximum number of identities to be evaluated in a session - type MaxEvaluationsPerSession: Get<u32>; - /// Maximum number of evaluators in a session - #[pallet::constant] - type MaxEvaluatorsPerSession: Get<u32>; /// Minimum ratio of accessible referees type MinAccessibleReferees: Get<Perbill>; /// Number of session to keep a positive evaluation result @@ -82,8 +82,6 @@ pub mod pallet { EvaluationPool< <T as frame_system::Config>::AccountId, <T as pallet_certification::Config<I>>::IdtyIndex, - <T as Config<I>>::MaxEvaluationsPerSession, - 100, >, ValueQuery, >; @@ -95,8 +93,6 @@ pub mod pallet { EvaluationPool< <T as frame_system::Config>::AccountId, <T as pallet_certification::Config<I>>::IdtyIndex, - <T as Config<I>>::MaxEvaluationsPerSession, - 100, >, ValueQuery, >; @@ -108,8 +104,6 @@ pub mod pallet { EvaluationPool< <T as frame_system::Config>::AccountId, <T as pallet_certification::Config<I>>::IdtyIndex, - <T as Config<I>>::MaxEvaluationsPerSession, - 100, >, ValueQuery, >; @@ -120,6 +114,10 @@ pub mod pallet { StorageValue<_, <T as frame_system::Config>::Hash, ValueQuery>; /// Distance evaluation status by identity + /// + /// * `.0` is the account who requested an evaluation and reserved the price, + /// for whom the price will be unreserved or slashed when the evaluation completes. + /// * `.1` is the status of the evaluation. #[pallet::storage] #[pallet::getter(fn identity_distance_status)] pub type IdentityDistanceStatus<T: Config<I>, I: 'static = ()> = StorageMap< @@ -137,7 +135,10 @@ pub mod pallet { _, Twox64Concat, u32, - BoundedVec<<T as pallet_certification::Config<I>>::IdtyIndex, T::MaxEvaluationsPerSession>, + BoundedVec< + <T as pallet_certification::Config<I>>::IdtyIndex, + ConstU32<MAX_EVALUATIONS_PER_SESSION>, + >, ValueQuery, >; @@ -237,7 +238,13 @@ pub mod pallet { Pallet::<T, I>::do_update_evaluation(evaluator, computation_result) } - /// Push an evaluation result to the pool + /// Set the distance evaluation status of an identity + /// + /// Removes the status if `status` is `None`. + /// + /// * `status.0` is the account for whom the price will be unreserved or slashed + /// when the evaluation completes. + /// * `status.1` is the status of the evaluation. #[pallet::weight(1_000_000)] pub fn force_set_distance_status( origin: OriginFor<T>, @@ -270,8 +277,6 @@ pub mod pallet { &mut EvaluationPool< <T as frame_system::Config>::AccountId, <T as pallet_certification::Config<I>>::IdtyIndex, - <T as Config<I>>::MaxEvaluationsPerSession, - 100, //<T as Config<I>>::MaxEvaluatorsPerSession, >, ) -> R, >( @@ -292,8 +297,6 @@ pub mod pallet { &mut EvaluationPool< <T as frame_system::Config>::AccountId, <T as pallet_certification::Config<I>>::IdtyIndex, - <T as Config<I>>::MaxEvaluationsPerSession, - 100, //<T as Config<I>>::MaxEvaluatorsPerSession, >, ) -> R, >( @@ -317,8 +320,6 @@ pub mod pallet { ) -> EvaluationPool< <T as frame_system::Config>::AccountId, <T as pallet_certification::Config<I>>::IdtyIndex, - <T as Config<I>>::MaxEvaluationsPerSession, - 100, //<T as Config<I>>::MaxEvaluatorsPerSession, > { match index % 3 { 0 => EvaluationPool2::<T, I>::take(), @@ -336,15 +337,14 @@ pub mod pallet { pallet_session::CurrentIndex::<T>::get(), |current_pool| { ensure!( - current_pool.0.len() - < (<T as Config<I>>::MaxEvaluationsPerSession::get() as usize), + current_pool.evaluations.len() < (MAX_EVALUATIONS_PER_SESSION as usize), Error::<T, I>::QueueFull ); T::Currency::reserve(&who, <T as Config<I>>::EvaluationPrice::get())?; current_pool - .0 + .evaluations .try_push((idty_index, median::MedianAcc::new())) .map_err(|_| Error::<T, I>::QueueFull)?; @@ -371,19 +371,19 @@ pub mod pallet { pallet_session::CurrentIndex::<T>::get(), |result_pool| { ensure!( - computation_result.distances.len() == result_pool.0.len(), + computation_result.distances.len() == result_pool.evaluations.len(), Error::<T, I>::WrongResultLength ); if result_pool - .1 + .evaluators .try_insert(evaluator) .map_err(|_| Error::<T, I>::TooManyEvaluators)? { for (distance_value, (_identity, median_acc)) in computation_result .distances .into_iter() - .zip(result_pool.0.iter_mut()) + .zip(result_pool.evaluations.iter_mut()) { median_acc.push(distance_value); } @@ -410,10 +410,8 @@ pub mod pallet { let current_pool: EvaluationPool< <T as frame_system::Config>::AccountId, <T as pallet_certification::Config<I>>::IdtyIndex, - <T as Config<I>>::MaxEvaluationsPerSession, - 100, //<T as Config<I>>::MaxEvaluatorsPerSession, > = Pallet::<T, I>::take_current_pool(index); - for (idty, median_acc) in current_pool.0.into_iter() { + for (idty, median_acc) in current_pool.evaluations.into_iter() { if let Some(median_result) = median_acc.get_median() { let median = match median_result { MedianResult::One(m) => m, diff --git a/pallets/distance/src/types.rs b/pallets/distance/src/types.rs index f5f6589b3..8a3613344 100644 --- a/pallets/distance/src/types.rs +++ b/pallets/distance/src/types.rs @@ -14,13 +14,14 @@ // You should have received a copy of the GNU Affero General Public License // along with Duniter-v2S. If not, see <https://www.gnu.org/licenses/>. -pub use crate::median::*; +pub use crate::{median::*, MAX_EVALUATIONS_PER_SESSION, MAX_EVALUATORS_PER_SESSION}; pub use sp_distance::ComputationResult; use codec::{Decode, Encode}; use frame_support::{pallet_prelude::*, BoundedBTreeSet}; use sp_runtime::Perbill; +/// Status of the distance evaluation of an identity #[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)] pub enum DistanceStatus { /// Identity is in evaluation. @@ -29,15 +30,31 @@ pub enum DistanceStatus { Valid, } -pub type EvaluationPool< - AccountId, - IdtyIndex, - MaxEvaluationsPerSession, - const MAX_EVALUATORS_PER_SESSION: u32, -> = ( - BoundedVec< +/// Pool where distance evaluation requests and results are stored +/// +/// Depending on the pool rotation, this may not be complete, and still be accepting +/// new evaluation requests (with empty median accumulators) or new evaluations (with evaluators +/// and new samples in the median accumulators). +#[derive(Encode, Decode, Clone, RuntimeDebug, TypeInfo)] +pub struct EvaluationPool<AccountId: Ord, IdtyIndex> { + /// List of identities with their evaluation result + /// The result is the median of all the evaluations. + pub evaluations: BoundedVec< (IdtyIndex, MedianAcc<Perbill, MAX_EVALUATORS_PER_SESSION>), - MaxEvaluationsPerSession, + ConstU32<MAX_EVALUATIONS_PER_SESSION>, >, - BoundedBTreeSet<AccountId, ConstU32<MAX_EVALUATORS_PER_SESSION>>, -); + /// Evaluators who have published a result + /// Its length should be the same as the number of samples + /// in each evaluation result `MedianAcc`. + /// An evaluator is not allowed to publish twice in a single session. + pub evaluators: BoundedBTreeSet<AccountId, ConstU32<MAX_EVALUATORS_PER_SESSION>>, +} + +impl<AccountId: Ord, IdtyIndex> Default for EvaluationPool<AccountId, IdtyIndex> { + fn default() -> Self { + Self { + evaluations: BoundedVec::default(), + evaluators: BoundedBTreeSet::new(), + } + } +} diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs index e8c4157f0..6a8cede3b 100644 --- a/runtime/common/src/pallets_config.rs +++ b/runtime/common/src/pallets_config.rs @@ -487,8 +487,6 @@ macro_rules! pallets_config { impl pallet_distance::Config<Instance1> for Runtime { type Currency = Balances; type EvaluationPrice = frame_support::traits::ConstU64<1000>; - type MaxEvaluationsPerSession = frame_support::traits::ConstU32<1000>; - type MaxEvaluatorsPerSession = frame_support::traits::ConstU32<100>; type MinAccessibleReferees = MinAccessibleReferees; type ResultExpiration = frame_support::traits::ConstU32<720>; } -- GitLab