mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 09:21:04 +00:00
Use decl_error in common runtime modules (#796)
* use decl_error in common runtime modules * Apply suggestions from code review Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
committed by
Bastian Köcher
parent
5621c18632
commit
f50d0c6505
@@ -25,7 +25,7 @@ use sp_runtime::traits::{
|
||||
use frame_support::weights::SimpleDispatchInfo;
|
||||
use codec::{Encode, Decode, Codec};
|
||||
use frame_support::{
|
||||
decl_module, decl_storage, decl_event, ensure, dispatch::DispatchResult,
|
||||
decl_module, decl_storage, decl_event, decl_error, ensure, dispatch::DispatchResult,
|
||||
traits::{Currency, ReservableCurrency, WithdrawReason, ExistenceRequirement, Get, Randomness},
|
||||
};
|
||||
use primitives::parachain::{
|
||||
@@ -224,8 +224,37 @@ decl_event!(
|
||||
}
|
||||
);
|
||||
|
||||
decl_error! {
|
||||
pub enum Error for Module<T: Trait> {
|
||||
/// This auction is already in progress.
|
||||
AuctionInProgress,
|
||||
/// The lease period is in the past.
|
||||
LeasePeriodInPast,
|
||||
/// The origin for this call must be a parachain.
|
||||
NotParaOrigin,
|
||||
/// The parachain ID is not onboarding.
|
||||
ParaNotOnboarding,
|
||||
/// The origin for this call must be the origin who registered the parachain.
|
||||
InvalidOrigin,
|
||||
/// Parachain is already registered.
|
||||
AlreadyRegistered,
|
||||
/// The code must correspond to the hash.
|
||||
InvalidCode,
|
||||
/// Deployment data has not been set for this parachain.
|
||||
UnsetDeployData,
|
||||
/// The bid must overlap all intersecting ranges.
|
||||
NonIntersectingRange,
|
||||
/// Not a current auction.
|
||||
NotCurrentAuction,
|
||||
/// Not an auction.
|
||||
NotAuction,
|
||||
}
|
||||
}
|
||||
|
||||
decl_module! {
|
||||
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
|
||||
type Error = Error<T>;
|
||||
|
||||
fn deposit_event() = default;
|
||||
|
||||
fn on_initialize(n: T::BlockNumber) {
|
||||
@@ -274,8 +303,8 @@ decl_module! {
|
||||
#[compact] lease_period_index: LeasePeriodOf<T>
|
||||
) {
|
||||
ensure_root(origin)?;
|
||||
ensure!(!Self::is_in_progress(), "auction already in progress");
|
||||
ensure!(lease_period_index >= Self::lease_period_index(), "lease period in past");
|
||||
ensure!(!Self::is_in_progress(), Error::<T>::AuctionInProgress);
|
||||
ensure!(lease_period_index >= Self::lease_period_index(), Error::<T>::LeasePeriodInPast);
|
||||
|
||||
// Bump the counter.
|
||||
let n = <AuctionCounter>::mutate(|n| { *n += 1; *n });
|
||||
@@ -340,7 +369,7 @@ decl_module! {
|
||||
) {
|
||||
let who = ensure_signed(origin)?;
|
||||
let para_id = <ParaId>::try_from_account(&who)
|
||||
.ok_or("account is not a parachain")?;
|
||||
.ok_or(Error::<T>::NotParaOrigin)?;
|
||||
let bidder = Bidder::Existing(para_id);
|
||||
Self::handle_bid(bidder, auction_index, first_slot, last_slot, amount)?;
|
||||
}
|
||||
@@ -355,7 +384,7 @@ decl_module! {
|
||||
let who = ensure_signed(origin)?;
|
||||
let dest = T::Lookup::lookup(dest)?;
|
||||
let para_id = <ParaId>::try_from_account(&who)
|
||||
.ok_or("not a parachain origin")?;
|
||||
.ok_or(Error::<T>::NotParaOrigin)?;
|
||||
<Offboarding<T>>::insert(para_id, dest);
|
||||
}
|
||||
|
||||
@@ -375,11 +404,11 @@ decl_module! {
|
||||
) {
|
||||
let who = ensure_signed(origin)?;
|
||||
let (starts, details) = <Onboarding<T>>::get(¶_id)
|
||||
.ok_or("parachain id not in onboarding")?;
|
||||
.ok_or(Error::<T>::ParaNotOnboarding)?;
|
||||
if let IncomingParachain::Unset(ref nb) = details {
|
||||
ensure!(nb.who == who && nb.sub == sub, "parachain not registered by origin");
|
||||
ensure!(nb.who == who && nb.sub == sub, Error::<T>::InvalidOrigin);
|
||||
} else {
|
||||
Err("already registered")?
|
||||
Err(Error::<T>::AlreadyRegistered)?
|
||||
}
|
||||
let item = (starts, IncomingParachain::Fixed{code_hash, initial_head_data});
|
||||
<Onboarding<T>>::insert(¶_id, item);
|
||||
@@ -400,9 +429,9 @@ decl_module! {
|
||||
#[weight = SimpleDispatchInfo::FixedNormal(5_000_000)]
|
||||
pub fn elaborate_deploy_data(_origin, #[compact] para_id: ParaId, code: Vec<u8>) -> DispatchResult {
|
||||
let (starts, details) = <Onboarding<T>>::get(¶_id)
|
||||
.ok_or("parachain id not in onboarding")?;
|
||||
.ok_or(Error::<T>::ParaNotOnboarding)?;
|
||||
if let IncomingParachain::Fixed{code_hash, initial_head_data} = details {
|
||||
ensure!(<T as system::Trait>::Hashing::hash(&code) == code_hash, "code not doesn't correspond to hash");
|
||||
ensure!(<T as system::Trait>::Hashing::hash(&code) == code_hash, Error::<T>::InvalidCode);
|
||||
if starts > Self::lease_period_index() {
|
||||
// Hasn't yet begun. Replace the on-boarding entry with the new information.
|
||||
let item = (starts, IncomingParachain::Deploy{code, initial_head_data});
|
||||
@@ -417,7 +446,7 @@ decl_module! {
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
Err("deploy data not yet fixed".into())
|
||||
Err(Error::<T>::UnsetDeployData)?
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -684,9 +713,9 @@ impl<T: Trait> Module<T> {
|
||||
amount: BalanceOf<T>
|
||||
) -> DispatchResult {
|
||||
// Bidding on latest auction.
|
||||
ensure!(auction_index == <AuctionCounter>::get(), "not current auction");
|
||||
ensure!(auction_index == <AuctionCounter>::get(), Error::<T>::NotCurrentAuction);
|
||||
// Assume it's actually an auction (this should never fail because of above).
|
||||
let (first_lease_period, _) = <AuctionInfo<T>>::get().ok_or("not an auction")?;
|
||||
let (first_lease_period, _) = <AuctionInfo<T>>::get().ok_or(Error::<T>::NotAuction)?;
|
||||
|
||||
// Our range.
|
||||
let range = SlotRange::new_bounded(first_lease_period, first_slot, last_slot)?;
|
||||
@@ -708,7 +737,7 @@ impl<T: Trait> Module<T> {
|
||||
.expect("array has SLOT_RANGE_COUNT items; index never reaches that value; qed")
|
||||
)
|
||||
)),
|
||||
"bidder winning non-intersecting range",
|
||||
Error::<T>::NonIntersectingRange,
|
||||
);
|
||||
|
||||
// Ok; we are the new winner of this range - reserve the additional amount and record.
|
||||
@@ -1205,7 +1234,7 @@ mod tests {
|
||||
assert_ok!(Slots::bid(Origin::signed(1), 0, 1, 2, 2, 1));
|
||||
assert_noop!(
|
||||
Slots::bid(Origin::signed(1), 0, 1, 3, 3, 1),
|
||||
"bidder winning non-intersecting range"
|
||||
Error::<Test>::NonIntersectingRange
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user