Skip to content

Commit

Permalink
Add pallet-alliance into node runtime
Browse files Browse the repository at this point in the history
Signed-off-by: koushiro <koushiro.cqx@gmail.com>
  • Loading branch information
koushiro committed Sep 18, 2021
1 parent 179b4cc commit 4a8ad01
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 43 deletions.
9 changes: 5 additions & 4 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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 @@ -169,14 +170,16 @@ std = [
"log/std",
"frame-try-runtime/std",
"sp-npos-elections/std",
"sp-io/std"
"sp-io/std",
"pallet-alliance/std",
]
runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-election-provider-multi-phase/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"pallet-alliance/runtime-benchmarks",
"pallet-assets/runtime-benchmarks",
"pallet-babe/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
Expand Down Expand Up @@ -214,6 +217,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
72 changes: 70 additions & 2 deletions bin/node/runtime/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@

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

use crate::{Authorship, Balances, NegativeImbalance};
use frame_support::traits::{Currency, OnUnbalanced};
use crate::{AllianceMotion, Authorship, Balances, Call, Identity, NegativeImbalance};
use frame_support::{
dispatch::{DispatchError, DispatchResultWithPostInfo},
traits::{Currency, OnUnbalanced},
weights::Weight,
};
use node_primitives::{AccountId, Hash};
use pallet_alliance::{IdentityVerifier, ProposalIndex, ProposalProvider};
use pallet_identity::Judgement;

pub struct Author;
impl OnUnbalanced<NegativeImbalance> for Author {
Expand All @@ -27,6 +34,67 @@ impl OnUnbalanced<NegativeImbalance> for Author {
}
}

pub struct AllianceIdentityVerifier;
impl IdentityVerifier<AccountId> for AllianceIdentityVerifier {
fn super_account_id(who: &AccountId) -> Option<AccountId> {
Identity::super_of(who).map(|parent| parent.0)
}

fn has_identity(who: &AccountId, fields: u64) -> bool {
Identity::has_identity(who, fields)
}

fn has_good_judgement(who: &AccountId) -> bool {
if let Some(judgements) =
Identity::identity(who).map(|registration| registration.judgements)
{
judgements
.iter()
.filter(|(_, j)| Judgement::KnownGood == *j || Judgement::Reasonable == *j)
.count() > 0
} else {
false
}
}
}

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

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
45 changes: 44 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub use sp_runtime::BuildStorage;

/// Implementations of some helper traits passed into runtime modules as associated types.
pub mod impls;
use impls::Author;
use impls::{AllianceIdentityVerifier, AllianceProposalProvider, Author};

/// Constant values used within the runtime.
pub mod constants;
Expand Down Expand Up @@ -1208,6 +1208,46 @@ impl pallet_transaction_storage::Config for Runtime {
type WeightInfo = pallet_transaction_storage::weights::SubstrateWeight<Runtime>;
}

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 MaxBlacklistCount: u32 = 100;
}

impl pallet_alliance::Config for Runtime {
type Event = Event;
type Proposal = Call;
type SuperMajorityOrigin = EnsureOneOf<
AccountId,
EnsureRoot<AccountId>,
pallet_collective::EnsureProportionMoreThan<_2, _3, AccountId, AllianceCollective>,
>;
type Currency = Balances;
type InitializeMembers = AllianceMotion;
type MembershipChanged = AllianceMotion;
type Slashed = Treasury;
type IdentityVerifier = AllianceIdentityVerifier;
type ProposalProvider = AllianceProposalProvider;
type MaxBlacklistCount = MaxBlacklistCount;
type CandidateDeposit = CandidateDeposit;
}

construct_runtime!(
pub enum Runtime where
Block = Block,
Expand Down Expand Up @@ -1254,6 +1294,8 @@ construct_runtime!(
Gilt: pallet_gilt::{Pallet, Call, Storage, Event<T>, Config},
Uniques: pallet_uniques::{Pallet, Call, Storage, Event<T>},
TransactionStorage: pallet_transaction_storage::{Pallet, Call, Storage, Inherent, Config<T>, Event<T>},
AllianceMotion: pallet_collective::<Instance3>::{Pallet, Storage, Origin<T>, Event<T>},
Alliance: pallet_alliance::{Pallet, Call, Storage, Event<T>},
}
);

