diff --git a/core/src/data_lookup/compact.rs b/core/src/data_lookup/compact.rs
index a291a66..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,7 +52,18 @@ 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 {
+ return CompactDataLookup {
+ size: u32::MAX,
+ index: Vec::default(),
+ };
+ }
+
let index = lookup
.index
.iter()
@@ -53,8 +75,11 @@ impl CompactDataLookup {
}
}
+// 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 {
- 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 c241616..a466ac6 100644
--- a/core/src/data_lookup/mod.rs
+++ b/core/src/data_lookup/mod.rs
@@ -33,6 +33,7 @@ pub enum Error {
)]
pub struct DataLookup {
pub(crate) index: Vec<(AppId, DataLookupRange)>,
+ pub(crate) is_error: bool,
}
impl DataLookup {
@@ -112,12 +113,26 @@ impl DataLookup {
})
.collect::>()?;
- Ok(Self { index })
+ Ok(Self {
+ index,
+ is_error: false,
+ })
}
- /// This function is only used when something has gone wrong during header extension building
+ /// This function is used a block contains no data submissions.
pub fn new_empty() -> Self {
- Self { index: Vec::new() }
+ Self {
+ index: Vec::new(),
+ is_error: false,
+ }
+ }
+
+ /// 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,
+ }
}
}
@@ -125,6 +140,10 @@ impl TryFrom for DataLookup {
type Error = Error;
fn try_from(compacted: CompactDataLookup) -> Result {
+ if compacted.size == u32::MAX {
+ return Ok(DataLookup::new_error());
+ }
+
let mut offset = 0;
let mut prev_id = AppId(0);
let mut index = Vec::with_capacity(
@@ -146,7 +165,10 @@ impl TryFrom for DataLookup {
index.push((prev_id, offset..compacted.size));
}
- let lookup = DataLookup { index };
+ let lookup = DataLookup {
+ index,
+ is_error: false,
+ };
ensure!(lookup.len() == compacted.size, Error::DataNotSorted);
Ok(lookup)
@@ -159,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()
}
}
@@ -219,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!(
diff --git a/core/src/header/extension/mod.rs b/core/src/header/extension/mod.rs
index e5278a8..d204487 100644
--- a/core/src/header/extension/mod.rs
+++ b/core/src/header/extension/mod.rs
@@ -52,6 +52,18 @@ impl HeaderExtension {
forward_to_version!(self, cols)
}
+ pub fn get_empty_header(data_root: H256, version: HeaderVersion) -> HeaderExtension {
+ match version {
+ HeaderVersion::V3 => v3::HeaderExtension::get_empty_header(data_root).into(),
+ }
+ }
+
+ pub fn get_faulty_header(data_root: H256, version: HeaderVersion) -> HeaderExtension {
+ match version {
+ HeaderVersion::V3 => v3::HeaderExtension::get_faulty_header(data_root).into(),
+ }
+ }
+
pub fn get_header_version(&self) -> HeaderVersion {
match self {
HeaderExtension::V3(_) => HeaderVersion::V3,
diff --git a/core/src/header/extension/v3.rs b/core/src/header/extension/v3.rs
index 3980c19..a5c0358 100644
--- a/core/src/header/extension/v3.rs
+++ b/core/src/header/extension/v3.rs
@@ -3,6 +3,7 @@ use scale_info::TypeInfo;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use sp_core::{RuntimeDebug, H256};
+use sp_std::vec;
use crate::{v3::KateCommitment, DataLookup};
@@ -30,4 +31,24 @@ impl HeaderExtension {
pub fn cols(&self) -> u16 {
self.commitment.cols
}
+
+ pub fn get_empty_header(data_root: H256) -> Self {
+ let empty_commitment: Vec = vec![];
+ let empty_app_lookup = DataLookup::new_empty();
+ let commitment = KateCommitment::new(0, 0, data_root, empty_commitment);
+ HeaderExtension {
+ app_lookup: empty_app_lookup,
+ commitment,
+ }
+ }
+
+ pub fn get_faulty_header(data_root: H256) -> Self {
+ let empty_commitment: Vec = vec![];
+ let error_app_lookup = DataLookup::new_error();
+ let commitment = KateCommitment::new(0, 0, data_root, empty_commitment);
+ HeaderExtension {
+ app_lookup: error_app_lookup,
+ commitment,
+ }
+ }
}