Skip to content
Snippets Groups Projects
Commit 68087d9c authored by Cédric Moreau's avatar Cédric Moreau
Browse files

feat(smith-members): events

parent ce5f317f
No related branches found
No related tags found
No related merge requests found
...@@ -116,8 +116,22 @@ pub mod pallet { ...@@ -116,8 +116,22 @@ pub mod pallet {
#[pallet::event] #[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)] #[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> { pub enum Event<T: Config> {
/// An identity is being inivited to become a smith
InvitationSent {
idty_index: T::IdtyIndex,
invited_by: T::IdtyIndex,
},
/// The invitation has been accepted
InvitationAccepted { idty_index: T::IdtyIndex },
/// Certification received
CertificationReceived {
idty_index: T::IdtyIndex,
issued_by: T::IdtyIndex,
},
/// A smith gathered enough certifications to become an authority (can call `go_online()`). /// A smith gathered enough certifications to become an authority (can call `go_online()`).
SmithBecameAuthority { idty_index: T::IdtyIndex }, PromotedToSmith { idty_index: T::IdtyIndex },
/// A smith has been removed from the smiths set
SmithExcluded { idty_index: T::IdtyIndex },
} }
#[pallet::genesis_config] #[pallet::genesis_config]
...@@ -248,7 +262,7 @@ pub mod pallet { ...@@ -248,7 +262,7 @@ pub mod pallet {
let who = ensure_signed(origin.clone())?; let who = ensure_signed(origin.clone())?;
let issuer = T::IdtyIdOf::convert(who.clone()).ok_or(Error::<T>::UnknownIssuer)?; let issuer = T::IdtyIdOf::convert(who.clone()).ok_or(Error::<T>::UnknownIssuer)?;
Self::check_invite_smith(issuer, receiver)?; Self::check_invite_smith(issuer, receiver)?;
Self::do_invite_smith(receiver); Self::do_invite_smith(issuer, receiver);
Ok(().into()) Ok(().into())
} }
...@@ -299,7 +313,7 @@ impl<T: Config> Pallet<T> { ...@@ -299,7 +313,7 @@ impl<T: Config> Pallet<T> {
Ok(().into()) Ok(().into())
} }
fn do_invite_smith(receiver: T::IdtyIndex) { fn do_invite_smith(issuer: T::IdtyIndex, receiver: T::IdtyIndex) {
let new_expires_on = CurrentSession::<T>::get() + T::InactivityMaxDuration::get(); let new_expires_on = CurrentSession::<T>::get() + T::InactivityMaxDuration::get();
// TODO: another way to write this? // TODO: another way to write this?
if Smiths::<T>::get(receiver).is_some() { if Smiths::<T>::get(receiver).is_some() {
...@@ -321,6 +335,10 @@ impl<T: Config> Pallet<T> { ...@@ -321,6 +335,10 @@ impl<T: Config> Pallet<T> {
); );
} }
ExpiresOn::<T>::append(new_expires_on, receiver); ExpiresOn::<T>::append(new_expires_on, receiver);
Self::deposit_event(Event::<T>::InvitationSent {
idty_index: receiver,
invited_by: issuer,
});
} }
fn check_accept_invitation(receiver: T::IdtyIndex) -> DispatchResultWithPostInfo { fn check_accept_invitation(receiver: T::IdtyIndex) -> DispatchResultWithPostInfo {
...@@ -339,6 +357,9 @@ impl<T: Config> Pallet<T> { ...@@ -339,6 +357,9 @@ impl<T: Config> Pallet<T> {
let maybe_smith_meta = maybe_smith_meta.as_mut().expect("status checked earlier"); let maybe_smith_meta = maybe_smith_meta.as_mut().expect("status checked earlier");
maybe_smith_meta.status = SmithStatus::Pending; maybe_smith_meta.status = SmithStatus::Pending;
}); });
Self::deposit_event(Event::<T>::InvitationAccepted {
idty_index: receiver,
});
Ok(().into()) Ok(().into())
} }
...@@ -388,8 +409,12 @@ impl<T: Config> Pallet<T> { ...@@ -388,8 +409,12 @@ impl<T: Config> Pallet<T> {
// expiry postponed // expiry postponed
let new_expires_on = CurrentSession::<T>::get() + T::InactivityMaxDuration::get(); let new_expires_on = CurrentSession::<T>::get() + T::InactivityMaxDuration::get();
maybe_smith_meta.expires_on = Some(new_expires_on); maybe_smith_meta.expires_on = Some(new_expires_on);
Self::deposit_event(Event::<T>::CertificationReceived {
idty_index: receiver,
issued_by: issuer,
});
if maybe_smith_meta.status == SmithStatus::Smith { if maybe_smith_meta.status == SmithStatus::Smith {
Self::deposit_event(Event::<T>::SmithBecameAuthority { Self::deposit_event(Event::<T>::PromotedToSmith {
idty_index: receiver, idty_index: receiver,
}); });
} }
...@@ -433,7 +458,7 @@ impl<T: Config> Pallet<T> { ...@@ -433,7 +458,7 @@ impl<T: Config> Pallet<T> {
smith, smith,
SmithRemovalReason::OfflineTooLong, SmithRemovalReason::OfflineTooLong,
); );
// TODO: add event? (+ add others too) Self::deposit_event(Event::<T>::SmithExcluded { idty_index: smith });
} }
} }
} }
...@@ -479,9 +504,7 @@ impl<T: Config> Pallet<T> { ...@@ -479,9 +504,7 @@ impl<T: Config> Pallet<T> {
// T::OnSmithDelete::on_smith_delete(idty_index, SmithRemovalReason::Blacklisted); // T::OnSmithDelete::on_smith_delete(idty_index, SmithRemovalReason::Blacklisted);
// } // }
} }
}
impl<T: Config> Pallet<T> {
fn provide_is_member(idty_id: &T::IdtyIndex) -> bool { fn provide_is_member(idty_id: &T::IdtyIndex) -> bool {
let Some(smith) = Smiths::<T>::get(idty_id) else { let Some(smith) = Smiths::<T>::get(idty_id) else {
return false; return false;
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#![cfg(test)] #![cfg(test)]
use crate::{self as pallet_smith_members}; use crate::{self as pallet_smith_members};
use frame_support::pallet_prelude::Hooks;
use frame_support::{ use frame_support::{
parameter_types, parameter_types,
traits::{ConstU32, ConstU64}, traits::{ConstU32, ConstU64},
...@@ -107,3 +108,14 @@ pub fn new_test_ext( ...@@ -107,3 +108,14 @@ pub fn new_test_ext(
.unwrap() .unwrap()
.into() .into()
} }
pub fn run_to_block(n: u64) {
while System::block_number() < n {
Smith::on_finalize(System::block_number());
System::on_finalize(System::block_number());
System::reset_events();
System::set_block_number(System::block_number() + 1);
System::on_initialize(System::block_number());
Smith::on_initialize(System::block_number());
}
}
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
#![cfg(test)] #![cfg(test)]
use super::*; use super::*;
use crate::mock::{new_test_ext, Runtime, RuntimeOrigin}; use crate::mock::{new_test_ext, run_to_block, Runtime, RuntimeEvent, RuntimeOrigin, System};
use frame_support::{assert_err, assert_ok}; use frame_support::{assert_err, assert_ok};
#[cfg(test)] #[cfg(test)]
...@@ -35,14 +35,23 @@ fn process_to_become_a_smith_and_lose_it() { ...@@ -35,14 +35,23 @@ fn process_to_become_a_smith_and_lose_it() {
], ],
}) })
.execute_with(|| { .execute_with(|| {
// Events cannot be recorded on genesis
run_to_block(1);
// State before // State before
assert_eq!(Smiths::<Runtime>::get(5), None); assert_eq!(Smiths::<Runtime>::get(5), None);
// Try to invite // Try to invite
assert_ok!(Pallet::<Runtime>::invite_smith(RuntimeOrigin::signed(1), 5)); assert_ok!(Pallet::<Runtime>::invite_smith(RuntimeOrigin::signed(1), 5));
System::assert_has_event(RuntimeEvent::Smith(Event::<Runtime>::InvitationSent {
idty_index: 5,
invited_by: 1,
}));
// Accept invitation // Accept invitation
assert_ok!(Pallet::<Runtime>::accept_invitation(RuntimeOrigin::signed( assert_ok!(Pallet::<Runtime>::accept_invitation(RuntimeOrigin::signed(
5 5
))); )));
System::assert_has_event(RuntimeEvent::Smith(Event::<Runtime>::InvitationAccepted {
idty_index: 5,
}));
// State after // State after
assert_eq!( assert_eq!(
Smiths::<Runtime>::get(5).unwrap(), Smiths::<Runtime>::get(5).unwrap(),
...@@ -58,6 +67,12 @@ fn process_to_become_a_smith_and_lose_it() { ...@@ -58,6 +67,12 @@ fn process_to_become_a_smith_and_lose_it() {
RuntimeOrigin::signed(1), RuntimeOrigin::signed(1),
5 5
)); ));
System::assert_has_event(RuntimeEvent::Smith(
Event::<Runtime>::CertificationReceived {
idty_index: 5,
issued_by: 1,
},
));
assert_eq!( assert_eq!(
Smiths::<Runtime>::get(5).unwrap(), Smiths::<Runtime>::get(5).unwrap(),
SmithMeta { SmithMeta {
...@@ -72,6 +87,15 @@ fn process_to_become_a_smith_and_lose_it() { ...@@ -72,6 +87,15 @@ fn process_to_become_a_smith_and_lose_it() {
RuntimeOrigin::signed(2), RuntimeOrigin::signed(2),
5 5
)); ));
System::assert_has_event(RuntimeEvent::Smith(
Event::<Runtime>::CertificationReceived {
idty_index: 5,
issued_by: 1,
},
));
System::assert_has_event(RuntimeEvent::Smith(Event::<Runtime>::PromotedToSmith {
idty_index: 5,
}));
assert_eq!( assert_eq!(
Smiths::<Runtime>::get(5).unwrap(), Smiths::<Runtime>::get(5).unwrap(),
SmithMeta { SmithMeta {
...@@ -89,6 +113,15 @@ fn process_to_become_a_smith_and_lose_it() { ...@@ -89,6 +113,15 @@ fn process_to_become_a_smith_and_lose_it() {
assert!(Smiths::<Runtime>::get(5).is_some()); assert!(Smiths::<Runtime>::get(5).is_some());
// On session 5 no more smiths because of lack of activity // On session 5 no more smiths because of lack of activity
Pallet::<Runtime>::on_new_session(5); Pallet::<Runtime>::on_new_session(5);
System::assert_has_event(RuntimeEvent::Smith(Event::<Runtime>::SmithExcluded {
idty_index: 1,
}));
System::assert_has_event(RuntimeEvent::Smith(Event::<Runtime>::SmithExcluded {
idty_index: 2,
}));
System::assert_has_event(RuntimeEvent::Smith(Event::<Runtime>::SmithExcluded {
idty_index: 5,
}));
assert_eq!( assert_eq!(
Smiths::<Runtime>::get(1), Smiths::<Runtime>::get(1),
Some(SmithMeta { Some(SmithMeta {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment