Skip to content

Commit 49e5336

Browse files
committed
Refactor neuron precompil
- introduce PrecompileHandle and Precompile trait extensions
1 parent fc3beb2 commit 49e5336

File tree

7 files changed

+317
-613
lines changed

7 files changed

+317
-613
lines changed

runtime/src/precompiles/balance_transfer.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,39 @@ use sp_runtime::AccountId32;
1111
use sp_std::vec::Vec;
1212

1313
use crate::precompiles::{
14-
contract_to_origin, get_method_id, get_pubkey, get_slice, try_dispatch_runtime_call,
14+
contract_to_origin, get_method_id, parse_slice, parse_pubkey, PrecompileExt, PrecompileHandleExt,
1515
};
1616
use crate::Runtime;
1717

18-
pub const BALANCE_TRANSFER_INDEX: u64 = 2048;
19-
// ss58 public key i.e., the contract sends funds it received to the destination address from the
20-
// method parameter.
21-
const CONTRACT_ADDRESS_SS58: [u8; 32] = [
22-
0x07, 0xec, 0x71, 0x2a, 0x5d, 0x38, 0x43, 0x4d, 0xdd, 0x03, 0x3f, 0x8f, 0x02, 0x4e, 0xcd, 0xfc,
23-
0x4b, 0xb5, 0x95, 0x1c, 0x13, 0xc3, 0x08, 0x5c, 0x39, 0x9c, 0x8a, 0x5f, 0x62, 0x93, 0x70, 0x5d,
24-
];
25-
2618
pub struct BalanceTransferPrecompile;
2719

2820
#[precompile_utils::precompile]
2921
impl BalanceTransferPrecompile {
3022
#[precompile::public("transfer(bytes32)")]
3123
#[precompile::payable]
3224
fn transfer(handle: &mut impl PrecompileHandle, address: H256) -> EvmResult<()> {
33-
let amount = handle.context().apparent_value;
34-
35-
// Use BalanceConverter to convert EVM amount to Substrate balance
36-
let amount_sub =
37-
<Runtime as pallet_evm::Config>::BalanceConverter::into_substrate_balance(amount)
38-
.ok_or(ExitError::OutOfFund)?;
25+
let amount_sub = handle.try_convert_apparent_value()?;
3926

4027
if amount_sub.is_zero() {
4128
return Ok(());
4229
}
4330

44-
let dest = get_pubkey(address.as_bytes())?.0.into();
31+
let dest = parse_pubkey(address.as_bytes())?.0.into();
4532

4633
let call = pallet_balances::Call::<Runtime>::transfer_allow_death {
4734
dest,
4835
value: amount_sub.unique_saturated_into(),
4936
};
5037

51-
try_dispatch_runtime_call(handle, call, contract_to_origin(&CONTRACT_ADDRESS_SS58)?)
38+
handle.try_dispatch_runtime_call(call, contract_to_origin(&Self::ADDRESS_SS58)?)
5239
}
5340
}
41+
42+
impl PrecompileExt for BalanceTransferPrecompile {
43+
const INDEX: u64 = 2048;
44+
const ADDRESS_SS58: [u8; 32] = [
45+
0x07, 0xec, 0x71, 0x2a, 0x5d, 0x38, 0x43, 0x4d, 0xdd, 0x03, 0x3f, 0x8f, 0x02, 0x4e, 0xcd,
46+
0xfc, 0x4b, 0xb5, 0x95, 0x1c, 0x13, 0xc3, 0x08, 0x5c, 0x39, 0x9c, 0x8a, 0x5f, 0x62, 0x93,
47+
0x70, 0x5d,
48+
];
49+
}

runtime/src/precompiles/ed25519.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ extern crate alloc;
22

33
use alloc::vec::Vec;
44

5-
use crate::precompiles::get_slice;
65
use ed25519_dalek::{Signature, Verifier, VerifyingKey};
76
use fp_evm::{ExitError, ExitSucceed, LinearCostPrecompile, PrecompileFailure};
87

