From 4d9ee162d18f5280655c8ac4e9d10a94141e33c4 Mon Sep 17 00:00:00 2001 From: Emma Turner Date: Tue, 25 Jun 2024 12:18:17 +0100 Subject: [PATCH] encoding: remove hash field kind --- CHANGELOG.md | 1 + tezos-encoding-derive/src/encoding.rs | 1 - tezos-encoding-derive/src/make.rs | 1 - tezos-encoding-derive/src/nom.rs | 87 +++++++-------------------- tezos-encoding-derive/src/symbol.rs | 3 +- tezos-encoding/src/nom.rs | 16 +---- 6 files changed, 28 insertions(+), 81 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a195eba830..a47c452702 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ parameterized by the lifetime of the input byte slice. - `tezos_data_encoding`: Removed unused `DecodeErrorKind::Hash` and `DecodeError::hash_error` - `tezos_crypto_rs`: Removed unused `Error` type from `PublicKeyWithHash` +- Removed support for `hash` field attribute when deriving encodings. ### Fixed diff --git a/tezos-encoding-derive/src/encoding.rs b/tezos-encoding-derive/src/encoding.rs index a9ad825fdc..4d2cfcdeb6 100644 --- a/tezos-encoding-derive/src/encoding.rs +++ b/tezos-encoding-derive/src/encoding.rs @@ -21,7 +21,6 @@ pub struct StructEncoding<'a> { #[derive(Debug)] pub enum FieldKind<'a> { Encoded(Box>), - Hash, Skip, } diff --git a/tezos-encoding-derive/src/make.rs b/tezos-encoding-derive/src/make.rs index 2d82ebfb2f..d40b6ebf1e 100644 --- a/tezos-encoding-derive/src/make.rs +++ b/tezos-encoding-derive/src/make.rs @@ -62,7 +62,6 @@ fn make_fields<'a>( fn field_kind<'a, 'b>(meta: &'a [syn::Meta]) -> Option> { meta.iter().find_map(|meta| match meta { syn::Meta::Path(path) if path == symbol::SKIP => Some(FieldKind::Skip), - syn::Meta::Path(path) if path == symbol::HASH => Some(FieldKind::Hash), _ => None, }) } diff --git a/tezos-encoding-derive/src/nom.rs b/tezos-encoding-derive/src/nom.rs index 1548d1e468..cdcf09404f 100644 --- a/tezos-encoding-derive/src/nom.rs +++ b/tezos-encoding-derive/src/nom.rs @@ -1,6 +1,7 @@ // Copyright (c) SimpleStaking, Viable Systems and Tezedge Contributors // SPDX-FileCopyrightText: 2023 Nomadic Labs -// SPDX-FileCopyrightText: 2023 TriliTech +// SPDX-FileCopyrightText: 2023-2024 TriliTech +// // SPDX-License-Identifier: MIT use once_cell::sync::Lazy as SyncLazy; @@ -127,82 +128,39 @@ fn generate_struct_one_field_nom_read(encoding: &StructEncoding) -> TokenStream fn generate_struct_many_fields_nom_read(encoding: &StructEncoding) -> TokenStream { let name = encoding.name; - let (fields, hash) = encoding - .fields - .iter() - .partition::, _>(|f| !matches!(f.kind, FieldKind::Hash)); - let field1 = fields.iter().map(|field| field.name); + let field1 = encoding.fields.iter().map(|field| field.name); let field2 = field1.clone(); - let field_name = fields + let field_name = encoding + .fields .iter() .map(|field| format!("{}::{}", name, field.name)); let field_nom_read = encoding.fields.iter().map(generate_struct_field_nom_read); - if let Some(hash_field) = hash.first() { - let field3 = field1.clone(); - let hash_name = hash_field.name; - quote_spanned! { - hash_field.name.span()=> - nom::combinator::map( - tezos_data_encoding::nom::hashed( - nom::sequence::tuple(( - #(tezos_data_encoding::nom::field(#field_name, #field_nom_read)),* - )) - ), - |((#(#field2),*), #hash_name)| { - #name { #(#field3),*, #hash_name: #hash_name.into() } - }) - } - } else { - quote_spanned! { - encoding.name.span()=> - nom::combinator::map( - nom::sequence::tuple(( - #(tezos_data_encoding::nom::field(#field_name, #field_nom_read)),* - )), - |(#(#field1),*)| #name { #(#field2),* } - ) - } + quote_spanned! { + encoding.name.span()=> + nom::combinator::map( + nom::sequence::tuple(( + #(tezos_data_encoding::nom::field(#field_name, #field_nom_read)),* + )), + |(#(#field1),*)| #name { #(#field2),* } + ) } } fn generate_struct_multi_fields_nom_read(encoding: &StructEncoding) -> TokenStream { let name = encoding.name; - let (fields, hash) = encoding - .fields - .iter() - .partition::, _>(|f| !matches!(f.kind, FieldKind::Hash)); - let field1 = fields.iter().map(|field| field.name); + let field1 = encoding.fields.iter().map(|field| field.name); let field2 = field1.clone(); - let field_name = fields + let field_name = encoding + .fields .iter() .map(|field| format!("{}::{}", name, field.name)); let field_nom_read = encoding.fields.iter().map(generate_struct_field_nom_read); - if let Some(hash_field) = hash.first() { - let field3 = field1.clone(); - let field4 = field1.clone(); - let hash_name = hash_field.name; - quote_spanned! { - hash_field.name.span()=> - nom::combinator::map( - tezos_data_encoding::nom::hashed( - (|input| { - #(let (input, #field1) = tezos_data_encoding::nom::field(#field_name, #field_nom_read)(input)?;)* - Ok((input, (#(#field2),* ))) - }) - ), - |((#(#field3),*), #hash_name)| { - #name { #(#field4),*, #hash_name: #hash_name.into() } - } - ) - } - } else { - quote_spanned! { - encoding.name.span()=> - (|input| { - #(let (input, #field1) = tezos_data_encoding::nom::field(#field_name, #field_nom_read)(input)?;)* - Ok((input, #name { #(#field2),* })) - }) - } + quote_spanned! { + encoding.name.span()=> + (|input| { + #(let (input, #field1) = tezos_data_encoding::nom::field(#field_name, #field_nom_read)(input)?;)* + Ok((input, #name { #(#field2),* })) + }) } } @@ -222,7 +180,6 @@ fn generate_struct_field_nom_read(field: &FieldEncoding) -> TokenStream { } } FieldKind::Skip => quote!(|input| Ok((input, Default::default()))), - FieldKind::Hash => unreachable!(), } } diff --git a/tezos-encoding-derive/src/symbol.rs b/tezos-encoding-derive/src/symbol.rs index 38edbba3ac..1f38bf3257 100644 --- a/tezos-encoding-derive/src/symbol.rs +++ b/tezos-encoding-derive/src/symbol.rs @@ -1,4 +1,6 @@ // Copyright (c) SimpleStaking, Viable Systems and Tezedge Contributors +// SPDX-FileCopyrightText: 2024 TriliTech +// // SPDX-License-Identifier: MIT use std::{ @@ -42,7 +44,6 @@ pub const COMPOSITE: Symbol = Symbol("composite"); /// Attribute name used to mark field/variant as ignored. pub const SKIP: Symbol = Symbol("skip"); -pub const HASH: Symbol = Symbol("hash"); /// Attribute used to specify maximal size/lengh. pub const MAX: Symbol = Symbol("max"); diff --git a/tezos-encoding/src/nom.rs b/tezos-encoding/src/nom.rs index 9c5289cc0d..15c0369b4e 100644 --- a/tezos-encoding/src/nom.rs +++ b/tezos-encoding/src/nom.rs @@ -1,5 +1,6 @@ // Copyright (c) SimpleStaking, Viable Systems, Nomadic Labs and Tezedge Contributors -// SPDX-CopyrightText: 2022-2023 TriliTech +// SPDX-CopyrightText: 2022-2024 TriliTech +// // SPDX-License-Identifier: MIT use bitvec::slice::BitSlice; @@ -506,7 +507,7 @@ where move |input| parser(input).map_err(|e| e.map(|e| e.add_field(name))) } -/// Applies the `parser` to the input, addin enum variant context to the error. +/// Applies the `parser` to the input, adding enum variant context to the error. #[inline(always)] pub fn variant<'a, O, F>( name: &'static str, @@ -566,17 +567,6 @@ pub fn n_bignum(mut input: NomInput) -> NomResult { Ok((input, BigUint::from_bytes_be(&bitvec.into_vec()))) } -pub fn hashed<'a, O, F>(mut parser: F) -> impl FnMut(NomInput<'a>) -> NomResult<'a, (O, Vec)> -where - F: FnMut(NomInput<'a>) -> NomResult<'a, O>, -{ - move |input| { - let (rest, result) = parser(input)?; - let hash = crypto::blake2b::digest_256(&input[..input.len() - rest.len()]); - Ok((rest, (result, hash))) - } -} - #[cfg(test)] mod test { use num_bigint::BigInt;