Skip to content

Commit 7400fea

Browse files
authored
FIX: cleanup FeeLock queue entries on unlock (#469)
- reproduce bug in dedicated UT - automatically cleanup UnlockQueue storage entries
1 parent 87a2b46 commit 7400fea

File tree

5 files changed

+79
-33
lines changed

5 files changed

+79
-33
lines changed

pallets/fee-lock/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,9 @@ impl<T: Config> FeeLockTriggerTrait<T::AccountId> for Pallet<T> {
525525
);
526526
}
527527

528-
FeeLockMetadataQeueuePosition::<T>::take(&who);
528+
if let Some(pos) = FeeLockMetadataQeueuePosition::<T>::take(&who) {
529+
UnlockQueue::<T>::take(pos);
530+
}
529531
AccountFeeLockData::<T>::remove(&who);
530532

531533
Self::deposit_event(Event::FeeLockUnlocked(

pallets/fee-lock/src/tests.rs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -741,12 +741,6 @@ const ACCOUNT_WITH_LOCKED_TOKENS: orml_tokens::AccountData<u128> = AccountData {
741741
frozen: 0u128,
742742
};
743743

744-
const ACCOUNT_WITH_LOCKED_TOKENS_TWICE: orml_tokens::AccountData<u128> = AccountData {
745-
free: INITIAL_AMOUNT - 2 * FEE_LOCK_AMOUNT,
746-
reserved: 2 * FEE_LOCK_AMOUNT,
747-
frozen: 0u128,
748-
};
749-
750744
fn calculate_estimated_weight(unlock_fee_calls: u64, reads: u64, writes: u64) -> Weight {
751745
<Test as frame_system::Config>::DbWeight::get().reads(reads) +
752746
<Test as frame_system::Config>::DbWeight::get().writes(writes) +
@@ -1123,3 +1117,45 @@ fn test_unlock_happens_in_order() {
11231117
);
11241118
});
11251119
}
1120+
1121+
#[test]
1122+
fn test_queue_storage_is_cleaned_up() {
1123+
ExtBuilder::new()
1124+
.create_token(NativeCurrencyId::get())
1125+
.mint(ALICE, NativeCurrencyId::get(), INITIAL_AMOUNT)
1126+
.mint(BOB, NativeCurrencyId::get(), INITIAL_AMOUNT)
1127+
.initialize_fee_locks(PERIOD_LENGTH, FEE_LOCK_AMOUNT, SWAP_VALUE_THRESHOLD)
1128+
.build()
1129+
.execute_with(|| {
1130+
assert_eq!(UnlockQueue::<Test>::get(0), None);
1131+
assert_eq!(UnlockQueue::<Test>::get(1), None);
1132+
1133+
<FeeLock as FeeLockTriggerTrait<_>>::process_fee_lock(&ALICE).unwrap();
1134+
assert_eq!(UnlockQueue::<Test>::get(0), Some(ALICE));
1135+
assert_eq!(UnlockQueue::<Test>::get(1), None);
1136+
assert_eq!(
1137+
Tokens::accounts(ALICE, NativeCurrencyId::get()),
1138+
ACCOUNT_WITH_LOCKED_TOKENS
1139+
);
1140+
1141+
<FeeLock as FeeLockTriggerTrait<_>>::process_fee_lock(&BOB).unwrap();
1142+
assert_eq!(UnlockQueue::<Test>::get(0), Some(ALICE));
1143+
assert_eq!(UnlockQueue::<Test>::get(1), Some(BOB));
1144+
assert_eq!(Tokens::accounts(BOB, NativeCurrencyId::get()), ACCOUNT_WITH_LOCKED_TOKENS);
1145+
1146+
fast_forward_blocks(PERIOD_LENGTH);
1147+
FeeLock::on_idle(System::block_number(), Weight::from_ref_time(u64::MAX));
1148+
1149+
assert_eq!(
1150+
Tokens::accounts(ALICE, NativeCurrencyId::get()),
1151+
ACCOUNT_WITHOUT_LOCKED_TOKENS
1152+
);
1153+
assert_eq!(
1154+
Tokens::accounts(BOB, NativeCurrencyId::get()),
1155+
ACCOUNT_WITHOUT_LOCKED_TOKENS
1156+
);
1157+
1158+
assert_eq!(UnlockQueue::<Test>::get(0), None);
1159+
assert_eq!(UnlockQueue::<Test>::get(1), None);
1160+
});
1161+
}

