Skip to content

Commit

Permalink
merge fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
1xstj committed Nov 22, 2023
1 parent 3549888 commit cb989fd
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 48 deletions.
24 changes: 16 additions & 8 deletions pallets/roles/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
// along with Tangle. If not, see <http://www.gnu.org/licenses/>.

use super::*;
use frame_support::{pallet_prelude::DispatchResult, traits::WithdrawReasons};
use sp_runtime::Saturating;
use frame_support::pallet_prelude::DispatchResult;
use sp_runtime::{Percent, Saturating};
use tangle_primitives::{jobs::JobKey, traits::roles::RolesHandler};

/// Implements RolesHandler for the pallet.
Expand All @@ -30,16 +30,24 @@ impl<T: Config> RolesHandler<T::AccountId> for Pallet<T> {
/// # Returns
/// Returns `true` if the validator is permitted to work with this job type, otherwise `false`.
fn is_validator(address: T::AccountId, job_key: JobKey) -> bool {
let assigned_role = AccountRolesMapping::<T>::get(address);
let assigned_roles = AccountRolesMapping::<T>::get(address);

if let Some(assigned_role) = assigned_role {
let mut found_role = false;

for assigned_role in assigned_roles {
match job_key {
JobKey::DKG | JobKey::DKGSignature => assigned_role.is_tss(),
JobKey::ZkSaasPhaseOne | JobKey::ZkSaasPhaseTwo => assigned_role.is_zksaas(),
JobKey::DKG | JobKey::DKGSignature =>
if assigned_role.is_tss() {
found_role = true;
},
JobKey::ZkSaasPhaseOne | JobKey::ZkSaasPhaseTwo =>
if assigned_role.is_zksaas() {
found_role = true;
},
}
} else {
return false
}

return found_role
}

/// Slash validator stake for the reported offence. The function should be a best effort
Expand Down
29 changes: 16 additions & 13 deletions pallets/roles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,23 +200,23 @@ pub mod pallet {
let staking_ledger =
pallet_staking::Ledger::<T>::get(&stash_account).ok_or(Error::<T>::NotValidator)?;

// validate the metadata
T::MPCHandler::validate_authority_key(
stash_account.clone(),
role.clone().get_authority_key(),
)?;

// Bond with stash account.
let stash_balance = T::Currency::free_balance(&stash_account);
let value = bond_value.min(stash_balance);
let max_re_staking_bond = Self::calculate_max_re_stake_amount(staking_ledger.active);

// Validate role staking records.
for record in records.clone() {
let role = record.role;
let re_stake_amount = record.re_staked;
// Check if role is already assigned.
ensure!(!Self::has_role(stash_account.clone(), role), Error::<T>::HasRoleAssigned);
ensure!(
!Self::has_role(stash_account.clone(), role.clone()),
Error::<T>::HasRoleAssigned
);

// validate the metadata
T::MPCHandler::validate_authority_key(
stash_account.clone(),
role.clone().get_authority_key(),
)?;

// Re-staking amount of record should meet min re-staking amount requirement.
let min_re_staking_bond = MinReStakingBond::<T>::get();
Expand All @@ -238,7 +238,7 @@ pub mod pallet {

// Now that records are validated we can add them and update ledger
for record in records {
Self::add_role(stash_account.clone(), record.role)?;
Self::add_role(stash_account.clone(), record.role.clone())?;
Self::deposit_event(Event::<T>::RoleAssigned {
account: stash_account.clone(),
role: record.role,
Expand Down Expand Up @@ -271,14 +271,17 @@ pub mod pallet {
);

// check if role is assigned.
ensure!(Self::has_role(stash_account.clone(), role), Error::<T>::NoRoleAssigned);
ensure!(
Self::has_role(stash_account.clone(), role.clone()),
Error::<T>::NoRoleAssigned
);

// TODO: Call jobs manager to remove the services.
// On successful removal of services, remove the role from the mapping.
// Issue link for reference : https://github.com/webb-tools/tangle/issues/292

// Remove role from the mapping.
Self::remove_role(stash_account.clone(), role)?;
Self::remove_role(stash_account.clone(), role.clone())?;
// Remove stash account related info.
Self::kill_stash(&stash_account);

Expand Down
5 changes: 2 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, Perbill,
BuildStorage, DispatchResult, Perbill,
};
use tangle_primitives::traits::jobs::MPCHandler;
use tangle_primitives::jobs::*;
use tangle_primitives::{jobs::*, traits::jobs::MPCHandler};

pub type AccountId = u64;
pub type Balance = u128;
Expand Down
58 changes: 34 additions & 24 deletions pallets/roles/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,35 @@
use super::*;
use frame_support::{assert_err, assert_ok};
use mock::*;
use tangle_primitives::{jobs::ValidatorOffence, traits::roles::RolesHandler};

#[test]
fn test_assign_roles() {
new_test_ext_raw_authorities(vec![1, 2, 3, 4]).execute_with(|| {
// Initially account if funded with 10000 tokens and we are trying to bond 5000 tokens

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

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

assert_events(vec![RuntimeEvent::Roles(crate::Event::RoleAssigned {
account: 1,
role: RoleType::Tss,
role: RoleType::Tss(Default::default()),
})]);

// Lets verify role assigned to account.
assert_eq!(Roles::has_role(1, RoleType::Tss), true);
assert_eq!(Roles::has_role(1, RoleType::Tss(Default::default())), true);
// Verify ledger mapping
assert_eq!(
Roles::ledger(1),
Some(RoleStakingLedger {
stash: 1,
total: 5000,
roles: vec![RoleStakingRecord { role: RoleType::Tss, re_staked: 5000 }]
roles: vec![RoleStakingRecord {
role: RoleType::Tss(Default::default()),
re_staked: 5000
}]
})
);
});
Expand All @@ -56,26 +59,29 @@ fn test_assign_multiple_roles() {

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

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

// Lets verify role assigned to account.
assert_eq!(Roles::has_role(1, RoleType::Tss), true);
assert_eq!(Roles::has_role(1, RoleType::Tss(Default::default())), true);

// Lets verify role assigned to account.
assert_eq!(Roles::has_role(1, RoleType::ZkSaas), true);
assert_eq!(Roles::has_role(1, RoleType::ZkSaas(Default::default())), true);

assert_eq!(
Roles::ledger(1),
Some(RoleStakingLedger {
stash: 1,
total: 5000,
roles: vec![
RoleStakingRecord { role: RoleType::Tss, re_staked: 2500 },
RoleStakingRecord { role: RoleType::ZkSaas, re_staked: 2500 },
RoleStakingRecord { role: RoleType::Tss(Default::default()), re_staked: 2500 },
RoleStakingRecord {
role: RoleType::ZkSaas(Default::default()),
re_staked: 2500
},
]
})
);
Expand All @@ -91,8 +97,8 @@ fn test_assign_roles_should_fail_if_total_re_stake_value_exceeds_max_re_stake_va

// Roles user is interested in re-staking.
let role_records = vec![
RoleStakingRecord { role: RoleType::Tss, re_staked: 5000 },
RoleStakingRecord { role: RoleType::ZkSaas, re_staked: 5000 },
RoleStakingRecord { role: RoleType::Tss(Default::default()), re_staked: 5000 },
RoleStakingRecord { role: RoleType::ZkSaas(Default::default()), re_staked: 5000 },
];
// Since max re_stake limit is 5000 it should fail with `ExceedsMaxReStakeValue` error.
assert_err!(
Expand All @@ -108,20 +114,21 @@ fn test_clear_role() {
// Initially account if funded with 10000 tokens and we are trying to bond 5000 tokens

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

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

// Now lets clear the role
assert_ok!(Roles::clear_role(RuntimeOrigin::signed(1), RoleType::Tss));
assert_ok!(Roles::clear_role(RuntimeOrigin::signed(1), RoleType::Tss(Default::default())));

assert_events(vec![RuntimeEvent::Roles(crate::Event::RoleRemoved {
account: 1,
role: RoleType::Tss,
role: RoleType::Tss(Default::default()),
})]);

// Role should be removed from account role mappings.
assert_eq!(Roles::has_role(1, RoleType::Tss), false);
assert_eq!(Roles::has_role(1, RoleType::Tss(Default::default())), false);

// Ledger should be removed from ledger mappings.
assert_eq!(Roles::ledger(1), None);
Expand All @@ -134,7 +141,8 @@ fn test_assign_roles_should_fail_if_not_validator() {
// we will use account 5 which is not a validator

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

assert_err!(
Roles::assign_roles(RuntimeOrigin::signed(5), role_records),
Expand All @@ -150,18 +158,19 @@ fn test_unbound_funds_should_work() {
// for providing TSS services.

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

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

// Lets verify role is assigned to account.
assert_eq!(Roles::has_role(1, RoleType::Tss), true);
assert_eq!(Roles::has_role(1, RoleType::Tss(Default::default())), true);

// Lets clear the role.
assert_ok!(Roles::clear_role(RuntimeOrigin::signed(1), RoleType::Tss));
assert_ok!(Roles::clear_role(RuntimeOrigin::signed(1), RoleType::Tss(Default::default())));

// Role should be removed from account role mappings.
assert_eq!(Roles::has_role(1, RoleType::Tss), false);
assert_eq!(Roles::has_role(1, RoleType::Tss(Default::default())), false);

// unbound funds.
assert_ok!(Roles::unbound_funds(RuntimeOrigin::signed(1), 5000));
Expand All @@ -186,12 +195,13 @@ fn test_unbound_funds_should_fail_if_role_assigned() {
// for providing TSS services.

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

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

// Lets verify role is assigned to account.
assert_eq!(Roles::has_role(1, RoleType::Tss), true);
assert_eq!(Roles::has_role(1, RoleType::Tss(Default::default())), true);

// Lets try to unbound funds.
assert_err!(
Expand Down

0 comments on commit cb989fd

Please sign in to comment.