Skip to content

Commit

Permalink
Merge pull request #1129 from opentensor/feat/use-opentensor-org-fp
Browse files Browse the repository at this point in the history
Feat/use opentensor org fp
  • Loading branch information
unconst authored Jan 7, 2025
2 parents d261f67 + e90a0ff commit 8cb62c5
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 70 deletions.
48 changes: 24 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 22 additions & 22 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -166,32 +166,32 @@ sc-network-sync = { git = "https://github.com/paritytech/polkadot-sdk.git", tag
substrate-prometheus-endpoint = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false }

# Frontier
fp-evm = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fp-rpc = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fp-self-contained = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false, features = [
fp-evm = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
fp-rpc = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
fp-self-contained = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false, features = [
"serde",
] }
fp-account = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fc-storage = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fc-db = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fc-consensus = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fp-consensus = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fp-dynamic-fee = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fc-api = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fc-rpc = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fc-rpc-core = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fc-mapping-sync = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fp-account = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
fc-storage = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
fc-db = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
fc-consensus = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
fp-consensus = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
fp-dynamic-fee = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
fc-api = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
fc-rpc = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
fc-rpc-core = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
fc-mapping-sync = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }

# Frontier FRAME
pallet-base-fee = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
pallet-dynamic-fee = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
pallet-ethereum = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
pallet-evm = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
pallet-evm-chain-id = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
pallet-evm-precompile-modexp = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
pallet-evm-precompile-sha3fips = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
pallet-evm-precompile-simple = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
pallet-hotfix-sufficients = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
pallet-base-fee = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
pallet-dynamic-fee = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
pallet-ethereum = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
pallet-evm = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
pallet-evm-chain-id = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
pallet-evm-precompile-modexp = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
pallet-evm-precompile-sha3fips = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
pallet-evm-precompile-simple = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }
pallet-hotfix-sufficients = { git = "https://github.com/opentensor/frontier", rev = "635bdac882", default-features = false }

#DRAND
pallet-drand = { path = "pallets/drand", default-features = false }
Expand Down
69 changes: 45 additions & 24 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1194,37 +1194,57 @@ parameter_types! {
/// The difference between EVM decimals and Substrate decimals.
/// Substrate balances has 9 decimals, while EVM has 18, so the
/// difference factor is 9 decimals, or 10^9
const EVM_DECIMALS_FACTOR: u64 = 1_000_000_000_u64;
const EVM_TO_SUBSTRATE_DECIMALS: u64 = 1_000_000_000_u64;

pub struct SubtensorEvmBalanceConverter;

impl BalanceConverter for SubtensorEvmBalanceConverter {
/// Convert from Substrate balance (u64) to EVM balance (U256)
fn into_evm_balance(value: U256) -> Option<U256> {
value
.checked_mul(U256::from(EVM_DECIMALS_FACTOR))
.and_then(|evm_value| {
// Ensure the result fits within the maximum U256 value
if evm_value <= U256::MAX {
Some(evm_value)
} else {
None
}
})
if let Some(evm_value) = value.checked_mul(U256::from(EVM_TO_SUBSTRATE_DECIMALS)) {
// Ensure the result fits within the maximum U256 value
if evm_value <= U256::MAX {
Some(evm_value)
} else {
// Log value too large
log::debug!(
"SubtensorEvmBalanceConverter::into_evm_balance( {:?} ) larger than U256::MAX",
value
);
None
}
} else {
// Log overflow
log::debug!(
"SubtensorEvmBalanceConverter::into_evm_balance( {:?} ) overflow",
value
);
None
}
}

/// Convert from EVM balance (U256) to Substrate balance (u64)
fn into_substrate_balance(value: U256) -> Option<U256> {
value
.checked_div(U256::from(EVM_DECIMALS_FACTOR))
.and_then(|substrate_value| {
// Ensure the result fits within the TAO balance type (u64)
if substrate_value <= U256::from(u64::MAX) {
Some(substrate_value)
} else {
None
}
})
if let Some(substrate_value) = value.checked_div(U256::from(EVM_TO_SUBSTRATE_DECIMALS)) {
// Ensure the result fits within the TAO balance type (u64)
if substrate_value <= U256::from(u64::MAX) {
Some(substrate_value)
} else {
// Log value too large
log::debug!(
"SubtensorEvmBalanceConverter::into_substrate_balance( {:?} ) larger than u64::MAX",
value
);
None
}
} else {
// Log overflow
log::debug!(
"SubtensorEvmBalanceConverter::into_substrate_balance( {:?} ) overflow",
value
);
None
}
}
}

Expand Down Expand Up @@ -2141,7 +2161,7 @@ fn test_into_substrate_balance_valid() {
#[test]
fn test_into_substrate_balance_large_value() {
// Maximum valid balance for u64
let evm_balance = U256::from(u64::MAX) * U256::from(EVM_DECIMALS_FACTOR); // Max u64 TAO in EVM
let evm_balance = U256::from(u64::MAX) * U256::from(EVM_TO_SUBSTRATE_DECIMALS); // Max u64 TAO in EVM
let expected_substrate_balance = U256::from(u64::MAX);

let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance);
Expand All @@ -2151,7 +2171,8 @@ fn test_into_substrate_balance_large_value() {
#[test]
fn test_into_substrate_balance_exceeds_u64() {
// EVM balance that exceeds u64 after conversion
let evm_balance = (U256::from(u64::MAX) + U256::from(1)) * U256::from(EVM_DECIMALS_FACTOR);
let evm_balance =
(U256::from(u64::MAX) + U256::from(1)) * U256::from(EVM_TO_SUBSTRATE_DECIMALS);

let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance);
assert_eq!(result, None); // Exceeds u64, should return None
Expand Down Expand Up @@ -2191,7 +2212,7 @@ fn test_into_evm_balance_valid() {
fn test_into_evm_balance_overflow() {
// Substrate balance larger than u64::MAX but valid within U256
let substrate_balance = U256::from(u64::MAX) + U256::from(1); // Large balance
let expected_evm_balance = substrate_balance * U256::from(EVM_DECIMALS_FACTOR);
let expected_evm_balance = substrate_balance * U256::from(EVM_TO_SUBSTRATE_DECIMALS);

let result = SubtensorEvmBalanceConverter::into_evm_balance(substrate_balance);
assert_eq!(result, Some(expected_evm_balance)); // Should return the scaled value
Expand Down

0 comments on commit 8cb62c5

Please sign in to comment.