Skip to content

Commit

Permalink
Zepter
Browse files Browse the repository at this point in the history
  • Loading branch information
lrazovic authored and JuaniRios committed Sep 12, 2024
1 parent 10a6907 commit cd5caa4
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 39 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

39 changes: 38 additions & 1 deletion integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]

2 changes: 2 additions & 0 deletions pallets/funding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
30 changes: 19 additions & 11 deletions pallets/on-slash-vesting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
"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"
]
42 changes: 19 additions & 23 deletions pallets/on-slash-vesting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AccountId, Balance: Clone> {
fn on_slash(account: &AccountId, amount: Balance);
Expand All @@ -33,27 +31,25 @@ where
T::Currency: Currency<AccountIdOf<T>, Balance = u128>,
{
fn on_slash(account: &AccountIdOf<T>, slashed_amount: u128) {
let Some(vesting_schedules) = <Vesting<T>>::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::<T::BlockNumberToBalance>(now).saturating_sub(slashed_amount);
let start_block: u128 = T::BlockNumberToBalance::convert(now);
let end_block: u128 = schedule.ending_block_as_balance::<T::BlockNumberToBalance>();
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) = <Vesting<T>>::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::<T::BlockNumberToBalance>(now).saturating_sub(slashed_amount);
let start_block = T::BlockNumberToBalance::convert(now);
let end_block = schedule.ending_block_as_balance::<T::BlockNumberToBalance>();
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());
}
}
<Vesting<T>>::set(account, Some(new_vesting_schedules));
let vest_result = <pallet_vesting::Pallet<T>>::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
};
<Vesting<T>>::set(account, Some(new_vesting_schedules));
let vest_result = <pallet_vesting::Pallet<T>>::vest(T::RuntimeOrigin::signed(account.clone()));
debug_assert!(vest_result.is_ok());
}
}
4 changes: 1 addition & 3 deletions pallets/on-slash-vesting/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ fn one_schedule() {
// Unlock 20
assert_ok!(PalletVesting::vest(RuntimeOrigin::signed(1)));
assert_eq!(PalletBalances::usable_balance(1), 20);
dbg!(<pallet_vesting::Vesting<TestRuntime>>::get(1));

// Slash 30
<PalletBalances as BalancedHold<u64>>::slash(&MockRuntimeHoldReason::Reason, &1u64, 30u128);
Expand Down Expand Up @@ -59,8 +58,7 @@ fn multiple_schedules() {

assert_ok!(<PalletBalances as MutateHold<u64>>::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);

Expand Down
7 changes: 6 additions & 1 deletion runtimes/polimec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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" ]

0 comments on commit cd5caa4

Please sign in to comment.