Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pallet-referenda: Detect incorrect pre-image length #3850

Merged
merged 4 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions prdoc/pr_3850.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
title: Detect incorrect pre-image length when submitting a referenda

doc:
- audience: Runtime User
description: |
When submitting a referenda the `proposal` is passed as argument.
The `proposal` is most of the time a reference to a `pre-image` and
which also contains the length of the `pre-image`. This pull request
adds some logic to check that if the `pre-image` already exists and if
it exists, it ensures that the length is passed correctly. This prevents
that the referenda can not be executed because of a mismatch of this
length.

crates:
- name: pallet-referenda
12 changes: 12 additions & 0 deletions substrate/frame/referenda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ pub mod pallet {
BadStatus,
/// The preimage does not exist.
PreimageNotExist,
/// The preimage is stored with a different length than the one provided.
PreimageStoredWithDifferentLength,
}

#[pallet::hooks]
Expand Down Expand Up @@ -462,6 +464,16 @@ pub mod pallet {
let proposal_origin = *proposal_origin;
let who = T::SubmitOrigin::ensure_origin(origin, &proposal_origin)?;

// If the pre-image is already stored, ensure that it has the same length as given in
// `proposal`.
if let (Some(preimage_len), Some(proposal_len)) =
(proposal.lookup_hash().and_then(|h| T::Preimages::len(&h)), proposal.lookup_len())
{
if preimage_len != proposal_len {
return Err(Error::<T, I>::PreimageStoredWithDifferentLength.into())
}
}

let track =
T::Tracks::track_for(&proposal_origin).map_err(|_| Error::<T, I>::NoTrack)?;
let submission_deposit = Self::take_deposit(who, T::SubmissionDeposit::get())?;
Expand Down
16 changes: 16 additions & 0 deletions substrate/frame/referenda/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,3 +666,19 @@ fn clear_metadata_works() {
}));
});
}

#[test]
fn detects_incorrect_len() {
ExtBuilder::default().build_and_execute(|| {
let hash = note_preimage(1);
assert_noop!(
Referenda::submit(
RuntimeOrigin::signed(1),
Box::new(RawOrigin::Root.into()),
frame_support::traits::Bounded::Lookup { hash, len: 3 },
DispatchTime::At(1),
),
Error::<Test>::PreimageStoredWithDifferentLength
);
});
}
Loading