Skip to content

Commit

Permalink
fix: nesting_budget weight
Browse files Browse the repository at this point in the history
  • Loading branch information
mrshiposha committed Oct 11, 2023
1 parent 053b43b commit 87f24e8
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 299 deletions.
22 changes: 0 additions & 22 deletions pallets/balances-adapter/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,6 @@ impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {
Weight::default()
}

fn burn_recursively_self_raw() -> Weight {
Weight::default()
}

fn burn_recursively_breadth_raw(_amount: u32) -> Weight {
Weight::default()
}

fn token_owner() -> Weight {
Weight::default()
}

fn set_allowance_for_all() -> Weight {
Weight::default()
}
Expand Down Expand Up @@ -120,16 +108,6 @@ impl<T: Config> CommonCollectionOperations<T> for NativeFungibleHandle<T> {
fail!(<pallet_common::Error<T>>::UnsupportedOperation);
}

fn burn_item_recursively(
&self,
_sender: <T>::CrossAccountId,
_token: TokenId,
_self_budget: &dyn up_data_structs::budget::Budget,
_breadth_budget: &dyn up_data_structs::budget::Budget,
) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
fail!(<pallet_common::Error<T>>::UnsupportedOperation);
}

fn set_collection_properties(
&self,
_sender: <T>::CrossAccountId,
Expand Down
38 changes: 0 additions & 38 deletions pallets/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1938,30 +1938,6 @@ pub trait CommonWeightInfo<CrossAccountId> {
/// The price of burning a token from another user.
fn burn_from() -> Weight;

/// Differs from burn_item in case of Fungible and Refungible, as it should burn
/// whole users's balance.
///
/// This method shouldn't be used directly, as it doesn't count breadth price, use [burn_recursively](CommonWeightInfo::burn_recursively) instead
fn burn_recursively_self_raw() -> Weight;

/// Cost of iterating over `amount` children while burning, without counting child burning itself.
///
/// This method shouldn't be used directly, as it doesn't count depth price, use [burn_recursively](CommonWeightInfo::burn_recursively) instead
fn burn_recursively_breadth_raw(amount: u32) -> Weight;

/// The price of recursive burning a token.
///
/// `max_selfs` - The maximum burning weight of the token itself.
/// `max_breadth` - The maximum number of nested tokens to burn.
fn burn_recursively(max_selfs: u32, max_breadth: u32) -> Weight {
Self::burn_recursively_self_raw()
.saturating_mul(max_selfs.max(1) as u64)
.saturating_add(Self::burn_recursively_breadth_raw(max_breadth))
}

/// The price of retrieving token owner
fn token_owner() -> Weight;

/// The price of setting approval for all
fn set_allowance_for_all() -> Weight;

Expand Down Expand Up @@ -2033,20 +2009,6 @@ pub trait CommonCollectionOperations<T: Config> {
amount: u128,
) -> DispatchResultWithPostInfo;

/// Burn token and all nested tokens recursievly.
///
/// * `sender` - The user who owns the token.
/// * `token` - Token id that will burned.
/// * `self_budget` - The budget that can be spent on burning tokens.
/// * `breadth_budget` - The budget that can be spent on burning nested tokens.
fn burn_item_recursively(
&self,
sender: T::CrossAccountId,
token: TokenId,
self_budget: &dyn Budget,
breadth_budget: &dyn Budget,
) -> DispatchResultWithPostInfo;

/// Set collection properties.
///
/// * `sender` - Must be either the owner of the collection or its admin.
Expand Down
39 changes: 1 addition & 38 deletions pallets/fungible/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@

use core::marker::PhantomData;

use frame_support::{
dispatch::DispatchResultWithPostInfo, ensure, fail, traits::Get, weights::Weight,
};
use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight};
use pallet_common::{
weights::WeightInfo as _, with_weight, CommonCollectionOperations, CommonWeightInfo,
RefungibleExtensions, SelfWeightOf as PalletCommonWeightOf,
};
use pallet_structure::Error as StructureError;
use sp_runtime::{ArithmeticError, DispatchError};
use sp_std::{vec, vec::Vec};
use up_data_structs::{
Expand Down Expand Up @@ -93,20 +90,6 @@ impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {
<SelfWeightOf<T>>::burn_from()
}

fn burn_recursively_self_raw() -> Weight {
// Read to get total balance
Self::burn_item().saturating_add(T::DbWeight::get().reads(1))
}

fn burn_recursively_breadth_raw(_amount: u32) -> Weight {
// Fungible tokens can't have children
Weight::zero()
}

fn token_owner() -> Weight {
Weight::zero()
}

fn set_allowance_for_all() -> Weight {
Weight::zero()
}
Expand Down Expand Up @@ -195,26 +178,6 @@ impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {
)
}

fn burn_item_recursively(
&self,
sender: T::CrossAccountId,
token: TokenId,
self_budget: &dyn Budget,
_breadth_budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
// Should not happen?
ensure!(
token == TokenId::default(),
<Error<T>>::FungibleItemsHaveNoId
);
ensure!(self_budget.consume(), <StructureError<T>>::DepthLimit,);

with_weight(
<Pallet<T>>::burn(self, &sender, <Balance<T>>::get((self.id, &sender))),
<CommonWeights<T>>::burn_recursively_self_raw(),
)
}

fn transfer(
&self,
from: T::CrossAccountId,
Expand Down
23 changes: 0 additions & 23 deletions pallets/nonfungible/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,6 @@ impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {
<SelfWeightOf<T>>::burn_from()
}

fn burn_recursively_self_raw() -> Weight {
<SelfWeightOf<T>>::burn_recursively_self_raw()
}

fn burn_recursively_breadth_raw(amount: u32) -> Weight {
<SelfWeightOf<T>>::burn_recursively_breadth_plus_self_plus_self_per_each_raw(amount)
.saturating_sub(Self::burn_recursively_self_raw().saturating_mul(amount as u64 + 1))
}

fn token_owner() -> Weight {
<SelfWeightOf<T>>::token_owner()
}

fn set_allowance_for_all() -> Weight {
<SelfWeightOf<T>>::set_allowance_for_all()
}
Expand Down Expand Up @@ -309,16 +296,6 @@ impl<T: Config> CommonCollectionOperations<T> for NonfungibleHandle<T> {
}
}

fn burn_item_recursively(
&self,
sender: T::CrossAccountId,
token: TokenId,
self_budget: &dyn Budget,
breadth_budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
<Pallet<T>>::burn_recursively(self, &sender, token, self_budget, breadth_budget)
}

fn transfer(
&self,
from: T::CrossAccountId,
Expand Down
47 changes: 1 addition & 46 deletions pallets/nonfungible/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ use pallet_common::{
};
use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};
use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};
use pallet_structure::{Error as StructureError, Pallet as PalletStructure};
use pallet_structure::Pallet as PalletStructure;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_core::{Get, H160};
Expand Down Expand Up @@ -503,51 +503,6 @@ impl<T: Config> Pallet<T> {
Ok(())
}

