Skip to content

Commit

Permalink
Fix bug #275:
Browse files Browse the repository at this point in the history
- Add a claim_reward feedback in the IssuedRewards Storage
- claim_reward returns OK if a MeetupValidationError occurs in the Registration phase
  • Loading branch information
echevrier committed Oct 27, 2022
1 parent 25bfafb commit a23b03d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 26 deletions.
2 changes: 1 addition & 1 deletion ceremonies/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ where
);

let cindex = encointer_scheduler::Pallet::<T>::current_ceremony_index();
IssuedRewards::<T>::insert((cid, cindex - 1), 0, ());
IssuedRewards::<T>::insert((cid, cindex - 1), 0, claim_reward_successfull());
let proof = create_proof_of_attendance::<T>(prover_account, cid, cindex - 1, prover);
proof
}
Expand Down
12 changes: 9 additions & 3 deletions ceremonies/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ mod storage_helper;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use encointer_primitives::common::PalletString;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

Expand Down Expand Up @@ -594,7 +595,12 @@ pub mod pallet {
// because in attesting phase there could be a failing early payout attempt
if current_phase == CeremonyPhaseType::Registering {
info!(target: LOG, "marking issuance as completed for failed meetup.");
<IssuedRewards<T>>::insert((cid, cindex), meetup_index, ());
<IssuedRewards<T>>::insert(
(cid, cindex),
meetup_index,
format!("Error::{:?}", err),
);
return Ok(Pays::No.into())
}
match err {
MeetupValidationError::BallotEmpty => {
Expand Down Expand Up @@ -1143,7 +1149,7 @@ pub mod pallet {
CommunityCeremony,
Blake2_128Concat,
MeetupIndexType,
(),
PalletString,
ValueQuery,
>;

Expand Down Expand Up @@ -1891,7 +1897,7 @@ impl<T: Config> Pallet<T> {
sp_io::offchain_index::set(&reputation_cache_dirty_key(participant), &true.encode());
}

<IssuedRewards<T>>::insert((cid, cindex), meetup_idx, ());
<IssuedRewards<T>>::insert((cid, cindex), meetup_idx, claim_reward_successfull());
info!(target: LOG, "issuing rewards completed");

Self::deposit_event(Event::RewardsIssued(
Expand Down
46 changes: 25 additions & 21 deletions ceremonies/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ fn registering_participant_works() {
let newbies = add_population(2, 2);
let newbie_1 = account_id(&newbies[0]);
let newbie_2 = account_id(&newbies[01]);
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, ());
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, claim_reward_successfull());
assert_ok!(register(newbie_1.clone(), cid, None));
assert_eq!(EncointerCeremonies::newbie_count((cid, cindex)), 1);

Expand Down Expand Up @@ -753,6 +753,8 @@ fn claim_rewards_works() {
run_to_next_phase();
// Registering
EncointerCeremonies::claim_rewards(Origin::signed(account_id(&alice)), cid, None).ok();
let msg = IssuedRewards::<TestRuntime>::get((cid, cindex), 1);
assert_eq!(msg, claim_reward_successfull());

assert!(event_deposited::<TestRuntime>(Event::RewardsIssued(cid, 1, 2).into()));

Expand Down Expand Up @@ -1035,9 +1037,11 @@ fn claim_rewards_error_results_in_meetup_marked_as_completed() {
run_to_next_phase();
// Registering
assert!(EncointerCeremonies::claim_rewards(Origin::signed(account_id(&alice)), cid, None)
.is_err());
.is_ok());
// in registering, the meetup is marked as completed
assert!(IssuedRewards::<TestRuntime>::contains_key((cid, cindex), 1));
let msg = IssuedRewards::<TestRuntime>::get((cid, cindex), 1);
assert_eq!(msg, format!("Error::{:?}", MeetupValidationError::NoDependableVote));
});
}

Expand Down Expand Up @@ -1273,7 +1277,7 @@ fn register_with_reputation_works() {

// see if Zoran can register with his fresh key
// for the next ceremony claiming his former attendance
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, ());
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, claim_reward_successfull());
let proof = prove_attendance(account_id(&zoran_new), cid, cindex - 1, &zoran);
assert_ok!(register(account_id(&zoran_new), cid, Some(proof)));
assert_eq!(
Expand Down Expand Up @@ -1674,7 +1678,7 @@ fn ceremony_index_and_purging_registry_works() {
IssuedRewards::<TestRuntime>::insert(
(cid, EncointerScheduler::current_ceremony_index()),
0,
(),
claim_reward_successfull(),
);

run_to_next_phase();
Expand Down Expand Up @@ -1738,7 +1742,7 @@ fn grow_population_and_removing_community_works() {
IssuedRewards::<TestRuntime>::insert(
(cid, EncointerScheduler::current_ceremony_index() - 1),
0,
(),
claim_reward_successfull(),
);
participants.iter().for_each(|p| {
assert!(
Expand Down Expand Up @@ -1946,8 +1950,8 @@ fn update_inactivity_counters_works() {

let mut cindex = 5;

IssuedRewards::<TestRuntime>::insert((cid0, cindex), 0, ());
IssuedRewards::<TestRuntime>::insert((cid1, cindex), 0, ());
IssuedRewards::<TestRuntime>::insert((cid0, cindex), 0, claim_reward_successfull());
IssuedRewards::<TestRuntime>::insert((cid1, cindex), 0, claim_reward_successfull());

let timeout = 1;
assert_eq!(
Expand All @@ -1956,7 +1960,7 @@ fn update_inactivity_counters_works() {
);

cindex += 1;
IssuedRewards::<TestRuntime>::insert((cid0, cindex), 0, ());
IssuedRewards::<TestRuntime>::insert((cid0, cindex), 0, claim_reward_successfull());
assert_eq!(
EncointerCeremonies::update_inactivity_counters(cindex, timeout, vec![cid0, cid1]),
vec![]
Expand Down Expand Up @@ -2006,7 +2010,7 @@ fn purge_inactive_communities_works() {
IssuedRewards::<TestRuntime>::insert(
(cid, EncointerScheduler::current_ceremony_index()),
0,
(),
claim_reward_successfull(),
);
run_to_next_phase();
run_to_next_phase();
Expand Down Expand Up @@ -2361,7 +2365,7 @@ fn remove_participant_from_registry_works() {
let cid = register_test_community::<TestRuntime>(None, 0.0, 0.0);
let cindex = EncointerScheduler::current_ceremony_index();

IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, ());
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, claim_reward_successfull());
let bootstrapper = account_id(&AccountKeyring::Ferdie.pair());
EncointerCommunities::insert_bootstrappers(cid, vec![bootstrapper.clone()]);

Expand Down Expand Up @@ -2422,7 +2426,7 @@ fn remove_participant_from_registry_works_for_all_participant_types() {
let cid = register_test_community::<TestRuntime>(None, 0.0, 0.0);
let cindex = EncointerScheduler::current_ceremony_index();

IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, ());
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, claim_reward_successfull());
let bootstrapper = account_id(&AccountKeyring::Ferdie.pair());
EncointerCommunities::insert_bootstrappers(cid, vec![bootstrapper.clone()]);

Expand Down Expand Up @@ -2474,7 +2478,7 @@ fn remove_participant_from_registry_with_no_participants_fails() {
let cid = register_test_community::<TestRuntime>(None, 0.0, 0.0);
let cindex = EncointerScheduler::current_ceremony_index();

IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, ());
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, claim_reward_successfull());
let bootstrapper = account_id(&AccountKeyring::Ferdie.pair());
EncointerCommunities::insert_bootstrappers(cid, vec![bootstrapper.clone()]);

Expand All @@ -2490,7 +2494,7 @@ fn upgrade_registration_works() {
let cid = register_test_community::<TestRuntime>(None, 0.0, 0.0);
let cindex = EncointerScheduler::current_ceremony_index();

IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, ());
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, claim_reward_successfull());
let bootstrapper = account_id(&AccountKeyring::Ferdie.pair());
EncointerCommunities::insert_bootstrappers(cid, vec![bootstrapper.clone()]);
assert!(EncointerBalances::issue(cid, &bootstrapper, NominalIncome::from_num(1)).is_ok());
Expand Down Expand Up @@ -2520,7 +2524,7 @@ fn upgrade_registration_fails_if_not_registered_or_not_newbie() {
new_test_ext().execute_with(|| {
let cid = register_test_community::<TestRuntime>(None, 0.0, 0.0);
let cindex = EncointerScheduler::current_ceremony_index();
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, ());
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, claim_reward_successfull());
let bootstrapper = account_id(&AccountKeyring::Ferdie.pair());
EncointerCommunities::insert_bootstrappers(cid, vec![bootstrapper.clone()]);
assert!(EncointerBalances::issue(cid, &bootstrapper, NominalIncome::from_num(1)).is_ok());
Expand Down Expand Up @@ -2555,7 +2559,7 @@ fn upgrade_registration_fails_in_wrong_phase() {
new_test_ext().execute_with(|| {
let cid = register_test_community::<TestRuntime>(None, 0.0, 0.0);
let cindex = EncointerScheduler::current_ceremony_index();
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, ());
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, claim_reward_successfull());
let bootstrapper = account_id(&AccountKeyring::Ferdie.pair());
EncointerCommunities::insert_bootstrappers(cid, vec![bootstrapper.clone()]);
assert!(EncointerBalances::issue(cid, &bootstrapper, NominalIncome::from_num(1)).is_ok());
Expand All @@ -2582,7 +2586,7 @@ fn upgrade_registration_fails_with_inexistent_community() {
new_test_ext().execute_with(|| {
let cid = register_test_community::<TestRuntime>(None, 0.0, 0.0);
let cindex = EncointerScheduler::current_ceremony_index();
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, ());
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, claim_reward_successfull());
let bootstrapper = account_id(&AccountKeyring::Ferdie.pair());
EncointerCommunities::insert_bootstrappers(cid, vec![bootstrapper.clone()]);
assert!(EncointerBalances::issue(cid, &bootstrapper, NominalIncome::from_num(1)).is_ok());
Expand All @@ -2608,7 +2612,7 @@ fn unregister_participant_works_with_reputables() {
new_test_ext().execute_with(|| {
let cid = register_test_community::<TestRuntime>(None, 0.0, 0.0);
let cindex = EncointerScheduler::current_ceremony_index();
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, ());
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, claim_reward_successfull());
let bootstrapper = account_id(&AccountKeyring::Ferdie.pair());
EncointerCommunities::insert_bootstrappers(cid, vec![bootstrapper.clone()]);
assert!(EncointerBalances::issue(cid, &bootstrapper, NominalIncome::from_num(1)).is_ok());
Expand Down Expand Up @@ -2649,7 +2653,7 @@ fn unregister_participant_fails_with_reputables_and_wrong_reputation() {
new_test_ext().execute_with(|| {
let cid = register_test_community::<TestRuntime>(None, 0.0, 0.0);
let cindex = EncointerScheduler::current_ceremony_index();
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, ());
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, claim_reward_successfull());
let bootstrapper = account_id(&AccountKeyring::Ferdie.pair());
EncointerCommunities::insert_bootstrappers(cid, vec![bootstrapper.clone()]);
assert!(EncointerBalances::issue(cid, &bootstrapper, NominalIncome::from_num(1)).is_ok());
Expand Down Expand Up @@ -2697,7 +2701,7 @@ fn unregister_participant_works_with_newbies() {
new_test_ext().execute_with(|| {
let cid = register_test_community::<TestRuntime>(None, 0.0, 0.0);
let cindex = EncointerScheduler::current_ceremony_index();
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, ());
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, claim_reward_successfull());
let bootstrapper = account_id(&AccountKeyring::Ferdie.pair());
EncointerCommunities::insert_bootstrappers(cid, vec![bootstrapper.clone()]);
assert!(EncointerBalances::issue(cid, &bootstrapper, NominalIncome::from_num(1)).is_ok());
Expand All @@ -2721,7 +2725,7 @@ fn unregister_participant_fails_in_wrong_phase() {
new_test_ext().execute_with(|| {
let cid = register_test_community::<TestRuntime>(None, 0.0, 0.0);
let cindex = EncointerScheduler::current_ceremony_index();
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, ());
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, claim_reward_successfull());
let bootstrapper = account_id(&AccountKeyring::Ferdie.pair());
EncointerCommunities::insert_bootstrappers(cid, vec![bootstrapper.clone()]);
assert!(EncointerBalances::issue(cid, &bootstrapper, NominalIncome::from_num(1)).is_ok());
Expand All @@ -2746,7 +2750,7 @@ fn unregister_participant_fails_with_inexistent_community() {
new_test_ext().execute_with(|| {
let cid = register_test_community::<TestRuntime>(None, 0.0, 0.0);
let cindex = EncointerScheduler::current_ceremony_index();
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, ());
IssuedRewards::<TestRuntime>::insert((cid, cindex - 1), 0, claim_reward_successfull());
let bootstrapper = account_id(&AccountKeyring::Ferdie.pair());
EncointerCommunities::insert_bootstrappers(cid, vec![bootstrapper.clone()]);
assert!(EncointerBalances::issue(cid, &bootstrapper, NominalIncome::from_num(1)).is_ok());
Expand Down
11 changes: 10 additions & 1 deletion primitives/src/ceremonies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
#[cfg(feature = "serde_derive")]
use serde::{Deserialize, Serialize};

use crate::communities::{CommunityIdentifier, Location};
use crate::{
common::PalletString,
communities::{CommunityIdentifier, Location},
};

use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_core::{RuntimeDebug, H256};
Expand Down Expand Up @@ -290,6 +294,7 @@ pub mod consts {
/// Dirty bit key for reputation offchain storage
pub const REPUTATION_CACHE_DIRTY_KEY: &[u8] = b"reputation_cache_dirty";
pub const STORAGE_REPUTATION_KEY: &[u8; 10] = b"reputation";
pub const CLAIM_REWARD_OK: &str = "Ok";
}

pub fn reputation_cache_key<Account: Encode>(account: &Account) -> Vec<u8> {
Expand All @@ -300,6 +305,10 @@ pub fn reputation_cache_dirty_key<Account: Encode>(account: &Account) -> Vec<u8>
(consts::REPUTATION_CACHE_DIRTY_KEY, account).encode()
}

pub fn claim_reward_successfull() -> PalletString {
PalletString::from(consts::CLAIM_REWARD_OK)
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)]
#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde_derive", serde(rename_all = "camelCase"))]
Expand Down

0 comments on commit a23b03d

Please sign in to comment.