From 50b9b8bc5e254692cb3721d2ea82386621f7f8b6 Mon Sep 17 00:00:00 2001 From: open-junius Date: Tue, 3 Dec 2024 13:32:03 +0800 Subject: [PATCH] axon data support --- runtime/src/precompiles/metagraph.rs | 34 ++++++++++++++++--- .../src/precompiles/solidity/metagraph.sol | 11 ++++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/runtime/src/precompiles/metagraph.rs b/runtime/src/precompiles/metagraph.rs index f53922a3f..f5a1c4c49 100644 --- a/runtime/src/precompiles/metagraph.rs +++ b/runtime/src/precompiles/metagraph.rs @@ -15,6 +15,8 @@ pub const METAGRAPH_PRECOMPILE_INDEX: u64 = 2050; use sp_runtime::AccountId32; pub struct MetagraphPrecompile; +const NO_HOTKEY: &str = "no hotkey"; + impl MetagraphPrecompile { pub fn execute(handle: &mut impl PrecompileHandle) -> PrecompileResult { log::error!("++++++ execute metagraph"); @@ -274,18 +276,40 @@ impl MetagraphPrecompile { let hotkey = pallet_subtensor::Pallet::::get_hotkey_for_net_and_uid(netuid, uid) .map_err(|_| PrecompileFailure::Error { - exit_status: ExitError::InvalidRange, + exit_status: ExitError::Other(sp_version::Cow::Borrowed(NO_HOTKEY)), })?; let axon = pallet_subtensor::Pallet::::get_axon_info(netuid, &hotkey); - // let result_u256 = U256::from(rank); - // let mut result = [0_u8; 32]; - // U256::to_big_endian(&result_u256, &mut result); + let mut block_result = [0_u8; 32]; + U256::to_big_endian(&U256::from(axon.block), &mut block_result); + + let mut version_result = [0_u8; 32]; + U256::to_big_endian(&U256::from(axon.version), &mut version_result); + + let mut ip_result = [0_u8; 32]; + U256::to_big_endian(&U256::from(axon.ip), &mut ip_result); + + let mut port_result = [0_u8; 32]; + U256::to_big_endian(&U256::from(axon.port), &mut port_result); + + let mut ip_type_result = [0_u8; 32]; + U256::to_big_endian(&U256::from(axon.ip_type), &mut ip_type_result); + + let mut protocol_result = [0_u8; 32]; + U256::to_big_endian(&U256::from(axon.protocol), &mut protocol_result); + + let mut result = [0_u8; 192]; + result[..32].copy_from_slice(&block_result); + result[32..64].copy_from_slice(&version_result); + result[64..96].copy_from_slice(&ip_result); + result[96..128].copy_from_slice(&port_result); + result[128..160].copy_from_slice(&ip_type_result); + result[160..].copy_from_slice(&protocol_result); Ok(PrecompileOutput { exit_status: ExitSucceed::Returned, - output: axon.decode().into(), + output: result.into(), }) } diff --git a/runtime/src/precompiles/solidity/metagraph.sol b/runtime/src/precompiles/solidity/metagraph.sol index 50440fe10..adc8f5a78 100644 --- a/runtime/src/precompiles/solidity/metagraph.sol +++ b/runtime/src/precompiles/solidity/metagraph.sol @@ -2,6 +2,15 @@ pragma solidity ^0.8.0; address constant IMetagraph_ADDRESS = 0x0000000000000000000000000000000000000802; +struct AxonInfo { + uint64 block; + uint32 version; + uint128 ip; + uint16 port; + uint8 ip_type; + uint8 protocol; +} + interface IMetagraph { /** @@ -11,4 +20,6 @@ interface IMetagraph { */ function getUidCount(uint16 netuid) external view returns (uint16); + function getAxon(uint16 netuid, uint16 uid) external view returns (AxonInfo memory); + }