Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
94cf161
Add pallet-alliance
koushiro Sep 17, 2021
f12601d
Update multihash/cid and format
koushiro Sep 17, 2021
0d93dd6
Remove useless deps
koushiro Sep 18, 2021
3bfc260
Add pallet-alliance into node runtime
koushiro Sep 18, 2021
445649e
Fix has_identity
koushiro Sep 22, 2021
0d2eaee
Add TooMantBlacklist Error
koushiro Oct 4, 2021
b69980d
Add TooLongWebsiteUrlLength Error
koushiro Oct 4, 2021
9cab715
Add remove_announcement
koushiro Oct 4, 2021
9241f08
Derive pallet_identity::Config and Fix test/benchmarking
koushiro Oct 5, 2021
0a71502
Add part weight for call of pallet-alliance
koushiro Oct 5, 2021
d7aeddf
Remove pallet_identity Config trait
koushiro Oct 9, 2021
b07520e
Fix propose arguments
koushiro Oct 11, 2021
9774055
Some nits
koushiro Oct 11, 2021
0013afd
cargo fmt
koushiro Oct 11, 2021
6b2c8af
Fix benchmarking of add_blacklist/remove_blacklist
koushiro Oct 11, 2021
1d2bc71
Some nits
koushiro Oct 11, 2021
e7f0cd3
Add benchmarking for init_members
koushiro Oct 11, 2021
181738b
Add benchmarking for propose/vote/veto
koushiro Oct 11, 2021
a359aea
Fix benchmarking
koushiro Oct 11, 2021
d1bb18d
Remove some useless
koushiro Oct 11, 2021
a2f986f
fix some compile issue
xlc Mar 12, 2022
e37664e
more fix
xlc Mar 12, 2022
a04c5b8
all checks
xlc Mar 12, 2022
fde2562
refactor
xlc Mar 12, 2022
1757810
refactor event
xlc Mar 12, 2022
03a6be5
refactor
xlc Mar 12, 2022
3e4e7f4
cleanup
xlc Mar 12, 2022
7333148
cleanup
xlc Mar 12, 2022
06b42a8
Merge branch 'master' into polkadot-alliance-review
xlc Mar 24, 2022
c49b633
fix benchmarking
xlc Mar 24, 2022
bbd087b
fix warning
xlc Mar 24, 2022
062bad8
fmt
xlc Mar 24, 2022
8ab53a8
fix benchmarks
xlc Mar 25, 2022
86b4fcf
Merge remote-tracking branch 'origin/master' into polkadot-alliance-r…
xlc Mar 31, 2022
c16c2a7
with storage info
xlc Mar 31, 2022
58ca07d
fmt
xlc Mar 31, 2022
4013d84
add new config
xlc Apr 2, 2022
a0c0971
fix benchmarks
xlc Apr 2, 2022
31f846b
fix
xlc Apr 3, 2022
616646b
fmt
xlc Apr 3, 2022
02aaf4e
Apply suggestions from code review
xlc Apr 4, 2022
82f84d1
improvements
xlc Apr 8, 2022
551e830
fix
xlc Apr 8, 2022
451396e
update docs
joepetrowski Apr 9, 2022
814d520
rename events, errors, and functions
joepetrowski Apr 9, 2022
f90df9e
make kicking events clearer
joepetrowski Apr 9, 2022
d93f872
Merge pull request #2 from joepetrowski/polkadot-alliance-review
xlc Apr 10, 2022
35d50b9
fix
xlc Apr 10, 2022
e024c73
fix tests
xlc Apr 10, 2022
1d0eef4
fix tests
xlc Apr 10, 2022
4f2a0bd
fix runtime
xlc Apr 10, 2022
3c8b878
fix build
xlc Apr 10, 2022
0f12ed4
remove unneeded change
xlc Apr 10, 2022
b20b72a
Merge remote-tracking branch 'origin/master' into polkadot-alliance-r…
xlc Apr 13, 2022
5b6b336
remove Candidate role from Alliance
joepetrowski Apr 30, 2022
a97b897
merge master
joepetrowski Apr 30, 2022
0970b78
fmt grumbles
joepetrowski Apr 30, 2022
d292548
update lock
joepetrowski Apr 30, 2022
6da40be
update recursion limit
joepetrowski Apr 30, 2022
dc6400c
benchmarks
joepetrowski Apr 30, 2022
428638a
convert-try
joepetrowski Apr 30, 2022
c029b0c
benchmark assertions
joepetrowski Apr 30, 2022
5ed70ae
fmt ,
joepetrowski Apr 30, 2022
d7766b0
merge master
joepetrowski May 19, 2022
50726b3
cargo lock
joepetrowski May 20, 2022
60d9434
Update frame/alliance/src/benchmarking.rs
joepetrowski May 20, 2022
2809717
Apply suggestions from code review
joepetrowski May 20, 2022
f270ada
add docs to public interfaces
joepetrowski May 20, 2022
955854a
Apply suggestions from code review
joepetrowski May 23, 2022
944a612
fix build (node)
joepetrowski May 23, 2022
7c063c8
review
joepetrowski May 23, 2022
5feb2d1
Merge remote-tracking branch 'origin' into polkadot-alliance-review
joepetrowski May 25, 2022
a576428
Merge remote-tracking branch 'origin' into polkadot-alliance-review
joepetrowski May 30, 2022
fb8dd06
merge master
joepetrowski Jun 8, 2022
0e39490
use EitherOfDiverse
joepetrowski Jun 8, 2022
d89078d
update cargo toml
joepetrowski Jun 9, 2022
8338125
make all dispatch class Normal
joepetrowski Jun 10, 2022
fdf4dc9
change blacklist to unscrupulous
joepetrowski Jun 10, 2022
0c10bcd
formatting
joepetrowski Jun 10, 2022
3ee24a8
fmt oops
joepetrowski Jun 10, 2022
78b63c1
rename benchmarking unscrupulous account creator
joepetrowski Jun 10, 2022
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
Prev Previous commit
Next Next commit
with storage info
  • Loading branch information
xlc committed Mar 31, 2022
commit c16c2a779751ae87e1efba1ed614aa4506115d7e
143 changes: 91 additions & 52 deletions frame/alliance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,16 @@ mod benchmarking;
mod types;
pub mod weights;

use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use sp_runtime::{
traits::{StaticLookup, Zero},
RuntimeDebug,
};
use sp_std::prelude::*;

use frame_support::{
codec::{Decode, Encode},
codec::{Decode, Encode, MaxEncodedLen},
dispatch::{
DispatchError, DispatchResult, DispatchResultWithPostInfo, Dispatchable, GetDispatchInfo,
PostDispatchInfo,
Expand All @@ -128,7 +130,7 @@ pub use weights::*;
/// Simple index type for proposal counting.
pub type ProposalIndex = u32;

type Url = Vec<u8>;
type UrlOf<T, I> = BoundedVec<u8, <T as pallet::Config<I>>::MaxWebsiteUrlLength>;

type BalanceOf<T, I = ()> =
<<T as Config<I>>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
Expand Down Expand Up @@ -157,7 +159,6 @@ impl<AccountId> IdentityVerifier<AccountId> for () {
None
}
}

pub trait ProposalProvider<AccountId, Hash, Proposal> {
fn propose_proposal(
who: AccountId,
Expand Down Expand Up @@ -186,29 +187,28 @@ pub trait ProposalProvider<AccountId, Hash, Proposal> {
}

/// The role of members.
#[derive(Copy, Clone, PartialEq, Eq, RuntimeDebug, Encode, Decode, TypeInfo)]
#[derive(Copy, Clone, PartialEq, Eq, RuntimeDebug, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub enum MemberRole {
Founder,
Fellow,
Ally,
}

/// The item type of blacklist.
#[derive(Clone, PartialEq, Eq, RuntimeDebug, Encode, Decode, TypeInfo)]
pub enum BlacklistItem<AccountId> {
#[derive(Clone, PartialEq, Eq, RuntimeDebug, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub enum BlacklistItem<AccountId, Url> {
AccountId(AccountId),
Website(Url),
}

type BlacklistItemOf<T, I> = BlacklistItem<<T as frame_system::Config>::AccountId, UrlOf<T, I>>;

#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

#[pallet::pallet]
#[pallet::generate_store(pub (super) trait Store)]
#[pallet::without_storage_info]
pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);

#[pallet::config]
Expand Down Expand Up @@ -283,6 +283,18 @@ pub mod pallet {
#[pallet::constant]
type CandidateDeposit: Get<BalanceOf<Self, I>>;

/// The maximum numbers of announcements.
#[pallet::constant]
type MaxAnnouncementsCount: Get<u32>;

/// The maximum numbers of members per member role.
#[pallet::constant]
type MaxMembersCount: Get<u32>;

/// The maximum numbers of candidates.
#[pallet::constant]
type MaxCandidatesCount: Get<u32>;

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}
Expand Down Expand Up @@ -331,6 +343,12 @@ pub mod pallet {
NotVetoableProposal,
/// The Announcement is not found.
MissingAnnouncement,
/// Number of members exceed MaxMembersCount.
TooManyMembers,
/// Number of candidates exceed MaxCandidatesCount.
TooManyCandidates,
/// Number of Announcements exceed MaxAnnouncementsCount.
TooManyAnnouncements,
}

#[pallet::event]
Expand Down Expand Up @@ -365,9 +383,9 @@ pub mod pallet {
/// A member has been kicked out to an ordinary account with its deposit slashed.
MemberKicked { member: T::AccountId, slashed: Option<BalanceOf<T, I>> },
/// Accounts or websites have been added into blacklist.
BlacklistAdded { items: Vec<BlacklistItem<T::AccountId>> },
BlacklistAdded { items: Vec<BlacklistItemOf<T, I>> },
/// Accounts or websites have been removed from blacklist.
BlacklistRemoved { items: Vec<BlacklistItem<T::AccountId>> },
BlacklistRemoved { items: Vec<BlacklistItemOf<T, I>> },
}

#[pallet::genesis_config]
Expand Down Expand Up @@ -408,17 +426,23 @@ pub mod pallet {
!Pallet::<T, I>::has_member(MemberRole::Founder),
"Founders are already initialized!"
);
Members::<T, I>::insert(MemberRole::Founder, self.founders.clone());
let members: BoundedVec<T::AccountId, T::MaxMembersCount> =
self.founders.clone().try_into().expect("Too many genesis founders");
Members::<T, I>::insert(MemberRole::Founder, members);
}
if !self.fellows.is_empty() {
assert!(
!Pallet::<T, I>::has_member(MemberRole::Fellow),
"Fellows are already initialized!"
);
Members::<T, I>::insert(MemberRole::Fellow, self.fellows.clone());
let members: BoundedVec<T::AccountId, T::MaxMembersCount> =
self.fellows.clone().try_into().expect("Too many genesis fellows");
Members::<T, I>::insert(MemberRole::Fellow, members);
}
if !self.allies.is_empty() {
Members::<T, I>::insert(MemberRole::Ally, self.allies.clone())
let members: BoundedVec<T::AccountId, T::MaxMembersCount> =
self.allies.clone().try_into().expect("Too many genesis allies");
Members::<T, I>::insert(MemberRole::Ally, members);
}

