2020-03-04 00:45:01 +00:00
|
|
|
#![recursion_limit = "256"]
|
2019-07-04 03:22:33 +00:00
|
|
|
//! Provides procedural derive macros for the `Encode` and `Decode` traits of the `eth2_ssz` crate.
|
|
|
|
//!
|
|
|
|
//! Supports field attributes, see each derive macro for more information.
|
2019-05-05 02:11:25 +00:00
|
|
|
|
2019-02-19 02:53:05 +00:00
|
|
|
use proc_macro::TokenStream;
|
2019-04-17 11:21:07 +00:00
|
|
|
use quote::quote;
|
2021-07-09 06:15:32 +00:00
|
|
|
use syn::{parse_macro_input, DataEnum, DataStruct, DeriveInput};
|
2019-02-19 02:53:05 +00:00
|
|
|
|
2019-03-15 07:33:32 +00:00
|
|
|
/// Returns a Vec of `syn::Ident` for each named field in the struct, whilst filtering out fields
|
|
|
|
/// that should not be serialized.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
/// Any unnamed struct field (like in a tuple struct) will raise a panic at compile time.
|
2021-01-19 00:34:28 +00:00
|
|
|
fn get_serializable_named_field_idents(struct_data: &syn::DataStruct) -> Vec<&syn::Ident> {
|
2019-03-15 07:33:32 +00:00
|
|
|
struct_data
|
|
|
|
.fields
|
|
|
|
.iter()
|
|
|
|
.filter_map(|f| {
|
|
|
|
if should_skip_serializing(&f) {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(match &f.ident {
|
|
|
|
Some(ref ident) => ident,
|
|
|
|
_ => panic!("ssz_derive only supports named struct fields."),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2019-05-05 02:11:25 +00:00
|
|
|
/// Returns a Vec of `syn::Type` for each named field in the struct, whilst filtering out fields
|
2019-05-04 04:11:48 +00:00
|
|
|
/// that should not be serialized.
|
2021-01-19 00:34:28 +00:00
|
|
|
fn get_serializable_field_types(struct_data: &syn::DataStruct) -> Vec<&syn::Type> {
|
2019-05-04 04:11:48 +00:00
|
|
|
struct_data
|
|
|
|
.fields
|
|
|
|
.iter()
|
|
|
|
.filter_map(|f| {
|
|
|
|
if should_skip_serializing(&f) {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(&f.ty)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2019-03-15 07:33:32 +00:00
|
|
|
/// Returns true if some field has an attribute declaring it should not be serialized.
|
|
|
|
///
|
|
|
|
/// The field attribute is: `#[ssz(skip_serializing)]`
|
|
|
|
fn should_skip_serializing(field: &syn::Field) -> bool {
|
2019-09-28 04:29:14 +00:00
|
|
|
field.attrs.iter().any(|attr| {
|
2020-05-17 11:16:48 +00:00
|
|
|
attr.path.is_ident("ssz")
|
|
|
|
&& attr.tokens.to_string().replace(" ", "") == "(skip_serializing)"
|
2019-09-28 04:29:14 +00:00
|
|
|
})
|
2019-03-15 07:33:32 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 06:15:32 +00:00
|
|
|
/// Implements `ssz::Encode` for some `struct` or `enum`.
|
2019-02-19 07:43:09 +00:00
|
|
|
///
|
|
|
|
/// Fields are encoded in the order they are defined.
|
2019-07-04 03:22:33 +00:00
|
|
|
///
|
|
|
|
/// ## Field attributes
|
|
|
|
///
|
|
|
|
/// - `#[ssz(skip_serializing)]`: the field will not be serialized.
|
2019-03-15 07:33:32 +00:00
|
|
|
#[proc_macro_derive(Encode, attributes(ssz))]
|
2019-02-19 02:53:05 +00:00
|
|
|
pub fn ssz_encode_derive(input: TokenStream) -> TokenStream {
|
|
|
|
let item = parse_macro_input!(input as DeriveInput);
|
|
|
|
|
2021-07-09 06:15:32 +00:00
|
|
|
match &item.data {
|
|
|
|
syn::Data::Struct(s) => ssz_encode_derive_struct(&item, s),
|
|
|
|
syn::Data::Enum(s) => ssz_encode_derive_enum(&item, s),
|
|
|
|
_ => panic!("ssz_derive only supports structs and enums"),
|
|
|
|
}
|
|
|
|
}
|
2019-02-19 02:53:05 +00:00
|
|
|
|
2021-07-09 06:15:32 +00:00
|
|
|
fn ssz_encode_derive_struct(derive_input: &DeriveInput, struct_data: &DataStruct) -> TokenStream {
|
|
|
|
let name = &derive_input.ident;
|
|
|
|
let (impl_generics, ty_generics, where_clause) = &derive_input.generics.split_for_impl();
|
2019-02-19 02:53:05 +00:00
|
|
|
|
2021-07-09 06:15:32 +00:00
|
|
|
let field_idents = get_serializable_named_field_idents(struct_data);
|
|
|
|
let field_idents_a = get_serializable_named_field_idents(struct_data);
|
|
|
|
let field_types_a = get_serializable_field_types(struct_data);
|
2019-05-04 04:11:48 +00:00
|
|
|
let field_types_b = field_types_a.clone();
|
2019-09-08 16:23:37 +00:00
|
|
|
let field_types_d = field_types_a.clone();
|
|
|
|
let field_types_e = field_types_a.clone();
|
|
|
|
let field_types_f = field_types_a.clone();
|
2019-02-19 02:53:05 +00:00
|
|
|
|
|
|
|
let output = quote! {
|
2019-05-13 05:12:19 +00:00
|
|
|
impl #impl_generics ssz::Encode for #name #ty_generics #where_clause {
|
2019-05-04 04:11:48 +00:00
|
|
|
fn is_ssz_fixed_len() -> bool {
|
|
|
|
#(
|
2019-05-13 05:12:19 +00:00
|
|
|
<#field_types_a as ssz::Encode>::is_ssz_fixed_len() &&
|
2019-05-04 04:11:48 +00:00
|
|
|
)*
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ssz_fixed_len() -> usize {
|
2019-05-13 05:12:19 +00:00
|
|
|
if <Self as ssz::Encode>::is_ssz_fixed_len() {
|
2020-10-25 23:27:39 +00:00
|
|
|
let mut len: usize = 0;
|
2019-05-04 04:11:48 +00:00
|
|
|
#(
|
2020-10-25 23:27:39 +00:00
|
|
|
len = len
|
|
|
|
.checked_add(<#field_types_b as ssz::Encode>::ssz_fixed_len())
|
|
|
|
.expect("encode ssz_fixed_len length overflow");
|
2019-05-04 04:11:48 +00:00
|
|
|
)*
|
2020-10-25 23:27:39 +00:00
|
|
|
len
|
2019-05-04 04:11:48 +00:00
|
|
|
} else {
|
|
|
|
ssz::BYTES_PER_LENGTH_OFFSET
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 16:23:37 +00:00
|
|
|
fn ssz_bytes_len(&self) -> usize {
|
|
|
|
if <Self as ssz::Encode>::is_ssz_fixed_len() {
|
|
|
|
<Self as ssz::Encode>::ssz_fixed_len()
|
|
|
|
} else {
|
2020-10-25 23:27:39 +00:00
|
|
|
let mut len: usize = 0;
|
2019-09-08 16:23:37 +00:00
|
|
|
#(
|
|
|
|
if <#field_types_d as ssz::Encode>::is_ssz_fixed_len() {
|
2020-10-25 23:27:39 +00:00
|
|
|
len = len
|
|
|
|
.checked_add(<#field_types_e as ssz::Encode>::ssz_fixed_len())
|
|
|
|
.expect("encode ssz_bytes_len length overflow");
|
2019-09-08 16:23:37 +00:00
|
|
|
} else {
|
2020-10-25 23:27:39 +00:00
|
|
|
len = len
|
|
|
|
.checked_add(ssz::BYTES_PER_LENGTH_OFFSET)
|
|
|
|
.expect("encode ssz_bytes_len length overflow for offset");
|
|
|
|
len = len
|
|
|
|
.checked_add(self.#field_idents_a.ssz_bytes_len())
|
|
|
|
.expect("encode ssz_bytes_len length overflow for bytes");
|
2019-09-08 16:23:37 +00:00
|
|
|
}
|
|
|
|
)*
|
|
|
|
|
|
|
|
len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-05 23:26:58 +00:00
|
|
|
fn ssz_append(&self, buf: &mut Vec<u8>) {
|
2020-10-25 23:27:39 +00:00
|
|
|
let mut offset: usize = 0;
|
|
|
|
#(
|
|
|
|
offset = offset
|
|
|
|
.checked_add(<#field_types_f as ssz::Encode>::ssz_fixed_len())
|
|
|
|
.expect("encode ssz_append offset overflow");
|
|
|
|
)*
|
2019-05-05 23:26:58 +00:00
|
|
|
|
2019-05-05 23:58:31 +00:00
|
|
|
let mut encoder = ssz::SszEncoder::container(buf, offset);
|
2019-05-04 04:11:48 +00:00
|
|
|
|
2019-02-19 02:53:05 +00:00
|
|
|
#(
|
2019-05-05 23:26:58 +00:00
|
|
|
encoder.append(&self.#field_idents);
|
2019-02-19 02:53:05 +00:00
|
|
|
)*
|
2019-05-04 04:11:48 +00:00
|
|
|
|
2019-05-05 23:58:31 +00:00
|
|
|
encoder.finalize();
|
2019-02-19 02:53:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
output.into()
|
|
|
|
}
|
|
|
|
|
2021-07-09 06:15:32 +00:00
|
|
|
/// Derive `Encode` for a restricted subset of all possible enum types.
|
|
|
|
///
|
|
|
|
/// Only supports:
|
|
|
|
/// - Enums with a single field per variant, where
|
|
|
|
/// - All fields are variably sized from an SSZ-perspective (not fixed size).
|
|
|
|
///
|
|
|
|
/// Will panic at compile-time if the single field requirement isn't met, but will panic *at run
|
|
|
|
/// time* if the variable-size requirement isn't met.
|
|
|
|
fn ssz_encode_derive_enum(derive_input: &DeriveInput, enum_data: &DataEnum) -> TokenStream {
|
|
|
|
let name = &derive_input.ident;
|
|
|
|
let (impl_generics, ty_generics, where_clause) = &derive_input.generics.split_for_impl();
|
|
|
|
|
|
|
|
let (patterns, assert_exprs): (Vec<_>, Vec<_>) = enum_data
|
|
|
|
.variants
|
|
|
|
.iter()
|
|
|
|
.map(|variant| {
|
|
|
|
let variant_name = &variant.ident;
|
|
|
|
|
|
|
|
if variant.fields.len() != 1 {
|
|
|
|
panic!("ssz::Encode can only be derived for enums with 1 field per variant");
|
|
|
|
}
|
|
|
|
|
|
|
|
let pattern = quote! {
|
|
|
|
#name::#variant_name(ref inner)
|
|
|
|
};
|
|
|
|
|
|
|
|
let ty = &(&variant.fields).into_iter().next().unwrap().ty;
|
|
|
|
let type_assert = quote! {
|
|
|
|
!<#ty as ssz::Encode>::is_ssz_fixed_len()
|
|
|
|
};
|
|
|
|
(pattern, type_assert)
|
|
|
|
})
|
|
|
|
.unzip();
|
|
|
|
|
|
|
|
let output = quote! {
|
|
|
|
impl #impl_generics ssz::Encode for #name #ty_generics #where_clause {
|
|
|
|
fn is_ssz_fixed_len() -> bool {
|
|
|
|
assert!(
|
|
|
|
#(
|
|
|
|
#assert_exprs &&
|
|
|
|
)* true,
|
|
|
|
"not all enum variants are variably-sized"
|
|
|
|
);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ssz_bytes_len(&self) -> usize {
|
|
|
|
match self {
|
|
|
|
#(
|
|
|
|
#patterns => inner.ssz_bytes_len(),
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ssz_append(&self, buf: &mut Vec<u8>) {
|
|
|
|
match self {
|
|
|
|
#(
|
|
|
|
#patterns => inner.ssz_append(buf),
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
output.into()
|
|
|
|
}
|
|
|
|
|
2019-03-15 07:33:32 +00:00
|
|
|
/// Returns true if some field has an attribute declaring it should not be deserialized.
|
|
|
|
///
|
|
|
|
/// The field attribute is: `#[ssz(skip_deserializing)]`
|
|
|
|
fn should_skip_deserializing(field: &syn::Field) -> bool {
|
2019-09-28 04:29:14 +00:00
|
|
|
field.attrs.iter().any(|attr| {
|
2020-05-17 11:16:48 +00:00
|
|
|
attr.path.is_ident("ssz")
|
|
|
|
&& attr.tokens.to_string().replace(" ", "") == "(skip_deserializing)"
|
2019-09-28 04:29:14 +00:00
|
|
|
})
|
2019-03-15 07:33:32 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 05:12:19 +00:00
|
|
|
/// Implements `ssz::Decode` for some `struct`.
|
2019-02-19 07:43:09 +00:00
|
|
|
///
|
|
|
|
/// Fields are decoded in the order they are defined.
|
2019-07-04 03:22:33 +00:00
|
|
|
///
|
|
|
|
/// ## Field attributes
|
|
|
|
///
|
|
|
|
/// - `#[ssz(skip_deserializing)]`: during de-serialization the field will be instantiated from a
|
|
|
|
/// `Default` implementation. The decoder will assume that the field was not serialized at all
|
|
|
|
/// (e.g., if it has been serialized, an error will be raised instead of `Default` overriding it).
|
2019-02-19 02:53:05 +00:00
|
|
|
#[proc_macro_derive(Decode)]
|
|
|
|
pub fn ssz_decode_derive(input: TokenStream) -> TokenStream {
|
|
|
|
let item = parse_macro_input!(input as DeriveInput);
|
|
|
|
|
|
|
|
let name = &item.ident;
|
2019-05-08 03:08:37 +00:00
|
|
|
let (impl_generics, ty_generics, where_clause) = &item.generics.split_for_impl();
|
2019-02-19 02:53:05 +00:00
|
|
|
|
|
|
|
let struct_data = match &item.data {
|
|
|
|
syn::Data::Struct(s) => s,
|
2019-02-19 05:04:29 +00:00
|
|
|
_ => panic!("ssz_derive only supports structs."),
|
2019-02-19 02:53:05 +00:00
|
|
|
};
|
|
|
|
|
2019-05-05 02:11:25 +00:00
|
|
|
let mut register_types = vec![];
|
2020-05-28 01:37:40 +00:00
|
|
|
let mut field_names = vec![];
|
2020-03-04 00:45:01 +00:00
|
|
|
let mut fixed_decodes = vec![];
|
2019-05-05 02:11:25 +00:00
|
|
|
let mut decodes = vec![];
|
|
|
|
let mut is_fixed_lens = vec![];
|
|
|
|
let mut fixed_lens = vec![];
|
2019-02-19 02:53:05 +00:00
|
|
|
|
2019-03-15 07:33:32 +00:00
|
|
|
// Build quotes for fields that should be deserialized and those that should be built from
|
|
|
|
// `Default`.
|
|
|
|
for field in &struct_data.fields {
|
|
|
|
match &field.ident {
|
|
|
|
Some(ref ident) => {
|
2020-05-28 01:37:40 +00:00
|
|
|
field_names.push(quote! {
|
|
|
|
#ident
|
|
|
|
});
|
|
|
|
|
2019-03-15 07:33:32 +00:00
|
|
|
if should_skip_deserializing(field) {
|
2019-05-05 02:11:25 +00:00
|
|
|
// Field should not be deserialized; use a `Default` impl to instantiate.
|
|
|
|
decodes.push(quote! {
|
2020-05-28 01:37:40 +00:00
|
|
|
let #ident = <_>::default();
|
2019-03-15 07:33:32 +00:00
|
|
|
});
|
2020-03-04 00:45:01 +00:00
|
|
|
|
|
|
|
fixed_decodes.push(quote! {
|
2020-05-28 01:37:40 +00:00
|
|
|
let #ident = <_>::default();
|
2020-03-04 00:45:01 +00:00
|
|
|
});
|
2019-03-15 07:33:32 +00:00
|
|
|
} else {
|
2019-05-05 02:11:25 +00:00
|
|
|
let ty = &field.ty;
|
|
|
|
|
|
|
|
register_types.push(quote! {
|
|
|
|
builder.register_type::<#ty>()?;
|
|
|
|
});
|
|
|
|
|
|
|
|
decodes.push(quote! {
|
2020-05-28 01:37:40 +00:00
|
|
|
let #ident = decoder.decode_next()?;
|
2019-05-05 02:11:25 +00:00
|
|
|
});
|
|
|
|
|
2020-03-04 00:45:01 +00:00
|
|
|
fixed_decodes.push(quote! {
|
2020-05-28 01:37:40 +00:00
|
|
|
let #ident = decode_field!(#ty);
|
2020-03-04 00:45:01 +00:00
|
|
|
});
|
|
|
|
|
2019-05-05 02:11:25 +00:00
|
|
|
is_fixed_lens.push(quote! {
|
2019-05-13 05:12:19 +00:00
|
|
|
<#ty as ssz::Decode>::is_ssz_fixed_len()
|
2019-05-05 02:11:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
fixed_lens.push(quote! {
|
2019-05-13 05:12:19 +00:00
|
|
|
<#ty as ssz::Decode>::ssz_fixed_len()
|
2019-03-15 07:33:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => panic!("ssz_derive only supports named struct fields."),
|
|
|
|
};
|
|
|
|
}
|
2019-02-19 02:53:05 +00:00
|
|
|
|
|
|
|
let output = quote! {
|
2019-05-13 05:12:19 +00:00
|
|
|
impl #impl_generics ssz::Decode for #name #ty_generics #where_clause {
|
2019-05-05 02:11:25 +00:00
|
|
|
fn is_ssz_fixed_len() -> bool {
|
2019-02-19 02:53:05 +00:00
|
|
|
#(
|
2019-05-05 02:11:25 +00:00
|
|
|
#is_fixed_lens &&
|
2019-02-19 02:53:05 +00:00
|
|
|
)*
|
2019-05-05 02:11:25 +00:00
|
|
|
true
|
|
|
|
}
|
2019-02-19 02:53:05 +00:00
|
|
|
|
2019-05-05 02:11:25 +00:00
|
|
|
fn ssz_fixed_len() -> usize {
|
2019-05-13 05:12:19 +00:00
|
|
|
if <Self as ssz::Decode>::is_ssz_fixed_len() {
|
2020-10-25 23:27:39 +00:00
|
|
|
let mut len: usize = 0;
|
2019-05-05 02:11:25 +00:00
|
|
|
#(
|
2020-10-25 23:27:39 +00:00
|
|
|
len = len
|
|
|
|
.checked_add(#fixed_lens)
|
|
|
|
.expect("decode ssz_fixed_len overflow");
|
2019-05-05 02:11:25 +00:00
|
|
|
)*
|
2020-10-25 23:27:39 +00:00
|
|
|
len
|
2019-05-05 02:11:25 +00:00
|
|
|
} else {
|
|
|
|
ssz::BYTES_PER_LENGTH_OFFSET
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-24 01:00:49 +00:00
|
|
|
fn from_ssz_bytes(bytes: &[u8]) -> std::result::Result<Self, ssz::DecodeError> {
|
2020-03-04 00:45:01 +00:00
|
|
|
if <Self as ssz::Decode>::is_ssz_fixed_len() {
|
|
|
|
if bytes.len() != <Self as ssz::Decode>::ssz_fixed_len() {
|
|
|
|
return Err(ssz::DecodeError::InvalidByteLength {
|
|
|
|
len: bytes.len(),
|
|
|
|
expected: <Self as ssz::Decode>::ssz_fixed_len(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-25 23:27:39 +00:00
|
|
|
let mut start: usize = 0;
|
2020-03-04 00:45:01 +00:00
|
|
|
let mut end = start;
|
|
|
|
|
|
|
|
macro_rules! decode_field {
|
|
|
|
($type: ty) => {{
|
|
|
|
start = end;
|
2020-10-25 23:27:39 +00:00
|
|
|
end = end
|
|
|
|
.checked_add(<$type as ssz::Decode>::ssz_fixed_len())
|
|
|
|
.ok_or_else(|| ssz::DecodeError::OutOfBoundsByte {
|
|
|
|
i: usize::max_value()
|
|
|
|
})?;
|
2020-03-04 00:45:01 +00:00
|
|
|
let slice = bytes.get(start..end)
|
|
|
|
.ok_or_else(|| ssz::DecodeError::InvalidByteLength {
|
|
|
|
len: bytes.len(),
|
|
|
|
expected: end
|
|
|
|
})?;
|
|
|
|
<$type as ssz::Decode>::from_ssz_bytes(slice)?
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2020-05-28 01:37:40 +00:00
|
|
|
#(
|
|
|
|
#fixed_decodes
|
|
|
|
)*
|
|
|
|
|
2020-03-04 00:45:01 +00:00
|
|
|
Ok(Self {
|
|
|
|
#(
|
2020-05-28 01:37:40 +00:00
|
|
|
#field_names,
|
2020-03-04 00:45:01 +00:00
|
|
|
)*
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
let mut builder = ssz::SszDecoderBuilder::new(bytes);
|
2019-05-05 02:11:25 +00:00
|
|
|
|
|
|
|
#(
|
2020-03-04 00:45:01 +00:00
|
|
|
#register_types
|
2019-05-05 02:11:25 +00:00
|
|
|
)*
|
2020-03-04 00:45:01 +00:00
|
|
|
|
|
|
|
let mut decoder = builder.build()?;
|
|
|
|
|
2020-05-28 01:37:40 +00:00
|
|
|
#(
|
|
|
|
#decodes
|
|
|
|
)*
|
|
|
|
|
|
|
|
|
2020-03-04 00:45:01 +00:00
|
|
|
Ok(Self {
|
|
|
|
#(
|
2020-05-28 01:37:40 +00:00
|
|
|
#field_names,
|
2020-03-04 00:45:01 +00:00
|
|
|
)*
|
|
|
|
})
|
|
|
|
}
|
2019-02-19 02:53:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
output.into()
|
|
|
|
}
|