Skip to content

Commit

Permalink
feat(types): Add constructor for creating EDS of an empty block (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
oblique authored Mar 11, 2024
1 parent debe5fe commit 0936723
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
26 changes: 25 additions & 1 deletion types/src/rsmt2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::consts::data_availability_header::{
};
use crate::namespaced_data::{NamespacedData, NamespacedDataId};
use crate::nmt::{Namespace, NamespacedSha2Hasher, Nmt, NmtExt, NS_SIZE};
use crate::{bail_validation, DataAvailabilityHeader, Error, Result};
use crate::{bail_validation, DataAvailabilityHeader, Error, InfoByte, Result};

/// Represents either column or row of the [`ExtendedDataSquare`].
///
Expand Down Expand Up @@ -237,6 +237,19 @@ impl ExtendedDataSquare {
Ok(eds)
}

/// Crate a new EDS that represents an empty block
pub fn empty() -> ExtendedDataSquare {
// ODS in this case it is just one tail padded share.
let ods = vec![[
Namespace::TAIL_PADDING.as_bytes(),
&[InfoByte::new(0, true).unwrap().as_u8()],
&[0; SHARE_SIZE - NS_SIZE - 1],
]
.concat()];

ExtendedDataSquare::from_ods(ods).expect("invalid EDS")
}

/// Create a new EDS out of the provided original data square shares.
///
/// This method is similar to the [`ExtendedDataSquare::new`] but parity data
Expand Down Expand Up @@ -466,6 +479,7 @@ pub(crate) fn is_ods_square(row: u16, column: u16, square_width: u16) -> bool {
#[cfg(test)]
mod tests {
use super::*;
use crate::ExtendedHeader;

#[test]
fn axis_type_serialization() {
Expand Down Expand Up @@ -716,4 +730,14 @@ mod tests {
ExtendedDataSquare::new(vec![share(1); square_width.pow(2)], "fake".to_string())
.unwrap_err();
}

#[test]
fn empty_block_eds() {
let s = include_str!("../test_data/chain1/extended_header_block_1.json");
let genesis: ExtendedHeader = serde_json::from_str(s).unwrap();

let eds = ExtendedDataSquare::empty();
let dah = DataAvailabilityHeader::from_eds(&eds);
assert_eq!(dah, genesis.dah);
}
}
16 changes: 3 additions & 13 deletions types/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use crate::byzantine::test_utils::corrupt_eds;
use crate::consts::appconsts::{SHARE_INFO_BYTES, SHARE_SIZE};
use crate::consts::version;
use crate::hash::{Hash, HashExt};
use crate::nmt::{Namespace, NamespacedHash, NamespacedHashExt, NS_SIZE};
use crate::nmt::{Namespace, NS_SIZE};
use crate::{DataAvailabilityHeader, ExtendedDataSquare, ExtendedHeader, ValidatorSet};

/// [`ExtendedHeader`] generator for testing purposes.
Expand Down Expand Up @@ -411,12 +411,7 @@ fn generate_new(
proposer_priority: 0_i64.into(),
}),
),
dah: dah.unwrap_or_else(|| {
DataAvailabilityHeader::new_unchecked(
vec![NamespacedHash::empty_root(), NamespacedHash::empty_root()],
vec![NamespacedHash::empty_root(), NamespacedHash::empty_root()],
)
}),
dah: dah.unwrap_or_else(|| DataAvailabilityHeader::from_eds(&ExtendedDataSquare::empty())),
};

hash_and_sign(&mut header, signing_key);
Expand Down Expand Up @@ -485,12 +480,7 @@ fn generate_next(
}],
},
validator_set: current.validator_set.clone(),
dah: dah.unwrap_or_else(|| {
DataAvailabilityHeader::new_unchecked(
vec![NamespacedHash::empty_root(), NamespacedHash::empty_root()],
vec![NamespacedHash::empty_root(), NamespacedHash::empty_root()],
)
}),
dah: dah.unwrap_or_else(|| DataAvailabilityHeader::from_eds(&ExtendedDataSquare::empty())),
};

hash_and_sign(&mut header, signing_key);
Expand Down

0 comments on commit 0936723

Please sign in to comment.