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

BoundedVec + Shims for Append/DecodeLength #8556

Merged
20 commits merged into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from 15 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
19 changes: 7 additions & 12 deletions frame/collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ use sp_io::storage;
use sp_runtime::{RuntimeDebug, traits::Hash};

use frame_support::{
decl_error, decl_event, decl_module, decl_storage, ensure, BoundedVec,
codec::{Decode, Encode},
decl_error, decl_event, decl_module, decl_storage,
dispatch::{
DispatchError, DispatchResult, DispatchResultWithPostInfo, Dispatchable, Parameter,
PostDispatchInfo,
},
ensure,
traits::{ChangeMembers, EnsureOrigin, Get, InitializeMembers, GetBacking, Backing},
weights::{DispatchClass, GetDispatchInfo, Weight, Pays},
};
Expand Down Expand Up @@ -195,7 +194,7 @@ pub struct Votes<AccountId, BlockNumber> {
decl_storage! {
trait Store for Module<T: Config<I>, I: Instance=DefaultInstance> as Collective {
/// The hashes of the active proposals.
pub Proposals get(fn proposals): Vec<T::Hash>;
pub Proposals get(fn proposals): BoundedVec<T::Hash, T::MaxProposals>;
/// Actual proposal for a given hash, if it's current.
pub ProposalOf get(fn proposal_of):
map hasher(identity) T::Hash => Option<<T as Config<I>>::Proposal>;
Expand Down Expand Up @@ -471,11 +470,7 @@ decl_module! {
} else {
let active_proposals =
<Proposals<T, I>>::try_mutate(|proposals| -> Result<usize, DispatchError> {
proposals.push(proposal_hash);
ensure!(
proposals.len() <= T::MaxProposals::get() as usize,
Error::<T, I>::TooManyProposals
);
proposals.try_push(proposal_hash).map_err(|_| Error::<T, I>::TooManyProposals)?;
Ok(proposals.len())
})?;
let index = Self::proposal_count();
Expand Down Expand Up @@ -1086,7 +1081,7 @@ mod tests {
fn motions_basic_environment_works() {
new_test_ext().execute_with(|| {
assert_eq!(Collective::members(), vec![1, 2, 3]);
assert_eq!(Collective::proposals(), Vec::<H256>::new());
assert_eq!(*Collective::proposals(), Vec::<H256>::new());
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
});
}

Expand Down Expand Up @@ -1316,7 +1311,7 @@ mod tests {
let hash = proposal.blake2_256().into();
let end = 4;
assert_ok!(Collective::propose(Origin::signed(1), 3, Box::new(proposal.clone()), proposal_len));
assert_eq!(Collective::proposals(), vec![hash]);
assert_eq!(*Collective::proposals(), vec![hash]);
assert_eq!(Collective::proposal_of(&hash), Some(proposal));
assert_eq!(
Collective::voting(&hash),
Expand Down Expand Up @@ -1577,9 +1572,9 @@ mod tests {
assert_ok!(Collective::propose(Origin::signed(1), 3, Box::new(proposal.clone()), proposal_len));
assert_ok!(Collective::vote(Origin::signed(2), hash.clone(), 0, false));
assert_ok!(Collective::close(Origin::signed(2), hash.clone(), 0, proposal_weight, proposal_len));
assert_eq!(Collective::proposals(), vec![]);
assert_eq!(*Collective::proposals(), vec![]);
assert_ok!(Collective::propose(Origin::signed(1), 2, Box::new(proposal.clone()), proposal_len));
assert_eq!(Collective::proposals(), vec![hash]);
assert_eq!(*Collective::proposals(), vec![hash]);
});
}

Expand Down
3 changes: 2 additions & 1 deletion frame/democracy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ impl pallet_scheduler::Config for Test {
}
parameter_types! {
pub const ExistentialDeposit: u64 = 1;
pub const MaxLocks: u32 = 10;
}
impl pallet_balances::Config for Test {
type MaxLocks = ();
type MaxLocks = MaxLocks;
type Balance = u64;
type Event = Event;
type DustRemoval = ();
Expand Down
20 changes: 10 additions & 10 deletions frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ pub use self::hash::{
};
pub use self::storage::{
StorageValue, StorageMap, StorageDoubleMap, StoragePrefixedMap, IterableStorageMap,
IterableStorageDoubleMap, migration
IterableStorageDoubleMap, migration,
bounded_vec::{self, BoundedVec},
};
pub use self::dispatch::{Parameter, Callable};
pub use sp_runtime::{self, ConsensusEngineId, print, traits::Printable};
Expand Down Expand Up @@ -112,20 +113,20 @@ impl TypeId for PalletId {
///
/// // generate a double map from `(u32, u32)` (with hasher `Twox64Concat`) to `Vec<u8>`
/// generate_storage_alias!(
/// OtherPrefix, OtherStorageName => DoubleMap<
/// OtherPrefix, OtherStorageName => DoubleMap<
/// (u32, u32),
/// (u32, u32),
/// Vec<u8>
/// >
/// >
/// );
///
/// // generate a map from `Config::AccountId` (with hasher `Twox64Concat`) to `Vec<u8>`
/// trait Config { type AccountId: codec::FullCodec; }
/// generate_storage_alias!(
/// Prefix, GenericStorage<T: Config> => Map<(Twox64Concat, T::AccountId), Vec<u8>>
/// Prefix, GenericStorage<T: Config> => Map<(Twox64Concat, T::AccountId), Vec<u8>>
/// );
/// # fn main() {}
///```
/// ```
#[macro_export]
macro_rules! generate_storage_alias {
// without generic for $name.
Expand All @@ -143,7 +144,7 @@ macro_rules! generate_storage_alias {
($pallet:ident, $name:ident => DoubleMap<($key1:ty, $hasher1:ty), ($key2:ty, $hasher2:ty), $value:ty>) => {
$crate::paste::paste! {
$crate::generate_storage_alias!(@GENERATE_INSTANCE_STRUCT $pallet, $name);
type $name = $crate::storage::types::StorageMap<
type $name = $crate::storage::types::StorageDoubleMap<
[<$name Instance>],
$hasher1,
$key1,
Expand Down Expand Up @@ -178,12 +179,11 @@ macro_rules! generate_storage_alias {
(
$pallet:ident,
$name:ident<$t:ident : $bounds:tt>
=> DoubleMap<($key1:ty, $hasher1:ty), ($key2:ty, $hasher2:ty), $value:ty>)
=> {
=> DoubleMap<($key1:ty, $hasher1:ty), ($key2:ty, $hasher2:ty), $value:ty>) => {
$crate::paste::paste! {
$crate::generate_storage_alias!(@GENERATE_INSTANCE_STRUCT $pallet, $name);
#[allow(type_alias_bounds)]
type $name<$t : $bounds> = $crate::storage::types::StorageMap<
type $name<$t : $bounds> = $crate::storage::types::StorageDoubleMap<
[<$name Instance>],
$key1,
$hasher1,
Expand Down Expand Up @@ -213,7 +213,7 @@ macro_rules! generate_storage_alias {
const STORAGE_PREFIX: &'static str = stringify!($name);
}
}
}
};
}

/// Create new implementations of the [`Get`](crate::traits::Get) trait.
Expand Down
Loading