Skip to content

Commit

Permalink
Validator re-staking (#297)
Browse files Browse the repository at this point in the history
* validator checks before assigning role

* block unbond call from pallet-staking and allow unbonding through roles-pallet

* update errors

* update docs

* add new tests

* remove slashing test

* Option to choose re-stake value (Full or custom)

* handle unwrap

* cargo fmt

* block chill and withdraw-unbond calls

* update events

* cargo fmt --all

* convert DispatchErrorWithPostInfo to DispatchError
  • Loading branch information
salman01zp authored Nov 15, 2023
1 parent bf7e79a commit 1ab7dad
Show file tree
Hide file tree
Showing 8 changed files with 518 additions and 162 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions pallets/roles/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ serde = { workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
pallet-balances = { workspace = true }
pallet-staking = { workspace = true }
pallet-session = { workspace = true }
pallet-timestamp = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
sp-staking = { workspace = true }
tangle-primitives = {workspace = true, default-features = false }
frame-benchmarking = { workspace = true, optional = true }
frame-election-provider-support = { workspace = true }

[dev-dependencies]
hex-literal = { workspace = true }
Expand All @@ -40,8 +45,13 @@ std = [
"sp-runtime/std",
"sp-std/std",
"sp-io/std",
"sp-staking/std",
"pallet-balances/std",
"tangle-primitives/std"
"pallet-staking/std",
"pallet-session/std",
"pallet-timestamp/std",
"tangle-primitives/std",
"frame-election-provider-support/std",
]

try-runtime = ["frame-support/try-runtime"]
Expand All @@ -50,5 +60,9 @@ runtime-benchmarks = [
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"pallet-balances/runtime-benchmarks"
"sp-staking/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"pallet-staking/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"frame-election-provider-support/runtime-benchmarks",
]
34 changes: 4 additions & 30 deletions pallets/roles/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// 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 tangle_primitives::{roles::RoleType, traits::roles::RolesHandler};

Expand Down Expand Up @@ -73,7 +72,7 @@ impl<T: Config> Pallet<T> {
/// The total amount of the balance that can be slashed.
pub fn slashable_balance_of(stash: &T::AccountId) -> BalanceOf<T> {
// Weight note: consider making the stake accessible through stash.
Self::ledger(&stash).map(|l| l.total_locked).unwrap_or_default()
Self::ledger(&stash).map(|l| l.total).unwrap_or_default()
}

/// Slash the given amount from the stash account.
Expand All @@ -85,9 +84,9 @@ impl<T: Config> Pallet<T> {
address: T::AccountId,
slash_amount: T::CurrencyBalance,
) -> sp_runtime::DispatchResult {
let mut ledger = Self::ledger(&address).ok_or(Error::<T>::InvalidStashController)?;
let mut ledger = Self::ledger(&address).ok_or(Error::<T>::AccountNotPaired)?;
let (_imbalance, _missing) = T::Currency::slash(&address, slash_amount.into());
ledger.total_locked = ledger.total_locked.saturating_sub(slash_amount.into());
ledger.total = ledger.total.saturating_sub(slash_amount.into());
Self::update_ledger(&address, &ledger);
Self::deposit_event(Event::Slashed { account: address, amount: slash_amount });
Ok(())
Expand All @@ -102,36 +101,11 @@ impl<T: Config> Pallet<T> {
/// # Note
/// This function will set a lock on the stash account.
pub(crate) fn update_ledger(staker: &T::AccountId, ledger: &RoleStakingLedger<T>) {
T::Currency::set_lock(
ROLES_STAKING_ID,
&ledger.stash,
ledger.total_locked,
WithdrawReasons::all(),
);
<Ledger<T>>::insert(staker, ledger);
}

/// Kill the stash account and remove all related information.
pub(crate) fn kill_stash(stash: &T::AccountId) -> DispatchResult {
pub(crate) fn kill_stash(stash: &T::AccountId) {
<Ledger<T>>::remove(&stash);
Ok(())
}

/// Unbond the stash account.
///
/// # Parameters
/// - `ledger`: The ledger of the stash account.
///
/// # Note
/// This function will remove the lock on the stash account.
pub(super) fn unbond(ledger: &RoleStakingLedger<T>) -> DispatchResult {
let stash = ledger.stash.clone();
if ledger.total_locked > T::Currency::minimum_balance() {
// Remove the lock.
T::Currency::remove_lock(ROLES_STAKING_ID, &stash);
// Kill the stash and related information.
Self::kill_stash(&stash)?;
}
Ok(())
}
}
Loading

0 comments on commit 1ab7dad

Please sign in to comment.