Skip to content

Commit 1e1ba1b

Browse files
authored
Merge pull request #433 from opentensor/junius/detailed-error-type
Improve errors definition and docs
2 parents 3784610 + 4f8f62e commit 1e1ba1b

File tree

19 files changed

+373
-348
lines changed

19 files changed

+373
-348
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ pub mod pallet {
6666
pub enum Error<T> {
6767
/// The subnet does not exist, check the netuid parameter
6868
SubnetDoesNotExist,
69-
/// The max allowed validator number to be set is larger than threshold
70-
StorageValueOutOfRange,
71-
/// The maximum allowed UIDs is out of boundary, it not allowed
72-
MaxAllowedUIdsNotAllowed,
69+
/// The maximum number of subnet validators must be less than the maximum number of allowed UIDs in the subnet.
70+
MaxValidatorsLargerThanMaxUIds,
71+
/// The maximum number of subnet validators must be more than the current number of UIDs already in the subnet.
72+
MaxAllowedUIdsLessThanCurrentUIds,
7373
}
7474

7575
/// Dispatchable functions allows users to interact with the pallet and invoke state changes.
@@ -385,7 +385,7 @@ pub mod pallet {
385385
);
386386
ensure!(
387387
T::Subtensor::get_subnetwork_n(netuid) < max_allowed_uids,
388-
Error::<T>::MaxAllowedUIdsNotAllowed
388+
Error::<T>::MaxAllowedUIdsLessThanCurrentUIds
389389
);
390390
T::Subtensor::set_max_allowed_uids(netuid, max_allowed_uids);
391391
log::info!(
@@ -625,7 +625,7 @@ pub mod pallet {
625625
);
626626
ensure!(
627627
max_allowed_validators <= T::Subtensor::get_max_allowed_uids(netuid),
628-
Error::<T>::StorageValueOutOfRange
628+
Error::<T>::MaxValidatorsLargerThanMaxUIds
629629
);
630630

631631
T::Subtensor::set_max_allowed_validators(netuid, max_allowed_validators);

pallets/collective/src/lib.rs

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -367,23 +367,21 @@ pub mod pallet {
367367
/// Duplicate proposals not allowed
368368
DuplicateProposal,
369369
/// Proposal must exist
370-
ProposalMissing,
371-
/// Mismatched index
372-
WrongIndex,
370+
ProposalNotExists,
371+
/// Index mismatched the proposal hash
372+
IndexMismatchProposalHash,
373373
/// Duplicate vote ignored
374374
DuplicateVote,
375-
/// Members are already initialized.
376-
AlreadyInitialized,
377-
/// The close call was made too early, before the end of the voting.
378-
TooEarly,
375+
/// The call to close the proposal was made too early, before the end of the voting
376+
TooEarlyToCloseProposal,
379377
/// There can only be a maximum of `MaxProposals` active proposals.
380-
TooManyProposals,
381-
/// The given weight bound for the proposal was too low.
382-
WrongProposalWeight,
383-
/// The given length bound for the proposal was too low.
384-
WrongProposalLength,
378+
TooManyActiveProposals,
379+
/// The given weight-bound for the proposal was too low.
380+
ProposalWeightLessThanDispatchCallWeight,
381+
/// The given length-bound for the proposal was too low.
382+
ProposalLengthBoundLessThanProposalLength,
385383
/// The given motion duration for the proposal was too low.
386-
WrongDuration,
384+
DurationLowerThanConfiguredMotionDuration,
387385
}
388386

389387
// Note that councillor operations are assigned to the operational class.
@@ -489,7 +487,7 @@ pub mod pallet {
489487
let proposal_len = proposal.encoded_size();
490488
ensure!(
491489
proposal_len <= length_bound as usize,
492-
Error::<T, I>::WrongProposalLength
490+
Error::<T, I>::ProposalLengthBoundLessThanProposalLength
493491
);
494492

495493
let proposal_hash = T::Hashing::hash_of(&proposal);
@@ -544,7 +542,7 @@ pub mod pallet {
544542

545543
ensure!(
546544
duration >= T::MotionDuration::get(),
547-
Error::<T, I>::WrongDuration
545+
Error::<T, I>::DurationLowerThanConfiguredMotionDuration
548546
);
549547

550548
let threshold = (T::GetVotingMembers::get_count() / 2) + 1;
@@ -689,32 +687,6 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
689687
Self::members().contains(who)
690688
}
691689

692-
/// Execute immediately when adding a new proposal.
693-
pub fn do_propose_execute(
694-
proposal: Box<<T as Config<I>>::Proposal>,
695-
length_bound: MemberCount,
696-
) -> Result<(u32, DispatchResultWithPostInfo), DispatchError> {
697-
let proposal_len = proposal.encoded_size();
698-
ensure!(
699-
proposal_len <= length_bound as usize,
700-
Error::<T, I>::WrongProposalLength
701-
);
702-
703-
let proposal_hash = T::Hashing::hash_of(&proposal);
704-
ensure!(
705-
!<ProposalOf<T, I>>::contains_key(proposal_hash),
706-
Error::<T, I>::DuplicateProposal
707-
);
708-
709-
let seats = Self::members().len() as MemberCount;
710-
let result = proposal.dispatch(RawOrigin::Members(1, seats).into());
711-
Self::deposit_event(Event::Executed {
712-
proposal_hash,
713-
result: result.map(|_| ()).map_err(|e| e.error),
714-
});
715-
Ok((proposal_len as u32, result))
716-
}
717-
718690
/// Add a new proposal to be voted.
719691
pub fn do_propose_proposed(
720692
who: T::AccountId,
@@ -726,7 +698,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
726698
let proposal_len = proposal.encoded_size();
727699
ensure!(
728700
proposal_len <= length_bound as usize,
729-
Error::<T, I>::WrongProposalLength
701+
Error::<T, I>::ProposalLengthBoundLessThanProposalLength
730702
);
731703

732704
let proposal_hash = T::Hashing::hash_of(&proposal);
@@ -739,7 +711,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
739711
<Proposals<T, I>>::try_mutate(|proposals| -> Result<usize, DispatchError> {
740712
proposals
741713
.try_push(proposal_hash)
742-
.map_err(|_| Error::<T, I>::TooManyProposals)?;
714+
.map_err(|_| Error::<T, I>::TooManyActiveProposals)?;
743715
Ok(proposals.len())
744716
})?;
745717

@@ -775,8 +747,11 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
775747
index: ProposalIndex,
776748
approve: bool,
777749
) -> Result<bool, DispatchError> {
778-
let mut voting = Self::voting(proposal).ok_or(Error::<T, I>::ProposalMissing)?;
779-
ensure!(voting.index == index, Error::<T, I>::WrongIndex);
750+
let mut voting = Self::voting(proposal).ok_or(Error::<T, I>::ProposalNotExists)?;
751+
ensure!(
752+
voting.index == index,
753+
Error::<T, I>::IndexMismatchProposalHash
754+
);
780755

781756
let position_yes = voting.ayes.iter().position(|a| a == &who);
782757
let position_no = voting.nays.iter().position(|a| a == &who);
@@ -826,8 +801,11 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
826801
proposal_weight_bound: Weight,
827802
length_bound: u32,
828803
) -> DispatchResultWithPostInfo {
829-
let voting = Self::voting(proposal_hash).ok_or(Error::<T, I>::ProposalMissing)?;
830-
ensure!(voting.index == index, Error::<T, I>::WrongIndex);
804+
let voting = Self::voting(proposal_hash).ok_or(Error::<T, I>::ProposalNotExists)?;
805+
ensure!(
806+
voting.index == index,
807+
Error::<T, I>::IndexMismatchProposalHash
808+
);
831809

832810
let mut no_votes = voting.nays.len() as MemberCount;
833811
let mut yes_votes = voting.ayes.len() as MemberCount;
@@ -876,7 +854,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
876854
// Only allow actual closing of the proposal after the voting period has ended.
877855
ensure!(
878856
frame_system::Pallet::<T>::block_number() >= voting.end,
879-
Error::<T, I>::TooEarly
857+
Error::<T, I>::TooEarlyToCloseProposal
880858
);
881859

882860
let prime_vote = Self::prime().map(|who| voting.ayes.iter().any(|a| a == &who));
@@ -939,16 +917,16 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
939917
let key = ProposalOf::<T, I>::hashed_key_for(hash);
940918
// read the length of the proposal storage entry directly
941919
let proposal_len =
942-
storage::read(&key, &mut [0; 0], 0).ok_or(Error::<T, I>::ProposalMissing)?;
920+
storage::read(&key, &mut [0; 0], 0).ok_or(Error::<T, I>::ProposalNotExists)?;
943921
ensure!(
944922
proposal_len <= length_bound,
945-
Error::<T, I>::WrongProposalLength
923+
Error::<T, I>::ProposalLengthBoundLessThanProposalLength
946924
);
947-
let proposal = ProposalOf::<T, I>::get(hash).ok_or(Error::<T, I>::ProposalMissing)?;
925+
let proposal = ProposalOf::<T, I>::get(hash).ok_or(Error::<T, I>::ProposalNotExists)?;
948926
let proposal_weight = proposal.get_dispatch_info().weight;
949927
ensure!(
950928
proposal_weight.all_lte(weight_bound),
951-
Error::<T, I>::WrongProposalWeight
929+
Error::<T, I>::ProposalWeightLessThanDispatchCallWeight
952930
);
953931
Ok((proposal, proposal_len as usize))
954932
}
@@ -1027,8 +1005,11 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
10271005
index: ProposalIndex,
10281006
who: &T::AccountId,
10291007
) -> Result<bool, DispatchError> {
1030-
let voting = Self::voting(proposal).ok_or(Error::<T, I>::ProposalMissing)?;
1031-
ensure!(voting.index == index, Error::<T, I>::WrongIndex);
1008+
let voting = Self::voting(proposal).ok_or(Error::<T, I>::ProposalNotExists)?;
1009+
ensure!(
1010+
voting.index == index,
1011+
Error::<T, I>::IndexMismatchProposalHash
1012+
);
10321013

10331014
let position_yes = voting.ayes.iter().position(|a| a == who);
10341015
let position_no = voting.nays.iter().position(|a| a == who);

pallets/collective/src/tests.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ fn close_works() {
305305
proposal_weight,
306306
proposal_len
307307
),
308-
Error::<Test, Instance1>::TooEarly
308+
Error::<Test, Instance1>::TooEarlyToCloseProposal
309309
);
310310

311311
System::set_block_number(4);
@@ -376,7 +376,7 @@ fn proposal_weight_limit_works_on_approve() {
376376
proposal_weight - Weight::from_parts(100, 0),
377377
proposal_len
378378
),
379-
Error::<Test, Instance1>::WrongProposalWeight
379+
Error::<Test, Instance1>::ProposalWeightLessThanDispatchCallWeight
380380
);
381381
assert_ok!(Collective::close(
382382
RuntimeOrigin::signed(4),
@@ -861,7 +861,7 @@ fn limit_active_proposals() {
861861
TryInto::<BlockNumberFor<Test>>::try_into(3u64)
862862
.expect("convert u64 to block number.")
863863
),
864-
Error::<Test, Instance1>::TooManyProposals
864+
Error::<Test, Instance1>::TooManyActiveProposals
865865
);
866866
})
867867
}
@@ -890,19 +890,19 @@ fn correct_validate_and_get_proposal() {
890890
length,
891891
weight
892892
),
893-
Error::<Test, Instance1>::ProposalMissing
893+
Error::<Test, Instance1>::ProposalNotExists
894894
);
895895
assert_noop!(
896896
Collective::validate_and_get_proposal(&hash, length - 2, weight),
897-
Error::<Test, Instance1>::WrongProposalLength
897+
Error::<Test, Instance1>::ProposalLengthBoundLessThanProposalLength
898898
);
899899
assert_noop!(
900900
Collective::validate_and_get_proposal(
901901
&hash,
902902
length,
903903
weight - Weight::from_parts(10, 0)
904904
),
905-
Error::<Test, Instance1>::WrongProposalWeight
905+
Error::<Test, Instance1>::ProposalWeightLessThanDispatchCallWeight
906906
);
907907
let res = Collective::validate_and_get_proposal(&hash, length, weight);
908908
assert_ok!(res.clone());
@@ -964,7 +964,7 @@ fn motions_ignoring_bad_index_collective_vote_works() {
964964
));
965965
assert_noop!(
966966
Collective::vote(RuntimeOrigin::signed(2), hash, 1, true),
967-
Error::<Test, Instance1>::WrongIndex,
967+
Error::<Test, Instance1>::IndexMismatchProposalHash,
968968
);
969969
});
970970
}
@@ -1117,8 +1117,8 @@ fn motions_all_first_vote_free_works() {
11171117
);
11181118
assert_eq!(close_rval.unwrap().pays_fee, Pays::No);
11191119

