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

Doc comments, make max size params const

parent f4d83ddc
No related branches found
No related tags found
1 merge request!105Distance Oracle
...@@ -78,14 +78,12 @@ where ...@@ -78,14 +78,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));
} }
......
...@@ -32,6 +32,11 @@ use sp_std::convert::TryInto; ...@@ -32,6 +32,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] #[frame_support::pallet]
pub mod pallet { pub mod pallet {
use super::*; use super::*;
...@@ -61,11 +66,6 @@ pub mod pallet { ...@@ -61,11 +66,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
...@@ -82,8 +82,6 @@ pub mod pallet { ...@@ -82,8 +82,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,
>; >;
...@@ -95,8 +93,6 @@ pub mod pallet { ...@@ -95,8 +93,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,
>; >;
...@@ -108,8 +104,6 @@ pub mod pallet { ...@@ -108,8 +104,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,
>; >;
...@@ -120,6 +114,10 @@ pub mod pallet { ...@@ -120,6 +114,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<
...@@ -137,7 +135,10 @@ pub mod pallet { ...@@ -137,7 +135,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,
>; >;
...@@ -237,7 +238,13 @@ pub mod pallet { ...@@ -237,7 +238,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::weight(1_000_000)] #[pallet::weight(1_000_000)]
pub fn force_set_distance_status( pub fn force_set_distance_status(
origin: OriginFor<T>, origin: OriginFor<T>,
...@@ -270,8 +277,6 @@ pub mod pallet { ...@@ -270,8 +277,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,
>( >(
...@@ -292,8 +297,6 @@ pub mod pallet { ...@@ -292,8 +297,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,
>( >(
...@@ -317,8 +320,6 @@ pub mod pallet { ...@@ -317,8 +320,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(),
...@@ -336,15 +337,14 @@ pub mod pallet { ...@@ -336,15 +337,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)?;
...@@ -371,19 +371,19 @@ pub mod pallet { ...@@ -371,19 +371,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);
} }
...@@ -410,10 +410,8 @@ pub mod pallet { ...@@ -410,10 +410,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,13 +14,14 @@ ...@@ -14,13 +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 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.
...@@ -29,15 +30,31 @@ pub enum DistanceStatus { ...@@ -29,15 +30,31 @@ pub enum DistanceStatus {
Valid, Valid,
} }
pub type EvaluationPool< /// Pool where distance evaluation requests and results are stored
AccountId, ///
IdtyIndex, /// Depending on the pool rotation, this may not be complete, and still be accepting
MaxEvaluationsPerSession, /// new evaluation requests (with empty median accumulators) or new evaluations (with evaluators
const MAX_EVALUATORS_PER_SESSION: u32, /// and new samples in the median accumulators).
> = ( #[derive(Encode, Decode, Clone, RuntimeDebug, TypeInfo)]
BoundedVec< 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>), (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(),
}
}
}
...@@ -487,8 +487,6 @@ macro_rules! pallets_config { ...@@ -487,8 +487,6 @@ macro_rules! pallets_config {
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