Skip to content

Commit

Permalink
CheckWeight: Add more logging (paritytech#1996)
Browse files Browse the repository at this point in the history
This adds more logging to `CheckWeight` to get a better understanding
why a transaction exhausts resources.
  • Loading branch information
bkchr committed Oct 24, 2023
1 parent 98f58d4 commit c3a6bd6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
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: {:?}",
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

0 comments on commit c3a6bd6

Please sign in to comment.