Skip to content

Commit 8144ed0

Browse files
committed
update neuron precompile
1 parent 3d05f7b commit 8144ed0

File tree

3 files changed

+375
-0
lines changed

3 files changed

+375
-0
lines changed

runtime/src/precompiles/neuron.rs

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ impl NeuronPrecompile {
2323
id if id == get_method_id("burnedRegister(uint16,bytes32)") => {
2424
Self::burned_register(handle, &method_input)
2525
}
26+
id if id
27+
== get_method_id(
28+
"serveAxon(uint16,uint32,uint128,uint16,uint8,uint8,uint8,uint8)",
29+
) =>
30+
{
31+
Self::serve_axon(handle, &method_input)
32+
}
33+
id if id
34+
== get_method_id(
35+
"serveAxonTls(uint16,uint32,uint128,uint16,uint8,uint8,uint8,uint8,bytes)",
36+
) =>
37+
{
38+
Self::serve_axon_tls(handle, &method_input)
39+
}
40+
id if id == get_method_id("servePrometheus(uint16,uint32,uint128,uint16,uint8)") => {
41+
Self::serve_prometheus(handle, &method_input)
42+
}
2643

2744
_ => Err(PrecompileFailure::Error {
2845
exit_status: ExitError::InvalidRange,
@@ -40,6 +57,53 @@ impl NeuronPrecompile {
4057
dispatch(handle, call, NEURON_CONTRACT_ADDRESS)
4158
}
4259

60+
pub fn serve_axon(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult {
61+
let (netuid, version, ip, port, ip_type, protocol, placeholder1, placeholder2) =
62+
Self::parse_serve_axon_parameters(data)?;
63+
let call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::<Runtime>::serve_axon {
64+
netuid,
65+
version,
66+
ip,
67+
port,
68+
ip_type,
69+
protocol,
70+
placeholder1,
71+
placeholder2,
72+
});
73+
dispatch(handle, call, NEURON_CONTRACT_ADDRESS)
74+
}
75+
76+
pub fn serve_axon_tls(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult {
77+
let (netuid, version, ip, port, ip_type, protocol, placeholder1, placeholder2, certificate) =
78+
Self::parse_serve_axon_tls_parameters(data)?;
79+
let call =
80+
RuntimeCall::SubtensorModule(pallet_subtensor::Call::<Runtime>::serve_axon_tls {
81+
netuid,
82+
version,
83+
ip,
84+
port,
85+
ip_type,
86+
protocol,
87+
placeholder1,
88+
placeholder2,
89+
certificate,
90+
});
91+
dispatch(handle, call, NEURON_CONTRACT_ADDRESS)
92+
}
93+
94+
pub fn serve_prometheus(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult {
95+
let (netuid, version, ip, port, ip_type) = Self::parse_serve_prometheus_parameters(data)?;
96+
let call =
97+
RuntimeCall::SubtensorModule(pallet_subtensor::Call::<Runtime>::serve_prometheus {
98+
netuid,
99+
version,
100+
ip,
101+
port,
102+
ip_type,
103+
});
104+
dispatch(handle, call, NEURON_CONTRACT_ADDRESS)
105+
}
106+
43107
fn parse_netuid_hotkey_parameter(data: &[u8]) -> Result<(u16, [u8; 32]), PrecompileFailure> {
44108
if data.len() < 64 {
45109
return Err(PrecompileFailure::Error {
@@ -55,4 +119,113 @@ impl NeuronPrecompile {
55119

56120
Ok((netuid, parameter))
57121
}
122+
123+
fn parse_serve_axon_parameters(
124+
data: &[u8],
125+
) -> Result<(u16, u32, u128, u16, u8, u8, u8, u8), PrecompileFailure> {
126+
if data.len() < 256 {
127+
return Err(PrecompileFailure::Error {
128+
exit_status: ExitError::InvalidRange,
129+
});
130+
}
131+
let mut netuid_vec = [0u8; 2];
132+
netuid_vec.copy_from_slice(get_slice(data, 30, 32)?);
133+
let netuid = u16::from_be_bytes(netuid_vec);
134+
135+
let mut version_vec = [0u8; 4];
136+
version_vec.copy_from_slice(get_slice(data, 60, 64)?);
137+
let version = u32::from_be_bytes(version_vec);
138+
139+
let mut ip_vec = [0u8; 16];
140+
ip_vec.copy_from_slice(get_slice(data, 80, 96)?);
141+
let ip = u128::from_be_bytes(ip_vec);
142+
143+
let mut port_vec = [0u8; 2];
144+
port_vec.copy_from_slice(get_slice(data, 126, 128)?);
145+
let port = u16::from_be_bytes(port_vec);
146+
147+
let ip_type = data[160];
148+
let protocol = data[192];
149+
let placeholder1 = data[224];
150+
let placeholder2 = data[256];
151+
Ok((
152+
netuid,
153+
version,
154+
ip,
155+
port,
156+
ip_type,
157+
protocol,
158+
placeholder1,
159+
placeholder2,
160+
))
161+
}
162+
163+
fn parse_serve_axon_tls_parameters(
164+
data: &[u8],
165+
) -> Result<(u16, u32, u128, u16, u8, u8, u8, u8, vec::Vec<u8>), PrecompileFailure> {
166+
if data.len() < 256 {
167+
return Err(PrecompileFailure::Error {
168+
exit_status: ExitError::InvalidRange,
169+
});
170+
}
171+
let mut netuid_vec = [0u8; 2];
172+
netuid_vec.copy_from_slice(get_slice(data, 30, 32)?);
173+
let netuid = u16::from_be_bytes(netuid_vec);
174+
175+
let mut version_vec = [0u8; 4];
176+
version_vec.copy_from_slice(get_slice(data, 60, 64)?);
177+
let version = u32::from_be_bytes(version_vec);
178+
179+
let mut ip_vec = [0u8; 16];
180+
ip_vec.copy_from_slice(get_slice(data, 80, 96)?);
181+
let ip = u128::from_be_bytes(ip_vec);
182+
183+
let mut port_vec = [0u8; 2];
184+
port_vec.copy_from_slice(get_slice(data, 126, 128)?);
185+
let port = u16::from_be_bytes(port_vec);
186+
187+
let ip_type = data[160];
188+
let protocol = data[192];
189+
let placeholder1 = data[224];
190+
let placeholder2 = data[256];
191+
Ok((
192+
netuid,
193+
version,
194+
ip,
195+
port,
196+
ip_type,
197+
protocol,
198+
placeholder1,
199+
placeholder2,
200+
vec![],
201+
))
202+
}
203+
204+
fn parse_serve_prometheus_parameters(
205+
data: &[u8],
206+
) -> Result<(u16, u32, u128, u16, u8), PrecompileFailure> {
207+
if data.len() < 160 {
208+
return Err(PrecompileFailure::Error {
209+
exit_status: ExitError::InvalidRange,
210+
});
211+
}
212+
let mut netuid_vec = [0u8; 2];
213+
netuid_vec.copy_from_slice(get_slice(data, 30, 32)?);
214+
let netuid = u16::from_be_bytes(netuid_vec);
215+
216+
let mut version_vec = [0u8; 4];
217+
version_vec.copy_from_slice(get_slice(data, 60, 64)?);
218+
let version = u32::from_be_bytes(version_vec);
219+
220+
let mut ip_vec = [0u8; 16];
221+
ip_vec.copy_from_slice(get_slice(data, 80, 96)?);
222+
let ip = u128::from_be_bytes(ip_vec);
223+
224+
let mut port_vec = [0u8; 2];
225+
port_vec.copy_from_slice(get_slice(data, 126, 128)?);
226+
let port = u16::from_be_bytes(port_vec);
227+
228+
let ip_type = data[160];
229+
Ok((netuid, version, ip, port, ip_type))
230+
}
58231
}

runtime/src/precompiles/solidity/neuron.abi

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,139 @@
1616
"outputs": [],
1717
"stateMutability": "payable",
1818
"type": "function"
19+
},
20+
{
21+
"inputs": [
22+
{
23+
"internalType": "uint16",
24+
"name": "netuid",
25+
"type": "uint16"
26+
},
27+
{
28+
"internalType": "uint32",
29+
"name": "version",
30+
"type": "uint32"
31+
},
32+
{
33+
"internalType": "uint128",
34+
"name": "ip",
35+
"type": "uint128"
36+
},
37+
{
38+
"internalType": "uint16",
39+
"name": "port",
40+
"type": "uint16"
41+
},
42+
{
43+
"internalType": "uint8",
44+
"name": "ipType",
45+
"type": "uint8"
46+
},
47+
{
48+
"internalType": "uint8",
49+
"name": "protocol",
50+
"type": "uint8"
51+
},
52+
{
53+
"internalType": "uint8",
54+
"name": "placeholder1",
55+
"type": "uint8"
56+
},
57+
{
58+
"internalType": "uint8",
59+
"name": "placeholder2",
60+
"type": "uint8"
61+
}
62+
],
63+
"name": "serveAxon",
64+
"outputs": [],
65+
"stateMutability": "payable",
66+
"type": "function"
67+
},
68+
{
69+
"inputs": [
70+
{
71+
"internalType": "uint16",
72+
"name": "netuid",
73+
"type": "uint16"
74+
},
75+
{
76+
"internalType": "uint32",
77+
"name": "version",
78+
"type": "uint32"
79+
},
80+
{
81+
"internalType": "uint128",
82+
"name": "ip",
83+
"type": "uint128"
84+
},
85+
{
86+
"internalType": "uint16",
87+
"name": "port",
88+
"type": "uint16"
89+
},
90+
{
91+
"internalType": "uint8",
92+
"name": "ipType",
93+
"type": "uint8"
94+
},
95+
{
96+
"internalType": "uint8",
97+
"name": "protocol",
98+
"type": "uint8"
99+
},
100+
{
101+
"internalType": "uint8",
102+
"name": "placeholder1",
103+
"type": "uint8"
104+
},
105+
{
106+
"internalType": "uint8",
107+
"name": "placeholder2",
108+
"type": "uint8"
109+
},
110+
{
111+
"internalType": "bytes",
112+
"name": "certificate",
113+
"type": "bytes"
114+
}
115+
],
116+
"name": "serveAxonTls",
117+
"outputs": [],
118+
"stateMutability": "payable",
119+
"type": "function"
120+
},
121+
{
122+
"inputs": [
123+
{
124+
"internalType": "uint16",
125+
"name": "netuid",
126+
"type": "uint16"
127+
},
128+
{
129+
"internalType": "uint32",
130+
"name": "version",
131+
"type": "uint32"
132+
},
133+
{
134+
"internalType": "uint128",
135+
"name": "ip",
136+
"type": "uint128"
137+
},
138+
{
139+
"internalType": "uint16",
140+
"name": "port",
141+
"type": "uint16"
142+
},
143+
{
144+
"internalType": "uint8",
145+
"name": "ipType",
146+
"type": "uint8"
147+
}
148+
],
149+
"name": "servePrometheus",
150+
"outputs": [],
151+
"stateMutability": "payable",
152+
"type": "function"
19153
}
20154
]

0 commit comments

Comments
 (0)