Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validator re-staking #297

Merged
merged 13 commits into from
Nov 15, 2023
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> {
salman01zp marked this conversation as resolved.
Show resolved Hide resolved
// 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());
salman01zp marked this conversation as resolved.
Show resolved Hide resolved
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
Loading