Skip to content

Commit 1bf2730

Browse files
committed
more method definition
1 parent 04300f5 commit 1bf2730

File tree

1 file changed

+221
-53
lines changed

1 file changed

+221
-53
lines changed

runtime/src/precompiles/metagraph.rs

Lines changed: 221 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,12 @@ use fp_evm::{
99
ExitError, ExitSucceed, LinearCostPrecompile, PrecompileFailure, PrecompileHandle,
1010
PrecompileOutput, PrecompileResult,
1111
};
12-
use sp_core::U256;
12+
use sp_core::{ByteArray, U256};
1313
use sp_std::vec;
1414
pub const METAGRAPH_PRECOMPILE_INDEX: u64 = 2050;
15+
use sp_runtime::AccountId32;
1516
pub struct MetagraphPrecompile;
1617

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-
3518
impl MetagraphPrecompile {
3619
pub fn execute(handle: &mut impl PrecompileHandle) -> PrecompileResult {
3720
log::error!("++++++ execute metagraph");
@@ -43,22 +26,40 @@ impl MetagraphPrecompile {
4326

4427
match method_id {
4528
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)") => {
29+
id if id == get_method_id("getStake(uint16,uint16)") => Self::get_stake(&method_input),
30+
id if id == get_method_id("getRank(uint16,uint16)") => Self::get_rank(&method_input),
31+
id if id == get_method_id("getTrust(uint16,uint16)") => Self::get_trust(&method_input),
32+
id if id == get_method_id("getConsensus(uint16,uint16)") => {
33+
Self::get_consensus(&method_input)
34+
}
35+
id if id == get_method_id("getIncentive(uint16,uint16)") => {
36+
Self::get_incentive(&method_input)
37+
}
38+
id if id == get_method_id("getDividends(uint16,uint16)") => {
39+
Self::get_dividends(&method_input)
40+
}
41+
id if id == get_method_id("getEmission(uint16,uint16)") => {
42+
Self::get_emission(&method_input)
43+
}
44+
id if id == get_method_id("getVtrust(uint16,uint16)") => {
45+
Self::get_vtrust(&method_input)
46+
}
47+
id if id == get_method_id("getValidatorStatus(uint16,uint16)") => {
5348
Self::get_validator_status(&method_input)
5449
}
55-
id if id == get_method_id("getUidCount(uint16)") => {
56-
Self::get_last_updated(&method_input)
50+
id if id == get_method_id("getLastUpdate(uint16,uint16)") => {
51+
Self::get_last_update(&method_input)
52+
}
53+
id if id == get_method_id("getIsActive(uint16,uint16)") => {
54+
Self::get_is_active(&method_input)
55+
}
56+
id if id == get_method_id("getAxon(uint16,uint16)") => Self::get_axon(&method_input),
57+
id if id == get_method_id("getHotkey(uint16,uint16)") => {
58+
Self::get_hotkey(&method_input)
59+
}
60+
id if id == get_method_id("getColdkey(uint16,uint16)") => {
61+
Self::get_coldkey(&method_input)
5762
}
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),
6263

6364
_ => Err(PrecompileFailure::Error {
6465
exit_status: ExitError::InvalidRange,
@@ -67,14 +68,9 @@ impl MetagraphPrecompile {
6768
}
6869

6970
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);
71+
log::error!("++++++ data len is {:?}", data.len());
72+
73+
let netuid = Self::parse_netuid(data)?;
7874

7975
log::error!("++++++ netuid is {:?}", netuid);
8076

@@ -91,100 +87,272 @@ impl MetagraphPrecompile {
9187
}
9288

9389
fn get_stake(data: &[u8]) -> PrecompileResult {
90+
let netuid = Self::parse_netuid(data)?;
91+
let uid = Self::parse_uid(&data[32..])?;
92+
93+
let hotkey = pallet_subtensor::Pallet::<Runtime>::get_hotkey_for_net_and_uid(netuid, uid)
94+
.map_err(|_| PrecompileFailure::Error {
95+
exit_status: ExitError::InvalidRange,
96+
})?;
97+
98+
let stake = pallet_subtensor::TotalHotkeyStake::<Runtime>::get(&hotkey);
99+
100+
let result_u256 = U256::from(stake);
101+
let mut result = [0_u8; 32];
102+
U256::to_big_endian(&result_u256, &mut result);
103+
94104
Ok(PrecompileOutput {
95105
exit_status: ExitSucceed::Returned,
96106
output: [].into(),
97107
})
98108
}
99109

100110
fn get_rank(data: &[u8]) -> PrecompileResult {
111+
let netuid = Self::parse_netuid(data)?;
112+
let uid = Self::parse_uid(&data[32..])?;
113+
114+
let rank = pallet_subtensor::Pallet::<Runtime>::get_rank_for_uid(netuid, uid);
115+
116+
let result_u256 = U256::from(rank);
117+
let mut result = [0_u8; 32];
118+
U256::to_big_endian(&result_u256, &mut result);
119+
101120
Ok(PrecompileOutput {
102121
exit_status: ExitSucceed::Returned,
103-
output: [].into(),
122+
output: result.into(),
104123
})
105124
}
106125

107126
fn get_trust(data: &[u8]) -> PrecompileResult {
127+
let netuid = Self::parse_netuid(data)?;
128+
let uid = Self::parse_uid(&data[32..])?;
129+
130+
let trust = pallet_subtensor::Pallet::<Runtime>::get_trust_for_uid(netuid, uid);
131+
132+
let result_u256 = U256::from(trust);
133+
let mut result = [0_u8; 32];
134+
U256::to_big_endian(&result_u256, &mut result);
135+
108136
Ok(PrecompileOutput {
109137
exit_status: ExitSucceed::Returned,
110-
output: [].into(),
138+
output: result.into(),
111139
})
112140
}
113141

114142
fn get_consensus(data: &[u8]) -> PrecompileResult {
143+
let netuid = Self::parse_netuid(data)?;
144+
let uid = Self::parse_uid(&data[32..])?;
145+
146+
let consensus = pallet_subtensor::Pallet::<Runtime>::get_consensus_for_uid(netuid, uid);
147+
148+
let result_u256 = U256::from(consensus);
149+
let mut result = [0_u8; 32];
150+
U256::to_big_endian(&result_u256, &mut result);
151+
115152
Ok(PrecompileOutput {
116153
exit_status: ExitSucceed::Returned,
117-
output: [].into(),
154+
output: result.into(),
118155
})
119156
}
120157

121158
fn get_incentive(data: &[u8]) -> PrecompileResult {
159+
let netuid = Self::parse_netuid(data)?;
160+
let uid = Self::parse_uid(&data[32..])?;
161+
162+
let incentive = pallet_subtensor::Pallet::<Runtime>::get_incentive_for_uid(netuid, uid);
163+
164+
let result_u256 = U256::from(incentive);
165+
let mut result = [0_u8; 32];
166+
U256::to_big_endian(&result_u256, &mut result);
167+
122168
Ok(PrecompileOutput {
123169
exit_status: ExitSucceed::Returned,
124-
output: [].into(),
170+
output: result.into(),
125171
})
126172
}
127173

128174
fn get_dividends(data: &[u8]) -> PrecompileResult {
175+
let netuid = Self::parse_netuid(data)?;
176+
let uid = Self::parse_uid(&data[32..])?;
177+
178+
let dividends = pallet_subtensor::Pallet::<Runtime>::get_dividends_for_uid(netuid, uid);
179+
180+
let result_u256 = U256::from(dividends);
181+
let mut result = [0_u8; 32];
182+
U256::to_big_endian(&result_u256, &mut result);
183+
129184
Ok(PrecompileOutput {
130185
exit_status: ExitSucceed::Returned,
131-
output: [].into(),
186+
output: result.into(),
132187
})
133188
}
134189

135190
fn get_emission(data: &[u8]) -> PrecompileResult {
191+
let netuid = Self::parse_netuid(data)?;
192+
let uid = Self::parse_uid(&data[32..])?;
193+
194+
let emission = pallet_subtensor::Pallet::<Runtime>::get_emission_for_uid(netuid, uid);
195+
196+
let result_u256 = U256::from(emission);
197+
let mut result = [0_u8; 32];
198+
U256::to_big_endian(&result_u256, &mut result);
199+
136200
Ok(PrecompileOutput {
137201
exit_status: ExitSucceed::Returned,
138-
output: [].into(),
202+
output: result.into(),
139203
})
140204
}
141205

142206
fn get_vtrust(data: &[u8]) -> PrecompileResult {
207+
let netuid = Self::parse_netuid(data)?;
208+
let uid = Self::parse_uid(&data[32..])?;
209+
210+
let vtrust = pallet_subtensor::Pallet::<Runtime>::get_validator_trust_for_uid(netuid, uid);
211+
212+
let result_u256 = U256::from(vtrust);
213+
let mut result = [0_u8; 32];
214+
U256::to_big_endian(&result_u256, &mut result);
215+
143216
Ok(PrecompileOutput {
144217
exit_status: ExitSucceed::Returned,
145-
output: [].into(),
218+
output: result.into(),
146219
})
147220
}
148221

149222
fn get_validator_status(data: &[u8]) -> PrecompileResult {
223+
let netuid = Self::parse_netuid(data)?;
224+
let uid = Self::parse_uid(&data[32..])?;
225+
226+
let validator_permit =
227+
pallet_subtensor::Pallet::<Runtime>::get_validator_permit_for_uid(netuid, uid);
228+
229+
// let result_u256 = U256::from(validator_status);
230+
// let mut result = [0_u8; 32];
231+
// U256::to_big_endian(&result_u256, &mut result);
232+
150233
Ok(PrecompileOutput {
151234
exit_status: ExitSucceed::Returned,
152235
output: [].into(),
153236
})
154237
}
155238

156-
fn get_last_updated(data: &[u8]) -> PrecompileResult {
239+
fn get_last_update(data: &[u8]) -> PrecompileResult {
240+
let netuid = Self::parse_netuid(data)?;
241+
let uid = Self::parse_uid(&data[32..])?;
242+
243+
let last_update = pallet_subtensor::Pallet::<Runtime>::get_last_update_for_uid(netuid, uid);
244+
245+
let result_u256 = U256::from(last_update);
246+
let mut result = [0_u8; 32];
247+
U256::to_big_endian(&result_u256, &mut result);
248+
157249
Ok(PrecompileOutput {
158250
exit_status: ExitSucceed::Returned,
159-
output: [].into(),
251+
output: result.into(),
160252
})
161253
}
162254

163255
fn get_is_active(data: &[u8]) -> PrecompileResult {
256+
let netuid = Self::parse_netuid(data)?;
257+
let uid = Self::parse_uid(&data[32..])?;
258+
259+
let rank = pallet_subtensor::Pallet::<Runtime>::get_rank_for_uid(netuid, uid);
260+
261+
let result_u256 = U256::from(rank);
262+
let mut result = [0_u8; 32];
263+
U256::to_big_endian(&result_u256, &mut result);
264+
164265
Ok(PrecompileOutput {
165266
exit_status: ExitSucceed::Returned,
166-
output: [].into(),
267+
output: result.into(),
167268
})
168269
}
169270

170271
fn get_axon(data: &[u8]) -> PrecompileResult {
272+
let netuid = Self::parse_netuid(data)?;
273+
let uid = Self::parse_uid(&data[32..])?;
274+
275+
let hotkey = pallet_subtensor::Pallet::<Runtime>::get_hotkey_for_net_and_uid(netuid, uid)
276+
.map_err(|_| PrecompileFailure::Error {
277+
exit_status: ExitError::InvalidRange,
278+
})?;
279+
280+
let axon = pallet_subtensor::Pallet::<Runtime>::get_axon_info(netuid, &hotkey);
281+
282+
// let result_u256 = U256::from(rank);
283+
// let mut result = [0_u8; 32];
284+
// U256::to_big_endian(&result_u256, &mut result);
285+
171286
Ok(PrecompileOutput {
172287
exit_status: ExitSucceed::Returned,
173-
output: [].into(),
288+
output: axon.decode().into(),
174289
})
175290
}
176291

177292
fn get_hotkey(data: &[u8]) -> PrecompileResult {
293+
let netuid = Self::parse_netuid(data)?;
294+
let uid = Self::parse_uid(&data[32..])?;
295+
296+
let hotkey = pallet_subtensor::Pallet::<Runtime>::get_hotkey_for_net_and_uid(netuid, uid)
297+
.map_err(|_| PrecompileFailure::Error {
298+
exit_status: ExitError::InvalidRange,
299+
})?;
300+
178301
Ok(PrecompileOutput {
179302
exit_status: ExitSucceed::Returned,
180-
output: [].into(),
303+
output: hotkey.as_slice().into(),
181304
})
182305
}
183306

184307
fn get_coldkey(data: &[u8]) -> PrecompileResult {
308+
let netuid = Self::parse_netuid(data)?;
309+
let uid = Self::parse_uid(&data[32..])?;
310+
311+
let hotkey = pallet_subtensor::Pallet::<Runtime>::get_hotkey_for_net_and_uid(netuid, uid)
312+
.map_err(|_| PrecompileFailure::Error {
313+
exit_status: ExitError::InvalidRange,
314+
})?;
315+
316+
let coldkey = pallet_subtensor::Owner::<Runtime>::get(&hotkey);
317+
185318
Ok(PrecompileOutput {
186319
exit_status: ExitSucceed::Returned,
187-
output: [].into(),
320+
output: coldkey.as_slice().into(),
188321
})
189322
}
323+
324+
fn parse_netuid(data: &[u8]) -> Result<u16, PrecompileFailure> {
325+
if data.len() < 32 {
326+
return Err(PrecompileFailure::Error {
327+
exit_status: ExitError::InvalidRange,
328+
});
329+
}
330+
let mut netuid = [0u8; 2];
331+
netuid.copy_from_slice(get_slice(data, 30, 32)?);
332+
let result = u16::from_be_bytes(netuid);
333+
Ok(result)
334+
}
335+
336+
fn parse_uid(data: &[u8]) -> Result<u16, PrecompileFailure> {
337+
if data.len() < 32 {
338+
return Err(PrecompileFailure::Error {
339+
exit_status: ExitError::InvalidRange,
340+
});
341+
}
342+
let mut uid = [0u8; 2];
343+
uid.copy_from_slice(get_slice(data, 30, 32)?);
344+
let result = u16::from_be_bytes(uid);
345+
Ok(result)
346+
}
347+
348+
fn parse_hotkey(data: &[u8]) -> Result<[u8; 32], PrecompileFailure> {
349+
if data.len() < 32 {
350+
return Err(PrecompileFailure::Error {
351+
exit_status: ExitError::InvalidRange,
352+
});
353+
}
354+
let mut hotkey = [0u8; 32];
355+
hotkey.copy_from_slice(get_slice(data, 0, 32)?);
356+
Ok(hotkey)
357+
}
190358
}

0 commit comments

Comments
 (0)