From d9ac4a430a3313ca8f2736ccf07b4f13f686e17e Mon Sep 17 00:00:00 2001 From: Leonardo Razovic <4128940+lrazovic@users.noreply.github.com> Date: Tue, 10 Sep 2024 15:53:58 +0200 Subject: [PATCH] Zepter --- pallets/funding/Cargo.toml | 1 + pallets/on-slash-vesting/Cargo.toml | 22 ++++++++-------- pallets/on-slash-vesting/src/lib.rs | 41 +++++++++++++---------------- runtimes/polimec/Cargo.toml | 6 ++++- 4 files changed, 35 insertions(+), 35 deletions(-) diff --git a/pallets/funding/Cargo.toml b/pallets/funding/Cargo.toml index 86165bff7..a151c6ad4 100644 --- a/pallets/funding/Cargo.toml +++ b/pallets/funding/Cargo.toml @@ -71,6 +71,7 @@ std = [ "frame-system/std", "itertools/use_std", "log/std", + "on-slash-vesting/std", "pallet-assets/std", "pallet-balances/std", "pallet-insecure-randomness-collective-flip/std", diff --git a/pallets/on-slash-vesting/Cargo.toml b/pallets/on-slash-vesting/Cargo.toml index ffb1e7745..b3b022e0c 100644 --- a/pallets/on-slash-vesting/Cargo.toml +++ b/pallets/on-slash-vesting/Cargo.toml @@ -29,14 +29,14 @@ workspace = true default = [ "std" ] std = [ - "pallet-vesting/std", - "frame-support/std", - "frame-system/std", - "pallet-balances/std", - "log/std", - "parity-scale-codec/std", - "scale-info/std", - "sp-runtime/std", - "sp-io/std", - "serde/std", -] \ No newline at end of file + "frame-support/std", + "frame-system/std", + "log/std", + "pallet-balances/std", + "pallet-vesting/std", + "parity-scale-codec/std", + "scale-info/std", + "serde/std", + "sp-io/std", + "sp-runtime/std", +] diff --git a/pallets/on-slash-vesting/src/lib.rs b/pallets/on-slash-vesting/src/lib.rs index 9e9c17ea6..c8b012f81 100644 --- a/pallets/on-slash-vesting/src/lib.rs +++ b/pallets/on-slash-vesting/src/lib.rs @@ -6,14 +6,12 @@ mod mock; #[cfg(test)] mod test; -extern crate alloc; -use alloc::vec::Vec; use frame_support::{ sp_runtime::{traits::Convert, FixedPointNumber, FixedU128}, traits::{Currency, OriginTrait}, }; use pallet_vesting::Vesting; -use sp_runtime::traits::BlockNumberProvider; +use sp_runtime::{traits::BlockNumberProvider, BoundedVec}; pub trait OnSlash { fn on_slash(account: &AccountId, amount: Balance); @@ -33,27 +31,24 @@ where T::Currency: Currency, Balance = u128>, { fn on_slash(account: &AccountIdOf, slashed_amount: u128) { - let Some(vesting_schedules) = >::get(account) else { return }; - let vesting_schedules = vesting_schedules.to_vec(); - let mut new_vesting_schedules = Vec::new(); - let now = T::BlockNumberProvider::current_block_number(); - for schedule in vesting_schedules { - let total_locked = schedule.locked_at::(now).saturating_sub(slashed_amount); - let start_block: u128 = T::BlockNumberToBalance::convert(now); - let end_block: u128 = schedule.ending_block_as_balance::(); - let duration = end_block.saturating_sub(start_block); - let per_block = FixedU128::from_rational(total_locked, duration).saturating_mul_int(1u128); - let new_schedule = pallet_vesting::VestingInfo::new(total_locked, per_block, now); - if new_schedule.is_valid() { - new_vesting_schedules.push(new_schedule); + if let Some(vesting_schedules) = >::get(account) { + let mut new_vesting_schedules = BoundedVec::with_bounded_capacity(vesting_schedules.len()); + let now = T::BlockNumberProvider::current_block_number(); + for schedule in vesting_schedules { + let total_locked = schedule.locked_at::(now).saturating_sub(slashed_amount); + let start_block = T::BlockNumberToBalance::convert(now); + let end_block = schedule.ending_block_as_balance::(); + let duration = end_block.saturating_sub(start_block); + let per_block = FixedU128::from_rational(total_locked, duration).saturating_mul_int(1u128); + let new_schedule = pallet_vesting::VestingInfo::new(total_locked, per_block, now); + if new_schedule.is_valid() { + // The push should always succeed because we are iterating over a bounded vector. + let push_result = new_vesting_schedules.try_push(new_schedule); + debug_assert!(push_result.is_ok()); + } } + >::set(account, Some(new_vesting_schedules)); + let _ = >::vest(T::RuntimeOrigin::signed(account.clone())); } - let Ok(new_vesting_schedules) = new_vesting_schedules.try_into() else { - log::error!("Failed to convert new vesting schedules into BoundedVec"); - return - }; - >::set(account, Some(new_vesting_schedules)); - let vest_result = >::vest(T::RuntimeOrigin::signed(account.clone())); - debug_assert!(vest_result.is_ok()); } } diff --git a/runtimes/polimec/Cargo.toml b/runtimes/polimec/Cargo.toml index bfe2d8b38..21477dd59 100644 --- a/runtimes/polimec/Cargo.toml +++ b/runtimes/polimec/Cargo.toml @@ -130,6 +130,7 @@ std = [ "frame-system/std", "frame-try-runtime?/std", "log/std", + "on-slash-vesting/std", "orml-oracle/std", "pallet-assets/std", "pallet-aura/std", @@ -276,6 +277,9 @@ try-runtime = [ # A feature that should be enabled when the runtime should be built for on-chain # deployment. This will disable stuff that shouldn't be part of the on-chain wasm # to make it smaller, like logging for example. -on-chain-release-build = [ "sp-api/disable-logging", "pallet-funding/on-chain-release-build" ] +on-chain-release-build = [ + "pallet-funding/on-chain-release-build", + "sp-api/disable-logging", +] development-settings = [ "shared-configuration/development-settings" ]