Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix binary sv2 proc macro #1502

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: Reserve internal field names & enforce checks
- Introduced a reserved set of field names (`__decodable_internal_data`, `__decodable_internal_offset`)
- Added a compile-time error if a struct contains a reserved field name
- Replaced all occurrences of `data` and `offset` with the reserved identifiers
Shourya742 committed Feb 25, 2025

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
commit 92a71df5b2e9511aaef4c7e6dce52b9f868ecbe4
40 changes: 33 additions & 7 deletions protocols/v2/binary-sv2/derive_codec/src/lib.rs
Original file line number Diff line number Diff line change
@@ -56,6 +56,9 @@ use alloc::{
use core::iter::FromIterator;
use proc_macro::{Group, TokenStream, TokenTree};

/// Reserved field names to avoid conflicts
const RESERVED_FIELDS: [&str; 2] = ["__decodable_internal_data", "__decodable_internal_offset"];

// Checks if a `TokenStream` contains a group with a bracket delimiter (`[]`),
// and further examines if the group has an identifier called `already_sized`.
//
@@ -557,20 +560,39 @@ fn parse_struct_fields(group: Vec<TokenTree>) -> Vec<ParsedField> {
pub fn decodable(item: TokenStream) -> TokenStream {
let parsed_struct = get_struct_properties(item);

let data_ident = RESERVED_FIELDS[0];
let offset_ident = RESERVED_FIELDS[1];

for field in &parsed_struct.fields {
if RESERVED_FIELDS.contains(&field.name.as_str()) {
return format!(
"compile_error!(\"Field name '{}' is reserved and cannot be used in struct '{}'. Rename it to avoid conflicts.\");",
field.name, parsed_struct.name
)
.parse()
.unwrap();
}
}

let mut derive_fields = String::new();

for f in parsed_struct.fields.clone() {
let field = format!(
"
let {}: Vec<FieldMarker> = {}{}::get_structure(& data[offset..])?;
offset += {}.size_hint_(&data, offset)?;
let {}: Vec<FieldMarker> = {}{}::get_structure(& {}[{}..])?;
{} += {}.size_hint_(&{}, {})?;
let {} = {}.try_into()?;
fields.push({});
",
f.name,
f.type_,
f.get_generics(),
data_ident,
offset_ident,
offset_ident,
f.name,
data_ident,
offset_ident,
f.name,
f.name,
f.name
@@ -602,11 +624,12 @@ pub fn decodable(item: TokenStream) -> TokenStream {
for f in fields.clone() {
let field = format!(
"
{}: {}{}::from_decoded_fields(data.pop().ok_or(Error::NoDecodableFieldPassed)?.into())?,
{}: {}{}::from_decoded_fields({}.pop().ok_or(Error::NoDecodableFieldPassed)?.into())?,
",
f.name,
f.type_,
f.get_generics()
f.get_generics(),
data_ident
);
derive_decoded_fields.push_str(&field)
}
@@ -624,14 +647,14 @@ pub fn decodable(item: TokenStream) -> TokenStream {
use super::*;
impl{} Decodable<'decoder> for {}{} {{
fn get_structure(data: &[u8]) -> Result<Vec<FieldMarker>, Error> {{
fn get_structure({}: &[u8]) -> Result<Vec<FieldMarker>, Error> {{
let mut fields = Vec::new();
let mut offset = 0;
let mut {} = 0;
{}
Ok(fields)
}}
fn from_decoded_fields(mut data: Vec<DecodableField<'decoder>>) -> Result<Self, Error> {{
fn from_decoded_fields(mut {}: Vec<DecodableField<'decoder>>) -> Result<Self, Error> {{
Ok(Self {{
{}
}})
@@ -659,7 +682,10 @@ pub fn decodable(item: TokenStream) -> TokenStream {
impl_generics,
parsed_struct.name,
parsed_struct.generics,
data_ident,
offset_ident,
derive_fields,
data_ident,
derive_decoded_fields,
// impl into_static
impl_generics,