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

Asset Pallet: Support repeated destroys to safely destroy large assets #12310

Merged
merged 73 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
6a9d9d1
Support repeated destroys to safely destroy large assets
tonyalaribe Sep 20, 2022
7db1fd5
require freezing accounts before destroying
tonyalaribe Sep 20, 2022
cbe14b2
support only deleting asset as final stage when there's no assets left
tonyalaribe Sep 21, 2022
001eaef
Merge branch 'master' into aa/safely-destroy-large-assets
tonyalaribe Sep 21, 2022
1beae07
pre: introduce the RemoveKeyLimit config parameter
tonyalaribe Sep 22, 2022
c9ce171
debug_ensure empty account in the right if block
tonyalaribe Sep 22, 2022
3067e38
update to having separate max values for accounts and approvals
tonyalaribe Sep 22, 2022
7c4f610
add tests and use RemoveKeyLimit constant
tonyalaribe Sep 26, 2022
aca497f
add useful comments to the extrinsics, and calculate returned weight
tonyalaribe Sep 27, 2022
634345a
Merge branch 'master' of github.com:paritytech/substrate into aa/safe…
tonyalaribe Sep 27, 2022
93d886d
add benchmarking for start_destroy and finish destroy
tonyalaribe Sep 27, 2022
2d3b650
push failing benchmark logic
tonyalaribe Sep 27, 2022
5a612d1
add benchmark tests for new functions
tonyalaribe Sep 27, 2022
b961255
update weights via local benchmarks
tonyalaribe Sep 27, 2022
91e3c18
remove extra weight file
tonyalaribe Sep 27, 2022
60f954a
Update frame/assets/src/lib.rs
tonyalaribe Sep 28, 2022
ce00b5b
Update frame/assets/src/types.rs
tonyalaribe Sep 28, 2022
5492b6d
Update frame/assets/src/lib.rs
tonyalaribe Sep 28, 2022
bc20a6c
effect some changes from codereview
tonyalaribe Sep 28, 2022
6ceb592
use NotFrozen error
tonyalaribe Sep 29, 2022
2edac52
remove origin checks, as anyone can complete destruction after owner …
tonyalaribe Sep 29, 2022
aa2aecf
fix comments about Origin behaviour
tonyalaribe Sep 29, 2022
10fdd90
add AssetStatus docs
tonyalaribe Sep 29, 2022
139d2e9
modularize logic to allow calling logic in on_idle and on_initialize …
tonyalaribe Oct 5, 2022
597f532
introduce simple migration for assets details
tonyalaribe Oct 6, 2022
a09e762
Merge remote-tracking branch 'origin/master' into aa/safely-destroy-l…
Oct 6, 2022
70930ac
reintroduce logging in the migrations
tonyalaribe Oct 7, 2022
dc4a243
move deposit_Event out of the mutate block
tonyalaribe Oct 12, 2022
05d778d
Update frame/assets/src/functions.rs
tonyalaribe Oct 12, 2022
62d4f13
Update frame/assets/src/migration.rs
tonyalaribe Oct 12, 2022
e9e108b
move AssetNotLive checkout out of the mutate blocks
tonyalaribe Oct 12, 2022
5df4a62
Merge branch 'aa/safely-destroy-large-assets' of github.com:paritytec…
tonyalaribe Oct 12, 2022
544ac52
rename RemoveKeysLimit to RemoveItemsLimit
tonyalaribe Oct 13, 2022
9206acf
update docs
tonyalaribe Oct 13, 2022
29c7745
fix event name in benchmark
tonyalaribe Oct 14, 2022
2986f29
fix cargo fmt.
tonyalaribe Oct 14, 2022
6ac84a1
fix lint in benchmarking
tonyalaribe Oct 14, 2022
1f2930f
Merge branch 'master' of github.com:paritytech/substrate into aa/safe…
tonyalaribe Oct 14, 2022
791aa7f
Empty commit to trigger CI
tonyalaribe Oct 14, 2022
450a698
Update frame/assets/src/lib.rs
tonyalaribe Oct 14, 2022
3daef79
Update frame/assets/src/lib.rs
tonyalaribe Oct 14, 2022
ea34cf5
Update frame/assets/src/functions.rs
tonyalaribe Oct 14, 2022
84cbb47
Update frame/assets/src/functions.rs
tonyalaribe Oct 14, 2022
082a10c
Update frame/assets/src/functions.rs
tonyalaribe Oct 14, 2022
e84d5ae
Update frame/assets/src/lib.rs
tonyalaribe Oct 14, 2022
78cde15
Update frame/assets/src/functions.rs
tonyalaribe Oct 14, 2022
c6470fb
effect change suggested during code review
tonyalaribe Oct 14, 2022
85e50b9
move limit to a single location
tonyalaribe Oct 14, 2022
c97e850
Merge branch 'master' of github.com:paritytech/substrate into aa/safe…
tonyalaribe Oct 17, 2022
df27e0b
Update frame/assets/src/functions.rs
tonyalaribe Oct 18, 2022
ca9fa2e
rename events
tonyalaribe Oct 18, 2022
9609f13
fix weight typo, using rocksdb instead of T::DbWeight. Pending genera…
tonyalaribe Oct 18, 2022
26d27cc
switch to using dead_account.len()
tonyalaribe Oct 18, 2022
72ec8f8
rename event in the benchmarks
tonyalaribe Oct 20, 2022
ee30cf8
empty to retrigger CI
tonyalaribe Oct 20, 2022
87a7877
Merge branch 'master' of github.com:paritytech/substrate into aa/safe…
tonyalaribe Oct 20, 2022
773212e
trigger CI to check cumulus dependency
tonyalaribe Oct 20, 2022
a4be57f
trigger CI for dependent cumulus
tonyalaribe Oct 20, 2022
111da0e
Merge branch 'master' into aa/safely-destroy-large-assets
tonyalaribe Oct 22, 2022
9c06bb8
Update frame/assets/src/migration.rs
tonyalaribe Oct 24, 2022
4eef069
move is-frozen to the assetStatus enum (#12547)
tonyalaribe Oct 26, 2022
486814f
add pre and post migration hooks
tonyalaribe Oct 26, 2022
c185cb0
update do_transfer logic to add new assert for more correct error mes…
tonyalaribe Oct 26, 2022
91095af
trigger CI
tonyalaribe Oct 26, 2022
9ef3c2b
switch checking AssetStatus from checking Destroying state to checkin…
tonyalaribe Oct 27, 2022
6165576
fix error type in tests from Frozen to AssetNotLive
tonyalaribe Oct 27, 2022
7811b62
trigger CI
tonyalaribe Oct 27, 2022
cd3e28d
change ensure check for fn reducible_balance()
tonyalaribe Oct 27, 2022
aee596e
change the error type to Error:<T,I>::IncorrectStatus to be clearer
tonyalaribe Nov 7, 2022
30814df
Trigger CI
tonyalaribe Nov 7, 2022
9b653a5
Merge branch 'master' into aa/safely-destroy-large-assets
tonyalaribe Nov 7, 2022
57fe747
Merge branch 'master' into aa/safely-destroy-large-assets
tonyalaribe Nov 10, 2022
7ba1ef5
Merge branch 'master' into aa/safely-destroy-large-assets
tonyalaribe Nov 14, 2022
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
3 changes: 3 additions & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,7 @@ parameter_types! {
pub const StringLimit: u32 = 50;
pub const MetadataDepositBase: Balance = 10 * DOLLARS;
pub const MetadataDepositPerByte: Balance = 1 * DOLLARS;
pub const RemoveKeysLimit: u32 = 1000;
}

impl pallet_assets::Config for Runtime {
Expand All @@ -1427,6 +1428,8 @@ impl pallet_assets::Config for Runtime {
type Freezer = ();
type Extra = ();
type WeightInfo = pallet_assets::weights::SubstrateWeight<Runtime>;
type RemoveAccountsLimit = RemoveAccountsLimit;
type RemoveApprovalsLimit = RemoveApprovalsLimit;
}

parameter_types! {
Expand Down
3 changes: 1 addition & 2 deletions frame/assets/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,10 @@ benchmarks_instance_pallet! {

destroy {
let c in 0 .. 5_000;
let s in 0 .. 5_000;
let a in 0 .. 5_00;
let (caller, _) = create_default_asset::<T, I>(true);
add_consumers::<T, I>(caller.clone(), c);
add_sufficients::<T, I>(caller.clone(), s);
// add_sufficients::<T, I>(caller.clone(), s);
add_approvals::<T, I>(caller.clone(), a);
let witness = Asset::<T, I>::get(T::AssetId::default()).unwrap().destroy_witness();
}: _(SystemOrigin::Signed(caller), Default::default(), witness)
Expand Down
61 changes: 48 additions & 13 deletions frame/assets/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,35 +679,70 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
if let Some(check_owner) = maybe_check_owner {
ensure!(details.owner == check_owner, Error::<T, I>::NoPermission);
}

ensure!(details.is_frozen, Error::<T, I>::BadWitness);
ensure!(details.accounts <= witness.accounts, Error::<T, I>::BadWitness);
ensure!(details.sufficients <= witness.sufficients, Error::<T, I>::BadWitness);
ensure!(details.approvals <= witness.approvals, Error::<T, I>::BadWitness);

for (who, v) in Account::<T, I>::drain_prefix(id) {
let mut removed_accounts = 0u32;
let mut removed_approvals = 0u32;

let accounts_to_delete: Vec<(T::AccountId, _)> = Account::<T, I>::iter_prefix(id)
.take(T::RemoveAccountsLimit::get() as usize)
.collect();

for (who, v) in accounts_to_delete {
if removed_accounts >= T::RemoveAccountsLimit::get() {
break
}

Account::<T, I>::remove(id, &who);

// We have to force this as it's destroying the entire asset class.
// This could mean that some accounts now have irreversibly reserved
// funds.
let _ = Self::dead_account(&who, &mut details, &v.reason, true);
dead_accounts.push(who);
removed_accounts += 1;
}
tonyalaribe marked this conversation as resolved.
Show resolved Hide resolved
tonyalaribe marked this conversation as resolved.
Show resolved Hide resolved
debug_assert_eq!(details.accounts, 0);
debug_assert_eq!(details.sufficients, 0);

let metadata = Metadata::<T, I>::take(&id);
T::Currency::unreserve(
&details.owner,
details.deposit.saturating_add(metadata.deposit),
);
for ((owner, destination), approval) in Approvals::<T, I>::iter_prefix((id,)) {
if removed_approvals >= T::RemoveApprovalsLimit::get() {
break
}

for ((owner, _), approval) in Approvals::<T, I>::drain_prefix((&id,)) {
T::Currency::unreserve(&owner, approval.deposit);
Approvals::<T, I>::remove((id, owner, destination));
removed_approvals += 1;
}

let accounts_remaining = Account::<T, I>::iter_prefix(id).count();
tonyalaribe marked this conversation as resolved.
Show resolved Hide resolved
if accounts_remaining > 0 {
// Reintroduce the details, so the asset is not deleted yet.
let _ = maybe_details.insert(details.clone());

Self::deposit_event(Event::PartiallyDestroyed {
asset_id: id,
accounts_destroyed: dead_accounts.len() as u32,
accounts_remaining: accounts_remaining as u32,
});
} else {
debug_assert_eq!(details.accounts, 0);
debug_assert_eq!(details.sufficients, 0);

let metadata = Metadata::<T, I>::take(&id);
T::Currency::unreserve(
&details.owner,
details.deposit.saturating_add(metadata.deposit),
);
Self::deposit_event(Event::Destroyed { asset_id: id });
}
Self::deposit_event(Event::Destroyed { asset_id: id });

Ok(DestroyWitness {
accounts: details.accounts,
sufficients: details.sufficients,
approvals: details.approvals,
accounts: removed_accounts,
sufficients: 0,
approvals: removed_approvals,
})
},
)?;
Expand Down
24 changes: 19 additions & 5 deletions frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ pub mod pallet {
+ MaxEncodedLen
+ TypeInfo;

/// Max number of accounts to destroy per extrinsic call.
#[pallet::constant]
type RemoveAccountsLimit: Get<u32>;

/// Max number of approvalss to destroy per extrinsic call.
#[pallet::constant]
type RemoveApprovalsLimit: Get<u32>;

/// Identifier for the class of asset.
type AssetId: Member
+ Parameter
Expand Down Expand Up @@ -409,6 +417,12 @@ pub mod pallet {
AssetThawed { asset_id: T::AssetId },
/// An asset class was destroyed.
Destroyed { asset_id: T::AssetId },
/// An asset class was only destroyed. The destroy action should be re-executed.
PartiallyDestroyed {
asset_id: T::AssetId,
accounts_destroyed: u32,
accounts_remaining: u32,
},
/// Some asset class was force-created.
ForceCreated { asset_id: T::AssetId, owner: T::AccountId },
/// New metadata has been set for an asset.
Expand Down Expand Up @@ -570,6 +584,7 @@ pub mod pallet {
}

/// Destroy a class of fungible assets.
/// Due to weight restrictions, this function may need to be called multiple
///
/// The origin must conform to `ForceOrigin` or must be Signed and the sender must be the
/// owner of the asset `id`.
Expand All @@ -587,10 +602,10 @@ pub mod pallet {
/// - `c = (witness.accounts - witness.sufficients)`
/// - `s = witness.sufficients`
/// - `a = witness.approvals`
/// TODO: Change the weights to T::RemoveKeysLimit::get() like in cloudloads
#[pallet::weight(T::WeightInfo::destroy(
witness.accounts.saturating_sub(witness.sufficients),
witness.sufficients,
witness.approvals,
T::RemoveAccountsLimit::get(),
T::RemoveApprovalsLimit::get(),
))]
pub fn destroy(
origin: OriginFor<T>,
Expand All @@ -603,8 +618,7 @@ pub mod pallet {
};
let details = Self::do_destroy(id, witness, maybe_check_owner)?;
Ok(Some(T::WeightInfo::destroy(
details.accounts.saturating_sub(details.sufficients),
details.sufficients,
details.accounts,
details.approvals,
))
.into())
Expand Down
7 changes: 7 additions & 0 deletions frame/assets/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ impl pallet_balances::Config for Test {
type ReserveIdentifier = [u8; 8];
}

parameter_types! {
pub const RemoveAccountsLimit: u32 = 5;
pub const RemoveApprovalsLimit: u32 = 5;
}

impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type Balance = u64;
Expand All @@ -99,6 +104,8 @@ impl Config for Test {
type Freezer = TestFreezer;
type WeightInfo = ();
type Extra = ();
type RemoveAccountsLimit = RemoveAccountsLimit;
type RemoveApprovalsLimit = RemoveApprovalsLimit;
}

use std::collections::HashMap;
Expand Down
37 changes: 37 additions & 0 deletions frame/assets/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ fn lifecycle_should_work() {
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 0, 20, 100));
assert_eq!(Account::<Test>::iter_prefix(0).count(), 2);

assert_ok!(Assets::freeze_asset(RuntimeOrigin::signed(1), 0));
let w = Asset::<Test>::get(0).unwrap().destroy_witness();
assert_ok!(Assets::destroy(RuntimeOrigin::signed(1), 0, w));
assert_eq!(Balances::reserved_balance(&1), 0);
Expand All @@ -335,6 +336,7 @@ fn lifecycle_should_work() {
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 0, 20, 100));
assert_eq!(Account::<Test>::iter_prefix(0).count(), 2);

assert_ok!(Assets::freeze_asset(RuntimeOrigin::signed(1), 0));
let w = Asset::<Test>::get(0).unwrap().destroy_witness();
assert_ok!(Assets::destroy(RuntimeOrigin::root(), 0, w));
assert_eq!(Balances::reserved_balance(&1), 0);
Expand All @@ -353,6 +355,7 @@ fn destroy_with_bad_witness_should_not_work() {
let mut w = Asset::<Test>::get(0).unwrap().destroy_witness();
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 0, 10, 100));
// witness too low
assert_ok!(Assets::freeze_asset(RuntimeOrigin::signed(1), 0));
assert_noop!(Assets::destroy(RuntimeOrigin::signed(1), 0, w), Error::<Test>::BadWitness);
// witness too high is okay though
w.accounts += 2;
Expand All @@ -372,6 +375,7 @@ fn destroy_should_refund_approvals() {
assert_ok!(Assets::approve_transfer(RuntimeOrigin::signed(1), 0, 4, 50));
assert_eq!(Balances::reserved_balance(&1), 3);

assert_ok!(Assets::freeze_asset(RuntimeOrigin::signed(1), 0));
let w = Asset::<Test>::get(0).unwrap().destroy_witness();
assert_ok!(Assets::destroy(RuntimeOrigin::signed(1), 0, w));
assert_eq!(Balances::reserved_balance(&1), 0);
Expand All @@ -381,6 +385,38 @@ fn destroy_should_refund_approvals() {
});
}

#[test]
fn partial_destroy_should_work() {
new_test_ext().execute_with(|| {
assert_ok!(Assets::force_create(RuntimeOrigin::root(), 0, 1, true, 1));
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 0, 1, 10));
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 0, 2, 10));
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 0, 3, 10));
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 0, 4, 10));
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 0, 5, 10));
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 0, 6, 10));
assert_ok!(Assets::freeze_asset(RuntimeOrigin::signed(1), 0));

