Skip to content

Commit 04300f5

Browse files
committed
init solution
1 parent 9da2947 commit 04300f5

File tree

4 files changed

+231
-0
lines changed

4 files changed

+231
-0
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
extern crate alloc;
2+
3+
use alloc::vec::Vec;
4+
5+
use crate::precompiles::{get_method_id, get_slice};
6+
use crate::{Runtime, RuntimeCall};
7+
use ed25519_dalek::{Signature, Verifier, VerifyingKey};
8+
use fp_evm::{
9+
ExitError, ExitSucceed, LinearCostPrecompile, PrecompileFailure, PrecompileHandle,
10+
PrecompileOutput, PrecompileResult,
11+
};
12+
use sp_core::U256;
13+
use sp_std::vec;
14+
pub const METAGRAPH_PRECOMPILE_INDEX: u64 = 2050;
15+
pub struct MetagraphPrecompile;
16+
17+
/*
18+
get_uid_count SubnetworkN
19+
get_stake Total stake of the neuron in Tao
20+
get_rank Rank score of the neuron
21+
get_trust Trust score assigned to the neuron by other neurons
22+
get_consensus Consensus score of the neuron
23+
get_incentive Incentive score representing the neuron's incentive alignment
24+
get_dividends Dividends earned by the neuron
25+
get_emission Emission received by the neuron (with 18 decimals)
26+
get_vtrust Validator trust score indicating the network's trust in the neuron as a validator
27+
get_validator_status Validator status of the neuron
28+
get_last_updated Number of blocks since the neuron's last update
29+
get_is_active Activity status of the neuron
30+
get_axon Network endpoint information of the neuron
31+
get_hotkey Hotkey (public key as bytes32) of the neuron
32+
get_coldkey Coldkey (public key as bytes32) of the neuron
33+
*/
34+
35+
impl MetagraphPrecompile {
36+
pub fn execute(handle: &mut impl PrecompileHandle) -> PrecompileResult {
37+
log::error!("++++++ execute metagraph");
38+
let txdata = handle.input();
39+
let method_id = get_slice(txdata, 0, 4)?;
40+
let method_input = txdata
41+
.get(4..)
42+
.map_or_else(vec::Vec::new, |slice| slice.to_vec()); // Avoiding borrowing conflicts
43+
44+
match method_id {
45+
id if id == get_method_id("getUidCount(uint16)") => Self::get_uid_count(&method_input),
46+
id if id == get_method_id("getUidCount(uint16)") => Self::get_stake(&method_input),
47+
id if id == get_method_id("getUidCount(uint16)") => Self::get_rank(&method_input),
48+
id if id == get_method_id("getUidCount(uint16)") => Self::get_trust(&method_input),
49+
id if id == get_method_id("getUidCount(uint16)") => Self::get_consensus(&method_input),
50+
id if id == get_method_id("getUidCount(uint16)") => Self::get_emission(&method_input),
51+
id if id == get_method_id("getUidCount(uint16)") => Self::get_vtrust(&method_input),
52+
id if id == get_method_id("getUidCount(uint16)") => {
53+
Self::get_validator_status(&method_input)
54+
}
55+
id if id == get_method_id("getUidCount(uint16)") => {
56+
Self::get_last_updated(&method_input)
57+
}
58+
id if id == get_method_id("getUidCount(uint16)") => Self::get_is_active(&method_input),
59+
id if id == get_method_id("getUidCount(uint16)") => Self::get_axon(&method_input),
60+
id if id == get_method_id("getUidCount(uint16)") => Self::get_hotkey(&method_input),
61+
id if id == get_method_id("getUidCount(uint16)") => Self::get_coldkey(&method_input),
62+
63+
_ => Err(PrecompileFailure::Error {
64+
exit_status: ExitError::InvalidRange,
65+
}),
66+
}
67+
}
68+
69+
fn get_uid_count(data: &[u8]) -> PrecompileResult {
70+
if data.len() < 2 {
71+
return Err(PrecompileFailure::Error {
72+
exit_status: ExitError::InvalidRange,
73+
});
74+
}
75+
let mut netuid = [0u8; 2];
76+
netuid.copy_from_slice(get_slice(data, 0, 2)?);
77+
let netuid = u16::from_be_bytes(netuid);
78+
79+
log::error!("++++++ netuid is {:?}", netuid);
80+
81+
let uid_count = pallet_subtensor::SubnetworkN::<Runtime>::get(netuid);
82+
83+
let uid_count_u256 = U256::from(uid_count);
84+
let mut result = [0_u8; 32];
85+
U256::to_big_endian(&uid_count_u256, &mut result);
86+
87+
Ok(PrecompileOutput {
88+
exit_status: ExitSucceed::Returned,
89+
output: result.into(),
90+
})
91+
}
92+
93+
fn get_stake(data: &[u8]) -> PrecompileResult {
94+
Ok(PrecompileOutput {
95+
exit_status: ExitSucceed::Returned,
96+
output: [].into(),
97+
})
98+
}
99+
100+
fn get_rank(data: &[u8]) -> PrecompileResult {
101+
Ok(PrecompileOutput {
102+
exit_status: ExitSucceed::Returned,
103+
output: [].into(),
104+
})
105+
}
106+
107+
fn get_trust(data: &[u8]) -> PrecompileResult {
108+
Ok(PrecompileOutput {
109+
exit_status: ExitSucceed::Returned,
110+
output: [].into(),
111+
})
112+
}
113+
114+
fn get_consensus(data: &[u8]) -> PrecompileResult {
115+
Ok(PrecompileOutput {
116+
exit_status: ExitSucceed::Returned,
117+
output: [].into(),
118+
})
119+
}
120+
121+
fn get_incentive(data: &[u8]) -> PrecompileResult {
122+
Ok(PrecompileOutput {
123+
exit_status: ExitSucceed::Returned,
124+
output: [].into(),
125+
})
126+
}
127+
128+
fn get_dividends(data: &[u8]) -> PrecompileResult {
129+
Ok(PrecompileOutput {
130+
exit_status: ExitSucceed::Returned,
131+
output: [].into(),
132+
})
133+
}
134+
135+
fn get_emission(data: &[u8]) -> PrecompileResult {
136+
Ok(PrecompileOutput {
137+
exit_status: ExitSucceed::Returned,
138+
output: [].into(),
139+
})
140+
}
141+
142+
fn get_vtrust(data: &[u8]) -> PrecompileResult {
143+
Ok(PrecompileOutput {
144+
exit_status: ExitSucceed::Returned,
145+
output: [].into(),
146+
})
147+
}
148+
149+
fn get_validator_status(data: &[u8]) -> PrecompileResult {
150+
Ok(PrecompileOutput {
151+
exit_status: ExitSucceed::Returned,
152+
output: [].into(),
153+
})
154+
}
155+
156+
fn get_last_updated(data: &[u8]) -> PrecompileResult {
157+
Ok(PrecompileOutput {
158+
exit_status: ExitSucceed::Returned,
159+
output: [].into(),
160+
})
161+
}
162+
163+
fn get_is_active(data: &[u8]) -> PrecompileResult {
164+
Ok(PrecompileOutput {
165+
exit_status: ExitSucceed::Returned,
166+
output: [].into(),
167+
})
168+
}
169+
170+
fn get_axon(data: &[u8]) -> PrecompileResult {
171+
Ok(PrecompileOutput {
172+
exit_status: ExitSucceed::Returned,
173+
output: [].into(),
174+
})
175+
}
176+
177+
fn get_hotkey(data: &[u8]) -> PrecompileResult {
178+
Ok(PrecompileOutput {
179+
exit_status: ExitSucceed::Returned,
180+
output: [].into(),
181+
})
182+
}
183+
184+
fn get_coldkey(data: &[u8]) -> PrecompileResult {
185+
Ok(PrecompileOutput {
186+
exit_status: ExitSucceed::Returned,
187+
output: [].into(),
188+
})
189+
}
190+
}

