Skip to content

Commit

Permalink
Unlock rewards to slash offline validators
Browse files Browse the repository at this point in the history
  • Loading branch information
mishk committed Jan 9, 2023
1 parent 0404e80 commit b55ed72
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 65 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

57 changes: 57 additions & 0 deletions pallets/rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,59 @@ impl<T: Config> Module<T> {
<Self as Store>::RewardLocks::insert(author, locks);
}

pub fn unlock_upto(author: &T::AccountId, amount: BalanceOf<T>) {
let mut locks = Self::reward_locks(&author);
let locked_amount = Self::total_locked(&author);
let mut total_unlock: BalanceOf<T> = Zero::zero();
let mut to_unlock = Vec::new();
let unlock_amount = locked_amount - amount;

let d = u128::from_le_bytes(locked_amount.encode().try_into().unwrap());
log::debug!(target: LOG_TARGET, "locked_amount: {}", d);
let d = u128::from_le_bytes(amount.encode().try_into().unwrap());
log::debug!(target: LOG_TARGET, "amount: {}", d);

for (block_number, locked_balance) in locks.iter() {
if total_unlock.saturating_add(*locked_balance) >= unlock_amount {
let adj = total_unlock.saturating_add(*locked_balance) - unlock_amount;
total_unlock = total_unlock.saturating_add(adj);
if adj == Zero::zero() {
to_unlock.push(*block_number);
}
break
}
to_unlock.push(*block_number);
total_unlock = total_unlock.saturating_add(*locked_balance);
}
let d = u128::from_le_bytes(total_unlock.encode().try_into().unwrap());
log::debug!(target: LOG_TARGET, "total_unlocked: {}", d);

let total_locked = locked_amount - total_unlock;

for block_number in to_unlock {
locks.remove(&block_number);
}

let d = u128::from_le_bytes(total_locked.encode().try_into().unwrap());
log::debug!(target: LOG_TARGET, "total_locked: {}", d);

if total_locked <= Zero::zero() {
T::Currency::remove_lock(
REWARDS_ID,
&author,
);
}
else {
T::Currency::set_lock(
REWARDS_ID,
&author,
total_locked,
WithdrawReasons::except(WithdrawReasons::TRANSACTION_PAYMENT),
);
}
<Self as Store>::RewardLocks::insert(author, locks);
}

fn do_mints(mints: &BTreeMap<T::AccountId, BalanceOf<T>>) {
for (destination, mint) in mints {
drop(T::Currency::deposit_creating(&destination, *mint));
Expand All @@ -497,4 +550,8 @@ impl<T: Config> RewardLocksApi<T::AccountId, BalanceOf<T>> for Pallet<T> {
|s, (_block_number, locked_balance)| s.saturating_add(*locked_balance)
)
}

fn unlock_upto(author: &T::AccountId, amount: BalanceOf<T>) {
Self::unlock_upto(author, amount);
}
}
3 changes: 3 additions & 0 deletions pallets/validator-set/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ sp-io = { default-features = false, git = "https://github.com/paritytech/substra
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", rev = "b0777b4c7f7" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", rev = "b0777b4c7f7" }
sp-staking = { default-features = false, git = "https://github.com/paritytech/substrate", rev = "b0777b4c7f7" }
sp-version = { default-features = false, git = "https://github.com/paritytech/substrate", rev = "b0777b4c7f7" }
frame-benchmarking = { default-features = false, git = "tps://github.com/paritytech/substrate", rev = "b0777b4c7f7", optional = true }
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", rev = "b0777b4c7f7" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", rev = "b0777b4c7f7" }
pallet-session = { default-features = false, features = ['historical'], git = "https://github.com/paritytech/substrate", rev = "b0777b4c7f7" }
pallet-treasury = { default-features = false, git = "https://github.com/paritytech/substrate", rev = "b0777b4c7f7" }
pallet-balances = { default-features = false, git = "https://github.com/paritytech/substrate", rev = "b0777b4c7f7" }

scale-info = { default-features = false, features = ['derive'], version = '2.1.1' }

rewards-api = { default-features = false, path = "../../traits/rewards" }
validator-set-api = { default-features = false, path = "../../traits/validator-set" }
sp-consensus-poscan = { path = "../../primitives/consensus/poscan", default-features = false }

[features]
default = ['std']
Expand Down
Loading

0 comments on commit b55ed72

Please sign in to comment.