From 4f92d340958114ebef2320b7ea62b910354e9661 Mon Sep 17 00:00:00 2001 From: open-junius Date: Mon, 18 Nov 2024 23:11:13 +0800 Subject: [PATCH 1/8] add netuid argument --- runtime/src/precompiles/staking.rs | 35 ++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/runtime/src/precompiles/staking.rs b/runtime/src/precompiles/staking.rs index daa95ba93..99d392fb9 100644 --- a/runtime/src/precompiles/staking.rs +++ b/runtime/src/precompiles/staking.rs @@ -72,11 +72,17 @@ impl StakingPrecompile { ::BalanceConverter::into_substrate_balance(amount) .ok_or(ExitError::OutOfFund)?; + let netuid = + Self::parse_netuid(data.get(56..64).unwrap_or(Err(PrecompileFailure::Error { + exit_status: ExitError::InvalidRange, + })?))?; + + // let netuid_u16 = netuid.as_u32(); + // Create the add_stake call let call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::::add_stake { hotkey, - // TODO update contract to add netuid - netuid: 1, + netuid, amount_staked: amount_sub.unique_saturated_into(), }); // Dispatch the add_stake call @@ -96,10 +102,14 @@ impl StakingPrecompile { ::BalanceConverter::into_substrate_balance(amount) .ok_or(ExitError::OutOfFund)?; + let netuid = + Self::parse_netuid(data.get(64..72).unwrap_or(Err(PrecompileFailure::Error { + exit_status: ExitError::InvalidRange, + })?))?; + let call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::::remove_stake { hotkey, - // TODO update contract to add netuid - netuid: 1, + netuid, amount_unstaked: amount_sub.unique_saturated_into(), }); Self::dispatch(handle, call) @@ -116,6 +126,23 @@ impl StakingPrecompile { Ok(hotkey) } + fn parse_netuid(data: &[u8]) -> Result { + let netuid = data + .get(0..8) + .map(U256::from_big_endian) + .ok_or(ExitError::InvalidRange)?; + + let u16_max_u256 = U256::from(u16::MAX); + if netuid > u16_max_u256 { + // if netuid.as_u128() > u16::MAX as u128 { + return Err(PrecompileFailure::Error { + exit_status: ExitError::InvalidRange, + }); + } + + Ok(netuid.as_u32() as u16) + } + fn dispatch(handle: &mut impl PrecompileHandle, call: RuntimeCall) -> PrecompileResult { let account_id = as AddressMapping>::into_account_id( From a8ce070b4ec0f743519e3596e871a1032760611f Mon Sep 17 00:00:00 2001 From: open-junius Date: Mon, 18 Nov 2024 23:13:58 +0800 Subject: [PATCH 2/8] removed commented code --- runtime/src/precompiles/staking.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/runtime/src/precompiles/staking.rs b/runtime/src/precompiles/staking.rs index 99d392fb9..7aa674141 100644 --- a/runtime/src/precompiles/staking.rs +++ b/runtime/src/precompiles/staking.rs @@ -77,8 +77,6 @@ impl StakingPrecompile { exit_status: ExitError::InvalidRange, })?))?; - // let netuid_u16 = netuid.as_u32(); - // Create the add_stake call let call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::::add_stake { hotkey, From 9bbb726969c86c788e8464f3fd7aa3f4dcca167f Mon Sep 17 00:00:00 2001 From: open-junius Date: Tue, 19 Nov 2024 19:49:29 +0800 Subject: [PATCH 3/8] keep --- runtime/src/precompiles/staking.rs | 46 ++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/runtime/src/precompiles/staking.rs b/runtime/src/precompiles/staking.rs index 7aa674141..27c81d5b1 100644 --- a/runtime/src/precompiles/staking.rs +++ b/runtime/src/precompiles/staking.rs @@ -66,16 +66,24 @@ impl StakingPrecompile { } fn add_stake(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { + log::error!("++++++ add_stake data as {:?}", data); let hotkey = Self::parse_hotkey(data)?.into(); let amount: U256 = handle.context().apparent_value; let amount_sub = ::BalanceConverter::into_substrate_balance(amount) .ok_or(ExitError::OutOfFund)?; - let netuid = - Self::parse_netuid(data.get(56..64).unwrap_or(Err(PrecompileFailure::Error { - exit_status: ExitError::InvalidRange, - })?))?; + log::error!("++++++ amount_sub {:?}", &amount_sub); + + let netuid_vec = data.get(56..64).ok_or(PrecompileFailure::Error { + exit_status: ExitError::InvalidRange, + })?; + + log::error!("++++++ netuid_vec is {:?}", netuid_vec); + + let netuid = Self::parse_netuid(netuid_vec)?; + + log::error!("++++++ netuid is {}", netuid); // Create the add_stake call let call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::::add_stake { @@ -87,6 +95,8 @@ impl StakingPrecompile { Self::dispatch(handle, call) } fn remove_stake(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { + log::error!("------ add_stake data as {:?}", data); + let hotkey = Self::parse_hotkey(data)?.into(); // We have to treat this as uint256 (because of Solidity ABI encoding rules, it pads uint64), @@ -100,10 +110,21 @@ impl StakingPrecompile { ::BalanceConverter::into_substrate_balance(amount) .ok_or(ExitError::OutOfFund)?; - let netuid = - Self::parse_netuid(data.get(64..72).unwrap_or(Err(PrecompileFailure::Error { - exit_status: ExitError::InvalidRange, - })?))?; + log::error!("------ add_stake amount as {:?}", amount); + + let netuid_vec = data.get(88..96).ok_or(PrecompileFailure::Error { + exit_status: ExitError::InvalidRange, + })?; + + log::error!("------ netuid_vec is {:?}", netuid_vec); + + let netuid = Self::parse_netuid(netuid_vec)?; + log::error!("------ add_stake netuid as {:?}", netuid); + + // let netuid = + // Self::parse_netuid(data.get(64..72).unwrap_or(Err(PrecompileFailure::Error { + // exit_status: ExitError::InvalidRange, + // })?))?; let call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::::remove_stake { hotkey, @@ -125,12 +146,19 @@ impl StakingPrecompile { } fn parse_netuid(data: &[u8]) -> Result { + log::error!("++++++ parse_netuid data is {:?}", &data); + let netuid = data .get(0..8) .map(U256::from_big_endian) .ok_or(ExitError::InvalidRange)?; + log::error!("++++++ parse_netuid is {:?}", &netuid); + let u16_max_u256 = U256::from(u16::MAX); + + log::error!("++++++ parse_netuid u16_max_u256 {:?}", &u16_max_u256); + if netuid > u16_max_u256 { // if netuid.as_u128() > u16::MAX as u128 { return Err(PrecompileFailure::Error { @@ -138,6 +166,8 @@ impl StakingPrecompile { }); } + log::error!("++++++ parse_netuid netuid.as_u32() {:?}", &netuid.as_u32()); + Ok(netuid.as_u32() as u16) } From e42e354d39bf97a95b668fd5dce93e661700f3a1 Mon Sep 17 00:00:00 2001 From: open-junius Date: Tue, 19 Nov 2024 20:58:23 +0800 Subject: [PATCH 4/8] correct parse netuid --- runtime/src/precompiles/staking.rs | 31 ------------------------------ 1 file changed, 31 deletions(-) diff --git a/runtime/src/precompiles/staking.rs b/runtime/src/precompiles/staking.rs index 27c81d5b1..130b61ba2 100644 --- a/runtime/src/precompiles/staking.rs +++ b/runtime/src/precompiles/staking.rs @@ -66,25 +66,16 @@ impl StakingPrecompile { } fn add_stake(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { - log::error!("++++++ add_stake data as {:?}", data); let hotkey = Self::parse_hotkey(data)?.into(); let amount: U256 = handle.context().apparent_value; let amount_sub = ::BalanceConverter::into_substrate_balance(amount) .ok_or(ExitError::OutOfFund)?; - log::error!("++++++ amount_sub {:?}", &amount_sub); - let netuid_vec = data.get(56..64).ok_or(PrecompileFailure::Error { exit_status: ExitError::InvalidRange, })?; - - log::error!("++++++ netuid_vec is {:?}", netuid_vec); - let netuid = Self::parse_netuid(netuid_vec)?; - - log::error!("++++++ netuid is {}", netuid); - // Create the add_stake call let call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::::add_stake { hotkey, @@ -95,8 +86,6 @@ impl StakingPrecompile { Self::dispatch(handle, call) } fn remove_stake(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { - log::error!("------ add_stake data as {:?}", data); - let hotkey = Self::parse_hotkey(data)?.into(); // We have to treat this as uint256 (because of Solidity ABI encoding rules, it pads uint64), @@ -110,21 +99,10 @@ impl StakingPrecompile { ::BalanceConverter::into_substrate_balance(amount) .ok_or(ExitError::OutOfFund)?; - log::error!("------ add_stake amount as {:?}", amount); - let netuid_vec = data.get(88..96).ok_or(PrecompileFailure::Error { exit_status: ExitError::InvalidRange, })?; - - log::error!("------ netuid_vec is {:?}", netuid_vec); - let netuid = Self::parse_netuid(netuid_vec)?; - log::error!("------ add_stake netuid as {:?}", netuid); - - // let netuid = - // Self::parse_netuid(data.get(64..72).unwrap_or(Err(PrecompileFailure::Error { - // exit_status: ExitError::InvalidRange, - // })?))?; let call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::::remove_stake { hotkey, @@ -146,28 +124,19 @@ impl StakingPrecompile { } fn parse_netuid(data: &[u8]) -> Result { - log::error!("++++++ parse_netuid data is {:?}", &data); - let netuid = data .get(0..8) .map(U256::from_big_endian) .ok_or(ExitError::InvalidRange)?; - log::error!("++++++ parse_netuid is {:?}", &netuid); - let u16_max_u256 = U256::from(u16::MAX); - log::error!("++++++ parse_netuid u16_max_u256 {:?}", &u16_max_u256); - if netuid > u16_max_u256 { // if netuid.as_u128() > u16::MAX as u128 { return Err(PrecompileFailure::Error { exit_status: ExitError::InvalidRange, }); } - - log::error!("++++++ parse_netuid netuid.as_u32() {:?}", &netuid.as_u32()); - Ok(netuid.as_u32() as u16) } From c5cf5a5704cbf5556db0360a85c8fa2763f32912 Mon Sep 17 00:00:00 2001 From: open-junius Date: Tue, 19 Nov 2024 23:48:02 +0800 Subject: [PATCH 5/8] fix clippy --- runtime/src/precompiles/staking.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/runtime/src/precompiles/staking.rs b/runtime/src/precompiles/staking.rs index 130b61ba2..138d428f6 100644 --- a/runtime/src/precompiles/staking.rs +++ b/runtime/src/precompiles/staking.rs @@ -137,7 +137,14 @@ impl StakingPrecompile { exit_status: ExitError::InvalidRange, }); } - Ok(netuid.as_u32() as u16) + let result: u16 = netuid + .as_u32() + .try_into() + .map_err(|_| PrecompileFailure::Error { + exit_status: ExitError::InvalidRange, + })?; + + Ok(result) } fn dispatch(handle: &mut impl PrecompileHandle, call: RuntimeCall) -> PrecompileResult { From 35ab9e80b6bac19ddb300e907594bafed4b7ca5c Mon Sep 17 00:00:00 2001 From: open-junius Date: Wed, 20 Nov 2024 20:39:31 +0800 Subject: [PATCH 6/8] fix clippy --- runtime/src/precompiles/staking.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/runtime/src/precompiles/staking.rs b/runtime/src/precompiles/staking.rs index 138d428f6..ba7ec0251 100644 --- a/runtime/src/precompiles/staking.rs +++ b/runtime/src/precompiles/staking.rs @@ -132,7 +132,6 @@ impl StakingPrecompile { let u16_max_u256 = U256::from(u16::MAX); if netuid > u16_max_u256 { - // if netuid.as_u128() > u16::MAX as u128 { return Err(PrecompileFailure::Error { exit_status: ExitError::InvalidRange, }); From 2dd396f01c79f9555c865090912c7f41fb286463 Mon Sep 17 00:00:00 2001 From: open-junius Date: Wed, 20 Nov 2024 21:07:03 +0800 Subject: [PATCH 7/8] fix clippy --- runtime/src/precompiles/staking.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/runtime/src/precompiles/staking.rs b/runtime/src/precompiles/staking.rs index ba7ec0251..7669c6761 100644 --- a/runtime/src/precompiles/staking.rs +++ b/runtime/src/precompiles/staking.rs @@ -124,10 +124,10 @@ impl StakingPrecompile { } fn parse_netuid(data: &[u8]) -> Result { - let netuid = data - .get(0..8) - .map(U256::from_big_endian) - .ok_or(ExitError::InvalidRange)?; + let mut bytes = [0_u8; 8]; + bytes.copy_from_slice(data.get(0..8).ok_or(ExitError::InvalidRange)?); + + let netuid = U256::from_big_endian(&bytes); let u16_max_u256 = U256::from(u16::MAX); @@ -136,12 +136,9 @@ impl StakingPrecompile { exit_status: ExitError::InvalidRange, }); } - let result: u16 = netuid - .as_u32() - .try_into() - .map_err(|_| PrecompileFailure::Error { - exit_status: ExitError::InvalidRange, - })?; + let mut two_bytes = [0_u8; 2]; + two_bytes.copy_from_slice(&bytes[6..8]); + let result: u16 = u16::from_le_bytes(two_bytes); Ok(result) } From aa5c06ec1b8916b4954bd5eff64187418352acaa Mon Sep 17 00:00:00 2001 From: open-junius Date: Wed, 20 Nov 2024 22:31:44 +0800 Subject: [PATCH 8/8] fix clippy --- runtime/src/precompiles/staking.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/precompiles/staking.rs b/runtime/src/precompiles/staking.rs index 7669c6761..8b91346ad 100644 --- a/runtime/src/precompiles/staking.rs +++ b/runtime/src/precompiles/staking.rs @@ -138,7 +138,7 @@ impl StakingPrecompile { } let mut two_bytes = [0_u8; 2]; two_bytes.copy_from_slice(&bytes[6..8]); - let result: u16 = u16::from_le_bytes(two_bytes); + let result: u16 = u16::from_be_bytes(two_bytes); Ok(result) }