/// Same as [`burn`] but burns all the tokens that are nested in the token first
///
/// - `self_budget`: Limit for searching children in depth.
/// - `breadth_budget`: Limit of breadth of searching children.
///
/// [`burn`]: struct.Pallet.html#method.burn
#[transactional]
pub fn burn_recursively(
collection: &NonfungibleHandle<T>,
sender: &T::CrossAccountId,
token: TokenId,
self_budget: &dyn Budget,
breadth_budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
ensure!(self_budget.consume(), <StructureError<T>>::DepthLimit,);

let current_token_account =
T::CrossTokenAddressMapping::token_to_address(collection.id, token);

let mut weight = Weight::zero();

// This method is transactional, if user in fact doesn't have permissions to remove token -
// tokens removed here will be restored after rejected transaction
for ((collection, token), _) in <TokenChildren<T>>::iter_prefix((collection.id, token)) {
ensure!(breadth_budget.consume(), <StructureError<T>>::BreadthLimit,);
let PostDispatchInfo { actual_weight, .. } =
<PalletStructure<T>>::burn_item_recursively(
current_token_account.clone(),
collection,
token,
self_budget,
breadth_budget,
)?;
if let Some(actual_weight) = actual_weight {
weight = weight.saturating_add(actual_weight);
}
}

Self::burn(collection, sender, token)?;
DispatchResultWithPostInfo::Ok(PostDispatchInfo {
actual_weight: Some(weight + <SelfWeightOf<T>>::burn_item()),
pays_fee: Pays::Yes,
})
}

