Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conviction pallet uses fungible trait #1821

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f2e2deb
make it work
PieWol Oct 8, 2023
b6fb31f
traits at the right places
PieWol Oct 9, 2023
ab3dd13
propagate holding results
PieWol Oct 9, 2023
15ce5a1
adjust test config
PieWol Oct 9, 2023
a82c077
fix tests
PieWol Oct 9, 2023
cb5a46e
lock -> hold renaming
PieWol Oct 9, 2023
faf0769
Merge branch 'master' into conviction-use-fungible
PieWol Oct 9, 2023
ea9f072
move lib to freezes
PieWol Nov 20, 2023
302fb17
revamp to freezes
PieWol Nov 22, 2023
dff9968
Merge branch 'master' into conviction-use-fungible
PieWol Nov 22, 2023
b433650
Merge branch 'master' into conviction-use-fungible
PieWol Jan 1, 2024
6d6bf7b
make runtimes compile
PieWol Jan 4, 2024
093e605
Merge branch 'conviction-use-fungible' of github.com:PieWol/polkadot-…
PieWol Jan 4, 2024
fc656ae
add RFR to node runtime
PieWol Jan 4, 2024
cd2434d
Merge branch 'master' into conviction-use-fungible
PieWol Jan 4, 2024
d82af95
remove renaming in code
PieWol Jan 4, 2024
4cbadcf
conviction.rs comment fix
PieWol Jan 4, 2024
7859ba1
tests.rs renaming removed
PieWol Jan 4, 2024
78aeebb
further renaming removal
PieWol Jan 4, 2024
28f808b
spelling
PieWol Jan 4, 2024
2ca7f1a
Merge branch 'master' into conviction-use-fungible
PieWol Jan 4, 2024
1e4fcb9
how to fund?
PieWol Jan 4, 2024
37bcb43
compile benchmarks
PieWol Jan 9, 2024
6119321
fix clippy
PieWol Jan 10, 2024
f4871a5
fix benchmarks
PieWol Feb 25, 2024
782c0f0
Merge remote-tracking branch 'origin/master' into conviction-use-fung…
PieWol Feb 25, 2024
e4fdcb2
clean up
PieWol Feb 25, 2024
d1bcae2
changes integrity test?
PieWol Feb 25, 2024
620f7e7
thaw previous freezes
PieWol Feb 25, 2024
9273e25
fix clippy
PieWol Feb 26, 2024
b4fb279
integrity fix?
PieWol Feb 29, 2024
9581a7c
Merge branch 'paritytech:master' into conviction-use-fungible
PieWol Feb 29, 2024
82c54de
use mint_into
PieWol Mar 15, 2024
acc8806
issue with maxVotes?
PieWol Mar 15, 2024
bb9d555
clueless
PieWol Mar 15, 2024
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
1 change: 1 addition & 0 deletions polkadot/runtime/rococo/src/governance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl pallet_conviction_voting::Config for Runtime {
type WeightInfo = weights::pallet_conviction_voting::WeightInfo<Self>;
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type RuntimeFreezeReason = RuntimeFreezeReason;
type VoteLockingPeriod = VoteLockingPeriod;
type MaxVotes = ConstU32<512>;
type MaxTurnout =
Expand Down
2 changes: 1 addition & 1 deletion polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ impl pallet_balances::Config for Runtime {
type MaxReserves = MaxReserves;
type ReserveIdentifier = [u8; 8];
type WeightInfo = weights::pallet_balances_balances::WeightInfo<Runtime>;
type FreezeIdentifier = ();
type FreezeIdentifier = RuntimeFreezeReason;
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type MaxFreezes = ConstU32<1>;
Expand Down
3 changes: 2 additions & 1 deletion polkadot/runtime/westend/src/governance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ parameter_types! {
impl pallet_conviction_voting::Config for Runtime {
type WeightInfo = weights::pallet_conviction_voting::WeightInfo<Self>;
type RuntimeEvent = RuntimeEvent;
type RuntimeFreezeReason = RuntimeFreezeReason;
type Currency = Balances;
type VoteLockingPeriod = VoteLockingPeriod;
type MaxVotes = ConstU32<512>;
type MaxVotes = ConstU32<3>;
type MaxTurnout =
frame_support::traits::tokens::currency::ActiveIssuanceOf<Balances, Self::AccountId>;
type Polls = Referenda;
Expand Down
2 changes: 1 addition & 1 deletion polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ impl pallet_balances::Config for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = RuntimeFreezeReason;
type MaxFreezes = ConstU32<1>;
type MaxFreezes = ConstU32<2>;
}

parameter_types! {
Expand Down
3 changes: 2 additions & 1 deletion substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ impl pallet_balances::Config for Runtime {
type AccountStore = frame_system::Pallet<Runtime>;
type WeightInfo = pallet_balances::weights::SubstrateWeight<Runtime>;
type FreezeIdentifier = RuntimeFreezeReason;
type MaxFreezes = ConstU32<1>;
type MaxFreezes = ConstU32<2>;
}

parameter_types! {
Expand Down Expand Up @@ -918,6 +918,7 @@ parameter_types! {
impl pallet_conviction_voting::Config for Runtime {
type WeightInfo = pallet_conviction_voting::weights::SubstrateWeight<Self>;
type RuntimeEvent = RuntimeEvent;
type RuntimeFreezeReason = RuntimeFreezeReason;
type Currency = Balances;
type VoteLockingPeriod = VoteLockingPeriod;
type MaxVotes = ConstU32<512>;
Expand Down
9 changes: 4 additions & 5 deletions substrate/frame/conviction-voting/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ use frame_support::{
traits::{
fungible,
tokens::{Fortitude::Polite, Preservation::Expendable},
Currency, Get,
Get,
},
};
use sp_runtime::traits::Bounded;
use sp_std::collections::btree_map::BTreeMap;

use crate::Pallet as ConvictionVoting;
Expand All @@ -55,7 +54,7 @@ fn fill_voting<T: Config<I>, I: 'static>(

fn funded_account<T: Config<I>, I: 'static>(name: &'static str, index: u32) -> T::AccountId {
let caller: T::AccountId = account(name, index, SEED);
T::Currency::make_free_balance_be(&caller, BalanceOf::<T, I>::max_value());
let _ = T::Currency::mint_into(&caller, 1_000_000u32.into());
caller
}

Expand Down Expand Up @@ -248,8 +247,8 @@ benchmarks_instance_pallet! {
let caller = funded_account::<T, I>("caller", 0);
let caller_lookup = T::Lookup::unlookup(caller.clone());
whitelist_account!(caller);
let normal_account_vote = account_vote::<T, I>(T::Currency::free_balance(&caller) - 100u32.into());
let big_account_vote = account_vote::<T, I>(T::Currency::free_balance(&caller));
let normal_account_vote = account_vote::<T, I>(T::Currency::balance(&caller) - 100u32.into());
let big_account_vote = account_vote::<T, I>(T::Currency::balance(&caller));

// Fill everything up to the max by filling all classes with votes and voting on them all.
let (class, all_polls) = fill_voting::<T, I>();
Expand Down
83 changes: 48 additions & 35 deletions substrate/frame/conviction-voting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@
#![recursion_limit = "256"]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "runtime-benchmarks")]
use frame_support::traits::fungible::Mutate;

use frame_support::{
dispatch::DispatchResult,
ensure,
traits::{
fungible, Currency, Get, LockIdentifier, LockableCurrency, PollStatus, Polling,
ReservableCurrency, WithdrawReasons,
fungible::{Inspect, InspectFreeze, MutateFreeze},
Get, PollStatus, Polling,
},
};

use frame_system::pallet_prelude::BlockNumberFor;
use sp_runtime::{
traits::{AtLeast32BitUnsigned, Saturating, StaticLookup, Zero},
Expand All @@ -61,11 +65,9 @@ mod tests;
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;

const CONVICTION_VOTING_ID: LockIdentifier = *b"pyconvot";

type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
type BalanceOf<T, I = ()> =
<<T as Config<I>>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
<<T as Config<I>>::Currency as Inspect<<T as frame_system::Config>::AccountId>>::Balance;
type VotingOf<T, I = ()> = Voting<
BalanceOf<T, I>,
<T as frame_system::Config>::AccountId,
Expand Down Expand Up @@ -104,12 +106,19 @@ pub mod pallet {
// System level stuff.
type RuntimeEvent: From<Event<Self, I>>
+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// The overarching freeze reason.
type RuntimeFreezeReason: From<FreezeReason>;
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
/// Currency type with which voting happens.
type Currency: ReservableCurrency<Self::AccountId>
+ LockableCurrency<Self::AccountId, Moment = BlockNumberFor<Self>>
+ fungible::Inspect<Self::AccountId>;

#[cfg(not(feature = "runtime-benchmarks"))]
type Currency: MutateFreeze<Self::AccountId, Id = Self::RuntimeFreezeReason>
+ InspectFreeze<Self::AccountId>;

#[cfg(feature = "runtime-benchmarks")]
type Currency: MutateFreeze<Self::AccountId, Id = Self::RuntimeFreezeReason>
+ InspectFreeze<Self::AccountId>
+ Mutate<Self::AccountId>;

/// The implementation of the logic which conducts polls.
type Polls: Polling<
Expand Down Expand Up @@ -201,6 +210,14 @@ pub mod pallet {
BadClass,
}

/// The reasons for the pallet placing locks on funds.
#[pallet::composite_enum]
pub enum FreezeReason {
/// The Pallet locks funds for voting reasons.
#[codec(index = 0)]
ConvictionVoting,
}

#[pallet::call]
impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Vote in a poll. If `vote.is_aye()`, the vote is to enact the proposal;
Expand Down Expand Up @@ -309,7 +326,7 @@ pub mod pallet {
) -> DispatchResult {
ensure_signed(origin)?;
let target = T::Lookup::lookup(target)?;
Self::update_lock(&class, &target);
Self::update_lock(&class, &target)?;
Ok(())
}

Expand All @@ -329,9 +346,9 @@ pub mod pallet {
/// - it finished corresponding to the vote of the account, and
/// - the account made a standard vote with conviction, and
/// - the lock period of the conviction is not over
/// ...then the lock will be aggregated into the overall account's lock, which may involve
/// *overlocking* (where the two locks are combined into a single lock that is the maximum
/// of both the amount locked and the time is it locked for).
/// ...then the lock will be aggregated into the overall account's lock for this pallets
/// reason, which may involve the two locks to be combined into a single lock that is the
/// maximum of both the amount locked and the time is it locked for).
///
/// The dispatch origin of this call must be _Signed_, and the signer must have a vote
/// registered for poll `index`.
Expand Down Expand Up @@ -426,7 +443,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
}
// Extend the lock to `balance` (rather than setting it) since we don't know what
// other votes are in place.
Self::extend_lock(who, &class, vote.balance());
Self::extend_freeze(who, &class, vote.balance())?;
Ok(())
})
})
Expand Down Expand Up @@ -583,7 +600,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
Self::increase_upstream_delegation(&target, &class, conviction.votes(balance));
// Extend the lock to `balance` (rather than setting it) since we don't know what
// other votes are in place.
Self::extend_lock(&who, &class, balance);
Self::extend_freeze(&who, &class, balance)?;
Ok(votes)
})?;
Self::deposit_event(Event::<T, I>::Delegated(who, target));
Expand Down Expand Up @@ -629,7 +646,11 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
Ok(votes)
}

fn extend_lock(who: &T::AccountId, class: &ClassOf<T, I>, amount: BalanceOf<T, I>) {
fn extend_freeze(
who: &T::AccountId,
class: &ClassOf<T, I>,
amount: BalanceOf<T, I>,
) -> DispatchResult {
ClassLocksFor::<T, I>::mutate(who, |locks| {
match locks.iter().position(|x| &x.0 == class) {
Some(i) => locks[i].1 = locks[i].1.max(amount),
Expand All @@ -644,25 +665,21 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
},
}
});
T::Currency::extend_lock(
CONVICTION_VOTING_ID,
who,
amount,
WithdrawReasons::except(WithdrawReasons::RESERVE),
);
T::Currency::extend_freeze(&FreezeReason::ConvictionVoting.into(), who, amount)?;
Ok(())
}

/// Rejig the lock on an account. It will never get more stringent (since that would indicate
/// a security hole) but may be reduced from what they are currently.
fn update_lock(class: &ClassOf<T, I>, who: &T::AccountId) {
let class_lock_needed = VotingFor::<T, I>::mutate(who, class, |voting| {
fn update_lock(class: &ClassOf<T, I>, who: &T::AccountId) -> DispatchResult {
let class_freeze_needed = VotingFor::<T, I>::mutate(who, class, |voting| {
voting.rejig(frame_system::Pallet::<T>::block_number());
voting.locked_balance()
});
let lock_needed = ClassLocksFor::<T, I>::mutate(who, |locks| {
let freeze_needed = ClassLocksFor::<T, I>::mutate(who, |locks| {
locks.retain(|x| &x.0 != class);
if !class_lock_needed.is_zero() {
let ok = locks.try_push((class.clone(), class_lock_needed)).is_ok();
if !class_freeze_needed.is_zero() {
let ok = locks.try_push((class.clone(), class_freeze_needed)).is_ok();
debug_assert!(
ok,
"Vec bounded by number of classes; \
Expand All @@ -672,15 +689,11 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
}
locks.iter().map(|x| x.1).max().unwrap_or(Zero::zero())
});
if lock_needed.is_zero() {
T::Currency::remove_lock(CONVICTION_VOTING_ID, who);
if freeze_needed.is_zero() {
T::Currency::thaw(&FreezeReason::ConvictionVoting.into(), who)?;
} else {
T::Currency::set_lock(
CONVICTION_VOTING_ID,
who,
lock_needed,
WithdrawReasons::except(WithdrawReasons::RESERVE),
);
T::Currency::set_freeze(&FreezeReason::ConvictionVoting.into(), who, freeze_needed)?;
}
Ok(())
}
}
7 changes: 4 additions & 3 deletions substrate/frame/conviction-voting/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ impl pallet_balances::Config for Test {
type ExistentialDeposit = ConstU64<1>;
type AccountStore = System;
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type FreezeIdentifier = FreezeReason;
type MaxFreezes = ConstU32<1>;
type RuntimeHoldReason = ();
type RuntimeFreezeReason = ();
}
Expand Down Expand Up @@ -157,6 +157,7 @@ impl Polling<TallyOf<Test>> for TestPolls {
}

impl Config for Test {
type RuntimeFreezeReason = FreezeReason;
type RuntimeEvent = RuntimeEvent;
type Currency = pallet_balances::Pallet<Self>;
type VoteLockingPeriod = ConstU64<3>;
Expand Down Expand Up @@ -387,7 +388,7 @@ fn successful_conviction_vote_balance_stays_locked_for_correct_time() {
for i in 1..=5 {
assert_ok!(Voting::unlock(RuntimeOrigin::signed(i), c, i));
let expired = block >= (3 << (i - 1)) + 3;
assert_eq!(Balances::usable_balance(i), i * 10 - if expired { 0 } else { 10 });
assert_eq!(Balances::usable_balance(i), (i * 10) - if expired { 0 } else { 10 });
}
}
});
Expand Down
Loading