Skip to content

Commit 766aaf3

Browse files
committed
Clamp the precision above just in case tokens with denominations more than 41 occur.
1 parent e281104 commit 766aaf3

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

crates/shielded_token/src/conversion.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ where
107107
storage.read(&reward_precision_key)?.map_or_else(
108108
|| -> Result<u128> {
109109
// Since reading reward precision has failed, choose a
110-
// thousandth of the given token
110+
// thousandth of the given token. But clamp the precision above
111+
// by 10^38, the maximum power of 10 that can be contained by a
112+
// u128.
111113
let precision_denom =
112-
std::cmp::max(u32::from(denomination.0), 3)
113-
.checked_sub(3)
114-
.expect("Cannot underflow");
114+
u32::from(denomination.0).saturating_sub(3).clamp(0, 38);
115115
let reward_precision = checked!(10u128 ^ precision_denom)?;
116116
// Record the precision that is now being used so that it does
117117
// not have to be recomputed each time, and to

0 commit comments

Comments
 (0)