Skip to content

Commit e0a5242

Browse files
Check for naming length restriction before building proposal (#1960)
# Goal The goal of this PR is to check for provider name length before bundling a proposal that might eventually fail upon execution due to provider name being set is too long Closes #1654 # Checklist - [ ] Chain spec updated - [ ] Custom RPC OR Runtime API added/changed? Updated js/api-augment. - [ ] Design doc(s) updated - [x] Tests added - [ ] Benchmarks added - [ ] Weights updated
1 parent 7de94fb commit e0a5242

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

pallets/msa/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,13 +886,16 @@ pub mod pallet {
886886
origin: OriginFor<T>,
887887
provider_name: Vec<u8>,
888888
) -> DispatchResult {
889+
let bounded_name: BoundedVec<u8, T::MaxProviderNameSize> =
890+
provider_name.try_into().map_err(|_| Error::<T>::ExceedsMaxProviderNameSize)?;
891+
889892
let proposer = ensure_signed(origin)?;
890893
Self::ensure_valid_msa_key(&proposer)?;
891894

892895
let proposal: Box<T::Proposal> = Box::new(
893896
(Call::<T>::create_provider_via_governance {
894897
provider_key: proposer.clone(),
895-
provider_name,
898+
provider_name: bounded_name.into(),
896899
})
897900
.into(),
898901
);

pallets/msa/src/tests/governance_tests.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use frame_support::{
2-
assert_ok,
2+
assert_noop, assert_ok,
33
traits::{ChangeMembers, Hash},
44
};
55

@@ -8,7 +8,7 @@ use sp_weights::Weight;
88
use pretty_assertions::assert_eq;
99
use sp_core::{Encode, Pair};
1010

11-
use crate::tests::mock::*;
11+
use crate::{tests::mock::*, Error};
1212

1313
#[test]
1414
fn create_provider_via_governance_happy_path() {
@@ -121,3 +121,17 @@ fn propose_to_be_provider_happy_path() {
121121
assert!(Msa::is_registered_provider(_new_msa_id));
122122
})
123123
}
124+
125+
#[test]
126+
fn propose_to_be_provider_long_name_should_fail() {
127+
new_test_ext().execute_with(|| {
128+
// Create a new MSA account and request that it become a provider
129+
let (_new_msa_id, key_pair) = create_account();
130+
let proposal_res = Msa::propose_to_be_provider(
131+
RuntimeOrigin::signed(key_pair.public().into()),
132+
Vec::from("this_is_a_really_long_name_that_should_fail"),
133+
);
134+
135+
assert_noop!(proposal_res, Error::<Test>::ExceedsMaxProviderNameSize);
136+
})
137+
}

0 commit comments

Comments
 (0)