Society v2 (#11324)

* New Society

* More logic drafting

* More work

* Building

* Some tests

* Fixes

* Improvements to the voting process

* More tests

* Test number 20

* Tests

* 30 tests

* Another test]

* All tests enabled

* Minor stuff

* generate_storage_alias: Rewrite as proc macro attribute

This rewrites the `generate_storage_alias!` declarative macro as proc-macro attribute. While doing
this the name is changed to `storage_alias`. The prefix can now also be the name of a pallet. This
makes storage aliases work in migrations for all kind of chains and not just for the ones that use
predefined prefixes.

* Maintenance operations don't pay fee

* Fix compilation and FMT

* Moare fixes

* Migrations

* Fix tests and add migration testing

* Introduce lazy-cleanup and avoid unbounded prefix removal

* Fixes

* Fixes

* [WIP][Society] Adding benchmarking to the v2. (#11776)

* [Society] Adding benchmarking to the v2.

* [Society] Code review.

* [Society] Better code.

* Using clear() + clear_prefix() and adding more tests.

* Benchmarking again...

* Fix Cargo

* Fixes

* Fixes

* Spelling

* Fix benchmarks

* Another fix

* Remove println

---------

Co-authored-by: Bastian Köcher <info@kchr.de>
Co-authored-by: Artur Gontijo <arturgontijo@users.noreply.github.com>
This commit is contained in:
Gavin Wood
2023-06-18 18:22:17 +02:00
committed by GitHub
parent 116b6e65dc
commit 33a6536299
25 changed files with 3416 additions and 1444 deletions
+144 -47
View File
@@ -21,7 +21,7 @@ use super::*;
use crate as pallet_society;
use frame_support::{
ord_parameter_types, parameter_types,
assert_noop, assert_ok, ord_parameter_types, parameter_types,
traits::{ConstU32, ConstU64},
};
use frame_support_test::TestRandomness;
@@ -32,6 +32,8 @@ use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
};
use RuntimeOrigin as Origin;
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
@@ -52,8 +54,12 @@ parameter_types! {
}
ord_parameter_types! {
pub const ChallengePeriod: u64 = 8;
pub const ClaimPeriod: u64 = 1;
pub const FounderSetAccount: u128 = 1;
pub const SuspensionJudgementSetAccount: u128 = 2;
pub const MaxPayouts: u32 = 10;
pub const MaxBids: u32 = 10;
}
impl frame_system::Config for Test {
@@ -101,34 +107,31 @@ impl pallet_balances::Config for Test {
impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type PalletId = SocietyPalletId;
type Currency = pallet_balances::Pallet<Self>;
type Randomness = TestRandomness<Self>;
type CandidateDeposit = ConstU64<25>;
type WrongSideDeduction = ConstU64<2>;
type MaxStrikes = ConstU32<2>;
type GraceStrikes = ConstU32<1>;
type PeriodSpend = ConstU64<1000>;
type MembershipChanged = ();
type RotationPeriod = ConstU64<4>;
type VotingPeriod = ConstU64<3>;
type ClaimPeriod = ClaimPeriod;
type MaxLockDuration = ConstU64<100>;
type FounderSetOrigin = EnsureSignedBy<FounderSetAccount, u128>;
type SuspensionJudgementOrigin = EnsureSignedBy<SuspensionJudgementSetAccount, u128>;
type ChallengePeriod = ConstU64<8>;
type MaxCandidateIntake = ConstU32<10>;
type PalletId = SocietyPalletId;
type ChallengePeriod = ChallengePeriod;
type MaxPayouts = MaxPayouts;
type MaxBids = MaxBids;
type WeightInfo = ();
}
pub struct EnvBuilder {
members: Vec<u128>,
balance: u64,
balances: Vec<(u128, u64)>,
pot: u64,
max_members: u32,
founded: bool,
}
impl EnvBuilder {
pub fn new() -> Self {
Self {
members: vec![10],
balance: 10_000,
balances: vec![
(10, 50),
@@ -142,7 +145,7 @@ impl EnvBuilder {
(90, 50),
],
pot: 0,
max_members: 100,
founded: true,
}
}
@@ -152,39 +155,22 @@ impl EnvBuilder {
pallet_balances::GenesisConfig::<Test> { balances: self.balances }
.assimilate_storage(&mut t)
.unwrap();
pallet_society::GenesisConfig::<Test> {
members: self.members,
pot: self.pot,
max_members: self.max_members,
}
.assimilate_storage(&mut t)
.unwrap();
pallet_society::GenesisConfig::<Test> { pot: self.pot }
.assimilate_storage(&mut t)
.unwrap();
let mut ext: sp_io::TestExternalities = t.into();
ext.execute_with(f)
ext.execute_with(|| {
if self.founded {
let r = b"be cool".to_vec();
assert!(Society::found_society(Origin::signed(1), 10, 10, 8, 2, 25, r).is_ok());
}
let r = f();
migrations::assert_internal_consistency::<Test, ()>();
r
})
}
#[allow(dead_code)]
pub fn with_members(mut self, m: Vec<u128>) -> Self {
self.members = m;
self
}
#[allow(dead_code)]
pub fn with_balances(mut self, b: Vec<(u128, u64)>) -> Self {
self.balances = b;
self
}
#[allow(dead_code)]
pub fn with_pot(mut self, p: u64) -> Self {
self.pot = p;
self
}
#[allow(dead_code)]
pub fn with_balance(mut self, b: u64) -> Self {
self.balance = b;
self
}
#[allow(dead_code)]
pub fn with_max_members(mut self, n: u32) -> Self {
self.max_members = n;
pub fn founded(mut self, f: bool) -> Self {
self.founded = f;
self
}
}
@@ -202,10 +188,121 @@ pub fn run_to_block(n: u64) {
}
/// Creates a bid struct using input parameters.
pub fn create_bid<AccountId, Balance>(
value: Balance,
pub fn bid<AccountId, Balance>(
who: AccountId,
kind: BidKind<AccountId, Balance>,
value: Balance,
) -> Bid<AccountId, Balance> {
Bid { who, kind, value }
}
/// Creates a candidate struct using input parameters.
pub fn candidacy<AccountId, Balance>(
round: RoundIndex,
bid: Balance,
kind: BidKind<AccountId, Balance>,
approvals: VoteCount,
rejections: VoteCount,
) -> Candidacy<AccountId, Balance> {
Candidacy { round, kind, bid, tally: Tally { approvals, rejections }, skeptic_struck: false }
}
pub fn next_challenge() {
let challenge_period: u64 = <Test as Config>::ChallengePeriod::get();
let now = System::block_number();
run_to_block(now + challenge_period - now % challenge_period);
}
pub fn next_voting() {
if let Period::Voting { more, .. } = Society::period() {
run_to_block(System::block_number() + more);
}
}
pub fn conclude_intake(allow_resignation: bool, judge_intake: Option<bool>) {
next_voting();
let round = RoundCount::<Test>::get();
for (who, candidacy) in Candidates::<Test>::iter() {
if candidacy.tally.clear_approval() {
assert_ok!(Society::claim_membership(Origin::signed(who)));
assert_noop!(
Society::claim_membership(Origin::signed(who)),
Error::<Test>::NotCandidate
);
continue
}
if candidacy.tally.clear_rejection() && allow_resignation {
assert_noop!(
Society::claim_membership(Origin::signed(who)),
Error::<Test>::NotApproved
);
assert_ok!(Society::resign_candidacy(Origin::signed(who)));
continue
}
if let (Some(founder), Some(approve)) = (Founder::<Test>::get(), judge_intake) {
if !candidacy.tally.clear_approval() && !approve {
// can be rejected by founder
assert_ok!(Society::kick_candidate(Origin::signed(founder), who));
continue
}
if !candidacy.tally.clear_rejection() && approve {
// can be rejected by founder
assert_ok!(Society::bestow_membership(Origin::signed(founder), who));
continue
}
}
if candidacy.tally.clear_rejection() && round > candidacy.round + 1 {
assert_noop!(
Society::claim_membership(Origin::signed(who)),
Error::<Test>::NotApproved
);
assert_ok!(Society::drop_candidate(Origin::signed(0), who));
assert_noop!(
Society::drop_candidate(Origin::signed(0), who),
Error::<Test>::NotCandidate
);
continue
}
if !candidacy.skeptic_struck {
assert_ok!(Society::punish_skeptic(Origin::signed(who)));
}
}
}
pub fn next_intake() {
let claim_period: u64 = <Test as Config>::ClaimPeriod::get();
match Society::period() {
Period::Voting { more, .. } => run_to_block(System::block_number() + more + claim_period),
Period::Claim { more, .. } => run_to_block(System::block_number() + more),
}
}
pub fn place_members(members: impl AsRef<[u128]>) {
for who in members.as_ref() {
assert_ok!(Society::insert_member(who, 0));
}
}
pub fn members() -> Vec<u128> {
let mut r = Members::<Test>::iter_keys().collect::<Vec<_>>();
r.sort();
r
}
pub fn membership() -> Vec<(u128, MemberRecord)> {
let mut r = Members::<Test>::iter().collect::<Vec<_>>();
r.sort_by_key(|x| x.0);
r
}
pub fn candidacies() -> Vec<(u128, Candidacy<u128, u64>)> {
let mut r = Candidates::<Test>::iter().collect::<Vec<_>>();
r.sort_by_key(|x| x.0);
r
}
pub fn candidates() -> Vec<u128> {
let mut r = Candidates::<Test>::iter_keys().collect::<Vec<_>>();
r.sort();
r
}