Skip to content

Commit

Permalink
fix: Roles pallet - rename validators to restakers (#472)
Browse files Browse the repository at this point in the history
* rename all validators to restakers

* set submission len to 32
  • Loading branch information
1xstj authored Feb 14, 2024
1 parent 8b59cd0 commit 7874bea
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 90 deletions.
2 changes: 1 addition & 1 deletion pallets/dkg/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ parameter_types! {
#[derive(Clone, Debug, Eq, PartialEq, TypeInfo)]
pub const MaxParticipants: u32 = 10;
#[derive(Clone, Debug, Eq, PartialEq, TypeInfo)]
pub const MaxSubmissionLen: u32 = 256;
pub const MaxSubmissionLen: u32 = 32;
#[derive(Clone, Debug, Eq, PartialEq, TypeInfo)]
pub const MaxKeyLen: u32 = 256;
#[derive(Clone, Debug, Eq, PartialEq, TypeInfo)]
Expand Down
4 changes: 2 additions & 2 deletions pallets/jobs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ pub mod module {

for participant in participants {
ensure!(
T::RolesHandler::is_validator(participant.clone(), role_type),
T::RolesHandler::is_restaker(participant.clone(), role_type),
Error::<T>::InvalidValidator
);

Expand All @@ -299,7 +299,7 @@ pub mod module {
let participants = result.participants().ok_or(Error::<T>::InvalidJobPhase)?;
for participant in participants {
ensure!(
T::RolesHandler::is_validator(participant.clone(), role_type),
T::RolesHandler::is_restaker(participant.clone(), role_type),
Error::<T>::InvalidValidator
);

Expand Down
4 changes: 3 additions & 1 deletion pallets/jobs/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ impl pallet_staking::Config for Runtime {

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

Expand All @@ -335,6 +336,7 @@ impl pallet_roles::Config for Runtime {
type ValidatorSet = Historical;
type ReportOffences = OffenceHandler;
type MaxKeyLen = MaxKeyLen;
type MaxRestake = MaxRestake;
type MaxRolesPerValidator = MaxActiveJobsPerValidator;
type WeightInfo = ();
}
Expand All @@ -344,7 +346,7 @@ parameter_types! {
#[derive(Clone, Debug, Eq, PartialEq, TypeInfo)]
pub const MaxParticipants: u32 = 10;
#[derive(Clone, Debug, Eq, PartialEq, TypeInfo)]
pub const MaxSubmissionLen: u32 = 256;
pub const MaxSubmissionLen: u32 = 32;
#[derive(Clone, Debug, Eq, PartialEq, TypeInfo)]
pub const MaxKeyLen: u32 = 256;
#[derive(Clone, Debug, Eq, PartialEq, TypeInfo)]
Expand Down
4 changes: 2 additions & 2 deletions pallets/roles/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ mod benchmarks {
fn update_profile() {
let caller: T::AccountId = create_validator_account::<T>("Alice");
let shared_profile = shared_profile::<T>();
let ledger = RoleStakingLedger::<T>::new(caller.clone(), shared_profile.clone(), vec![]);
let ledger = RestakingLedger::<T>::new(caller.clone(), shared_profile.clone(), vec![]);
Ledger::<T>::insert(caller.clone(), ledger);
// Updating shared stake from 3000 to 5000 tokens
let updated_profile = updated_profile::<T>();
Expand All @@ -129,7 +129,7 @@ mod benchmarks {
fn delete_profile() {
let caller: T::AccountId = create_validator_account::<T>("Alice");
let shared_profile = shared_profile::<T>();
let ledger = RoleStakingLedger::<T>::new(caller.clone(), shared_profile.clone(), vec![]);
let ledger = RestakingLedger::<T>::new(caller.clone(), shared_profile.clone(), vec![]);
Ledger::<T>::insert(caller.clone(), ledger);

#[extrinsic_call]
Expand Down
35 changes: 18 additions & 17 deletions pallets/roles/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ use frame_support::{
};
use sp_runtime::{
traits::{CheckedDiv, Convert},
Perbill, Percent,
Perbill,
};

use sp_staking::offence::Offence;
use tangle_primitives::{
jobs::{traits::JobsHandler, JobId, ReportValidatorOffence},
jobs::{traits::JobsHandler, JobId, ReportRestakerOffence},
roles::traits::RolesHandler,
};

Expand All @@ -42,7 +42,7 @@ 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, role_type: RoleType) -> bool {
fn is_restaker(address: T::AccountId, role_type: RoleType) -> bool {
let assigned_roles = AccountRolesMapping::<T>::get(address);
assigned_roles.contains(&role_type)
}
Expand All @@ -57,7 +57,7 @@ impl<T: Config> RolesHandler<T::AccountId> for Pallet<T> {
///
/// Returns Ok() if validator offence report is submitted successfully.
fn report_offence(
offence_report: ReportValidatorOffence<T::AccountId>,
offence_report: ReportRestakerOffence<T::AccountId>,
) -> sp_runtime::DispatchResult {
Self::report_offence(offence_report)
}
Expand Down Expand Up @@ -192,7 +192,7 @@ impl<T: Config> Pallet<T> {
/// Check if account can chill, unbond and withdraw funds.
///
/// # Parameters
/// - `account`: The account ID of the validator.
/// - `account`: The account ID of the restaker.
///
/// # Returns
/// Returns boolean value.
Expand All @@ -208,14 +208,15 @@ impl<T: Config> Pallet<T> {
/// Calculate max restake amount for the given account.
///
/// # Parameters
/// - `total_stake`: Total stake of the validator
/// - `total_stake`: Total stake of the restaker
///
/// # Returns
/// Returns the max restake amount.
pub(crate) fn calculate_max_restake_amount(total_stake: BalanceOf<T>) -> BalanceOf<T> {
// User can restake max 50% of the total stake
Percent::from_percent(50) * total_stake
T::MaxRestake::get() * total_stake
}

/// Calculate slash value for restaked amount
///
/// # Parameters
Expand All @@ -232,17 +233,17 @@ impl<T: Config> Pallet<T> {
slash_fraction * total_stake
}

/// Report offence for the given validator.
/// This function will report validators for committing offence.
/// Report offence for the given restaker.
/// This function will report restakers for committing offence.
///
/// # Parameters
/// - `offence_report`: The offence report.
///
/// # Returns
///
/// Returns Ok() if validator offence report is submitted successfully.
/// Returns Ok() if restaker offence report is submitted successfully.
pub(crate) fn report_offence(
offence_report: ReportValidatorOffence<T::AccountId>,
offence_report: ReportRestakerOffence<T::AccountId>,
) -> sp_runtime::DispatchResult {
let offenders = offence_report
.clone()
Expand Down Expand Up @@ -288,10 +289,10 @@ impl<T: Config> Pallet<T> {
/// Update the ledger for the given stash account.
///
/// # Parameters
/// - `staker`: The stash account ID.
/// - `restaker`: The stash account ID.
/// - `ledger`: The new ledger.
pub(crate) fn update_ledger(staker: &T::AccountId, ledger: &RoleStakingLedger<T>) {
<Ledger<T>>::insert(staker, ledger);
pub(crate) fn update_ledger(restaker: &T::AccountId, ledger: &RestakingLedger<T>) {
<Ledger<T>>::insert(restaker, ledger);
}

pub fn distribute_rewards() -> DispatchResult {
Expand Down Expand Up @@ -348,12 +349,12 @@ impl<T: Config> Pallet<T> {
Ok(())
}

pub fn update_ledger_role_key(staker: &T::AccountId, role_key: Vec<u8>) -> DispatchResult {
let mut ledger = Ledger::<T>::get(staker).ok_or(Error::<T>::NoProfileFound)?;
pub fn update_ledger_role_key(restaker: &T::AccountId, role_key: Vec<u8>) -> DispatchResult {
let mut ledger = Ledger::<T>::get(restaker).ok_or(Error::<T>::NoProfileFound)?;
let bounded_role_key: BoundedVec<u8, T::MaxKeyLen> =
role_key.try_into().map_err(|_| Error::<T>::KeySizeExceeded)?;
ledger.role_key = bounded_role_key;
Self::update_ledger(staker, &ledger);
Self::update_ledger(restaker, &ledger);
Ok(())
}
}
Expand Down
Loading

0 comments on commit 7874bea

Please sign in to comment.