-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathneuron.rs
58 lines (47 loc) · 2.05 KB
/
neuron.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use pallet_evm::{ExitError, PrecompileFailure, PrecompileHandle, PrecompileResult};
use crate::precompiles::{dispatch, get_method_id, get_pubkey, get_slice};
use sp_runtime::AccountId32;
use sp_std::vec;
use crate::{Runtime, RuntimeCall};
pub const NEURON_PRECOMPILE_INDEX: u64 = 2052;
// this is neuron smart contract's(0x0000000000000000000000000000000000000804) sr25519 address
pub const NEURON_CONTRACT_ADDRESS: &str = "5GKZiUUgTnWSz3BgiVBMehEKkLszsG4ZXnvgWpWFUFKqrqyn";
pub struct NeuronPrecompile;
impl NeuronPrecompile {
pub fn execute(handle: &mut impl PrecompileHandle) -> PrecompileResult {
let txdata = handle.input();
let method_id = get_slice(txdata, 0, 4)?;
let method_input = txdata
.get(4..)
.map_or_else(vec::Vec::new, |slice| slice.to_vec()); // Avoiding borrowing conflicts
match method_id {
id if id == get_method_id("burnedRegister(uint16,bytes32)") => {
Self::burned_register(handle, &method_input)
}
_ => Err(PrecompileFailure::Error {
exit_status: ExitError::InvalidRange,
}),
}
}
pub fn burned_register(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult {
let (netuid, hotkey) = Self::parse_netuid_hotkey_parameter(data)?;
let call =
RuntimeCall::SubtensorModule(pallet_subtensor::Call::<Runtime>::burned_register {
netuid,
hotkey,
});
dispatch(handle, call, NEURON_CONTRACT_ADDRESS)
}
fn parse_netuid_hotkey_parameter(data: &[u8]) -> Result<(u16, AccountId32), PrecompileFailure> {
if data.len() < 64 {
return Err(PrecompileFailure::Error {
exit_status: ExitError::InvalidRange,
});
}
let mut netuid_vec = [0u8; 2];
netuid_vec.copy_from_slice(get_slice(data, 30, 32)?);
let netuid = u16::from_be_bytes(netuid_vec);
let (hotkey, _) = get_pubkey(get_slice(data, 32, 64)?)?;
Ok((netuid, hotkey))
}
}