Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit f57c644

Browse files
shawntabrizibkchr
andauthored
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>
1 parent e19dfaa commit f57c644

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

frame/system/src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ impl<O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>, Acco
820820
}
821821

822822
pub struct EnsureSigned<AccountId>(sp_std::marker::PhantomData<AccountId>);
823-
impl<O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>, AccountId: Default>
823+
impl<O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>, AccountId: Decode>
824824
EnsureOrigin<O> for EnsureSigned<AccountId>
825825
{
826826
type Success = AccountId;
@@ -833,15 +833,18 @@ impl<O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>, Acco
833833

834834
#[cfg(feature = "runtime-benchmarks")]
835835
fn successful_origin() -> O {
836-
O::from(RawOrigin::Signed(Default::default()))
836+
let zero_account_id =
837+
AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes())
838+
.expect("infinite length input; no invalid inputs for type; qed");
839+
O::from(RawOrigin::Signed(zero_account_id))
837840
}
838841
}
839842

840843
pub struct EnsureSignedBy<Who, AccountId>(sp_std::marker::PhantomData<(Who, AccountId)>);
841844
impl<
842845
O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>,
843846
Who: SortedMembers<AccountId>,
844-
AccountId: PartialEq + Clone + Ord + Default,
847+
AccountId: PartialEq + Clone + Ord + Decode,
845848
> EnsureOrigin<O> for EnsureSignedBy<Who, AccountId>
846849
{
847850
type Success = AccountId;
@@ -854,10 +857,13 @@ impl<
854857

855858
#[cfg(feature = "runtime-benchmarks")]
856859
fn successful_origin() -> O {
860+
let zero_account_id =
861+
AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes())
862+
.expect("infinite length input; no invalid inputs for type; qed");
857863
let members = Who::sorted_members();
858864
let first_member = match members.get(0) {
859865
Some(account) => account.clone(),
860-
None => Default::default(),
866+
None => zero_account_id,
861867
};
862868
O::from(RawOrigin::Signed(first_member.clone()))
863869
}

frame/system/src/tests.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,26 @@ fn runtime_updated_digest_emitted_when_heap_pages_changed() {
486486
assert_runtime_updated_digest(1);
487487
});
488488
}
489+
490+
#[test]
491+
fn ensure_signed_stuff_works() {
492+
struct Members;
493+
impl SortedMembers<u64> for Members {
494+
fn sorted_members() -> Vec<u64> {
495+
(0..10).collect()
496+
}
497+
}
498+
499+
let signed_origin = Origin::signed(0u64);
500+
assert_ok!(EnsureSigned::try_origin(signed_origin.clone()));
501+
assert_ok!(EnsureSignedBy::<Members, _>::try_origin(signed_origin));
502+
503+
#[cfg(feature = "runtime-benchmarks")]
504+
{
505+
let successful_origin: Origin = EnsureSigned::successful_origin();
506+
assert_ok!(EnsureSigned::try_origin(successful_origin));
507+
508+
let successful_origin: Origin = EnsureSignedBy::<Members, _>::successful_origin();
509+
assert_ok!(EnsureSignedBy::<Members, _>::try_origin(successful_origin));
510+
}
511+
}

0 commit comments

Comments
 (0)