let w = Asset::<Test>::get(0).unwrap().destroy_witness();
assert_ok!(Assets::destroy(RuntimeOrigin::signed(1), 0, w));
System::assert_has_event(RuntimeEvent::Assets(crate::Event::PartiallyDestroyed {
asset_id: 0,
accounts_destroyed: 5,
accounts_remaining: 1,
}));
// PartiallyDestroyed Asset should continue to exist
assert!(Asset::<Test>::contains_key(0));

// Second call to destroy on PartiallyDestroyed asset
let w2 = Asset::<Test>::get(0).unwrap().destroy_witness();
assert_ok!(Assets::destroy(RuntimeOrigin::signed(1), 0, w2));
System::assert_has_event(RuntimeEvent::Assets(crate::Event::Destroyed { asset_id: 0 }));

// Destroyed Asset should not exist
assert!(!Asset::<Test>::contains_key(0));
})
}

#[test]
fn non_providing_should_work() {
new_test_ext().execute_with(|| {
Expand Down Expand Up @@ -790,6 +826,7 @@ fn destroy_calls_died_hooks() {
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 0, 1, 100));
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 0, 2, 100));
// Destroy the asset.
assert_ok!(Assets::freeze_asset(RuntimeOrigin::signed(1), 0));
let w = Asset::<Test>::get(0).unwrap().destroy_witness();
assert_ok!(Assets::destroy(RuntimeOrigin::signed(1), 0, w));

