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

Commit

Permalink
BoundedVec + Shims for Append/DecodeLength (#8556)
Browse files Browse the repository at this point in the history
* prototype for shawn

* Clean and document it

* Add more docs

* Move imports

* Some changes for easier compat.

* revert exmaple pallet

* rename

* BoundedVec for AccountLocks (#8580)

* Example with balances

* Fix tests

* Make it indexable

* fix

* Fix tests

* fix test

* Fix collective as well

* Fix test

* Update frame/support/src/storage/mod.rs

Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com>

* Repot and add for value

* Add for map and double map

* Final touches.

* Update frame/support/src/storage/bounded_vec.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Add a few more tests

* Add import

Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com>
Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
  • Loading branch information
3 people authored Apr 16, 2021
1 parent c93ef27 commit 8c4e296
Show file tree
Hide file tree
Showing 8 changed files with 597 additions and 30 deletions.
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());
});
}

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

0 comments on commit 8c4e296

Please sign in to comment.