Skip to content
Snippets Groups Projects
Commit cbdd5057 authored by Pascal Engélibert's avatar Pascal Engélibert :bicyclist: Committed by Hugo Trentesaux
Browse files

Doc comments, make max size params const

(cherry picked from commit e3e73001)
parent 9d95121e
No related branches found
No related tags found
No related merge requests found
...@@ -76,14 +76,12 @@ where ...@@ -76,14 +76,12 @@ where
), ),
)? )?
.map_or_else(Default::default, |raw| { .map_or_else(Default::default, |raw| {
pallet_distance::EvaluationPool::<AccountId32, IdtyIndex, ConstU32<100>, 100>::decode( pallet_distance::EvaluationPool::<AccountId32, IdtyIndex>::decode(&mut &raw.0[..])
&mut &raw.0[..],
)
.expect("cannot decode EvaluationPool") .expect("cannot decode EvaluationPool")
}); });
// Have we already published a result for this session? // 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)); return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None));
} }
......
...@@ -34,6 +34,11 @@ use sp_std::convert::TryInto; ...@@ -34,6 +34,11 @@ use sp_std::convert::TryInto;
type IdtyIndex = u32; 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(dev_mode)] // dev mode while waiting for benchmarks #[frame_support::pallet(dev_mode)] // dev mode while waiting for benchmarks
pub mod pallet { pub mod pallet {
use super::*; use super::*;
...@@ -63,11 +68,6 @@ pub mod pallet { ...@@ -63,11 +68,6 @@ pub mod pallet {
type EvaluationPrice: Get< type EvaluationPrice: Get<
<Self::Currency as frame_support::traits::Currency<Self::AccountId>>::Balance, <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 /// Minimum ratio of accessible referees
type MinAccessibleReferees: Get<Perbill>; type MinAccessibleReferees: Get<Perbill>;
/// Number of session to keep a positive evaluation result /// Number of session to keep a positive evaluation result
...@@ -86,8 +86,6 @@ pub mod pallet { ...@@ -86,8 +86,6 @@ pub mod pallet {
EvaluationPool< EvaluationPool<
<T as frame_system::Config>::AccountId, <T as frame_system::Config>::AccountId,
<T as pallet_certification::Config<I>>::IdtyIndex, <T as pallet_certification::Config<I>>::IdtyIndex,
<T as Config<I>>::MaxEvaluationsPerSession,
100,
>, >,
ValueQuery, ValueQuery,
>; >;
...@@ -99,8 +97,6 @@ pub mod pallet { ...@@ -99,8 +97,6 @@ pub mod pallet {
EvaluationPool< EvaluationPool<
<T as frame_system::Config>::AccountId, <T as frame_system::Config>::AccountId,
<T as pallet_certification::Config<I>>::IdtyIndex, <T as pallet_certification::Config<I>>::IdtyIndex,
<T as Config<I>>::MaxEvaluationsPerSession,
100,
>, >,
ValueQuery, ValueQuery,
>; >;
...@@ -112,8 +108,6 @@ pub mod pallet { ...@@ -112,8 +108,6 @@ pub mod pallet {
EvaluationPool< EvaluationPool<
<T as frame_system::Config>::AccountId, <T as frame_system::Config>::AccountId,
<T as pallet_certification::Config<I>>::IdtyIndex, <T as pallet_certification::Config<I>>::IdtyIndex,
<T as Config<I>>::MaxEvaluationsPerSession,
100,
>, >,
ValueQuery, ValueQuery,
>; >;
...@@ -124,6 +118,10 @@ pub mod pallet { ...@@ -124,6 +118,10 @@ pub mod pallet {
StorageValue<_, <T as frame_system::Config>::Hash, ValueQuery>; StorageValue<_, <T as frame_system::Config>::Hash, ValueQuery>;
/// Distance evaluation status by identity /// 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::storage]
#[pallet::getter(fn identity_distance_status)] #[pallet::getter(fn identity_distance_status)]
pub type IdentityDistanceStatus<T: Config<I>, I: 'static = ()> = StorageMap< pub type IdentityDistanceStatus<T: Config<I>, I: 'static = ()> = StorageMap<
...@@ -141,7 +139,10 @@ pub mod pallet { ...@@ -141,7 +139,10 @@ pub mod pallet {
_, _,
Twox64Concat, Twox64Concat,
u32, 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, ValueQuery,
>; >;
...@@ -247,7 +248,13 @@ pub mod pallet { ...@@ -247,7 +248,13 @@ pub mod pallet {
Pallet::<T, I>::do_update_evaluation(evaluator, computation_result) 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::call_index(3)] #[pallet::call_index(3)]
#[pallet::weight(0)] #[pallet::weight(0)]
// #[pallet::weight(T::WeightInfo::force_set_distance_status())] // #[pallet::weight(T::WeightInfo::force_set_distance_status())]
...@@ -282,8 +289,6 @@ pub mod pallet { ...@@ -282,8 +289,6 @@ pub mod pallet {
&mut EvaluationPool< &mut EvaluationPool<
<T as frame_system::Config>::AccountId, <T as frame_system::Config>::AccountId,
<T as pallet_certification::Config<I>>::IdtyIndex, <T as pallet_certification::Config<I>>::IdtyIndex,
<T as Config<I>>::MaxEvaluationsPerSession,
100, //<T as Config<I>>::MaxEvaluatorsPerSession,
>, >,
) -> R, ) -> R,
>( >(
...@@ -304,8 +309,6 @@ pub mod pallet { ...@@ -304,8 +309,6 @@ pub mod pallet {
&mut EvaluationPool< &mut EvaluationPool<
<T as frame_system::Config>::AccountId, <T as frame_system::Config>::AccountId,
<T as pallet_certification::Config<I>>::IdtyIndex, <T as pallet_certification::Config<I>>::IdtyIndex,
<T as Config<I>>::MaxEvaluationsPerSession,
100, //<T as Config<I>>::MaxEvaluatorsPerSession,
>, >,
) -> R, ) -> R,
>( >(
...@@ -329,8 +332,6 @@ pub mod pallet { ...@@ -329,8 +332,6 @@ pub mod pallet {
) -> EvaluationPool< ) -> EvaluationPool<
<T as frame_system::Config>::AccountId, <T as frame_system::Config>::AccountId,
<T as pallet_certification::Config<I>>::IdtyIndex, <T as pallet_certification::Config<I>>::IdtyIndex,
<T as Config<I>>::MaxEvaluationsPerSession,
100, //<T as Config<I>>::MaxEvaluatorsPerSession,
> { > {
match index % 3 { match index % 3 {
0 => EvaluationPool2::<T, I>::take(), 0 => EvaluationPool2::<T, I>::take(),
...@@ -348,15 +349,14 @@ pub mod pallet { ...@@ -348,15 +349,14 @@ pub mod pallet {
pallet_session::CurrentIndex::<T>::get(), pallet_session::CurrentIndex::<T>::get(),
|current_pool| { |current_pool| {
ensure!( ensure!(
current_pool.0.len() current_pool.evaluations.len() < (MAX_EVALUATIONS_PER_SESSION as usize),
< (<T as Config<I>>::MaxEvaluationsPerSession::get() as usize),
Error::<T, I>::QueueFull Error::<T, I>::QueueFull
); );
T::Currency::reserve(&who, <T as Config<I>>::EvaluationPrice::get())?; T::Currency::reserve(&who, <T as Config<I>>::EvaluationPrice::get())?;
current_pool current_pool
.0 .evaluations
.try_push((idty_index, median::MedianAcc::new())) .try_push((idty_index, median::MedianAcc::new()))
.map_err(|_| Error::<T, I>::QueueFull)?; .map_err(|_| Error::<T, I>::QueueFull)?;
...@@ -383,19 +383,19 @@ pub mod pallet { ...@@ -383,19 +383,19 @@ pub mod pallet {
pallet_session::CurrentIndex::<T>::get(), pallet_session::CurrentIndex::<T>::get(),
|result_pool| { |result_pool| {
ensure!( ensure!(
computation_result.distances.len() == result_pool.0.len(), computation_result.distances.len() == result_pool.evaluations.len(),
Error::<T, I>::WrongResultLength Error::<T, I>::WrongResultLength
); );
if result_pool if result_pool
.1 .evaluators
.try_insert(evaluator) .try_insert(evaluator)
.map_err(|_| Error::<T, I>::TooManyEvaluators)? .map_err(|_| Error::<T, I>::TooManyEvaluators)?
{ {
for (distance_value, (_identity, median_acc)) in computation_result for (distance_value, (_identity, median_acc)) in computation_result
.distances .distances
.into_iter() .into_iter()
.zip(result_pool.0.iter_mut()) .zip(result_pool.evaluations.iter_mut())
{ {
median_acc.push(distance_value); median_acc.push(distance_value);
} }
...@@ -422,10 +422,8 @@ pub mod pallet { ...@@ -422,10 +422,8 @@ pub mod pallet {
let current_pool: EvaluationPool< let current_pool: EvaluationPool<
<T as frame_system::Config>::AccountId, <T as frame_system::Config>::AccountId,
<T as pallet_certification::Config<I>>::IdtyIndex, <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); > = 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() { if let Some(median_result) = median_acc.get_median() {
let median = match median_result { let median = match median_result {
MedianResult::One(m) => m, MedianResult::One(m) => m,
......
...@@ -14,14 +14,14 @@ ...@@ -14,14 +14,14 @@
// You should have received a copy of the GNU Affero General Public License // 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/>. // 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; pub use sp_distance::ComputationResult;
use codec::{Decode, Encode}; use codec::{Decode, Encode};
use frame_support::{pallet_prelude::*, BoundedBTreeSet}; use frame_support::{pallet_prelude::*, BoundedBTreeSet};
use sp_runtime::Perbill; use sp_runtime::Perbill;
/// Status of the distance computation of an identity in the wot /// Status of the distance evaluation of an identity
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)] #[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)]
pub enum DistanceStatus { pub enum DistanceStatus {
/// Identity is in evaluation. /// Identity is in evaluation.
...@@ -31,24 +31,30 @@ pub enum DistanceStatus { ...@@ -31,24 +31,30 @@ pub enum DistanceStatus {
} }
/// Pool where distance evaluation requests and results are stored /// Pool where distance evaluation requests and results are stored
pub type EvaluationPool< ///
AccountId, /// Depending on the pool rotation, this may not be complete, and still be accepting
IdtyIndex, /// new evaluation requests (with empty median accumulators) or new evaluations (with evaluators
MaxEvaluationsPerSession, /// and new samples in the median accumulators).
const MAX_EVALUATORS_PER_SESSION: u32, #[derive(Encode, Decode, Clone, RuntimeDebug, TypeInfo)]
> = ( pub struct EvaluationPool<AccountId: Ord, IdtyIndex> {
// .0 vector of published results /// List of identities with their evaluation result
BoundedVec< /// The result is the median of all the evaluations.
( pub evaluations: BoundedVec<
// index of the identity waiting for results (IdtyIndex, MedianAcc<Perbill, MAX_EVALUATORS_PER_SESSION>),
IdtyIndex, ConstU32<MAX_EVALUATIONS_PER_SESSION>,
// vector of results from evaluators
// stored in median accumulator to update median while adding results
MedianAcc<Perbill, MAX_EVALUATORS_PER_SESSION>,
),
MaxEvaluationsPerSession,
>, >,
// .1 set of evaluators who already publised a result in this pool /// Evaluators who have published a result
// prevents an evaluator from publishing a result multiple times /// Its length should be the same as the number of samples
BoundedBTreeSet<AccountId, ConstU32<MAX_EVALUATORS_PER_SESSION>>, /// 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(),
}
}
}
...@@ -488,8 +488,6 @@ parameter_types! { ...@@ -488,8 +488,6 @@ parameter_types! {
impl pallet_distance::Config<Instance1> for Runtime { impl pallet_distance::Config<Instance1> for Runtime {
type Currency = Balances; type Currency = Balances;
type EvaluationPrice = frame_support::traits::ConstU64<1000>; 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 MinAccessibleReferees = MinAccessibleReferees;
type ResultExpiration = frame_support::traits::ConstU32<720>; type ResultExpiration = frame_support::traits::ConstU32<720>;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment