diff --git a/core/src/data_lookup/compact.rs b/core/src/data_lookup/compact.rs index 86f8f23..cfc2625 100644 --- a/core/src/data_lookup/compact.rs +++ b/core/src/data_lookup/compact.rs @@ -1,14 +1,13 @@ use crate::{AppId, DataLookup}; use codec::{Decode, Encode}; -use derive_more::Constructor; use scale_info::TypeInfo; use sp_std::vec::Vec; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -#[derive(Copy, Clone, Encode, Decode, TypeInfo, Constructor, Debug)] +#[derive(Copy, Clone, Encode, Decode, TypeInfo, Debug)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] pub struct DataLookupItem { @@ -17,6 +16,12 @@ pub struct DataLookupItem { pub start: u32, } +impl DataLookupItem { + pub fn new(app_id: AppId, start: u32) -> Self { + Self { app_id, start } + } +} + impl From<(A, S)> for DataLookupItem where u32: From, @@ -30,7 +35,13 @@ where } } -#[derive(Encode, Decode, TypeInfo, Constructor, Debug, Clone)] +// If .size is equal to u32::MAX then the no commitment was generated +// because of an error that occurred. +// +// This is just a temporary solution that will be replaced by a more +// sofisticated one once we do to do the next header change. +// +#[derive(Encode, Decode, TypeInfo, Debug, Clone)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct CompactDataLookup { /// size of the look up @@ -41,34 +52,34 @@ pub struct CompactDataLookup { } impl CompactDataLookup { - pub fn from_expanded(lookup: &DataLookup) -> Self { + pub fn new(size: u32, index: Vec) -> Self { + Self { size, index } + } + + pub fn from_data_lookup(lookup: &DataLookup) -> Self { if lookup.is_error { - CompactDataLookup { + return CompactDataLookup { size: u32::MAX, index: Vec::default(), - } - } else { - let index = lookup - .index - .iter() - .filter(|(id, _)| *id != AppId(0)) - .map(|(id, range)| DataLookupItem::new(*id, range.start)) - .collect(); - let size = lookup.index.last().map_or(0, |(_, range)| range.end); - Self { size, index } + }; } + + let index = lookup + .index + .iter() + .filter(|(id, _)| *id != AppId(0)) + .map(|(id, range)| DataLookupItem::new(*id, range.start)) + .collect(); + let size = lookup.index.last().map_or(0, |(_, range)| range.end); + Self { size, index } } } +// We added this just to please the compiler regarding the Serde macro. +// Do not change this implementation! +// impl From for CompactDataLookup { fn from(lookup: DataLookup) -> Self { - if lookup.is_error { - CompactDataLookup { - size: u32::MAX, - index: Vec::default(), - } - } else { - CompactDataLookup::from_expanded(&lookup) - } + Self::from_data_lookup(&lookup) } } diff --git a/core/src/data_lookup/mod.rs b/core/src/data_lookup/mod.rs index c83d603..a466ac6 100644 --- a/core/src/data_lookup/mod.rs +++ b/core/src/data_lookup/mod.rs @@ -181,7 +181,7 @@ impl TryFrom for DataLookup { impl Encode for DataLookup { /// Encodes as a `compact::DataLookup`. fn encode(&self) -> Vec { - let compacted = CompactDataLookup::from_expanded(self); + let compacted: CompactDataLookup = CompactDataLookup::from_data_lookup(&self); compacted.encode() } } @@ -241,7 +241,7 @@ mod test { fn compressed_conversions(id_lens: Vec<(u32, usize)>) { let lookup = DataLookup::from_id_and_len_iter(id_lens.into_iter()).unwrap(); - let compact_lookup = CompactDataLookup::from_expanded(&lookup); + let compact_lookup = CompactDataLookup::from_data_lookup(&lookup); let expanded_lookup = DataLookup::try_from(compact_lookup.clone()).unwrap(); assert_eq!(