Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Allow council to slash treasury tip #7753

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 66 additions & 4 deletions frame/tips/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ fn setup_tip<T: Config>(r: u32, t: u32) ->
fn create_tips<T: Config>(t: u32, hash: T::Hash, value: BalanceOf<T>) ->
Result<(), &'static str>
{
println!(
"Benchmark-create_tips-entry",
);

for i in 0 .. t {
let caller = account("member", i, SEED);
ensure!(T::Tippers::contains(&caller), "caller is not a tipper");
Expand All @@ -76,6 +80,11 @@ fn create_tips<T: Config>(t: u32, hash: T::Hash, value: BalanceOf<T>) ->
open_tip.closes = Some(T::BlockNumber::zero());
}
});

println!(
"Benchmark-create_tips-exit",
);

Ok(())
}

Expand Down Expand Up @@ -147,6 +156,10 @@ benchmarks! {
close_tip {
let t in 1 .. MAX_TIPPERS;

println!(
"Benchmark-close-tip-entry",
);

// Make sure pot is funded
setup_pod_account::<T>();

Expand All @@ -164,14 +177,62 @@ benchmarks! {
let reason_hash = T::Hashing::hash(&reason[..]);
let hash = T::Hashing::hash_of(&(&reason_hash, &beneficiary));
ensure!(Tips::<T>::contains_key(hash), "tip does not exist");

println!(
"Benchmark-close-tip-hash-{:#?}",
hash,
);

create_tips::<T>(t, hash.clone(), value)?;

let caller = account("caller", t, SEED);
// Whitelist caller account from further DB operations.
let caller_key = frame_system::Account::<T>::hashed_key_for(&caller);
frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into());

println!(
"Benchmark-close-tip-exit",
);
}: _(RawOrigin::Signed(caller), hash)

slash_tip {
let t in 1 .. MAX_TIPPERS;

println!(
"Benchmark-slash-tip-entry",
);

// Make sure pot is funded
setup_pod_account::<T>();

// Set up a new tip proposal
let (member, reason, beneficiary, value) = setup_tip::<T>(0, t)?;
let value = T::Currency::minimum_balance().saturating_mul(100u32.into());
TipsMod::<T>::tip_new(
RawOrigin::Signed(member).into(),
reason.clone(),
beneficiary.clone(),
value
)?;

let reason_hash = T::Hashing::hash(&reason[..]);
let hash = T::Hashing::hash_of(&(&reason_hash, &beneficiary));
ensure!(Tips::<T>::contains_key(hash), "tip does not exist");

println!(
"Benchmark-slash-tip-hash-{:#?}",
hash,
);

TipsMod::<T>::slash_tip(
RawOrigin::Root.into(),
hash.clone(),
)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you calling slash tip here?

It is called below already when you do benchmarking

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just delete this and it should work fine.


// // Whitelist caller account from further DB operations.
// let caller_key = frame_system::Account::<T>::hashed_key_for(&caller);
// frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into());
}: _(RawOrigin::Root, hash)
}

#[cfg(test)]
Expand All @@ -183,11 +244,12 @@ mod tests {
#[test]
fn test_benchmarks() {
new_test_ext().execute_with(|| {
assert_ok!(test_benchmark_report_awesome::<Test>());
assert_ok!(test_benchmark_retract_tip::<Test>());
assert_ok!(test_benchmark_tip_new::<Test>());
assert_ok!(test_benchmark_tip::<Test>());
// assert_ok!(test_benchmark_report_awesome::<Test>());
// assert_ok!(test_benchmark_retract_tip::<Test>());
// assert_ok!(test_benchmark_tip_new::<Test>());
// assert_ok!(test_benchmark_tip::<Test>());
assert_ok!(test_benchmark_close_tip::<Test>());
assert_ok!(test_benchmark_slash_tip::<Test>());
});
}
}
64 changes: 62 additions & 2 deletions frame/tips/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ use frame_support::traits::{
use sp_runtime::{ Percent, RuntimeDebug, traits::{
Zero, AccountIdConversion, Hash, BadOrigin
}};

use frame_support::traits::{Contains, ContainsLengthBound};
use frame_support::traits::{
Contains, ContainsLengthBound,
OnUnbalanced, EnsureOrigin,
};
use codec::{Encode, Decode};
use frame_system::{self as system, ensure_signed};
pub use weights::WeightInfo;
Expand Down Expand Up @@ -170,6 +172,8 @@ decl_event!(
TipClosed(Hash, AccountId, Balance),
/// A tip suggestion has been retracted. \[tip_hash\]
TipRetracted(Hash),
/// A tip suggestion has been slashed. \[tip_hash, finder, deposit\]
TipSlashed(Hash, AccountId, Balance),
}
);

