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

Commit

Permalink
Logic for the (Core) Fellowship (#13503)
Browse files Browse the repository at this point in the history
* More drafting

* Paymaster pallet

* Fix build

* More tests

* Rename

* Rename

* Renaming

* Revert old changes

* Multi-phase payouts to avoid bank-runs

* Tests

* Tests

* Allow payment to be targeted elsewhere

* Proper ssync payment failure handling

* Test for repayment

* Docs

* Impl RankedMembers for RankedCollective

* Implement Pay for Pot (i.e. basic account).

* Benchmarks

* Weights

* Introduce Salary benchmark into node

* Fix warning

* ".git/.scripts/commands/bench/bench.sh" pallet dev pallet_salary

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Jegor Sidorenko <[email protected]>

* Update frame/salary/src/lib.rs

Co-authored-by: Jegor Sidorenko <[email protected]>

* Update lib.rs

* Update frame/salary/src/lib.rs

Co-authored-by: joe petrowski <[email protected]>

* Docs

* Update frame/salary/src/lib.rs

Co-authored-by: joe petrowski <[email protected]>

* Update frame/salary/src/lib.rs

Co-authored-by: joe petrowski <[email protected]>

* Fix

* Fixes

* Fixes

* Move some salary traits stuff to a shared location

* Initial draft

* Comment out bits

* Fix

* First couple of tests

* One more test

* Update frame/salary/src/lib.rs

Co-authored-by: Oliver Tale-Yazdi <[email protected]>

* Update frame/salary/src/lib.rs

Co-authored-by: Oliver Tale-Yazdi <[email protected]>

* Mul floor

* Tests

* Mul floor

* Fix warnings

* Fix test

* Tests

* Last tests

* Docs

* Fix warnings

* Benchmarks

* Weights

* Integrate benchmark

* Fixes

* Fix

* ".git/.scripts/commands/bench/bench.sh" pallet dev pallet_core_fellowship

* Better process flow

* Fix benchmarks & tests

* Docs

* Fixes

* Fixes

* docs

* ".git/.scripts/commands/bench/bench.sh" pallet dev pallet_core_fellowship

* Docs and allow custom evidence size

* ".git/.scripts/commands/bench/bench.sh" pallet dev pallet_core_fellowship

* Update frame/core-fellowship/src/lib.rs

Co-authored-by: Kian Paimani <[email protected]>

* Update frame/core-fellowship/src/tests.rs

Co-authored-by: Kian Paimani <[email protected]>

* Update frame/core-fellowship/src/benchmarking.rs

* Update frame/core-fellowship/src/benchmarking.rs

* Apply suggestions from code review

* Rename

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: joe petrowski <[email protected]>

* Reduce magic numbers

* Update frame/core-fellowship/src/lib.rs

Co-authored-by: Oliver Tale-Yazdi <[email protected]>

* Update frame/core-fellowship/src/lib.rs

Co-authored-by: Oliver Tale-Yazdi <[email protected]>

* Benchmark result

* Remove dependency

* set_params should pay

* induct should pay

* Remove some other free calls

---------

Co-authored-by: command-bot <>
Co-authored-by: Jegor Sidorenko <[email protected]>
Co-authored-by: joe petrowski <[email protected]>
Co-authored-by: Oliver Tale-Yazdi <[email protected]>
Co-authored-by: Kian Paimani <[email protected]>
  • Loading branch information
5 people authored Mar 11, 2023
1 parent 8f9b074 commit 9d34fb0
Show file tree
Hide file tree
Showing 14 changed files with 1,716 additions and 7 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ members = [
"frame/contracts/proc-macro",
"frame/contracts/primitives",
"frame/conviction-voting",
"frame/core-fellowship",
"frame/democracy",
"frame/fast-unstake",
"frame/try-runtime",
Expand Down
4 changes: 4 additions & 0 deletions bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pallet-collective = { version = "4.0.0-dev", default-features = false, path = ".
pallet-contracts = { version = "4.0.0-dev", default-features = false, path = "../../../frame/contracts" }
pallet-contracts-primitives = { version = "7.0.0", default-features = false, path = "../../../frame/contracts/primitives/" }
pallet-conviction-voting = { version = "4.0.0-dev", default-features = false, path = "../../../frame/conviction-voting" }
pallet-core-fellowship = { version = "4.0.0-dev", default-features = false, path = "../../../frame/core-fellowship" }
pallet-democracy = { version = "4.0.0-dev", default-features = false, path = "../../../frame/democracy" }
pallet-election-provider-multi-phase = { version = "4.0.0-dev", default-features = false, path = "../../../frame/election-provider-multi-phase" }
pallet-election-provider-support-benchmarking = { version = "4.0.0-dev", default-features = false, path = "../../../frame/election-provider-support/benchmarking", optional = true }
Expand Down Expand Up @@ -148,6 +149,7 @@ std = [
"pallet-contracts/std",
"pallet-contracts-primitives/std",
"pallet-conviction-voting/std",
"pallet-core-fellowship/std",
"pallet-democracy/std",
"pallet-elections-phragmen/std",
"pallet-fast-unstake/std",
Expand Down Expand Up @@ -235,6 +237,7 @@ runtime-benchmarks = [
"pallet-collective/runtime-benchmarks",
"pallet-contracts/runtime-benchmarks",
"pallet-conviction-voting/runtime-benchmarks",
"pallet-core-fellowship/runtime-benchmarks",
"pallet-democracy/runtime-benchmarks",
"pallet-election-provider-multi-phase/runtime-benchmarks",
"pallet-election-provider-support-benchmarking/runtime-benchmarks",
Expand Down Expand Up @@ -294,6 +297,7 @@ try-runtime = [
"pallet-collective/try-runtime",
"pallet-contracts/try-runtime",
"pallet-conviction-voting/try-runtime",
"pallet-core-fellowship/try-runtime",
"pallet-democracy/try-runtime",
"pallet-election-provider-multi-phase/try-runtime",
"pallet-elections-phragmen/try-runtime",
Expand Down
14 changes: 14 additions & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1574,6 +1574,18 @@ impl pallet_salary::Config for Runtime {
type Budget = Budget;
}

impl pallet_core_fellowship::Config for Runtime {
type WeightInfo = ();
type RuntimeEvent = RuntimeEvent;
type Members = RankedCollective;
type Balance = Balance;
type ParamsOrigin = frame_system::EnsureRoot<AccountId>;
type InductOrigin = pallet_core_fellowship::EnsureInducted<Runtime, (), 1>;
type ApproveOrigin = frame_system::EnsureRootWithSuccess<AccountId, ConstU16<9>>;
type PromoteOrigin = frame_system::EnsureRootWithSuccess<AccountId, ConstU16<9>>;
type EvidenceSize = ConstU32<16_384>;
}

parameter_types! {
pub Features: PalletFeatures = PalletFeatures::all_enabled();
pub const MaxAttributesPerCall: u32 = 10;
Expand Down Expand Up @@ -1770,6 +1782,7 @@ construct_runtime!(
Uniques: pallet_uniques,
Nfts: pallet_nfts,
Salary: pallet_salary,
CoreFellowship: pallet_core_fellowship,
TransactionStorage: pallet_transaction_storage,
VoterList: pallet_bags_list::<Instance1>,
StateTrieMigration: pallet_state_trie_migration,
Expand Down Expand Up @@ -1866,6 +1879,7 @@ mod benches {
[pallet_collective, Council]
[pallet_conviction_voting, ConvictionVoting]
[pallet_contracts, Contracts]
[pallet_core_fellowship, CoreFellowship]
[pallet_democracy, Democracy]
[pallet_election_provider_multi_phase, ElectionProviderMultiPhase]
[pallet_election_provider_support_benchmarking, EPSBench::<Runtime>]
Expand Down
55 changes: 55 additions & 0 deletions frame/core-fellowship/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
[package]
name = "pallet-core-fellowship"
version = "4.0.0-dev"
authors = ["Parity Technologies <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
homepage = "https://substrate.io"
repository = "https://github.com/paritytech/substrate/"
description = "Logic as per the description of The Fellowship for core Polkadot technology"
readme = "README.md"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
log = { version = "0.4.16", default-features = false }
scale-info = { version = "2.0.1", default-features = false, features = ["derive"] }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../benchmarking" }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
sp-arithmetic = { version = "6.0.0", default-features = false, path = "../../primitives/arithmetic" }
sp-core = { version = "7.0.0", default-features = false, path = "../../primitives/core" }
sp-io = { version = "7.0.0", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "7.0.0", default-features = false, path = "../../primitives/runtime" }
sp-std = { version = "5.0.0", default-features = false, path = "../../primitives/std" }

[dev-dependencies]
pallet-ranked-collective = { version = "4.0.0-dev", default-features = false, path = "../ranked-collective" }
pallet-salary = { version = "4.0.0-dev", default-features = false, path = "../salary" }

[features]
default = ["std"]
std = [
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"log/std",
"scale-info/std",
"sp-arithmetic/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"pallet-ranked-collective/runtime-benchmarks",
"pallet-salary/runtime-benchmarks",
]
try-runtime = ["frame-support/try-runtime"]
3 changes: 3 additions & 0 deletions frame/core-fellowship/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Core Fellowship

Logic specific to the core Polkadot Fellowship.
216 changes: 216 additions & 0 deletions frame/core-fellowship/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
// This file is part of Substrate.

// Copyright (C) 2020-2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Salary pallet benchmarking.

#![cfg(feature = "runtime-benchmarks")]

use super::*;
use crate::Pallet as CoreFellowship;

use frame_benchmarking::v2::*;
use frame_system::RawOrigin;
use sp_arithmetic::traits::Bounded;

const SEED: u32 = 0;

type BenchResult = Result<(), BenchmarkError>;

#[instance_benchmarks]
mod benchmarks {
use super::*;

fn ensure_evidence<T: Config<I>, I: 'static>(who: &T::AccountId) -> BenchResult {
let evidence = BoundedVec::try_from(vec![0; Evidence::<T, I>::bound()]).unwrap();
let wish = Wish::Retention;
let origin = RawOrigin::Signed(who.clone()).into();
CoreFellowship::<T, I>::submit_evidence(origin, wish, evidence)?;
assert!(MemberEvidence::<T, I>::contains_key(who));
Ok(())
}

fn make_member<T: Config<I>, I: 'static>(rank: u16) -> Result<T::AccountId, BenchmarkError> {
let member = account("member", 0, SEED);
T::Members::induct(&member)?;
for _ in 0..rank {
T::Members::promote(&member)?;
}
CoreFellowship::<T, I>::import(RawOrigin::Signed(member.clone()).into())?;
Ok(member)
}

#[benchmark]
fn set_params() -> Result<(), BenchmarkError> {
let params = ParamsType {
active_salary: [100u32.into(); 9],
passive_salary: [10u32.into(); 9],
demotion_period: [100u32.into(); 9],
min_promotion_period: [100u32.into(); 9],
offboard_timeout: 1u32.into(),
};

#[extrinsic_call]
_(RawOrigin::Root, Box::new(params.clone()));

assert_eq!(Params::<T, I>::get(), params);
Ok(())
}

#[benchmark]
fn bump_offboard() -> Result<(), BenchmarkError> {
let member = make_member::<T, I>(0)?;

// Set it to the max value to ensure that any possible auto-demotion period has passed.
frame_system::Pallet::<T>::set_block_number(T::BlockNumber::max_value());
ensure_evidence::<T, I>(&member)?;
assert!(Member::<T, I>::contains_key(&member));

#[extrinsic_call]
CoreFellowship::<T, I>::bump(RawOrigin::Signed(member.clone()), member.clone());

assert!(!Member::<T, I>::contains_key(&member));
assert!(!MemberEvidence::<T, I>::contains_key(&member));
Ok(())
}

#[benchmark]
fn bump_demote() -> Result<(), BenchmarkError> {
let member = make_member::<T, I>(2)?;

// Set it to the max value to ensure that any possible auto-demotion period has passed.
frame_system::Pallet::<T>::set_block_number(T::BlockNumber::max_value());
ensure_evidence::<T, I>(&member)?;
assert!(Member::<T, I>::contains_key(&member));
assert_eq!(T::Members::rank_of(&member), Some(2));

#[extrinsic_call]
CoreFellowship::<T, I>::bump(RawOrigin::Signed(member.clone()), member.clone());

assert!(Member::<T, I>::contains_key(&member));
assert_eq!(T::Members::rank_of(&member), Some(1));
assert!(!MemberEvidence::<T, I>::contains_key(&member));
Ok(())
}

#[benchmark]
fn set_active() -> Result<(), BenchmarkError> {
let member = make_member::<T, I>(1)?;
assert!(Member::<T, I>::get(&member).unwrap().is_active);

#[extrinsic_call]
_(RawOrigin::Signed(member.clone()), false);

assert!(!Member::<T, I>::get(&member).unwrap().is_active);
Ok(())
}

#[benchmark]
fn induct() -> Result<(), BenchmarkError> {
let candidate: T::AccountId = account("candidate", 0, SEED);

#[extrinsic_call]
_(RawOrigin::Root, candidate.clone());

assert_eq!(T::Members::rank_of(&candidate), Some(0));
assert!(Member::<T, I>::contains_key(&candidate));
Ok(())
}

#[benchmark]
fn promote() -> Result<(), BenchmarkError> {
let member = make_member::<T, I>(1)?;
ensure_evidence::<T, I>(&member)?;

#[extrinsic_call]
_(RawOrigin::Root, member.clone(), 2u8.into());

assert_eq!(T::Members::rank_of(&member), Some(2));
assert!(!MemberEvidence::<T, I>::contains_key(&member));
Ok(())
}

#[benchmark]
fn offboard() -> Result<(), BenchmarkError> {
let member = make_member::<T, I>(0)?;
T::Members::demote(&member)?;
ensure_evidence::<T, I>(&member)?;

assert!(T::Members::rank_of(&member).is_none());
assert!(Member::<T, I>::contains_key(&member));
assert!(MemberEvidence::<T, I>::contains_key(&member));

#[extrinsic_call]
_(RawOrigin::Signed(member.clone()), member.clone());

assert!(!Member::<T, I>::contains_key(&member));
assert!(!MemberEvidence::<T, I>::contains_key(&member));
Ok(())
}

#[benchmark]
fn import() -> Result<(), BenchmarkError> {
let member = account("member", 0, SEED);
T::Members::induct(&member)?;
T::Members::promote(&member)?;

assert!(!Member::<T, I>::contains_key(&member));

#[extrinsic_call]
_(RawOrigin::Signed(member.clone()));

assert!(Member::<T, I>::contains_key(&member));
Ok(())
}

#[benchmark]
fn approve() -> Result<(), BenchmarkError> {
let member = make_member::<T, I>(1)?;
let then = frame_system::Pallet::<T>::block_number();
let now = then.saturating_plus_one();
frame_system::Pallet::<T>::set_block_number(now);
ensure_evidence::<T, I>(&member)?;

assert_eq!(Member::<T, I>::get(&member).unwrap().last_proof, then);

#[extrinsic_call]
_(RawOrigin::Root, member.clone(), 1u8.into());

assert_eq!(Member::<T, I>::get(&member).unwrap().last_proof, now);
assert!(!MemberEvidence::<T, I>::contains_key(&member));
Ok(())
}

#[benchmark]
fn submit_evidence() -> Result<(), BenchmarkError> {
let member = make_member::<T, I>(1)?;
let evidence = vec![0; Evidence::<T, I>::bound()].try_into().unwrap();

assert!(!MemberEvidence::<T, I>::contains_key(&member));

#[extrinsic_call]
_(RawOrigin::Signed(member.clone()), Wish::Retention, evidence);

assert!(MemberEvidence::<T, I>::contains_key(&member));
Ok(())
}

impl_benchmark_test_suite! {
CoreFellowship,
crate::tests::new_test_ext(),
crate::tests::Test,
}
}
Loading

0 comments on commit 9d34fb0

Please sign in to comment.