From 53e49b6602ebad9c940f6bba6607b1c2ce7579dd Mon Sep 17 00:00:00 2001 From: Greg Zaitsev Date: Mon, 13 Jan 2025 18:22:08 -0500 Subject: [PATCH 1/6] Add netuid parameter to staking precompile add_stake and remove_stake methods --- pallets/subtensor/src/macros/genesis.rs | 2 +- runtime/src/precompiles/solidity/staking.abi | 32 ++++++++++++++--- runtime/src/precompiles/solidity/staking.sol | 8 ++--- runtime/src/precompiles/staking.rs | 36 ++++++++++++++------ 4 files changed, 58 insertions(+), 20 deletions(-) diff --git a/pallets/subtensor/src/macros/genesis.rs b/pallets/subtensor/src/macros/genesis.rs index 810202183..96c4db62e 100644 --- a/pallets/subtensor/src/macros/genesis.rs +++ b/pallets/subtensor/src/macros/genesis.rs @@ -49,7 +49,7 @@ mod genesis { let hotkey = DefaultAccount::::get(); SubnetMechanism::::insert(netuid, 1); // Make dynamic. Owner::::insert(hotkey.clone(), hotkey.clone()); - SubnetAlphaIn::::insert(netuid, 1); + SubnetAlphaIn::::insert(netuid, 10_000_000_000); SubnetTAO::::insert(netuid, 10_000_000_000); NetworksAdded::::insert(netuid, true); TotalNetworks::::mutate(|n| *n = n.saturating_add(1)); diff --git a/runtime/src/precompiles/solidity/staking.abi b/runtime/src/precompiles/solidity/staking.abi index 44b1829c4..878b1dc18 100644 --- a/runtime/src/precompiles/solidity/staking.abi +++ b/runtime/src/precompiles/solidity/staking.abi @@ -7,9 +7,9 @@ "type": "bytes32" }, { - "internalType": "uint16", + "internalType": "uint256", "name": "netuid", - "type": "uint16" + "type": "uint256" } ], "name": "addStake", @@ -17,6 +17,30 @@ "stateMutability": "payable", "type": "function" }, + { + inputs: [ + { + internalType: "bytes32", + name: "hotkey", + type: "bytes32", + }, + { + internalType: "bytes32", + name: "coldkey", + type: "bytes32", + }, + ], + name: "getStake", + outputs: [ + { + internalType: "uint64", + name: "", + type: "uint64", + }, + ], + stateMutability: "view", + type: "function", + }, { "inputs": [ { @@ -30,9 +54,9 @@ "type": "uint256" }, { - "internalType": "uint16", + "internalType": "uint256", "name": "netuid", - "type": "uint16" + "type": "uint256" } ], "name": "removeStake", diff --git a/runtime/src/precompiles/solidity/staking.sol b/runtime/src/precompiles/solidity/staking.sol index ec7fb7297..ce62b4326 100644 --- a/runtime/src/precompiles/solidity/staking.sol +++ b/runtime/src/precompiles/solidity/staking.sol @@ -14,13 +14,13 @@ interface IStaking { * https://github.com/polkadot-evm/frontier/blob/2e219e17a526125da003e64ef22ec037917083fa/frame/evm/src/lib.rs#L739 * * @param hotkey The hotkey public key (32 bytes). - * @param netuid The subnet to stake to (uint16). Currently a noop, functionality will be enabled with RAO. + * @param netuid The subnet to stake to (uint256). Currently a noop, functionality will be enabled with RAO. * * Requirements: * - `hotkey` must be a valid hotkey registered on the network, ensuring that the stake is * correctly attributed. */ - function addStake(bytes32 hotkey, uint16 netuid) external payable; + function addStake(bytes32 hotkey, uint256 netuid) external payable; /** * @dev Removes a subtensor stake `amount` from the specified `hotkey`. @@ -33,7 +33,7 @@ interface IStaking { * * @param hotkey The hotkey public key (32 bytes). * @param amount The amount to unstake in rao. - * @param netuid The subnet to stake to (uint16). Currently a noop, functionality will be enabled with RAO. + * @param netuid The subnet to stake to (uint256). Currently a noop, functionality will be enabled with RAO. * * Requirements: @@ -41,5 +41,5 @@ interface IStaking { * correctly attributed. * - The existing stake amount must be not lower than specified amount */ - function removeStake(bytes32 hotkey, uint256 amount, uint16 netuid) external; + function removeStake(bytes32 hotkey, uint256 amount, uint256 netuid) external; } diff --git a/runtime/src/precompiles/staking.rs b/runtime/src/precompiles/staking.rs index bf930005b..ce6ac746b 100644 --- a/runtime/src/precompiles/staking.rs +++ b/runtime/src/precompiles/staking.rs @@ -53,10 +53,10 @@ impl StakingPrecompile { .map_or_else(vec::Vec::new, |slice| slice.to_vec()); // Avoiding borrowing conflicts match method_id { - id if id == get_method_id("addStake(bytes32,uint16)") => { + id if id == get_method_id("addStake(bytes32,uint256)") => { Self::add_stake(handle, &method_input) } - id if id == get_method_id("removeStake(bytes32,uint256,uint16)") => { + id if id == get_method_id("removeStake(bytes32,uint256,uint256)") => { Self::remove_stake(handle, &method_input) } _ => Err(PrecompileFailure::Error { @@ -68,9 +68,7 @@ impl StakingPrecompile { fn add_stake(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { let hotkey = Self::parse_hotkey(data)?.into(); let amount: U256 = handle.context().apparent_value; - - // TODO: Use netuid method parameter here - let netuid: u16 = 0; + let netuid: u16 = Self::parse_netuid(data, 0x3E)?; let amount_sub = ::BalanceConverter::into_substrate_balance(amount) @@ -85,11 +83,10 @@ impl StakingPrecompile { // Dispatch the add_stake call Self::dispatch(handle, call) } + fn remove_stake(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { let hotkey = Self::parse_hotkey(data)?.into(); - - // TODO: Use netuid method parameter here - let netuid: u16 = 0; + let netuid: u16 = Self::parse_netuid(data, 0x5E)?; // We have to treat this as uint256 (because of Solidity ABI encoding rules, it pads uint64), // but this will never exceed 8 bytes, se we will ignore higher bytes and will only use lower @@ -121,6 +118,20 @@ impl StakingPrecompile { Ok(hotkey) } + fn parse_netuid(data: &[u8], offset: usize) -> Result { + if data.len() < (offset + 2) as usize { + return Err(PrecompileFailure::Error { + exit_status: ExitError::InvalidRange, + }); + } + + let mut netuid_bytes = [0u8; 2]; + netuid_bytes.copy_from_slice(get_slice(data, offset, offset+2)?); + let netuid: u16 = netuid_bytes[1] as u16 | ((netuid_bytes[0] as u16) << 8u16); + + Ok(netuid) + } + fn dispatch(handle: &mut impl PrecompileHandle, call: RuntimeCall) -> PrecompileResult { let account_id = as AddressMapping>::into_account_id( @@ -145,9 +156,12 @@ impl StakingPrecompile { exit_status: ExitSucceed::Returned, output: vec![], }), - Err(_) => Err(PrecompileFailure::Error { - exit_status: ExitError::Other("Subtensor call failed".into()), - }), + Err(_) => { + log::warn!("Returning error PrecompileFailure::Error"); + Err(PrecompileFailure::Error { + exit_status: ExitError::Other("Subtensor call failed".into()), + }) + }, } } From ec229d7a1dac0ca0c2edc0d40e3c156099ef79b3 Mon Sep 17 00:00:00 2001 From: Greg Zaitsev Date: Tue, 14 Jan 2025 09:57:47 -0500 Subject: [PATCH 2/6] Fix state mutability in staking precompile ABI --- runtime/src/precompiles/solidity/staking.abi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/precompiles/solidity/staking.abi b/runtime/src/precompiles/solidity/staking.abi index 878b1dc18..a3c51ea45 100644 --- a/runtime/src/precompiles/solidity/staking.abi +++ b/runtime/src/precompiles/solidity/staking.abi @@ -61,7 +61,7 @@ ], "name": "removeStake", "outputs": [], - "stateMutability": "payable", + "stateMutability": "nonpayable", "type": "function" } ] From 75a281eb98b22e740392dada1a68536052adbc7e Mon Sep 17 00:00:00 2001 From: Greg Zaitsev Date: Tue, 14 Jan 2025 10:58:36 -0500 Subject: [PATCH 3/6] Remove type from netuid declaration --- runtime/src/precompiles/staking.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/src/precompiles/staking.rs b/runtime/src/precompiles/staking.rs index ce6ac746b..ce70612a6 100644 --- a/runtime/src/precompiles/staking.rs +++ b/runtime/src/precompiles/staking.rs @@ -68,7 +68,7 @@ impl StakingPrecompile { fn add_stake(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { let hotkey = Self::parse_hotkey(data)?.into(); let amount: U256 = handle.context().apparent_value; - let netuid: u16 = Self::parse_netuid(data, 0x3E)?; + let netuid = Self::parse_netuid(data, 0x3E)?; let amount_sub = ::BalanceConverter::into_substrate_balance(amount) @@ -86,7 +86,7 @@ impl StakingPrecompile { fn remove_stake(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { let hotkey = Self::parse_hotkey(data)?.into(); - let netuid: u16 = Self::parse_netuid(data, 0x5E)?; + let netuid = Self::parse_netuid(data, 0x5E)?; // We have to treat this as uint256 (because of Solidity ABI encoding rules, it pads uint64), // but this will never exceed 8 bytes, se we will ignore higher bytes and will only use lower From a379a13a9c445d9bc0edb2ff761f16315b87920f Mon Sep 17 00:00:00 2001 From: Greg Zaitsev Date: Tue, 14 Jan 2025 11:01:38 -0500 Subject: [PATCH 4/6] Format --- runtime/src/precompiles/staking.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/src/precompiles/staking.rs b/runtime/src/precompiles/staking.rs index ce70612a6..0f228251c 100644 --- a/runtime/src/precompiles/staking.rs +++ b/runtime/src/precompiles/staking.rs @@ -126,7 +126,7 @@ impl StakingPrecompile { } let mut netuid_bytes = [0u8; 2]; - netuid_bytes.copy_from_slice(get_slice(data, offset, offset+2)?); + netuid_bytes.copy_from_slice(get_slice(data, offset, offset + 2)?); let netuid: u16 = netuid_bytes[1] as u16 | ((netuid_bytes[0] as u16) << 8u16); Ok(netuid) @@ -161,7 +161,7 @@ impl StakingPrecompile { Err(PrecompileFailure::Error { exit_status: ExitError::Other("Subtensor call failed".into()), }) - }, + } } } From 89b31375fba43f78e192edbe1210253cbbf2b7b2 Mon Sep 17 00:00:00 2001 From: Greg Zaitsev Date: Tue, 14 Jan 2025 11:16:44 -0500 Subject: [PATCH 5/6] Fix clippy for staking precompile --- 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 0f228251c..0be8bf240 100644 --- a/runtime/src/precompiles/staking.rs +++ b/runtime/src/precompiles/staking.rs @@ -119,7 +119,7 @@ impl StakingPrecompile { } fn parse_netuid(data: &[u8], offset: usize) -> Result { - if data.len() < (offset + 2) as usize { + if data.len() < offset + 2 { return Err(PrecompileFailure::Error { exit_status: ExitError::InvalidRange, }); From a60b1da91704c5df0395db3658630a9c5ee4a7c7 Mon Sep 17 00:00:00 2001 From: Greg Zaitsev Date: Tue, 14 Jan 2025 11:57:15 -0500 Subject: [PATCH 6/6] Bump spec version --- runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 85ede567a..72221b9ff 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -220,7 +220,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // `spec_version`, and `authoring_version` are the same between Wasm and native. // This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use // the compatible custom types. - spec_version: 221, + spec_version: 222, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 1,