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/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, diff --git a/runtime/src/precompiles/solidity/staking.abi b/runtime/src/precompiles/solidity/staking.abi index 167c21882..1332718ad 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": [ { @@ -54,14 +78,14 @@ "type": "uint256" }, { - "internalType": "uint16", + "internalType": "uint256", "name": "netuid", - "type": "uint16" + "type": "uint256" } ], "name": "removeStake", "outputs": [], - "stateMutability": "payable", + "stateMutability": "nonpayable", "type": "function" } ] \ No newline at end of file diff --git a/runtime/src/precompiles/solidity/staking.sol b/runtime/src/precompiles/solidity/staking.sol index 441610693..c7efe5a8a 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). * * 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,26 +33,25 @@ 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). * * Requirements: * - `hotkey` must be a valid hotkey registered on the network, ensuring that the stake is * 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; - /** - * @dev Returns the stake amount associated with the specified `hotkey` and `coldkey`. - * - * This function retrieves the current stake amount linked to a specific hotkey and coldkey pair. - * It is a view function, meaning it does not modify the state of the contract and is free to call. - * - * @param hotkey The hotkey public key (32 bytes). - * @param coldkey The coldkey public key (32 bytes). - * @param netuid The subnet the stake is on (uint16). - * @return The current stake amount in uint64 format. - */ - function getStake(bytes32 hotkey, bytes32 coldkey, uint16 netuid) external view returns (uint64); + /** + * @dev Returns the stake amount associated with the specified `hotkey` and `coldkey`. + * + * This function retrieves the current stake amount linked to a specific hotkey and coldkey pair. + * It is a view function, meaning it does not modify the state of the contract and is free to call. + * + * @param hotkey The hotkey public key (32 bytes). + * @param coldkey The coldkey public key (32 bytes). + * @param netuid The subnet the stake is on (uint256). + * @return The current stake amount in uint64 format. + */ + function getStake(bytes32 hotkey, bytes32 coldkey, uint256 netuid) external view returns (uint64); } diff --git a/runtime/src/precompiles/staking.rs b/runtime/src/precompiles/staking.rs index 9d48501d4..f4439b8a9 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) } id if id == get_method_id("getStake(bytes32,bytes32,uint16)") => { @@ -71,9 +71,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 = Self::parse_netuid(data, 0x3E)?; let amount_sub = ::BalanceConverter::into_substrate_balance(amount) @@ -88,11 +86,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 = 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 @@ -157,6 +154,20 @@ impl StakingPrecompile { Ok(hotkey) } + fn parse_netuid(data: &[u8], offset: usize) -> Result { + if data.len() < offset + 2 { + 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( @@ -181,9 +192,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()), + }) + } } }