Skip to content

Commit a5a2432

Browse files
authored
CheckWeight: Add more logging (#1996)
This adds more logging to `CheckWeight` to get a better understanding why a transaction exhausts resources.
1 parent 39c04fd commit a5a2432

File tree

3 files changed

+56
-15
lines changed

3 files changed

+56
-15
lines changed

substrate/frame/support/src/dispatch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ impl<Call: Encode + GetDispatchInfo, Extra: Encode> GetDispatchInfo
390390
}
391391

392392
/// A struct holding value for each `DispatchClass`.
393-
#[derive(Clone, Eq, PartialEq, Default, RuntimeDebug, Encode, Decode, TypeInfo, MaxEncodedLen)]
393+
#[derive(Clone, Eq, PartialEq, Default, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
394394
pub struct PerDispatchClass<T> {
395395
/// Value for `Normal` extrinsics.
396396
normal: T,

substrate/frame/system/src/extensions/check_weight.rs

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
1717

18-
use crate::{limits::BlockWeights, Config, Pallet};
18+
use crate::{limits::BlockWeights, Config, Pallet, LOG_TARGET};
1919
use codec::{Decode, Encode};
2020
use frame_support::{
2121
dispatch::{DispatchInfo, PostDispatchInfo},
@@ -50,8 +50,16 @@ where
5050
) -> Result<(), TransactionValidityError> {
5151
let max = T::BlockWeights::get().get(info.class).max_extrinsic;
5252
match max {
53-
Some(max) if info.weight.any_gt(max) =>
54-
Err(InvalidTransaction::ExhaustsResources.into()),
53+
Some(max) if info.weight.any_gt(max) => {
54+
log::debug!(
55+
target: LOG_TARGET,
56+
"Extrinsic {} is greater than the max extrinsic {}",
57+
info.weight,
58+
max,
59+
);
60+
61+
Err(InvalidTransaction::ExhaustsResources.into())
62+
},
5563
_ => Ok(()),
5664
}
5765
}
@@ -79,6 +87,13 @@ where
7987
let added_len = len as u32;
8088
let next_len = current_len.saturating_add(added_len);
8189
if next_len > *length_limit.max.get(info.class) {
90+
log::debug!(
91+
target: LOG_TARGET,
92+
"Exceeded block length limit: {} > {}",
93+
next_len,
94+
length_limit.max.get(info.class),
95+
);
96+
8297
Err(InvalidTransaction::ExhaustsResources.into())
8398
} else {
8499
Ok(next_len)
@@ -137,17 +152,28 @@ where
137152
if limit_per_class.max_total.is_none() && limit_per_class.reserved.is_none() {
138153
all_weight.accrue(extrinsic_weight, info.class)
139154
} else {
140-
all_weight
141-
.checked_accrue(extrinsic_weight, info.class)
142-
.map_err(|_| InvalidTransaction::ExhaustsResources)?;
155+
all_weight.checked_accrue(extrinsic_weight, info.class).map_err(|_| {
156+
log::debug!(
157+
target: LOG_TARGET,
158+
"All weight checked add overflow.",
159+
);
160+
161+
InvalidTransaction::ExhaustsResources
162+
})?;
143163
}
144164

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

147167
// Check if we don't exceed per-class allowance
148168
match limit_per_class.max_total {
149-
Some(max) if per_class.any_gt(max) =>
150-
return Err(InvalidTransaction::ExhaustsResources.into()),
169+
Some(max) if per_class.any_gt(max) => {
170+
log::debug!(
171+
target: LOG_TARGET,
172+
"Exceeded the per-class allowance.",
173+
);
174+
175+
return Err(InvalidTransaction::ExhaustsResources.into())
176+
},
151177
// There is no `max_total` limit (`None`),
152178
// or we are below the limit.
153179
_ => {},
@@ -158,8 +184,14 @@ where
158184
if all_weight.total().any_gt(maximum_weight.max_block) {
159185
match limit_per_class.reserved {
160186
// We are over the limit in reserved pool.
161-
Some(reserved) if per_class.any_gt(reserved) =>
162-
return Err(InvalidTransaction::ExhaustsResources.into()),
187+
Some(reserved) if per_class.any_gt(reserved) => {
188+
log::debug!(
189+
target: LOG_TARGET,
190+
"Total block weight is exceeded.",
191+
);
192+
193+
return Err(InvalidTransaction::ExhaustsResources.into())
194+
},
163195
// There is either no limit in reserved pool (`None`),
164196
// or we are below the limit.
165197
_ => {},
@@ -233,6 +265,18 @@ where
233265
})
234266
}
235267

268+
log::trace!(
269+
target: LOG_TARGET,
270+
"Used block weight: {:?}",
271+
crate::BlockWeight::<T>::get(),
272+
);
273+
274+
log::trace!(
275+
target: LOG_TARGET,
276+
"Used block length: {:?}",
277+
Pallet::<T>::all_extrinsics_len(),
278+
);
279+
236280
Ok(())
237281
}
238282
}

substrate/primitives/weights/src/weight_v2.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@
1818
use codec::{Decode, Encode, MaxEncodedLen};
1919
use core::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};
2020
use sp_arithmetic::traits::{Bounded, CheckedAdd, CheckedSub, Zero};
21-
use sp_debug_derive::RuntimeDebug;
2221

2322
use super::*;
2423

25-
#[derive(
26-
Encode, Decode, MaxEncodedLen, TypeInfo, Eq, PartialEq, Copy, Clone, RuntimeDebug, Default,
27-
)]
24+
#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Eq, PartialEq, Copy, Clone, Debug, Default)]
2825
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2926
pub struct Weight {
3027
#[codec(compact)]

0 commit comments

Comments
 (0)