1
- extern crate alloc;
2
- use crate :: precompiles:: { get_method_id, get_slice} ;
3
- use crate :: Runtime ;
4
1
use fp_evm:: {
5
2
ExitError , ExitSucceed , PrecompileFailure , PrecompileHandle , PrecompileOutput , PrecompileResult ,
6
3
} ;
7
4
use sp_core:: { ByteArray , U256 } ;
8
5
use sp_std:: vec;
9
- pub const METAGRAPH_PRECOMPILE_INDEX : u64 = 2050 ;
6
+
7
+ use crate :: precompiles:: { get_method_id, parse_slice, PrecompileExt , PrecompileHandleExt } ;
8
+ use crate :: Runtime ;
9
+
10
10
pub struct MetagraphPrecompile ;
11
11
12
12
const NO_HOTKEY : & str = "no hotkey" ;
13
13
14
+ #[ precompile_utils:: precompile]
14
15
impl MetagraphPrecompile {
15
- pub fn execute ( handle : & mut impl PrecompileHandle ) -> PrecompileResult {
16
- let txdata = handle. input ( ) ;
17
- let method_id = get_slice ( txdata, 0 , 4 ) ?;
18
- let method_input = txdata
19
- . get ( 4 ..)
20
- . map_or_else ( vec:: Vec :: new, |slice| slice. to_vec ( ) ) ; // Avoiding borrowing conflicts
21
-
22
- match method_id {
23
- id if id == get_method_id ( "getUidCount(uint16)" ) => Self :: get_uid_count ( & method_input) ,
24
- id if id == get_method_id ( "getStake(uint16,uint16)" ) => Self :: get_stake ( & method_input) ,
25
- id if id == get_method_id ( "getRank(uint16,uint16)" ) => Self :: get_rank ( & method_input) ,
26
- id if id == get_method_id ( "getTrust(uint16,uint16)" ) => Self :: get_trust ( & method_input) ,
27
- id if id == get_method_id ( "getConsensus(uint16,uint16)" ) => {
28
- Self :: get_consensus ( & method_input)
29
- }
30
- id if id == get_method_id ( "getIncentive(uint16,uint16)" ) => {
31
- Self :: get_incentive ( & method_input)
32
- }
33
- id if id == get_method_id ( "getDividends(uint16,uint16)" ) => {
34
- Self :: get_dividends ( & method_input)
35
- }
36
- id if id == get_method_id ( "getEmission(uint16,uint16)" ) => {
37
- Self :: get_emission ( & method_input)
38
- }
39
- id if id == get_method_id ( "getVtrust(uint16,uint16)" ) => {
40
- Self :: get_vtrust ( & method_input)
41
- }
42
- id if id == get_method_id ( "getValidatorStatus(uint16,uint16)" ) => {
43
- Self :: get_validator_status ( & method_input)
44
- }
45
- id if id == get_method_id ( "getLastUpdate(uint16,uint16)" ) => {
46
- Self :: get_last_update ( & method_input)
47
- }
48
- id if id == get_method_id ( "getIsActive(uint16,uint16)" ) => {
49
- Self :: get_is_active ( & method_input)
50
- }
51
- id if id == get_method_id ( "getAxon(uint16,uint16)" ) => Self :: get_axon ( & method_input) ,
52
- id if id == get_method_id ( "getHotkey(uint16,uint16)" ) => {
53
- Self :: get_hotkey ( & method_input)
54
- }
55
- id if id == get_method_id ( "getColdkey(uint16,uint16)" ) => {
56
- Self :: get_coldkey ( & method_input)
57
- }
58
-
59
- _ => Err ( PrecompileFailure :: Error {
60
- exit_status : ExitError :: InvalidRange ,
61
- } ) ,
62
- }
63
- }
16
+ // pub fn execute(handle: &mut impl PrecompileHandle) -> PrecompileResult {
17
+ // let txdata = handle.input();
18
+ // let method_id = get_slice(txdata, 0, 4)?;
19
+ // let method_input = txdata
20
+ // .get(4..)
21
+ // .map_or_else(vec::Vec::new, |slice| slice.to_vec()); // Avoiding borrowing conflicts
22
+
23
+ // match method_id {
24
+ // id if id == get_method_id("getUidCount(uint16)") => Self::get_uid_count(&method_input),
25
+ // id if id == get_method_id("getStake(uint16,uint16)") => Self::get_stake(&method_input),
26
+ // id if id == get_method_id("getRank(uint16,uint16)") => Self::get_rank(&method_input),
27
+ // id if id == get_method_id("getTrust(uint16,uint16)") => Self::get_trust(&method_input),
28
+ // id if id == get_method_id("getConsensus(uint16,uint16)") => {
29
+ // Self::get_consensus(&method_input)
30
+ // }
31
+ // id if id == get_method_id("getIncentive(uint16,uint16)") => {
32
+ // Self::get_incentive(&method_input)
33
+ // }
34
+ // id if id == get_method_id("getDividends(uint16,uint16)") => {
35
+ // Self::get_dividends(&method_input)
36
+ // }
37
+ // id if id == get_method_id("getEmission(uint16,uint16)") => {
38
+ // Self::get_emission(&method_input)
39
+ // }
40
+ // id if id == get_method_id("getVtrust(uint16,uint16)") => {
41
+ // Self::get_vtrust(&method_input)
42
+ // }
43
+ // id if id == get_method_id("getValidatorStatus(uint16,uint16)") => {
44
+ // Self::get_validator_status(&method_input)
45
+ // }
46
+ // id if id == get_method_id("getLastUpdate(uint16,uint16)") => {
47
+ // Self::get_last_update(&method_input)
48
+ // }
49
+ // id if id == get_method_id("getIsActive(uint16,uint16)") => {
50
+ // Self::get_is_active(&method_input)
51
+ // }
52
+ // id if id == get_method_id("getAxon(uint16,uint16)") => Self::get_axon(&method_input),
53
+ // id if id == get_method_id("getHotkey(uint16,uint16)") => {
54
+ // Self::get_hotkey(&method_input)
55
+ // }
56
+ // id if id == get_method_id("getColdkey(uint16,uint16)") => {
57
+ // Self::get_coldkey(&method_input)
58
+ // }
59
+
60
+ // _ => Err(PrecompileFailure::Error {
61
+ // exit_status: ExitError::InvalidRange,
62
+ // }),
63
+ // }
64
+ // }
64
65
65
66
fn get_uid_count ( data : & [ u8 ] ) -> PrecompileResult {
66
67
let netuid = Self :: parse_netuid ( data) ?;
@@ -78,7 +79,7 @@ impl MetagraphPrecompile {
78
79
79
80
fn get_stake ( data : & [ u8 ] ) -> PrecompileResult {
80
81
let netuid = Self :: parse_netuid ( data) ?;
81
- let uid = Self :: parse_uid ( get_slice ( data, 32 , 64 ) ?) ?;
82
+ let uid = Self :: parse_uid ( parse_slice ( data, 32 , 64 ) ?) ?;
82
83
let hotkey = pallet_subtensor:: Pallet :: < Runtime > :: get_hotkey_for_net_and_uid ( netuid, uid)
83
84
. map_err ( |_| PrecompileFailure :: Error {
84
85
exit_status : ExitError :: InvalidRange ,
@@ -97,7 +98,7 @@ impl MetagraphPrecompile {
97
98
98
99
fn get_rank ( data : & [ u8 ] ) -> PrecompileResult {
99
100
let netuid = Self :: parse_netuid ( data) ?;
100
- let uid = Self :: parse_uid ( get_slice ( data, 32 , 64 ) ?) ?;
101
+ let uid = Self :: parse_uid ( parse_slice ( data, 32 , 64 ) ?) ?;
101
102
let rank = pallet_subtensor:: Pallet :: < Runtime > :: get_rank_for_uid ( netuid, uid) ;
102
103
103
104
let result_u256 = U256 :: from ( rank) ;
@@ -112,7 +113,7 @@ impl MetagraphPrecompile {
112
113
113
114
fn get_trust ( data : & [ u8 ] ) -> PrecompileResult {
114
115
let netuid = Self :: parse_netuid ( data) ?;
115
- let uid = Self :: parse_uid ( get_slice ( data, 32 , 64 ) ?) ?;
116
+ let uid = Self :: parse_uid ( parse_slice ( data, 32 , 64 ) ?) ?;
116
117
117
118
let trust = pallet_subtensor:: Pallet :: < Runtime > :: get_trust_for_uid ( netuid, uid) ;
118
119
@@ -128,7 +129,7 @@ impl MetagraphPrecompile {
128
129
129
130
fn get_consensus ( data : & [ u8 ] ) -> PrecompileResult {
130
131
let netuid = Self :: parse_netuid ( data) ?;
131
- let uid = Self :: parse_uid ( get_slice ( data, 32 , 64 ) ?) ?;
132
+ let uid = Self :: parse_uid ( parse_slice ( data, 32 , 64 ) ?) ?;
132
133
133
134
let consensus = pallet_subtensor:: Pallet :: < Runtime > :: get_consensus_for_uid ( netuid, uid) ;
134
135
@@ -144,7 +145,7 @@ impl MetagraphPrecompile {
144
145
145
146
fn get_incentive ( data : & [ u8 ] ) -> PrecompileResult {
146
147
let netuid = Self :: parse_netuid ( data) ?;
147
- let uid = Self :: parse_uid ( get_slice ( data, 32 , 64 ) ?) ?;
148
+ let uid = Self :: parse_uid ( parse_slice ( data, 32 , 64 ) ?) ?;
148
149
149
150
let incentive = pallet_subtensor:: Pallet :: < Runtime > :: get_incentive_for_uid ( netuid, uid) ;
150
151
@@ -160,7 +161,7 @@ impl MetagraphPrecompile {
160
161
161
162
fn get_dividends ( data : & [ u8 ] ) -> PrecompileResult {
162
163
let netuid = Self :: parse_netuid ( data) ?;
163
- let uid = Self :: parse_uid ( get_slice ( data, 32 , 64 ) ?) ?;
164
+ let uid = Self :: parse_uid ( parse_slice ( data, 32 , 64 ) ?) ?;
164
165
165
166
let dividends = pallet_subtensor:: Pallet :: < Runtime > :: get_dividends_for_uid ( netuid, uid) ;
166
167
@@ -176,7 +177,7 @@ impl MetagraphPrecompile {
176
177
177
178
fn get_emission ( data : & [ u8 ] ) -> PrecompileResult {
178
179
let netuid = Self :: parse_netuid ( data) ?;
179
- let uid = Self :: parse_uid ( get_slice ( data, 32 , 64 ) ?) ?;
180
+ let uid = Self :: parse_uid ( parse_slice ( data, 32 , 64 ) ?) ?;
180
181
181
182
let emission = pallet_subtensor:: Pallet :: < Runtime > :: get_emission_for_uid ( netuid, uid) ;
182
183
@@ -192,7 +193,7 @@ impl MetagraphPrecompile {
192
193
193
194
fn get_vtrust ( data : & [ u8 ] ) -> PrecompileResult {
194
195
let netuid = Self :: parse_netuid ( data) ?;
195
- let uid = Self :: parse_uid ( get_slice ( data, 32 , 64 ) ?) ?;
196
+ let uid = Self :: parse_uid ( parse_slice ( data, 32 , 64 ) ?) ?;
196
197
197
198
let vtrust = pallet_subtensor:: Pallet :: < Runtime > :: get_validator_trust_for_uid ( netuid, uid) ;
198
199
@@ -208,7 +209,7 @@ impl MetagraphPrecompile {
208
209
209
210
fn get_validator_status ( data : & [ u8 ] ) -> PrecompileResult {
210
211
let netuid = Self :: parse_netuid ( data) ?;
211
- let uid = Self :: parse_uid ( get_slice ( data, 32 , 64 ) ?) ?;
212
+ let uid = Self :: parse_uid ( parse_slice ( data, 32 , 64 ) ?) ?;
212
213
213
214
let validator_permit =
214
215
pallet_subtensor:: Pallet :: < Runtime > :: get_validator_permit_for_uid ( netuid, uid) ;
@@ -229,7 +230,7 @@ impl MetagraphPrecompile {
229
230
230
231
fn get_last_update ( data : & [ u8 ] ) -> PrecompileResult {
231
232
let netuid = Self :: parse_netuid ( data) ?;
232
- let uid = Self :: parse_uid ( get_slice ( data, 32 , 64 ) ?) ?;
233
+ let uid = Self :: parse_uid ( parse_slice ( data, 32 , 64 ) ?) ?;
233
234
234
235
let last_update = pallet_subtensor:: Pallet :: < Runtime > :: get_last_update_for_uid ( netuid, uid) ;
235
236
@@ -245,7 +246,7 @@ impl MetagraphPrecompile {
245
246
246
247
fn get_is_active ( data : & [ u8 ] ) -> PrecompileResult {
247
248
let netuid = Self :: parse_netuid ( data) ?;
248
- let uid = Self :: parse_uid ( get_slice ( data, 32 , 64 ) ?) ?;
249
+ let uid = Self :: parse_uid ( parse_slice ( data, 32 , 64 ) ?) ?;
249
250
250
251
let active = pallet_subtensor:: Pallet :: < Runtime > :: get_active_for_uid ( netuid, uid) ;
251
252
@@ -261,7 +262,7 @@ impl MetagraphPrecompile {
261
262
262
263
fn get_axon ( data : & [ u8 ] ) -> PrecompileResult {
263
264
let netuid = Self :: parse_netuid ( data) ?;
264
- let uid = Self :: parse_uid ( get_slice ( data, 32 , 64 ) ?) ?;
265
+ let uid = Self :: parse_uid ( parse_slice ( data, 32 , 64 ) ?) ?;
265
266
266
267
let hotkey = pallet_subtensor:: Pallet :: < Runtime > :: get_hotkey_for_net_and_uid ( netuid, uid)
267
268
. map_err ( |_| PrecompileFailure :: Error {
@@ -304,7 +305,7 @@ impl MetagraphPrecompile {
304
305
305
306
fn get_hotkey ( data : & [ u8 ] ) -> PrecompileResult {
306
307
let netuid = Self :: parse_netuid ( data) ?;
307
- let uid = Self :: parse_uid ( get_slice ( data, 32 , 64 ) ?) ?;
308
+ let uid = Self :: parse_uid ( parse_slice ( data, 32 , 64 ) ?) ?;
308
309
309
310
let hotkey = pallet_subtensor:: Pallet :: < Runtime > :: get_hotkey_for_net_and_uid ( netuid, uid)
310
311
. map_err ( |_| PrecompileFailure :: Error {
@@ -319,7 +320,7 @@ impl MetagraphPrecompile {
319
320
320
321
fn get_coldkey ( data : & [ u8 ] ) -> PrecompileResult {
321
322
let netuid = Self :: parse_netuid ( data) ?;
322
- let uid = Self :: parse_uid ( get_slice ( data, 32 , 64 ) ?) ?;
323
+ let uid = Self :: parse_uid ( parse_slice ( data, 32 , 64 ) ?) ?;
323
324
324
325
let hotkey = pallet_subtensor:: Pallet :: < Runtime > :: get_hotkey_for_net_and_uid ( netuid, uid)
325
326
. map_err ( |_| PrecompileFailure :: Error {
@@ -341,7 +342,7 @@ impl MetagraphPrecompile {
341
342
} ) ;
342
343
}
343
344
let mut netuid = [ 0u8 ; 2 ] ;
344
- netuid. copy_from_slice ( get_slice ( data, 30 , 32 ) ?) ;
345
+ netuid. copy_from_slice ( parse_slice ( data, 30 , 32 ) ?) ;
345
346
let result = u16:: from_be_bytes ( netuid) ;
346
347
Ok ( result)
347
348
}
@@ -353,8 +354,13 @@ impl MetagraphPrecompile {
353
354
} ) ;
354
355
}
355
356
let mut uid = [ 0u8 ; 2 ] ;
356
- uid. copy_from_slice ( get_slice ( data, 30 , 32 ) ?) ;
357
+ uid. copy_from_slice ( parse_slice ( data, 30 , 32 ) ?) ;
357
358
let result = u16:: from_be_bytes ( uid) ;
358
359
Ok ( result)
359
360
}
360
361
}
362
+
363
+ impl PrecompileExt for MetagraphPrecompile {
364
+ const INDEX : u64 = 2050 ;
365
+ const ADDRESS_SS58 : [ u8 ; 32 ] = [ 0 ; 32 ] ;
366
+ }
0 commit comments