From 0936723c815fc6fc72aaa2f5c97e1c6f9a8d89e4 Mon Sep 17 00:00:00 2001 From: Yiannis Marangos Date: Mon, 11 Mar 2024 07:53:12 +0200 Subject: [PATCH] feat(types): Add constructor for creating EDS of an empty block (#241) --- types/src/rsmt2d.rs | 26 +++++++++++++++++++++++++- types/src/test_utils.rs | 16 +++------------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/types/src/rsmt2d.rs b/types/src/rsmt2d.rs index c3bd8294..ea2eb7bf 100644 --- a/types/src/rsmt2d.rs +++ b/types/src/rsmt2d.rs @@ -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`]. /// @@ -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 @@ -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() { @@ -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); + } } diff --git a/types/src/test_utils.rs b/types/src/test_utils.rs index 34d40c5b..d65199e7 100644 --- a/types/src/test_utils.rs +++ b/types/src/test_utils.rs @@ -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. @@ -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); @@ -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);