Skip to content

Commit

Permalink
return more accurate unspent from storage reclaim tx ext
Browse files Browse the repository at this point in the history
  • Loading branch information
gui1117 committed Nov 6, 2024
1 parent 472d63c commit 597fde4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
14 changes: 6 additions & 8 deletions cumulus/pallets/weight-reclaim/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,21 +225,19 @@ where
}

let accurate_weight = benchmarked_actual_weight.set_proof_size(measured_proof_size);

frame_system::BlockWeight::<T>::mutate(|current_weight| {
let already_reclaimed = frame_system::ExtrinsicWeightReclaimed::<T>::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::<T>::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::<T>::put(accurate_unspent);
Ok(accurate_unspent)
}

fn bare_validate(
Expand Down
20 changes: 14 additions & 6 deletions cumulus/pallets/weight-reclaim/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
Expand Down Expand Up @@ -532,11 +533,14 @@ fn full_basic_refund() {
let post_info = extrinsic.apply::<Test>(&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(),
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 597fde4

Please sign in to comment.