Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StorageWeightReclaim: set to node pov size if higher #5281

Merged
merged 9 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
57 changes: 54 additions & 3 deletions cumulus/primitives/storage-weight-reclaim/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,25 @@ where
let storage_size_diff =
benchmarked_weight.saturating_sub(unspent).abs_diff(consumed_weight as u64);

let extrinsic_len = frame_system::AllExtrinsicsLen::<T>::get().unwrap_or(0);
let node_side_pov_size = post_dispatch_proof_size.saturating_add(extrinsic_len.into());

// This value will be reclaimed by [`frame_system::CheckWeight`], so we need to calculate
// that in.
frame_system::BlockWeight::<T>::mutate(|current| {
if consumed_weight > benchmarked_weight {
let block_weight_proof_size = current.total().proof_size();

// If we encounter a situation where the node-side proof size is already higher than
// what we have in the runtime bookkeeping, we add the difference to the `BlockWeight`.
// This prevents that the proof size grows faster than the runtime proof size.
if node_side_pov_size > block_weight_proof_size {
skunert marked this conversation as resolved.
Show resolved Hide resolved
let missing = node_side_pov_size.saturating_sub(block_weight_proof_size);
current.accrue(Weight::from_parts(0, missing), info.class);
log::warn!(
target: LOG_TARGET,
"Node-side PoV size higher than runtime proof size weight. node-side: {node_side_pov_size} extrinsic_len: {extrinsic_len} runtime: {block_weight_proof_size}, missing: {missing}. Setting to node-side proof size."
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good indeed.

I also think we could formalize the concept of weight reservation. So that on_initialize can reserve some weight for on_finalize.
I believe that extrinsic should also be able to reserve some weight if they trigger some logic in on_finalize.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think we could formalize the concept of weight reservation. So that on_initialize can reserve some weight for on_finalize.
I believe that extrinsic should also be able to reserve some weight if they trigger some logic in on_finalize.

I don't have a full overview of all the implications of this. But for sure it would help making the math here more accurate. This is one of the reasons why we need to work with the diffs instead of just setting the proof size to the node variant in the first place.

} else if consumed_weight > benchmarked_weight {
log::error!(
target: LOG_TARGET,
"Benchmarked storage weight smaller than consumed storage weight. benchmarked: {benchmarked_weight} consumed: {consumed_weight} unspent: {unspent}"
Expand Down Expand Up @@ -294,6 +309,42 @@ mod tests {
})
}

#[test]
fn sets_to_node_storage_proof_if_higher() {
// The storage proof reported by the proof recorder is higher than what is stored on
// the runtime side.
let mut test_ext = setup_test_externalities(&[1000, 1005]);

test_ext.execute_with(|| {
// Stored in BlockWeight is 5
set_current_storage_weight(5);

// Benchmarked storage weight: 10
let info = DispatchInfo { weight: Weight::from_parts(0, 10), ..Default::default() };
let post_info = PostDispatchInfo::default();

// Should add 10 + 150 (len) to weight.
assert_ok!(CheckWeight::<Test>::do_pre_dispatch(&info, LEN));

let pre = StorageWeightReclaim::<Test>(PhantomData)
.pre_dispatch(&ALICE, CALL, &info, LEN)
.unwrap();
assert_eq!(pre, Some(1000));

assert_ok!(CheckWeight::<Test>::post_dispatch(None, &info, &post_info, 0, &Ok(())));
// We expect a refund of 400
assert_ok!(StorageWeightReclaim::<Test>::post_dispatch(
Some(pre),
&info,
&post_info,
LEN,
&Ok(())
));

assert_eq!(get_storage_weight().total().proof_size(), 1155);
})
}

#[test]
fn does_nothing_without_extension() {
let mut test_ext = new_test_ext();
Expand Down Expand Up @@ -507,7 +558,7 @@ mod tests {

#[test]
fn test_nothing_relcaimed() {
let mut test_ext = setup_test_externalities(&[100, 200]);
let mut test_ext = setup_test_externalities(&[0, 100]);

test_ext.execute_with(|| {
set_current_storage_weight(0);
Expand All @@ -530,7 +581,7 @@ mod tests {
.pre_dispatch(&ALICE, CALL, &info, LEN)
.unwrap();
// Should return `setup_test_externalities` proof recorder value: 100.
assert_eq!(pre, Some(100));
assert_eq!(pre, Some(0));

// The `CheckWeight` extension will refund `actual_weight` from `PostDispatchInfo`
// we always need to call `post_dispatch` to verify that they interoperate correctly.
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ pub mod pallet {

/// Total length (in bytes) for all extrinsics put together, for the current block.
#[pallet::storage]
pub(super) type AllExtrinsicsLen<T: Config> = StorageValue<_, u32>;
pub type AllExtrinsicsLen<T: Config> = StorageValue<_, u32>;

/// Map of block numbers to block hashes.
#[pallet::storage]
Expand Down
Loading