Skip to content

Commit

Permalink
Merge pull request #84 from availproject/ghali/header-error-management
Browse files Browse the repository at this point in the history
add empty / error header constructor, put u32::MAX as size for compac…
  • Loading branch information
markopoloparadox authored Apr 10, 2024
2 parents faafcab + 498ab18 commit 9504cdd
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 11 deletions.
35 changes: 30 additions & 5 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,7 +52,18 @@ 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 {
return CompactDataLookup {
size: u32::MAX,
index: Vec::default(),
};
}

let index = lookup
.index
.iter()
Expand All @@ -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<DataLookup> for CompactDataLookup {
fn from(lookup: DataLookup) -> Self {
CompactDataLookup::from_expanded(&lookup)
Self::from_data_lookup(&lookup)
}
}
34 changes: 28 additions & 6 deletions core/src/data_lookup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub enum Error {
)]
pub struct DataLookup {
pub(crate) index: Vec<(AppId, DataLookupRange)>,
pub(crate) is_error: bool,
}

impl DataLookup {
Expand Down Expand Up @@ -112,19 +113,37 @@ impl DataLookup {
})
.collect::<Result<_, _>>()?;

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,
}
}
}

impl TryFrom<CompactDataLookup> for DataLookup {
type Error = Error;

fn try_from(compacted: CompactDataLookup) -> Result<Self, Self::Error> {
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(
Expand All @@ -146,7 +165,10 @@ impl TryFrom<CompactDataLookup> 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)
Expand All @@ -159,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 @@ -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!(
Expand Down
12 changes: 12 additions & 0 deletions core/src/header/extension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions core/src/header/extension/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<u8> = 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<u8> = 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,
}
}
}

0 comments on commit 9504cdd

Please sign in to comment.