From 062410c137b7293e1c52ed9c9ece4d4bb71bc147 Mon Sep 17 00:00:00 2001 From: gui Date: Wed, 7 Aug 2024 20:25:54 +0900 Subject: [PATCH 1/4] Fix --- .../storage-weight-reclaim/src/lib.rs | 48 +++++++++++++++++-- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/cumulus/primitives/storage-weight-reclaim/src/lib.rs b/cumulus/primitives/storage-weight-reclaim/src/lib.rs index f48dd927ee96..d480f6b3a2b1 100644 --- a/cumulus/primitives/storage-weight-reclaim/src/lib.rs +++ b/cumulus/primitives/storage-weight-reclaim/src/lib.rs @@ -165,15 +165,14 @@ where ); return Ok(()) }; - let benchmarked_weight = info.weight.proof_size(); - let consumed_weight = post_dispatch_proof_size.saturating_sub(pre_dispatch_proof_size); - // Unspent weight according to the `actual_weight` from `PostDispatchInfo` // This unspent weight will be refunded by the `CheckWeight` extension, so we need to // account for that. let unspent = post_info.calc_unspent(info).proof_size(); - let storage_size_diff = - benchmarked_weight.saturating_sub(unspent).abs_diff(consumed_weight as u64); + let benchmarked_weight = info.weight.proof_size().saturating_sub(unspent); + let consumed_weight = post_dispatch_proof_size.saturating_sub(pre_dispatch_proof_size); + + let storage_size_diff = benchmarked_weight.abs_diff(consumed_weight as u64); // This value will be reclaimed by [`frame_system::CheckWeight`], so we need to calculate // that in. @@ -294,6 +293,45 @@ mod tests { }) } + #[test] + fn underestimating_refund() { + // We fixed a bug where weight info weight > actually consumed weight > post info weight + // resulted in error. + + // The real cost will be 100 bytes of storage size + let mut test_ext = setup_test_externalities(&[0, 100]); + + test_ext.execute_with(|| { + set_current_storage_weight(1000); + + // Benchmarked storage weight: 500 + let info = DispatchInfo { weight: Weight::from_parts(0, 101), ..Default::default() }; + let post_info = PostDispatchInfo { + actual_weight: Some(Weight::from_parts(0, 99)), + pays_fee: Default::default(), + }; + + assert_ok!(CheckWeight::::do_pre_dispatch(&info, LEN)); + + let pre = StorageWeightReclaim::(PhantomData) + .pre_dispatch(&ALICE, CALL, &info, LEN) + .unwrap(); + assert_eq!(pre, Some(0)); + + assert_ok!(CheckWeight::::post_dispatch(None, &info, &post_info, 0, &Ok(()))); + // We expect an accrue of 1 + assert_ok!(StorageWeightReclaim::::post_dispatch( + Some(pre), + &info, + &post_info, + LEN, + &Ok(()) + )); + + assert_eq!(get_storage_weight().total().proof_size(), 1250); + }) + } + #[test] fn does_nothing_without_extension() { let mut test_ext = new_test_ext(); From 1c22eafedc86fc10ca5e5286673ca08cd630f5a2 Mon Sep 17 00:00:00 2001 From: gui Date: Wed, 7 Aug 2024 20:32:16 +0900 Subject: [PATCH 2/4] comment --- cumulus/primitives/storage-weight-reclaim/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cumulus/primitives/storage-weight-reclaim/src/lib.rs b/cumulus/primitives/storage-weight-reclaim/src/lib.rs index d480f6b3a2b1..5984fa77a2c5 100644 --- a/cumulus/primitives/storage-weight-reclaim/src/lib.rs +++ b/cumulus/primitives/storage-weight-reclaim/src/lib.rs @@ -295,7 +295,7 @@ mod tests { #[test] fn underestimating_refund() { - // We fixed a bug where weight info weight > actually consumed weight > post info weight + // We fixed a bug where `pre dispatch info weight > consumed weight > post info weight` // resulted in error. // The real cost will be 100 bytes of storage size From 23736e3c7ce1c716d5cd1d9f7aa2fab5201f9e94 Mon Sep 17 00:00:00 2001 From: gui Date: Wed, 7 Aug 2024 20:40:10 +0900 Subject: [PATCH 3/4] prdoc --- prdoc/pr_5273.prdoc | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 prdoc/pr_5273.prdoc diff --git a/prdoc/pr_5273.prdoc b/prdoc/pr_5273.prdoc new file mode 100644 index 000000000000..c67b83480944 --- /dev/null +++ b/prdoc/pr_5273.prdoc @@ -0,0 +1,10 @@ +title: Fix storage weight reclaim bug. + +doc: + - audience: Runtime Dev + description: | + A bug in storage weight reclaim signed extension is fixed. The bug was causing an underestimate of the proof size when the post dispatch info was underestimating the proof size and the pre dispatch info was overestimating the proof size at the same time. + +crates: + - name: cumulus_primitives_storage_weight_reclaim + bump: patch From 0966cd981b6a825ddfa1e246201d6a4fc263ea01 Mon Sep 17 00:00:00 2001 From: gui Date: Wed, 7 Aug 2024 20:57:53 +0900 Subject: [PATCH 4/4] fix prdoc --- prdoc/pr_5273.prdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prdoc/pr_5273.prdoc b/prdoc/pr_5273.prdoc index c67b83480944..981172c6c13f 100644 --- a/prdoc/pr_5273.prdoc +++ b/prdoc/pr_5273.prdoc @@ -6,5 +6,5 @@ doc: A bug in storage weight reclaim signed extension is fixed. The bug was causing an underestimate of the proof size when the post dispatch info was underestimating the proof size and the pre dispatch info was overestimating the proof size at the same time. crates: - - name: cumulus_primitives_storage_weight_reclaim + - name: cumulus-primitives-storage-weight-reclaim bump: patch