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

integration tests, expiration

parent 90a5352c
No related branches found
No related tags found
1 merge request!105Distance Oracle
...@@ -25,7 +25,6 @@ pub use traits::*; ...@@ -25,7 +25,6 @@ pub use traits::*;
pub use types::*; pub use types::*;
use frame_support::traits::StorageVersion; use frame_support::traits::StorageVersion;
//use median_accumulator::*;
use pallet_authority_members::SessionIndex; use pallet_authority_members::SessionIndex;
use sp_distance::{InherentError, INHERENT_IDENTIFIER}; use sp_distance::{InherentError, INHERENT_IDENTIFIER};
use sp_inherents::{InherentData, InherentIdentifier}; use sp_inherents::{InherentData, InherentIdentifier};
...@@ -69,7 +68,7 @@ pub mod pallet { ...@@ -69,7 +68,7 @@ pub mod pallet {
type MaxEvaluatorsPerSession: Get<u32>; type MaxEvaluatorsPerSession: Get<u32>;
/// Minimum ratio of accessible referees /// Minimum ratio of accessible referees
type MinAccessibleReferees: Get<Perbill>; type MinAccessibleReferees: Get<Perbill>;
// /// Handler for the evaluation price in case of negative result /// Number of session to keep a positive evaluation result
type ResultExpiration: Get<u32>; type ResultExpiration: Get<u32>;
} }
...@@ -83,7 +82,7 @@ pub mod pallet { ...@@ -83,7 +82,7 @@ pub mod pallet {
<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, <T as Config<I>>::MaxEvaluationsPerSession,
100, //<T as Config<I>>::MaxEvaluatorsPerSession, 100,
>, >,
ValueQuery, ValueQuery,
>; >;
...@@ -95,7 +94,7 @@ pub mod pallet { ...@@ -95,7 +94,7 @@ pub mod pallet {
<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, <T as Config<I>>::MaxEvaluationsPerSession,
100, //<T as Config<I>>::MaxEvaluatorsPerSession, 100,
>, >,
ValueQuery, ValueQuery,
>; >;
...@@ -107,7 +106,7 @@ pub mod pallet { ...@@ -107,7 +106,7 @@ pub mod pallet {
<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, <T as Config<I>>::MaxEvaluationsPerSession,
100, //<T as Config<I>>::MaxEvaluatorsPerSession, 100,
>, >,
ValueQuery, ValueQuery,
>; >;
...@@ -118,8 +117,8 @@ pub mod pallet { ...@@ -118,8 +117,8 @@ pub mod pallet {
StorageValue<_, <T as frame_system::Config>::Hash, ValueQuery>; StorageValue<_, <T as frame_system::Config>::Hash, ValueQuery>;
#[pallet::storage] #[pallet::storage]
#[pallet::getter(fn distance_ok_identities)] #[pallet::getter(fn identity_distance_status)]
pub type IdentitiesDistanceStatus<T: Config<I>, I: 'static = ()> = StorageMap< pub type IdentityDistanceStatus<T: Config<I>, I: 'static = ()> = StorageMap<
_, _,
Twox64Concat, Twox64Concat,
<T as pallet_certification::Config<I>>::IdtyIndex, <T as pallet_certification::Config<I>>::IdtyIndex,
...@@ -127,6 +126,17 @@ pub mod pallet { ...@@ -127,6 +126,17 @@ pub mod pallet {
OptionQuery, OptionQuery,
>; >;
/// Identities by distance status expiration session index
#[pallet::storage]
#[pallet::getter(fn distance_status_expire_on)]
pub type DistanceStatusExpireOn<T: Config<I>, I: 'static = ()> = StorageMap<
_,
Twox64Concat,
u32,
BoundedVec<<T as pallet_certification::Config<I>>::IdtyIndex, T::MaxEvaluationsPerSession>,
ValueQuery,
>;
/// Did evaluation get updated in this block? /// Did evaluation get updated in this block?
#[pallet::storage] #[pallet::storage]
pub(super) type DidUpdate<T: Config<I>, I: 'static = ()> = StorageValue<_, bool, ValueQuery>; pub(super) type DidUpdate<T: Config<I>, I: 'static = ()> = StorageValue<_, bool, ValueQuery>;
...@@ -184,7 +194,7 @@ pub mod pallet { ...@@ -184,7 +194,7 @@ pub mod pallet {
.ok_or(Error::<T, I>::NoIdentity)?; .ok_or(Error::<T, I>::NoIdentity)?;
ensure!( ensure!(
IdentitiesDistanceStatus::<T, I>::get(idty).is_none(), IdentityDistanceStatus::<T, I>::get(idty).is_none(),
Error::<T, I>::AlreadyInEvaluation Error::<T, I>::AlreadyInEvaluation
); );
...@@ -222,6 +232,26 @@ pub mod pallet { ...@@ -222,6 +232,26 @@ 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
#[pallet::weight(1_000_000)]
pub fn force_set_distance_status(
origin: OriginFor<T>,
identity: <T as pallet_certification::Config<I>>::IdtyIndex,
status: Option<DistanceStatus>,
) -> DispatchResult {
ensure_root(origin)?;
IdentityDistanceStatus::<T, I>::set(identity, status);
DistanceStatusExpireOn::<T, I>::mutate(
pallet_session::CurrentIndex::<T>::get() + T::ResultExpiration::get(),
move |identities| {
identities
.try_push(identity)
.map_err(|_| Error::<T, I>::ManyEvaluationsInBlock.into())
},
)
}
} }
// INTERNAL FUNCTIONS // // INTERNAL FUNCTIONS //
...@@ -288,7 +318,12 @@ pub mod pallet { ...@@ -288,7 +318,12 @@ pub mod pallet {
.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)?;
IdentitiesDistanceStatus::<T, I>::insert(idty_index, DistanceStatus::Pending); IdentityDistanceStatus::<T, I>::insert(idty_index, DistanceStatus::Pending);
DistanceStatusExpireOn::<T, I>::mutate(
pallet_session::CurrentIndex::<T>::get() + T::ResultExpiration::get(),
move |identities| identities.try_push(idty_index).ok(),
);
Ok(()) Ok(())
}, },
...@@ -333,6 +368,9 @@ pub mod pallet { ...@@ -333,6 +368,9 @@ pub mod pallet {
fn on_new_session(index: SessionIndex) -> Weight { fn on_new_session(index: SessionIndex) -> Weight {
EvaluationBlock::<T, I>::set(frame_system::Pallet::<T>::parent_hash()); EvaluationBlock::<T, I>::set(frame_system::Pallet::<T>::parent_hash());
// Make results expire
DistanceStatusExpireOn::<T, I>::remove(index);
// Apply the results from the current pool (which was previous session's result pool) // Apply the results from the current pool (which was previous session's result pool)
// We take the results so the pool is left empty for the new session. // We take the results so the pool is left empty for the new session.
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
...@@ -352,7 +390,7 @@ pub mod pallet { ...@@ -352,7 +390,7 @@ pub mod pallet {
MedianResult::Two(m1, m2) => m1 + (m2 - m1) / 2, // Avoid overflow (since max is 1) MedianResult::Two(m1, m2) => m1 + (m2 - m1) / 2, // Avoid overflow (since max is 1)
}; };
if median >= T::MinAccessibleReferees::get() { if median >= T::MinAccessibleReferees::get() {
IdentitiesDistanceStatus::<T, I>::mutate(idty, |status| { IdentityDistanceStatus::<T, I>::mutate(idty, |status| {
*status = Some(DistanceStatus::Valid) *status = Some(DistanceStatus::Valid)
}); });
T::Currency::unreserve( T::Currency::unreserve(
...@@ -360,14 +398,14 @@ pub mod pallet { ...@@ -360,14 +398,14 @@ pub mod pallet {
<T as Config<I>>::EvaluationPrice::get(), <T as Config<I>>::EvaluationPrice::get(),
); );
} else { } else {
IdentitiesDistanceStatus::<T, I>::remove(idty); IdentityDistanceStatus::<T, I>::remove(idty);
<T as Config<I>>::Currency::slash_reserved( <T as Config<I>>::Currency::slash_reserved(
&account_id, &account_id,
<T as Config<I>>::EvaluationPrice::get(), <T as Config<I>>::EvaluationPrice::get(),
); );
} }
} else { } else {
IdentitiesDistanceStatus::<T, I>::remove(idty); IdentityDistanceStatus::<T, I>::remove(idty);
T::Currency::unreserve(&account_id, <T as Config<I>>::EvaluationPrice::get()); T::Currency::unreserve(&account_id, <T as Config<I>>::EvaluationPrice::get());
} }
} }
......
...@@ -117,7 +117,7 @@ where ...@@ -117,7 +117,7 @@ where
I: 'static, I: 'static,
{ {
fn is_distance_ok(idty_id: &<T as pallet_certification::Config<I>>::IdtyIndex) -> bool { fn is_distance_ok(idty_id: &<T as pallet_certification::Config<I>>::IdtyIndex) -> bool {
pallet_distance::Pallet::<T, I>::distance_ok_identities(idty_id) pallet_distance::Pallet::<T, I>::identity_distance_status(idty_id)
== Some(pallet_distance::DistanceStatus::Valid) == Some(pallet_distance::DistanceStatus::Valid)
} }
} }
...@@ -22,6 +22,7 @@ use frame_support::instances::{Instance1, Instance2}; ...@@ -22,6 +22,7 @@ use frame_support::instances::{Instance1, Instance2};
use frame_support::traits::{GenesisBuild, OnFinalize, OnInitialize}; use frame_support::traits::{GenesisBuild, OnFinalize, OnInitialize};
use gdev_runtime::opaque::SessionKeys; use gdev_runtime::opaque::SessionKeys;
use gdev_runtime::*; use gdev_runtime::*;
use pallet_authority_members::OnNewSession;
use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId; use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId;
use sp_consensus_babe::{AuthorityId as BabeId, Slot}; use sp_consensus_babe::{AuthorityId as BabeId, Slot};
use sp_consensus_vrf::schnorrkel::{VRFOutput, VRFProof}; use sp_consensus_vrf::schnorrkel::{VRFOutput, VRFProof};
...@@ -319,8 +320,9 @@ fn mock_babe_initialize(n: u32) { ...@@ -319,8 +320,9 @@ fn mock_babe_initialize(n: u32) {
pub fn run_to_block(n: u32) { pub fn run_to_block(n: u32) {
while System::block_number() < n { while System::block_number() < n {
// Finalize the previous block // Finalize the previous block
//Babe::on_finalize(System::block_number()); Babe::on_finalize(System::block_number());
//Timestamp::on_finalize(System::block_number()); //Timestamp::on_finalize(System::block_number());
Distance::on_finalize(System::block_number());
TransactionPayment::on_finalize(System::block_number()); TransactionPayment::on_finalize(System::block_number());
Authorship::on_finalize(System::block_number()); Authorship::on_finalize(System::block_number());
Grandpa::on_finalize(System::block_number()); Grandpa::on_finalize(System::block_number());
...@@ -329,12 +331,22 @@ pub fn run_to_block(n: u32) { ...@@ -329,12 +331,22 @@ pub fn run_to_block(n: u32) {
System::reset_events(); System::reset_events();
System::set_block_number(System::block_number() + 1); System::set_block_number(System::block_number() + 1);
// Current slot is not incremented by BABE
pallet_babe::CurrentSlot::<Runtime>::put(pallet_babe::CurrentSlot::<Runtime>::get() + 1);
// Initialize the new block // Initialize the new block
Account::on_initialize(System::block_number()); Account::on_initialize(System::block_number());
Scheduler::on_initialize(System::block_number()); Scheduler::on_initialize(System::block_number());
mock_babe_initialize(System::block_number()); mock_babe_initialize(System::block_number());
Authorship::on_initialize(System::block_number());
Session::on_initialize(System::block_number()); Session::on_initialize(System::block_number());
Authorship::on_initialize(System::block_number());
/*if session_before != session_after {
Distance::on_new_session(session_after);
} else {
use pallet_session::ShouldEndSession;
assert!(!Babe::should_end_session(System::block_number()));
}*/
UniversalDividend::on_initialize(System::block_number()); UniversalDividend::on_initialize(System::block_number());
Wot::on_initialize(System::block_number()); Wot::on_initialize(System::block_number());
...@@ -346,6 +358,7 @@ pub fn run_to_block(n: u32) { ...@@ -346,6 +358,7 @@ pub fn run_to_block(n: u32) {
SmithCert::on_initialize(System::block_number()); SmithCert::on_initialize(System::block_number());
Timestamp::set_timestamp(System::block_number() as u64 * BLOCK_TIME); Timestamp::set_timestamp(System::block_number() as u64 * BLOCK_TIME);
Distance::on_initialize(System::block_number());
} }
} }
......
...@@ -113,7 +113,10 @@ fn test_remove_identity() { ...@@ -113,7 +113,10 @@ fn test_remove_identity() {
#[test] #[test]
fn test_validate_identity_when_claim() { fn test_validate_identity_when_claim() {
ExtBuilder::new(1, 3, 4) ExtBuilder::new(1, 3, 4)
.with_initial_balances(vec![(AccountKeyring::Ferdie.to_account_id(), 1000)]) .with_initial_balances(vec![
(AccountKeyring::Eve.to_account_id(), 2000),
(AccountKeyring::Ferdie.to_account_id(), 1000),
])
.build() .build()
.execute_with(|| { .execute_with(|| {
run_to_block(1); run_to_block(1);
...@@ -141,6 +144,20 @@ fn test_validate_identity_when_claim() { ...@@ -141,6 +144,20 @@ fn test_validate_identity_when_claim() {
5 5
)); ));
assert_ok!(Distance::evaluate_distance(
frame_system::RawOrigin::Signed(AccountKeyring::Eve.to_account_id()).into(),
));
run_to_block(51); // Pass 2 sessions
assert_ok!(Distance::force_update_evaluation(
frame_system::RawOrigin::Root.into(),
AccountKeyring::Alice.to_account_id(),
pallet_distance::ComputationResult {
distances: vec![Perbill::one()],
}
));
run_to_block(76); // Pass 1 session
// eve can claim her membership // eve can claim her membership
assert_ok!(Membership::claim_membership( assert_ok!(Membership::claim_membership(
frame_system::RawOrigin::Signed(AccountKeyring::Eve.to_account_id()).into(), frame_system::RawOrigin::Signed(AccountKeyring::Eve.to_account_id()).into(),
...@@ -181,9 +198,25 @@ fn test_membership_expiry() { ...@@ -181,9 +198,25 @@ fn test_membership_expiry() {
/// test membership renewal /// test membership renewal
#[test] #[test]
fn test_membership_renewal() { fn test_membership_renewal() {
ExtBuilder::new(1, 3, 4).build().execute_with(|| { ExtBuilder::new(1, 3, 4)
// renew at block 2 .with_initial_balances(vec![(AccountKeyring::Alice.to_account_id(), 2000)])
run_to_block(2); .build()
.execute_with(|| {
assert_ok!(Distance::evaluate_distance(
frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into(),
));
run_to_block(51); // Pass 2 sessions
assert_ok!(Distance::force_update_evaluation(
frame_system::RawOrigin::Root.into(),
AccountKeyring::Alice.to_account_id(),
pallet_distance::ComputationResult {
distances: vec![Perbill::one()],
}
));
run_to_block(76); // Pass 1 session
// renew at block 76
assert_ok!(Membership::renew_membership( assert_ok!(Membership::renew_membership(
frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into(), frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into(),
)); ));
...@@ -191,8 +224,8 @@ fn test_membership_renewal() { ...@@ -191,8 +224,8 @@ fn test_membership_renewal() {
pallet_membership::Event::MembershipRenewed(1), pallet_membership::Event::MembershipRenewed(1),
)); ));
// renew at block 3 // renew at block 77
run_to_block(3); run_to_block(77);
assert_ok!(Membership::renew_membership( assert_ok!(Membership::renew_membership(
frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into(), frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into(),
)); ));
...@@ -200,8 +233,8 @@ fn test_membership_renewal() { ...@@ -200,8 +233,8 @@ fn test_membership_renewal() {
pallet_membership::Event::MembershipRenewed(1), pallet_membership::Event::MembershipRenewed(1),
)); ));
// should expire at block 103 = 3+100 // should expire at block 177 = 77+100
run_to_block(103); run_to_block(177);
System::assert_has_event(RuntimeEvent::Membership( System::assert_has_event(RuntimeEvent::Membership(
pallet_membership::Event::MembershipExpired(1), pallet_membership::Event::MembershipExpired(1),
)); ));
...@@ -310,6 +343,11 @@ fn test_ud_claimed_membership_on_and_off() { ...@@ -310,6 +343,11 @@ fn test_ud_claimed_membership_on_and_off() {
)); ));
// alice claims back her membership // alice claims back her membership
assert_ok!(Distance::force_set_distance_status(
frame_system::RawOrigin::Root.into(),
1,
Some(pallet_distance::DistanceStatus::Valid)
));
assert_ok!(Membership::claim_membership( assert_ok!(Membership::claim_membership(
frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into() frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into()
)); ));
...@@ -628,6 +666,11 @@ fn test_validate_new_idty_after_few_uds() { ...@@ -628,6 +666,11 @@ fn test_validate_new_idty_after_few_uds() {
2, 2,
5, 5,
)); ));
assert_ok!(Distance::force_set_distance_status(
frame_system::RawOrigin::Root.into(),
5,
Some(pallet_distance::DistanceStatus::Valid)
));
assert_ok!(Identity::validate_identity( assert_ok!(Identity::validate_identity(
frame_system::RawOrigin::Signed(AccountKeyring::Bob.to_account_id()).into(), frame_system::RawOrigin::Signed(AccountKeyring::Bob.to_account_id()).into(),
5, 5,
...@@ -646,7 +689,7 @@ fn test_validate_new_idty_after_few_uds() { ...@@ -646,7 +689,7 @@ fn test_validate_new_idty_after_few_uds() {
} }
/// test that newly validated identity gets initialized with the next UD /// test that newly validated identity gets initialized with the next UD
// even when the method used is membership claim /// even when the method used is membership claim
#[test] #[test]
fn test_claim_memberhsip_after_few_uds() { fn test_claim_memberhsip_after_few_uds() {
ExtBuilder::new(1, 3, 4) ExtBuilder::new(1, 3, 4)
...@@ -654,7 +697,6 @@ fn test_claim_memberhsip_after_few_uds() { ...@@ -654,7 +697,6 @@ fn test_claim_memberhsip_after_few_uds() {
(AccountKeyring::Alice.to_account_id(), 1_000), (AccountKeyring::Alice.to_account_id(), 1_000),
(AccountKeyring::Bob.to_account_id(), 1_000), (AccountKeyring::Bob.to_account_id(), 1_000),
(AccountKeyring::Charlie.to_account_id(), 1_000), (AccountKeyring::Charlie.to_account_id(), 1_000),
(AccountKeyring::Eve.to_account_id(), 1_000),
]) ])
.build() .build()
.execute_with(|| { .execute_with(|| {
...@@ -682,6 +724,11 @@ fn test_claim_memberhsip_after_few_uds() { ...@@ -682,6 +724,11 @@ fn test_claim_memberhsip_after_few_uds() {
)); ));
// eve should be able to claim her membership // eve should be able to claim her membership
assert_ok!(Distance::force_set_distance_status(
frame_system::RawOrigin::Root.into(),
5,
Some(pallet_distance::DistanceStatus::Valid)
));
assert_ok!(Membership::claim_membership( assert_ok!(Membership::claim_membership(
frame_system::RawOrigin::Signed(AccountKeyring::Eve.to_account_id()).into(), frame_system::RawOrigin::Signed(AccountKeyring::Eve.to_account_id()).into(),
)); ));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment