From 597fde45aed898df8880eaaed0dedcee0255ef56 Mon Sep 17 00:00:00 2001 From: gui Date: Wed, 6 Nov 2024 11:51:16 +0900 Subject: [PATCH] return more accurate unspent from storage reclaim tx ext --- cumulus/pallets/weight-reclaim/src/lib.rs | 14 ++++++-------- cumulus/pallets/weight-reclaim/src/tests.rs | 20 ++++++++++++++------ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/cumulus/pallets/weight-reclaim/src/lib.rs b/cumulus/pallets/weight-reclaim/src/lib.rs index 5cdc4abefba3..2f4ce19f3e94 100644 --- a/cumulus/pallets/weight-reclaim/src/lib.rs +++ b/cumulus/pallets/weight-reclaim/src/lib.rs @@ -225,21 +225,19 @@ where } let accurate_weight = benchmarked_actual_weight.set_proof_size(measured_proof_size); - frame_system::BlockWeight::::mutate(|current_weight| { let already_reclaimed = frame_system::ExtrinsicWeightReclaimed::::get(); current_weight.accrue(already_reclaimed, info.class); current_weight.reduce(info.total_weight(), info.class); current_weight.accrue(accurate_weight, info.class); - - // The saturation will happen if the pre dispatch weight is underestimating the proof - // size. - // In this case the extrinsic proof size weight reclaimed is 0. - let accurate_unspent = info.total_weight().saturating_sub(accurate_weight); - frame_system::ExtrinsicWeightReclaimed::::put(accurate_unspent); }); - Ok(inner_refund) + // The saturation will happen if the pre dispatch weight is underestimating the proof + // size. + // In this case the extrinsic proof size weight reclaimed is 0. + let accurate_unspent = info.total_weight().saturating_sub(accurate_weight); + frame_system::ExtrinsicWeightReclaimed::::put(accurate_unspent); + Ok(accurate_unspent) } fn bare_validate( diff --git a/cumulus/pallets/weight-reclaim/src/tests.rs b/cumulus/pallets/weight-reclaim/src/tests.rs index 16a32f536a4e..990c515b8cd4 100644 --- a/cumulus/pallets/weight-reclaim/src/tests.rs +++ b/cumulus/pallets/weight-reclaim/src/tests.rs @@ -250,6 +250,7 @@ fn basic_refund() { assert_ok!(Tx::post_dispatch(pre, &info, &mut post_info, LEN, &Ok(()))); + // TODO TODO: assert_eq!(post_info.actual_weight, Weight::from_parts(0, 650)); assert_eq!(get_storage_weight().proof_size(), 1250); }); } @@ -532,11 +533,14 @@ fn full_basic_refund() { let post_info = extrinsic.apply::(&info, LEN).unwrap().unwrap(); // Assertions: - let post_info_tx_proof_size = - check_weight + storage_weight_reclaim + mock_ext - mock_ext_refund; assert_eq!( - post_info.actual_weight, - Some(call_info.call_weight + Weight::from_parts(3, post_info_tx_proof_size)) + post_info.actual_weight.unwrap().ref_time(), + call_info.call_weight.ref_time() + 3, + ); + assert_eq!( + post_info.actual_weight.unwrap().proof_size(), + // LEN is part of the base extrinsic, not the post info weight actual weight. + actual_used_proof_size as u64, ); assert_eq!( get_storage_weight().proof_size(), @@ -578,8 +582,12 @@ fn full_accrue() { let post_info_tx_proof_size = check_weight + storage_weight_reclaim + mock_ext - mock_ext_refund; assert_eq!( - post_info.actual_weight, - Some(call_info.call_weight + Weight::from_parts(3, post_info_tx_proof_size)) + post_info.actual_weight.unwrap().ref_time(), + call_info.call_weight.ref_time() + 3, + ); + assert_eq!( + post_info.actual_weight.unwrap().proof_size(), + info.total_weight().proof_size(), // The post info doesn't get the accrue. ); assert_eq!( get_storage_weight().proof_size(),