Skip to content

Commit 46fe7ab

Browse files
committed
add logging to checked mul and div
1 parent 6b09787 commit 46fe7ab

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

runtime/src/lib.rs

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,30 +1201,50 @@ pub struct SubtensorEvmBalanceConverter;
12011201
impl BalanceConverter for SubtensorEvmBalanceConverter {
12021202
/// Convert from Substrate balance (u64) to EVM balance (U256)
12031203
fn into_evm_balance(value: U256) -> Option<U256> {
1204-
value
1205-
.checked_mul(U256::from(EVM_TO_SUBSTRATE_DECIMALS))
1206-
.and_then(|evm_value| {
1207-
// Ensure the result fits within the maximum U256 value
1208-
if evm_value <= U256::MAX {
1209-
Some(evm_value)
1210-
} else {
1211-
None
1212-
}
1213-
})
1204+
if let Some(evm_value) = value.checked_mul(U256::from(EVM_TO_SUBSTRATE_DECIMALS)) {
1205+
// Ensure the result fits within the maximum U256 value
1206+
if evm_value <= U256::MAX {
1207+
Some(evm_value)
1208+
} else {
1209+
// Log value too large
1210+
log::debug!(
1211+
"SubtensorEvmBalanceConverter::into_evm_balance( {:?} ) larger than U256::MAX",
1212+
value
1213+
);
1214+
None
1215+
}
1216+
} else {
1217+
// Log overflow
1218+
log::debug!(
1219+
"SubtensorEvmBalanceConverter::into_evm_balance( {:?} ) overflow",
1220+
value
1221+
);
1222+
None
1223+
}
12141224
}
12151225

12161226
/// Convert from EVM balance (U256) to Substrate balance (u64)
12171227
fn into_substrate_balance(value: U256) -> Option<U256> {
1218-
value
1219-
.checked_div(U256::from(EVM_TO_SUBSTRATE_DECIMALS))
1220-
.and_then(|substrate_value| {
1221-
// Ensure the result fits within the TAO balance type (u64)
1222-
if substrate_value <= U256::from(u64::MAX) {
1223-
Some(substrate_value)
1224-
} else {
1225-
None
1226-
}
1227-
})
1228+
if let Some(substrate_value) = value.checked_div(U256::from(EVM_TO_SUBSTRATE_DECIMALS)) {
1229+
// Ensure the result fits within the TAO balance type (u64)
1230+
if substrate_value <= U256::from(u64::MAX) {
1231+
Some(substrate_value)
1232+
} else {
1233+
// Log value too large
1234+
log::debug!(
1235+
"SubtensorEvmBalanceConverter::into_substrate_balance( {:?} ) larger than u64::MAX",
1236+
value
1237+
);
1238+
None
1239+
}
1240+
} else {
1241+
// Log overflow
1242+
log::debug!(
1243+
"SubtensorEvmBalanceConverter::into_substrate_balance( {:?} ) overflow",
1244+
value
1245+
);
1246+
None
1247+
}
12281248
}
12291249
}
12301250

@@ -2151,7 +2171,8 @@ fn test_into_substrate_balance_large_value() {
21512171
#[test]
21522172
fn test_into_substrate_balance_exceeds_u64() {
21532173
// EVM balance that exceeds u64 after conversion
2154-
let evm_balance = (U256::from(u64::MAX) + U256::from(1)) * U256::from(EVM_TO_SUBSTRATE_DECIMALS);
2174+
let evm_balance =
2175+
(U256::from(u64::MAX) + U256::from(1)) * U256::from(EVM_TO_SUBSTRATE_DECIMALS);
21552176

21562177
let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance);
21572178
assert_eq!(result, None); // Exceeds u64, should return None

0 commit comments

Comments
 (0)