Skip to content

Commit 2d32d96

Browse files
committed
update register_network with identity precompile
1 parent ea136bb commit 2d32d96

File tree

1 file changed

+172
-9
lines changed

1 file changed

+172
-9
lines changed

runtime/src/precompiles/subnet.rs

Lines changed: 172 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use pallet_evm::{
55
AddressMapping, ExitError, HashedAddressMapping, PrecompileFailure, PrecompileHandle,
66
PrecompileResult,
77
};
8-
use sp_runtime::traits::BlakeTwo256;
98
use sp_runtime::AccountId32;
9+
use sp_runtime::{traits::BlakeTwo256, Vec};
1010
use sp_std::vec;
1111
pub const SUBNET_PRECOMPILE_INDEX: u64 = 2051;
1212
// bytes with max lenght 1K
@@ -62,14 +62,26 @@ impl SubnetPrecompile {
6262
)
6363
}
6464
33.. => {
65-
let (hotkey, subnet_name, github_repo, subnet_contact) =
66-
Self::parse_register_network_parameters(data)?;
65+
let (
66+
hotkey,
67+
subnet_name,
68+
github_repo,
69+
subnet_contact,
70+
subnet_url,
71+
discord,
72+
description,
73+
additional,
74+
) = Self::parse_register_network_parameters(data)?;
6775

68-
let identity: pallet_subtensor::SubnetIdentityOf =
69-
pallet_subtensor::SubnetIdentityOf {
76+
let identity: pallet_subtensor::SubnetIdentityOfV2 =
77+
pallet_subtensor::SubnetIdentityOfV2 {
7078
subnet_name,
7179
github_repo,
7280
subnet_contact,
81+
subnet_url,
82+
discord,
83+
description,
84+
additional,
7385
};
7486

7587
// Create the register_network callcle
@@ -98,12 +110,24 @@ impl SubnetPrecompile {
98110

99111
fn parse_register_network_parameters(
100112
data: &[u8],
101-
) -> Result<(AccountId32, vec::Vec<u8>, vec::Vec<u8>, vec::Vec<u8>), PrecompileFailure> {
113+
) -> Result<
114+
(
115+
AccountId32,
116+
Vec<u8>,
117+
Vec<u8>,
118+
Vec<u8>,
119+
Vec<u8>,
120+
Vec<u8>,
121+
Vec<u8>,
122+
Vec<u8>,
123+
),
124+
PrecompileFailure,
125+
> {
102126
let (pubkey, dynamic_params) = get_pubkey(data)?;
103127
let dynamic_data_len = dynamic_params.len();
104128

105129
let mut buf = [0_u8; 4];
106-
// get all start point for three data items: name, repo and contact
130+
// get all start points for the data items: name, repo, contact, url, discord, description, additional
107131
buf.copy_from_slice(get_slice(data, 60, 64)?);
108132
let subnet_name_start: usize = u32::from_be_bytes(buf) as usize;
109133
if subnet_name_start > dynamic_data_len {
@@ -140,6 +164,54 @@ impl SubnetPrecompile {
140164
});
141165
}
142166

167+
buf.copy_from_slice(get_slice(data, 156, 160)?);
168+
let subnet_url_start: usize = u32::from_be_bytes(buf) as usize;
169+
if subnet_url_start > dynamic_data_len {
170+
log::error!(
171+
"the start position of subnet_url as {} is too big ",
172+
subnet_url_start
173+
);
174+
return Err(PrecompileFailure::Error {
175+
exit_status: ExitError::InvalidRange,
176+
});
177+
}
178+
179+
buf.copy_from_slice(get_slice(data, 188, 192)?);
180+
let discord_start: usize = u32::from_be_bytes(buf) as usize;
181+
if discord_start > dynamic_data_len {
182+
log::error!(
183+
"the start position of discord as {} is too big ",
184+
discord_start
185+
);
186+
return Err(PrecompileFailure::Error {
187+
exit_status: ExitError::InvalidRange,
188+
});
189+
}
190+
191+
buf.copy_from_slice(get_slice(data, 220, 224)?);
192+
let description_start: usize = u32::from_be_bytes(buf) as usize;
193+
if description_start > dynamic_data_len {
194+
log::error!(
195+
"the start position of description as {} is too big ",
196+
description_start
197+
);
198+
return Err(PrecompileFailure::Error {
199+
exit_status: ExitError::InvalidRange,
200+
});
201+
}
202+
203+
buf.copy_from_slice(get_slice(data, 252, 256)?);
204+
let additional_start: usize = u32::from_be_bytes(buf) as usize;
205+
if additional_start > dynamic_data_len {
206+
log::error!(
207+
"the start position of additional as {} is too big ",
208+
additional_start
209+
);
210+
return Err(PrecompileFailure::Error {
211+
exit_status: ExitError::InvalidRange,
212+
});
213+
}
214+
143215
// get name
144216
buf.copy_from_slice(get_slice(
145217
data,
@@ -149,7 +221,10 @@ impl SubnetPrecompile {
149221
let subnet_name_len: usize = u32::from_be_bytes(buf) as usize;
150222

151223
if subnet_name_len > MAX_SINGLE_PARAMETER_SIZE {
152-
log::error!("the length of subnet nae as {} is too big", subnet_name_len);
224+
log::error!(
225+
"the length of subnet name as {} is too big",
226+
subnet_name_len
227+
);
153228
return Err(PrecompileFailure::Error {
154229
exit_status: ExitError::InvalidRange,
155230
});
@@ -210,6 +285,94 @@ impl SubnetPrecompile {
210285
subnet_contact_start + subnet_contact_len + 32,
211286
)?);
212287

213-
Ok((pubkey, name_vec, repo_vec, contact_vec))
288+
// get subnet_url
289+
buf.copy_from_slice(get_slice(
290+
data,
291+
subnet_url_start + 28,
292+
subnet_url_start + 32,
293+
)?);
294+
let subnet_url_len: usize = u32::from_be_bytes(buf) as usize;
295+
if subnet_url_len > MAX_SINGLE_PARAMETER_SIZE {
296+
log::error!("the length of subnet_url as {} is too big", subnet_url_len);
297+
return Err(PrecompileFailure::Error {
298+
exit_status: ExitError::InvalidRange,
299+
});
300+
}
301+
let mut url_vec = vec![0; subnet_url_len];
302+
url_vec.copy_from_slice(get_slice(
303+
data,
304+
subnet_url_start + 32,
305+
subnet_url_start + subnet_url_len + 32,
306+
)?);
307+
308+
// get discord
309+
buf.copy_from_slice(get_slice(data, discord_start + 28, discord_start + 32)?);
310+
let discord_len: usize = u32::from_be_bytes(buf) as usize;
311+
if discord_len > MAX_SINGLE_PARAMETER_SIZE {
312+
log::error!("the length of discord as {} is too big", discord_len);
313+
return Err(PrecompileFailure::Error {
314+
exit_status: ExitError::InvalidRange,
315+
});
316+
}
317+
let mut discord_vec = vec![0; discord_len];
318+
discord_vec.copy_from_slice(get_slice(
319+
data,
320+
discord_start + 32,
321+
discord_start + discord_len + 32,
322+
)?);
323+
324+
// get description
325+
buf.copy_from_slice(get_slice(
326+
data,
327+
description_start + 28,
328+
description_start + 32,
329+
)?);
330+
let description_len: usize = u32::from_be_bytes(buf) as usize;
331+
if description_len > MAX_SINGLE_PARAMETER_SIZE {
332+
log::error!(
333+
"the length of description as {} is too big",
334+
description_len
335+
);
336+
return Err(PrecompileFailure::Error {
337+
exit_status: ExitError::InvalidRange,
338+
});
339+
}
340+
let mut description_vec = vec![0; description_len];
341+
description_vec.copy_from_slice(get_slice(
342+
data,
343+
description_start + 32,
344+
description_start + description_len + 32,
345+
)?);
346+
347+
// get additional
348+
buf.copy_from_slice(get_slice(
349+
data,
350+
additional_start + 28,
351+
additional_start + 32,
352+
)?);
353+
let additional_len: usize = u32::from_be_bytes(buf) as usize;
354+
if additional_len > MAX_SINGLE_PARAMETER_SIZE {
355+
log::error!("the length of additional as {} is too big", additional_len);
356+
return Err(PrecompileFailure::Error {
357+
exit_status: ExitError::InvalidRange,
358+
});
359+
}
360+
let mut additional_vec = vec![0; additional_len];
361+
additional_vec.copy_from_slice(get_slice(
362+
data,
363+
additional_start + 32,
364+
additional_start + additional_len + 32,
365+
)?);
366+
367+
Ok((
368+
pubkey,
369+
name_vec,
370+
repo_vec,
371+
contact_vec,
372+
url_vec,
373+
discord_vec,
374+
description_vec,
375+
additional_vec,
376+
))
214377
}
215378
}

0 commit comments

Comments
 (0)