Expand Down Expand Up @@ -1683,6 +1725,7 @@ impl_runtime_apis! {
add_benchmark!(params, batches, pallet_uniques, Uniques);
add_benchmark!(params, batches, pallet_utility, Utility);
add_benchmark!(params, batches, pallet_vesting, Vesting);
add_benchmark!(params, batches, pallet_alliance, Alliance);

if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }
Ok(batches)
Expand Down
39 changes: 17 additions & 22 deletions frame/alliance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,12 @@ type NegativeImbalanceOf<T, I = ()> = <<T as Config<I>>::Currency as Currency<
<T as frame_system::Config>::AccountId,
>>::NegativeImbalance;

const IDENTITY_FIELD_DISPLAY: u64 =
0b0000000000000000000000000000000000000000000000000000000000000001;
const IDENTITY_FIELD_WEB: u64 = 0b0000000000000000000000000000000000000000000000000000000000000100;

pub trait IdentityVerifier<AccountId: Clone + Ord> {
fn super_account_id(who: &AccountId) -> Option<AccountId>;
fn has_identity(who: &AccountId, fields: u64) -> bool;

fn verify_identity(who: &AccountId, fields: u64) -> bool;
fn has_good_judgement(who: &AccountId) -> bool;

fn verify_judgement(who: &AccountId) -> bool;
fn super_account_id(who: &AccountId) -> Option<AccountId>;
}

