Skip to content

Commit

Permalink
Merge pull request availproject#166 from availproject/ghali/prevent-e…
Browse files Browse the repository at this point in the history
…mpty-data

Prevent empty bounded vectors for appKey and data submission
  • Loading branch information
Leouarz authored Jun 5, 2023
2 parents 3512e30 + facd564 commit 49e2c04
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
9 changes: 8 additions & 1 deletion pallets/dactr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub mod pallet {
key: AppKeyFor<T>,
) -> DispatchResultWithPostInfo {
let owner = ensure_signed(origin)?;

ensure!(!key.is_empty(), Error::<T>::AppKeyCannotBeEmpty);
let id = AppKeys::<T>::try_mutate(&key, |key_info| -> Result<AppId, Error<T>> {
ensure!(key_info.is_none(), Error::<T>::AppKeyAlreadyExists);

Expand All @@ -135,6 +135,7 @@ pub mod pallet {
data: AppDataFor<T>,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
ensure!(!data.is_empty(), Error::<T>::DataCannotBeEmpty);
Self::deposit_event(Event::DataSubmitted { who, data });

Ok(().into())
Expand Down Expand Up @@ -198,11 +199,17 @@ pub mod pallet {
pub enum Error<T> {
/// The application key already exists.
AppKeyAlreadyExists,
/// The application key is an empty string.
AppKeyCannotBeEmpty,
/// The last application ID overflowed.
LastAppIdOverflowed,
/// The submitted data is empty.
DataCannotBeEmpty,
/// The last block length proposal Id overflowed.
LastBlockLenProposalIdOverflowed,
/// The proposed block dimensions are out of bounds.
BlockDimensionsOutOfBounds,
/// The proposed block dimensions are too small.
BlockDimensionsTooSmall,
}

Expand Down
32 changes: 6 additions & 26 deletions pallets/dactr/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,13 @@ mod create_application_key {
}

#[test]
fn create_application_key_empty() {
fn app_key_cannot_be_empty() {
new_test_ext().execute_with(|| {
let alice: RuntimeOrigin = RawOrigin::Signed(ALICE).into();
let new_id = DataAvailability::peek_next_application_id();
let new_key = AppKeyFor::<Test>::try_from(vec![]).unwrap();

assert_eq!(DataAvailability::application_key(&new_key), None);
assert_ok!(DataAvailability::create_application_key(
alice,
new_key.clone()
));
assert_eq!(
DataAvailability::application_key(&new_key),
Some(AppKeyInfoFor::<Test> {
id: new_id,
owner: ALICE
})
);

let event = RuntimeEvent::DataAvailability(Event::ApplicationKeyCreated {
key: new_key,
owner: ALICE,
id: new_id,
});
System::assert_last_event(event);
let err = DataAvailability::create_application_key(alice, new_key);
assert_noop!(err, Error::AppKeyCannotBeEmpty);
})
}

Expand Down Expand Up @@ -110,15 +92,13 @@ mod submit_data {
}

#[test]
fn submit_data_empty() {
fn data_cannot_be_empty() {
new_test_ext().execute_with(|| {
let alice: RuntimeOrigin = RawOrigin::Signed(ALICE).into();
let data = AppDataFor::<Test>::try_from(vec![]).unwrap();

assert_ok!(DataAvailability::submit_data(alice, data.clone()));

let event = RuntimeEvent::DataAvailability(Event::DataSubmitted { who: ALICE, data });
System::assert_last_event(event);
let err = DataAvailability::submit_data(alice, data);
assert_noop!(err, Error::DataCannotBeEmpty);
})
}

Expand Down

0 comments on commit 49e2c04

Please sign in to comment.