Skip to content

Commit 50b9b8b

Browse files
committed
axon data support
1 parent 1bf2730 commit 50b9b8b

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

runtime/src/precompiles/metagraph.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ pub const METAGRAPH_PRECOMPILE_INDEX: u64 = 2050;
1515
use sp_runtime::AccountId32;
1616
pub struct MetagraphPrecompile;
1717

18+
const NO_HOTKEY: &str = "no hotkey";
19+
1820
impl MetagraphPrecompile {
1921
pub fn execute(handle: &mut impl PrecompileHandle) -> PrecompileResult {
2022
log::error!("++++++ execute metagraph");
@@ -274,18 +276,40 @@ impl MetagraphPrecompile {
274276

275277
let hotkey = pallet_subtensor::Pallet::<Runtime>::get_hotkey_for_net_and_uid(netuid, uid)
276278
.map_err(|_| PrecompileFailure::Error {
277-
exit_status: ExitError::InvalidRange,
279+
exit_status: ExitError::Other(sp_version::Cow::Borrowed(NO_HOTKEY)),
278280
})?;
279281

280282
let axon = pallet_subtensor::Pallet::<Runtime>::get_axon_info(netuid, &hotkey);
281283

282-
// let result_u256 = U256::from(rank);
283-
// let mut result = [0_u8; 32];
284-
// U256::to_big_endian(&result_u256, &mut result);
284+
let mut block_result = [0_u8; 32];
285+
U256::to_big_endian(&U256::from(axon.block), &mut block_result);
286+
287+
let mut version_result = [0_u8; 32];
288+
U256::to_big_endian(&U256::from(axon.version), &mut version_result);
289+
290+
let mut ip_result = [0_u8; 32];
291+
U256::to_big_endian(&U256::from(axon.ip), &mut ip_result);
292+
293+
let mut port_result = [0_u8; 32];
294+
U256::to_big_endian(&U256::from(axon.port), &mut port_result);
295+
296+
let mut ip_type_result = [0_u8; 32];
297+
U256::to_big_endian(&U256::from(axon.ip_type), &mut ip_type_result);
298+
299+
let mut protocol_result = [0_u8; 32];
300+
U256::to_big_endian(&U256::from(axon.protocol), &mut protocol_result);
301+
302+
let mut result = [0_u8; 192];
303+
result[..32].copy_from_slice(&block_result);
304+
result[32..64].copy_from_slice(&version_result);
305+
result[64..96].copy_from_slice(&ip_result);
306+
result[96..128].copy_from_slice(&port_result);
307+
result[128..160].copy_from_slice(&ip_type_result);
308+
result[160..].copy_from_slice(&protocol_result);
285309

286310
Ok(PrecompileOutput {
287311
exit_status: ExitSucceed::Returned,
288-
output: axon.decode().into(),
312+
output: result.into(),
289313
})
290314
}
291315

runtime/src/precompiles/solidity/metagraph.sol

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ pragma solidity ^0.8.0;
22

33
address constant IMetagraph_ADDRESS = 0x0000000000000000000000000000000000000802;
44

5+
struct AxonInfo {
6+
uint64 block;
7+
uint32 version;
8+
uint128 ip;
9+
uint16 port;
10+
uint8 ip_type;
11+
uint8 protocol;
12+
}
13+
514
interface IMetagraph {
615

716
/**
@@ -11,4 +20,6 @@ interface IMetagraph {
1120
*/
1221
function getUidCount(uint16 netuid) external view returns (uint16);
1322

23+
function getAxon(uint16 netuid, uint16 uid) external view returns (AxonInfo memory);
24+
1425
}

0 commit comments

Comments
 (0)