T::InitializeMembers::initialize_members(
Expand All @@ -439,7 +463,8 @@ pub mod pallet {
/// The current IPFS cids of the announcements.
#[pallet::storage]
#[pallet::getter(fn announcements)]
pub type Announcements<T: Config<I>, I: 'static = ()> = StorageValue<_, Vec<Cid>, ValueQuery>;
pub type Announcements<T: Config<I>, I: 'static = ()> =
StorageValue<_, BoundedVec<Cid, T::MaxAnnouncementsCount>, ValueQuery>;

/// Maps member and their candidate deposit.
#[pallet::storage]
Expand All @@ -452,15 +477,20 @@ pub mod pallet {
#[pallet::storage]
#[pallet::getter(fn candidates)]
pub type Candidates<T: Config<I>, I: 'static = ()> =
StorageValue<_, Vec<T::AccountId>, ValueQuery>;
StorageValue<_, BoundedVec<T::AccountId, T::MaxCandidatesCount>, ValueQuery>;

/// Maps member type to alliance members, including founder, fellow and ally.
/// Founders and fellows can propose and vote on alliance motions,
/// and ally can only wait to be elevated to fellow.
#[pallet::storage]
#[pallet::getter(fn members)]
pub type Members<T: Config<I>, I: 'static = ()> =
StorageMap<_, Twox64Concat, MemberRole, Vec<T::AccountId>, ValueQuery>;
pub type Members<T: Config<I>, I: 'static = ()> = StorageMap<
_,
Twox64Concat,
MemberRole,
BoundedVec<T::AccountId, T::MaxMembersCount>,
ValueQuery,
>;

/// The members are being kicked out. They can't retire during the motion.
#[pallet::storage]
Expand All @@ -472,13 +502,13 @@ pub mod pallet {
#[pallet::storage]
#[pallet::getter(fn account_blacklist)]
pub type AccountBlacklist<T: Config<I>, I: 'static = ()> =
StorageValue<_, Vec<T::AccountId>, ValueQuery>;
StorageValue<_, BoundedVec<T::AccountId, T::MaxBlacklistCount>, ValueQuery>;

/// The current blacklist of websites.
#[pallet::storage]
#[pallet::getter(fn website_blacklist)]
pub type WebsiteBlacklist<T: Config<I>, I: 'static = ()> =
StorageValue<_, Vec<Url>, ValueQuery>;
StorageValue<_, BoundedVec<UrlOf<T, I>, T::MaxBlacklistCount>, ValueQuery>;

#[pallet::call]
impl<T: Config<I>, I: 'static> Pallet<T, I> {
Expand Down Expand Up @@ -608,12 +638,19 @@ pub mod pallet {
))]
pub fn init_members(
origin: OriginFor<T>,
mut founders: Vec<T::AccountId>,
mut fellows: Vec<T::AccountId>,
mut allies: Vec<T::AccountId>,
founders: Vec<T::AccountId>,
fellows: Vec<T::AccountId>,
allies: Vec<T::AccountId>,
) -> DispatchResult {
ensure_root(origin)?;

let mut founders: BoundedVec<T::AccountId, T::MaxMembersCount> =
founders.try_into().map_err(|_| Error::<T, I>::TooManyMembers)?;
let mut fellows: BoundedVec<T::AccountId, T::MaxMembersCount> =
fellows.try_into().map_err(|_| Error::<T, I>::TooManyMembers)?;
let mut allies: BoundedVec<T::AccountId, T::MaxMembersCount> =
allies.try_into().map_err(|_| Error::<T, I>::TooManyMembers)?;

ensure!(
!Self::has_member(MemberRole::Founder) &&
!Self::has_member(MemberRole::Fellow) &&
Expand Down Expand Up @@ -644,7 +681,11 @@ pub mod pallet {
founders, fellows, allies
);

Self::deposit_event(Event::MembersInitialized { founders, fellows, allies });
Self::deposit_event(Event::MembersInitialized {
founders: founders.into(),
fellows: fellows.into(),
allies: allies.into(),
});
Ok(())
}

Expand All @@ -665,7 +706,7 @@ pub mod pallet {
T::SuperMajorityOrigin::ensure_origin(origin)?;

let mut announcements = <Announcements<T, I>>::get();
announcements.push(announcement.clone());
announcements.try_push(announcement.clone()).map_err(|_| Error::<T, I>::TooManyAnnouncements)?;
<Announcements<T, I>>::put(announcements);

Self::deposit_event(Event::NewAnnouncement { announcement });
Expand Down Expand Up @@ -841,7 +882,7 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::add_blacklist(infos.len() as u32, T::MaxWebsiteUrlLength::get()))]
pub fn add_blacklist(
origin: OriginFor<T>,
infos: Vec<BlacklistItem<T::AccountId>>,
infos: Vec<BlacklistItemOf<T, I>>,
) -> DispatchResult {
T::SuperMajorityOrigin::ensure_origin(origin)?;

Expand All @@ -861,17 +902,6 @@ pub mod pallet {
}
}

let account_blacklist_len = AccountBlacklist::<T, I>::decode_len().unwrap_or_default();
ensure!(
(account_blacklist_len + accounts.len()) as u32 <= T::MaxBlacklistCount::get(),
Error::<T, I>::TooManyBlacklist
);
let web_blacklist_len = WebsiteBlacklist::<T, I>::decode_len().unwrap_or_default();
ensure!(
(web_blacklist_len + webs.len()) as u32 <= T::MaxBlacklistCount::get(),
Error::<T, I>::TooManyBlacklist
);

Self::do_add_blacklist(&mut accounts, &mut webs)?;
Self::deposit_event(Event::BlacklistAdded { items: infos });
Ok(())
Expand All @@ -881,7 +911,7 @@ pub mod pallet {
#[pallet::weight(<T as Config<I>>::WeightInfo::remove_blacklist(infos.len() as u32, T::MaxWebsiteUrlLength::get()))]
pub fn remove_blacklist(
origin: OriginFor<T>,
infos: Vec<BlacklistItem<T::AccountId>>,
infos: Vec<BlacklistItemOf<T, I>>,
) -> DispatchResult {
T::SuperMajorityOrigin::ensure_origin(origin)?;
let mut accounts = vec![];
Expand Down Expand Up @@ -910,7 +940,9 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
fn add_candidate(who: &T::AccountId) -> DispatchResult {
<Candidates<T, I>>::try_mutate(|candidates| {
let pos = candidates.binary_search(who).err().ok_or(Error::<T, I>::AlreadyCandidate)?;
candidates.insert(pos, who.clone());
candidates
.try_insert(pos, who.clone())
.map_err(|_| Error::<T, I>::TooManyCandidates)?;
Ok(())
})
}
Expand Down Expand Up @@ -959,11 +991,11 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
}

fn votable_member_sorted() -> Vec<T::AccountId> {
let mut founders = Members::<T, I>::get(MemberRole::Founder);
let mut fellows = Members::<T, I>::get(MemberRole::Fellow);
let mut founders = Members::<T, I>::get(MemberRole::Founder).into_inner();
let mut fellows = Members::<T, I>::get(MemberRole::Fellow).into_inner();
founders.append(&mut fellows);
founders.sort();
founders
founders.into()
}

fn is_kicking(who: &T::AccountId) -> bool {
Expand All @@ -974,7 +1006,9 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
fn add_member(who: &T::AccountId, role: MemberRole) -> DispatchResult {
<Members<T, I>>::try_mutate(role, |members| -> DispatchResult {
let pos = members.binary_search(who).err().ok_or(Error::<T, I>::AlreadyMember)?;
members.insert(pos, who.clone());
members
.try_insert(pos, who.clone())
.map_err(|_| Error::<T, I>::TooManyMembers)?;
Ok(())
})?;

Expand All @@ -1001,7 +1035,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
}

/// Check if a user is in blacklist.
fn is_blacklist(info: &BlacklistItem<T::AccountId>) -> bool {
fn is_blacklist(info: &BlacklistItemOf<T, I>) -> bool {
match info {
BlacklistItem::Website(url) => <WebsiteBlacklist<T, I>>::get().contains(url),
BlacklistItem::AccountId(who) => <AccountBlacklist<T, I>>::get().contains(who),
Expand All @@ -1016,27 +1050,32 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Add a identity info to the blacklist set.
fn do_add_blacklist(
new_accounts: &mut Vec<T::AccountId>,
new_webs: &mut Vec<Url>,
new_webs: &mut Vec<UrlOf<T, I>>,
) -> DispatchResult {
if !new_accounts.is_empty() {
<AccountBlacklist<T, I>>::mutate(|accounts| {
accounts.append(new_accounts);
<AccountBlacklist<T, I>>::try_mutate(|accounts| -> DispatchResult {
accounts.try_append(new_accounts).map_err(|_| Error::<T, I>::TooManyBlacklist)?;
accounts.sort();
});

Ok(())
})?;
}
if !new_webs.is_empty() {
<WebsiteBlacklist<T, I>>::mutate(|webs| {
webs.append(new_webs);
<WebsiteBlacklist<T, I>>::try_mutate(|webs| -> DispatchResult {
webs.try_append(new_webs).map_err(|_| Error::<T, I>::TooManyBlacklist)?;
webs.sort();
});

Ok(())
})?;
}

Ok(())
}

/// Remove a identity info from the blacklist.
fn do_remove_blacklist(
out_accounts: &mut Vec<T::AccountId>,
out_webs: &mut Vec<Url>,
out_webs: &mut Vec<UrlOf<T, I>>,
) -> DispatchResult {
if !out_accounts.is_empty() {
<AccountBlacklist<T, I>>::try_mutate(|accounts| -> DispatchResult {
Expand Down
24 changes: 24 additions & 0 deletions frame/support/src/storage/bounded_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ impl<T, S> BoundedVec<T, S> {
self.0.sort_by(compare)
}

/// Exactly the same semantics as [`slice::sort`].
///
/// This is safe since sorting cannot change the number of elements in the vector.
pub fn sort(&mut self)
where
T: sp_std::cmp::Ord,
{
self.0.sort()
}

/// Exactly the same semantics as `Vec::remove`.
///
/// # Panics
Expand Down Expand Up @@ -374,6 +384,20 @@ impl<T, S: Get<u32>> BoundedVec<T, S> {
}
}

/// Exactly the same semantics as [`Vec::append`], but returns an error and does nothing if the
/// length of the outcome is larger than the bound.
pub fn try_append(
&mut self,
other: &mut Vec<T>,
) -> Result<(), ()> {
if other.len().saturating_add(self.len()) <= Self::bound() {
self.0.append(other);
Ok(())
} else {
Err(())
}
}

/// Consumes self and mutates self via the given `mutate` function.
///
/// If the outcome of mutation is within bounds, `Some(Self)` is returned. Else, `None` is
Expand Down