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

Commit

Permalink
Fixing conflict for PR 11596 (#11899)
Browse files Browse the repository at this point in the history
  • Loading branch information
hbulgarini authored Jul 24, 2022
1 parent e57c795 commit d95402b
Showing 1 changed file with 59 additions and 59 deletions.
118 changes: 59 additions & 59 deletions frame/tips/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ use frame_support::{
pub use pallet::*;
pub use weights::WeightInfo;

pub type BalanceOf<T> = pallet_treasury::BalanceOf<T>;
pub type NegativeImbalanceOf<T> = pallet_treasury::NegativeImbalanceOf<T>;
pub type BalanceOf<T, I = ()> = pallet_treasury::BalanceOf<T, I>;
pub type NegativeImbalanceOf<T, I = ()> = pallet_treasury::NegativeImbalanceOf<T, I>;

/// An open tipping "motion". Retains all details of a tip including information on the finder
/// and the members who have voted.
Expand Down Expand Up @@ -125,12 +125,12 @@ pub mod pallet {
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);
pub struct Pallet<T, I = ()>(_);

#[pallet::config]
pub trait Config: frame_system::Config + pallet_treasury::Config {
pub trait Config<I: 'static = ()>: frame_system::Config + pallet_treasury::Config<I> {
/// The overarching event type.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
type Event: From<Event<Self, I>> + IsType<<Self as frame_system::Config>::Event>;

/// Maximum acceptable reason length.
///
Expand All @@ -140,7 +140,7 @@ pub mod pallet {

/// The amount held on deposit per byte within the tip report reason or bounty description.
#[pallet::constant]
type DataDepositPerByte: Get<BalanceOf<Self>>;
type DataDepositPerByte: Get<BalanceOf<Self, I>>;

/// The period for which a tip remains open after is has achieved threshold tippers.
#[pallet::constant]
Expand All @@ -152,7 +152,7 @@ pub mod pallet {

/// The amount held on deposit for placing a tip report.
#[pallet::constant]
type TipReportDepositBase: Get<BalanceOf<Self>>;
type TipReportDepositBase: Get<BalanceOf<Self, I>>;

/// Origin from which tippers must come.
///
Expand All @@ -170,38 +170,38 @@ pub mod pallet {
/// guaranteed to be a secure hash.
#[pallet::storage]
#[pallet::getter(fn tips)]
pub type Tips<T: Config> = StorageMap<
pub type Tips<T: Config<I>, I: 'static = ()> = StorageMap<
_,
Twox64Concat,
T::Hash,
OpenTip<T::AccountId, BalanceOf<T>, T::BlockNumber, T::Hash, T::Tippers>,
OpenTip<T::AccountId, BalanceOf<T, I>, T::BlockNumber, T::Hash, T::Tippers>,
OptionQuery,
>;

/// Simple preimage lookup from the reason's hash to the original data. Again, has an
/// insecure enumerable hash since the key is guaranteed to be the result of a secure hash.
#[pallet::storage]
#[pallet::getter(fn reasons)]
pub type Reasons<T: Config> =
pub type Reasons<T: Config<I>, I: 'static = ()> =
StorageMap<_, Identity, T::Hash, BoundedVec<u8, T::MaximumReasonLength>, OptionQuery>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
pub enum Event<T: Config<I>, I: 'static = ()> {
/// A new tip suggestion has been opened.
NewTip { tip_hash: T::Hash },
/// A tip suggestion has reached threshold and is closing.
TipClosing { tip_hash: T::Hash },
/// A tip suggestion has been closed.
TipClosed { tip_hash: T::Hash, who: T::AccountId, payout: BalanceOf<T> },
TipClosed { tip_hash: T::Hash, who: T::AccountId, payout: BalanceOf<T, I> },
/// A tip suggestion has been retracted.
TipRetracted { tip_hash: T::Hash },
/// A tip suggestion has been slashed.
TipSlashed { tip_hash: T::Hash, finder: T::AccountId, deposit: BalanceOf<T> },
TipSlashed { tip_hash: T::Hash, finder: T::AccountId, deposit: BalanceOf<T, I> },
}

#[pallet::error]
pub enum Error<T> {
pub enum Error<T, I = ()> {
/// The reason given is just too big.
ReasonTooBig,
/// The tip was already found/started.
Expand All @@ -219,7 +219,7 @@ pub mod pallet {
}

#[pallet::call]
impl<T: Config> Pallet<T> {
impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Report something `reason` that deserves a tip and claim any eventual the finder's fee.
///
/// The dispatch origin for this call must be _Signed_.
Expand All @@ -239,7 +239,7 @@ pub mod pallet {
/// - DbReads: `Reasons`, `Tips`
/// - DbWrites: `Reasons`, `Tips`
/// # </weight>
#[pallet::weight(<T as Config>::WeightInfo::report_awesome(reason.len() as u32))]
#[pallet::weight(<T as Config<I>>::WeightInfo::report_awesome(reason.len() as u32))]
pub fn report_awesome(
origin: OriginFor<T>,
reason: Vec<u8>,
Expand All @@ -249,18 +249,18 @@ pub mod pallet {

// make reason bounded
let reason: BoundedVec<u8, _> =
reason.try_into().map_err(|_| Error::<T>::ReasonTooBig)?;
reason.try_into().map_err(|_| Error::<T, I>::ReasonTooBig)?;

let reason_hash = T::Hashing::hash(&reason[..]);
ensure!(!Reasons::<T>::contains_key(&reason_hash), Error::<T>::AlreadyKnown);
ensure!(!Reasons::<T, I>::contains_key(&reason_hash), Error::<T, I>::AlreadyKnown);
let hash = T::Hashing::hash_of(&(&reason_hash, &who));
ensure!(!Tips::<T>::contains_key(&hash), Error::<T>::AlreadyKnown);
ensure!(!Tips::<T, I>::contains_key(&hash), Error::<T, I>::AlreadyKnown);

let deposit = T::TipReportDepositBase::get() +
T::DataDepositPerByte::get() * (reason.len() as u32).into();
let deposit = T::TipReportDepositBase::get()
+ T::DataDepositPerByte::get() * (reason.len() as u32).into();
T::Currency::reserve(&finder, deposit)?;

Reasons::<T>::insert(&reason_hash, &reason);
Reasons::<T, I>::insert(&reason_hash, &reason);
let tip = OpenTip {
reason: reason_hash,
who,
Expand All @@ -270,7 +270,7 @@ pub mod pallet {
tips: BoundedVec::default(),
finders_fee: true,
};
Tips::<T>::insert(&hash, tip);
Tips::<T, I>::insert(&hash, tip);
Self::deposit_event(Event::NewTip { tip_hash: hash });
Ok(())
}
Expand All @@ -294,14 +294,14 @@ pub mod pallet {
/// - DbReads: `Tips`, `origin account`
/// - DbWrites: `Reasons`, `Tips`, `origin account`
/// # </weight>
#[pallet::weight(<T as Config>::WeightInfo::retract_tip())]
#[pallet::weight(<T as Config<I>>::WeightInfo::retract_tip())]
pub fn retract_tip(origin: OriginFor<T>, hash: T::Hash) -> DispatchResult {
let who = ensure_signed(origin)?;
let tip = Tips::<T>::get(&hash).ok_or(Error::<T>::UnknownTip)?;
ensure!(tip.finder == who, Error::<T>::NotFinder);
let tip = Tips::<T, I>::get(&hash).ok_or(Error::<T, I>::UnknownTip)?;
ensure!(tip.finder == who, Error::<T, I>::NotFinder);

Reasons::<T>::remove(&tip.reason);
Tips::<T>::remove(&hash);
Reasons::<T, I>::remove(&tip.reason);
Tips::<T, I>::remove(&hash);
if !tip.deposit.is_zero() {
let err_amount = T::Currency::unreserve(&who, tip.deposit);
debug_assert!(err_amount.is_zero());
Expand Down Expand Up @@ -332,28 +332,28 @@ pub mod pallet {
/// - DbReads: `Tippers`, `Reasons`
/// - DbWrites: `Reasons`, `Tips`
/// # </weight>
#[pallet::weight(<T as Config>::WeightInfo::tip_new(reason.len() as u32, T::Tippers::max_len() as u32))]
#[pallet::weight(<T as Config<I>>::WeightInfo::tip_new(reason.len() as u32, T::Tippers::max_len() as u32))]
pub fn tip_new(
origin: OriginFor<T>,
reason: Vec<u8>,
who: T::AccountId,
#[pallet::compact] tip_value: BalanceOf<T>,
#[pallet::compact] tip_value: BalanceOf<T, I>,
) -> DispatchResult {
// make reason bounded
let reason: BoundedVec<u8, _> =
reason.try_into().map_err(|_| Error::<T>::ReasonTooBig)?;
reason.try_into().map_err(|_| Error::<T, I>::ReasonTooBig)?;

let tipper = ensure_signed(origin)?;
ensure!(T::Tippers::contains(&tipper), BadOrigin);
let reason_hash = T::Hashing::hash(&reason[..]);
ensure!(!Reasons::<T>::contains_key(&reason_hash), Error::<T>::AlreadyKnown);
ensure!(!Reasons::<T, I>::contains_key(&reason_hash), Error::<T, I>::AlreadyKnown);
let hash = T::Hashing::hash_of(&(&reason_hash, &who));

Reasons::<T>::insert(&reason_hash, &reason);
Reasons::<T, I>::insert(&reason_hash, &reason);
Self::deposit_event(Event::NewTip { tip_hash: hash });
let tips = vec![(tipper.clone(), tip_value)]
.try_into()
.map_err(|_| Error::<T>::TipsTooBig)?;
.map_err(|_| Error::<T, I>::TipsTooBig)?;
let tip = OpenTip {
reason: reason_hash,
who,
Expand All @@ -363,7 +363,7 @@ pub mod pallet {
tips,
finders_fee: false,
};
Tips::<T>::insert(&hash, tip);
Tips::<T, I>::insert(&hash, tip);
Ok(())
}

Expand Down Expand Up @@ -391,20 +391,20 @@ pub mod pallet {
/// - DbReads: `Tippers`, `Tips`
/// - DbWrites: `Tips`
/// # </weight>
#[pallet::weight(<T as Config>::WeightInfo::tip(T::Tippers::max_len() as u32))]
#[pallet::weight(<T as Config<I>>::WeightInfo::tip(T::Tippers::max_len() as u32))]
pub fn tip(
origin: OriginFor<T>,
hash: T::Hash,
#[pallet::compact] tip_value: BalanceOf<T>,
#[pallet::compact] tip_value: BalanceOf<T, I>,
) -> DispatchResult {
let tipper = ensure_signed(origin)?;
ensure!(T::Tippers::contains(&tipper), BadOrigin);

let mut tip = Tips::<T>::get(hash).ok_or(Error::<T>::UnknownTip)?;
let mut tip = Tips::<T, I>::get(hash).ok_or(Error::<T, I>::UnknownTip)?;
if Self::insert_tip_and_check_closing(&mut tip, tipper, tip_value)? {
Self::deposit_event(Event::TipClosing { tip_hash: hash });
}
Tips::<T>::insert(&hash, tip);
Tips::<T, I>::insert(&hash, tip);
Ok(())
}

Expand All @@ -424,16 +424,16 @@ pub mod pallet {
/// - DbReads: `Tips`, `Tippers`, `tip finder`
/// - DbWrites: `Reasons`, `Tips`, `Tippers`, `tip finder`
/// # </weight>
#[pallet::weight(<T as Config>::WeightInfo::close_tip(T::Tippers::max_len() as u32))]
#[pallet::weight(<T as Config<I>>::WeightInfo::close_tip(T::Tippers::max_len() as u32))]
pub fn close_tip(origin: OriginFor<T>, hash: T::Hash) -> DispatchResult {
ensure_signed(origin)?;

let tip = Tips::<T>::get(hash).ok_or(Error::<T>::UnknownTip)?;
let n = tip.closes.as_ref().ok_or(Error::<T>::StillOpen)?;
ensure!(frame_system::Pallet::<T>::block_number() >= *n, Error::<T>::Premature);
let tip = Tips::<T, I>::get(hash).ok_or(Error::<T, I>::UnknownTip)?;
let n = tip.closes.as_ref().ok_or(Error::<T, I>::StillOpen)?;
ensure!(frame_system::Pallet::<T>::block_number() >= *n, Error::<T, I>::Premature);
// closed.
Reasons::<T>::remove(&tip.reason);
Tips::<T>::remove(hash);
Reasons::<T, I>::remove(&tip.reason);
Tips::<T, I>::remove(hash);
Self::payout_tip(hash, tip);
Ok(())
}
Expand All @@ -450,17 +450,17 @@ pub mod pallet {
/// `T` is charged as upper bound given by `ContainsLengthBound`.
/// The actual cost depends on the implementation of `T::Tippers`.
/// # </weight>
#[pallet::weight(<T as Config>::WeightInfo::slash_tip(T::Tippers::max_len() as u32))]
#[pallet::weight(<T as Config<I>>::WeightInfo::slash_tip(T::Tippers::max_len() as u32))]
pub fn slash_tip(origin: OriginFor<T>, hash: T::Hash) -> DispatchResult {
T::RejectOrigin::ensure_origin(origin)?;

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

if !tip.deposit.is_zero() {
let imbalance = T::Currency::slash_reserved(&tip.finder, tip.deposit).0;
T::OnSlash::on_unbalanced(imbalance);
}
Reasons::<T>::remove(&tip.reason);
Reasons::<T, I>::remove(&tip.reason);
Self::deposit_event(Event::TipSlashed {
tip_hash: hash,
finder: tip.finder,
Expand All @@ -471,7 +471,7 @@ pub mod pallet {
}
}

impl<T: Config> Pallet<T> {
impl<T: Config<I>, I: 'static> Pallet<T, I> {
// Add public immutables and private mutables.

/// The account ID of the treasury pot.
Expand All @@ -487,16 +487,16 @@ impl<T: Config> Pallet<T> {
///
/// `O(T)` and one storage access.
fn insert_tip_and_check_closing(
tip: &mut OpenTip<T::AccountId, BalanceOf<T>, T::BlockNumber, T::Hash, T::Tippers>,
tip: &mut OpenTip<T::AccountId, BalanceOf<T, I>, T::BlockNumber, T::Hash, T::Tippers>,
tipper: T::AccountId,
tip_value: BalanceOf<T>,
tip_value: BalanceOf<T, I>,
) -> Result<bool, sp_runtime::DispatchError> {
match tip.tips.binary_search_by_key(&&tipper, |x| &x.0) {
Ok(pos) => tip.tips[pos] = (tipper, tip_value),
Err(pos) => tip
.tips
.try_insert(pos, (tipper, tip_value))
.map_err(|_| Error::<T>::TipsTooBig)?,
.map_err(|_| Error::<T, I>::TipsTooBig)?,
}
Self::retain_active_tips(&mut tip.tips);
let threshold = (T::Tippers::count() + 1) / 2;
Expand All @@ -510,7 +510,7 @@ impl<T: Config> Pallet<T> {

/// Remove any non-members of `Tippers` from a `tips` vector. `O(T)`.
fn retain_active_tips(
tips: &mut BoundedVec<(T::AccountId, BalanceOf<T>), LengthBoundMaximum<T::Tippers>>,
tips: &mut BoundedVec<(T::AccountId, BalanceOf<T, I>), LengthBoundMaximum<T::Tippers>>,
) {
let members = T::Tippers::sorted_members();
let mut members_iter = members.iter();
Expand All @@ -522,9 +522,9 @@ impl<T: Config> Pallet<T> {
Some(m) => {
member = members_iter.next();
if m < a {
continue
continue;
} else {
break true
break true;
}
},
}
Expand All @@ -537,14 +537,14 @@ impl<T: Config> Pallet<T> {
/// Plus `O(T)` (`T` is Tippers length).
fn payout_tip(
hash: T::Hash,
tip: OpenTip<T::AccountId, BalanceOf<T>, T::BlockNumber, T::Hash, T::Tippers>,
tip: OpenTip<T::AccountId, BalanceOf<T, I>, T::BlockNumber, T::Hash, T::Tippers>,
) {
let mut tips = tip.tips;
Self::retain_active_tips(&mut tips);
tips.sort_by_key(|i| i.1);

let treasury = Self::account_id();
let max_payout = pallet_treasury::Pallet::<T>::pot();
let max_payout = pallet_treasury::Pallet::<T, I>::pot();

let mut payout = tips[tips.len() / 2].1.min(max_payout);
if !tip.deposit.is_zero() {
Expand Down Expand Up @@ -599,7 +599,7 @@ impl<T: Config> Pallet<T> {

for (hash, old_tip) in storage_key_iter::<
T::Hash,
OldOpenTip<T::AccountId, BalanceOf<T>, T::BlockNumber, T::Hash>,
OldOpenTip<T::AccountId, BalanceOf<T, I>, T::BlockNumber, T::Hash>,
Twox64Concat,
>(module, item)
.drain()
Expand All @@ -617,7 +617,7 @@ impl<T: Config> Pallet<T> {
tips: BoundedVec::try_from(old_tip.tips).map_err(|_| ())?,
finders_fee,
};
Tips::<T>::insert(hash, new_tip)
Tips::<T, I>::insert(hash, new_tip)
}

Ok(())
Expand Down

0 comments on commit d95402b

Please sign in to comment.