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

CheckWeight: Add more logging #1996

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion substrate/frame/support/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ impl<Call: Encode + GetDispatchInfo, Extra: Encode> GetDispatchInfo
}

/// A struct holding value for each `DispatchClass`.
#[derive(Clone, Eq, PartialEq, Default, RuntimeDebug, Encode, Decode, TypeInfo, MaxEncodedLen)]
#[derive(Clone, Eq, PartialEq, Default, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub struct PerDispatchClass<T> {
/// Value for `Normal` extrinsics.
normal: T,
Expand Down
64 changes: 54 additions & 10 deletions substrate/frame/system/src/extensions/check_weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{limits::BlockWeights, Config, Pallet};
use crate::{limits::BlockWeights, Config, Pallet, LOG_TARGET};
use codec::{Decode, Encode};
use frame_support::{
dispatch::{DispatchInfo, PostDispatchInfo},
Expand Down Expand Up @@ -50,8 +50,16 @@ where
) -> Result<(), TransactionValidityError> {
let max = T::BlockWeights::get().get(info.class).max_extrinsic;
match max {
Some(max) if info.weight.any_gt(max) =>
Err(InvalidTransaction::ExhaustsResources.into()),
Some(max) if info.weight.any_gt(max) => {
log::debug!(
target: LOG_TARGET,
"Extrinsic {} is greater than the max extrinsic {}",
info.weight,
max,
);

Err(InvalidTransaction::ExhaustsResources.into())
},
_ => Ok(()),
}
}
Expand Down Expand Up @@ -79,6 +87,13 @@ where
let added_len = len as u32;
let next_len = current_len.saturating_add(added_len);
if next_len > *length_limit.max.get(info.class) {
log::debug!(
target: LOG_TARGET,
"Exceeded block length limit: {} > {}",
next_len,
length_limit.max.get(info.class),
);

Err(InvalidTransaction::ExhaustsResources.into())
} else {
Ok(next_len)
Expand Down Expand Up @@ -137,17 +152,28 @@ where
if limit_per_class.max_total.is_none() && limit_per_class.reserved.is_none() {
all_weight.accrue(extrinsic_weight, info.class)
} else {
all_weight
.checked_accrue(extrinsic_weight, info.class)
.map_err(|_| InvalidTransaction::ExhaustsResources)?;
all_weight.checked_accrue(extrinsic_weight, info.class).map_err(|_| {
log::debug!(
target: LOG_TARGET,
"All weight checked add overflow.",
);

InvalidTransaction::ExhaustsResources
})?;
}

let per_class = *all_weight.get(info.class);

// Check if we don't exceed per-class allowance
match limit_per_class.max_total {
Some(max) if per_class.any_gt(max) =>
return Err(InvalidTransaction::ExhaustsResources.into()),
Some(max) if per_class.any_gt(max) => {
log::debug!(
target: LOG_TARGET,
"Exceeded the per-class allowance.",
);

return Err(InvalidTransaction::ExhaustsResources.into())
},
// There is no `max_total` limit (`None`),
// or we are below the limit.
_ => {},
Expand All @@ -158,8 +184,14 @@ where
if all_weight.total().any_gt(maximum_weight.max_block) {
match limit_per_class.reserved {
// We are over the limit in reserved pool.
Some(reserved) if per_class.any_gt(reserved) =>
return Err(InvalidTransaction::ExhaustsResources.into()),
Some(reserved) if per_class.any_gt(reserved) => {
log::debug!(
target: LOG_TARGET,
"Total block weight is exceeded.",
);

return Err(InvalidTransaction::ExhaustsResources.into())
},
// There is either no limit in reserved pool (`None`),
// or we are below the limit.
_ => {},
Expand Down Expand Up @@ -233,6 +265,18 @@ where
})
}

log::trace!(
target: LOG_TARGET,
"Used block weight: {:?}",
crate::BlockWeight::<T>::get(),
);

log::trace!(
target: LOG_TARGET,
"Used block length: {:?}",
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
Pallet::<T>::all_extrinsics_len(),
);

Ok(())
}
}
Expand Down
5 changes: 1 addition & 4 deletions substrate/primitives/weights/src/weight_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
use codec::{Decode, Encode, MaxEncodedLen};
use core::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};
use sp_arithmetic::traits::{Bounded, CheckedAdd, CheckedSub, Zero};
use sp_debug_derive::RuntimeDebug;

use super::*;

#[derive(
Encode, Decode, MaxEncodedLen, TypeInfo, Eq, PartialEq, Copy, Clone, RuntimeDebug, Default,
)]
#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Eq, PartialEq, Copy, Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Weight {
#[codec(compact)]
Expand Down