From 82936fa06a6f55bd50913ab543bd273ff1a2983b Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 15 Nov 2024 11:42:17 -0500 Subject: [PATCH 01/20] hotfix to fix EVM balance transfer precision --- runtime/src/precompiles/balance_transfer.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/runtime/src/precompiles/balance_transfer.rs b/runtime/src/precompiles/balance_transfer.rs index 6ae554fa4..9891681af 100644 --- a/runtime/src/precompiles/balance_transfer.rs +++ b/runtime/src/precompiles/balance_transfer.rs @@ -25,6 +25,19 @@ impl BalanceTransferPrecompile { // Forward all received value to the destination address let amount: U256 = handle.context().apparent_value; + // Define precision adjustment (e.g., 18 decimals for EVM) + const PRECISION: u32 = 18; + + // Adjust `amount` to fit within substrate `Balance` type (u64) + let amount_sub: ::Balance = { + // Divide `amount` by 10^(18 - N) where N is substrate precision (e.g., 12) + let adjusted_amount = amount + / U256::exp10(PRECISION - ::BalancePrecision); + adjusted_amount + .try_into() + .map_err(|_| ExitError::OutOfFund)? // Ensure it fits in `Balance` + }; + // This is hardcoded hashed address mapping of // 0x0000000000000000000000000000000000000800 to ss58 public key // i.e. the contract sends funds it received to the destination address @@ -37,14 +50,11 @@ impl BalanceTransferPrecompile { let address_bytes_dst: &[u8] = get_slice(txdata, 4, 36)?; let account_id_src = bytes_to_account_id(&address_bytes_src)?; let account_id_dst = bytes_to_account_id(address_bytes_dst)?; - let amount_sub = - ::BalanceConverter::into_substrate_balance(amount) - .ok_or(ExitError::OutOfFund)?; let call = RuntimeCall::Balances(pallet_balances::Call::::transfer_allow_death { dest: account_id_dst.into(), - value: amount_sub.unique_saturated_into(), + value: amount_sub, }); let result = call.dispatch(RawOrigin::Signed(account_id_src).into()); From 6bdbaf83ad5eb15740c75936d92a0d54ecc6a3cd Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 15 Nov 2024 12:10:25 -0500 Subject: [PATCH 02/20] improve safety and performance of EVM <=> TAO balance transfers --- runtime/src/precompiles/balance_transfer.rs | 53 ++++++++++++++------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/runtime/src/precompiles/balance_transfer.rs b/runtime/src/precompiles/balance_transfer.rs index 9891681af..d1096723c 100644 --- a/runtime/src/precompiles/balance_transfer.rs +++ b/runtime/src/precompiles/balance_transfer.rs @@ -25,30 +25,48 @@ impl BalanceTransferPrecompile { // Forward all received value to the destination address let amount: U256 = handle.context().apparent_value; - // Define precision adjustment (e.g., 18 decimals for EVM) - const PRECISION: u32 = 18; + // Precision adjustment factor: 18 (EVM) - 9 (TAO) + const PRECISION_FACTOR: u128 = 1_000_000_000; // 10^9 - // Adjust `amount` to fit within substrate `Balance` type (u64) - let amount_sub: ::Balance = { - // Divide `amount` by 10^(18 - N) where N is substrate precision (e.g., 12) - let adjusted_amount = amount - / U256::exp10(PRECISION - ::BalancePrecision); - adjusted_amount - .try_into() - .map_err(|_| ExitError::OutOfFund)? // Ensure it fits in `Balance` - }; + // Calculate remainder to detect precision loss + let remainder = amount % U256::from(PRECISION_FACTOR); + if remainder > U256::zero() { + tracing::warn!( + "Precision loss detected during transfer: lost {:?} wei", + remainder + ); + } + + // Adjust `amount` to substrate balance type + let amount_sub: ::Balance = (amount + / U256::from(PRECISION_FACTOR)) + .try_into() + .map_err(|_| { + tracing::error!( + "Failed to convert amount {:?} to substrate balance type", + amount + ); + ExitError::OutOfFund + })?; + + if amount_sub == 0 { + return Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: vec![], + }); + } - // This is hardcoded hashed address mapping of - // 0x0000000000000000000000000000000000000800 to ss58 public key - // i.e. the contract sends funds it received to the destination address - // from the method parameter - let address_bytes_src: [u8; 32] = [ + // This is a hardcoded hashed address mapping of + // 0x0000000000000000000000000000000000000800 to an ss58 public key + // i.e., the contract sends funds it received to the destination address + // from the method parameter. + const ADDRESS_BYTES_SRC: [u8; 32] = [ 0x07, 0xec, 0x71, 0x2a, 0x5d, 0x38, 0x43, 0x4d, 0xdd, 0x03, 0x3f, 0x8f, 0x02, 0x4e, 0xcd, 0xfc, 0x4b, 0xb5, 0x95, 0x1c, 0x13, 0xc3, 0x08, 0x5c, 0x39, 0x9c, 0x8a, 0x5f, 0x62, 0x93, 0x70, 0x5d, ]; let address_bytes_dst: &[u8] = get_slice(txdata, 4, 36)?; - let account_id_src = bytes_to_account_id(&address_bytes_src)?; + let account_id_src = bytes_to_account_id(&ADDRESS_BYTES_SRC)?; let account_id_dst = bytes_to_account_id(address_bytes_dst)?; let call = @@ -57,6 +75,7 @@ impl BalanceTransferPrecompile { value: amount_sub, }); + // Dispatch the call let result = call.dispatch(RawOrigin::Signed(account_id_src).into()); if result.is_err() { return Err(PrecompileFailure::Error { From 57efc6398f1099ac162f7d210c02b57ccd498b9f Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 15 Nov 2024 12:28:20 -0500 Subject: [PATCH 03/20] bump CI From dfed247fb2079681ab82f2226c489a9e70bf24ed Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 15 Nov 2024 12:35:15 -0500 Subject: [PATCH 04/20] fix --- runtime/src/precompiles/balance_transfer.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/src/precompiles/balance_transfer.rs b/runtime/src/precompiles/balance_transfer.rs index d1096723c..26997e3ae 100644 --- a/runtime/src/precompiles/balance_transfer.rs +++ b/runtime/src/precompiles/balance_transfer.rs @@ -31,7 +31,7 @@ impl BalanceTransferPrecompile { // Calculate remainder to detect precision loss let remainder = amount % U256::from(PRECISION_FACTOR); if remainder > U256::zero() { - tracing::warn!( + log::warn!( "Precision loss detected during transfer: lost {:?} wei", remainder ); @@ -42,7 +42,7 @@ impl BalanceTransferPrecompile { / U256::from(PRECISION_FACTOR)) .try_into() .map_err(|_| { - tracing::error!( + log::error!( "Failed to convert amount {:?} to substrate balance type", amount ); From ec2b5f696a7404541a9e15c32b93c5a2c53ffc61 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 15 Nov 2024 12:36:42 -0500 Subject: [PATCH 05/20] fix again --- runtime/src/precompiles/balance_transfer.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/runtime/src/precompiles/balance_transfer.rs b/runtime/src/precompiles/balance_transfer.rs index 26997e3ae..7995bd9a2 100644 --- a/runtime/src/precompiles/balance_transfer.rs +++ b/runtime/src/precompiles/balance_transfer.rs @@ -38,16 +38,16 @@ impl BalanceTransferPrecompile { } // Adjust `amount` to substrate balance type - let amount_sub: ::Balance = (amount - / U256::from(PRECISION_FACTOR)) - .try_into() - .map_err(|_| { - log::error!( - "Failed to convert amount {:?} to substrate balance type", - amount - ); - ExitError::OutOfFund - })?; + let amount_sub: Balance = + (amount / U256::from(PRECISION_FACTOR)) + .try_into() + .map_err(|_| { + tracing::error!( + "Failed to convert amount {:?} to substrate balance type", + amount + ); + ExitError::OutOfFund + })?; if amount_sub == 0 { return Ok(PrecompileOutput { From decb56a4271992afcd368ec7b60157290299cee2 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 15 Nov 2024 12:37:13 -0500 Subject: [PATCH 06/20] fix --- runtime/src/precompiles/balance_transfer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/precompiles/balance_transfer.rs b/runtime/src/precompiles/balance_transfer.rs index 7995bd9a2..865541d4a 100644 --- a/runtime/src/precompiles/balance_transfer.rs +++ b/runtime/src/precompiles/balance_transfer.rs @@ -42,7 +42,7 @@ impl BalanceTransferPrecompile { (amount / U256::from(PRECISION_FACTOR)) .try_into() .map_err(|_| { - tracing::error!( + log::error!( "Failed to convert amount {:?} to substrate balance type", amount ); From 049aa86e02c9da684a6c7259198e4debac20c912 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 15 Nov 2024 12:37:48 -0500 Subject: [PATCH 07/20] import Balance --- runtime/src/precompiles/balance_transfer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/precompiles/balance_transfer.rs b/runtime/src/precompiles/balance_transfer.rs index 865541d4a..42bfaed5b 100644 --- a/runtime/src/precompiles/balance_transfer.rs +++ b/runtime/src/precompiles/balance_transfer.rs @@ -7,7 +7,7 @@ use sp_core::U256; use sp_runtime::traits::{Dispatchable, UniqueSaturatedInto}; use sp_std::vec; -use crate::{Runtime, RuntimeCall}; +use crate::{Balance, Runtime, RuntimeCall}; use crate::precompiles::{bytes_to_account_id, get_method_id, get_slice}; From 5254de9c8a01d21f9b876e26b2913b5908232927 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 15 Nov 2024 12:45:28 -0500 Subject: [PATCH 08/20] actually just use BalanceConverter --- runtime/src/precompiles/balance_transfer.rs | 32 ++++----------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/runtime/src/precompiles/balance_transfer.rs b/runtime/src/precompiles/balance_transfer.rs index 42bfaed5b..289a64167 100644 --- a/runtime/src/precompiles/balance_transfer.rs +++ b/runtime/src/precompiles/balance_transfer.rs @@ -1,10 +1,9 @@ use frame_system::RawOrigin; use pallet_evm::{ - BalanceConverter, ExitError, ExitSucceed, PrecompileFailure, PrecompileHandle, - PrecompileOutput, PrecompileResult, + ExitError, ExitSucceed, PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileResult, }; use sp_core::U256; -use sp_runtime::traits::{Dispatchable, UniqueSaturatedInto}; +use sp_runtime::traits::Dispatchable; use sp_std::vec; use crate::{Balance, Runtime, RuntimeCall}; @@ -25,29 +24,10 @@ impl BalanceTransferPrecompile { // Forward all received value to the destination address let amount: U256 = handle.context().apparent_value; - // Precision adjustment factor: 18 (EVM) - 9 (TAO) - const PRECISION_FACTOR: u128 = 1_000_000_000; // 10^9 - - // Calculate remainder to detect precision loss - let remainder = amount % U256::from(PRECISION_FACTOR); - if remainder > U256::zero() { - log::warn!( - "Precision loss detected during transfer: lost {:?} wei", - remainder - ); - } - - // Adjust `amount` to substrate balance type - let amount_sub: Balance = - (amount / U256::from(PRECISION_FACTOR)) - .try_into() - .map_err(|_| { - log::error!( - "Failed to convert amount {:?} to substrate balance type", - amount - ); - ExitError::OutOfFund - })?; + // Use BalanceConverter to convert EVM amount to Substrate balance + let amount_sub = + ::BalanceConverter::into_substrate_balance(amount) + .ok_or_else(|| ExitError::OutOfFund)?; if amount_sub == 0 { return Ok(PrecompileOutput { From 0a46a7345627ed19c25fa5d76df75d798d4b05fd Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 15 Nov 2024 12:48:15 -0500 Subject: [PATCH 09/20] fix SubstrateEvmBalanceConverter --- runtime/src/lib.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 2455da5d0..f1cc4be89 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -1119,18 +1119,28 @@ parameter_types! { const EVM_DECIMALS_FACTOR: u64 = 1_000_000_000_u64; pub struct SubtensorEvmBalanceConverter; +pub struct SubtensorEvmBalanceConverter; + impl BalanceConverter for SubtensorEvmBalanceConverter { + /// Convert from Substrate balance to EVM balance fn into_evm_balance(value: U256) -> Option { - U256::from(UniqueSaturatedInto::::unique_saturated_into(value)) - .checked_mul(U256::from(EVM_DECIMALS_FACTOR)) + // Scale up by EVM_DECIMALS_FACTOR, ensuring no overflow + value.checked_mul(U256::from(EVM_DECIMALS_FACTOR)) } + /// Convert from EVM balance to Substrate balance fn into_substrate_balance(value: U256) -> Option { - if value <= U256::from(u64::MAX) { - value.checked_div(U256::from(EVM_DECIMALS_FACTOR)) - } else { - None - } + // Scale down by EVM_DECIMALS_FACTOR, truncating precision safely + value + .checked_div(U256::from(EVM_DECIMALS_FACTOR)) + .and_then(|substrate_value| { + // Ensure the result fits in Substrate's Balance type (u128) + if substrate_value <= U256::from(u128::MAX) { + Some(substrate_value) + } else { + None + } + }) } } From 43f782a2dd9083b2f3424710b026cc24c9dbda5c Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 15 Nov 2024 13:10:54 -0500 Subject: [PATCH 10/20] fixes --- runtime/src/lib.rs | 1 - runtime/src/precompiles/balance_transfer.rs | 9 +++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index f1cc4be89..689e1c7d4 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -1118,7 +1118,6 @@ parameter_types! { /// difference factor is 9 decimals, or 10^9 const EVM_DECIMALS_FACTOR: u64 = 1_000_000_000_u64; -pub struct SubtensorEvmBalanceConverter; pub struct SubtensorEvmBalanceConverter; impl BalanceConverter for SubtensorEvmBalanceConverter { diff --git a/runtime/src/precompiles/balance_transfer.rs b/runtime/src/precompiles/balance_transfer.rs index 289a64167..12eeaaf37 100644 --- a/runtime/src/precompiles/balance_transfer.rs +++ b/runtime/src/precompiles/balance_transfer.rs @@ -1,12 +1,13 @@ use frame_system::RawOrigin; use pallet_evm::{ - ExitError, ExitSucceed, PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileResult, + BalanceConverter, ExitError, ExitSucceed, PrecompileFailure, PrecompileHandle, + PrecompileOutput, PrecompileResult, }; use sp_core::U256; use sp_runtime::traits::Dispatchable; use sp_std::vec; -use crate::{Balance, Runtime, RuntimeCall}; +use crate::{Runtime, RuntimeCall}; use crate::precompiles::{bytes_to_account_id, get_method_id, get_slice}; @@ -29,7 +30,7 @@ impl BalanceTransferPrecompile { ::BalanceConverter::into_substrate_balance(amount) .ok_or_else(|| ExitError::OutOfFund)?; - if amount_sub == 0 { + if amount_sub.is_zero() { return Ok(PrecompileOutput { exit_status: ExitSucceed::Returned, output: vec![], @@ -52,7 +53,7 @@ impl BalanceTransferPrecompile { let call = RuntimeCall::Balances(pallet_balances::Call::::transfer_allow_death { dest: account_id_dst.into(), - value: amount_sub, + value: amount_sub.unique_saturated_into(), }); // Dispatch the call From e522268372d3ed78cc2c74d6d8846621e350fa1a Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 15 Nov 2024 13:17:59 -0500 Subject: [PATCH 11/20] fix --- runtime/src/precompiles/balance_transfer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/precompiles/balance_transfer.rs b/runtime/src/precompiles/balance_transfer.rs index 12eeaaf37..3e2639b84 100644 --- a/runtime/src/precompiles/balance_transfer.rs +++ b/runtime/src/precompiles/balance_transfer.rs @@ -4,7 +4,7 @@ use pallet_evm::{ PrecompileOutput, PrecompileResult, }; use sp_core::U256; -use sp_runtime::traits::Dispatchable; +use sp_runtime::traits::{Dispatchable, UniqueSaturatedInto}; use sp_std::vec; use crate::{Runtime, RuntimeCall}; From 14c2ce7c86030e45bcd83c3000359b4c050d25c4 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 15 Nov 2024 13:21:50 -0500 Subject: [PATCH 12/20] clippy --- runtime/src/precompiles/balance_transfer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/precompiles/balance_transfer.rs b/runtime/src/precompiles/balance_transfer.rs index 3e2639b84..99d911b02 100644 --- a/runtime/src/precompiles/balance_transfer.rs +++ b/runtime/src/precompiles/balance_transfer.rs @@ -28,7 +28,7 @@ impl BalanceTransferPrecompile { // Use BalanceConverter to convert EVM amount to Substrate balance let amount_sub = ::BalanceConverter::into_substrate_balance(amount) - .ok_or_else(|| ExitError::OutOfFund)?; + .ok_or(ExitError::OutOfFund)?; if amount_sub.is_zero() { return Ok(PrecompileOutput { From de2049d528de8404eaaf60fd7ee04ba4df0986dd Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 15 Nov 2024 13:56:24 -0500 Subject: [PATCH 13/20] whoops --- runtime/src/lib.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 689e1c7d4..c6af9edc9 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -1121,20 +1121,16 @@ const EVM_DECIMALS_FACTOR: u64 = 1_000_000_000_u64; pub struct SubtensorEvmBalanceConverter; impl BalanceConverter for SubtensorEvmBalanceConverter { - /// Convert from Substrate balance to EVM balance fn into_evm_balance(value: U256) -> Option { - // Scale up by EVM_DECIMALS_FACTOR, ensuring no overflow value.checked_mul(U256::from(EVM_DECIMALS_FACTOR)) } - /// Convert from EVM balance to Substrate balance fn into_substrate_balance(value: U256) -> Option { - // Scale down by EVM_DECIMALS_FACTOR, truncating precision safely value .checked_div(U256::from(EVM_DECIMALS_FACTOR)) .and_then(|substrate_value| { - // Ensure the result fits in Substrate's Balance type (u128) - if substrate_value <= U256::from(u128::MAX) { + // Ensure the result fits within the Subtensor Balance type (u64) + if substrate_value <= U256::from(u64::MAX) { Some(substrate_value) } else { None From 67b798efa9e8647f041587b754eb3a238c2e465d Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 15 Nov 2024 15:17:10 -0500 Subject: [PATCH 14/20] final fix + tests --- runtime/src/lib.rs | 88 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 82 insertions(+), 6 deletions(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index c6af9edc9..0d20069fe 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -1121,15 +1121,26 @@ const EVM_DECIMALS_FACTOR: 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 { - value.checked_mul(U256::from(EVM_DECIMALS_FACTOR)) + 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 + } + }) } + /// Convert from EVM balance (U256) to Substrate balance (u64) fn into_substrate_balance(value: U256) -> Option { value .checked_div(U256::from(EVM_DECIMALS_FACTOR)) .and_then(|substrate_value| { - // Ensure the result fits within the Subtensor Balance type (u64) + // Ensure the result fits within the TAO balance type (u64) if substrate_value <= U256::from(u64::MAX) { Some(substrate_value) } else { @@ -2008,9 +2019,6 @@ impl_runtime_apis! { } } -// #[cfg(test)] -// mod tests { - #[test] fn check_whitelist() { use crate::*; @@ -2033,4 +2041,72 @@ fn check_whitelist() { // System Events assert!(whitelist.contains("26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7")); } -// } + +#[test] +fn test_into_substrate_balance_valid() { + // Valid conversion within u64 range + let evm_balance = U256::from(1_000_000_000_000_000_000u128); // 1 TAO in EVM + let expected_substrate_balance = U256::from(1_000_000_000u128); // 1 TAO in Substrate + + let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance); + assert_eq!(result, Some(expected_substrate_balance)); +} + +#[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 expected_substrate_balance = U256::from(u64::MAX); + + let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance); + assert_eq!(result, Some(expected_substrate_balance)); +} + +#[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 result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance); + assert_eq!(result, None); // Exceeds u64, should return None +} + +#[test] +fn test_into_substrate_balance_precision_loss() { + // EVM balance with precision loss + let evm_balance = U256::from(1_000_000_000_123_456_789u128); // 1 TAO + extra precision in EVM + let expected_substrate_balance = U256::from(1_000_000_000u128); // Truncated to 1 TAO in Substrate + + let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance); + assert_eq!(result, Some(expected_substrate_balance)); +} + +#[test] +fn test_into_substrate_balance_zero_value() { + // Zero balance should convert to zero + let evm_balance = U256::from(0); + let expected_substrate_balance = U256::from(0); + + let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance); + assert_eq!(result, Some(expected_substrate_balance)); +} + +#[test] +fn test_into_evm_balance_valid() { + // Valid conversion from Substrate to EVM + let substrate_balance = U256::from(1_000_000_000u128); // 1 TAO in Substrate + let expected_evm_balance = U256::from(1_000_000_000_000_000_000u128); // 1 TAO in EVM + + let result = SubtensorEvmBalanceConverter::into_evm_balance(substrate_balance); + assert_eq!(result, Some(expected_evm_balance)); +} + +#[test] +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 result = SubtensorEvmBalanceConverter::into_evm_balance(substrate_balance); + assert_eq!(result, Some(expected_evm_balance)); // Should return the scaled value +} From 9282b6eb799de7927f43d40dd4bca0da3f95bc79 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 15 Nov 2024 15:27:20 -0500 Subject: [PATCH 15/20] force cargo audit version --- .github/workflows/cargo-audit.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cargo-audit.yml b/.github/workflows/cargo-audit.yml index 3197150b1..788673b80 100644 --- a/.github/workflows/cargo-audit.yml +++ b/.github/workflows/cargo-audit.yml @@ -36,7 +36,7 @@ jobs: key: ubuntu-latest-${{ env.RUST_BIN_DIR }} - name: Install cargo-audit - run: cargo install --version 0.20.1 cargo-audit + run: cargo install --version 0.20.1 --force cargo-audit - name: cargo audit run: cargo audit --ignore RUSTSEC-2024-0336 # rustls issue; wait for upstream to resolve this From 417c35f6c2a4f1de4a15e7c1534a87c6f5d609d5 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 15 Nov 2024 15:28:00 -0500 Subject: [PATCH 16/20] display cargo-audit version --- .github/workflows/cargo-audit.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/cargo-audit.yml b/.github/workflows/cargo-audit.yml index 788673b80..31a56e5c0 100644 --- a/.github/workflows/cargo-audit.yml +++ b/.github/workflows/cargo-audit.yml @@ -38,5 +38,8 @@ jobs: - name: Install cargo-audit run: cargo install --version 0.20.1 --force cargo-audit + - name: Display cargo-audit --version + run: cargo audit --version + - name: cargo audit run: cargo audit --ignore RUSTSEC-2024-0336 # rustls issue; wait for upstream to resolve this From ea5d28449e139ad11667ceb2794ee43b0fd2b6c2 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 15 Nov 2024 15:31:17 -0500 Subject: [PATCH 17/20] don't need rust cache for cargo audit step, it can't use it --- .github/workflows/cargo-audit.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.github/workflows/cargo-audit.yml b/.github/workflows/cargo-audit.yml index 31a56e5c0..5ca2ad73c 100644 --- a/.github/workflows/cargo-audit.yml +++ b/.github/workflows/cargo-audit.yml @@ -30,11 +30,6 @@ jobs: components: rustfmt, clippy profile: minimal - - name: Utilize Shared Rust Cache - uses: Swatinem/rust-cache@v2.2.1 - with: - key: ubuntu-latest-${{ env.RUST_BIN_DIR }} - - name: Install cargo-audit run: cargo install --version 0.20.1 --force cargo-audit From cbc589da530ae316d5e745afa4258cea511bc5bf Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 15 Nov 2024 15:33:34 -0500 Subject: [PATCH 18/20] don't need to install rust stable --- .github/workflows/cargo-audit.yml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.github/workflows/cargo-audit.yml b/.github/workflows/cargo-audit.yml index 5ca2ad73c..1934dcaeb 100644 --- a/.github/workflows/cargo-audit.yml +++ b/.github/workflows/cargo-audit.yml @@ -23,13 +23,6 @@ jobs: sudo apt-get update && sudo apt-get install -y clang curl libssl-dev llvm libudev-dev protobuf-compiler - - name: Install Rust Stable - uses: actions-rs/toolchain@v1.0.6 - with: - toolchain: stable - components: rustfmt, clippy - profile: minimal - - name: Install cargo-audit run: cargo install --version 0.20.1 --force cargo-audit From 4f1ef7c299e65da829f6cf67a6895a4a429c7ef6 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 15 Nov 2024 15:38:47 -0500 Subject: [PATCH 19/20] some CI fixes --- .github/workflows/check-devnet.yml | 3 --- .github/workflows/check-finney.yml | 3 --- .github/workflows/check-testnet.yml | 3 --- .github/workflows/try-runtime.yml | 4 ++-- 4 files changed, 2 insertions(+), 11 deletions(-) diff --git a/.github/workflows/check-devnet.yml b/.github/workflows/check-devnet.yml index 8f04d78cf..c7f39082e 100644 --- a/.github/workflows/check-devnet.yml +++ b/.github/workflows/check-devnet.yml @@ -20,9 +20,6 @@ jobs: sudo apt-get install -y curl clang curl libssl-dev llvm \ libudev-dev protobuf-compiler - - name: Set up Rust Toolchain - run: curl https://sh.rustup.rs -sSf | sh -s -- -y - - name: Install substrate-spec-version run: cargo install substrate-spec-version diff --git a/.github/workflows/check-finney.yml b/.github/workflows/check-finney.yml index 947b9a902..292bae7ee 100644 --- a/.github/workflows/check-finney.yml +++ b/.github/workflows/check-finney.yml @@ -20,9 +20,6 @@ jobs: sudo apt-get install -y curl clang curl libssl-dev llvm \ libudev-dev protobuf-compiler - - name: Set up Rust Toolchain - run: curl https://sh.rustup.rs -sSf | sh -s -- -y - - name: Install substrate-spec-version run: cargo install substrate-spec-version diff --git a/.github/workflows/check-testnet.yml b/.github/workflows/check-testnet.yml index a869129ab..03c7e8f8a 100644 --- a/.github/workflows/check-testnet.yml +++ b/.github/workflows/check-testnet.yml @@ -20,9 +20,6 @@ jobs: sudo apt-get install -y curl clang curl libssl-dev llvm \ libudev-dev protobuf-compiler - - name: Set up Rust Toolchain - run: curl https://sh.rustup.rs -sSf | sh -s -- -y - - name: Install substrate-spec-version run: cargo install substrate-spec-version diff --git a/.github/workflows/try-runtime.yml b/.github/workflows/try-runtime.yml index 174e6db37..a86482804 100644 --- a/.github/workflows/try-runtime.yml +++ b/.github/workflows/try-runtime.yml @@ -14,7 +14,7 @@ jobs: runs-on: SubtensorCI steps: - name: Checkout sources - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Run Try Runtime Checks uses: "paritytech/try-runtime-gha@v0.1.0" @@ -29,7 +29,7 @@ jobs: runs-on: SubtensorCI steps: - name: Checkout sources - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Run Try Runtime Checks uses: "paritytech/try-runtime-gha@v0.1.0" From ed8288d660de08493c73f75e467b2918a11d80a0 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 15 Nov 2024 15:44:44 -0500 Subject: [PATCH 20/20] a bunch of CI fixes --- .github/workflows/check-rust.yml | 53 +++----------------------------- 1 file changed, 4 insertions(+), 49 deletions(-) diff --git a/.github/workflows/check-rust.yml b/.github/workflows/check-rust.yml index 80d543163..3c9fc759b 100644 --- a/.github/workflows/check-rust.yml +++ b/.github/workflows/check-rust.yml @@ -43,9 +43,7 @@ jobs: env: RELEASE_NAME: development # RUSTFLAGS: -A warnings - RUSTV: ${{ matrix.rust-branch }} RUST_BACKTRACE: full - RUST_BIN_DIR: target/${{ matrix.rust-target }} SKIP_WASM_BUILD: 1 TARGET: ${{ matrix.rust-target }} steps: @@ -55,10 +53,10 @@ jobs: - name: Install dependencies run: sudo apt-get update && sudo apt-get install -y build-essential - - name: Install Rust ${{ matrix.rust-branch }} + - name: Install Rust Nightly uses: actions-rs/toolchain@v1.0.6 with: - toolchain: ${{ matrix.rust-branch }} + toolchain: nightly components: rustfmt profile: minimal @@ -84,11 +82,10 @@ jobs: env: RELEASE_NAME: development # RUSTFLAGS: -A warnings - RUSTV: ${{ matrix.rust-branch }} RUST_BACKTRACE: full - RUST_BIN_DIR: target/${{ matrix.rust-target }} SKIP_WASM_BUILD: 1 TARGET: ${{ matrix.rust-target }} + RUST_BIN_DIR: target/${{ matrix.rust-target }} steps: - name: Check-out repository under $GITHUB_WORKSPACE uses: actions/checkout@v4 @@ -98,13 +95,6 @@ jobs: sudo apt-get update && sudo apt-get install -y clang curl libssl-dev llvm libudev-dev protobuf-compiler - - name: Install Rust ${{ matrix.rust-branch }} - uses: actions-rs/toolchain@v1.0.6 - with: - toolchain: ${{ matrix.rust-branch }} - components: rustfmt, clippy - profile: minimal - - name: Utilize Shared Rust Cache uses: Swatinem/rust-cache@v2.2.1 with: @@ -128,12 +118,11 @@ jobs: # - macos-latest env: RELEASE_NAME: development - RUSTV: ${{ matrix.rust-branch }} RUSTFLAGS: -D warnings RUST_BACKTRACE: full - RUST_BIN_DIR: target/${{ matrix.rust-target }} SKIP_WASM_BUILD: 1 TARGET: ${{ matrix.rust-target }} + RUST_BIN_DIR: target/${{ matrix.rust-target }} steps: - name: Check-out repository under $GITHUB_WORKSPACE uses: actions/checkout@v4 @@ -166,8 +155,6 @@ jobs: runs-on: SubtensorCI strategy: matrix: - rust-branch: - - stable rust-target: - x86_64-unknown-linux-gnu # - x86_64-apple-darwin @@ -180,7 +167,6 @@ jobs: env: RELEASE_NAME: development # RUSTFLAGS: -A warnings - RUSTV: ${{ matrix.rust-branch }} RUST_BACKTRACE: full RUST_BIN_DIR: target/${{ matrix.rust-target }} SKIP_WASM_BUILD: 1 @@ -194,13 +180,6 @@ jobs: sudo apt-get update && sudo apt-get install -y clang curl libssl-dev llvm libudev-dev protobuf-compiler - - name: Install Rust ${{ matrix.rust-branch }} - uses: actions-rs/toolchain@v1.0.6 - with: - toolchain: ${{ matrix.rust-branch }} - components: rustfmt, clippy - profile: minimal - - name: Utilize Shared Rust Cache uses: Swatinem/rust-cache@v2.2.1 with: @@ -215,8 +194,6 @@ jobs: runs-on: SubtensorCI strategy: matrix: - rust-branch: - - stable rust-target: - x86_64-unknown-linux-gnu # - x86_64-apple-darwin @@ -229,7 +206,6 @@ jobs: env: RELEASE_NAME: development # RUSTFLAGS: -A warnings - RUSTV: ${{ matrix.rust-branch }} RUST_BACKTRACE: full RUST_BIN_DIR: target/${{ matrix.rust-target }} SKIP_WASM_BUILD: 1 @@ -243,13 +219,6 @@ jobs: sudo apt-get update && sudo apt-get install -y clang curl libssl-dev llvm libudev-dev protobuf-compiler - - name: Install Rust ${{ matrix.rust-branch }} - uses: actions-rs/toolchain@v1.0.6 - with: - toolchain: ${{ matrix.rust-branch }} - components: rustfmt, clippy - profile: minimal - - name: Utilize Rust shared cached uses: Swatinem/rust-cache@v2.2.1 with: @@ -278,7 +247,6 @@ jobs: env: RELEASE_NAME: development # RUSTFLAGS: -A warnings - RUSTV: ${{ matrix.rust-branch }} RUST_BACKTRACE: full RUST_BIN_DIR: target/${{ matrix.rust-target }} SKIP_WASM_BUILD: 1 @@ -292,13 +260,6 @@ jobs: sudo apt-get update && sudo apt-get install -y clang curl libssl-dev llvm libudev-dev protobuf-compiler - - name: Install Rust ${{ matrix.rust-branch }} - uses: actions-rs/toolchain@v1.0.6 - with: - toolchain: ${{ matrix.rust-branch }} - components: rustfmt, clippy - profile: minimal - - name: Utilize Rust shared cached uses: Swatinem/rust-cache@v2.2.1 with: @@ -322,12 +283,6 @@ jobs: runs-on: SubtensorCI steps: - - name: Install stable Rust - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: stable - - name: Install Zepter run: cargo install --locked -q zepter && zepter --version