/// A batch operation to add, edit or remove properties for a token.
///
/// - `nesting_budget`: Limit for searching parents in-depth to check ownership.
Expand Down
38 changes: 2 additions & 36 deletions pallets/refungible/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@

use core::marker::PhantomData;

use frame_support::{
dispatch::DispatchResultWithPostInfo, ensure, fail, traits::Get, weights::Weight,
};
use frame_support::{dispatch::DispatchResultWithPostInfo, fail, weights::Weight};
use pallet_common::{
weights::WeightInfo as _, with_weight, write_token_properties_total_weight,
CommonCollectionOperations, CommonWeightInfo, RefungibleExtensions,
};
use pallet_structure::{Error as StructureError, Pallet as PalletStructure};
use pallet_structure::Pallet as PalletStructure;
use sp_runtime::DispatchError;
use sp_std::{collections::btree_map::BTreeMap, vec, vec::Vec};
use up_data_structs::{
Expand Down Expand Up @@ -131,19 +129,6 @@ impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {
<SelfWeightOf<T>>::burn_from()
}

fn burn_recursively_self_raw() -> Weight {
// Read to get total balance
Self::burn_item() + T::DbWeight::get().reads(1)
}
fn burn_recursively_breadth_raw(_amount: u32) -> Weight {
// Refungible token can't have children
Weight::zero()
}

fn token_owner() -> Weight {
<SelfWeightOf<T>>::token_owner()
}

fn set_allowance_for_all() -> Weight {
<SelfWeightOf<T>>::set_allowance_for_all()
}
Expand Down Expand Up @@ -260,25 +245,6 @@ impl<T: Config> CommonCollectionOperations<T> for RefungibleHandle<T> {
)
}

fn burn_item_recursively(
&self,
sender: T::CrossAccountId,
token: TokenId,
self_budget: &dyn Budget,
_breadth_budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
ensure!(self_budget.consume(), <StructureError<T>>::DepthLimit,);
with_weight(
<Pallet<T>>::burn(
self,
&sender,
token,
<Balance<T>>::get((self.id, token, &sender)),
),
<CommonWeights<T>>::burn_recursively_self_raw(),
)
}

fn transfer(
&self,
from: T::CrossAccountId,
Expand Down
22 changes: 1 addition & 21 deletions pallets/structure/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::{
dispatch::{DispatchResult, DispatchResultWithPostInfo},
fail,
pallet_prelude::*,
};
use frame_support::{dispatch::DispatchResult, fail, pallet_prelude::*};
use pallet_common::{
dispatch::CollectionDispatch, erc::CrossAccountId, eth::is_collection,
CommonCollectionOperations,
Expand Down Expand Up @@ -269,22 +265,6 @@ impl<T: Config> Pallet<T> {
Err(<Error<T>>::DepthLimit.into())
}

/// Burn token and all of it's nested tokens
///
/// - `self_budget`: Limit for searching children in depth.
/// - `breadth_budget`: Limit of breadth of searching children.
pub fn burn_item_recursively(
from: T::CrossAccountId,
collection: CollectionId,
token: TokenId,
self_budget: &dyn Budget,
breadth_budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
let dispatch = T::CollectionDispatch::dispatch(collection)?;
let dispatch = dispatch.as_dyn();
dispatch.burn_item_recursively(from, token, self_budget, breadth_budget)
}

/// Check if `token` indirectly owned by `user`
///
/// Returns `true` if `user` is `token`'s owner. Or If token is provided as `user` then
Expand Down
2 changes: 2 additions & 0 deletions pallets/unique/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ std = [
'sp-std/std',
'up-common/std',
'up-data-structs/std',
'pallet-structure/std',
]
stubgen = ["evm-coder/stubgen", "pallet-common/stubgen"]
try-runtime = ["frame-support/try-runtime"]
Expand All @@ -54,6 +55,7 @@ pallet-evm = { workspace = true }
pallet-evm-coder-substrate = { workspace = true }
pallet-nonfungible = { workspace = true }
pallet-refungible = { workspace = true }
pallet-structure = { workspace = true }
scale-info = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
Expand Down
Loading

0 comments on commit 87f24e8

Please sign in to comment.