Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Remove panics in dot pallet (paritytech#330)
Browse files Browse the repository at this point in the history
* Don't panic

* Add more comments
  • Loading branch information
vgeddes authored Apr 1, 2021
1 parent 7bdca4f commit 49a5b17
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions parachain/pallets/dot-app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ decl_event!(

decl_error! {
pub enum Error for Module<T: Config> {

/// Illegal conversion between native and wrapped DOT.
///
/// In practice, this error should never occur under the conditions
/// we've tested. If however the bridge or the peer Ethereum contract
/// is exploited, then all bets are off.
Overflow
}
}

Expand All @@ -98,6 +103,8 @@ decl_module! {

fn deposit_event() = default;

// Verify that `T::Decimals` is 10 (DOT), or 12 (KSM) to guarantee
// safe conversions between native and wrapped DOT.
fn integrity_test() {
sp_io::TestExternalities::new_empty().execute_with(|| {
let allowed_decimals: &[u32] = &[10, 12];
Expand All @@ -115,10 +122,7 @@ decl_module! {

T::Currency::transfer(&who, &Self::account_id(), amount, AllowDeath)?;

let amount_wrapped = match wrap::<T>(amount, T::Decimals::get()) {
Some(value) => value,
None => panic!("Runtime is misconfigured"),
};
let amount_wrapped = wrap::<T>(amount, T::Decimals::get()).ok_or(Error::<T>::Overflow)?;

let message = OutboundPayload {
sender: who.clone(),
Expand All @@ -139,10 +143,7 @@ decl_module! {
return Err(DispatchError::BadOrigin.into());
}

let amount_unwrapped = match unwrap::<T>(amount, T::Decimals::get()) {
Some(value) => value,
None => panic!("Runtime is misconfigured"),
};
let amount_unwrapped = unwrap::<T>(amount, T::Decimals::get()).ok_or(Error::<T>::Overflow)?;

let recipient = T::Lookup::lookup(recipient)?;
T::Currency::transfer(&Self::account_id(), &recipient, amount_unwrapped, KeepAlive)?;
Expand Down

0 comments on commit 49a5b17

Please sign in to comment.