pub trait ProposalProvider<AccountId, Hash, Proposal> {
Expand Down Expand Up @@ -276,12 +272,10 @@ pub mod pallet {
KickingMember,
/// Balance is insufficient to be a candidate.
InsufficientCandidateFunds,
/// The account's identity has not been judged.
WithoutVerifiedIdentity,
/// The account's identity has not display field.
WithoutIdentityDisplay,
/// The account' identity has not website field.
WithoutIdentityWebsite,
/// The account's identity has not display field and website field.
WithoutIdentityDisplayAndWebsite,
/// The account's identity has no good judgement.
WithoutGoodIdentityJudgement,
/// The proposal hash is not found.
MissingProposalHash,
/// The proposal is not vetoable.
Expand Down Expand Up @@ -934,18 +928,19 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
}

fn has_identity(who: &T::AccountId) -> DispatchResult {
let judgement = |w: &T::AccountId| -> DispatchResult {
ensure!(
T::IdentityVerifier::verify_identity(w, IDENTITY_FIELD_DISPLAY),
Error::<T, I>::WithoutIdentityDisplay
);
const IDENTITY_FIELD_DISPLAY: u64 =
0b0000000000000000000000000000000000000000000000000000000000000001;
const IDENTITY_FIELD_WEB: u64 =
0b0000000000000000000000000000000000000000000000000000000000000100;

let judgement = |who: &T::AccountId| -> DispatchResult {
ensure!(
T::IdentityVerifier::verify_judgement(w),
Error::<T, I>::WithoutVerifiedIdentity
T::IdentityVerifier::has_identity(who, IDENTITY_FIELD_DISPLAY | IDENTITY_FIELD_WEB),
Error::<T, I>::WithoutIdentityDisplayAndWebsite
);
ensure!(
T::IdentityVerifier::verify_identity(w, IDENTITY_FIELD_WEB),
Error::<T, I>::WithoutIdentityWebsite
T::IdentityVerifier::has_good_judgement(who),
Error::<T, I>::WithoutGoodIdentityJudgement
);
Ok(())
};
Expand Down
10 changes: 5 additions & 5 deletions frame/alliance/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ impl pallet_collective::Config<AllianceCollective> for Test {

pub struct AllianceIdentityVerifier;
impl IdentityVerifier<u64> for AllianceIdentityVerifier {
fn super_account_id(_who: &u64) -> Option<u64> {
None
fn has_identity(_who: &u64, _fields: u64) -> bool {
true
}

fn verify_identity(_who: &u64, _field: u64) -> bool {
fn has_good_judgement(_who: &u64) -> bool {
true
}

fn verify_judgement(_who: &u64) -> bool {
true
fn super_account_id(_who: &u64) -> Option<u64> {
None
}
}

Expand Down
2 changes: 1 addition & 1 deletion frame/identity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { package = "parity-scale-codec", version = "2.2.0", default-features = false, features = ["derive", "max-encoded-len"] }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
enumflags2 = { version = "0.6.2" }
enumflags2 = { version = "0.7.1" }
sp-std = { version = "4.0.0-dev", default-features = false, path = "../../primitives/std" }
sp-io = { version = "4.0.0-dev", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "4.0.0-dev", default-features = false, path = "../../primitives/runtime" }
Expand Down
19 changes: 14 additions & 5 deletions frame/identity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ pub mod weights;
use frame_support::traits::{BalanceStatus, Currency, OnUnbalanced, ReservableCurrency};
use sp_runtime::traits::{AppendZerosInput, Saturating, StaticLookup, Zero};
use sp_std::{convert::TryInto, prelude::*};
pub use weights::WeightInfo;

pub use pallet::*;
pub use types::{
Data, IdentityField, IdentityFields, IdentityInfo, Judgement, RegistrarIndex, RegistrarInfo,
Registration,
};
pub use weights::WeightInfo;

type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
Expand Down Expand Up @@ -160,7 +160,7 @@ pub mod pallet {
/// TWOX-NOTE: OK ― `AccountId` is a secure hash.
#[pallet::storage]
#[pallet::getter(fn identity)]
pub(super) type IdentityOf<T: Config> = StorageMap<
pub type IdentityOf<T: Config> = StorageMap<
_,
Twox64Concat,
T::AccountId,
Expand All @@ -172,7 +172,7 @@ pub mod pallet {
/// context. If the account is not some other account's sub-identity, then just `None`.
#[pallet::storage]
#[pallet::getter(fn super_of)]
pub(super) type SuperOf<T: Config> =
pub type SuperOf<T: Config> =
StorageMap<_, Blake2_128Concat, T::AccountId, (T::AccountId, Data), OptionQuery>;

/// Alternative "sub" identities of this account.
Expand All @@ -182,7 +182,7 @@ pub mod pallet {
/// TWOX-NOTE: OK ― `AccountId` is a secure hash.
#[pallet::storage]
#[pallet::getter(fn subs_of)]
pub(super) type SubsOf<T: Config> = StorageMap<
pub type SubsOf<T: Config> = StorageMap<
_,
Twox64Concat,
T::AccountId,
Expand All @@ -196,7 +196,7 @@ pub mod pallet {
/// The index into this can be cast to `RegistrarIndex` to get a valid value.
#[pallet::storage]
#[pallet::getter(fn registrars)]
pub(super) type Registrars<T: Config> = StorageValue<
pub type Registrars<T: Config> = StorageValue<
_,
BoundedVec<Option<RegistrarInfo<BalanceOf<T>, T::AccountId>>, T::MaxRegistrars>,
ValueQuery,
Expand Down Expand Up @@ -970,4 +970,13 @@ impl<T: Config> Pallet<T> {
.filter_map(|a| SuperOf::<T>::get(&a).map(|x| (a, x.1)))
.collect()
}

/// Check if the account has corresponding identity information by the identity field.
pub fn has_identity(who: &T::AccountId, fields: u64) -> bool {
if let Some(info) = IdentityOf::<T>::get(who).map(|registration| registration.info) {
(info.fields().0.bits() & fields) == 1
} else {
false
}
}
}
Loading

0 comments on commit 4a8ad01

Please sign in to comment.