1120-
// trying to close the proposal, which is already closed.
1121-
// Expecting error "ProposalMissing" with Pays::Yes
1120+
// Trying to close the proposal, which is already closed
1121+
// Error: "ProposalNotExists" with Pays::Yes.
11221122
let close_rval: DispatchResultWithPostInfo = Collective::close(
11231123
RuntimeOrigin::signed(2),
11241124
hash,
@@ -1454,7 +1454,7 @@ fn motion_with_no_votes_closes_with_disapproval() {
14541454
proposal_weight,
14551455
proposal_len
14561456
),
1457-
Error::<Test, Instance1>::TooEarly
1457+
Error::<Test, Instance1>::TooEarlyToCloseProposal
14581458
);
14591459

14601460
// Once the motion duration passes,
@@ -1508,7 +1508,7 @@ fn close_disapprove_does_not_care_about_weight_or_len() {
15081508
// It will not close with bad weight/len information
15091509
assert_noop!(
15101510
Collective::close(RuntimeOrigin::signed(2), hash, 0, Weight::zero(), 0),
1511-
Error::<Test, Instance1>::WrongProposalLength,
1511+
Error::<Test, Instance1>::ProposalLengthBoundLessThanProposalLength,
15121512
);
15131513
assert_noop!(
15141514
Collective::close(
@@ -1518,7 +1518,7 @@ fn close_disapprove_does_not_care_about_weight_or_len() {
15181518
Weight::zero(),
15191519
proposal_len
15201520
),
1521-
Error::<Test, Instance1>::WrongProposalWeight,
1521+
Error::<Test, Instance1>::ProposalWeightLessThanDispatchCallWeight,
15221522
);
15231523
// Now we make the proposal fail
15241524
assert_ok!(Collective::vote(RuntimeOrigin::signed(1), hash, 0, false));

pallets/commitments/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ pub mod pallet {
7777
#[pallet::error]
7878
pub enum Error<T> {
7979
/// Account passed too many additional fields to their commitment
80-
TooManyFields,
81-
/// Account isn't allow to make commitments to the chain
82-
CannotCommit,
83-
/// Account is trying to commit data too fast
84-
RateLimitExceeded,
80+
TooManyFieldsInCommitmentInfo,
81+
/// Account is not allow to make commitments to the chain
82+
AccountNotAllowedCommit,
83+
/// Account is trying to commit data too fast, rate limit exceeded
84+
CommitmentSetRateLimitExceeded,
8585
}
8686

8787
/// Identity data by account
@@ -126,20 +126,20 @@ pub mod pallet {
126126
let who = ensure_signed(origin)?;
127127
ensure!(
128128
T::CanCommit::can_commit(netuid, &who),
129-
Error::<T>::CannotCommit
129+
Error::<T>::AccountNotAllowedCommit
130130
);
131131

132132
let extra_fields = info.fields.len() as u32;
133133
ensure!(
134134
extra_fields <= T::MaxFields::get(),
135-
Error::<T>::TooManyFields
135+
Error::<T>::TooManyFieldsInCommitmentInfo
136136
);
137137

138138
let cur_block = <frame_system::Pallet<T>>::block_number();
139139
if let Some(last_commit) = <LastCommitment<T>>::get(netuid, &who) {
140140
ensure!(
141141
cur_block >= last_commit + T::RateLimit::get(),
142-
Error::<T>::RateLimitExceeded
142+
Error::<T>::CommitmentSetRateLimitExceeded
143143
);
144144
}
145145

pallets/registry/src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ pub mod pallet {
8282

8383
#[pallet::error]
8484
pub enum Error<T> {
85-
/// Account attempted to register an identity but doesn't meet the requirements.
85+
/// Account attempted to register an identity but does not meet the requirements.
8686
CannotRegister,
8787
/// Account passed too many additional fields to their identity
88-
TooManyFields,
88+
TooManyFieldsInIdentityInfo,
8989
/// Account doesn't have a registered identity
9090
NotRegistered,
9191
}
@@ -130,7 +130,7 @@ pub mod pallet {
130130
let extra_fields = info.additional.len() as u32;
131131
ensure!(
132132
extra_fields <= T::MaxAdditionalFields::get(),
133-
Error::<T>::TooManyFields
133+
Error::<T>::TooManyFieldsInIdentityInfo
134134
);
135135

136136
let fd = <BalanceOf<T>>::from(extra_fields) * T::FieldDeposit::get();
@@ -179,10 +179,6 @@ pub mod pallet {
179179
identified: T::AccountId,
180180
) -> DispatchResultWithPostInfo {
181181
let who = ensure_signed(origin)?;
182-
ensure!(
183-
T::CanRegister::can_register(&who, &identified),
184-
Error::<T>::CannotRegister
185-
);
186182

187183
let id = <IdentityOf<T>>::take(&identified).ok_or(Error::<T>::NotRegistered)?;
188184
let deposit = id.total_deposit();

0 commit comments

Comments
 (0)