Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
1xstj committed Nov 24, 2023
1 parent dd85da3 commit cd6dce5
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 10 deletions.
21 changes: 14 additions & 7 deletions pallets/roles/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ use super::*;
use frame_support::{
log,
pallet_prelude::DispatchResult,
traits::{Currency, ExistenceRequirement::KeepAlive, OneSessionHandler},
traits::{Currency, OneSessionHandler},
};
use sp_runtime::{Percent, Saturating};
use sp_std::ops::Div;
use sp_runtime::{traits::CheckedDiv, Percent, Saturating};

use tangle_primitives::{jobs::JobKey, traits::roles::RolesHandler};

/// Implements RolesHandler for the pallet.
Expand Down Expand Up @@ -208,10 +208,17 @@ impl<T: Config> Pallet<T> {
let reward_distribution = T::ValidatorRewardDistribution::get();

let dist = reward_distribution.get_reward_distribution();
let tss_validator_reward =
dist.0.mul_floor(total_rewards).div((tss_validators.len() as u32).into());
let zksaas_validators_reward =
dist.1.mul_floor(total_rewards).div((zksaas_validators.len() as u32).into());

let tss_validator_reward = dist
.0
.mul_floor(total_rewards)
.checked_div(&(tss_validators.len() as u32).into())
.unwrap_or(Zero::zero());
let zksaas_validators_reward = dist
.1
.mul_floor(total_rewards)
.checked_div(&(zksaas_validators.len() as u32).into())
.unwrap_or(Zero::zero());

log::debug!("Reward for tss validator : {:?}", tss_validator_reward);
log::debug!("Reward for zksaas validator : {:?}", zksaas_validators_reward);
Expand Down
13 changes: 10 additions & 3 deletions pallets/roles/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ use sp_core::H256;
use sp_runtime::{
testing::{Header, UintAuthorityId},
traits::IdentityLookup,
BuildStorage, DispatchResult, Perbill,
BuildStorage, DispatchResult, Perbill, Percent,
};
use tangle_primitives::{jobs::*, traits::jobs::MPCHandler};

use tangle_primitives::{jobs::*, roles::ValidatorRewardDistribution, traits::jobs::MPCHandler};
pub type AccountId = u64;
pub type Balance = u128;
pub type BlockNumber = u64;
Expand Down Expand Up @@ -243,11 +242,19 @@ impl JobsHandler<AccountId> for MockJobsHandler {
}
}

parameter_types! {
pub InflationRewardPerEra: Balance = 10_000;
pub Reward : ValidatorRewardDistribution = ValidatorRewardDistribution::try_new(Percent::from_rational(1_u32,2_u32), Percent::from_rational(1_u32,2_u32)).unwrap();
}

impl Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type JobsHandler = MockJobsHandler;
type MaxRolesPerAccount = ConstU32<2>;
type MPCHandler = MockMPCHandler;
type InflationRewardPerEra = InflationRewardPerEra;
type AuthorityId = UintAuthorityId;
type ValidatorRewardDistribution = Reward;
type WeightInfo = ();
}

Expand Down
57 changes: 57 additions & 0 deletions pallets/roles/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,60 @@ fn test_unbound_funds_should_work_if_no_role_assigned() {
})]);
});
}

#[test]
fn test_reward_dist_works_as_expected_with_one_validator() {
new_test_ext_raw_authorities(vec![1, 2, 3, 4]).execute_with(|| {
assert_eq!(Balances::free_balance(1), 20_000);

// Roles user is interested in re-staking.
let role_records = vec![
RoleStakingRecord {
metadata: RoleTypeMetadata::Tss(Default::default()),
re_staked: 2500,
},
RoleStakingRecord {
metadata: RoleTypeMetadata::ZkSaas(Default::default()),
re_staked: 2500,
},
];

assert_ok!(Roles::assign_roles(RuntimeOrigin::signed(1), role_records));

// The reward is 100, we have 5 authorities
assert_ok!(Roles::distribute_rewards());

// ensure the distribution is correct
assert_eq!(Balances::free_balance(1), 20_000 + 10_000);
});
}

#[test]
fn test_reward_dist_works_as_expected_with_multiple_validator() {
new_test_ext_raw_authorities(vec![1, 2, 3, 4]).execute_with(|| {
let _reward_amount = 10_000;
assert_eq!(Balances::free_balance(1), 20_000);

// Roles user is interested in re-staking.
let role_records = vec![
RoleStakingRecord {
metadata: RoleTypeMetadata::Tss(Default::default()),
re_staked: 2500,
},
RoleStakingRecord {
metadata: RoleTypeMetadata::ZkSaas(Default::default()),
re_staked: 2500,
},
];

assert_ok!(Roles::assign_roles(RuntimeOrigin::signed(1), role_records.clone()));
assert_ok!(Roles::assign_roles(RuntimeOrigin::signed(2), role_records));

// The reward is 100, we have 5 authorities
assert_ok!(Roles::distribute_rewards());

// ensure the distribution is correct
assert_eq!(Balances::free_balance(1), 20_000 + 5000);
assert_eq!(Balances::free_balance(2), 20_000 + 5000);
});
}

0 comments on commit cd6dce5

Please sign in to comment.