9-
pub const EDVERIFY_PRECOMPILE_INDEX: u64 = 1026;
8+
use crate::precompiles::{parse_slice, PrecompileExt};
109

1110
pub struct Ed25519Verify;
1211

@@ -23,13 +22,13 @@ impl LinearCostPrecompile for Ed25519Verify {
2322

2423
let mut buf = [0u8; 32];
2524

26-
let msg = get_slice(input, 4, 36)?;
27-
let pk = VerifyingKey::try_from(get_slice(input, 36, 68)?).map_err(|_| {
25+
let msg = parse_slice(input, 4, 36)?;
26+
let pk = VerifyingKey::try_from(parse_slice(input, 36, 68)?).map_err(|_| {
2827
PrecompileFailure::Error {
2928
exit_status: ExitError::Other("Public key recover failed".into()),
3029
}
3130
})?;
32-
let sig = Signature::try_from(get_slice(input, 68, 132)?).map_err(|_| {
31+
let sig = Signature::try_from(parse_slice(input, 68, 132)?).map_err(|_| {
3332
PrecompileFailure::Error {
3433
exit_status: ExitError::Other("Signature recover failed".into()),
3534
}
@@ -42,3 +41,8 @@ impl LinearCostPrecompile for Ed25519Verify {
4241
Ok((ExitSucceed::Returned, buf.to_vec()))
4342
}
4443
}
44+
45+
impl PrecompileExt for Ed25519Verify {
46+
const INDEX: u64 = 1026;
47+
const ADDRESS_SS58: [u8; 32] = [0; 32];
48+
}

runtime/src/precompiles/metagraph.rs

Lines changed: 75 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,67 @@
1-
extern crate alloc;
2-
use crate::precompiles::{get_method_id, get_slice};
3-
use crate::Runtime;
41
use fp_evm::{
52
ExitError, ExitSucceed, PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileResult,
63
};
74
use sp_core::{ByteArray, U256};
85
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+
1010
pub struct MetagraphPrecompile;
1111

1212
const NO_HOTKEY: &str = "no hotkey";
1313

14+
#[precompile_utils::precompile]
1415
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+
// }
6465

6566
fn get_uid_count(data: &[u8]) -> PrecompileResult {
6667
let netuid = Self::parse_netuid(data)?;
@@ -78,7 +79,7 @@ impl MetagraphPrecompile {
7879

7980
fn get_stake(data: &[u8]) -> PrecompileResult {
8081
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)?)?;
8283
let hotkey = pallet_subtensor::Pallet::<Runtime>::get_hotkey_for_net_and_uid(netuid, uid)
8384
.map_err(|_| PrecompileFailure::Error {
8485
exit_status: ExitError::InvalidRange,
@@ -97,7 +98,7 @@ impl MetagraphPrecompile {
9798

9899
fn get_rank(data: &[u8]) -> PrecompileResult {
99100
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)?)?;
101102
let rank = pallet_subtensor::Pallet::<Runtime>::get_rank_for_uid(netuid, uid);
102103

103104
let result_u256 = U256::from(rank);
@@ -112,7 +113,7 @@ impl MetagraphPrecompile {
112113

113114
fn get_trust(data: &[u8]) -> PrecompileResult {
114115
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)?)?;
116117

117118
let trust = pallet_subtensor::Pallet::<Runtime>::get_trust_for_uid(netuid, uid);
118119

@@ -128,7 +129,7 @@ impl MetagraphPrecompile {
128129

129130
fn get_consensus(data: &[u8]) -> PrecompileResult {
130131
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)?)?;
132133

133134
let consensus = pallet_subtensor::Pallet::<Runtime>::get_consensus_for_uid(netuid, uid);
134135

@@ -144,7 +145,7 @@ impl MetagraphPrecompile {
144145

145146
fn get_incentive(data: &[u8]) -> PrecompileResult {
146147
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)?)?;
148149

149150
let incentive = pallet_subtensor::Pallet::<Runtime>::get_incentive_for_uid(netuid, uid);
150151

@@ -160,7 +161,7 @@ impl MetagraphPrecompile {
160161

161162
fn get_dividends(data: &[u8]) -> PrecompileResult {
162163
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)?)?;
164165

165166
let dividends = pallet_subtensor::Pallet::<Runtime>::get_dividends_for_uid(netuid, uid);
166167

@@ -176,7 +177,7 @@ impl MetagraphPrecompile {
176177

177178
fn get_emission(data: &[u8]) -> PrecompileResult {
178179
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)?)?;
180181

181182
let emission = pallet_subtensor::Pallet::<Runtime>::get_emission_for_uid(netuid, uid);
182183

@@ -192,7 +193,7 @@ impl MetagraphPrecompile {
192193

193194
fn get_vtrust(data: &[u8]) -> PrecompileResult {
194195
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)?)?;
196197

197198
let vtrust = pallet_subtensor::Pallet::<Runtime>::get_validator_trust_for_uid(netuid, uid);
198199

@@ -208,7 +209,7 @@ impl MetagraphPrecompile {
208209

209210
fn get_validator_status(data: &[u8]) -> PrecompileResult {
210211
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)?)?;
212213

213214
let validator_permit =
214215
pallet_subtensor::Pallet::<Runtime>::get_validator_permit_for_uid(netuid, uid);
@@ -229,7 +230,7 @@ impl MetagraphPrecompile {
229230

230231
fn get_last_update(data: &[u8]) -> PrecompileResult {
231232
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)?)?;
233234

234235
let last_update = pallet_subtensor::Pallet::<Runtime>::get_last_update_for_uid(netuid, uid);
235236

@@ -245,7 +246,7 @@ impl MetagraphPrecompile {
245246

246247
fn get_is_active(data: &[u8]) -> PrecompileResult {
247248
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)?)?;
249250

250251
let active = pallet_subtensor::Pallet::<Runtime>::get_active_for_uid(netuid, uid);
251252

@@ -261,7 +262,7 @@ impl MetagraphPrecompile {
261262

262263
fn get_axon(data: &[u8]) -> PrecompileResult {
263264
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)?)?;
265266

266267
let hotkey = pallet_subtensor::Pallet::<Runtime>::get_hotkey_for_net_and_uid(netuid, uid)
267268
.map_err(|_| PrecompileFailure::Error {
@@ -304,7 +305,7 @@ impl MetagraphPrecompile {
304305

305306
fn get_hotkey(data: &[u8]) -> PrecompileResult {
306307
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)?)?;
308309

309310
let hotkey = pallet_subtensor::Pallet::<Runtime>::get_hotkey_for_net_and_uid(netuid, uid)
310311
.map_err(|_| PrecompileFailure::Error {
@@ -319,7 +320,7 @@ impl MetagraphPrecompile {
319320

320321
fn get_coldkey(data: &[u8]) -> PrecompileResult {
321322
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)?)?;
323324

324325
let hotkey = pallet_subtensor::Pallet::<Runtime>::get_hotkey_for_net_and_uid(netuid, uid)
325326
.map_err(|_| PrecompileFailure::Error {
@@ -341,7 +342,7 @@ impl MetagraphPrecompile {
341342
});
342343
}
343344
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)?);
345346
let result = u16::from_be_bytes(netuid);
346347
Ok(result)
347348
}
@@ -353,8 +354,13 @@ impl MetagraphPrecompile {
353354
});
354355
}
355356
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)?);
357358
let result = u16::from_be_bytes(uid);
358359
Ok(result)
359360
}
360361
}
362+
363+
impl PrecompileExt for MetagraphPrecompile {
364+
const INDEX: u64 = 2050;
365+
const ADDRESS_SS58: [u8; 32] = [0; 32];
366+
}

0 commit comments

Comments
 (0)