runtime/mangata-kusama/src/weights/pallet_fee_lock.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
//! Autogenerated weights for pallet_fee_lock
2020
//!
2121
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
22-
//! DATE: 2023-02-13, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
23-
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024
22+
//! DATE: 2023-04-06, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
23+
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("kusama"), DB CACHE: 1024
2424
2525
// Executed Command:
2626
// target/release/mangata-node
2727
// benchmark
2828
// pallet
2929
// -l=info,xyk=error,collective-mangata=warn,bootstrap=warn
3030
// --chain
31-
// dev
31+
// kusama
3232
// --execution
3333
// wasm
3434
// --wasm-execution
@@ -65,34 +65,38 @@ pub struct ModuleWeight<T>(PhantomData<T>);
6565
impl<T: frame_system::Config> pallet_fee_lock::WeightInfo for ModuleWeight<T> {
6666
// Storage: FeeLock FeeLockMetadata (r:1 w:1)
6767
fn update_fee_lock_metadata() -> Weight {
68-
(Weight::from_ref_time(33_569_000))
68+
(Weight::from_ref_time(37_491_000))
6969
.saturating_add(T::DbWeight::get().reads(1 as u64))
7070
.saturating_add(T::DbWeight::get().writes(1 as u64))
7171
}
7272
// Storage: FeeLock AccountFeeLockData (r:1 w:1)
7373
// Storage: FeeLock FeeLockMetadata (r:1 w:0)
7474
// Storage: Tokens Accounts (r:1 w:1)
75+
// Storage: FeeLock FeeLockMetadataQeueuePosition (r:1 w:1)
76+
// Storage: FeeLock UnlockQueue (r:1 w:1)
7577
fn unlock_fee() -> Weight {
76-
(Weight::from_ref_time(48_631_000))
77-
.saturating_add(T::DbWeight::get().reads(3 as u64))
78-
.saturating_add(T::DbWeight::get().writes(2 as u64))
78+
(Weight::from_ref_time(58_080_000))
79+
.saturating_add(T::DbWeight::get().reads(5 as u64))
80+
.saturating_add(T::DbWeight::get().writes(4 as u64))
7981
}
8082
}
8183

8284
// For backwards compatibility and tests
8385
impl WeightInfo for () {
8486
// Storage: FeeLock FeeLockMetadata (r:1 w:1)
8587
fn update_fee_lock_metadata() -> Weight {
86-
(Weight::from_ref_time(33_569_000))
88+
(Weight::from_ref_time(37_491_000))
8789
.saturating_add(RocksDbWeight::get().reads(1 as u64))
8890
.saturating_add(RocksDbWeight::get().writes(1 as u64))
8991
}
9092
// Storage: FeeLock AccountFeeLockData (r:1 w:1)
9193
// Storage: FeeLock FeeLockMetadata (r:1 w:0)
9294
// Storage: Tokens Accounts (r:1 w:1)
95+
// Storage: FeeLock FeeLockMetadataQeueuePosition (r:1 w:1)
96+
// Storage: FeeLock UnlockQueue (r:1 w:1)
9397
fn unlock_fee() -> Weight {
94-
(Weight::from_ref_time(48_631_000))
95-
.saturating_add(RocksDbWeight::get().reads(3 as u64))
96-
.saturating_add(RocksDbWeight::get().writes(2 as u64))
98+
(Weight::from_ref_time(58_080_000))
99+
.saturating_add(RocksDbWeight::get().reads(5 as u64))
100+
.saturating_add(RocksDbWeight::get().writes(4 as u64))
97101
}
98102
}

runtime/mangata-rococo/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
164164
spec_name: create_runtime_str!("mangata-parachain"),
165165
impl_name: create_runtime_str!("mangata-parachain"),
166166
authoring_version: 14,
167-
spec_version: 2802,
167+
spec_version: 002900,
168168
impl_version: 0,
169169
apis: RUNTIME_API_VERSIONS,
170-
transaction_version: 2802,
170+
transaction_version: 002900,
171171
state_version: 0,
172172
};
173173