Expand Down Expand Up @@ -398,6 +402,13 @@ decl_module! {
/// # </weight>
#[weight = <T as Config>::WeightInfo::close_tip(T::Tippers::max_len() as u32)]
fn close_tip(origin, hash: T::Hash) {

if_std! {
println!(
"close_tip-Entry",
);
}

ensure_signed(origin)?;

let tip = Tips::<T>::get(hash).ok_or(Error::<T>::UnknownTip)?;
Expand All @@ -407,6 +418,55 @@ decl_module! {
Reasons::<T>::remove(&tip.reason);
Tips::<T>::remove(hash);
Self::payout_tip(hash, tip);

if_std! {
println!(
"close_tip-Exit",
);
}
}

/// Remove and slash an already-open tip.
///
/// May only be called from `T::RejectOrigin`.
///
/// As a result, API will slash the finder and the deposits are lost.
///
/// Emits `TipSlashed` if successful.
// #[weight = <T as Config>::WeightInfo::slash_tip(T::Tippers::max_len() as u32)]
#[weight = 10_000]
fn slash_tip(origin, hash: T::Hash) {

if_std! {
println!(
"slash_tip-Entry",
);
}

T::RejectOrigin::ensure_origin(origin)?;

if_std! {
println!(
"slash_tip-hash-{:#?}",
hash,
);
}

let tip = Tips::<T>::take(hash).ok_or(Error::<T>::UnknownTip)?;

if !tip.deposit.is_zero() {
let deposit = tip.deposit;
let imbalance = T::Currency::slash_reserved(&tip.finder, deposit).0;
T::OnSlash::on_unbalanced(imbalance);
}
Reasons::<T>::remove(&tip.reason);
Self::deposit_event(RawEvent::TipSlashed(hash, tip.finder, tip.deposit));

if_std! {
println!(
"slash_tip-exit",
);
}
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions frame/tips/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,30 @@ fn close_tip_works() {
});
}

#[test]
fn slash_tip_works() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
Balances::make_free_balance_be(&Treasury::account_id(), 101);
assert_eq!(Treasury::pot(), 100);
assert_eq!(Balances::reserved_balance(0), 0);
assert_eq!(Balances::free_balance(0), 100);
assert_ok!(TipsModTestInst::report_awesome(Origin::signed(0), b"awesome.dot".to_vec(), 3));
assert_eq!(Balances::reserved_balance(0), 12);
assert_eq!(Balances::free_balance(0), 88);
let h = tip_hash();
assert_eq!(last_event(), RawEvent::NewTip(h));
assert_noop!(
TipsModTestInst::slash_tip(Origin::signed(0), h.clone()),
BadOrigin,
);
assert_ok!(TipsModTestInst::slash_tip(Origin::root(), h.clone()));
assert_eq!(last_event(), RawEvent::TipSlashed(h, 0, 12));
assert_eq!(Balances::reserved_balance(0), 0);
assert_eq!(Balances::free_balance(0), 88);
});
}

#[test]
fn retract_tip_works() {
new_test_ext().execute_with(|| {
Expand Down