Expand Down
14 changes: 3 additions & 11 deletions frame/assets/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use sp_std::marker::PhantomData;
pub trait WeightInfo {
fn create() -> Weight;
fn force_create() -> Weight;
fn destroy(c: u32, s: u32, a: u32, ) -> Weight;
fn destroy(c: u32, a: u32, ) -> Weight;
fn mint() -> Weight;
fn burn() -> Weight;
fn transfer() -> Weight;
Expand Down Expand Up @@ -89,21 +89,17 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Storage: System Account (r:5000 w:5000)
// Storage: Assets Metadata (r:1 w:0)
// Storage: Assets Approvals (r:501 w:500)
fn destroy(c: u32, s: u32, a: u32, ) -> Weight {
fn destroy(c: u32, a: u32, ) -> Weight {
Weight::from_ref_time(0 as u64)
// Standard Error: 37_000
.saturating_add(Weight::from_ref_time(17_145_000 as u64).saturating_mul(c as u64))
// Standard Error: 37_000
.saturating_add(Weight::from_ref_time(19_333_000 as u64).saturating_mul(s as u64))
// Standard Error: 375_000
.saturating_add(Weight::from_ref_time(17_046_000 as u64).saturating_mul(a as u64))
.saturating_add(T::DbWeight::get().reads(5 as u64))
.saturating_add(T::DbWeight::get().reads((2 as u64).saturating_mul(c as u64)))
.saturating_add(T::DbWeight::get().reads((2 as u64).saturating_mul(s as u64)))
.saturating_add(T::DbWeight::get().reads((1 as u64).saturating_mul(a as u64)))
.saturating_add(T::DbWeight::get().writes(2 as u64))
.saturating_add(T::DbWeight::get().writes((2 as u64).saturating_mul(c as u64)))
.saturating_add(T::DbWeight::get().writes((2 as u64).saturating_mul(s as u64)))
.saturating_add(T::DbWeight::get().writes((1 as u64).saturating_mul(a as u64)))
}
// Storage: Assets Asset (r:1 w:1)
Expand Down Expand Up @@ -274,21 +270,17 @@ impl WeightInfo for () {
// Storage: System Account (r:5000 w:5000)
// Storage: Assets Metadata (r:1 w:0)
// Storage: Assets Approvals (r:501 w:500)
fn destroy(c: u32, s: u32, a: u32, ) -> Weight {
fn destroy(c: u32, a: u32, ) -> Weight {
Weight::from_ref_time(0 as u64)
// Standard Error: 37_000
.saturating_add(Weight::from_ref_time(17_145_000 as u64).saturating_mul(c as u64))
// Standard Error: 37_000
.saturating_add(Weight::from_ref_time(19_333_000 as u64).saturating_mul(s as u64))
// Standard Error: 375_000
.saturating_add(Weight::from_ref_time(17_046_000 as u64).saturating_mul(a as u64))
.saturating_add(RocksDbWeight::get().reads(5 as u64))
.saturating_add(RocksDbWeight::get().reads((2 as u64).saturating_mul(c as u64)))
.saturating_add(RocksDbWeight::get().reads((2 as u64).saturating_mul(s as u64)))
.saturating_add(RocksDbWeight::get().reads((1 as u64).saturating_mul(a as u64)))
.saturating_add(RocksDbWeight::get().writes(2 as u64))
.saturating_add(RocksDbWeight::get().writes((2 as u64).saturating_mul(c as u64)))
.saturating_add(RocksDbWeight::get().writes((2 as u64).saturating_mul(s as u64)))
.saturating_add(RocksDbWeight::get().writes((1 as u64).saturating_mul(a as u64)))
}
// Storage: Assets Asset (r:1 w:1)
Expand Down