Skip to content

Commit

Permalink
Added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
markopoloparadox committed Apr 10, 2024
1 parent 6c631d3 commit 27d1afa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
57 changes: 34 additions & 23 deletions core/src/data_lookup/compact.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<A, S> From<(A, S)> for DataLookupItem
where
u32: From<A>,
Expand All @@ -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
Expand All @@ -41,34 +52,34 @@ pub struct CompactDataLookup {
}

impl CompactDataLookup {
pub fn from_expanded(lookup: &DataLookup) -> Self {
pub fn new(size: u32, index: Vec<DataLookupItem>) -> 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<DataLookup> 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)
}
}
4 changes: 2 additions & 2 deletions core/src/data_lookup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl TryFrom<CompactDataLookup> for DataLookup {
impl Encode for DataLookup {
/// Encodes as a `compact::DataLookup`.
fn encode(&self) -> Vec<u8> {
let compacted = CompactDataLookup::from_expanded(self);
let compacted: CompactDataLookup = CompactDataLookup::from_data_lookup(&self);

Check warning on line 184 in core/src/data_lookup/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test

this expression creates a reference which is immediately dereferenced by the compiler
compacted.encode()
}
}
Expand Down Expand Up @@ -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!(
Expand Down

0 comments on commit 27d1afa

Please sign in to comment.