@@ -9,29 +9,12 @@ use fp_evm::{
9
9
ExitError , ExitSucceed , LinearCostPrecompile , PrecompileFailure , PrecompileHandle ,
10
10
PrecompileOutput , PrecompileResult ,
11
11
} ;
12
- use sp_core:: U256 ;
12
+ use sp_core:: { ByteArray , U256 } ;
13
13
use sp_std:: vec;
14
14
pub const METAGRAPH_PRECOMPILE_INDEX : u64 = 2050 ;
15
+ use sp_runtime:: AccountId32 ;
15
16
pub struct MetagraphPrecompile ;
16
17
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
18
impl MetagraphPrecompile {
36
19
pub fn execute ( handle : & mut impl PrecompileHandle ) -> PrecompileResult {
37
20
log:: error!( "++++++ execute metagraph" ) ;
@@ -43,22 +26,40 @@ impl MetagraphPrecompile {
43
26
44
27
match method_id {
45
28
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)" ) => {
53
48
Self :: get_validator_status ( & method_input)
54
49
}
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)
57
62
}
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
63
64
_ => Err ( PrecompileFailure :: Error {
64
65
exit_status : ExitError :: InvalidRange ,
@@ -67,14 +68,9 @@ impl MetagraphPrecompile {
67
68
}
68
69
69
70
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) ?;
78
74
79
75
log:: error!( "++++++ netuid is {:?}" , netuid) ;
80
76
@@ -91,100 +87,272 @@ impl MetagraphPrecompile {
91
87
}
92
88
93
89
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
+
94
104
Ok ( PrecompileOutput {
95
105
exit_status : ExitSucceed :: Returned ,
96
106
output : [ ] . into ( ) ,
97
107
} )
98
108
}
99
109
100
110
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
+
101
120
Ok ( PrecompileOutput {
102
121
exit_status : ExitSucceed :: Returned ,
103
- output : [ ] . into ( ) ,
122
+ output : result . into ( ) ,
104
123
} )
105
124
}
106
125
107
126
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
+
108
136
Ok ( PrecompileOutput {
109
137
exit_status : ExitSucceed :: Returned ,
110
- output : [ ] . into ( ) ,
138
+ output : result . into ( ) ,
111
139
} )
112
140
}
113
141
114
142
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
+
115
152
Ok ( PrecompileOutput {
116
153
exit_status : ExitSucceed :: Returned ,
117
- output : [ ] . into ( ) ,
154
+ output : result . into ( ) ,
118
155
} )
119
156
}
120
157
121
158
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
+
122
168
Ok ( PrecompileOutput {
123
169
exit_status : ExitSucceed :: Returned ,
124
- output : [ ] . into ( ) ,
170
+ output : result . into ( ) ,
125
171
} )
126
172
}
127
173
128
174
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
+
129
184
Ok ( PrecompileOutput {
130
185
exit_status : ExitSucceed :: Returned ,
131
- output : [ ] . into ( ) ,
186
+ output : result . into ( ) ,
132
187
} )
133
188
}
134
189
135
190
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
+
136
200
Ok ( PrecompileOutput {
137
201
exit_status : ExitSucceed :: Returned ,
138
- output : [ ] . into ( ) ,
202
+ output : result . into ( ) ,
139
203
} )
140
204
}
141
205
142
206
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
+
143
216
Ok ( PrecompileOutput {
144
217
exit_status : ExitSucceed :: Returned ,
145
- output : [ ] . into ( ) ,
218
+ output : result . into ( ) ,
146
219
} )
147
220
}
148
221
149
222
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
+
150
233
Ok ( PrecompileOutput {
151
234
exit_status : ExitSucceed :: Returned ,
152
235
output : [ ] . into ( ) ,
153
236
} )
154
237
}
155
238
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
+
157
249
Ok ( PrecompileOutput {
158
250
exit_status : ExitSucceed :: Returned ,
159
- output : [ ] . into ( ) ,
251
+ output : result . into ( ) ,
160
252
} )
161
253
}
162
254
163
255
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
+
164
265
Ok ( PrecompileOutput {
165
266
exit_status : ExitSucceed :: Returned ,
166
- output : [ ] . into ( ) ,
267
+ output : result . into ( ) ,
167
268
} )
168
269
}
169
270
170
271
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
+
171
286
Ok ( PrecompileOutput {
172
287
exit_status : ExitSucceed :: Returned ,
173
- output : [ ] . into ( ) ,
288
+ output : axon . decode ( ) . into ( ) ,
174
289
} )
175
290
}
176
291
177
292
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
+
178
301
Ok ( PrecompileOutput {
179
302
exit_status : ExitSucceed :: Returned ,
180
- output : [ ] . into ( ) ,
303
+ output : hotkey . as_slice ( ) . into ( ) ,
181
304
} )
182
305
}
183
306
184
307
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
+
185
318
Ok ( PrecompileOutput {
186
319
exit_status : ExitSucceed :: Returned ,
187
- output : [ ] . into ( ) ,
320
+ output : coldkey . as_slice ( ) . into ( ) ,
188
321
} )
189
322
}
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
+ }
190
358
}
0 commit comments