diff --git a/core/src/data_lookup/compact.rs b/core/src/data_lookup/compact.rs index 7f930ab..aad7b5c 100644 --- a/core/src/data_lookup/compact.rs +++ b/core/src/data_lookup/compact.rs @@ -35,7 +35,7 @@ where } } -// If .size is equal to u32::MAX then the no commitment was generated +// If .size is 0, and index contains items then no commitment was generated // because of an error that occurred. // // This is just a temporary solution that will be replaced by a more @@ -56,17 +56,22 @@ impl CompactDataLookup { Self { size, index } } + pub fn is_error(&self) -> bool { + // For backward compatibility, case when size is u32::MAX is also supported + self.size == u32::MAX || (self.size == 0 && !self.index.is_empty()) + } + + // Data lookup is not valid if size is 0 and lookup index is not empty + fn new_error() -> Self { + Self { + size: 0, + index: [DataLookupItem::new(AppId(0), 0)].to_vec(), + } + } + pub fn from_data_lookup(lookup: &DataLookup) -> Self { - if lookup.is_error { - // Data lookup is not valid if size is 0 and lookup index is not empty - return CompactDataLookup { - size: 0, - index: [DataLookupItem { - app_id: AppId(0), - start: 0, - }] - .to_vec(), - }; + if lookup.is_error() { + return Self::new_error(); } let index = lookup diff --git a/core/src/data_lookup/mod.rs b/core/src/data_lookup/mod.rs index a466ac6..b0a0560 100644 --- a/core/src/data_lookup/mod.rs +++ b/core/src/data_lookup/mod.rs @@ -33,7 +33,6 @@ pub enum Error { )] pub struct DataLookup { pub(crate) index: Vec<(AppId, DataLookupRange)>, - pub(crate) is_error: bool, } impl DataLookup { @@ -45,6 +44,10 @@ impl DataLookup { self.len() == 0 } + pub fn is_error(&self) -> bool { + self.is_empty() && !self.index.is_empty() + } + pub fn range_of(&self, app_id: AppId) -> Option { self.index .iter() @@ -113,25 +116,18 @@ impl DataLookup { }) .collect::>()?; - Ok(Self { - index, - is_error: false, - }) + Ok(Self { index }) } /// This function is used a block contains no data submissions. pub fn new_empty() -> Self { - Self { - index: Vec::new(), - is_error: false, - } + Self { index: Vec::new() } } /// This function is only used when something has gone wrong during header extension building pub fn new_error() -> Self { Self { - index: Vec::new(), - is_error: true, + index: vec![(AppId(0), 0..0)], } } } @@ -140,7 +136,7 @@ impl TryFrom for DataLookup { type Error = Error; fn try_from(compacted: CompactDataLookup) -> Result { - if compacted.size == u32::MAX { + if compacted.is_error() { return Ok(DataLookup::new_error()); } @@ -165,10 +161,7 @@ impl TryFrom for DataLookup { index.push((prev_id, offset..compacted.size)); } - let lookup = DataLookup { - index, - is_error: false, - }; + let lookup = DataLookup { index }; ensure!(lookup.len() == compacted.size, Error::DataNotSorted); Ok(lookup)