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

Commit

Permalink
pallet alliance (paritytech#11112)
Browse files Browse the repository at this point in the history
* Add pallet-alliance

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Update multihash/cid and format

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Remove useless deps

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Add pallet-alliance into node runtime

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Fix has_identity

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Add TooMantBlacklist Error

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Add TooLongWebsiteUrlLength Error

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Add remove_announcement

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Derive pallet_identity::Config and Fix test/benchmarking

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Add part weight for call of pallet-alliance

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Remove pallet_identity Config trait

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Fix propose arguments

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Some nits

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* cargo fmt

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Fix benchmarking of add_blacklist/remove_blacklist

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Some nits

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Add benchmarking for init_members

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Add benchmarking for propose/vote/veto

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Fix benchmarking

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Remove some useless

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* fix some compile issue

* more fix

* all checks

* refactor

* refactor event

* refactor

* cleanup

* cleanup

* fix benchmarking

* fix warning

* fmt

* fix benchmarks

* with storage info

* fmt

* add new config

* fix benchmarks

* fix

* fmt

* Apply suggestions from code review

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* improvements

* fix

* update docs

* rename events, errors, and functions

* make kicking events clearer

* fix

* fix tests

* fix tests

* fix runtime

* fix build

* remove unneeded change

* remove Candidate role from Alliance

* fmt grumbles

* update lock

* update recursion limit

* benchmarks

* convert-try

* benchmark assertions

* fmt ,

* cargo lock

* Update frame/alliance/src/benchmarking.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* add docs to public interfaces

* Apply suggestions from code review

Co-authored-by: Squirrel <gilescope@gmail.com>

* fix build (node)

* review

* use EitherOfDiverse

* update cargo toml

* make all dispatch class Normal

* change blacklist to unscrupulous

* formatting

* fmt oops

* rename benchmarking unscrupulous account creator

Co-authored-by: koushiro <koushiro.cqx@gmail.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: joepetrowski <joe@parity.io>
Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>
Co-authored-by: Squirrel <gilescope@gmail.com>
  • Loading branch information
6 people authored and DaviRain-Su committed Aug 23, 2022
1 parent 2b1b010 commit 7fffee6
Show file tree
Hide file tree
Showing 16 changed files with 3,745 additions and 165 deletions.
23 changes: 23 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 @@ -65,6 +65,7 @@ members = [
"client/transaction-pool",
"client/transaction-pool/api",
"client/utils",
"frame/alliance",
"frame/assets",
"frame/atomic-swap",
"frame/aura",
Expand Down
2 changes: 2 additions & 0 deletions bin/node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ pub fn testnet_genesis(
gilt: Default::default(),
transaction_storage: Default::default(),
transaction_payment: Default::default(),
alliance: Default::default(),
alliance_motion: Default::default(),
nomination_pools: Default::default(),
}
}
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 @@ -50,6 +50,7 @@ frame-system-benchmarking = { version = "4.0.0-dev", default-features = false, p
frame-election-provider-support = { version = "4.0.0-dev", default-features = false, path = "../../../frame/election-provider-support" }
frame-system-rpc-runtime-api = { version = "4.0.0-dev", default-features = false, path = "../../../frame/system/rpc/runtime-api/" }
frame-try-runtime = { version = "0.10.0-dev", default-features = false, path = "../../../frame/try-runtime", optional = true }
pallet-alliance = { version = "4.0.0-dev", default-features = false, path = "../../../frame/alliance" }
pallet-assets = { version = "4.0.0-dev", default-features = false, path = "../../../frame/assets" }
pallet-authority-discovery = { version = "4.0.0-dev", default-features = false, path = "../../../frame/authority-discovery" }
pallet-authorship = { version = "4.0.0-dev", default-features = false, path = "../../../frame/authorship" }
Expand Down Expand Up @@ -187,12 +188,14 @@ std = [
"frame-try-runtime/std",
"sp-io/std",
"pallet-child-bounties/std",
"pallet-alliance/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"pallet-alliance/runtime-benchmarks",
"pallet-assets/runtime-benchmarks",
"pallet-babe/runtime-benchmarks",
"pallet-bags-list/runtime-benchmarks",
Expand Down Expand Up @@ -243,6 +246,7 @@ try-runtime = [
"frame-executive/try-runtime",
"frame-try-runtime",
"frame-system/try-runtime",
"pallet-alliance/try-runtime",
"pallet-assets/try-runtime",
"pallet-authority-discovery/try-runtime",
"pallet-authorship/try-runtime",
Expand Down
77 changes: 73 additions & 4 deletions bin/node/runtime/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@

//! Some configurable implementations as associated type for the substrate runtime.

use crate::{AccountId, Assets, Authorship, Balances, NegativeImbalance, Runtime};
use frame_support::traits::{
fungibles::{Balanced, CreditOf},
Currency, OnUnbalanced,
use crate::{
AccountId, AllianceMotion, Assets, Authorship, Balances, Call, Hash, NegativeImbalance, Runtime,
};
use frame_support::{
pallet_prelude::*,
traits::{
fungibles::{Balanced, CreditOf},
Currency, OnUnbalanced,
},
};
use pallet_alliance::{IdentityVerifier, ProposalIndex, ProposalProvider};
use pallet_asset_tx_payment::HandleCredit;
use sp_std::prelude::*;

pub struct Author;
impl OnUnbalanced<NegativeImbalance> for Author {
Expand All @@ -45,6 +52,68 @@ impl HandleCredit<AccountId, Assets> for CreditToBlockAuthor {
}
}

pub struct AllianceIdentityVerifier;
impl IdentityVerifier<AccountId> for AllianceIdentityVerifier {
fn has_identity(who: &AccountId, fields: u64) -> bool {
crate::Identity::has_identity(who, fields)
}

fn has_good_judgement(who: &AccountId) -> bool {
use pallet_identity::Judgement;
if let Some(judgements) =
crate::Identity::identity(who).map(|registration| registration.judgements)
{
judgements
.iter()
.any(|(_, j)| matches!(j, Judgement::KnownGood | Judgement::Reasonable))
} else {
false
}
}

fn super_account_id(who: &AccountId) -> Option<AccountId> {
crate::Identity::super_of(who).map(|parent| parent.0)
}
}

pub struct AllianceProposalProvider;
impl ProposalProvider<AccountId, Hash, Call> for AllianceProposalProvider {
fn propose_proposal(
who: AccountId,
threshold: u32,
proposal: Box<Call>,
length_bound: u32,
) -> Result<(u32, u32), DispatchError> {
AllianceMotion::do_propose_proposed(who, threshold, proposal, length_bound)
}

fn vote_proposal(
who: AccountId,
proposal: Hash,
index: ProposalIndex,
approve: bool,
) -> Result<bool, DispatchError> {
AllianceMotion::do_vote(who, proposal, index, approve)
}

fn veto_proposal(proposal_hash: Hash) -> u32 {
AllianceMotion::do_disapprove_proposal(proposal_hash)
}

fn close_proposal(
proposal_hash: Hash,
proposal_index: ProposalIndex,
proposal_weight_bound: Weight,
length_bound: u32,
) -> DispatchResultWithPostInfo {
AllianceMotion::do_close(proposal_hash, proposal_index, proposal_weight_bound, length_bound)
}

fn proposal_of(proposal_hash: Hash) -> Option<Call> {
AllianceMotion::proposal_of(proposal_hash)
}
}

#[cfg(test)]
mod multiplier_tests {
use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment};
Expand Down
70 changes: 67 additions & 3 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! The Substrate runtime. This can be compiled with `#[no_std]`, ready for Wasm.

#![cfg_attr(not(feature = "std"), no_std)]
// `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256.
// `construct_runtime!` does a lot of recursion and requires us to increase the limit to 512.
#![recursion_limit = "512"]

use codec::{Decode, Encode, MaxEncodedLen};
Expand Down Expand Up @@ -90,7 +90,9 @@ pub use sp_runtime::BuildStorage;

/// Implementations of some helper traits passed into runtime modules as associated types.
pub mod impls;
use impls::{Author, CreditToBlockAuthor};
#[cfg(not(feature = "runtime-benchmarks"))]
use impls::AllianceIdentityVerifier;
use impls::{AllianceProposalProvider, Author, CreditToBlockAuthor};

/// Constant values used within the runtime.
pub mod constants;
Expand Down Expand Up @@ -1506,6 +1508,67 @@ impl pallet_state_trie_migration::Config for Runtime {
type WeightInfo = ();
}

parameter_types! {
pub const AllianceMotionDuration: BlockNumber = 5 * DAYS;
pub const AllianceMaxProposals: u32 = 100;
pub const AllianceMaxMembers: u32 = 100;
}

type AllianceCollective = pallet_collective::Instance3;
impl pallet_collective::Config<AllianceCollective> for Runtime {
type Origin = Origin;
type Proposal = Call;
type Event = Event;
type MotionDuration = AllianceMotionDuration;
type MaxProposals = AllianceMaxProposals;
type MaxMembers = AllianceMaxMembers;
type DefaultVote = pallet_collective::PrimeDefaultVote;
type WeightInfo = pallet_collective::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
pub const MaxFounders: u32 = 10;
pub const MaxFellows: u32 = AllianceMaxMembers::get() - MaxFounders::get();
pub const MaxAllies: u32 = 100;
pub const AllyDeposit: Balance = 10 * DOLLARS;
}

impl pallet_alliance::Config for Runtime {
type Event = Event;
type Proposal = Call;
type AdminOrigin = EitherOfDiverse<
EnsureRoot<AccountId>,
pallet_collective::EnsureProportionMoreThan<AccountId, AllianceCollective, 2, 3>,
>;
type MembershipManager = EitherOfDiverse<
EnsureRoot<AccountId>,
pallet_collective::EnsureProportionMoreThan<AccountId, AllianceCollective, 2, 3>,
>;
type AnnouncementOrigin = EitherOfDiverse<
EnsureRoot<AccountId>,
pallet_collective::EnsureProportionMoreThan<AccountId, AllianceCollective, 2, 3>,
>;
type Currency = Balances;
type Slashed = Treasury;
type InitializeMembers = AllianceMotion;
type MembershipChanged = AllianceMotion;
#[cfg(not(feature = "runtime-benchmarks"))]
type IdentityVerifier = AllianceIdentityVerifier;
#[cfg(feature = "runtime-benchmarks")]
type IdentityVerifier = ();
type ProposalProvider = AllianceProposalProvider;
type MaxProposals = AllianceMaxProposals;
type MaxFounders = MaxFounders;
type MaxFellows = MaxFellows;
type MaxAllies = MaxAllies;
type MaxUnscrupulousItems = ConstU32<100>;
type MaxWebsiteUrlLength = ConstU32<255>;
type MaxAnnouncementsCount = ConstU32<100>;
type MaxMembersCount = AllianceMaxMembers;
type AllyDeposit = AllyDeposit;
type WeightInfo = pallet_alliance::weights::SubstrateWeight<Runtime>;
}

construct_runtime!(
pub enum Runtime where
Block = Block,
Expand Down Expand Up @@ -1563,6 +1626,8 @@ construct_runtime!(
Remark: pallet_remark,
ConvictionVoting: pallet_conviction_voting,
Whitelist: pallet_whitelist,
AllianceMotion: pallet_collective::<Instance3>,
Alliance: pallet_alliance,
NominationPools: pallet_nomination_pools,
RankedPolls: pallet_referenda::<Instance2>,
RankedCollective: pallet_ranked_collective,
Expand Down Expand Up @@ -2049,7 +2114,6 @@ impl_runtime_apis! {
let mut batches = Vec::<BenchmarkBatch>::new();
let params = (&config, &whitelist);
add_benchmarks!(params, batches);

Ok(batches)
}
}
Expand Down
2 changes: 2 additions & 0 deletions bin/node/testing/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ pub fn config_endowed(code: Option<&[u8]>, extra_endowed: Vec<AccountId>) -> Gen
gilt: Default::default(),
transaction_storage: Default::default(),
transaction_payment: Default::default(),
alliance: Default::default(),
alliance_motion: Default::default(),
nomination_pools: Default::default(),
}
}
68 changes: 68 additions & 0 deletions frame/alliance/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
[package]
name = "pallet-alliance"
version = "4.0.0-dev"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
license = "Apache-2.0"
homepage = "https://docs.substrate.io/"
repository = "https://github.com/paritytech/substrate/"
description = "The Alliance pallet provides a collective for standard-setting industry collaboration."
readme = "README.md"

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

[dependencies]
hex = { version = "0.4", default-features = false, features = ["alloc"], optional = true }
sha2 = { version = "0.10.1", default-features = false, optional = true }
log = { version = "0.4.14", default-features = false }

codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.0.1", default-features = false, features = ["derive"] }

sp-std = { version = "4.0.0", default-features = false, path = "../../primitives/std" }
sp-core = { version = "6.0.0", default-features = false, path = "../../primitives/core" }
sp-io = { version = "6.0.0", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "6.0.0", default-features = false, path = "../../primitives/runtime" }

frame-benchmarking = { version = "4.0.0-dev", default-features = false, path = "../benchmarking", optional = true }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }

pallet-identity = { version = "4.0.0-dev", path = "../identity", default-features = false }
pallet-collective = { version = "4.0.0-dev", path = "../collective", default-features = false, optional = true }

[dev-dependencies]
hex-literal = "0.3.1"
sha2 = "0.10.1"
pallet-balances = { version = "4.0.0-dev", path = "../balances" }
pallet-collective = { version = "4.0.0-dev", path = "../collective" }

[features]
default = ["std"]
std = [
"log/std",
"codec/std",
"scale-info/std",
"sp-std/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"frame-support/std",
"frame-system/std",
"pallet-identity/std",
]
runtime-benchmarks = [
"hex",
"sha2",
"frame-benchmarking",
"sp-runtime/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-collective/runtime-benchmarks",
"pallet-identity/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
]
Loading

0 comments on commit 7fffee6

Please sign in to comment.