Skip to content

Commit

Permalink
✨ Slashing behavior for pallet vesting (#396)
Browse files Browse the repository at this point in the history
## What?
- Reduce vesting schedules of pallet-vesting after a slash is made when an evaluation is settled

## Why?
- A user could have negative transferable balance if they had some tokens locked for vesting and then got slashed.

## How?
- Semi-generic solution which should be easily adapted to the Polkadot SDK. PR is [here](paritytech/polkadot-sdk#5623).
- pallet-funding (in the future pallet-balances) accepts a tuple of items that implement a trait called on_slash.
- pallet funding calls this after slashing the evaluator (we don't use the slash interface so we call the trait directly. In the future this trait should also be called when using the slash function)
- We implement on pallet vesting the trait where we see how many tokens should be released at the moment of slashing, and then apply the slash on the remaining frozen amount. We recalculate the per_block amount to keep the same end block 

## Testing?
- 2 tests in the new crate on-slash-vesting
- 1 integration test

## Anything Else?
- For now the trait for slashing needs to be in the same crate we implement it since we can't impl a foreign trait on a foreign crate.
- Soon we should submit a PR to polkadot-sdk where we submit this new slash interface on the tokens::fungible trait, and also add our vesting impl directly inside pallet-vesting.
  • Loading branch information
JuaniRios committed Sep 12, 2024
1 parent 49c850a commit b0681c3
Show file tree
Hide file tree
Showing 18 changed files with 678 additions and 19 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pallet-sandbox = { path = "pallets/sandbox", default-features = false }
pallet-parachain-staking = { path = "pallets/parachain-staking", default-features = false }
pallet-linear-release = { path = "pallets/linear-release", default-features = false }
polimec-receiver = { path = "pallets/polimec-receiver", default-features = false }
on-slash-vesting = { path = "pallets/on-slash-vesting", default-features = false }

# Internal macros
macros = { path = "macros" }
Expand Down Expand Up @@ -109,6 +110,7 @@ color-print = "0.3.5"
xcm-emulator = { version = "0.12.0", default-features = false }

# Substrate (with default disabled)
impl-trait-for-tuples = { version = "0.2.2", default-features = false }
frame-benchmarking = { version = "35.0.0", default-features = false }
frame-benchmarking-cli = { version = "39.0.0" }
frame-executive = { version = "35.0.0", default-features = false }
Expand Down
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"
]

74 changes: 68 additions & 6 deletions integration-tests/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,69 @@ fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public
TPublic::Pair::from_string(&format!("//{}", seed), None).expect("static values are valid; qed").public()
}

pub struct Prices {
pub dot: FixedU128,
pub usdc: FixedU128,
pub usdt: FixedU128,
pub plmc: FixedU128,
}

// PricesBuilder for optional fields before building Prices
#[derive(Clone, Copy)]
pub struct PricesBuilder {
dot: Option<FixedU128>,
usdc: Option<FixedU128>,
usdt: Option<FixedU128>,
plmc: Option<FixedU128>,
}

impl PricesBuilder {
// Initialize a new builder with None for each field
pub fn new() -> Self {
Self { dot: None, usdc: None, usdt: None, plmc: None }
}

pub fn default() -> Prices {
Prices {
dot: FixedU128::from_rational(69, 1),
usdc: FixedU128::from_rational(1, 1),
usdt: FixedU128::from_rational(1, 1),
plmc: FixedU128::from_rational(840, 100),
}
}

// Setters that take FixedU128 and return &mut self for chaining
pub fn dot(&mut self, price: FixedU128) -> &mut Self {
self.dot = Some(price);
self
}

pub fn usdc(&mut self, price: FixedU128) -> &mut Self {
self.usdc = Some(price);
self
}

pub fn usdt(&mut self, price: FixedU128) -> &mut Self {
self.usdt = Some(price);
self
}

pub fn plmc(&mut self, price: FixedU128) -> &mut Self {
self.plmc = Some(price);
self
}

// Build Prices using provided values or default values
pub fn build(self) -> Prices {
Prices {
dot: self.dot.unwrap_or(FixedU128::from_rational(69, 1)), // Default DOT price
usdc: self.usdc.unwrap_or(FixedU128::from_rational(1, 1)), // Default USDC price
usdt: self.usdt.unwrap_or(FixedU128::from_rational(1, 1)), // Default USDT price
plmc: self.plmc.unwrap_or(FixedU128::from_rational(840, 100)), // Default PLMC price
}
}
}

pub mod accounts {
use super::*;
pub const ALICE: &str = "Alice";
Expand Down Expand Up @@ -377,13 +440,12 @@ pub mod polimec {
const GENESIS_PARACHAIN_BOND_RESERVE_PERCENT: Percent = Percent::from_percent(0);
const GENESIS_NUM_SELECTED_CANDIDATES: u32 = 5;

#[allow(unused)]
pub fn set_prices() {
pub fn set_prices(prices: Prices) {
PolimecNet::execute_with(|| {
let dot = (AcceptedFundingAsset::DOT.id(), FixedU128::from_rational(69, 1));
let usdc = (AcceptedFundingAsset::USDC.id(), FixedU128::from_rational(1, 1));
let usdt = (AcceptedFundingAsset::USDT.id(), FixedU128::from_rational(1, 1));
let plmc = (pallet_funding::PLMC_FOREIGN_ID, FixedU128::from_rational(840, 100));
let dot = (AcceptedFundingAsset::DOT.id(), prices.dot);
let usdc = (AcceptedFundingAsset::USDC.id(), prices.usdc);
let usdt = (AcceptedFundingAsset::USDT.id(), prices.usdt);
let plmc = (pallet_funding::PLMC_FOREIGN_ID, prices.plmc);

let values: BoundedVec<(u32, FixedU128), <PolimecRuntime as orml_oracle::Config>::MaxFeedValues> =
vec![dot, usdc, usdt, plmc].try_into().expect("benchmarks can panic");
Expand Down
6 changes: 3 additions & 3 deletions integration-tests/src/tests/ct_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::*;
use crate::{constants::PricesBuilder, *};
use frame_support::traits::{fungible::Mutate, fungibles::Inspect};
use itertools::Itertools;
use pallet_funding::{assert_close_enough, types::*, ProjectId, WeightInfo};
Expand Down Expand Up @@ -190,7 +190,7 @@ fn create_settled_project() -> (ProjectId, Vec<AccountId>) {

#[test]
fn full_pallet_migration_test() {
polimec::set_prices();
polimec::set_prices(PricesBuilder::default());
let (project_id, participants) = create_settled_project();
let _project_status =
PolimecNet::execute_with(|| pallet_funding::ProjectsDetails::<PolimecRuntime>::get(project_id).unwrap().status);
Expand Down Expand Up @@ -296,7 +296,7 @@ fn create_project_with_unsettled_participation(participation_type: Participation

#[test]
fn cannot_start_pallet_migration_with_unsettled_participations() {
polimec::set_prices();
polimec::set_prices(PricesBuilder::default());

let tup_1 = create_project_with_unsettled_participation(ParticipationType::Evaluation);
let tup_2 = create_project_with_unsettled_participation(ParticipationType::Bid);
Expand Down
16 changes: 8 additions & 8 deletions integration-tests/src/tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{tests::defaults::*, *};
use crate::{constants::PricesBuilder, tests::defaults::*, *};
use frame_support::{
traits::{
fungible::Mutate,
Expand Down Expand Up @@ -271,7 +271,7 @@ fn excel_ct_amounts() -> UserToCTBalance {

#[test]
fn evaluation_round_completed() {
polimec::set_prices();
polimec::set_prices(PricesBuilder::default());

let mut inst = IntegrationInstantiator::new(None);

Expand All @@ -286,7 +286,7 @@ fn evaluation_round_completed() {

#[test]
fn auction_round_completed() {
polimec::set_prices();
polimec::set_prices(PricesBuilder::default());

let mut inst = IntegrationInstantiator::new(None);

Expand Down Expand Up @@ -326,7 +326,7 @@ fn auction_round_completed() {

#[test]
fn community_round_completed() {
polimec::set_prices();
polimec::set_prices(PricesBuilder::default());

let mut inst = IntegrationInstantiator::new(None);

Expand All @@ -353,7 +353,7 @@ fn community_round_completed() {

#[test]
fn remainder_round_completed() {
polimec::set_prices();
polimec::set_prices(PricesBuilder::default());

let mut inst = IntegrationInstantiator::new(None);

Expand Down Expand Up @@ -386,7 +386,7 @@ fn remainder_round_completed() {

#[test]
fn funds_raised() {
polimec::set_prices();
polimec::set_prices(PricesBuilder::default());

let mut inst = IntegrationInstantiator::new(None);

Expand Down Expand Up @@ -418,7 +418,7 @@ fn funds_raised() {

#[test]
fn ct_minted() {
polimec::set_prices();
polimec::set_prices(PricesBuilder::default());

let mut inst = IntegrationInstantiator::new(None);

Expand All @@ -445,7 +445,7 @@ fn ct_minted() {

#[test]
fn ct_migrated() {
polimec::set_prices();
polimec::set_prices(PricesBuilder::default());

let mut inst = IntegrationInstantiator::new(None);

Expand Down
Loading

0 comments on commit b0681c3

Please sign in to comment.