Skip to content

Commit

Permalink
parse keys for stake map
Browse files Browse the repository at this point in the history
  • Loading branch information
open-junius committed Nov 12, 2024
1 parent 6b6431e commit 9688e03
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions runtime/src/precompiles/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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::<Runtime>::get_stake_for_coldkey_and_hotkey(&hotkey, &hotkey);
let stake = pallet_subtensor::Pallet::<Runtime>::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 {
Expand Down

0 comments on commit 9688e03

Please sign in to comment.