Skip to content

Commit

Permalink
empty namespace table implies empty block payload, with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ggutoski committed Jun 26, 2024
1 parent 50c2abc commit a0eb396
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
12 changes: 8 additions & 4 deletions sequencer/src/block/full_payload/ns_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,11 @@ impl NsTable {
/// 1. Byte length must hold a whole number of entries.
/// 2. All namespace IDs and offsets must increase monotonically. Offsets
/// must be nonzero.
/// 3. Header consistent with byte length (obsolete after
/// <https://github.com/EspressoSystems/espresso-sequencer/issues/1604>)
/// 4. Final offset must equal `payload_byte_len` (obsolete after
/// <https://github.com/EspressoSystems/espresso-sequencer/issues/1604>)
/// 3. Header consistent with byte length. (Obsolete after
/// <https://github.com/EspressoSystems/espresso-sequencer/issues/1604>.)
/// 4. Final offset must equal `payload_byte_len`. (Obsolete after
/// <https://github.com/EspressoSystems/espresso-sequencer/issues/1604>.)
/// If the namespace table is empty then `payload_byte_len` must be 0.
pub fn validate(
&self,
payload_byte_len: &PayloadByteLen,
Expand All @@ -242,6 +243,8 @@ impl NsTable {
if final_offset != payload_byte_len.as_usize() {
return Err(InvalidFinalOffset);
}
} else if payload_byte_len.as_usize() != 0 {
return Err(ExpectNonemptyNsTable);
}

Ok(())
Expand Down Expand Up @@ -372,6 +375,7 @@ pub enum NsTableValidationError {
NonIncreasingEntries,
InvalidHeader, // TODO this variant obsolete after https://github.com/EspressoSystems/espresso-sequencer/issues/1604
InvalidFinalOffset, // TODO this variant obsolete after https://github.com/EspressoSystems/espresso-sequencer/issues/1604
ExpectNonemptyNsTable,
}

pub struct NsTableBuilder {
Expand Down
22 changes: 22 additions & 0 deletions sequencer/src/block/full_payload/ns_table/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ async fn payload_byte_len() {

// final offset less than payload byte len
modify_final_offset(-1);

// zero-length payload
let empty_block = Payload::from_transactions([], &Default::default(), &Default::default())
.await
.unwrap()
.0;
assert_eq!(empty_block.ns_table().len().0, 0);
assert_eq!(
empty_block.ns_table().bytes,
usize_to_bytes::<NUM_NSS_BYTE_LEN>(0)
);
empty_block
.ns_table()
.validate(&empty_block.byte_len())
.unwrap();

// empty namespace table with nonempty payload
*block.ns_table_mut() = empty_block.ns_table().clone();
assert_eq!(
block.ns_table().validate(&payload_byte_len).unwrap_err(),
ExpectNonemptyNsTable
);
}

#[test]
Expand Down

0 comments on commit a0eb396

Please sign in to comment.