runtime/src/precompiles/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ use pallet_evm_precompile_simple::{ECRecover, ECRecoverPublicKey, Identity, Ripe
1313
// Include custom precompiles
1414
mod balance_transfer;
1515
mod ed25519;
16+
mod metagraph;
1617
mod staking;
1718

1819
use balance_transfer::*;
1920
use ed25519::*;
21+
use metagraph::*;
2022
use staking::*;
2123

2224
pub struct FrontierPrecompiles<R>(PhantomData<R>);
@@ -73,6 +75,10 @@ where
7375
Some(BalanceTransferPrecompile::execute(handle))
7476
}
7577
a if a == hash(STAKING_PRECOMPILE_INDEX) => Some(StakingPrecompile::execute(handle)),
78+
a if a == hash(METAGRAPH_PRECOMPILE_INDEX) => {
79+
Some(MetagraphPrecompile::execute(handle))
80+
}
81+
7682
_ => None,
7783
}
7884
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[
2+
{
3+
"inputs": [
4+
{
5+
"internalType": "uint16",
6+
"name": "netuid",
7+
"type": "uint16"
8+
}
9+
],
10+
"name": "getUidCount",
11+
"outputs": [
12+
{
13+
"internalType": "uint16",
14+
"name": "",
15+
"type": "uint16"
16+
}
17+
],
18+
"stateMutability": "view",
19+
"type": "function"
20+
}
21+
]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pragma solidity ^0.8.0;
2+
3+
address constant IMetagraph_ADDRESS = 0x0000000000000000000000000000000000000802;
4+
5+
interface IMetagraph {
6+
7+
/**
8+
* @dev Returns the count of unique identifiers (UIDs) associated with a given network identifier (netuid).
9+
* @param netuid The network identifier for which to retrieve the UID count.
10+
* @return The count of UIDs associated with the specified netuid.
11+
*/
12+
function getUidCount(uint16 netuid) external view returns (uint16);
13+
14+
}

0 commit comments

Comments
 (0)