From 6b6431e1af77acbc3d4b6cf29edcff852db595cf Mon Sep 17 00:00:00 2001 From: open-junius Date: Tue, 12 Nov 2024 18:02:12 +0800 Subject: [PATCH 1/4] get stake interface in solidity --- runtime/src/precompiles/solidity/staking.abi | 28 ++++++++++++++++++-- runtime/src/precompiles/solidity/staking.sol | 12 +++++++++ runtime/src/precompiles/staking.rs | 15 +++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/runtime/src/precompiles/solidity/staking.abi b/runtime/src/precompiles/solidity/staking.abi index 44b1829c4..f20d7c338 100644 --- a/runtime/src/precompiles/solidity/staking.abi +++ b/runtime/src/precompiles/solidity/staking.abi @@ -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": [ { @@ -37,7 +61,7 @@ ], "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 ec7fb7297..e0a867638 100644 --- a/runtime/src/precompiles/solidity/staking.sol +++ b/runtime/src/precompiles/solidity/staking.sol @@ -42,4 +42,16 @@ interface IStaking { * - The existing stake amount must be not lower than specified amount */ function removeStake(bytes32 hotkey, uint256 amount, uint16 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). + * @return The current stake amount in uint64 format. + */ + function getStake(bytes32 hotkey, bytes32 coldkey) external view returns (uint64); } diff --git a/runtime/src/precompiles/staking.rs b/runtime/src/precompiles/staking.rs index e6237dfcf..104bb2116 100644 --- a/runtime/src/precompiles/staking.rs +++ b/runtime/src/precompiles/staking.rs @@ -59,6 +59,9 @@ impl StakingPrecompile { id if id == get_method_id("removeStake(bytes32,uint256,uint16)") => { Self::remove_stake(handle, &method_input) } + id if id == get_method_id("getStake(bytes32,bytes32)") => { + Self::get_stake(handle, &method_input) + } _ => Err(PrecompileFailure::Error { exit_status: ExitError::InvalidRange, }), @@ -101,6 +104,18 @@ impl StakingPrecompile { Self::dispatch(handle, call) } + fn get_stake(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { + let hotkey: AccountId32 = Self::parse_hotkey(data)?.into(); + + let data = + pallet_subtensor::Pallet::::get_stake_for_coldkey_and_hotkey(&hotkey, &hotkey); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: data.to_le_bytes().to_vec(), + }) + } + fn parse_hotkey(data: &[u8]) -> Result<[u8; 32], PrecompileFailure> { if data.len() < 32 { return Err(PrecompileFailure::Error { From 9688e037061a66b0bfdb507081c71f76e912fd6a Mon Sep 17 00:00:00 2001 From: open-junius Date: Tue, 12 Nov 2024 21:59:14 +0800 Subject: [PATCH 2/4] parse keys for stake map --- runtime/src/precompiles/staking.rs | 31 ++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/runtime/src/precompiles/staking.rs b/runtime/src/precompiles/staking.rs index 104bb2116..0190db413 100644 --- a/runtime/src/precompiles/staking.rs +++ b/runtime/src/precompiles/staking.rs @@ -60,7 +60,7 @@ impl StakingPrecompile { Self::remove_stake(handle, &method_input) } id if id == get_method_id("getStake(bytes32,bytes32)") => { - Self::get_stake(handle, &method_input) + Self::get_stake(&method_input) } _ => Err(PrecompileFailure::Error { exit_status: ExitError::InvalidRange, @@ -104,18 +104,37 @@ impl StakingPrecompile { Self::dispatch(handle, call) } - fn get_stake(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { - let hotkey: AccountId32 = Self::parse_hotkey(data)?.into(); + fn get_stake(data: &[u8]) -> PrecompileResult { + let (hotkey, coldkey) = Self::parse_hotkey_coldkey(data)?.into(); - let data = - pallet_subtensor::Pallet::::get_stake_for_coldkey_and_hotkey(&hotkey, &hotkey); + let stake = pallet_subtensor::Pallet::::get_stake_for_coldkey_and_hotkey( + &hotkey.into(), + &coldkey.into(), + ); + + let stake_u256 = U256::from(stake); + let mut result = [0_u8; 32]; + U256::to_big_endian(&stake_u256, &mut result); Ok(PrecompileOutput { exit_status: ExitSucceed::Returned, - output: data.to_le_bytes().to_vec(), + output: result.into(), }) } + fn parse_hotkey_coldkey(data: &[u8]) -> Result<([u8; 32], [u8; 32]), PrecompileFailure> { + if data.len() < 64 { + return Err(PrecompileFailure::Error { + exit_status: ExitError::InvalidRange, + }); + } + let mut hotkey = [0u8; 32]; + hotkey.copy_from_slice(get_slice(data, 0, 32)?); + let mut coldkey = [0u8; 32]; + coldkey.copy_from_slice(get_slice(data, 32, 64)?); + Ok((hotkey, coldkey)) + } + fn parse_hotkey(data: &[u8]) -> Result<[u8; 32], PrecompileFailure> { if data.len() < 32 { return Err(PrecompileFailure::Error { From 9f64385ff2e518e2ef5a34da733e9741a4c4a57c Mon Sep 17 00:00:00 2001 From: open-junius Date: Wed, 13 Nov 2024 13:24:42 +0800 Subject: [PATCH 3/4] 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 0190db413..3af0e3e5e 100644 --- a/runtime/src/precompiles/staking.rs +++ b/runtime/src/precompiles/staking.rs @@ -105,7 +105,7 @@ impl StakingPrecompile { } fn get_stake(data: &[u8]) -> PrecompileResult { - let (hotkey, coldkey) = Self::parse_hotkey_coldkey(data)?.into(); + let (hotkey, coldkey) = Self::parse_hotkey_coldkey(data)?; let stake = pallet_subtensor::Pallet::::get_stake_for_coldkey_and_hotkey( &hotkey.into(), From 8647528c8ff61601d9ab0ee94958fd9f4dd1c0e3 Mon Sep 17 00:00:00 2001 From: open-junius Date: Wed, 13 Nov 2024 16:51:11 +0800 Subject: [PATCH 4/4] revert payable attribute --- 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 f20d7c338..167c21882 100644 --- a/runtime/src/precompiles/solidity/staking.abi +++ b/runtime/src/precompiles/solidity/staking.abi @@ -61,7 +61,7 @@ ], "name": "removeStake", "outputs": [], - "stateMutability": "nonpayable", + "stateMutability": "payable", "type": "function" } ] \ No newline at end of file