Fix Places where AccountId Uses Default (#10556)

* fix places where accountid is default

* Update frame/system/src/lib.rs

* fmt

* add simple test

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Shawn Tabrizi
2021-12-28 19:42:58 -04:00
committed by GitHub
parent 409d810089
commit ea50ecac4a
2 changed files with 33 additions and 4 deletions
+10 -4
View File
@@ -820,7 +820,7 @@ impl<O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>, Acco
}
pub struct EnsureSigned<AccountId>(sp_std::marker::PhantomData<AccountId>);
impl<O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>, AccountId: Default>
impl<O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>, AccountId: Decode>
EnsureOrigin<O> for EnsureSigned<AccountId>
{
type Success = AccountId;
@@ -833,7 +833,10 @@ impl<O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>, Acco
#[cfg(feature = "runtime-benchmarks")]
fn successful_origin() -> O {
O::from(RawOrigin::Signed(Default::default()))
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))
}
}
@@ -841,7 +844,7 @@ pub struct EnsureSignedBy<Who, AccountId>(sp_std::marker::PhantomData<(Who, Acco
impl<
O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>,
Who: SortedMembers<AccountId>,
AccountId: PartialEq + Clone + Ord + Default,
AccountId: PartialEq + Clone + Ord + Decode,
> EnsureOrigin<O> for EnsureSignedBy<Who, AccountId>
{
type Success = AccountId;
@@ -854,10 +857,13 @@ impl<
#[cfg(feature = "runtime-benchmarks")]
fn successful_origin() -> O {
let zero_account_id =
AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes())
.expect("infinite length input; no invalid inputs for type; qed");
let members = Who::sorted_members();
let first_member = match members.get(0) {
Some(account) => account.clone(),
None => Default::default(),
None => zero_account_id,
};
O::from(RawOrigin::Signed(first_member.clone()))
}
+23
View File
@@ -486,3 +486,26 @@ fn runtime_updated_digest_emitted_when_heap_pages_changed() {
assert_runtime_updated_digest(1);
});
}
#[test]
fn ensure_signed_stuff_works() {
struct Members;
impl SortedMembers<u64> for Members {
fn sorted_members() -> Vec<u64> {
(0..10).collect()
}
}
let signed_origin = Origin::signed(0u64);
assert_ok!(EnsureSigned::try_origin(signed_origin.clone()));
assert_ok!(EnsureSignedBy::<Members, _>::try_origin(signed_origin));
#[cfg(feature = "runtime-benchmarks")]
{
let successful_origin: Origin = EnsureSigned::successful_origin();
assert_ok!(EnsureSigned::try_origin(successful_origin));
let successful_origin: Origin = EnsureSignedBy::<Members, _>::successful_origin();
assert_ok!(EnsureSignedBy::<Members, _>::try_origin(successful_origin));
}
}