Skip to content

Commit 67b798e

Browse files
committed
final fix + tests
1 parent de2049d commit 67b798e

File tree

1 file changed

+82
-6
lines changed

1 file changed

+82
-6
lines changed

runtime/src/lib.rs

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,15 +1121,26 @@ const EVM_DECIMALS_FACTOR: u64 = 1_000_000_000_u64;
11211121
pub struct SubtensorEvmBalanceConverter;
11221122

11231123
impl BalanceConverter for SubtensorEvmBalanceConverter {
1124+
/// Convert from Substrate balance (u64) to EVM balance (U256)
11241125
fn into_evm_balance(value: U256) -> Option<U256> {
1125-
value.checked_mul(U256::from(EVM_DECIMALS_FACTOR))
1126+
value
1127+
.checked_mul(U256::from(EVM_DECIMALS_FACTOR))
1128+
.and_then(|evm_value| {
1129+
// Ensure the result fits within the maximum U256 value
1130+
if evm_value <= U256::MAX {
1131+
Some(evm_value)
1132+
} else {
1133+
None
1134+
}
1135+
})
11261136
}
11271137

1138+
/// Convert from EVM balance (U256) to Substrate balance (u64)
11281139
fn into_substrate_balance(value: U256) -> Option<U256> {
11291140
value
11301141
.checked_div(U256::from(EVM_DECIMALS_FACTOR))
11311142
.and_then(|substrate_value| {
1132-
// Ensure the result fits within the Subtensor Balance type (u64)
1143+
// Ensure the result fits within the TAO balance type (u64)
11331144
if substrate_value <= U256::from(u64::MAX) {
11341145
Some(substrate_value)
11351146
} else {
@@ -2008,9 +2019,6 @@ impl_runtime_apis! {
20082019
}
20092020
}
20102021

2011-
// #[cfg(test)]
2012-
// mod tests {
2013-
20142022
#[test]
20152023
fn check_whitelist() {
20162024
use crate::*;
@@ -2033,4 +2041,72 @@ fn check_whitelist() {
20332041
// System Events
20342042
assert!(whitelist.contains("26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7"));
20352043
}
2036-
// }
2044+
2045+
#[test]
2046+
fn test_into_substrate_balance_valid() {
2047+
// Valid conversion within u64 range
2048+
let evm_balance = U256::from(1_000_000_000_000_000_000u128); // 1 TAO in EVM
2049+
let expected_substrate_balance = U256::from(1_000_000_000u128); // 1 TAO in Substrate
2050+
2051+
let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance);
2052+
assert_eq!(result, Some(expected_substrate_balance));
2053+
}
2054+
2055+
#[test]
2056+
fn test_into_substrate_balance_large_value() {
2057+
// Maximum valid balance for u64
2058+
let evm_balance = U256::from(u64::MAX) * U256::from(EVM_DECIMALS_FACTOR); // Max u64 TAO in EVM
2059+
let expected_substrate_balance = U256::from(u64::MAX);
2060+
2061+
let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance);
2062+
assert_eq!(result, Some(expected_substrate_balance));
2063+
}
2064+
2065+
#[test]
2066+
fn test_into_substrate_balance_exceeds_u64() {
2067+
// EVM balance that exceeds u64 after conversion
2068+
let evm_balance = (U256::from(u64::MAX) + U256::from(1)) * U256::from(EVM_DECIMALS_FACTOR);
2069+
2070+
let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance);
2071+
assert_eq!(result, None); // Exceeds u64, should return None
2072+
}
2073+
2074+
#[test]
2075+
fn test_into_substrate_balance_precision_loss() {
2076+
// EVM balance with precision loss
2077+
let evm_balance = U256::from(1_000_000_000_123_456_789u128); // 1 TAO + extra precision in EVM
2078+
let expected_substrate_balance = U256::from(1_000_000_000u128); // Truncated to 1 TAO in Substrate
2079+
2080+
let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance);
2081+
assert_eq!(result, Some(expected_substrate_balance));
2082+
}
2083+
2084+
#[test]
2085+
fn test_into_substrate_balance_zero_value() {
2086+
// Zero balance should convert to zero
2087+
let evm_balance = U256::from(0);
2088+
let expected_substrate_balance = U256::from(0);
2089+
2090+
let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance);
2091+
assert_eq!(result, Some(expected_substrate_balance));
2092+
}
2093+
2094+
#[test]
2095+
fn test_into_evm_balance_valid() {
2096+
// Valid conversion from Substrate to EVM
2097+
let substrate_balance = U256::from(1_000_000_000u128); // 1 TAO in Substrate
2098+
let expected_evm_balance = U256::from(1_000_000_000_000_000_000u128); // 1 TAO in EVM
2099+
2100+
let result = SubtensorEvmBalanceConverter::into_evm_balance(substrate_balance);
2101+
assert_eq!(result, Some(expected_evm_balance));
2102+
}
2103+
2104+
#[test]
2105+
fn test_into_evm_balance_overflow() {
2106+
// Substrate balance larger than u64::MAX but valid within U256
2107+
let substrate_balance = U256::from(u64::MAX) + U256::from(1); // Large balance
2108+
let expected_evm_balance = substrate_balance * U256::from(EVM_DECIMALS_FACTOR);
2109+
2110+
let result = SubtensorEvmBalanceConverter::into_evm_balance(substrate_balance);
2111+
assert_eq!(result, Some(expected_evm_balance)); // Should return the scaled value
2112+
}

0 commit comments

Comments
 (0)