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

Replace T::AccountId with <T::Lookup as StaticLookup>::Source #11670

Merged
merged 23 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions frame/bags-list/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ frame_benchmarking::benchmarks! {
let dest_head: T::AccountId = account("dest_head", 0, 0);
assert_ok!(List::<T, _>::insert(dest_head.clone(), dest_bag_thresh));

let origin_middle_lookup = T::Lookup::unlookup(origin_middle.clone());

// the bags are in the expected state after initial setup.
assert_eq!(
List::<T, _>::get_bags(),
Expand All @@ -69,7 +71,7 @@ frame_benchmarking::benchmarks! {
let caller = whitelisted_caller();
// update the weight of `origin_middle` to guarantee it will be rebagged into the destination.
T::ScoreProvider::set_score_of(&origin_middle, dest_bag_thresh);
}: rebag(SystemOrigin::Signed(caller), origin_middle.clone())
}: rebag(SystemOrigin::Signed(caller), origin_middle_lookup.clone())
verify {
// check the bags have updated as expected.
assert_eq!(
Expand Down Expand Up @@ -114,6 +116,8 @@ frame_benchmarking::benchmarks! {
let dest_head: T::AccountId = account("dest_head", 0, 0);
assert_ok!(List::<T, _>::insert(dest_head.clone(), dest_bag_thresh));

let origin_tail_lookup = T::Lookup::unlookup(origin_tail.clone());

// the bags are in the expected state after initial setup.
assert_eq!(
List::<T, _>::get_bags(),
Expand All @@ -126,7 +130,7 @@ frame_benchmarking::benchmarks! {
let caller = whitelisted_caller();
// update the weight of `origin_tail` to guarantee it will be rebagged into the destination.
T::ScoreProvider::set_score_of(&origin_tail, dest_bag_thresh);
}: rebag(SystemOrigin::Signed(caller), origin_tail.clone())
}: rebag(SystemOrigin::Signed(caller), origin_tail_lookup.clone())
verify {
// check the bags have updated as expected.
assert_eq!(
Expand Down Expand Up @@ -166,13 +170,15 @@ frame_benchmarking::benchmarks! {
T::ScoreProvider::set_score_of(&lighter, bag_thresh - One::one());
T::ScoreProvider::set_score_of(&heavier, bag_thresh);

let lighter_lookup = T::Lookup::unlookup(lighter.clone());

assert_eq!(
List::<T, _>::iter().map(|n| n.id().clone()).collect::<Vec<_>>(),
vec![lighter.clone(), heavier_prev.clone(), heavier.clone(), heavier_next.clone()]
);

whitelist_account!(heavier);
}: _(SystemOrigin::Signed(heavier.clone()), lighter.clone())
}: _(SystemOrigin::Signed(heavier.clone()), lighter_lookup.clone())
verify {
assert_eq!(
List::<T, _>::iter().map(|n| n.id().clone()).collect::<Vec<_>>(),
Expand Down
14 changes: 11 additions & 3 deletions frame/bags-list/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
use codec::FullCodec;
use frame_election_provider_support::{ScoreProvider, SortedListProvider};
use frame_system::ensure_signed;
use sp_runtime::traits::{AtLeast32BitUnsigned, Bounded};
use sp_runtime::traits::{AtLeast32BitUnsigned, Bounded, StaticLookup};
use sp_std::prelude::*;

#[cfg(any(feature = "runtime-benchmarks", test))]
Expand Down Expand Up @@ -222,8 +222,12 @@ pub mod pallet {
///
/// If `dislocated` does not exists, it returns an error.
#[pallet::weight(T::WeightInfo::rebag_non_terminal().max(T::WeightInfo::rebag_terminal()))]
pub fn rebag(origin: OriginFor<T>, dislocated: T::AccountId) -> DispatchResult {
pub fn rebag(
origin: OriginFor<T>,
dislocated: <T::Lookup as StaticLookup>::Source,
) -> DispatchResult {
ensure_signed(origin)?;
let dislocated = T::Lookup::lookup(dislocated)?;
let current_score = T::ScoreProvider::score(&dislocated);
let _ = Pallet::<T, I>::do_rebag(&dislocated, current_score)
.map_err::<Error<T, I>, _>(Into::into)?;
Expand All @@ -239,8 +243,12 @@ pub mod pallet {
/// - both nodes are within the same bag,
/// - and `origin` has a greater `Score` than `lighter`.
#[pallet::weight(T::WeightInfo::put_in_front_of())]
pub fn put_in_front_of(origin: OriginFor<T>, lighter: T::AccountId) -> DispatchResult {
pub fn put_in_front_of(
origin: OriginFor<T>,
lighter: <T::Lookup as StaticLookup>::Source,
) -> DispatchResult {
let heavier = ensure_signed(origin)?;
let lighter = T::Lookup::lookup(lighter)?;
List::<T, I>::put_in_front_of(&lighter, &heavier)
.map_err::<Error<T, I>, _>(Into::into)
.map_err::<DispatchError, _>(Into::into)
Expand Down
12 changes: 8 additions & 4 deletions frame/conviction-voting/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ benchmarks! {
remove_other_vote {
let caller = funded_account::<T>("caller", 0);
let voter = funded_account::<T>("caller", 0);
let voter_lookup = T::Lookup::unlookup(voter.clone());
whitelist_account!(caller);
let old_account_vote = account_vote::<T>(100u32.into());

Expand All @@ -166,7 +167,7 @@ benchmarks! {

let index = polls[0];
assert!(T::Polls::end_ongoing(index, false).is_ok());
}: _(RawOrigin::Signed(caller.clone()), voter.clone(), class.clone(), index)
}: _(RawOrigin::Signed(caller.clone()), voter_lookup, class.clone(), index)
verify {
assert_matches!(
VotingFor::<T>::get(&voter, &class),
Expand All @@ -181,6 +182,7 @@ benchmarks! {
let class = T::Polls::max_ongoing().0;
let polls = &all_polls[&class];
let voter = funded_account::<T>("voter", 0);
let voter_lookup = T::Lookup::unlookup(voter.clone());
let caller = funded_account::<T>("caller", 0);
whitelist_account!(caller);

Expand All @@ -196,7 +198,7 @@ benchmarks! {
Voting::Casting(Casting { votes, .. }) if votes.len() == r as usize
);

}: _(RawOrigin::Signed(caller.clone()), class.clone(), voter, Conviction::Locked1x, delegated_balance)
}: _(RawOrigin::Signed(caller.clone()), class.clone(), voter_lookup, Conviction::Locked1x, delegated_balance)
verify {
assert_matches!(VotingFor::<T>::get(&caller, &class), Voting::Delegating(_));
}
Expand All @@ -208,6 +210,7 @@ benchmarks! {
let class = T::Polls::max_ongoing().0;
let polls = &all_polls[&class];
let voter = funded_account::<T>("voter", 0);
let voter_lookup = T::Lookup::unlookup(voter.clone());
let caller = funded_account::<T>("caller", 0);
whitelist_account!(caller);

Expand All @@ -217,7 +220,7 @@ benchmarks! {
ConvictionVoting::<T>::delegate(
RawOrigin::Signed(caller.clone()).into(),
class.clone(),
voter.clone(),
voter_lookup,
Conviction::Locked1x,
delegated_balance,
)?;
Expand All @@ -238,6 +241,7 @@ benchmarks! {

unlock {
let caller = funded_account::<T>("caller", 0);
let caller_lookup = T::Lookup::unlookup(caller.clone());
whitelist_account!(caller);
let normal_account_vote = account_vote::<T>(T::Currency::free_balance(&caller) - 100u32.into());
let big_account_vote = account_vote::<T>(T::Currency::free_balance(&caller));
Expand Down Expand Up @@ -265,7 +269,7 @@ benchmarks! {
ConvictionVoting::<T>::remove_vote(RawOrigin::Signed(caller.clone()).into(), Some(class.clone()), polls[0])?;

// We can now unlock on `class` from 200 to 100...
}: _(RawOrigin::Signed(caller.clone()), class, caller.clone())
}: _(RawOrigin::Signed(caller.clone()), class, caller_lookup)
verify {
assert_eq!(orig_usable, <T::Currency as fungible::Inspect<T::AccountId>>::reducible_balance(&caller, false));
}
Expand Down
11 changes: 7 additions & 4 deletions frame/conviction-voting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use frame_support::{
},
};
use sp_runtime::{
traits::{AtLeast32BitUnsigned, Saturating, Zero},
traits::{AtLeast32BitUnsigned, Saturating, StaticLookup, Zero},
ArithmeticError, Perbill,
};
use sp_std::prelude::*;
Expand Down Expand Up @@ -245,11 +245,12 @@ pub mod pallet {
pub fn delegate(
origin: OriginFor<T>,
class: ClassOf<T, I>,
to: T::AccountId,
to: <T::Lookup as StaticLookup>::Source,
conviction: Conviction,
balance: BalanceOf<T, I>,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
let to = T::Lookup::lookup(to)?;
let votes = Self::try_delegate(who, class, to, conviction, balance)?;

Ok(Some(T::WeightInfo::delegate(votes)).into())
Expand Down Expand Up @@ -294,9 +295,10 @@ pub mod pallet {
pub fn unlock(
origin: OriginFor<T>,
class: ClassOf<T, I>,
target: T::AccountId,
target: <T::Lookup as StaticLookup>::Source,
) -> DispatchResult {
ensure_signed(origin)?;
let target = T::Lookup::lookup(target)?;
Self::update_lock(&class, &target);
Ok(())
}
Expand Down Expand Up @@ -359,11 +361,12 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::remove_other_vote())]
pub fn remove_other_vote(
origin: OriginFor<T>,
target: T::AccountId,
target: <T::Lookup as StaticLookup>::Source,
class: ClassOf<T, I>,
index: PollIndexOf<T, I>,
) -> DispatchResult {
let who = ensure_signed(origin)?;
let target = T::Lookup::lookup(target)?;
let scope = if target == who { UnvoteScope::Any } else { UnvoteScope::OnlyExpired };
Self::try_remove_vote(&target, index, Some(class), scope)?;
Ok(())
Expand Down
18 changes: 12 additions & 6 deletions frame/democracy/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,9 +481,10 @@ benchmarks! {
let caller = funded_account::<T>("caller", 0);
// Caller will initially delegate to `old_delegate`
let old_delegate: T::AccountId = funded_account::<T>("old_delegate", r);
let old_delegate_lookup = T::Lookup::unlookup(old_delegate.clone());
Democracy::<T>::delegate(
RawOrigin::Signed(caller.clone()).into(),
old_delegate.clone(),
old_delegate_lookup,
Conviction::Locked1x,
delegated_balance,
)?;
Expand All @@ -495,6 +496,7 @@ benchmarks! {
assert_eq!(balance, delegated_balance, "delegation balance didn't work");
// Caller will now switch to `new_delegate`
let new_delegate: T::AccountId = funded_account::<T>("new_delegate", r);
let new_delegate_lookup = T::Lookup::unlookup(new_delegate.clone());
let account_vote = account_vote::<T>(initial_balance);
// We need to create existing direct votes for the `new_delegate`
for i in 0..r {
Expand All @@ -507,7 +509,7 @@ benchmarks! {
};
assert_eq!(votes.len(), r as usize, "Votes were not recorded.");
whitelist_account!(caller);
}: _(RawOrigin::Signed(caller.clone()), new_delegate.clone(), Conviction::Locked1x, delegated_balance)
}: _(RawOrigin::Signed(caller.clone()), new_delegate_lookup, Conviction::Locked1x, delegated_balance)
verify {
let (target, balance) = match VotingOf::<T>::get(&caller) {
Voting::Delegating { target, balance, .. } => (target, balance),
Expand All @@ -531,9 +533,10 @@ benchmarks! {
let caller = funded_account::<T>("caller", 0);
// Caller will delegate
let the_delegate: T::AccountId = funded_account::<T>("delegate", r);
let the_delegate_lookup = T::Lookup::unlookup(the_delegate.clone());
Democracy::<T>::delegate(
RawOrigin::Signed(caller.clone()).into(),
the_delegate.clone(),
the_delegate_lookup,
Conviction::Locked1x,
delegated_balance,
)?;
Expand Down Expand Up @@ -640,6 +643,7 @@ benchmarks! {
let r in 1 .. MAX_REFERENDUMS;

let locker = funded_account::<T>("locker", 0);
let locker_lookup = T::Lookup::unlookup(locker.clone());
// Populate votes so things are locked
let base_balance: BalanceOf<T> = 100u32.into();
let small_vote = account_vote::<T>(base_balance);
Expand All @@ -652,7 +656,7 @@ benchmarks! {

let caller = funded_account::<T>("caller", 0);
whitelist_account!(caller);
}: unlock(RawOrigin::Signed(caller), locker.clone())
}: unlock(RawOrigin::Signed(caller), locker_lookup)
verify {
// Note that we may want to add a `get_lock` api to actually verify
let voting = VotingOf::<T>::get(&locker);
Expand All @@ -664,6 +668,7 @@ benchmarks! {
let r in 1 .. MAX_REFERENDUMS;

let locker = funded_account::<T>("locker", 0);
let locker_lookup = T::Lookup::unlookup(locker.clone());
// Populate votes so things are locked
let base_balance: BalanceOf<T> = 100u32.into();
let small_vote = account_vote::<T>(base_balance);
Expand All @@ -690,7 +695,7 @@ benchmarks! {

let caller = funded_account::<T>("caller", 0);
whitelist_account!(caller);
}: unlock(RawOrigin::Signed(caller), locker.clone())
}: unlock(RawOrigin::Signed(caller), locker_lookup)
verify {
let votes = match VotingOf::<T>::get(&locker) {
Voting::Direct { votes, .. } => votes,
Expand Down Expand Up @@ -736,6 +741,7 @@ benchmarks! {
let r in 1 .. MAX_REFERENDUMS;

let caller = funded_account::<T>("caller", r);
let caller_lookup = T::Lookup::unlookup(caller.clone());
let account_vote = account_vote::<T>(100u32.into());

for i in 0 .. r {
Expand All @@ -751,7 +757,7 @@ benchmarks! {

let referendum_index = r - 1;
whitelist_account!(caller);
}: _(RawOrigin::Signed(caller.clone()), caller.clone(), referendum_index)
}: _(RawOrigin::Signed(caller.clone()), caller_lookup, referendum_index)
verify {
let votes = match VotingOf::<T>::get(&caller) {
Voting::Direct { votes, .. } => votes,
Expand Down
14 changes: 10 additions & 4 deletions frame/democracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ use frame_support::{
};
use scale_info::TypeInfo;
use sp_runtime::{
traits::{Bounded, Dispatchable, Hash, Saturating, Zero},
traits::{Bounded, Dispatchable, Hash, Saturating, StaticLookup, Zero},
ArithmeticError, DispatchError, DispatchResult, RuntimeDebug,
};
use sp_std::prelude::*;
Expand Down Expand Up @@ -941,11 +941,12 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::delegate(T::MaxVotes::get()))]
pub fn delegate(
origin: OriginFor<T>,
to: T::AccountId,
to: <T::Lookup as StaticLookup>::Source,
conviction: Conviction,
balance: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
let to = T::Lookup::lookup(to)?;
let votes = Self::try_delegate(who, to, conviction, balance)?;

Ok(Some(T::WeightInfo::delegate(votes)).into())
Expand Down Expand Up @@ -1124,8 +1125,12 @@ pub mod pallet {
T::WeightInfo::unlock_set(T::MaxVotes::get())
.max(T::WeightInfo::unlock_remove(T::MaxVotes::get()))
)]
pub fn unlock(origin: OriginFor<T>, target: T::AccountId) -> DispatchResult {
pub fn unlock(
origin: OriginFor<T>,
target: <T::Lookup as StaticLookup>::Source,
) -> DispatchResult {
ensure_signed(origin)?;
let target = T::Lookup::lookup(target)?;
Self::update_lock(&target);
Ok(())
}
Expand Down Expand Up @@ -1181,10 +1186,11 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::remove_other_vote(T::MaxVotes::get()))]
pub fn remove_other_vote(
origin: OriginFor<T>,
target: T::AccountId,
target: <T::Lookup as StaticLookup>::Source,
index: ReferendumIndex,
) -> DispatchResult {
let who = ensure_signed(origin)?;
let target = T::Lookup::lookup(target)?;
let scope = if target == who { UnvoteScope::Any } else { UnvoteScope::OnlyExpired };
Self::try_remove_vote(&target, index, scope)?;
Ok(())
Expand Down
Loading