Introduce EnsureOrigin::try_successul_origin (#11558)

* Introduce `EnsureOrigin::try_successul_origin`

* Formatting

* Fixes

* Add Morph

* Fixes

* Formatting
This commit is contained in:
Gavin Wood
2022-05-31 19:12:07 +01:00
committed by GitHub
parent adf0773f9d
commit 9107ae41fd
6 changed files with 221 additions and 97 deletions
+16 -16
View File
@@ -66,6 +66,8 @@
#[cfg(feature = "std")]
use serde::Serialize;
#[cfg(feature = "runtime-benchmarks")]
use sp_runtime::traits::TrailingZeroInput;
use sp_runtime::{
generic,
traits::{
@@ -782,8 +784,8 @@ impl<O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>, Acco
}
#[cfg(feature = "runtime-benchmarks")]
fn successful_origin() -> O {
O::from(RawOrigin::Root)
fn try_successful_origin() -> Result<O, ()> {
Ok(O::from(RawOrigin::Root))
}
}
@@ -805,8 +807,8 @@ impl<
}
#[cfg(feature = "runtime-benchmarks")]
fn successful_origin() -> O {
O::from(RawOrigin::Root)
fn try_successful_origin() -> Result<O, ()> {
Ok(O::from(RawOrigin::Root))
}
}
@@ -823,11 +825,10 @@ impl<O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>, Acco
}
#[cfg(feature = "runtime-benchmarks")]
fn successful_origin() -> O {
fn try_successful_origin() -> Result<O, ()> {
let zero_account_id =
AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes())
.expect("infinite length input; no invalid inputs for type; qed");
O::from(RawOrigin::Signed(zero_account_id))
AccountId::decode(&mut TrailingZeroInput::zeroes()).map_err(|_| ())?;
Ok(O::from(RawOrigin::Signed(zero_account_id)))
}
}
@@ -847,16 +848,15 @@ impl<
}
#[cfg(feature = "runtime-benchmarks")]
fn successful_origin() -> O {
fn try_successful_origin() -> Result<O, ()> {
let zero_account_id =
AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes())
.expect("infinite length input; no invalid inputs for type; qed");
AccountId::decode(&mut TrailingZeroInput::zeroes()).map_err(|_| ())?;
let members = Who::sorted_members();
let first_member = match members.get(0) {
Some(account) => account.clone(),
None => zero_account_id,
};
O::from(RawOrigin::Signed(first_member))
Ok(O::from(RawOrigin::Signed(first_member)))
}
}
@@ -873,8 +873,8 @@ impl<O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>, Acco
}
#[cfg(feature = "runtime-benchmarks")]
fn successful_origin() -> O {
O::from(RawOrigin::None)
fn try_successful_origin() -> Result<O, ()> {
Ok(O::from(RawOrigin::None))
}
}
@@ -886,8 +886,8 @@ impl<O, T> EnsureOrigin<O> for EnsureNever<T> {
}
#[cfg(feature = "runtime-benchmarks")]
fn successful_origin() -> O {
unimplemented!()
fn try_successful_origin() -> Result<O, ()> {
Err(())
}
}