From 21216efabc8de7350908e7414717aa760ac8bd71 Mon Sep 17 00:00:00 2001 From: Vera Abramova Date: Fri, 25 Aug 2023 11:00:39 +0300 Subject: [PATCH] refactor: simplify cutter, remove all specialty treatment except option --- for_tests/westend9430_short_for_transaction | Bin 1479 -> 1517 bytes src/cut_metadata.rs | 338 ++++---------------- src/decoding_sci.rs | 4 +- src/special_indicators.rs | 6 +- src/tests.rs | 2 +- 5 files changed, 68 insertions(+), 282 deletions(-) diff --git a/for_tests/westend9430_short_for_transaction b/for_tests/westend9430_short_for_transaction index 3ebf0008a191bde4fe8f928f43bb94d065df3121..d3bea6e38b222bb141b424112c9201b8e7d7d22b 100644 GIT binary patch delta 69 zcmX@k{g#_ez|zFnAY~$(p(rB*2rw%E2@VD(5SxLOd83&Ci!h6VZ+=lpVvdC=3s8z7 Rl!4*a? diff --git a/src/cut_metadata.rs b/src/cut_metadata.rs index 249eda6..7bf0174 100644 --- a/src/cut_metadata.rs +++ b/src/cut_metadata.rs @@ -1,25 +1,10 @@ //! Metadata shortened, draft phase. use frame_metadata::v14::ExtrinsicMetadata; use parity_scale_codec::{Decode, Encode, OptionBool}; -use primitive_types::{H160, H512}; use scale_info::{ form::PortableForm, interner::UntrackedSymbol, Field, Path, PortableRegistry, Type, TypeDef, TypeDefBitSequence, TypeDefPrimitive, TypeDefVariant, Variant, }; -use sp_arithmetic::{PerU16, Perbill, Percent, Permill, Perquintill}; - -#[cfg(not(feature = "std"))] -use crate::additional_types::{ - AccountId32, PublicEcdsa, PublicEd25519, PublicSr25519, SignatureEcdsa, SignatureEd25519, - SignatureSr25519, -}; -#[cfg(feature = "std")] -use sp_core::{ - crypto::AccountId32, - ecdsa::{Public as PublicEcdsa, Signature as SignatureEcdsa}, - ed25519::{Public as PublicEd25519, Signature as SignatureEd25519}, - sr25519::{Public as PublicSr25519, Signature as SignatureSr25519}, -}; use crate::std::{borrow::ToOwned, string::String, vec::Vec}; @@ -36,9 +21,8 @@ use crate::decoding_sci::{ use crate::error::{MetaCutError, ParserError, SignableError}; use crate::propagated::{Checker, Propagated, SpecialtySet}; use crate::special_indicators::{ - Hint, PalletSpecificItem, SpecialtyTypeChecked, SpecialtyTypeHinted, ENUM_INDEX_ENCODED_LEN, + Hint, PalletSpecificItem, SpecialtyTypeHinted, ENUM_INDEX_ENCODED_LEN, }; -use crate::special_types::{special_case_era, special_case_h256, CheckCompact}; use crate::traits::{AddressableBuffer, AsMetadata, AsPallet, ExternalMemory, ResolveType}; use crate::MarkedData; @@ -173,15 +157,7 @@ impl HashPrep for PortableRegistry { fn hash_prep(&self) -> Result, MetaCutError> { let mut draft_registry = DraftRegistry::new(); for registry_entry in self.types.iter() { - match SpecialtyTypeHinted::from_ty(®istry_entry.ty) { - SpecialtyTypeHinted::Era => { - add_as_enum::( - &mut draft_registry, - ®istry_entry.ty.path, - None, - registry_entry.id, - )?; - } + match SpecialtyTypeHinted::from_type(®istry_entry.ty) { SpecialtyTypeHinted::Option(_) => { add_ty_as_regular::( &mut draft_registry, @@ -403,7 +379,7 @@ where if let TypeDef::Variant(x) = &call_ty.type_def { if let SpecialtyTypeHinted::PalletSpecific(PalletSpecificItem::Call) = - SpecialtyTypeHinted::from_ty(&call_ty) + SpecialtyTypeHinted::from_type(&call_ty) { pass_variant::( &x.variants, @@ -606,8 +582,64 @@ where let info_ty = Info::from_ty(&ty); propagated.add_info(&info_ty); - match SpecialtyTypeChecked::from_type::(&ty, data, ext_memory, position, registry) { - SpecialtyTypeChecked::None => match &ty.type_def { + + if let SpecialtyTypeHinted::Option(ty_symbol) = SpecialtyTypeHinted::from_type(&ty) { + propagated + .reject_compact() + .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; + add_ty_as_regular::(draft_registry, ty, id)?; + + let param_ty = registry + .resolve_ty(ty_symbol.id, ext_memory) + .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; + + match ¶m_ty.type_def { + TypeDef::Primitive(TypeDefPrimitive::Bool) => { + let slice_to_decode = data + .read_slice(ext_memory, *position, ENUM_INDEX_ENCODED_LEN) + .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; + OptionBool::decode(&mut slice_to_decode.as_ref()).map_err(|_| { + MetaCutError::Signable(SignableError::Parsing( + ParserError::UnexpectedOptionVariant { + position: *position, + }, + )) + })?; + *position += ENUM_INDEX_ENCODED_LEN; + add_ty_as_regular::(draft_registry, param_ty, ty_symbol.id) + } + _ => match data + .read_byte(ext_memory, *position) + .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))? + { + 0 => { + *position += ENUM_INDEX_ENCODED_LEN; + Ok(()) + } + 1 => { + *position += ENUM_INDEX_ENCODED_LEN; + pass_type::( + &Ty::Resolved(ResolvedTy { + ty: param_ty.to_owned(), + id: ty_symbol.id, + }), + data, + ext_memory, + position, + registry, + propagated, + draft_registry, + ) + } + _ => Err(MetaCutError::Signable(SignableError::Parsing( + ParserError::UnexpectedOptionVariant { + position: *position, + }, + ))), + }, + } + } else { + match &ty.type_def { TypeDef::Composite(x) => { pass_fields::( &x.fields, @@ -738,252 +770,6 @@ where )?; add_ty_as_regular::(draft_registry, ty, id) } - }, - SpecialtyTypeChecked::AccountId32 => { - add_ty_as_regular::(draft_registry, ty, id)?; - AccountId32::parse_check_compact::( - data, - ext_memory, - position, - propagated.compact_at(), - ) - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - Ok(()) - } - SpecialtyTypeChecked::Era => { - propagated - .reject_compact() - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - add_as_enum::(draft_registry, &ty.path, None, id)?; - special_case_era::(data, ext_memory, position) - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - Ok(()) - } - SpecialtyTypeChecked::H160 => { - add_ty_as_regular::(draft_registry, ty, id)?; - H160::parse_check_compact::(data, ext_memory, position, propagated.compact_at()) - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - Ok(()) - } - SpecialtyTypeChecked::H256 => { - propagated - .reject_compact() - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - add_ty_as_regular::(draft_registry, ty, id)?; - special_case_h256::( - data, - ext_memory, - position, - propagated.checker.specialty_set.hash256(), - ) - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - Ok(()) - } - SpecialtyTypeChecked::H512 => { - add_ty_as_regular::(draft_registry, ty, id)?; - H512::parse_check_compact::(data, ext_memory, position, propagated.compact_at()) - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - Ok(()) - } - SpecialtyTypeChecked::Option(ty_symbol) => { - propagated - .reject_compact() - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - add_ty_as_regular::(draft_registry, ty, id)?; - - let param_ty = registry - .resolve_ty(ty_symbol.id, ext_memory) - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - - match ¶m_ty.type_def { - TypeDef::Primitive(TypeDefPrimitive::Bool) => { - let slice_to_decode = data - .read_slice(ext_memory, *position, ENUM_INDEX_ENCODED_LEN) - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - OptionBool::decode(&mut slice_to_decode.as_ref()).map_err(|_| { - MetaCutError::Signable(SignableError::Parsing( - ParserError::UnexpectedOptionVariant { - position: *position, - }, - )) - })?; - *position += ENUM_INDEX_ENCODED_LEN; - add_ty_as_regular::(draft_registry, param_ty, ty_symbol.id) - } - _ => match data - .read_byte(ext_memory, *position) - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))? - { - 0 => { - *position += ENUM_INDEX_ENCODED_LEN; - Ok(()) - } - 1 => { - *position += ENUM_INDEX_ENCODED_LEN; - pass_type::( - &Ty::Resolved(ResolvedTy { - ty: param_ty.to_owned(), - id: ty_symbol.id, - }), - data, - ext_memory, - position, - registry, - propagated, - draft_registry, - ) - } - _ => Err(MetaCutError::Signable(SignableError::Parsing( - ParserError::UnexpectedOptionVariant { - position: *position, - }, - ))), - }, - } - } - SpecialtyTypeChecked::PalletSpecific { - pallet_name: _, - pallet_info, - pallet_variant, - item_ty_id, - variants, - item: _, - } => { - propagated - .reject_compact() - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - add_as_enum::(draft_registry, &ty.path, Some(pallet_variant), id)?; - pass_variant::( - &variants, - data, - ext_memory, - position, - registry, - draft_registry, - &pallet_info.path, - item_ty_id, - ) - } - SpecialtyTypeChecked::Perbill => { - add_ty_as_regular::(draft_registry, ty, id)?; - Perbill::parse_check_compact::( - data, - ext_memory, - position, - propagated.compact_at(), - ) - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - Ok(()) - } - SpecialtyTypeChecked::Percent => { - add_ty_as_regular::(draft_registry, ty, id)?; - Percent::parse_check_compact::( - data, - ext_memory, - position, - propagated.compact_at(), - ) - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - Ok(()) - } - SpecialtyTypeChecked::Permill => { - add_ty_as_regular::(draft_registry, ty, id)?; - Permill::parse_check_compact::( - data, - ext_memory, - position, - propagated.compact_at(), - ) - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - Ok(()) - } - SpecialtyTypeChecked::Perquintill => { - add_ty_as_regular::(draft_registry, ty, id)?; - Perquintill::parse_check_compact::( - data, - ext_memory, - position, - propagated.compact_at(), - ) - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - Ok(()) - } - SpecialtyTypeChecked::PerU16 => { - add_ty_as_regular::(draft_registry, ty, id)?; - PerU16::parse_check_compact::( - data, - ext_memory, - position, - propagated.compact_at(), - ) - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - Ok(()) - } - SpecialtyTypeChecked::PublicEd25519 => { - add_ty_as_regular::(draft_registry, ty, id)?; - PublicEd25519::parse_check_compact::( - data, - ext_memory, - position, - propagated.compact_at(), - ) - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - Ok(()) - } - SpecialtyTypeChecked::PublicSr25519 => { - add_ty_as_regular::(draft_registry, ty, id)?; - PublicSr25519::parse_check_compact::( - data, - ext_memory, - position, - propagated.compact_at(), - ) - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - Ok(()) - } - SpecialtyTypeChecked::PublicEcdsa => { - add_ty_as_regular::(draft_registry, ty, id)?; - PublicEcdsa::parse_check_compact::( - data, - ext_memory, - position, - propagated.compact_at(), - ) - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - Ok(()) - } - SpecialtyTypeChecked::SignatureEd25519 => { - add_ty_as_regular::(draft_registry, ty, id)?; - SignatureEd25519::parse_check_compact::( - data, - ext_memory, - position, - propagated.compact_at(), - ) - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - Ok(()) - } - SpecialtyTypeChecked::SignatureSr25519 => { - add_ty_as_regular::(draft_registry, ty, id)?; - SignatureSr25519::parse_check_compact::( - data, - ext_memory, - position, - propagated.compact_at(), - ) - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - Ok(()) - } - SpecialtyTypeChecked::SignatureEcdsa => { - add_ty_as_regular::(draft_registry, ty, id)?; - SignatureEcdsa::parse_check_compact::( - data, - ext_memory, - position, - propagated.compact_at(), - ) - .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; - Ok(()) } } } @@ -1214,7 +1000,7 @@ where .map_err(|e| MetaCutError::Signable(SignableError::Parsing(e)))?; let mut id = entry_symbol_id; - while let SpecialtyTypeHinted::None = SpecialtyTypeHinted::from_ty(&ty) { + while let SpecialtyTypeHinted::None = SpecialtyTypeHinted::from_type(&ty) { let type_def = ty.type_def.to_owned(); match type_def { TypeDef::Composite(x) => { diff --git a/src/decoding_sci.rs b/src/decoding_sci.rs index fc8ac70..fcb9cfd 100644 --- a/src/decoding_sci.rs +++ b/src/decoding_sci.rs @@ -216,7 +216,7 @@ where if let TypeDef::Variant(x) = &call_ty.type_def { if let SpecialtyTypeHinted::PalletSpecific(PalletSpecificItem::Call) = - SpecialtyTypeHinted::from_ty(&call_ty) + SpecialtyTypeHinted::from_type(&call_ty) { let variant_data = decode_variant::(&x.variants, data, ext_memory, position, &types) @@ -1204,7 +1204,7 @@ where info.push(info_ty) } - if let SpecialtyTypeHinted::None = SpecialtyTypeHinted::from_ty(&ty) { + if let SpecialtyTypeHinted::None = SpecialtyTypeHinted::from_type(&ty) { let type_def = ty.type_def.to_owned(); match type_def { TypeDef::Composite(x) => { diff --git a/src/special_indicators.rs b/src/special_indicators.rs index f417f7c..adef946 100644 --- a/src/special_indicators.rs +++ b/src/special_indicators.rs @@ -353,7 +353,7 @@ pub enum PalletSpecificItem { impl SpecialtyTypeHinted { /// Get `SpecialtyTypeHinted` from type-associated [`Path`]. - pub fn from_ty(ty: &Type) -> Self { + pub fn from_type(ty: &Type) -> Self { match ty.path.ident() { Some(a) => match a.as_str() { ACCOUNT_ID32 => Self::AccountId32, @@ -480,7 +480,7 @@ impl SpecialtyTypeChecked { E: ExternalMemory, M: AsMetadata, { - match SpecialtyTypeHinted::from_ty(ty) { + match SpecialtyTypeHinted::from_type(ty) { SpecialtyTypeHinted::None => Self::None, SpecialtyTypeHinted::AccountId32 => Self::AccountId32, SpecialtyTypeHinted::Era => Self::Era, @@ -500,7 +500,7 @@ impl SpecialtyTypeChecked { match registry.resolve_ty(item_ty_id, ext_memory) { Ok(variants_ty) => { if let SpecialtyTypeHinted::PalletSpecific(item_repeated) = - SpecialtyTypeHinted::from_ty(&variants_ty) + SpecialtyTypeHinted::from_type(&variants_ty) { if item != item_repeated { Self::None diff --git a/src/tests.rs b/src/tests.rs index 1d15146..4589d5b 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1185,7 +1185,7 @@ fn short_metadata_1_call_only() { assert_eq!(draft_metadata_header.pallet_name, "Utility"); assert_eq!(draft_metadata_header.call_ty_id, 265); assert_eq!(draft_metadata_header.index, 16); - assert_eq!(short_registry.types.len(), 10); + assert_eq!(short_registry.types.len(), 12); } #[test]