@@ -178,10 +178,10 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
178178
impl_name: create_runtime_str!("mangata-parachain"),
179179

180180
authoring_version: 14,
181-
spec_version: 002900,
181+
spec_version: 002901,
182182
impl_version: 0,
183183
apis: RUNTIME_API_VERSIONS,
184-
transaction_version: 002900,
184+
transaction_version: 002901,
185185
state_version: 0,
186186
};
187187

runtime/mangata-rococo/src/weights/pallet_fee_lock.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
//! Autogenerated weights for pallet_fee_lock
2020
//!
2121
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
22-
//! DATE: 2023-02-13, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
23-
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024
22+
//! DATE: 2023-04-06, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
23+
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("kusama"), DB CACHE: 1024
2424
2525
// Executed Command:
2626
// target/release/mangata-node
2727
// benchmark
2828
// pallet
2929
// -l=info,xyk=error,collective-mangata=warn,bootstrap=warn
3030
// --chain
31-
// dev
31+
// kusama
3232
// --execution
3333
// wasm
3434
// --wasm-execution
@@ -65,34 +65,38 @@ pub struct ModuleWeight<T>(PhantomData<T>);
6565
impl<T: frame_system::Config> pallet_fee_lock::WeightInfo for ModuleWeight<T> {
6666
// Storage: FeeLock FeeLockMetadata (r:1 w:1)
6767
fn update_fee_lock_metadata() -> Weight {
68-
(Weight::from_ref_time(33_569_000))
68+
(Weight::from_ref_time(37_491_000))
6969
.saturating_add(T::DbWeight::get().reads(1 as u64))
7070
.saturating_add(T::DbWeight::get().writes(1 as u64))
7171
}
7272
// Storage: FeeLock AccountFeeLockData (r:1 w:1)
7373
// Storage: FeeLock FeeLockMetadata (r:1 w:0)
7474
// Storage: Tokens Accounts (r:1 w:1)
75+
// Storage: FeeLock FeeLockMetadataQeueuePosition (r:1 w:1)
76+
// Storage: FeeLock UnlockQueue (r:1 w:1)
7577
fn unlock_fee() -> Weight {
76-
(Weight::from_ref_time(48_631_000))
77-
.saturating_add(T::DbWeight::get().reads(3 as u64))
78-
.saturating_add(T::DbWeight::get().writes(2 as u64))
78+
(Weight::from_ref_time(58_080_000))
79+
.saturating_add(T::DbWeight::get().reads(5 as u64))
80+
.saturating_add(T::DbWeight::get().writes(4 as u64))
7981
}
8082
}
8183

8284
// For backwards compatibility and tests
8385
impl WeightInfo for () {
8486
// Storage: FeeLock FeeLockMetadata (r:1 w:1)
8587
fn update_fee_lock_metadata() -> Weight {
86-
(Weight::from_ref_time(33_569_000))
88+
(Weight::from_ref_time(37_491_000))
8789
.saturating_add(RocksDbWeight::get().reads(1 as u64))
8890
.saturating_add(RocksDbWeight::get().writes(1 as u64))
8991
}
9092
// Storage: FeeLock AccountFeeLockData (r:1 w:1)
9193
// Storage: FeeLock FeeLockMetadata (r:1 w:0)
9294
// Storage: Tokens Accounts (r:1 w:1)
95+
// Storage: FeeLock FeeLockMetadataQeueuePosition (r:1 w:1)
96+
// Storage: FeeLock UnlockQueue (r:1 w:1)
9397
fn unlock_fee() -> Weight {
94-
(Weight::from_ref_time(48_631_000))
95-
.saturating_add(RocksDbWeight::get().reads(3 as u64))
96-
.saturating_add(RocksDbWeight::get().writes(2 as u64))
98+
(Weight::from_ref_time(58_080_000))
99+
.saturating_add(RocksDbWeight::get().reads(5 as u64))
100+
.saturating_add(RocksDbWeight::get().writes(4 as u64))
97101
}
98102
}

0 commit comments

Comments
 (0)