Skip to content

Commit

Permalink
Merge pull request #891 from gregdhill/refactor/bounded-democracy
Browse files Browse the repository at this point in the history
refactor: use bounded storage items in democracy pallet
  • Loading branch information
sander2 authored Feb 7, 2023
2 parents 39b1dc6 + ab38f22 commit b3ad262
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 12 deletions.
29 changes: 19 additions & 10 deletions crates/democracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ pub mod pallet {
#[pallet::constant]
type MaxProposals: Get<u32>;

/// The maximum number of deposits a public proposal may have at any time.
#[pallet::constant]
type MaxDeposits: Get<u32>;

/// Unix time
type UnixTime: UnixTime;

Expand All @@ -258,14 +262,16 @@ pub mod pallet {
/// The public proposals. Unsorted. The second item is the proposal's hash.
#[pallet::storage]
#[pallet::getter(fn public_props)]
pub type PublicProps<T: Config> = StorageValue<_, Vec<(PropIndex, T::Hash, T::AccountId)>, ValueQuery>;
pub type PublicProps<T: Config> =
StorageValue<_, BoundedVec<(PropIndex, T::Hash, T::AccountId), T::MaxProposals>, ValueQuery>;

/// Those who have locked a deposit.
///
/// TWOX-NOTE: Safe, as increasing integer keys are safe.
#[pallet::storage]
#[pallet::getter(fn deposit_of)]
pub type DepositOf<T: Config> = StorageMap<_, Twox64Concat, PropIndex, (Vec<T::AccountId>, BalanceOf<T>)>;
pub type DepositOf<T: Config> =
StorageMap<_, Twox64Concat, PropIndex, (BoundedVec<T::AccountId, T::MaxDeposits>, BalanceOf<T>)>;

/// Map of hashes to the proposal preimage, along with who registered it and their deposit.
/// The block number is the block at which it was deposited.
Expand Down Expand Up @@ -409,8 +415,8 @@ pub mod pallet {
WrongUpperBound,
/// Maximum number of votes reached.
MaxVotesReached,
/// Maximum number of proposals reached.
TooManyProposals,
/// Maximum number of items reached.
TooMany,
/// Unable to convert value.
TryIntoIntError,
}
Expand Down Expand Up @@ -458,13 +464,15 @@ pub mod pallet {
let index = Self::public_prop_count();
let real_prop_count = PublicProps::<T>::decode_len().unwrap_or(0) as u32;
let max_proposals = T::MaxProposals::get();
ensure!(real_prop_count < max_proposals, Error::<T>::TooManyProposals);
ensure!(real_prop_count < max_proposals, Error::<T>::TooMany);

T::Currency::reserve(&who, value)?;
PublicPropCount::<T>::put(index + 1);
<DepositOf<T>>::insert(index, (&[&who][..], value));

<PublicProps<T>>::append((index, proposal_hash, who));
let depositors = BoundedVec::<_, T::MaxDeposits>::truncate_from(vec![who.clone()]);
DepositOf::<T>::insert(index, (depositors, value));

PublicProps::<T>::try_append((index, proposal_hash, who)).map_err(|_| Error::<T>::TooMany)?;

Self::deposit_event(Event::<T>::Proposed(index, value));
Ok(())
Expand All @@ -490,10 +498,11 @@ pub mod pallet {
let who = ensure_signed(origin)?;

let seconds = Self::len_of_deposit_of(proposal).ok_or_else(|| Error::<T>::ProposalMissing)?;
ensure!(seconds < T::MaxDeposits::get(), Error::<T>::TooMany);
ensure!(seconds <= seconds_upper_bound, Error::<T>::WrongUpperBound);
let mut deposit = Self::deposit_of(proposal).ok_or(Error::<T>::ProposalMissing)?;
T::Currency::reserve(&who, deposit.1)?;
deposit.0.push(who);
deposit.0.try_push(who.clone()).map_err(|_| Error::<T>::TooMany)?;
<DepositOf<T>>::insert(proposal, deposit);
Ok(())
}
Expand Down Expand Up @@ -852,7 +861,7 @@ impl<T: Config> Pallet<T> {
delay: T::BlockNumber,
voting_period: T::BlockNumber,
) -> DispatchResult {
let mut public_props = <PublicProps<T>>::get();
let mut public_props = Self::public_props();
let (winner_index, _) = public_props
.iter()
.enumerate()
Expand Down Expand Up @@ -994,7 +1003,7 @@ impl<T: Config> Pallet<T> {
for d in &depositors {
T::Currency::unreserve(d, deposit);
}
Self::deposit_event(Event::<T>::Tabled(prop_index, deposit, depositors));
Self::deposit_event(Event::<T>::Tabled(prop_index, deposit, depositors.to_vec()));
Self::inject_referendum(
now.saturating_add(T::VotingPeriod::get()),
proposal,
Expand Down
1 change: 1 addition & 0 deletions crates/democracy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ impl Config for Test {
type PalletsOrigin = OriginCaller;
type WeightInfo = ();
type MaxProposals = MaxProposals;
type MaxDeposits = ConstU32<1000>;
type UnixTime = Timestamp;
type Moment = u64;
type LaunchOffsetMillis = LaunchOffsetMillis;
Expand Down
14 changes: 12 additions & 2 deletions crates/democracy/src/tests/decoders.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! The for various partial storage decoders
use super::*;
use frame_support::storage::{migration, unhashed};
use frame_support::{
storage::{migration, unhashed},
BoundedVec,
};

#[test]
fn test_decode_compact_u32_at() {
Expand All @@ -25,7 +28,14 @@ fn test_decode_compact_u32_at() {
fn len_of_deposit_of() {
new_test_ext().execute_with(|| {
for l in vec![0, 1, 200, 1000] {
let value: (Vec<u64>, u64) = ((0..l).map(|_| Default::default()).collect(), 3u64);
let value: (BoundedVec<u64, _>, u64) = (
(0..l)
.map(|_| Default::default())
.collect::<Vec<_>>()
.try_into()
.unwrap(),
3u64,
);
DepositOf::<Test>::insert(2, value);
assert_eq!(Democracy::len_of_deposit_of(2), Some(l));
}
Expand Down
1 change: 1 addition & 0 deletions parachain/runtime/interlay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ impl democracy::Config for Runtime {
type MaxVotes = MaxVotes;
type WeightInfo = ();
type MaxProposals = MaxProposals;
type MaxDeposits = ConstU32<100>;
type UnixTime = Timestamp;
type Moment = Moment;
type LaunchOffsetMillis = LaunchOffsetMillis;
Expand Down
1 change: 1 addition & 0 deletions parachain/runtime/kintsugi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ impl democracy::Config for Runtime {
type MaxVotes = MaxVotes;
type WeightInfo = ();
type MaxProposals = MaxProposals;
type MaxDeposits = ConstU32<100>;
type UnixTime = Timestamp;
type Moment = Moment;
type LaunchOffsetMillis = LaunchOffsetMillis;
Expand Down
1 change: 1 addition & 0 deletions parachain/runtime/testnet-interlay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ impl democracy::Config for Runtime {
type MaxVotes = MaxVotes;
type WeightInfo = ();
type MaxProposals = MaxProposals;
type MaxDeposits = ConstU32<100>;
type UnixTime = Timestamp;
type Moment = Moment;
type LaunchOffsetMillis = LaunchOffsetMillis;
Expand Down
1 change: 1 addition & 0 deletions parachain/runtime/testnet-kintsugi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ impl democracy::Config for Runtime {
type MaxVotes = MaxVotes;
type WeightInfo = ();
type MaxProposals = MaxProposals;
type MaxDeposits = ConstU32<100>;
type UnixTime = Timestamp;
type Moment = Moment;
type LaunchOffsetMillis = LaunchOffsetMillis;
Expand Down
1 change: 1 addition & 0 deletions standalone/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ impl democracy::Config for Runtime {
type MaxVotes = MaxVotes;
type WeightInfo = ();
type MaxProposals = MaxProposals;
type MaxDeposits = ConstU32<100>;
type UnixTime = Timestamp;
type Moment = Moment;
type LaunchOffsetMillis = LaunchOffsetMillis;
Expand Down

0 comments on commit b3ad262

Please sign in to comment.