diff --git a/pallets/distance/src/lib.rs b/pallets/distance/src/lib.rs index d858edb07ac5e88caa77310ab6f76c30b95686a3..48ad748feb1e751196bde559c2d4e193abbe88ed 100644 --- a/pallets/distance/src/lib.rs +++ b/pallets/distance/src/lib.rs @@ -60,7 +60,7 @@ pub mod pallet { pub trait Config<I: 'static = ()>: frame_system::Config + pallet_authorship::Config - // + pallet_certification::Config<I, IdtyIndex = IdtyIndex> + + pallet_certification::Config<I, IdtyIndex = IdtyIndex> + pallet_identity::Config<IdtyIndex = IdtyIndex> + pallet_session::Config { @@ -91,7 +91,7 @@ pub mod pallet { _, EvaluationPool< <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, <T as Config<I>>::MaxEvaluatorsPerSession, >, @@ -103,7 +103,7 @@ pub mod pallet { _, EvaluationPool< <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, <T as Config<I>>::MaxEvaluatorsPerSession, >, @@ -115,7 +115,7 @@ pub mod pallet { _, EvaluationPool< <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, <T as Config<I>>::MaxEvaluatorsPerSession, >, @@ -233,38 +233,37 @@ pub mod pallet { !DidUpdate::<T, I>::exists(), Error::<T, I>::ManyEvaluationsInBlock, ); - Ok(()) - - // Pallet::<T, I>::mutate_current_pool( - // pallet_session::CurrentIndex::<T>::get().wrapping_add(1), - // |result_pool| { - // ensure!( - // computation_result.distances.len() == result_pool.0.len(), - // Error::<T, I>::WrongResultLength - // ); - // - // let author = - // pallet_authorship::Pallet::<T>::author().ok_or(Error::<T, I>::NoAuthor)?; - // - // if result_pool - // .1 - // .try_insert(author) - // .map_err(|_| Error::<T, I>::TooManyEvaluators)? - // { - // for (distance_value, (_identity, median_acc)) in computation_result - // .distances - // .into_iter() - // .zip(result_pool.0.iter_mut()) - // { - // median_acc.push(distance_value); - // } - // - // Ok(()) - // } else { - // Err(Error::<T, I>::ManyEvaluationsByAuthor.into()) - // } - // }, - // ) + + Pallet::<T, I>::mutate_current_pool( + pallet_session::CurrentIndex::<T>::get().wrapping_add(1), + |result_pool| { + ensure!( + computation_result.distances.len() == result_pool.0.len(), + Error::<T, I>::WrongResultLength + ); + + let author = + pallet_authorship::Pallet::<T>::author().ok_or(Error::<T, I>::NoAuthor)?; + + if result_pool + .1 + .try_insert(author) + .map_err(|_| Error::<T, I>::TooManyEvaluators)? + { + for (distance_value, (_identity, median_acc)) in computation_result + .distances + .into_iter() + .zip(result_pool.0.iter_mut()) + { + median_acc.push(distance_value); + } + + Ok(()) + } else { + Err(Error::<T, I>::ManyEvaluationsByAuthor.into()) + } + }, + ) } } @@ -273,29 +272,27 @@ pub mod pallet { impl<T: Config<I>, I: 'static> Pallet<T, I> { pub fn do_evaluate_distance( who: T::AccountId, - idty_index: u64, - // idty_index: <T as pallet_certification::Config<I>>::IdtyIndex, + idty_index: <T as pallet_certification::Config<I>>::IdtyIndex, ) -> Result<(), DispatchError> { - // Pallet::<T, I>::mutate_current_pool( - // pallet_session::CurrentIndex::<T>::get(), - // |current_pool| { - // ensure!( - // current_pool.0.len() - // < (<T as Config<I>>::MaxEvaluationsPerSession::get() as usize), - // Error::<T, I>::QueueFull - // ); - // - // T::Currency::reserve(&who, <T as Config<I>>::EvaluationPrice::get())?; - // - // current_pool - // .0 - // .try_push((idty_index, median::MedianAcc::new())) - // .map_err(|_| Error::<T, I>::QueueFull)?; - // - // Ok(()) - // }, - // ) - Ok(()) + Pallet::<T, I>::mutate_current_pool( + pallet_session::CurrentIndex::<T>::get(), + |current_pool| { + ensure!( + current_pool.0.len() + < (<T as Config<I>>::MaxEvaluationsPerSession::get() as usize), + Error::<T, I>::QueueFull + ); + + T::Currency::reserve(&who, <T as Config<I>>::EvaluationPrice::get())?; + + current_pool + .0 + .try_push((idty_index, median::MedianAcc::new())) + .map_err(|_| Error::<T, I>::QueueFull)?; + + Ok(()) + }, + ) } } @@ -303,27 +300,27 @@ pub mod pallet { impl<T: Config<I>, I: 'static> Pallet<T, I> { /// Mutate the evaluation pool containing the results to be applied on this session. - // fn mutate_current_pool< - // R, - // F: FnOnce( - // &mut EvaluationPool< - // <T as frame_system::Config>::AccountId, - // <T as pallet_certification::Config<I>>::IdtyIndex, - // <T as Config<I>>::MaxEvaluationsPerSession, - // <T as Config<I>>::MaxEvaluatorsPerSession, - // >, - // ) -> R, - // >( - // index: SessionIndex, - // f: F, - // ) -> R { - // match index % 3 { - // 0 => EvaluationPool2::<T, I>::mutate(f), - // 1 => EvaluationPool0::<T, I>::mutate(f), - // 2 => EvaluationPool1::<T, I>::mutate(f), - // _ => panic!("index % 3 < 3"), - // } - // } + fn mutate_current_pool< + R, + F: FnOnce( + &mut EvaluationPool< + <T as frame_system::Config>::AccountId, + <T as pallet_certification::Config<I>>::IdtyIndex, + <T as Config<I>>::MaxEvaluationsPerSession, + <T as Config<I>>::MaxEvaluatorsPerSession, + >, + ) -> R, + >( + index: SessionIndex, + f: F, + ) -> R { + match index % 3 { + 0 => EvaluationPool2::<T, I>::mutate(f), + 1 => EvaluationPool0::<T, I>::mutate(f), + 2 => EvaluationPool1::<T, I>::mutate(f), + _ => panic!("index % 3 < 3"), + } + } /// Take the evaluation pool containing the results to be applied on this session. #[allow(clippy::type_complexity)] @@ -331,7 +328,7 @@ pub mod pallet { index: SessionIndex, ) -> EvaluationPool< <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, <T as Config<I>>::MaxEvaluatorsPerSession, > { @@ -353,7 +350,7 @@ pub mod pallet { #[allow(clippy::type_complexity)] let current_pool: EvaluationPool< <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, <T as Config<I>>::MaxEvaluatorsPerSession, > = Pallet::<T, I>::take_current_pool(index); diff --git a/pallets/distance/src/mock.rs b/pallets/distance/src/mock.rs index 456d6b29c6a33b0977af993f5a7086142bbcd484..61ad83e667c907dd73701a56fffb89e73b50f639 100644 --- a/pallets/distance/src/mock.rs +++ b/pallets/distance/src/mock.rs @@ -56,7 +56,7 @@ frame_support::construct_runtime!( Authorship: pallet_authorship::{Pallet, Call, Storage}, Distance: pallet_distance::{Pallet, Call, Config<T>, Storage, Event<T>}, Identity: pallet_identity::{Pallet, Call, Config<T>, Storage, Event<T>}, - // Cert: pallet_certification::{Pallet, Call, Config<T>, Storage, Event<T>}, + Cert: pallet_certification::{Pallet, Call, Config<T>, Storage, Event<T>}, AuthorityMembers: pallet_authority_members::{Pallet, Call, Storage, Config<T>, Event<T>}, } ); @@ -182,26 +182,26 @@ impl pallet_identity::Config for Test { type WeightInfo = (); } -// parameter_types! { -// pub const MaxByIssuer: u32 = 4; -// pub const MinReceivedCertToBeAbleToIssueCert: u32 = 2; -// pub const CertPeriod: u64 = 2; -// pub const ValidityPeriod: u64 = 10; -// } -// -// impl pallet_certification::Config for Test { -// type CertPeriod = CertPeriod; -// type IdtyIndex = IdtyIndex; -// type OwnerKeyOf = sp_runtime::traits::ConvertInto; -// type CheckCertAllowed = (); -// type MaxByIssuer = MaxByIssuer; -// type MinReceivedCertToBeAbleToIssueCert = MinReceivedCertToBeAbleToIssueCert; -// type OnNewcert = (); -// type OnRemovedCert = (); -// type RuntimeEvent = RuntimeEvent; -// type WeightInfo = (); -// type ValidityPeriod = ValidityPeriod; -// } +parameter_types! { + pub const MaxByIssuer: u32 = 4; + pub const MinReceivedCertToBeAbleToIssueCert: u32 = 2; + pub const CertPeriod: u64 = 2; + pub const ValidityPeriod: u64 = 10; +} + +impl pallet_certification::Config for Test { + type CertPeriod = CertPeriod; + type IdtyIndex = IdtyIndex; + type OwnerKeyOf = sp_runtime::traits::ConvertInto; + type CheckCertAllowed = (); + type MaxByIssuer = MaxByIssuer; + type MinReceivedCertToBeAbleToIssueCert = MinReceivedCertToBeAbleToIssueCert; + type OnNewcert = (); + type OnRemovedCert = (); + type RuntimeEvent = RuntimeEvent; + type WeightInfo = (); + type ValidityPeriod = ValidityPeriod; +} impl pallet_authorship::Config for Test { type FindAuthor = (); @@ -248,7 +248,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities { GenesisConfig { system: SystemConfig::default(), authority_members: pallet_authority_members::GenesisConfig::default(), - // cert: pallet_certification::GenesisConfig::default(), + cert: pallet_certification::GenesisConfig::default(), distance: pallet_distance::GenesisConfig::default(), identity: pallet_identity::GenesisConfig::default(), session: pallet_session::GenesisConfig::default(), diff --git a/pallets/distance/src/types.rs b/pallets/distance/src/types.rs index 74338d34983c75e1d5b4146b05d952ab7b1fa360..ab629ec482290350863cadfb8a85de3273a67b13 100644 --- a/pallets/distance/src/types.rs +++ b/pallets/distance/src/types.rs @@ -30,13 +30,8 @@ pub enum DistanceStatus { Valid, } -pub type EvaluationPool< - AccountId, - // IdtyIndex, - MaxEvaluationsPerSession, - MaxEvaluatorsPerSession, -> = ( - BoundedVec<(u64, MedianAcc<Perbill, MaxEvaluatorsPerSession>), MaxEvaluationsPerSession>, +pub type EvaluationPool<AccountId, IdtyIndex, MaxEvaluationsPerSession, MaxEvaluatorsPerSession> = ( + BoundedVec<(IdtyIndex, MedianAcc<Perbill, MaxEvaluatorsPerSession>), MaxEvaluationsPerSession>, BoundedBTreeSet<AccountId, MaxEvaluatorsPerSession>, );