Remove the Copy bound on AssetId (#14158)

* Remove the `Copy` bound on `AssetId`

* Also relax the `Copy` bound in the assets pallet

* Fix warnings on the newest nightly Rust

* Remove some unnecessary `clone()`s

* Try to satisfy clippy

* Remove some more unnecessary `clone()`s

* Add more `.clone()`s for newly merged code

* Also add `clone()`s in the benchmarks

---------

Co-authored-by: parity-processbot <>
This commit is contained in:
Koute
2023-05-23 20:34:04 +09:00
committed by GitHub
parent 06865d4c08
commit 194c9edd4a
13 changed files with 223 additions and 187 deletions
@@ -98,7 +98,7 @@ pub trait Inspect<AccountId>: super::Inspect<AccountId> {
who: &AccountId,
amount: Self::Balance,
) -> DispatchResult {
ensure!(Self::hold_available(asset, reason, who), TokenError::CannotCreateHold);
ensure!(Self::hold_available(asset.clone(), reason, who), TokenError::CannotCreateHold);
ensure!(
amount <= Self::reducible_balance(asset, who, Protect, Force),
TokenError::FundsUnavailable
@@ -173,7 +173,7 @@ pub trait Unbalanced<AccountId>: Inspect<AccountId> {
mut amount: Self::Balance,
precision: Precision,
) -> Result<Self::Balance, DispatchError> {
let old_balance = Self::balance_on_hold(asset, reason, who);
let old_balance = Self::balance_on_hold(asset.clone(), reason, who);
if let BestEffort = precision {
amount = amount.min(old_balance);
}
@@ -193,7 +193,7 @@ pub trait Unbalanced<AccountId>: Inspect<AccountId> {
amount: Self::Balance,
precision: Precision,
) -> Result<Self::Balance, DispatchError> {
let old_balance = Self::balance_on_hold(asset, reason, who);
let old_balance = Self::balance_on_hold(asset.clone(), reason, who);
let new_balance = if let BestEffort = precision {
old_balance.saturating_add(amount)
} else {
@@ -221,11 +221,13 @@ pub trait Balanced<AccountId>: super::Balanced<AccountId> + Unbalanced<AccountId
who: &AccountId,
amount: Self::Balance,
) -> (Credit<AccountId, Self>, Self::Balance) {
let decrease = Self::decrease_balance_on_hold(asset, reason, who, amount, BestEffort)
.unwrap_or(Default::default());
let decrease =
Self::decrease_balance_on_hold(asset.clone(), reason, who, amount, BestEffort)
.unwrap_or(Default::default());
let credit =
Imbalance::<Self::AssetId, Self::Balance, Self::OnDropCredit, Self::OnDropDebt>::new(
asset, decrease,
asset.clone(),
decrease,
);
Self::done_slash(asset, reason, who, decrease);
(credit, amount.saturating_sub(decrease))
@@ -255,10 +257,10 @@ pub trait Mutate<AccountId>:
// NOTE: This doesn't change the total balance of the account so there's no need to
// check liquidity.
Self::ensure_can_hold(asset, reason, who, amount)?;
Self::ensure_can_hold(asset.clone(), reason, who, amount)?;
// Should be infallible now, but we proceed softly anyway.
Self::decrease_balance(asset, who, amount, Exact, Protect, Force)?;
Self::increase_balance_on_hold(asset, reason, who, amount, BestEffort)?;
Self::decrease_balance(asset.clone(), who, amount, Exact, Protect, Force)?;
Self::increase_balance_on_hold(asset.clone(), reason, who, amount, BestEffort)?;
Self::done_hold(asset, reason, who, amount);
Ok(())
}
@@ -281,13 +283,16 @@ pub trait Mutate<AccountId>:
// We want to make sure we can deposit the amount in advance. If we can't then something is
// very wrong.
ensure!(Self::can_deposit(asset, who, amount, Extant) == Success, TokenError::CannotCreate);
ensure!(
Self::can_deposit(asset.clone(), who, amount, Extant) == Success,
TokenError::CannotCreate
);
// Get the amount we can actually take from the hold. This might be less than what we want
// if we're only doing a best-effort.
let amount = Self::decrease_balance_on_hold(asset, reason, who, amount, precision)?;
let amount = Self::decrease_balance_on_hold(asset.clone(), reason, who, amount, precision)?;
// Increase the main balance by what we took. We always do a best-effort here because we
// already checked that we can deposit before.
let actual = Self::increase_balance(asset, who, amount, BestEffort)?;
let actual = Self::increase_balance(asset.clone(), who, amount, BestEffort)?;
Self::done_release(asset, reason, who, actual);
Ok(actual)
}
@@ -310,14 +315,17 @@ pub trait Mutate<AccountId>:
force: Fortitude,
) -> Result<Self::Balance, DispatchError> {
// We must check total-balance requirements if `!force`.
let liquid = Self::reducible_total_balance_on_hold(asset, who, force);
let liquid = Self::reducible_total_balance_on_hold(asset.clone(), who, force);
if let BestEffort = precision {
amount = amount.min(liquid);
} else {
ensure!(amount <= liquid, TokenError::Frozen);
}
let amount = Self::decrease_balance_on_hold(asset, reason, who, amount, precision)?;
Self::set_total_issuance(asset, Self::total_issuance(asset).saturating_sub(amount));
let amount = Self::decrease_balance_on_hold(asset.clone(), reason, who, amount, precision)?;
Self::set_total_issuance(
asset.clone(),
Self::total_issuance(asset.clone()).saturating_sub(amount),
);
Self::done_burn_held(asset, reason, who, amount);
Ok(amount)
}
@@ -348,8 +356,8 @@ pub trait Mutate<AccountId>:
force: Fortitude,
) -> Result<Self::Balance, DispatchError> {
// We must check total-balance requirements if `!force`.
let have = Self::balance_on_hold(asset, reason, source);
let liquid = Self::reducible_total_balance_on_hold(asset, source, force);
let have = Self::balance_on_hold(asset.clone(), reason, source);
let liquid = Self::reducible_total_balance_on_hold(asset.clone(), source, force);
if let BestEffort = precision {
amount = amount.min(liquid).min(have);
} else {
@@ -360,19 +368,20 @@ pub trait Mutate<AccountId>:
// We want to make sure we can deposit the amount in advance. If we can't then something is
// very wrong.
ensure!(
Self::can_deposit(asset, dest, amount, Extant) == Success,
Self::can_deposit(asset.clone(), dest, amount, Extant) == Success,
TokenError::CannotCreate
);
ensure!(
mode == Free || Self::hold_available(asset, reason, dest),
mode == Free || Self::hold_available(asset.clone(), reason, dest),
TokenError::CannotCreateHold
);
let amount = Self::decrease_balance_on_hold(asset, reason, source, amount, precision)?;
let amount =
Self::decrease_balance_on_hold(asset.clone(), reason, source, amount, precision)?;
let actual = if mode == OnHold {
Self::increase_balance_on_hold(asset, reason, dest, amount, precision)?
Self::increase_balance_on_hold(asset.clone(), reason, dest, amount, precision)?
} else {
Self::increase_balance(asset, dest, amount, precision)?
Self::increase_balance(asset.clone(), dest, amount, precision)?
};
Self::done_transfer_on_hold(asset, reason, source, dest, actual);
Ok(actual)
@@ -405,14 +414,14 @@ pub trait Mutate<AccountId>:
expendability: Preservation,
force: Fortitude,
) -> Result<Self::Balance, DispatchError> {
ensure!(Self::hold_available(asset, reason, dest), TokenError::CannotCreateHold);
ensure!(Self::hold_available(asset.clone(), reason, dest), TokenError::CannotCreateHold);
ensure!(
Self::can_deposit(asset, dest, amount, Extant) == Success,
Self::can_deposit(asset.clone(), dest, amount, Extant) == Success,
TokenError::CannotCreate
);
let actual =
Self::decrease_balance(asset, source, amount, precision, expendability, force)?;
Self::increase_balance_on_hold(asset, reason, dest, actual, precision)?;
Self::decrease_balance(asset.clone(), source, amount, precision, expendability, force)?;
Self::increase_balance_on_hold(asset.clone(), reason, dest, actual, precision)?;
Self::done_transfer_on_hold(asset, reason, source, dest, actual);
Ok(actual)
}
@@ -59,7 +59,7 @@ impl<
{
fn drop(&mut self) {
if !self.amount.is_zero() {
OnDrop::handle(self.asset, self.amount)
OnDrop::handle(self.asset.clone(), self.amount)
}
}
}
@@ -104,9 +104,9 @@ impl<
pub fn split(self, amount: B) -> (Self, Self) {
let first = self.amount.min(amount);
let second = self.amount - first;
let asset = self.asset;
let asset = self.asset.clone();
sp_std::mem::forget(self);
(Imbalance::new(asset, first), Imbalance::new(asset, second))
(Imbalance::new(asset.clone(), first), Imbalance::new(asset, second))
}
pub fn merge(mut self, other: Self) -> Result<Self, (Self, Self)> {
if self.asset == other.asset {
@@ -135,7 +135,7 @@ impl<
> {
if self.asset == other.asset {
let (a, b) = (self.amount, other.amount);
let asset = self.asset;
let asset = self.asset.clone();
sp_std::mem::forget((self, other));
if a == b {
@@ -154,7 +154,7 @@ impl<
}
pub fn asset(&self) -> A {
self.asset
self.asset.clone()
}
}
@@ -146,7 +146,7 @@ pub trait Unbalanced<AccountId>: Inspect<AccountId> {
/// This should not be reimplemented.
fn handle_raw_dust(asset: Self::AssetId, amount: Self::Balance) {
Self::handle_dust(Dust(
asset,
asset.clone(),
amount.min(Self::minimum_balance(asset).saturating_sub(One::one())),
))
}
@@ -193,13 +193,13 @@ pub trait Unbalanced<AccountId>: Inspect<AccountId> {
preservation: Preservation,
force: Fortitude,
) -> Result<Self::Balance, DispatchError> {
let old_balance = Self::balance(asset, who);
let free = Self::reducible_balance(asset, who, preservation, force);
let old_balance = Self::balance(asset.clone(), who);
let free = Self::reducible_balance(asset.clone(), who, preservation, force);
if let BestEffort = precision {
amount = amount.min(free);
}
let new_balance = old_balance.checked_sub(&amount).ok_or(TokenError::FundsUnavailable)?;
if let Some(dust) = Self::write_balance(asset, who, new_balance)? {
if let Some(dust) = Self::write_balance(asset.clone(), who, new_balance)? {
Self::handle_dust(Dust(asset, dust));
}
Ok(old_balance.saturating_sub(new_balance))
@@ -217,13 +217,13 @@ pub trait Unbalanced<AccountId>: Inspect<AccountId> {
amount: Self::Balance,
precision: Precision,
) -> Result<Self::Balance, DispatchError> {
let old_balance = Self::balance(asset, who);
let old_balance = Self::balance(asset.clone(), who);
let new_balance = if let BestEffort = precision {
old_balance.saturating_add(amount)
} else {
old_balance.checked_add(&amount).ok_or(ArithmeticError::Overflow)?
};
if new_balance < Self::minimum_balance(asset) {
if new_balance < Self::minimum_balance(asset.clone()) {
// Attempt to increase from 0 to below minimum -> stays at zero.
if let BestEffort = precision {
Ok(Self::Balance::default())
@@ -234,7 +234,7 @@ pub trait Unbalanced<AccountId>: Inspect<AccountId> {
if new_balance == old_balance {
Ok(Self::Balance::default())
} else {
if let Some(dust) = Self::write_balance(asset, who, new_balance)? {
if let Some(dust) = Self::write_balance(asset.clone(), who, new_balance)? {
Self::handle_dust(Dust(asset, dust));
}
Ok(new_balance.saturating_sub(old_balance))
@@ -258,11 +258,14 @@ pub trait Mutate<AccountId>: Inspect<AccountId> + Unbalanced<AccountId> {
who: &AccountId,
amount: Self::Balance,
) -> Result<Self::Balance, DispatchError> {
Self::total_issuance(asset)
Self::total_issuance(asset.clone())
.checked_add(&amount)
.ok_or(ArithmeticError::Overflow)?;
let actual = Self::increase_balance(asset, who, amount, Exact)?;
Self::set_total_issuance(asset, Self::total_issuance(asset).saturating_add(actual));
let actual = Self::increase_balance(asset.clone(), who, amount, Exact)?;
Self::set_total_issuance(
asset.clone(),
Self::total_issuance(asset.clone()).saturating_add(actual),
);
Self::done_mint_into(asset, who, amount);
Ok(actual)
}
@@ -277,13 +280,17 @@ pub trait Mutate<AccountId>: Inspect<AccountId> + Unbalanced<AccountId> {
precision: Precision,
force: Fortitude,
) -> Result<Self::Balance, DispatchError> {
let actual = Self::reducible_balance(asset, who, Expendable, force).min(amount);
let actual = Self::reducible_balance(asset.clone(), who, Expendable, force).min(amount);
ensure!(actual == amount || precision == BestEffort, TokenError::FundsUnavailable);
Self::total_issuance(asset)
Self::total_issuance(asset.clone())
.checked_sub(&actual)
.ok_or(ArithmeticError::Overflow)?;
let actual = Self::decrease_balance(asset, who, actual, BestEffort, Expendable, force)?;
Self::set_total_issuance(asset, Self::total_issuance(asset).saturating_sub(actual));
let actual =
Self::decrease_balance(asset.clone(), who, actual, BestEffort, Expendable, force)?;
Self::set_total_issuance(
asset.clone(),
Self::total_issuance(asset.clone()).saturating_sub(actual),
);
Self::done_burn_from(asset, who, actual);
Ok(actual)
}
@@ -303,13 +310,17 @@ pub trait Mutate<AccountId>: Inspect<AccountId> + Unbalanced<AccountId> {
who: &AccountId,
amount: Self::Balance,
) -> Result<Self::Balance, DispatchError> {
let actual = Self::reducible_balance(asset, who, Expendable, Polite).min(amount);
let actual = Self::reducible_balance(asset.clone(), who, Expendable, Polite).min(amount);
ensure!(actual == amount, TokenError::FundsUnavailable);
Self::total_issuance(asset)
Self::total_issuance(asset.clone())
.checked_sub(&actual)
.ok_or(ArithmeticError::Overflow)?;
let actual = Self::decrease_balance(asset, who, actual, BestEffort, Expendable, Polite)?;
Self::set_total_issuance(asset, Self::total_issuance(asset).saturating_sub(actual));
let actual =
Self::decrease_balance(asset.clone(), who, actual, BestEffort, Expendable, Polite)?;
Self::set_total_issuance(
asset.clone(),
Self::total_issuance(asset.clone()).saturating_sub(actual),
);
Self::done_shelve(asset, who, actual);
Ok(actual)
}
@@ -329,11 +340,14 @@ pub trait Mutate<AccountId>: Inspect<AccountId> + Unbalanced<AccountId> {
who: &AccountId,
amount: Self::Balance,
) -> Result<Self::Balance, DispatchError> {
Self::total_issuance(asset)
Self::total_issuance(asset.clone())
.checked_add(&amount)
.ok_or(ArithmeticError::Overflow)?;
let actual = Self::increase_balance(asset, who, amount, Exact)?;
Self::set_total_issuance(asset, Self::total_issuance(asset).saturating_add(actual));
let actual = Self::increase_balance(asset.clone(), who, amount, Exact)?;
Self::set_total_issuance(
asset.clone(),
Self::total_issuance(asset.clone()).saturating_add(actual),
);
Self::done_restore(asset, who, amount);
Ok(actual)
}
@@ -346,13 +360,13 @@ pub trait Mutate<AccountId>: Inspect<AccountId> + Unbalanced<AccountId> {
amount: Self::Balance,
preservation: Preservation,
) -> Result<Self::Balance, DispatchError> {
let _extra =
Self::can_withdraw(asset, source, amount).into_result(preservation != Expendable)?;
Self::can_deposit(asset, dest, amount, Extant).into_result()?;
Self::decrease_balance(asset, source, amount, BestEffort, preservation, Polite)?;
let _extra = Self::can_withdraw(asset.clone(), source, amount)
.into_result(preservation != Expendable)?;
Self::can_deposit(asset.clone(), dest, amount, Extant).into_result()?;
Self::decrease_balance(asset.clone(), source, amount, BestEffort, preservation, Polite)?;
// This should never fail as we checked `can_deposit` earlier. But we do a best-effort
// anyway.
let _ = Self::increase_balance(asset, dest, amount, BestEffort);
let _ = Self::increase_balance(asset.clone(), dest, amount, BestEffort);
Self::done_transfer(asset, source, dest, amount);
Ok(amount)
}
@@ -363,7 +377,7 @@ pub trait Mutate<AccountId>: Inspect<AccountId> + Unbalanced<AccountId> {
///
/// Returns the new balance.
fn set_balance(asset: Self::AssetId, who: &AccountId, amount: Self::Balance) -> Self::Balance {
let b = Self::balance(asset, who);
let b = Self::balance(asset.clone(), who);
if b > amount {
Self::burn_from(asset, who, b - amount, BestEffort, Force).map(|d| b.saturating_sub(d))
} else {
@@ -391,7 +405,7 @@ impl<AccountId, U: Unbalanced<AccountId>> HandleImbalanceDrop<U::AssetId, U::Bal
for IncreaseIssuance<AccountId, U>
{
fn handle(asset: U::AssetId, amount: U::Balance) {
U::set_total_issuance(asset, U::total_issuance(asset).saturating_add(amount))
U::set_total_issuance(asset.clone(), U::total_issuance(asset).saturating_add(amount))
}
}
@@ -402,7 +416,7 @@ impl<AccountId, U: Unbalanced<AccountId>> HandleImbalanceDrop<U::AssetId, U::Bal
for DecreaseIssuance<AccountId, U>
{
fn handle(asset: U::AssetId, amount: U::Balance) {
U::set_total_issuance(asset, U::total_issuance(asset).saturating_sub(amount))
U::set_total_issuance(asset.clone(), U::total_issuance(asset).saturating_sub(amount))
}
}
@@ -423,11 +437,11 @@ pub trait Balanced<AccountId>: Inspect<AccountId> + Unbalanced<AccountId> {
/// This is infallible, but doesn't guarantee that the entire `amount` is burnt, for example
/// in the case of underflow.
fn rescind(asset: Self::AssetId, amount: Self::Balance) -> Debt<AccountId, Self> {
let old = Self::total_issuance(asset);
let old = Self::total_issuance(asset.clone());
let new = old.saturating_sub(amount);
Self::set_total_issuance(asset, new);
Self::set_total_issuance(asset.clone(), new);
let delta = old - new;
Self::done_rescind(asset, delta);
Self::done_rescind(asset.clone(), delta);
Imbalance::<Self::AssetId, Self::Balance, Self::OnDropDebt, Self::OnDropCredit>::new(
asset, delta,
)
@@ -440,11 +454,11 @@ pub trait Balanced<AccountId>: Inspect<AccountId> + Unbalanced<AccountId> {
/// This is infallible, but doesn't guarantee that the entire `amount` is issued, for example
/// in the case of overflow.
fn issue(asset: Self::AssetId, amount: Self::Balance) -> Credit<AccountId, Self> {
let old = Self::total_issuance(asset);
let old = Self::total_issuance(asset.clone());
let new = old.saturating_add(amount);
Self::set_total_issuance(asset, new);
Self::set_total_issuance(asset.clone(), new);
let delta = new - old;
Self::done_issue(asset, delta);
Self::done_issue(asset.clone(), delta);
Imbalance::<Self::AssetId, Self::Balance, Self::OnDropCredit, Self::OnDropDebt>::new(
asset, delta,
)
@@ -458,7 +472,7 @@ pub trait Balanced<AccountId>: Inspect<AccountId> + Unbalanced<AccountId> {
asset: Self::AssetId,
amount: Self::Balance,
) -> (Debt<AccountId, Self>, Credit<AccountId, Self>) {
(Self::rescind(asset, amount), Self::issue(asset, amount))
(Self::rescind(asset.clone(), amount), Self::issue(asset, amount))
}
/// Mints `value` into the account of `who`, creating it as needed.
@@ -476,8 +490,8 @@ pub trait Balanced<AccountId>: Inspect<AccountId> + Unbalanced<AccountId> {
value: Self::Balance,
precision: Precision,
) -> Result<Debt<AccountId, Self>, DispatchError> {
let increase = Self::increase_balance(asset, who, value, precision)?;
Self::done_deposit(asset, who, increase);
let increase = Self::increase_balance(asset.clone(), who, value, precision)?;
Self::done_deposit(asset.clone(), who, increase);
Ok(Imbalance::<Self::AssetId, Self::Balance, Self::OnDropDebt, Self::OnDropCredit>::new(
asset, increase,
))
@@ -504,8 +518,9 @@ pub trait Balanced<AccountId>: Inspect<AccountId> + Unbalanced<AccountId> {
preservation: Preservation,
force: Fortitude,
) -> Result<Credit<AccountId, Self>, DispatchError> {
let decrease = Self::decrease_balance(asset, who, value, precision, preservation, force)?;
Self::done_withdraw(asset, who, decrease);
let decrease =
Self::decrease_balance(asset.clone(), who, value, precision, preservation, force)?;
Self::done_withdraw(asset.clone(), who, decrease);
Ok(Imbalance::<Self::AssetId, Self::Balance, Self::OnDropCredit, Self::OnDropDebt>::new(
asset, decrease,
))
@@ -545,7 +560,7 @@ pub trait Balanced<AccountId>: Inspect<AccountId> + Unbalanced<AccountId> {
) -> Result<Credit<AccountId, Self>, Debt<AccountId, Self>> {
let amount = debt.peek();
let asset = debt.asset();
let credit = match Self::withdraw(asset, who, amount, Exact, preservation, Polite) {
let credit = match Self::withdraw(asset.clone(), who, amount, Exact, preservation, Polite) {
Err(_) => return Err(debt),
Ok(d) => d,
};
@@ -221,10 +221,10 @@ impl WithdrawReasons {
/// Simple amalgamation trait to collect together properties for an AssetId under one roof.
pub trait AssetId:
FullCodec + Copy + Eq + PartialEq + Debug + scale_info::TypeInfo + MaxEncodedLen
FullCodec + Clone + Eq + PartialEq + Debug + scale_info::TypeInfo + MaxEncodedLen
{
}
impl<T: FullCodec + Copy + Eq + PartialEq + Debug + scale_info::TypeInfo + MaxEncodedLen> AssetId
impl<T: FullCodec + Clone + Eq + PartialEq + Debug + scale_info::TypeInfo + MaxEncodedLen> AssetId
for T
{
}