Skip to content

Commit 4a8ad01

Browse files
committed
Add pallet-alliance into node runtime
Signed-off-by: koushiro <[email protected]>
1 parent 179b4cc commit 4a8ad01

File tree

9 files changed

+195
-43
lines changed

9 files changed

+195
-43
lines changed

Cargo.lock

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/node/runtime/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ frame-system-benchmarking = { version = "4.0.0-dev", default-features = false, p
5151
frame-election-provider-support = { version = "4.0.0-dev", default-features = false, path = "../../../frame/election-provider-support" }
5252
frame-system-rpc-runtime-api = { version = "4.0.0-dev", default-features = false, path = "../../../frame/system/rpc/runtime-api/" }
5353
frame-try-runtime = { version = "0.10.0-dev", default-features = false, path = "../../../frame/try-runtime", optional = true }
54+
pallet-alliance = { version = "4.0.0-dev", default-features = false, path = "../../../frame/alliance" }
5455
pallet-assets = { version = "4.0.0-dev", default-features = false, path = "../../../frame/assets" }
5556
pallet-authority-discovery = { version = "4.0.0-dev", default-features = false, path = "../../../frame/authority-discovery" }
5657
pallet-authorship = { version = "4.0.0-dev", default-features = false, path = "../../../frame/authorship" }
@@ -169,14 +170,16 @@ std = [
169170
"log/std",
170171
"frame-try-runtime/std",
171172
"sp-npos-elections/std",
172-
"sp-io/std"
173+
"sp-io/std",
174+
"pallet-alliance/std",
173175
]
174176
runtime-benchmarks = [
175177
"frame-benchmarking",
176178
"frame-support/runtime-benchmarks",
177179
"frame-system/runtime-benchmarks",
178180
"pallet-election-provider-multi-phase/runtime-benchmarks",
179181
"sp-runtime/runtime-benchmarks",
182+
"pallet-alliance/runtime-benchmarks",
180183
"pallet-assets/runtime-benchmarks",
181184
"pallet-babe/runtime-benchmarks",
182185
"pallet-balances/runtime-benchmarks",
@@ -214,6 +217,7 @@ try-runtime = [
214217
"frame-executive/try-runtime",
215218
"frame-try-runtime",
216219
"frame-system/try-runtime",
220+
"pallet-alliance/try-runtime",
217221
"pallet-assets/try-runtime",
218222
"pallet-authority-discovery/try-runtime",
219223
"pallet-authorship/try-runtime",

bin/node/runtime/src/impls.rs

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,15 @@
1717

1818
//! Some configurable implementations as associated type for the substrate runtime.
1919
20-
use crate::{Authorship, Balances, NegativeImbalance};
21-
use frame_support::traits::{Currency, OnUnbalanced};
20+
use crate::{AllianceMotion, Authorship, Balances, Call, Identity, NegativeImbalance};
21+
use frame_support::{
22+
dispatch::{DispatchError, DispatchResultWithPostInfo},
23+
traits::{Currency, OnUnbalanced},
24+
weights::Weight,
25+
};
26+
use node_primitives::{AccountId, Hash};
27+
use pallet_alliance::{IdentityVerifier, ProposalIndex, ProposalProvider};
28+
use pallet_identity::Judgement;
2229

2330
pub struct Author;
2431
impl OnUnbalanced<NegativeImbalance> for Author {
@@ -27,6 +34,67 @@ impl OnUnbalanced<NegativeImbalance> for Author {
2734
}
2835
}
2936

37+
pub struct AllianceIdentityVerifier;
38+
impl IdentityVerifier<AccountId> for AllianceIdentityVerifier {
39+
fn super_account_id(who: &AccountId) -> Option<AccountId> {
40+
Identity::super_of(who).map(|parent| parent.0)
41+
}
42+
43+
fn has_identity(who: &AccountId, fields: u64) -> bool {
44+
Identity::has_identity(who, fields)
45+
}
46+
47+
fn has_good_judgement(who: &AccountId) -> bool {
48+
if let Some(judgements) =
49+
Identity::identity(who).map(|registration| registration.judgements)
50+
{
51+
judgements
52+
.iter()
53+
.filter(|(_, j)| Judgement::KnownGood == *j || Judgement::Reasonable == *j)
54+
.count() > 0
55+
} else {
56+
false
57+
}
58+
}
59+
}
60+
61+
pub struct AllianceProposalProvider;
62+
impl ProposalProvider<AccountId, Hash, Call> for AllianceProposalProvider {
63+
fn propose_proposal(
64+
who: AccountId,
65+
threshold: u32,
66+
proposal: Call,
67+
) -> Result<u32, DispatchError> {
68+
AllianceMotion::do_propose(who, threshold, proposal)
69+
}
70+
71+
fn vote_proposal(
72+
who: AccountId,
73+
proposal: Hash,
74+
index: ProposalIndex,
75+
approve: bool,
76+
) -> Result<bool, DispatchError> {
77+
AllianceMotion::do_vote(who, proposal, index, approve)
78+
}
79+
80+
fn veto_proposal(proposal_hash: Hash) -> u32 {
81+
AllianceMotion::do_disapprove_proposal(proposal_hash)
82+
}
83+
84+
fn close_proposal(
85+
proposal_hash: Hash,
86+
proposal_index: ProposalIndex,
87+
proposal_weight_bound: Weight,
88+
length_bound: u32,
89+
) -> DispatchResultWithPostInfo {
90+
AllianceMotion::do_close(proposal_hash, proposal_index, proposal_weight_bound, length_bound)
91+
}
92+
93+
fn proposal_of(proposal_hash: Hash) -> Option<Call> {
94+
AllianceMotion::proposal_of(proposal_hash)
95+
}
96+
}
97+
3098
#[cfg(test)]
3199
mod multiplier_tests {
32100
use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment};

bin/node/runtime/src/lib.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub use sp_runtime::BuildStorage;
8585

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

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

1211+
parameter_types! {
1212+
pub const AllianceMotionDuration: BlockNumber = 5 * DAYS;
1213+
pub const AllianceMaxProposals: u32 = 100;
1214+
pub const AllianceMaxMembers: u32 = 100;
1215+
}
1216+
1217+
type AllianceCollective = pallet_collective::Instance3;
1218+
impl pallet_collective::Config<AllianceCollective> for Runtime {
1219+
type Origin = Origin;
1220+
type Proposal = Call;
1221+
type Event = Event;
1222+
type MotionDuration = AllianceMotionDuration;
1223+
type MaxProposals = AllianceMaxProposals;
1224+
type MaxMembers = AllianceMaxMembers;
1225+
type DefaultVote = pallet_collective::PrimeDefaultVote;
1226+
type WeightInfo = pallet_collective::weights::SubstrateWeight<Runtime>;
1227+
}
1228+
1229+
parameter_types! {
1230+
pub const MaxBlacklistCount: u32 = 100;
1231+
}
1232+
1233+
impl pallet_alliance::Config for Runtime {
1234+
type Event = Event;
1235+
type Proposal = Call;
1236+
type SuperMajorityOrigin = EnsureOneOf<
1237+
AccountId,
1238+
EnsureRoot<AccountId>,
1239+
pallet_collective::EnsureProportionMoreThan<_2, _3, AccountId, AllianceCollective>,
1240+
>;
1241+
type Currency = Balances;
1242+
type InitializeMembers = AllianceMotion;
1243+
type MembershipChanged = AllianceMotion;
1244+
type Slashed = Treasury;
1245+
type IdentityVerifier = AllianceIdentityVerifier;
1246+
type ProposalProvider = AllianceProposalProvider;
1247+
type MaxBlacklistCount = MaxBlacklistCount;
1248+
type CandidateDeposit = CandidateDeposit;
1249+
}
1250+
12111251
construct_runtime!(
12121252
pub enum Runtime where
12131253
Block = Block,
@@ -1254,6 +1294,8 @@ construct_runtime!(
12541294
Gilt: pallet_gilt::{Pallet, Call, Storage, Event<T>, Config},
12551295
Uniques: pallet_uniques::{Pallet, Call, Storage, Event<T>},
12561296
TransactionStorage: pallet_transaction_storage::{Pallet, Call, Storage, Inherent, Config<T>, Event<T>},
1297+
AllianceMotion: pallet_collective::<Instance3>::{Pallet, Storage, Origin<T>, Event<T>},
1298+
Alliance: pallet_alliance::{Pallet, Call, Storage, Event<T>},
12571299
}
12581300
);
12591301

@@ -1683,6 +1725,7 @@ impl_runtime_apis! {
16831725
add_benchmark!(params, batches, pallet_uniques, Uniques);
16841726
add_benchmark!(params, batches, pallet_utility, Utility);
16851727
add_benchmark!(params, batches, pallet_vesting, Vesting);
1728+
add_benchmark!(params, batches, pallet_alliance, Alliance);
16861729

16871730
if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }
16881731
Ok(batches)

frame/alliance/src/lib.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,12 @@ type NegativeImbalanceOf<T, I = ()> = <<T as Config<I>>::Currency as Currency<
135135
<T as frame_system::Config>::AccountId,
136136
>>::NegativeImbalance;
137137

138-
const IDENTITY_FIELD_DISPLAY: u64 =
139-
0b0000000000000000000000000000000000000000000000000000000000000001;
140-
const IDENTITY_FIELD_WEB: u64 = 0b0000000000000000000000000000000000000000000000000000000000000100;
141-
142138
pub trait IdentityVerifier<AccountId: Clone + Ord> {
143-
fn super_account_id(who: &AccountId) -> Option<AccountId>;
139+
fn has_identity(who: &AccountId, fields: u64) -> bool;
144140

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

147-
fn verify_judgement(who: &AccountId) -> bool;
143+
fn super_account_id(who: &AccountId) -> Option<AccountId>;
148144
}
149145

150146
pub trait ProposalProvider<AccountId, Hash, Proposal> {
@@ -276,12 +272,10 @@ pub mod pallet {
276272
KickingMember,
277273
/// Balance is insufficient to be a candidate.
278274
InsufficientCandidateFunds,
279-
/// The account's identity has not been judged.
280-
WithoutVerifiedIdentity,
281-
/// The account's identity has not display field.
282-
WithoutIdentityDisplay,
283-
/// The account' identity has not website field.
284-
WithoutIdentityWebsite,
275+
/// The account's identity has not display field and website field.
276+
WithoutIdentityDisplayAndWebsite,
277+
/// The account's identity has no good judgement.
278+
WithoutGoodIdentityJudgement,
285279
/// The proposal hash is not found.
286280
MissingProposalHash,
287281
/// The proposal is not vetoable.
@@ -934,18 +928,19 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
934928
}
935929

936930
fn has_identity(who: &T::AccountId) -> DispatchResult {
937-
let judgement = |w: &T::AccountId| -> DispatchResult {
938-
ensure!(
939-
T::IdentityVerifier::verify_identity(w, IDENTITY_FIELD_DISPLAY),
940-
Error::<T, I>::WithoutIdentityDisplay
941-
);
931+
const IDENTITY_FIELD_DISPLAY: u64 =
932+
0b0000000000000000000000000000000000000000000000000000000000000001;
933+
const IDENTITY_FIELD_WEB: u64 =
934+
0b0000000000000000000000000000000000000000000000000000000000000100;
935+
936+
let judgement = |who: &T::AccountId| -> DispatchResult {
942937
ensure!(
943-
T::IdentityVerifier::verify_judgement(w),
944-
Error::<T, I>::WithoutVerifiedIdentity
938+
T::IdentityVerifier::has_identity(who, IDENTITY_FIELD_DISPLAY | IDENTITY_FIELD_WEB),
939+
Error::<T, I>::WithoutIdentityDisplayAndWebsite
945940
);
946941
ensure!(
947-
T::IdentityVerifier::verify_identity(w, IDENTITY_FIELD_WEB),
948-
Error::<T, I>::WithoutIdentityWebsite
942+
T::IdentityVerifier::has_good_judgement(who),
943+
Error::<T, I>::WithoutGoodIdentityJudgement
949944
);
950945
Ok(())
951946
};

frame/alliance/src/mock.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,16 @@ impl pallet_collective::Config<AllianceCollective> for Test {
9797

9898
pub struct AllianceIdentityVerifier;
9999
impl IdentityVerifier<u64> for AllianceIdentityVerifier {
100-
fn super_account_id(_who: &u64) -> Option<u64> {
101-
None
100+
fn has_identity(_who: &u64, _fields: u64) -> bool {
101+
true
102102
}
103103

104-
fn verify_identity(_who: &u64, _field: u64) -> bool {
104+
fn has_good_judgement(_who: &u64) -> bool {
105105
true
106106
}
107107

108-
fn verify_judgement(_who: &u64) -> bool {
109-
true
108+
fn super_account_id(_who: &u64) -> Option<u64> {
109+
None
110110
}
111111
}
112112

frame/identity/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ targets = ["x86_64-unknown-linux-gnu"]
1515
[dependencies]
1616
codec = { package = "parity-scale-codec", version = "2.2.0", default-features = false, features = ["derive", "max-encoded-len"] }
1717
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
18-
enumflags2 = { version = "0.6.2" }
18+
enumflags2 = { version = "0.7.1" }
1919
sp-std = { version = "4.0.0-dev", default-features = false, path = "../../primitives/std" }
2020
sp-io = { version = "4.0.0-dev", default-features = false, path = "../../primitives/io" }
2121
sp-runtime = { version = "4.0.0-dev", default-features = false, path = "../../primitives/runtime" }

frame/identity/src/lib.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ pub mod weights;
8181
use frame_support::traits::{BalanceStatus, Currency, OnUnbalanced, ReservableCurrency};
8282
use sp_runtime::traits::{AppendZerosInput, Saturating, StaticLookup, Zero};
8383
use sp_std::{convert::TryInto, prelude::*};
84-
pub use weights::WeightInfo;
8584

8685
pub use pallet::*;
8786
pub use types::{
8887
Data, IdentityField, IdentityFields, IdentityInfo, Judgement, RegistrarIndex, RegistrarInfo,
8988
Registration,
9089
};
90+
pub use weights::WeightInfo;
9191

9292
type BalanceOf<T> =
9393
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
@@ -160,7 +160,7 @@ pub mod pallet {
160160
/// TWOX-NOTE: OK ― `AccountId` is a secure hash.
161161
#[pallet::storage]
162162
#[pallet::getter(fn identity)]
163-
pub(super) type IdentityOf<T: Config> = StorageMap<
163+
pub type IdentityOf<T: Config> = StorageMap<
164164
_,
165165
Twox64Concat,
166166
T::AccountId,
@@ -172,7 +172,7 @@ pub mod pallet {
172172
/// context. If the account is not some other account's sub-identity, then just `None`.
173173
#[pallet::storage]
174174
#[pallet::getter(fn super_of)]
175-
pub(super) type SuperOf<T: Config> =
175+
pub type SuperOf<T: Config> =
176176
StorageMap<_, Blake2_128Concat, T::AccountId, (T::AccountId, Data), OptionQuery>;
177177

178178
/// Alternative "sub" identities of this account.
@@ -182,7 +182,7 @@ pub mod pallet {
182182
/// TWOX-NOTE: OK ― `AccountId` is a secure hash.
183183
#[pallet::storage]
184184
#[pallet::getter(fn subs_of)]
185-
pub(super) type SubsOf<T: Config> = StorageMap<
185+
pub type SubsOf<T: Config> = StorageMap<
186186
_,
187187
Twox64Concat,
188188
T::AccountId,
@@ -196,7 +196,7 @@ pub mod pallet {
196196
/// The index into this can be cast to `RegistrarIndex` to get a valid value.
197197
#[pallet::storage]
198198
#[pallet::getter(fn registrars)]
199-
pub(super) type Registrars<T: Config> = StorageValue<
199+
pub type Registrars<T: Config> = StorageValue<
200200
_,
201201
BoundedVec<Option<RegistrarInfo<BalanceOf<T>, T::AccountId>>, T::MaxRegistrars>,
202202
ValueQuery,
@@ -970,4 +970,13 @@ impl<T: Config> Pallet<T> {
970970
.filter_map(|a| SuperOf::<T>::get(&a).map(|x| (a, x.1)))
971971
.collect()
972972
}
973+
974+
/// Check if the account has corresponding identity information by the identity field.
975+
pub fn has_identity(who: &T::AccountId, fields: u64) -> bool {
976+
if let Some(info) = IdentityOf::<T>::get(who).map(|registration| registration.info) {
977+
(info.fields().0.bits() & fields) == 1
978+
} else {
979+
false
980+
}
981+
}
973982
}

0 commit comments

Comments
 (0)