diff --git a/Cargo.lock b/Cargo.lock index 6b74d6534..48f0a31f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -446,6 +446,7 @@ dependencies = [ "frame-system-benchmarking", "frame-system-rpc-runtime-api", "frame-try-runtime", + "hex-literal", "kusama-runtime-constants", "log", "pallet-asset-conversion", diff --git a/integration-tests/Cargo.toml b/integration-tests/Cargo.toml index 69a8d18b7..91a3f5f38 100644 --- a/integration-tests/Cargo.toml +++ b/integration-tests/Cargo.toml @@ -140,5 +140,42 @@ std = [ "xcm/std", ] development-settings = [ "polimec-runtime/development-settings" ] -runtime-benchmarks = [] +runtime-benchmarks = [ + "asset-hub-polkadot-runtime/runtime-benchmarks", + "polkadot-runtime/runtime-benchmarks", + "penpal-runtime/runtime-benchmarks", + "pallet-democracy/runtime-benchmarks", + "pallet-dispenser/runtime-benchmarks", + "pallet-elections-phragmen/runtime-benchmarks", + "pallet-funding/runtime-benchmarks", + "pallet-linear-release/runtime-benchmarks", + "pallet-parachain-staking/runtime-benchmarks", + "polimec-receiver/runtime-benchmarks", + "polimec-common/runtime-benchmarks", + "polimec-common-test-utils/runtime-benchmarks", + "polimec-runtime/runtime-benchmarks", + "cumulus-primitives-core/runtime-benchmarks", + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "orml-oracle/runtime-benchmarks", + "pallet-assets/runtime-benchmarks", + "pallet-balances/runtime-benchmarks", + "pallet-collective/runtime-benchmarks", + "pallet-im-online/runtime-benchmarks", + "pallet-membership/runtime-benchmarks", + "pallet-message-queue/runtime-benchmarks", + "pallet-scheduler/runtime-benchmarks", + "pallet-staking/runtime-benchmarks", + "pallet-treasury/runtime-benchmarks", + "pallet-vesting/runtime-benchmarks", + "pallet-xcm/runtime-benchmarks", + "parachains-common/runtime-benchmarks", + "polkadot-parachain-primitives/runtime-benchmarks", + "polkadot-primitives/runtime-benchmarks", + "polkadot-runtime-parachains/runtime-benchmarks", + "polkadot-service/runtime-benchmarks", + "sp-runtime/runtime-benchmarks", + "xcm-builder/runtime-benchmarks", + "xcm-executor/runtime-benchmarks" +] diff --git a/pallets/funding/Cargo.toml b/pallets/funding/Cargo.toml index 86165bff7..b9bbf8ebf 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", @@ -113,6 +114,7 @@ runtime-benchmarks = [ "sp-runtime/runtime-benchmarks", "xcm-builder/runtime-benchmarks", "xcm-executor/runtime-benchmarks", + "on-slash-vesting/runtime-benchmarks" ] try-runtime = [ "frame-support/try-runtime", diff --git a/pallets/on-slash-vesting/Cargo.toml b/pallets/on-slash-vesting/Cargo.toml index ffb1e7745..fc668a065 100644 --- a/pallets/on-slash-vesting/Cargo.toml +++ b/pallets/on-slash-vesting/Cargo.toml @@ -29,14 +29,22 @@ 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", +] + +runtime-benchmarks = [ + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "pallet-balances/runtime-benchmarks", + "pallet-vesting/runtime-benchmarks", + "sp-runtime/runtime-benchmarks" +] diff --git a/pallets/on-slash-vesting/src/lib.rs b/pallets/on-slash-vesting/src/lib.rs index 9e9c17ea6..571f986a2 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,25 @@ 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_result = >::vest(T::RuntimeOrigin::signed(account.clone())); + debug_assert!(vest_result.is_ok()); } - 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/pallets/on-slash-vesting/src/test.rs b/pallets/on-slash-vesting/src/test.rs index e29830ffc..25947965e 100644 --- a/pallets/on-slash-vesting/src/test.rs +++ b/pallets/on-slash-vesting/src/test.rs @@ -23,7 +23,6 @@ fn one_schedule() { // Unlock 20 assert_ok!(PalletVesting::vest(RuntimeOrigin::signed(1))); assert_eq!(PalletBalances::usable_balance(1), 20); - dbg!(>::get(1)); // Slash 30 >::slash(&MockRuntimeHoldReason::Reason, &1u64, 30u128); @@ -59,8 +58,7 @@ fn multiple_schedules() { assert_ok!(>::hold(&MockRuntimeHoldReason::Reason, &1u64, 100u128)); assert_eq!(PalletBalances::usable_balance(1), 0); - // see account data - dbg!(PalletSystem::account(1).data); + PalletSystem::set_block_number(3); diff --git a/runtimes/polimec/Cargo.toml b/runtimes/polimec/Cargo.toml index bfe2d8b38..63c0b724d 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", @@ -227,6 +228,7 @@ runtime-benchmarks = [ "sp-runtime/runtime-benchmarks", "xcm-builder/runtime-benchmarks", "xcm-executor/runtime-benchmarks", + "on-slash-vesting/runtime-benchmarks" ] try-runtime = [ @@ -276,6 +278,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" ]