diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b53eb7df5bf8a8a4aa85ce177e161dd005362ca7..393a9c2da7c0263911789f7b52602b59059a6a77 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -84,6 +84,7 @@ run_benchmarks: tags: - podman +# FIXME: "gtest_build" gdev_build: stage: build image: rust:1-bullseye diff --git a/Cargo.lock b/Cargo.lock index bbedc15e66b4abc0133c32d9704866348e986eda..ed613c0d86e50cc3023e787d54dc8b5073917fe6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1145,6 +1145,7 @@ dependencies = [ "pallet-scheduler", "pallet-session", "pallet-session-benchmarking", + "pallet-smith-members", "pallet-timestamp", "pallet-treasury", "pallet-universal-dividend", @@ -3137,6 +3138,7 @@ dependencies = [ "pallet-scheduler", "pallet-session", "pallet-session-benchmarking", + "pallet-smith-members", "pallet-sudo", "pallet-timestamp", "pallet-transaction-payment", @@ -3207,6 +3209,7 @@ dependencies = [ "pallet-scheduler", "pallet-session", "pallet-session-benchmarking", + "pallet-smith-members", "pallet-sudo", "pallet-timestamp", "pallet-transaction-payment", @@ -3512,6 +3515,7 @@ dependencies = [ "pallet-scheduler", "pallet-session", "pallet-session-benchmarking", + "pallet-smith-members", "pallet-sudo", "pallet-timestamp", "pallet-transaction-payment", @@ -6305,6 +6309,7 @@ dependencies = [ "sp-core", "sp-io", "sp-runtime", + "sp-staking", "sp-std 5.0.0", ] diff --git a/end2end-tests/cucumber-genesis/default.json b/end2end-tests/cucumber-genesis/default.json index fab39aad614d7f3f631cae19d24dfd757cc567d0..245e91e312e06afca31bf3a5023a5e423c8d2e31 100644 --- a/end2end-tests/cucumber-genesis/default.json +++ b/end2end-tests/cucumber-genesis/default.json @@ -56,13 +56,8 @@ "pending_membership_period": 500, "ud_creation_period": 60000, "ud_reeval_period": 600000, - "smith_cert_period": 15, "smith_cert_max_by_issuer": 8, - "smith_cert_min_received_cert_to_issue_cert": 2, - "smith_cert_validity_period": 1000, - "smith_membership_period": 1000, - "smith_pending_membership_period": 500, - "smith_wot_first_cert_issuable_on": 20, + "smith_inactivity_max_duration": 48, "smith_wot_min_cert_for_membership": 2, "wot_first_cert_issuable_on": 20, "wot_min_cert_for_create_idty_right": 2, diff --git a/end2end-tests/cucumber-genesis/wot.json b/end2end-tests/cucumber-genesis/wot.json index 4c7183b2a37cd3b82fd4c95ba360cf0025c91ae8..6b1fad67065eb3323c083cc7e0e80ae8ce6098ef 100644 --- a/end2end-tests/cucumber-genesis/wot.json +++ b/end2end-tests/cucumber-genesis/wot.json @@ -65,13 +65,8 @@ "pending_membership_period": 500, "ud_creation_period": 60000, "ud_reeval_period": 600000, - "smith_cert_period": 15, "smith_cert_max_by_issuer": 8, - "smith_cert_min_received_cert_to_issue_cert": 2, - "smith_cert_validity_period": 1000, - "smith_membership_period": 1000, - "smith_pending_membership_period": 500, - "smith_wot_first_cert_issuable_on": 20, + "smith_inactivity_max_duration": 48, "smith_wot_min_cert_for_membership": 2, "wot_first_cert_issuable_on": 20, "wot_min_cert_for_create_idty_right": 2, diff --git a/node/src/chain_spec/gdev.rs b/node/src/chain_spec/gdev.rs index 0639cbfed4d086ba965d2ce4fb8cc15f7c8d37f0..4d80232c6e87564a2a7cfcffb75261c29bd49164 100644 --- a/node/src/chain_spec/gdev.rs +++ b/node/src/chain_spec/gdev.rs @@ -24,9 +24,8 @@ use common_runtime::*; use gdev_runtime::{ opaque::SessionKeys, pallet_universal_dividend, parameters, AccountConfig, AuthorityMembersConfig, BabeConfig, BalancesConfig, CertConfig, GenesisConfig, IdentityConfig, - MembershipConfig, ParametersConfig, QuotaConfig, Runtime, SessionConfig, SmithCertConfig, - SmithMembershipConfig, SudoConfig, SystemConfig, TechnicalCommitteeConfig, - UniversalDividendConfig, WASM_BINARY, + MembershipConfig, ParametersConfig, QuotaConfig, Runtime, SessionConfig, SmithMembersConfig, + SudoConfig, SystemConfig, TechnicalCommitteeConfig, UniversalDividendConfig, WASM_BINARY, }; use jsonrpsee::core::JsonValue; use sc_network::config::MultiaddrWithPeerId; @@ -39,7 +38,7 @@ use std::{env, fs}; pub type ChainSpec = sc_service::GenericChainSpec<GenesisConfig>; -type GenesisParameters = gdev_runtime::GenesisParameters<u32, u32, u64>; +type GenesisParameters = gdev_runtime::GenesisParameters<u32, u32, u64, u32>; const TOKEN_DECIMALS: usize = 2; const TOKEN_SYMBOL: &str = "ĞD"; @@ -94,17 +93,10 @@ fn get_parameters(parameters_from_file: &Option<GenesisParameters>) -> CommonPar cert_validity_period: parameters_from_file.cert_validity_period, distance_min_accessible_referees: Perbill::from_percent(80), // TODO: generalize distance_max_depth: 5, // TODO: generalize - smith_sub_wot_first_issuable_on: parameters_from_file.smith_wot_first_cert_issuable_on, smith_sub_wot_min_cert_for_membership: parameters_from_file .smith_wot_min_cert_for_membership, - smith_membership_membership_period: parameters_from_file.smith_membership_period, - smith_membership_pending_membership_period: parameters_from_file - .smith_pending_membership_period, - smith_cert_cert_period: parameters_from_file.smith_cert_period, smith_cert_max_by_issuer: parameters_from_file.smith_cert_max_by_issuer, - smith_cert_min_received_cert_to_be_able_to_issue_cert: parameters_from_file - .smith_cert_min_received_cert_to_issue_cert, - smith_cert_validity_period: parameters_from_file.smith_cert_validity_period, + smith_inactivity_max_duration: parameters_from_file.smith_inactivity_max_duration, cert_cert_period: parameters_from_file.cert_period, treasury_spend_period: <Runtime as pallet_treasury::Config>::SpendPeriod::get(), } @@ -290,8 +282,7 @@ fn genesis_data_to_gdev_genesis_conf( parameters, common_parameters: _, session_keys_map, - smith_certs_by_receiver, - smith_memberships, + initial_smiths, sudo_key, technical_committee_members, ud, @@ -366,13 +357,7 @@ fn genesis_data_to_gdev_genesis_conf( certs_by_receiver, }, membership: MembershipConfig { memberships }, - smith_cert: SmithCertConfig { - apply_cert_period_at_genesis: true, - certs_by_receiver: smith_certs_by_receiver, - }, - smith_membership: SmithMembershipConfig { - memberships: smith_memberships, - }, + smith_members: SmithMembersConfig { initial_smiths }, universal_dividend: UniversalDividendConfig { first_reeval: first_ud_reeval, first_ud, @@ -387,8 +372,6 @@ fn get_local_chain_parameters() -> Option<GenesisParameters> { let babe_epoch_duration = get_env("DUNITER_BABE_EPOCH_DURATION", 30) as u64; let cert_validity_period = get_env("DUNITER_CERT_VALIDITY_PERIOD", 1_000); let membership_period = get_env("DUNITER_MEMBERSHIP_PERIOD", 1_000); - let smith_cert_validity_period = get_env("DUNITER_SMITH_CERT_VALIDITY_PERIOD", 1_000); - let smith_membership_period = get_env("DUNITER_SMITH_MEMBERSHIP_PERIOD", 1_000); let ud_creation_period = get_env("DUNITER_UD_CREATION_PERIOD", 60_000); let ud_reeval_period = get_env("DUNITER_UD_REEEVAL_PERIOD", 1_200_000); Some(GenesisParameters { @@ -403,13 +386,8 @@ fn get_local_chain_parameters() -> Option<GenesisParameters> { pending_membership_period: 500, ud_creation_period, ud_reeval_period, - smith_cert_period: 15, smith_cert_max_by_issuer: 8, - smith_cert_min_received_cert_to_issue_cert: 2, - smith_cert_validity_period, - smith_membership_period, - smith_pending_membership_period: 500, - smith_wot_first_cert_issuable_on: 20, + smith_inactivity_max_duration: 48, smith_wot_min_cert_for_membership: 2, wot_first_cert_issuable_on: 20, wot_min_cert_for_create_idty_right: 2, diff --git a/node/src/chain_spec/gen_genesis_data.rs b/node/src/chain_spec/gen_genesis_data.rs index 1813434b7a43e0366d89fc345c163a498102f250..b0acb65f826fafbeac9d8ae4c2bc12300a9f9e43 100644 --- a/node/src/chain_spec/gen_genesis_data.rs +++ b/node/src/chain_spec/gen_genesis_data.rs @@ -74,8 +74,7 @@ pub struct GenesisData<Parameters: DeserializeOwned, SessionKeys: Decode> { pub parameters: Option<Parameters>, pub common_parameters: Option<CommonParameters>, pub session_keys_map: BTreeMap<AccountId, SessionKeys>, - pub smith_certs_by_receiver: BTreeMap<u32, BTreeMap<u32, Option<u32>>>, - pub smith_memberships: BTreeMap<u32, MembershipData>, + pub initial_smiths: BTreeMap<u32, (bool, Vec<u32>)>, pub sudo_key: Option<AccountId>, pub technical_committee_members: Vec<AccountId>, pub ud: u64, @@ -235,9 +234,8 @@ struct CliqueSmith { session_keys: Option<String>, } -struct SmithWoT<SK: Decode> { - smith_certs_by_receiver: BTreeMap<u32, BTreeMap<u32, Option<u32>>>, - smith_memberships: BTreeMap<u32, sp_membership::MembershipData<u32>>, +struct SmithMembers<SK: Decode> { + initial_smiths_wot: BTreeMap<u32, (bool, Vec<u32>)>, session_keys_map: BTreeMap<<<MultiSignature as Verify>::Signer as IdentifyAccount>::AccountId, SK>, } @@ -249,7 +247,7 @@ struct GenesisInfo<'a> { inactive_identities: &'a HashMap<u32, String>, identities: &'a Vec<GenesisIdentity>, identity_index: &'a HashMap<u32, String>, - smith_memberships: &'a BTreeMap<u32, MembershipData>, + initial_smiths: &'a BTreeMap<u32, (bool, Vec<u32>)>, counter_online_authorities: &'a u32, counter_cert: &'a u32, counter_smith_cert: &'a u32, @@ -395,16 +393,14 @@ where was_fatal, counter_online_authorities, counter_smith_cert, - SmithWoT { - smith_certs_by_receiver, - smith_memberships, + SmithMembers { + initial_smiths_wot, session_keys_map, }, ) = create_smith_wot( &mut initial_authorities, &identities_v2, &smiths, - &common_parameters, &clique_smiths, )?; if was_fatal { @@ -429,7 +425,7 @@ where } // Verify smith certifications coherence - for (idty_index, certs) in &smith_certs_by_receiver { + for (idty_index, (_online, certs)) in &initial_smiths_wot { if certs.len() < common_parameters.smith_sub_wot_min_cert_for_membership as usize { log::error!( "[{}] has received only {}/{} smith certifications", @@ -515,7 +511,7 @@ where identities: &identities, inactive_identities: &inactive_identities, identity_index: &identity_index, - smith_memberships: &smith_memberships, + initial_smiths: &initial_smiths_wot, counter_online_authorities: &counter_online_authorities, counter_cert: &counter_cert, counter_smith_cert: &counter_smith_cert, @@ -637,8 +633,8 @@ where identities.len() - inactive_identities.len(), memberships.len() ); - assert_eq!(smith_memberships.len(), initial_authorities.len()); - assert_eq!(smith_memberships.len(), session_keys_map.len()); + assert_eq!(initial_smiths_wot.len(), initial_authorities.len()); + assert_eq!(initial_smiths_wot.len(), session_keys_map.len()); assert_eq!(identity_index.len(), identities.len()); assert_eq!( accounts.len(), @@ -657,7 +653,6 @@ where // genesis_certs_min_received => min_cert // genesis_memberships_expire_on => membership_period // genesis_smith_certs_min_received => smith_min_cert - // genesis_smith_memberships_expire_on => smith_membership_period let export = GenesisIndexerExport { first_ud, first_ud_reeval, @@ -730,8 +725,7 @@ where parameters, common_parameters: Some(common_parameters), session_keys_map, - smith_certs_by_receiver, - smith_memberships, + initial_smiths: initial_smiths_wot, sudo_key, technical_committee_members, ud, @@ -759,7 +753,7 @@ fn dump_genesis_info(info: GenesisInfo) { info.identity_index.len(), info.identities.len() - info.inactive_identities.len(), info.inactive_identities.len(), - info.smith_memberships.len(), + info.initial_smiths.len(), info.counter_online_authorities, info.counter_cert, info.counter_smith_cert, @@ -829,28 +823,15 @@ fn dump_genesis_info(info: GenesisInfo) { get_best_unit_and_diviser_for_perbill(p.distance_min_accessible_referees); let (distance_max_depth, distance_max_depth_unit) = get_best_unit_and_diviser_for_depth(p.distance_max_depth); - let (smith_sub_wot_first_issuable_on, smith_sub_wot_first_issuable_on_unit) = - get_best_unit_and_diviser_for_blocks(p.smith_sub_wot_first_issuable_on); - let (smith_sub_wot_min_cert_for_membership, smith_sub_wot_min_cert_for_membership_unit) = + let (smith_members_min_cert_for_membership, smith_members_min_cert_for_membership_unit) = get_best_unit_and_diviser_for_certs(p.smith_sub_wot_min_cert_for_membership); - let (smith_membership_membership_period, smith_membership_membership_period_unit) = - get_best_unit_and_diviser_for_blocks(p.smith_membership_membership_period); - let ( - smith_membership_pending_membership_period, - smith_membership_pending_membership_period_unit, - ) = get_best_unit_and_diviser_for_blocks(p.smith_membership_pending_membership_period); - let (smith_cert_cert_period, smith_cert_cert_period_unit) = - get_best_unit_and_diviser_for_blocks(p.smith_cert_cert_period); - let (smith_cert_max_by_issuer, smith_cert_max_by_issuer_unit) = + let (smith_members_max_by_issuer, smith_members_max_by_issuer_unit) = get_best_unit_and_diviser_for_certs(p.smith_cert_max_by_issuer); - let ( - smith_cert_min_received_cert_to_be_able_to_issue_cert, - smith_cert_min_received_cert_to_be_able_to_issue_cert_unit, - ) = get_best_unit_and_diviser_for_certs( - p.smith_cert_min_received_cert_to_be_able_to_issue_cert, - ); - let (smith_cert_validity_period, smith_cert_validity_period_unit) = - get_best_unit_and_diviser_for_blocks(p.smith_cert_validity_period); + let (smith_members_inactivity_max_duration, smith_members_inactivity_max_duration_unit) = + get_best_unit_and_diviser_for_sessions( + p.smith_inactivity_max_duration, + p.babe_epoch_duration, + ); let (treasury_spend_period, treasury_spend_period_unit) = get_best_unit_and_diviser_for_blocks(p.treasury_spend_period); @@ -883,14 +864,9 @@ fn dump_genesis_info(info: GenesisInfo) { - cert.validity_period: {} {} - distance.min_accessible_referees: {} {} - distance.max_depth: {} {}, - - smith_sub_wot.first_issuable_on: {} {} - - smith_sub_wot.min_cert_for_membership: {} {} - - smith_membership.membership_period: {} {} - - smith_membership.pending_membership_period: {} {} - - smith_cert.cert_period: {} {} - - smith_cert.max_by_issuer: {} {} - - smith_cert.min_received_cert_to_be_able_to_issue_cert: {} {} - - smith_cert.validity_period: {} {} + - smith_members.min_cert_for_membership: {} {} + - smith_members.max_by_issuer: {} {} + - smith_members.smith_inactivity_max_duration: {} {} - treasury.spend_period: {} {} - currency decimals: {}", babe_epoch_duration, @@ -945,22 +921,12 @@ fn dump_genesis_info(info: GenesisInfo) { distance_min_accessible_referees_unit, distance_max_depth, distance_max_depth_unit, - smith_sub_wot_first_issuable_on, - smith_sub_wot_first_issuable_on_unit, - smith_sub_wot_min_cert_for_membership, - smith_sub_wot_min_cert_for_membership_unit, - smith_membership_membership_period, - smith_membership_membership_period_unit, - smith_membership_pending_membership_period, - smith_membership_pending_membership_period_unit, - smith_cert_cert_period, - smith_cert_cert_period_unit, - smith_cert_max_by_issuer, - smith_cert_max_by_issuer_unit, - smith_cert_min_received_cert_to_be_able_to_issue_cert, - smith_cert_min_received_cert_to_be_able_to_issue_cert_unit, - smith_cert_validity_period, - smith_cert_validity_period_unit, + smith_members_min_cert_for_membership, + smith_members_min_cert_for_membership_unit, + smith_members_max_by_issuer, + smith_members_max_by_issuer_unit, + smith_members_inactivity_max_duration, + smith_members_inactivity_max_duration_unit, treasury_spend_period, treasury_spend_period_unit, info.common_parameters.decimals, @@ -979,6 +945,16 @@ fn get_best_unit_and_diviser_for_blocks(duration_in_blocks: u32) -> (f32, String get_best_unit_and_diviser_for_ms(duration_in_ms) } +fn get_best_unit_and_diviser_for_sessions( + duration_in_sessions: u32, + babe_epoch_duration_in_blocks: u64, +) -> (f32, String) { + let duration_in_ms = duration_in_sessions as f32 + * babe_epoch_duration_in_blocks as f32 + * (MILLISECS_PER_BLOCK as u32) as f32; + get_best_unit_and_diviser_for_ms(duration_in_ms) +} + fn get_best_unit_and_diviser_for_perbill_square(qty: Perbill) -> (f32, String) { let qty = f32::sqrt(qty.deconstruct() as f32 / 1_000_000_000f32) * 100f32; (qty, "%".to_string()) @@ -1073,16 +1049,14 @@ fn create_smith_wot<SK: Decode>( initial_authorities: &mut BTreeMap<u32, (AccountId32, bool)>, identities_v2: &HashMap<String, IdentityV2>, smiths: &Vec<SmithData>, - common_parameters: &CommonParameters, clique_smiths: &Option<Vec<CliqueSmith>>, -) -> Result<(bool, u32, u32, SmithWoT<SK>), String> { +) -> Result<(bool, u32, u32, SmithMembers<SK>), String> { let mut fatal = false; let mut counter_online_authorities = 0; // counter for smith certifications let mut counter_smith_cert = 0; let mut smith_certs_by_receiver = BTreeMap::new(); // smith memberships - let mut smith_memberships = BTreeMap::new(); let mut session_keys_map = BTreeMap::new(); // Then create the smith WoT for smith in smiths { @@ -1103,16 +1077,7 @@ fn create_smith_wot<SK: Decode>( &smith, identity, &identities_v2_clone, - common_parameters, )?; - - // smith memberships - smith_memberships.insert( - identity.index, - MembershipData { - expire_on: common_parameters.smith_membership_membership_period, - }, - ); } else { log::error!( "Smith '{}' does not correspond to exising identity", @@ -1125,9 +1090,18 @@ fn create_smith_wot<SK: Decode>( fatal, counter_online_authorities, counter_smith_cert, - SmithWoT { - smith_certs_by_receiver, - smith_memberships, + SmithMembers { + initial_smiths_wot: smith_certs_by_receiver + .iter() + .map(|(k, v)| { + let is_online = initial_authorities + .iter() + .filter(|(k2, (_, online))| *k2 == k && *online) + .count() + > 0; + (*k, (is_online, v.clone())) + }) + .collect(), session_keys_map, }, )) @@ -1549,15 +1523,14 @@ where } fn feed_smith_certs_by_receiver( - smith_certs_by_receiver: &mut BTreeMap<u32, BTreeMap<u32, Option<u32>>>, + smith_certs_by_receiver: &mut BTreeMap<u32, Vec<u32>>, clique_smiths: &Option<Vec<CliqueSmith>>, smith: &&SmithData, identity: &IdentityV2, identities_v2: &HashMap<String, IdentityV2>, - common_parameters: &CommonParameters, ) -> Result<u32, String> { let mut counter_smith_cert = 0; - let mut certs = BTreeMap::new(); + let mut certs = Vec::<u32>::new(); if clique_smiths.is_some() { // All initial smiths are considered to be certifying all each other clique_smiths @@ -1572,7 +1545,7 @@ fn feed_smith_certs_by_receiver( panic!("Identity '{}' does not exist", other_smith.name.as_str()) }) .index; - certs.insert(*issuer_index, None); + certs.push(*issuer_index); counter_smith_cert += 1; }); } else { @@ -1581,10 +1554,7 @@ fn feed_smith_certs_by_receiver( .get(issuer) .ok_or(format!("Identity '{}' does not exist", issuer))? .index; - certs.insert( - *issuer_index, - Some(common_parameters.smith_cert_validity_period), - ); + certs.push(*issuer_index); counter_smith_cert += 1; } } @@ -1767,11 +1737,11 @@ where let genesis_data_wallets_count = 0; let inactive_identities = HashMap::new(); - let smith_memberships = (1..=initial_smiths_len) - .map(|i| (i as u32, MembershipData { expire_on: 0 })) - .collect(); - - let (smith_certs_by_receiver, counter_smith_cert) = clique_wot(initial_smiths_len); + let initial_smiths_wot: BTreeMap<u32, (bool, Vec<u32>)> = clique_smith_wot(initial_smiths_len); + let counter_smith_cert = initial_smiths_wot + .iter() + .map(|(_, (_, v))| v.len()) + .sum::<usize>() as u32; let initial_authorities: BTreeMap< u32, @@ -1808,7 +1778,7 @@ where identities: &identities, inactive_identities: &inactive_identities, identity_index: &identity_index, - smith_memberships: &smith_memberships, + initial_smiths: &initial_smiths_wot, counter_online_authorities: &counter_online_authorities, counter_cert: &counter_cert, counter_smith_cert: &counter_smith_cert, @@ -1834,8 +1804,7 @@ where parameters, common_parameters: None, session_keys_map, - smith_certs_by_receiver, - smith_memberships, + initial_smiths: initial_smiths_wot, sudo_key: Some(root_key), technical_committee_members, ud, @@ -1844,6 +1813,7 @@ where Ok(genesis_data) } +// TODO: avoid duplication? #[cfg(feature = "gdev")] fn clique_wot( initial_identities_len: usize, @@ -1871,6 +1841,24 @@ fn clique_wot( (certs_by_issuer, count) } +// TODO: avoid duplication? +#[cfg(feature = "gdev")] +fn clique_smith_wot(initial_identities_len: usize) -> BTreeMap<IdtyIndex, (bool, Vec<IdtyIndex>)> { + let mut certs_by_issuer = BTreeMap::new(); + for i in 1..=initial_identities_len { + certs_by_issuer.insert( + i as IdtyIndex, + ( + true, + (1..=initial_identities_len) + .filter_map(|j| if i != j { Some(j as IdtyIndex) } else { None }) + .collect(), + ), + ); + } + certs_by_issuer +} + fn check_parameters_consistency( wallets: &BTreeMap<PubkeyV1, u64>, first_ud: &Option<u64>, @@ -1999,14 +1987,9 @@ pub struct CommonParameters { pub cert_validity_period: u32, pub distance_min_accessible_referees: Perbill, pub distance_max_depth: u32, - pub smith_sub_wot_first_issuable_on: u32, pub smith_sub_wot_min_cert_for_membership: u32, - pub smith_membership_membership_period: u32, - pub smith_membership_pending_membership_period: u32, - pub smith_cert_cert_period: u32, pub smith_cert_max_by_issuer: u32, - pub smith_cert_min_received_cert_to_be_able_to_issue_cert: u32, - pub smith_cert_validity_period: u32, + pub smith_inactivity_max_duration: u32, pub treasury_spend_period: u32, // TODO: replace u32 by BlockNumber when appropriate pub currency_name: String, diff --git a/node/src/chain_spec/gtest.rs b/node/src/chain_spec/gtest.rs index 796b2c7de224df2f3422af8097065389ac3e534c..485e0db182c5411d57507d9a75ad309382819568 100644 --- a/node/src/chain_spec/gtest.rs +++ b/node/src/chain_spec/gtest.rs @@ -20,12 +20,12 @@ use common_runtime::constants::*; use common_runtime::entities::IdtyData; use common_runtime::*; use frame_benchmarking::frame_support::traits::Get; +use gtest_runtime::SmithMembersConfig; use gtest_runtime::{ opaque::SessionKeys, pallet_universal_dividend, parameters, AccountConfig, AccountId, AuthorityMembersConfig, BabeConfig, BalancesConfig, CertConfig, GenesisConfig, IdentityConfig, - ImOnlineId, MembershipConfig, Perbill, QuotaConfig, Runtime, SessionConfig, SmithCertConfig, - SmithMembershipConfig, SudoConfig, SystemConfig, TechnicalCommitteeConfig, - UniversalDividendConfig, WASM_BINARY, + ImOnlineId, MembershipConfig, Perbill, QuotaConfig, Runtime, SessionConfig, SudoConfig, + SystemConfig, TechnicalCommitteeConfig, UniversalDividendConfig, WASM_BINARY, }; use jsonrpsee::core::JsonValue; use sc_consensus_grandpa::AuthorityId as GrandpaId; @@ -98,15 +98,9 @@ fn get_parameters(_: &Option<GenesisParameters>) -> CommonParameters { cert_validity_period: parameters::ValidityPeriod::get(), distance_min_accessible_referees: Perbill::from_percent(80), // TODO: generalize distance_max_depth: 5, // TODO: generalize - smith_sub_wot_first_issuable_on: parameters::SmithWotFirstCertIssuableOn::get(), smith_sub_wot_min_cert_for_membership: parameters::SmithWotMinCertForMembership::get(), - smith_membership_membership_period: parameters::SmithMembershipPeriod::get(), - smith_membership_pending_membership_period: parameters::SmithPendingMembershipPeriod::get(), - smith_cert_cert_period: parameters::SmithCertPeriod::get(), + smith_inactivity_max_duration: parameters::InactivityMaxDuration::get(), smith_cert_max_by_issuer: parameters::SmithMaxByIssuer::get(), - smith_cert_min_received_cert_to_be_able_to_issue_cert: - parameters::SmithMinReceivedCertToBeAbleToIssueCert::get(), - smith_cert_validity_period: parameters::SmithValidityPeriod::get(), cert_cert_period: parameters::CertPeriod::get(), treasury_spend_period: <Runtime as pallet_treasury::Config>::SpendPeriod::get(), } @@ -239,8 +233,7 @@ fn genesis_data_to_gtest_genesis_conf( parameters: _, common_parameters: _, session_keys_map, - smith_certs_by_receiver, - smith_memberships, + initial_smiths, sudo_key, technical_committee_members, ud, @@ -313,13 +306,7 @@ fn genesis_data_to_gtest_genesis_conf( certs_by_receiver, }, membership: MembershipConfig { memberships }, - smith_cert: SmithCertConfig { - apply_cert_period_at_genesis: true, - certs_by_receiver: smith_certs_by_receiver, - }, - smith_membership: SmithMembershipConfig { - memberships: smith_memberships, - }, + smith_members: SmithMembersConfig { initial_smiths }, universal_dividend: UniversalDividendConfig { first_reeval: first_ud_reeval, first_ud, diff --git a/pallets/authority-members/src/lib.rs b/pallets/authority-members/src/lib.rs index 32b0724124f0ff58503225e3a611632e3143a585..a3b03d0d0b2b6bd2c3c86486c3c80d87d5f7d6f5 100644 --- a/pallets/authority-members/src/lib.rs +++ b/pallets/authority-members/src/lib.rs @@ -411,7 +411,6 @@ pub mod pallet { // Emit event Self::deposit_event(Event::MemberRemoved { member: member_id }); - T::OnRemovedMember::on_removed_member(member_id); } /// perform incoming authorities insertion fn insert_in(member_id: T::MemberId) -> bool { diff --git a/pallets/duniter-test-parameters/src/lib.rs b/pallets/duniter-test-parameters/src/lib.rs index 0f75c3296a6272358c75333984b7f7bae308c24f..4da26974ba7202c4dce8476d415e965d54276878 100644 --- a/pallets/duniter-test-parameters/src/lib.rs +++ b/pallets/duniter-test-parameters/src/lib.rs @@ -35,6 +35,7 @@ pub mod types { BlockNumber: Default + Parameter, CertCount: Default + Parameter, PeriodCount: Default + Parameter, + SessionCount: Default + Parameter, > { pub babe_epoch_duration: PeriodCount, pub cert_period: BlockNumber, @@ -47,14 +48,9 @@ pub mod types { pub pending_membership_period: BlockNumber, pub ud_creation_period: PeriodCount, pub ud_reeval_period: PeriodCount, - pub smith_cert_period: BlockNumber, pub smith_cert_max_by_issuer: CertCount, - pub smith_cert_min_received_cert_to_issue_cert: CertCount, - pub smith_cert_validity_period: BlockNumber, - pub smith_membership_period: BlockNumber, - pub smith_pending_membership_period: BlockNumber, - pub smith_wot_first_cert_issuable_on: BlockNumber, pub smith_wot_min_cert_for_membership: CertCount, + pub smith_inactivity_max_duration: SessionCount, pub wot_first_cert_issuable_on: BlockNumber, pub wot_min_cert_for_create_idty_right: CertCount, pub wot_min_cert_for_membership: CertCount, @@ -81,20 +77,24 @@ pub mod pallet { pub trait Config: frame_system::Config { type CertCount: Default + MaybeSerializeDeserialize + Parameter; type PeriodCount: Default + MaybeSerializeDeserialize + Parameter; + type SessionCount: Default + MaybeSerializeDeserialize + Parameter; } // STORAGE // #[pallet::storage] #[pallet::getter(fn parameters)] - pub type ParametersStorage<T: Config> = - StorageValue<_, Parameters<T::BlockNumber, T::CertCount, T::PeriodCount>, ValueQuery>; + pub type ParametersStorage<T: Config> = StorageValue< + _, + Parameters<T::BlockNumber, T::CertCount, T::PeriodCount, T::SessionCount>, + ValueQuery, + >; // GENESIS #[pallet::genesis_config] pub struct GenesisConfig<T: Config> { - pub parameters: Parameters<T::BlockNumber, T::CertCount, T::PeriodCount>, + pub parameters: Parameters<T::BlockNumber, T::CertCount, T::PeriodCount, T::SessionCount>, } #[cfg(feature = "std")] diff --git a/pallets/duniter-wot/src/lib.rs b/pallets/duniter-wot/src/lib.rs index 41ad8db138320591f987b6a4c00870164d03bd62..39d8d684cb5bb0f1b752237c1ceaa08044643148 100644 --- a/pallets/duniter-wot/src/lib.rs +++ b/pallets/duniter-wot/src/lib.rs @@ -36,7 +36,6 @@ use frame_support::pallet_prelude::*; use pallet_certification::traits::SetNextIssuableOn; use pallet_identity::{IdtyEvent, IdtyStatus}; use pallet_membership::MembershipRemovalReason; -use sp_runtime::traits::IsMember; type IdtyIndex = u32; diff --git a/pallets/duniter-wot/src/mock.rs b/pallets/duniter-wot/src/mock.rs index 9420b482f4f48a76047750cfdd8d289f1677f83b..33034b24c40893d999e42a2e0020b1cdc51aa600 100644 --- a/pallets/duniter-wot/src/mock.rs +++ b/pallets/duniter-wot/src/mock.rs @@ -50,9 +50,6 @@ frame_support::construct_runtime!( Identity: pallet_identity::{Pallet, Call, Config<T>, Storage, Event<T>}, Membership: pallet_membership::<Instance1>::{Pallet, Call, Config<T>, Storage, Event<T>}, Cert: pallet_certification::<Instance1>::{Pallet, Call, Config<T>, Storage, Event<T>}, - SmithSubWot: pallet_duniter_wot::<Instance2>::{Pallet}, - SmithMembership: pallet_membership::<Instance2>::{Pallet, Call, Config<T>, Storage, Event<T>}, - SmithCert: pallet_certification::<Instance2>::{Pallet, Call, Config<T>, Storage, Event<T>}, } ); @@ -124,7 +121,7 @@ impl pallet_identity::traits::IdtyNameValidator for IdtyNameValidatorTestImpl { impl pallet_identity::Config for Test { type ChangeOwnerKeyPeriod = ChangeOwnerKeyPeriod; type ConfirmPeriod = ConfirmPeriod; - type CheckIdtyCallAllowed = (DuniterWot, SmithSubWot); + type CheckIdtyCallAllowed = DuniterWot; type IdtyCreationPeriod = IdtyCreationPeriod; type ValidationPeriod = ValidationPeriod; type AutorevocationPeriod = AutorevocationPeriod; @@ -135,7 +132,7 @@ impl pallet_identity::Config for Test { type AccountLinker = (); type Signer = UintAuthorityId; type Signature = TestSignature; - type OnIdtyChange = (DuniterWot, SmithSubWot); + type OnIdtyChange = DuniterWot; type RuntimeEvent = RuntimeEvent; type WeightInfo = (); #[cfg(feature = "runtime-benchmarks")] @@ -182,68 +179,10 @@ impl pallet_certification::Config<Instance1> for Test { type ValidityPeriod = ValidityPeriod; } -// SMITHS SUB-WOT // - -parameter_types! { - pub const SmithMinCertForMembership: u32 = 2; - pub const SmithFirstIssuableOn: u64 = 2; -} - -impl pallet_duniter_wot::Config<Instance2> for Test { - type IsSubWot = frame_support::traits::ConstBool<true>; - type MinCertForMembership = SmithMinCertForMembership; - type MinCertForCreateIdtyRight = frame_support::traits::ConstU32<0>; - type FirstIssuableOn = SmithFirstIssuableOn; - type IsDistanceOk = crate::traits::DistanceAlwaysOk; -} - -// SmithMembership -parameter_types! { - pub const SmithMembershipPeriod: u64 = 20; -} - -impl pallet_membership::Config<Instance2> for Test { - type CheckMembershipCallAllowed = SmithSubWot; - type IdtyId = IdtyIndex; - type IdtyIdOf = IdentityIndexOf<Self>; - type AccountIdOf = (); - type MembershipPeriod = SmithMembershipPeriod; - type OnEvent = SmithSubWot; - type WeightInfo = (); - type RuntimeEvent = RuntimeEvent; - #[cfg(feature = "runtime-benchmarks")] - type BenchmarkSetupHandler = (); -} - -// SmithCert -parameter_types! { - pub const SmithMaxByIssuer: u8 = 8; - pub const SmithMinReceivedCertToBeAbleToIssueCert: u32 = 2; - pub const SmithCertPeriod: u64 = 2; - pub const SmithValidityPeriod: u64 = 10; -} - -impl pallet_certification::Config<Instance2> for Test { - type CertPeriod = SmithCertPeriod; - type IdtyIndex = IdtyIndex; - type OwnerKeyOf = Identity; - type CheckCertAllowed = SmithSubWot; - type MaxByIssuer = SmithMaxByIssuer; - type MinReceivedCertToBeAbleToIssueCert = SmithMinReceivedCertToBeAbleToIssueCert; - type OnNewcert = SmithSubWot; - type OnRemovedCert = SmithSubWot; - type RuntimeEvent = RuntimeEvent; - type WeightInfo = (); - type ValidityPeriod = SmithValidityPeriod; -} - pub const NAMES: [&str; 6] = ["Alice", "Bob", "Charlie", "Dave", "Eve", "Ferdie"]; // Build genesis storage according to the mock runtime. -pub fn new_test_ext( - initial_identities_len: usize, - initial_smiths_len: usize, -) -> sp_io::TestExternalities { +pub fn new_test_ext(initial_identities_len: usize) -> sp_io::TestExternalities { let mut t = frame_system::GenesisConfig::default() .build_storage::<Test>() .unwrap(); @@ -289,28 +228,6 @@ pub fn new_test_ext( .assimilate_storage(&mut t) .unwrap(); - pallet_membership::GenesisConfig::<Test, Instance2> { - memberships: (1..=initial_smiths_len) - .map(|i| { - ( - i as u32, - sp_membership::MembershipData { - expire_on: SmithMembershipPeriod::get(), - }, - ) - }) - .collect(), - } - .assimilate_storage(&mut t) - .unwrap(); - - pallet_certification::GenesisConfig::<Test, Instance2> { - apply_cert_period_at_genesis: true, - certs_by_receiver: clique_wot(initial_smiths_len, SmithValidityPeriod::get()), - } - .assimilate_storage(&mut t) - .unwrap(); - frame_support::BasicExternalities::execute_with_storage(&mut t, || { // manually increment genesis identities sufficient counter // In real world, this is done by pallet-identity @@ -332,9 +249,6 @@ pub fn run_to_block(n: u64) { Identity::on_finalize(System::block_number()); Membership::on_finalize(System::block_number()); Cert::on_finalize(System::block_number()); - SmithSubWot::on_finalize(System::block_number()); - SmithMembership::on_finalize(System::block_number()); - SmithCert::on_finalize(System::block_number()); System::on_finalize(System::block_number()); // reset events and change block number System::reset_events(); @@ -345,9 +259,6 @@ pub fn run_to_block(n: u64) { Identity::on_initialize(System::block_number()); Membership::on_initialize(System::block_number()); Cert::on_initialize(System::block_number()); - SmithSubWot::on_initialize(System::block_number()); - SmithMembership::on_initialize(System::block_number()); - SmithCert::on_initialize(System::block_number()); } } diff --git a/pallets/duniter-wot/src/tests.rs b/pallets/duniter-wot/src/tests.rs index 3645f1979fca7d946fbddf0b408c7361467d9f3e..12a1851170672f47a1c80abe1a63bf296ac0453b 100644 --- a/pallets/duniter-wot/src/tests.rs +++ b/pallets/duniter-wot/src/tests.rs @@ -17,11 +17,10 @@ use crate::mock::*; use crate::pallet as pallet_duniter_wot; use codec::Encode; -use frame_support::instances::{Instance1, Instance2}; +use frame_support::instances::Instance1; use frame_support::{assert_noop, assert_ok}; use pallet_identity::{ - IdtyIndexAccountIdPayload, IdtyName, IdtyStatus, RevocationPayload, RevocationReason, - NEW_OWNER_KEY_PAYLOAD_PREFIX, REVOCATION_PAYLOAD_PREFIX, + IdtyName, IdtyStatus, RevocationPayload, RevocationReason, REVOCATION_PAYLOAD_PREFIX, }; use pallet_membership::MembershipRemovalReason; use sp_runtime::testing::TestSignature; @@ -30,7 +29,7 @@ use sp_runtime::testing::TestSignature; /// and good identity and certification metadate #[test] fn test_genesis_build() { - new_test_ext(3, 2).execute_with(|| { + new_test_ext(3).execute_with(|| { run_to_block(1); // Verify state assert_eq!(Identity::identities_count(), 3); @@ -45,7 +44,7 @@ fn test_genesis_build() { /// test that Alice is not able to create an identity when she received too few certs (2 of 4) #[test] fn test_creator_not_allowed_to_create_idty() { - new_test_ext(3, 2).execute_with(|| { + new_test_ext(3).execute_with(|| { run_to_block(1); // Alice did not receive enough certs @@ -61,7 +60,7 @@ fn test_creator_not_allowed_to_create_idty() { /// test that Alice is able to create an identity when she received enough certs (4) #[test] fn test_creator_allowed_to_create_idty() { - new_test_ext(5, 2).execute_with(|| { + new_test_ext(5).execute_with(|| { run_to_block(2); // Alice should be able to create an identity @@ -72,112 +71,10 @@ fn test_creator_allowed_to_create_idty() { }); } -/// test smith joining workflow -#[test] -fn test_join_smiths() { - new_test_ext(5, 3).execute_with(|| { - run_to_block(2); - - // Alice should be able to send a smith cert to Dave - run_to_block(3); - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(1), 1, 4)); - - // Bob should be able to send a smith cert to Dave - run_to_block(4); - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(2), 2, 4)); - - // Dave should be able to claim his membership - run_to_block(4); - assert_ok!(SmithMembership::claim_membership(RuntimeOrigin::signed(4),)); - System::assert_has_event(RuntimeEvent::SmithMembership( - pallet_membership::Event::MembershipAdded { - member: 4, - expire_on: 4 - + <Test as pallet_membership::Config<Instance2>>::MembershipPeriod::get(), - }, - )); - }); -} - -/// test smith membership expiry after cert expiration -#[test] -fn test_smith_certs_expirations_should_expire_smith_membership() { - new_test_ext(5, 3).execute_with(|| { - // After block #10, alice membership should be revoked due to smith certs expiration - run_to_block(10); - System::assert_has_event(RuntimeEvent::SmithMembership( - pallet_membership::Event::MembershipRemoved { - member: 1, - reason: MembershipRemovalReason::NotEnoughCerts, - }, - )); - }); -} - -/// test that smith can not change owner key -#[test] -fn test_smith_member_cant_change_its_idty_address() { - new_test_ext(5, 3).execute_with(|| { - run_to_block(2); - - let genesis_hash = System::block_hash(0); - let new_key_payload = IdtyIndexAccountIdPayload { - genesis_hash: &genesis_hash, - idty_index: 3u32, - old_owner_key: &3u64, - }; - - // Identity 3 can't change it's address - assert_noop!( - Identity::change_owner_key( - RuntimeOrigin::signed(3), - 13, - TestSignature(13, (NEW_OWNER_KEY_PAYLOAD_PREFIX, new_key_payload).encode()) - ), - pallet_duniter_wot::Error::<Test, Instance2>::NotAllowedToChangeIdtyAddress - ); - }); -} - -/// members of the smith subwot can revoke their identity -#[test] -fn test_smith_member_can_revoke_its_idty() { - new_test_ext(5, 3).execute_with(|| { - run_to_block(2); - - let revocation_payload = RevocationPayload { - idty_index: 3u32, - genesis_hash: System::block_hash(0), - }; - - // Identity 3 can't change it's address - assert_ok!(Identity::revoke_identity( - RuntimeOrigin::signed(3), - 3, - 3, - TestSignature(3, (REVOCATION_PAYLOAD_PREFIX, revocation_payload).encode()) - )); - // membership should be removed - System::assert_has_event(RuntimeEvent::Membership( - pallet_membership::Event::MembershipRemoved { - member: 3, - reason: pallet_membership::MembershipRemovalReason::Revoked, - }, - )); - // smith membership should be removed as well - System::assert_has_event(RuntimeEvent::SmithMembership( - pallet_membership::Event::MembershipRemoved { - member: 3, - reason: pallet_membership::MembershipRemovalReason::Revoked, - }, - )); - }); -} - /// test identity creation and that a first cert is emitted #[test] fn test_create_idty_ok() { - new_test_ext(5, 2).execute_with(|| { + new_test_ext(5).execute_with(|| { run_to_block(2); // Alice should be able to create an identity at block #2 @@ -205,7 +102,7 @@ fn test_create_idty_ok() { /// test identity validation #[test] fn test_new_idty_validation() { - new_test_ext(5, 2).execute_with(|| { + new_test_ext(5).execute_with(|| { // Alice creates Ferdie identity run_to_block(2); assert_ok!(Identity::create_identity(RuntimeOrigin::signed(1), 6)); @@ -258,7 +155,7 @@ fn test_new_idty_validation() { /// test that Ferdie can confirm an identity created for him by Alice #[test] fn test_confirm_idty_ok() { - new_test_ext(5, 2).execute_with(|| { + new_test_ext(5).execute_with(|| { run_to_block(2); // Alice creates Ferdie identity @@ -286,7 +183,7 @@ fn test_confirm_idty_ok() { /// - anyone can submit a revocation certificate signed by bob #[test] fn test_revoke_idty() { - new_test_ext(5, 1).execute_with(|| { + new_test_ext(5).execute_with(|| { run_to_block(2); // Alice identity can be revoked @@ -313,13 +210,6 @@ fn test_revoke_idty() { reason: pallet_membership::MembershipRemovalReason::Revoked, }, )); - // and her smith membership should be removed as well - System::assert_has_event(RuntimeEvent::SmithMembership( - pallet_membership::Event::MembershipRemoved { - member: 1, - reason: pallet_membership::MembershipRemovalReason::Revoked, - }, - )); // Anyone should be able to submit Bob revocation certificate assert_ok!(Identity::revoke_identity( @@ -351,7 +241,7 @@ fn test_revoke_idty() { /// test that expired membership lose the identity after a delay #[test] fn test_idty_membership_expire() { - new_test_ext(3, 2).execute_with(|| { + new_test_ext(3).execute_with(|| { run_to_block(4); // Alice renews her membership @@ -432,7 +322,7 @@ fn test_idty_membership_expire() { /// when an identity is confirmed and not validated, the certification received should be removed #[test] fn test_unvalidated_idty_certs_removal() { - new_test_ext(5, 2).execute_with(|| { + new_test_ext(5).execute_with(|| { // Alice creates Ferdie identity run_to_block(2); assert_ok!(Identity::create_identity(RuntimeOrigin::signed(1), 6)); @@ -455,50 +345,24 @@ fn test_unvalidated_idty_certs_removal() { /// test what happens when certification expire #[test] fn test_certification_expire() { - new_test_ext(3, 3).execute_with(|| { + new_test_ext(3).execute_with(|| { // smith cert Bob → Alice not renewed // cert Bob → Alice not renewed // --- BLOCK 2 --- run_to_block(2); - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(1), 1, 2)); - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(2), 2, 3)); - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(3), 3, 1)); assert_ok!(Cert::add_cert(RuntimeOrigin::signed(1), 1, 2)); assert_ok!(Cert::add_cert(RuntimeOrigin::signed(2), 2, 3)); assert_ok!(Cert::add_cert(RuntimeOrigin::signed(3), 3, 1)); // --- BLOCK 4 --- run_to_block(4); - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(1), 1, 3)); - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(3), 3, 2)); assert_ok!(Cert::add_cert(RuntimeOrigin::signed(1), 1, 3)); assert_ok!(Cert::add_cert(RuntimeOrigin::signed(3), 3, 2)); // --- BLOCK 7 --- run_to_block(7); - assert_ok!(SmithMembership::renew_membership(RuntimeOrigin::signed(1))); - assert_ok!(SmithMembership::renew_membership(RuntimeOrigin::signed(2))); - assert_ok!(SmithMembership::renew_membership(RuntimeOrigin::signed(3))); assert_ok!(Membership::renew_membership(RuntimeOrigin::signed(1))); assert_ok!(Membership::renew_membership(RuntimeOrigin::signed(2))); assert_ok!(Membership::renew_membership(RuntimeOrigin::signed(3))); - // smith cert Bob → Alice expires at block 10 - run_to_block(10); - // println!("{:?}", System::events()); - System::assert_has_event(RuntimeEvent::SmithCert( - pallet_certification::Event::CertRemoved { - issuer: 2, // Bob - receiver: 1, // Alice - expiration: true, - }, - )); - // in consequence, since Alice has only 1/2 smith certification remaining, she looses smith membership - System::assert_has_event(RuntimeEvent::SmithMembership( - pallet_membership::Event::MembershipRemoved { - member: 1, - reason: MembershipRemovalReason::NotEnoughCerts, - }, - )); - // --- BLOCK 14 --- run_to_block(14); assert_ok!(Membership::renew_membership(RuntimeOrigin::signed(1))); @@ -559,78 +423,27 @@ fn test_certification_expire() { // - when source or target membership is pending (both wot) #[test] fn test_cert_can_not_be_issued() { - new_test_ext(4, 3).execute_with(|| { + new_test_ext(4).execute_with(|| { // smith cert Bob → Alice not renewed // cert Bob → Alice not renewed // --- BLOCK 2 --- run_to_block(2); - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(1), 1, 2)); // +10 - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(2), 2, 3)); // +10 - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(3), 3, 1)); // +10 assert_ok!(Cert::add_cert(RuntimeOrigin::signed(1), 1, 2)); // +20 assert_ok!(Cert::add_cert(RuntimeOrigin::signed(2), 2, 3)); // +20 assert_ok!(Cert::add_cert(RuntimeOrigin::signed(3), 3, 4)); // +20 assert_ok!(Cert::add_cert(RuntimeOrigin::signed(4), 4, 1)); // +20 // --- BLOCK 4 --- run_to_block(4); - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(1), 1, 3)); // +10 - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(3), 3, 2)); // +10 assert_ok!(Cert::add_cert(RuntimeOrigin::signed(2), 2, 4)); // +20 assert_ok!(Cert::add_cert(RuntimeOrigin::signed(3), 3, 2)); // +20 assert_ok!(Cert::add_cert(RuntimeOrigin::signed(4), 4, 3)); // +20 // --- BLOCK 7 --- run_to_block(7); - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(3), 3, 1)); // +10 - assert_ok!(SmithMembership::renew_membership(RuntimeOrigin::signed(1))); // +20 - assert_ok!(SmithMembership::renew_membership(RuntimeOrigin::signed(2))); // +20 - assert_ok!(SmithMembership::renew_membership(RuntimeOrigin::signed(3))); // +20 assert_ok!(Membership::renew_membership(RuntimeOrigin::signed(1))); // + 8 assert_ok!(Membership::renew_membership(RuntimeOrigin::signed(2))); // + 8 assert_ok!(Membership::renew_membership(RuntimeOrigin::signed(3))); // + 8 assert_ok!(Membership::renew_membership(RuntimeOrigin::signed(4))); // + 8 - // smith cert Bob → Alice expires at block 10 - run_to_block(10); - // println!("{:?}", System::events()); - System::assert_has_event(RuntimeEvent::SmithCert( - pallet_certification::Event::CertRemoved { - issuer: 2, // Bob - receiver: 1, // Alice - expiration: true, - }, - )); - // in consequence, since Alice has only 1/2 smith certification remaining, she looses smith membership - System::assert_has_event(RuntimeEvent::SmithMembership( - pallet_membership::Event::MembershipRemoved { - member: 1, - reason: MembershipRemovalReason::NotEnoughCerts, - }, - )); - - run_to_block(11); - // Dave should be able to receive a smith cert since - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(3), 3, 4)); - // Bob renews his smith certification towards Alice - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(2), 2, 1)); - // now Alice has enough certs - assert_eq!( - SmithCert::idty_cert_meta(1), - pallet_certification::IdtyCertMeta { - issued_count: 2, // → Bob, → Charlie - // since Alice got back to min number of received certs to be able to issue cert - // her next_issuable_on was updated to block_number (11) + cert_period (2) - next_issuable_on: 13, // 11 + 2 - received_count: 2 // ↠Bob, ↠Charlie - } - ); - - // because Alice did not claim membership, she is not member at this point - assert_eq!(SmithMembership::membership(1), None); - run_to_block(13); - // /!\ COUNTERINTUITIVE BEHAVIOR - // Alice is not smith member, she should not be able to issue cert - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(1), 1, 2)); - run_to_block(14); assert_ok!(Membership::renew_membership(RuntimeOrigin::signed(1))); // + 8 assert_ok!(Membership::renew_membership(RuntimeOrigin::signed(2))); // + 8 @@ -669,54 +482,3 @@ fn test_cert_can_not_be_issued() { // she would have been able to emit a cert without being member }) } - -/// test non smith identity can not emit smith cert -#[test] -fn test_non_smith_can_not_issue_smith_cert() { - new_test_ext(4, 3).execute_with(|| { - assert_noop!( - SmithCert::add_cert(RuntimeOrigin::signed(4), 4, 1), - pallet_certification::Error::<Test, Instance2>::NotEnoughCertReceived - ); - }) -} - -/// FIXME this test should not fail with NotRespectCertPeriod but NotSmith or somthing like that -/// test non smith identity can not emit smith cert -#[test] -fn test_non_smith_with_certs_can_not_issue_smith_cert() { - new_test_ext(4, 3).execute_with(|| { - run_to_block(2); - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(1), 1, 4)); - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(2), 2, 4)); - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(3), 3, 4)); - assert_noop!( - SmithCert::add_cert(RuntimeOrigin::signed(4), 4, 1), - pallet_certification::Error::<Test, Instance2>::NotRespectCertPeriod - ); - }) -} - -/// FIXME this test should not succeed because Dave should not be able to issue cert -/// after revocation of his smith membership -/// test non smith identity can not emit smith cert -#[test] -fn test_revoked_smith_with_certs_can_not_issue_smith_cert() { - new_test_ext(4, 3).execute_with(|| { - run_to_block(2); - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(1), 1, 4)); - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(2), 2, 4)); - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(3), 3, 4)); - assert_ok!(SmithMembership::claim_membership(RuntimeOrigin::signed(4))); - assert_noop!( - SmithCert::add_cert(RuntimeOrigin::signed(4), 4, 1), - pallet_certification::Error::<Test, Instance2>::NotRespectCertPeriod - ); - run_to_block(4); - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(4), 4, 1),); - run_to_block(5); - assert_ok!(SmithMembership::revoke_membership(RuntimeOrigin::signed(4))); - run_to_block(6); - assert_ok!(SmithCert::add_cert(RuntimeOrigin::signed(4), 4, 1),); - }) -} diff --git a/pallets/membership/src/lib.rs b/pallets/membership/src/lib.rs index 4f44e11dce4bdee1559422732cf30d2309c47f37..4b8fd399301241c3f27afd90104c93ae5aa88f34 100644 --- a/pallets/membership/src/lib.rs +++ b/pallets/membership/src/lib.rs @@ -314,7 +314,7 @@ pub mod pallet { } /// check if identity is member - pub(super) fn is_member(idty_id: &T::IdtyId) -> bool { + pub fn is_member(idty_id: &T::IdtyId) -> bool { Membership::<T, I>::contains_key(idty_id) } } diff --git a/pallets/smith-members/Cargo.toml b/pallets/smith-members/Cargo.toml index 5ee739b1ecbd8e3c3867d4818d1b9a74e31883dc..4987f4696f28e70094b74377e277c60daf35d46b 100644 --- a/pallets/smith-members/Cargo.toml +++ b/pallets/smith-members/Cargo.toml @@ -23,6 +23,7 @@ frame-support = { git = 'https://github.com/duniter/substrate', branch = 'dunite frame-system = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42', default-features = false } pallet-balances = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42', default-features = false } sp-runtime = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42', default-features = false } +sp-staking = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42', default-features = false } sp-std = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42', default-features = false } [dev-dependencies] @@ -41,6 +42,7 @@ std = [ "scale-info/std", "serde", "sp-runtime/std", + "sp-staking/std", "sp-std/std", ] runtime-benchmarks = [] diff --git a/pallets/smith-members/src/impls.rs b/pallets/smith-members/src/impls.rs index 1fd68c58f869c03d84fd42f2f9f508166fb7589a..937aa5104175cb9ad938c19c7dccc4cbcc04b294 100644 --- a/pallets/smith-members/src/impls.rs +++ b/pallets/smith-members/src/impls.rs @@ -5,10 +5,9 @@ use pallet_identity::IdtyEvent; use sp_runtime::traits::Convert; /// We want to remove a Smith when he is removed from the higher level set of "authorities". -impl<T: Config> pallet_authority_members::OnRemovedMember<T> for Pallet<T> { - fn on_removed_member(_: T) { +impl<T: Config> pallet_authority_members::OnRemovedMember<T::MemberId> for Pallet<T> { + fn on_removed_member(_: T::MemberId) { todo!("Remove smith as well") - // TODO: not sure if we should listen authority-members or rather the initial pallet that called the removal } } diff --git a/pallets/smith-members/src/lib.rs b/pallets/smith-members/src/lib.rs index c8f929c3f32af0ed66ad62db2086ac0f6cfde759..5f8120461ba6537f1f10333c2f573a55e1eb4a79 100644 --- a/pallets/smith-members/src/lib.rs +++ b/pallets/smith-members/src/lib.rs @@ -23,7 +23,7 @@ mod mock; mod tests; mod impls; -mod traits; +pub mod traits; mod types; use codec::{Codec, Decode, Encode}; @@ -68,7 +68,9 @@ pub mod pallet { use frame_support::traits::StorageVersion; use pallet_authority_members::SessionIndex; use sp_runtime::traits::{Convert, IsMember}; - use std::collections::BTreeMap; + use sp_std::collections::btree_map::BTreeMap; + use sp_std::vec; + use sp_std::vec::Vec; const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); @@ -137,14 +139,14 @@ pub mod pallet { #[pallet::genesis_config] pub struct GenesisConfig<T: Config> { - pub certs_by_receiver: BTreeMap<T::IdtyIndex, Vec<T::IdtyIndex>>, + pub initial_smiths: BTreeMap<T::IdtyIndex, (bool, Vec<T::IdtyIndex>)>, } #[cfg(feature = "std")] impl<T: Config> Default for GenesisConfig<T> { fn default() -> Self { Self { - certs_by_receiver: Default::default(), + initial_smiths: Default::default(), } } } @@ -154,7 +156,7 @@ pub mod pallet { fn build(&self) { CurrentSession::<T>::put(0); let mut cert_meta_by_issuer = BTreeMap::<T::IdtyIndex, Vec<T::IdtyIndex>>::new(); - for (receiver, issuers) in &self.certs_by_receiver { + for (receiver, (is_online, issuers)) in &self.initial_smiths { // Forbid self-cert assert!( !issuers.contains(receiver), @@ -183,9 +185,11 @@ pub mod pallet { receiver, SmithMeta { status: smith_status, - expires_on: Some( - CurrentSession::<T>::get() + T::InactivityMaxDuration::get(), - ), + expires_on: if *is_online { + None + } else { + Some(CurrentSession::<T>::get() + T::InactivityMaxDuration::get()) + }, issued_certs: vec![], received_certs: issuers_, }, @@ -210,16 +214,19 @@ pub mod pallet { /// maps identity index to smith status #[pallet::storage] + #[pallet::getter(fn smiths)] pub type Smiths<T: Config> = StorageMap<_, Twox64Concat, T::IdtyIndex, SmithMeta<T::IdtyIndex>, OptionQuery>; /// maps session index to possible smith removals #[pallet::storage] + #[pallet::getter(fn expires_on)] pub type ExpiresOn<T: Config> = StorageMap<_, Twox64Concat, SessionIndex, Vec<T::IdtyIndex>, OptionQuery>; /// maps session index to possible smith removals #[pallet::storage] + #[pallet::getter(fn current_session)] pub type CurrentSession<T: Config> = StorageValue<_, SessionIndex, ValueQuery>; // ERRORS // @@ -463,7 +470,9 @@ impl<T: Config> Pallet<T> { } fn do_exclude_removed_wot_member(idty_index: T::IdtyIndex) { - Self::do_exclude_smith(idty_index, SmithRemovalReason::LostMembership); + if Smiths::<T>::get(idty_index).is_some() { + Self::do_exclude_smith(idty_index, SmithRemovalReason::LostMembership); + } } fn do_exclude_smith(idty_index: T::IdtyIndex, reason: SmithRemovalReason) { @@ -493,7 +502,7 @@ impl<T: Config> Pallet<T> { } // TODO: return what? - fn smith_goes_online(idty_index: T::IdtyIndex) { + pub fn smith_goes_online(idty_index: T::IdtyIndex) { if let Some(smith_meta) = Smiths::<T>::get(idty_index) { if smith_meta.expires_on.is_some() { Smiths::<T>::mutate(idty_index, |maybe_smith_meta| { diff --git a/pallets/smith-members/src/tests.rs b/pallets/smith-members/src/tests.rs index 82c57582f2f218b337879d7756a4f5f28de559b2..0081ce97f37d8117b261e1297a1bc55b6916c87a 100644 --- a/pallets/smith-members/src/tests.rs +++ b/pallets/smith-members/src/tests.rs @@ -28,11 +28,11 @@ use pallet_authority_members::OnNewSession; #[test] fn process_to_become_a_smith_and_lose_it() { new_test_ext(GenesisConfig { - certs_by_receiver: btreemap![ - 1 => vec![2, 3, 4], - 2 => vec![3, 4], - 3 => vec![], - 4 => vec![], + initial_smiths: btreemap![ + 1 => (false, vec![2, 3, 4]), + 2 => (false, vec![3, 4]), + 3 => (false, vec![]), + 4 => (false, vec![]), ], }) .execute_with(|| { @@ -162,11 +162,11 @@ fn process_to_become_a_smith_and_lose_it() { #[test] fn should_have_checks_on_certify() { new_test_ext(GenesisConfig { - certs_by_receiver: btreemap![ - 1 => vec![2, 3, 4], - 2 => vec![3, 4], - 3 => vec![4], - 4 => vec![1, 2], + initial_smiths: btreemap![ + 1 => (false, vec![2, 3, 4]), + 2 => (false, vec![3, 4]), + 3 => (false, vec![4]), + 4 => (false, vec![1, 2]), ], }) .execute_with(|| { @@ -259,11 +259,11 @@ fn should_have_checks_on_certify() { #[test] fn smith_activity_postpones_expiration() { new_test_ext(GenesisConfig { - certs_by_receiver: btreemap![ - 1 => vec![2, 3, 4], - 2 => vec![3, 4], - 3 => vec![], - 4 => vec![] + initial_smiths: btreemap![ + 1 => (false, vec![2, 3, 4]), + 2 => (false, vec![3, 4]), + 3 => (false, vec![]), + 4 => (false, vec![]) ], }) .execute_with(|| { @@ -346,11 +346,11 @@ fn smith_activity_postpones_expiration() { #[test] fn smith_coming_back_recovers_its_issued_certs() { new_test_ext(GenesisConfig { - certs_by_receiver: btreemap![ - 1 => vec![2, 3, 4], - 2 => vec![3, 4], - 3 => vec![1, 4], - 4 => vec![] + initial_smiths: btreemap![ + 1 => (false, vec![2, 3, 4]), + 2 => (false, vec![3, 4]), + 3 => (false, vec![1, 4]), + 4 => (false, vec![]), ], }) .execute_with(|| { @@ -414,11 +414,11 @@ fn smith_coming_back_recovers_its_issued_certs() { #[test] fn certifying_on_different_status() { new_test_ext(GenesisConfig { - certs_by_receiver: btreemap![ - 1 => vec![2, 3, 4], - 2 => vec![3, 4], - 3 => vec![1, 2], - 4 => vec![], + initial_smiths: btreemap![ + 1 => (false, vec![2, 3, 4]), + 2 => (false, vec![3, 4]), + 3 => (false, vec![1, 2]), + 4 => (false, vec![]), ], }) .execute_with(|| { @@ -481,11 +481,11 @@ fn certifying_on_different_status() { #[test] fn invitation_on_non_wot_member() { new_test_ext(GenesisConfig { - certs_by_receiver: btreemap![ - 1 => vec![2, 3, 4], - 2 => vec![3, 4], - 3 => vec![1, 2], - 4 => vec![], + initial_smiths: btreemap![ + 1 => (false, vec![2, 3, 4]), + 2 => (false, vec![3, 4]), + 3 => (false, vec![1, 2]), + 4 => (false, vec![]), ], }) .execute_with(|| { @@ -505,11 +505,11 @@ fn invitation_on_non_wot_member() { #[test] fn losing_wot_membership_cascades_to_smith_members() { new_test_ext(GenesisConfig { - certs_by_receiver: btreemap![ - 1 => vec![2, 3, 4], - 2 => vec![3, 4], - 3 => vec![1, 2], - 4 => vec![], + initial_smiths: btreemap![ + 1 => (false, vec![2, 3, 4]), + 2 => (false, vec![3, 4]), + 3 => (false, vec![1, 2]), + 4 => (false, vec![]), ], }) .execute_with(|| { diff --git a/pallets/smith-members/src/types.rs b/pallets/smith-members/src/types.rs index a6591f0e3e74b74c6b22bd6fa8ef80c3fda0daab..3a447cb8bdd2767a33f2b6fa12197317714569f4 100644 --- a/pallets/smith-members/src/types.rs +++ b/pallets/smith-members/src/types.rs @@ -19,9 +19,9 @@ use crate::SmithStatus; use codec::{Decode, Encode}; use frame_support::pallet_prelude::*; -#[cfg(feature = "std")] -use pallet_authority_members::SessionIndex; use scale_info::TypeInfo; +use sp_staking::SessionIndex; +use sp_std::vec::Vec; /// certification metadata attached to an identity #[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)] diff --git a/resources/gdev.yaml b/resources/gdev.yaml index 80296c9699bd4309bd795bcd3ea1e1bc74075a78..a901457535966732d744522220a1b26ce5dfe1ee 100644 --- a/resources/gdev.yaml +++ b/resources/gdev.yaml @@ -41,20 +41,10 @@ parameters: wot_min_cert_for_create_idty_right: 3 # ----- SMITH WOT ----- - # Duration to wait between two emitted certifications, in blocks. 14400 blocks = 24h = 1 day. - smith_cert_period: 14400 # Maximum quantity of currently valid certifications emitted by a same issuer. 15 certs. smith_cert_max_by_issuer: 15 - # Minimum quantity of smith certifications received to be able to smith certify someone else. - smith_cert_min_received_cert_to_issue_cert: 3 - # Validity duration of a certification, in blocks. 2102400 blocks = 146 days. - smith_cert_validity_period: 2102400 - # Validity duration of a membership. 1051200 blocks = 73 days. - smith_membership_period: 1051200 - # Validity duration of a pending membership. 172800 blocks = 12 days. - smith_pending_membership_period: 172800 - # Delay a new smith member must observe before being able to emit a smith certification. 14400 blocks = 24h = 1 day. - smith_wot_first_cert_issuable_on: 14400 + # Maximum duration a smith can be offline without being excluded + smith_inactivity_max_duration: 48 # Number of required received smith certs to become a smith member smith_wot_min_cert_for_membership: 3 diff --git a/runtime/common/Cargo.toml b/runtime/common/Cargo.toml index 620eb3c8c23fbc1ebbc97cec505f161934325897..04ca8dd0cb301d281b95f7a23ae3e6821c12f2d4 100644 --- a/runtime/common/Cargo.toml +++ b/runtime/common/Cargo.toml @@ -35,6 +35,7 @@ std = [ 'frame-system/std', 'log/std', 'pallet-authority-members/std', + 'pallet-smith-members/std', 'pallet-babe/std', 'pallet-balances/std', 'pallet-certification/std', @@ -73,6 +74,7 @@ try-runtime = [ [dependencies] duniter-primitives = { path = '../../primitives/duniter', default-features = false } pallet-authority-members = { path = '../../pallets/authority-members', default-features = false } +pallet-smith-members = { path = '../../pallets/smith-members', default-features = false } pallet-certification = { path = '../../pallets/certification', default-features = false } pallet-distance = { path = "../../pallets/distance", default-features = false } pallet-duniter-account = { path = '../../pallets/duniter-account', default-features = false } diff --git a/runtime/common/src/handlers.rs b/runtime/common/src/handlers.rs index a8fbd18a56f9cf450e83aa37f86cbdca3da168c9..57922ab7f56b02ab036312be9b757f3b10703645 100644 --- a/runtime/common/src/handlers.rs +++ b/runtime/common/src/handlers.rs @@ -17,9 +17,9 @@ use super::entities::*; use super::{AccountId, IdtyIndex}; use frame_support::dispatch::UnfilteredDispatchable; -use frame_support::instances::{Instance1, Instance2}; +use frame_support::instances::Instance1; use frame_support::pallet_prelude::Weight; -use frame_support::Parameter; +use pallet_smith_members::SmithRemovalReason; // new session handler pub struct OnNewSessionHandler<Runtime>(core::marker::PhantomData<Runtime>); @@ -75,58 +75,15 @@ impl< } } -// smith membership event handler -pub struct OnSmithMembershipEventHandler<Inner, Runtime>( - core::marker::PhantomData<(Inner, Runtime)>, -); -impl< - IdtyIndex: Copy + Parameter, - Inner: sp_membership::traits::OnEvent<IdtyIndex>, - Runtime: frame_system::Config<AccountId = AccountId> - + pallet_identity::Config<IdtyIndex = IdtyIndex> - + pallet_authority_members::Config<MemberId = IdtyIndex> - + pallet_membership::Config<Instance2>, - > sp_membership::traits::OnEvent<IdtyIndex> for OnSmithMembershipEventHandler<Inner, Runtime> -{ - fn on_event(membership_event: &sp_membership::Event<IdtyIndex>) { - (match membership_event { - sp_membership::Event::MembershipAdded(_idty_index) => { - // nothing when smith membership acquired - // user will have to claim authority membership - } - sp_membership::Event::MembershipRemoved(idty_index) => { - // TODO: recursive call? (see authority-members -> revoke_membership) - let call = pallet_authority_members::Call::<Runtime>::remove_member { - member_id: *idty_index, - }; - if let Err(e) = - call.dispatch_bypass_filter(frame_system::Origin::<Runtime>::Root.into()) - { - sp_std::if_std! { - println!("faid to remove member: {:?}", e) - } - } - } - _ => (), - }); - Inner::on_event(membership_event) - } -} - // authority member removal handler -// TODO refac smith wot -// or document the link between smith membership and authority member pub struct OnRemovedAuthorityMemberHandler<Runtime>(core::marker::PhantomData<Runtime>); -impl<Runtime> pallet_authority_members::traits::OnRemovedMember<IdtyIndex> +impl<Runtime> pallet_authority_members::traits::OnRemovedMember<Runtime::MemberId> for OnRemovedAuthorityMemberHandler<Runtime> where - Runtime: frame_system::Config + pallet_membership::Config<Instance2, IdtyId = IdtyIndex>, + Runtime: pallet_smith_members::Config, { - fn on_removed_member(idty_index: IdtyIndex) { - pallet_membership::Pallet::<Runtime, Instance2>::do_remove_membership( - idty_index, - pallet_membership::MembershipRemovalReason::System, - ); + fn on_removed_member(idty_index: Runtime::MemberId) { + pallet_smith_members::Pallet::<Runtime>::on_removed_member(idty_index); } } @@ -145,3 +102,21 @@ where *missed_any = true; } } + +pub struct OnSmithDeletedHandler<Runtime>(core::marker::PhantomData<Runtime>); +impl<Runtime> pallet_smith_members::traits::OnSmithDelete<Runtime::MemberId> + for OnSmithDeletedHandler<Runtime> +where + Runtime: pallet_authority_members::Config, +{ + fn on_smith_delete(idty_index: Runtime::MemberId, _reason: SmithRemovalReason) { + let call = pallet_authority_members::Call::<Runtime>::remove_member { + member_id: idty_index, + }; + if let Err(e) = call.dispatch_bypass_filter(frame_system::Origin::<Runtime>::Root.into()) { + sp_std::if_std! { + println!("faid to remove member: {:?}", e) + } + } + } +} diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs index d1779940a754f7132be8e17f5f4b76bb7b1dc2aa..90a9b220224a563fb529c3ed99458f97f493c65b 100644 --- a/runtime/common/src/pallets_config.rs +++ b/runtime/common/src/pallets_config.rs @@ -233,7 +233,7 @@ macro_rules! pallets_config { } impl pallet_authority_members::Config for Runtime { type RuntimeEvent = RuntimeEvent; - type IsMember = SmithMembership; + type IsMember = SmithMembers; type OnNewSession = OnNewSessionHandler<Runtime>; type OnRemovedMember = OnRemovedAuthorityMemberHandler<Runtime>; type MemberId = IdtyIndex; @@ -474,7 +474,7 @@ macro_rules! pallets_config { type ValidationPeriod = ValidationPeriod; type AutorevocationPeriod = AutorevocationPeriod; type DeletionPeriod = DeletionPeriod; - type CheckIdtyCallAllowed = (Wot, SmithSubWot); + type CheckIdtyCallAllowed = Wot; type IdtyCreationPeriod = IdtyCreationPeriod; type IdtyData = IdtyData; type IdtyIndex = IdtyIndex; @@ -482,7 +482,7 @@ macro_rules! pallets_config { type IdtyNameValidator = IdtyNameValidatorImpl; type Signer = <Signature as sp_runtime::traits::Verify>::Signer; type Signature = Signature; - type OnIdtyChange = (Wot, SmithSubWot, Quota); + type OnIdtyChange = (Wot, SmithMembers, Quota); type RuntimeEvent = RuntimeEvent; type WeightInfo = common_runtime::weights::pallet_identity::WeightInfo<Runtime>; #[cfg(feature = "runtime-benchmarks")] @@ -527,42 +527,18 @@ macro_rules! pallets_config { type WeightInfo = common_runtime::weights::pallet_distance::WeightInfo<Runtime>; } - // SMITHS SUB-WOT // - - use frame_support::instances::Instance2; - impl pallet_duniter_wot::Config<Instance2> for Runtime { - type FirstIssuableOn = SmithWotFirstCertIssuableOn; - type IsDistanceOk = pallet_duniter_wot::traits::DistanceAlwaysOk; - type IsSubWot = frame_support::traits::ConstBool<true>; - type MinCertForMembership = SmithWotMinCertForMembership; - type MinCertForCreateIdtyRight = frame_support::traits::ConstU32<0>; - } - - impl pallet_membership::Config<Instance2> for Runtime { - type CheckMembershipCallAllowed = SmithSubWot; - type IdtyId = IdtyIndex; - type IdtyIdOf = common_runtime::providers::IdentityIndexOf<Self>; - type AccountIdOf = common_runtime::providers::IdentityAccountIdProvider<Self>; - type MembershipPeriod = SmithMembershipPeriod; - type OnEvent = OnSmithMembershipEventHandler<SmithSubWot, Runtime>; + // SMITH-MEMBERS + impl pallet_smith_members::Config for Runtime { type RuntimeEvent = RuntimeEvent; - type WeightInfo = common_runtime::weights::pallet_membership_smith_membership::WeightInfo<Runtime>; - #[cfg(feature = "runtime-benchmarks")] - type BenchmarkSetupHandler = common_runtime::providers::BenchmarkSetupHandler<Runtime>; - } - - impl pallet_certification::Config<Instance2> for Runtime { - type CertPeriod = SmithCertPeriod; type IdtyIndex = IdtyIndex; - type OwnerKeyOf = Identity; - type CheckCertAllowed = SmithSubWot; + type IsWoTMember = common_runtime::providers::IsWoTMemberProvider<Runtime>; + type IdtyIdOf = common_runtime::providers::IdentityIndexOf<Self>; + type MinCertForMembership = SmithWotMinCertForMembership; type MaxByIssuer = SmithMaxByIssuer; - type MinReceivedCertToBeAbleToIssueCert = SmithMinReceivedCertToBeAbleToIssueCert; - type OnNewcert = SmithSubWot; - type OnRemovedCert = SmithSubWot; - type RuntimeEvent = RuntimeEvent; - type WeightInfo = common_runtime::weights::pallet_certification_smith_cert::WeightInfo<Runtime>; - type ValidityPeriod = SmithValidityPeriod; + type InactivityMaxDuration = InactivityMaxDuration; + type OnSmithDelete = OnSmithDeletedHandler<Runtime>; + type IdtyIdOfAuthorityId = sp_runtime::traits::ConvertInto; + type MemberId = IdtyIndex; } pub struct TechnicalCommitteeDefaultVote; diff --git a/runtime/common/src/providers.rs b/runtime/common/src/providers.rs index b9d23572036dd5ae23b1393adea35535e5cb0c4e..01da1ebe7519478c1c092e32b461bd80f86b7258 100644 --- a/runtime/common/src/providers.rs +++ b/runtime/common/src/providers.rs @@ -16,6 +16,7 @@ use crate::{entities::IdtyData, AccountId, IdtyIndex}; use core::marker::PhantomData; +use frame_support::instances::Instance1; use pallet_universal_dividend::FirstEligibleUd; use sp_runtime::DispatchError; use sp_std::boxed::Box; @@ -129,6 +130,18 @@ where } } +pub struct IsWoTMemberProvider<T>(PhantomData<T>); +impl<T: pallet_smith_members::Config> + sp_runtime::traits::IsMember<<T as pallet_membership::Config<Instance1>>::IdtyId> + for IsWoTMemberProvider<T> +where + T: pallet_distance::Config + pallet_membership::Config<Instance1>, +{ + fn is_member(idty_id: &T::IdtyId) -> bool { + pallet_membership::Pallet::<T, Instance1>::is_member(idty_id) + } +} + #[cfg(feature = "runtime-benchmarks")] pub struct BenchmarkSetupHandler<T>(PhantomData<T>); diff --git a/runtime/g1/Cargo.toml b/runtime/g1/Cargo.toml index e96b7a0843152daba2cc0b0414dda1942e52494e..f69d8e0ccb298f8a995f394706c4cd79da9b29ee 100644 --- a/runtime/g1/Cargo.toml +++ b/runtime/g1/Cargo.toml @@ -61,6 +61,7 @@ std = [ 'log/std', 'pallet-authority-discovery/std', 'pallet-authority-members/std', + 'pallet-smith-members/std', 'pallet-babe/std', 'pallet-balances/std', 'pallet-certification/std', @@ -134,6 +135,7 @@ sp-keyring = { git = 'https://github.com/duniter/substrate', branch = 'duniter-s # local common-runtime = { path = "../common", default-features = false } pallet-authority-members = { path = '../../pallets/authority-members', default-features = false } +pallet-smith-members = { path = '../../pallets/smith-members', default-features = false } pallet-certification = { path = '../../pallets/certification', default-features = false } pallet-distance = { path = "../../pallets/distance", default-features = false } pallet-duniter-test-parameters = { path = '../../pallets/duniter-test-parameters', default-features = false } diff --git a/runtime/g1/src/lib.rs b/runtime/g1/src/lib.rs index 8cef6bfcb4ef5219da5aa82056d79b3f256c8cf8..e86383d1a1d22fe6f5a1c68d5f4a969fb9306dba 100644 --- a/runtime/g1/src/lib.rs +++ b/runtime/g1/src/lib.rs @@ -66,6 +66,7 @@ use sp_version::NativeVersion; use sp_version::RuntimeVersion; // A few exports that help ease life for downstream crates. +use frame_support::instances::Instance2; pub use frame_support::{ construct_runtime, parameter_types, traits::{EqualPrivilegeOnly, KeyOwnerProofSystem, Randomness}, @@ -214,7 +215,9 @@ impl frame_support::traits::InstanceFilter<RuntimeCall> for ProxyType { // Some calls are never authorized from a proxied account !matches!( c, - RuntimeCall::Cert(..) | RuntimeCall::Identity(..) | RuntimeCall::SmithCert(..) + RuntimeCall::Cert(..) + | RuntimeCall::Identity(..) + | RuntimeCall::SmithMembers(..) ) } ProxyType::TransferOnly => { @@ -263,14 +266,15 @@ construct_runtime!( Quota: pallet_quota::{Pallet, Storage, Config<T>, Event<T>} = 66, // Consensus support. - AuthorityMembers: pallet_authority_members::{Pallet, Call, Storage, Config<T>, Event<T>} = 10, - Authorship: pallet_authorship::{Pallet, Storage} = 11, - Offences: pallet_offences::{Pallet, Storage, Event} = 12, - Historical: session_historical::{Pallet} = 13, - Session: pallet_session::{Pallet, Call, Storage, Event, Config<T>} = 14, - Grandpa: pallet_grandpa::{Pallet, Call, Storage, Config, Event, ValidateUnsigned} = 15, - ImOnline: pallet_im_online::{Pallet, Call, Storage, Event<T>, ValidateUnsigned, Config<T>} = 16, - AuthorityDiscovery: pallet_authority_discovery::{Pallet, Config} = 17, + SmithMembers: pallet_smith_members::{Pallet, Call, Storage, Config<T>, Event<T>} = 10, + AuthorityMembers: pallet_authority_members::{Pallet, Call, Storage, Config<T>, Event<T>} = 11, + Authorship: pallet_authorship::{Pallet, Storage} = 12, + Offences: pallet_offences::{Pallet, Storage, Event} = 13, + Historical: session_historical::{Pallet} = 14, + Session: pallet_session::{Pallet, Call, Storage, Event, Config<T>} = 15, + Grandpa: pallet_grandpa::{Pallet, Call, Storage, Config, Event, ValidateUnsigned} = 16, + ImOnline: pallet_im_online::{Pallet, Call, Storage, Event<T>, ValidateUnsigned, Config<T>} = 17, + AuthorityDiscovery: pallet_authority_discovery::{Pallet, Config} = 18, // Governance stuff. Sudo: pallet_sudo::{Pallet, Call, Config<T>, Storage, Event<T>} = 20, @@ -288,18 +292,13 @@ construct_runtime!( Cert: pallet_certification::<Instance1>::{Pallet, Call, Config<T>, Storage, Event<T>} = 43, Distance: pallet_distance::{Pallet, Call, Storage, Inherent, Event<T>} = 44, - // Smith Sub-Wot - SmithSubWot: pallet_duniter_wot::<Instance2>::{Pallet} = 50, - SmithMembership: pallet_membership::<Instance2>::{Pallet, Call, Config<T>, Storage, Event<T>} = 52, - SmithCert: pallet_certification::<Instance2>::{Pallet, Call, Config<T>, Storage, Event<T>} = 53, - // Utilities - AtomicSwap: pallet_atomic_swap::{Pallet, Call, Storage, Event<T>} = 60, - Multisig: pallet_multisig::{Pallet, Call, Storage, Event<T>} = 61, - ProvideRandomness: pallet_provide_randomness::{Pallet, Call, Storage, Event} = 62, - Proxy: pallet_proxy::{Pallet, Call, Storage, Event<T>} = 63, - Utility: pallet_utility::{Pallet, Call, Event} = 64, - Treasury: pallet_treasury::{Pallet, Call, Config, Storage, Event<T>} = 65, + AtomicSwap: pallet_atomic_swap::{Pallet, Call, Storage, Event<T>} = 50, + Multisig: pallet_multisig::{Pallet, Call, Storage, Event<T>} = 51, + ProvideRandomness: pallet_provide_randomness::{Pallet, Call, Storage, Event} = 52, + Proxy: pallet_proxy::{Pallet, Call, Storage, Event<T>} = 53, + Utility: pallet_utility::{Pallet, Call, Event} = 54, + Treasury: pallet_treasury::{Pallet, Call, Config, Storage, Event<T>} = 55, } ); diff --git a/runtime/g1/src/parameters.rs b/runtime/g1/src/parameters.rs index 11315f7fce15a178460901522ace74a2a33b977b..e0c55b2b38afe4d8cf0114d86f4351cbad332d64 100644 --- a/runtime/g1/src/parameters.rs +++ b/runtime/g1/src/parameters.rs @@ -117,26 +117,13 @@ parameter_types! { } /******************/ -/* SMITHS SUB-WOT */ +/* SMITH-MEMBERS */ /******************/ parameter_types! { - pub const SmithWotFirstCertIssuableOn: BlockNumber = 30 * DAYS; pub const SmithWotMinCertForMembership: u32 = 3; -} - -// Membership -parameter_types! { - pub const SmithMembershipPeriod: BlockNumber = 73 * DAYS; - pub const SmithPendingMembershipPeriod: BlockNumber = 12 * DAYS; -} - -// Certification -parameter_types! { - pub const SmithCertPeriod: BlockNumber = 5 * DAYS; pub const SmithMaxByIssuer: u32 = 12; - pub const SmithMinReceivedCertToBeAbleToIssueCert: u32 = 5; - pub const SmithValidityPeriod: BlockNumber = 146 * DAYS; + pub const InactivityMaxDuration: u32 = 48; } /*************/ diff --git a/runtime/gdev/Cargo.toml b/runtime/gdev/Cargo.toml index 976999d23464736b73a55792f68b3feef4b686d4..ce692df9d4acaa95f488e9f61790d88efc58d563 100644 --- a/runtime/gdev/Cargo.toml +++ b/runtime/gdev/Cargo.toml @@ -62,6 +62,7 @@ std = [ 'pallet-atomic-swap/std', 'pallet-authority-discovery/std', 'pallet-authority-members/std', + 'pallet-smith-members/std', 'pallet-babe/std', 'pallet-balances/std', 'pallet-certification/std', @@ -141,6 +142,7 @@ sp-staking = { git = 'https://github.com/duniter/substrate', branch = 'duniter-s # local common-runtime = { path = "../common", default-features = false } pallet-authority-members = { path = '../../pallets/authority-members', default-features = false } +pallet-smith-members = { path = '../../pallets/smith-members', default-features = false } pallet-certification = { path = '../../pallets/certification', default-features = false } pallet-distance = { path = "../../pallets/distance", default-features = false } pallet-duniter-test-parameters = { path = '../../pallets/duniter-test-parameters', default-features = false } diff --git a/runtime/gdev/src/lib.rs b/runtime/gdev/src/lib.rs index 5b13dec4398a09ea8847f73c876dc4892c8d5c39..383b56927f73468cd18b57eb87a7b4fe6adb4494 100644 --- a/runtime/gdev/src/lib.rs +++ b/runtime/gdev/src/lib.rs @@ -67,6 +67,7 @@ use sp_version::NativeVersion; use sp_version::RuntimeVersion; // A few exports that help ease life for downstream crates. +use frame_support::instances::Instance2; pub use frame_support::{ construct_runtime, parameter_types, traits::{EqualPrivilegeOnly, KeyOwnerProofSystem, Randomness}, @@ -218,7 +219,9 @@ impl frame_support::traits::InstanceFilter<RuntimeCall> for ProxyType { // Some calls are never authorized from a proxied account !matches!( c, - RuntimeCall::Cert(..) | RuntimeCall::Identity(..) | RuntimeCall::SmithCert(..) + RuntimeCall::Cert(..) + | RuntimeCall::Identity(..) + | RuntimeCall::SmithMembers(..) ) } ProxyType::TransferOnly => { @@ -261,20 +264,17 @@ common_runtime::pallets_config! { pub type WotMinCertForMembership = pallet_duniter_test_parameters::WotMinCertForMembership<Runtime>; pub type WotMinCertForCreateIdtyRight = pallet_duniter_test_parameters::WotMinCertForCreateIdtyRight<Runtime>; - pub type SmithCertPeriod = pallet_duniter_test_parameters::SmithCertPeriod<Runtime>; pub type SmithMaxByIssuer = pallet_duniter_test_parameters::SmithCertMaxByIssuer<Runtime>; - pub type SmithMinReceivedCertToBeAbleToIssueCert = - pallet_duniter_test_parameters::SmithCertMinReceivedCertToIssueCert<Runtime>; - pub type SmithValidityPeriod = pallet_duniter_test_parameters::SmithCertValidityPeriod<Runtime>; - pub type SmithMembershipPeriod = pallet_duniter_test_parameters::SmithMembershipPeriod<Runtime>; - pub type SmithWotFirstCertIssuableOn = - pallet_duniter_test_parameters::SmithWotFirstCertIssuableOn<Runtime>; pub type SmithWotMinCertForMembership = pallet_duniter_test_parameters::SmithWotMinCertForMembership<Runtime>; + // TODO: prefix with "Smith" + pub type InactivityMaxDuration = + pallet_duniter_test_parameters::SmithInactivityMaxDuration<Runtime>; impl pallet_duniter_test_parameters::Config for Runtime { type CertCount = u32; type PeriodCount = Balance; + type SessionCount = u32; } impl pallet_sudo::Config for Runtime { @@ -309,14 +309,15 @@ construct_runtime!( Quota: pallet_quota::{Pallet, Storage, Config<T>, Event<T>} = 66, // Consensus support - AuthorityMembers: pallet_authority_members::{Pallet, Call, Storage, Config<T>, Event<T>} = 10, - Authorship: pallet_authorship::{Pallet, Storage} = 11, - Offences: pallet_offences::{Pallet, Storage, Event} = 12, - Historical: session_historical::{Pallet} = 13, - Session: pallet_session::{Pallet, Call, Storage, Event, Config<T>} = 14, - Grandpa: pallet_grandpa::{Pallet, Call, Storage, Config, Event, ValidateUnsigned} = 15, - ImOnline: pallet_im_online::{Pallet, Call, Storage, Event<T>, ValidateUnsigned, Config<T>} = 16, - AuthorityDiscovery: pallet_authority_discovery::{Pallet, Config} = 17, + SmithMembers: pallet_smith_members::{Pallet, Call, Storage, Config<T>, Event<T>} = 10, + AuthorityMembers: pallet_authority_members::{Pallet, Call, Storage, Config<T>, Event<T>} = 11, + Authorship: pallet_authorship::{Pallet, Storage} = 12, + Offences: pallet_offences::{Pallet, Storage, Event} = 13, + Historical: session_historical::{Pallet} = 14, + Session: pallet_session::{Pallet, Call, Storage, Event, Config<T>} = 15, + Grandpa: pallet_grandpa::{Pallet, Call, Storage, Config, Event, ValidateUnsigned} = 16, + ImOnline: pallet_im_online::{Pallet, Call, Storage, Event<T>, ValidateUnsigned, Config<T>} = 17, + AuthorityDiscovery: pallet_authority_discovery::{Pallet, Config} = 18, // Governance stuff Sudo: pallet_sudo::{Pallet, Call, Config<T>, Storage, Event<T>} = 20, @@ -334,18 +335,13 @@ construct_runtime!( Cert: pallet_certification::<Instance1>::{Pallet, Call, Config<T>, Storage, Event<T>} = 43, Distance: pallet_distance::{Pallet, Call, Storage, Inherent, Event<T>} = 44, - // Smith Sub-Wot - SmithSubWot: pallet_duniter_wot::<Instance2>::{Pallet} = 50, - SmithMembership: pallet_membership::<Instance2>::{Pallet, Call, Config<T>, Storage, Event<T>} = 52, - SmithCert: pallet_certification::<Instance2>::{Pallet, Call, Config<T>, Storage, Event<T>} = 53, - // Utilities - AtomicSwap: pallet_atomic_swap::{Pallet, Call, Storage, Event<T>} = 60, - Multisig: pallet_multisig::{Pallet, Call, Storage, Event<T>} = 61, - ProvideRandomness: pallet_provide_randomness::{Pallet, Call, Storage, Event} = 62, - Proxy: pallet_proxy::{Pallet, Call, Storage, Event<T>} = 63, - Utility: pallet_utility::{Pallet, Call, Event} = 64, - Treasury: pallet_treasury::{Pallet, Call, Config, Storage, Event<T>} = 65, + AtomicSwap: pallet_atomic_swap::{Pallet, Call, Storage, Event<T>} = 50, + Multisig: pallet_multisig::{Pallet, Call, Storage, Event<T>} = 51, + ProvideRandomness: pallet_provide_randomness::{Pallet, Call, Storage, Event} = 52, + Proxy: pallet_proxy::{Pallet, Call, Storage, Event<T>} = 53, + Utility: pallet_utility::{Pallet, Call, Event} = 54, + Treasury: pallet_treasury::{Pallet, Call, Config, Storage, Event<T>} = 55, } ); diff --git a/runtime/gdev/tests/common/mod.rs b/runtime/gdev/tests/common/mod.rs index 5c20cd6ab0b337c6989ae8eb1640c836b5424c27..037c13f3be32e1f8ac99e3f7b1897fa7a9d054b3 100644 --- a/runtime/gdev/tests/common/mod.rs +++ b/runtime/gdev/tests/common/mod.rs @@ -18,11 +18,12 @@ use common_runtime::constants::*; use common_runtime::*; -use frame_support::instances::{Instance1, Instance2}; +use frame_support::instances::Instance1; use frame_support::traits::{GenesisBuild, OnFinalize, OnInitialize}; use gdev_runtime::opaque::SessionKeys; use gdev_runtime::*; use pallet_authority_members::OnNewSession; +use pallet_smith_members::SmithMeta; use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId; use sp_consensus_babe::{AuthorityId as BabeId, Slot}; use sp_consensus_babe::{VrfOutput, VrfProof}; @@ -54,7 +55,7 @@ pub struct ExtBuilder { initial_authorities_len: usize, initial_identities: BTreeMap<IdtyName, AccountId>, initial_smiths: Vec<AuthorityKeys>, - parameters: GenesisParameters<u32, u32, Balance>, + parameters: GenesisParameters<u32, u32, Balance, u32>, } impl ExtBuilder { @@ -108,14 +109,9 @@ impl ExtBuilder { pending_membership_period: 500, ud_creation_period: 60_000, ud_reeval_period: 60_000 * 20, - smith_cert_period: 15, smith_cert_max_by_issuer: 8, - smith_cert_min_received_cert_to_issue_cert: 2, - smith_cert_validity_period: 1_000, - smith_membership_period: 1_000, - smith_pending_membership_period: 500, - smith_wot_first_cert_issuable_on: 20, smith_wot_min_cert_for_membership: 2, + smith_inactivity_max_duration: 48, wot_first_cert_issuable_on: 20, wot_min_cert_for_create_idty_right: 2, wot_min_cert_for_membership: 2, @@ -142,7 +138,7 @@ impl ExtBuilder { }*/ pub fn change_parameters< - F: Fn(&mut pallet_duniter_test_parameters::Parameters<u32, u32, Balance>), + F: Fn(&mut pallet_duniter_test_parameters::Parameters<u32, u32, Balance, u32>), >( mut self, f: F, @@ -276,27 +272,8 @@ impl ExtBuilder { .assimilate_storage(&mut t) .unwrap(); - pallet_membership::GenesisConfig::<Runtime, Instance2> { - memberships: (1..=initial_smiths.len()) - .map(|i| { - ( - i as u32, - MembershipData { - expire_on: parameters.smith_membership_period, - }, - ) - }) - .collect(), - } - .assimilate_storage(&mut t) - .unwrap(); - - pallet_certification::GenesisConfig::<Runtime, Instance2> { - apply_cert_period_at_genesis: false, - certs_by_receiver: clique_wot( - initial_smiths.len(), - parameters.smith_cert_validity_period, - ), + pallet_smith_members::GenesisConfig::<Runtime> { + initial_smiths: clique_smith_wot(initial_smiths.len()), } .assimilate_storage(&mut t) .unwrap(); @@ -393,9 +370,6 @@ pub fn run_to_block(n: u32) { Identity::on_initialize(System::block_number()); Membership::on_initialize(System::block_number()); Cert::on_initialize(System::block_number()); - SmithSubWot::on_initialize(System::block_number()); - SmithMembership::on_initialize(System::block_number()); - SmithCert::on_initialize(System::block_number()); Timestamp::set_timestamp(System::block_number() as u64 * BLOCK_TIME); Distance::on_initialize(System::block_number()); @@ -476,6 +450,22 @@ fn clique_wot( certs_by_issuer } +fn clique_smith_wot(initial_identities_len: usize) -> BTreeMap<IdtyIndex, (bool, Vec<IdtyIndex>)> { + let mut certs_by_issuer = BTreeMap::new(); + for i in 1..=initial_identities_len { + certs_by_issuer.insert( + i as IdtyIndex, + ( + true, + (1..=initial_identities_len) + .filter_map(|j| if i != j { Some(j as IdtyIndex) } else { None }) + .collect(), + ), + ); + } + certs_by_issuer +} + fn session_keys( babe: BabeId, grandpa: GrandpaId, diff --git a/runtime/gdev/tests/integration_tests.rs b/runtime/gdev/tests/integration_tests.rs index ef4dce191d85262ad341243a4758665bf623895f..99de08be055323ebab7511a09d8112bcdf27f450 100644 --- a/runtime/gdev/tests/integration_tests.rs +++ b/runtime/gdev/tests/integration_tests.rs @@ -20,10 +20,11 @@ use common::*; use frame_support::instances::Instance1; use frame_support::traits::StoredMap; use frame_support::traits::{Get, PalletInfo, StorageInfo, StorageInfoTrait}; -use frame_support::{assert_noop, assert_ok}; +use frame_support::{assert_err, assert_noop, assert_ok}; use frame_support::{StorageHasher, Twox128}; use gdev_runtime::*; use pallet_membership::MembershipRemovalReason; +use pallet_smith_members::SmithStatus; use sp_core::Encode; use sp_keyring::AccountKeyring; use sp_runtime::MultiAddress; @@ -392,6 +393,7 @@ fn test_membership_expiry() { }); } +// TODO: use change_parameter to change autorevocation period #[test] #[ignore = "long to go to autorevocation period"] fn test_membership_expiry_with_identity_removal() { @@ -647,12 +649,6 @@ fn test_revoke_smith_identity() { Identity::do_revoke_identity(3, pallet_identity::RevocationReason::Root); // Verify events - System::assert_has_event(RuntimeEvent::SmithMembership( - pallet_membership::Event::MembershipRemoved { - member: 3, - reason: MembershipRemovalReason::Revoked, - }, - )); System::assert_has_event(RuntimeEvent::AuthorityMembers( pallet_authority_members::Event::MemberRemoved { member: 3 }, )); @@ -679,22 +675,17 @@ fn test_smith_certification() { ExtBuilder::new(1, 3, 4).build().execute_with(|| { run_to_block(1); - // alice can renew smith cert to bob - assert_ok!(SmithCert::add_cert( + assert_ok!(SmithMembers::certify_smith( frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into(), - 1, // alice - 2 // bob + 2 )); - // charlie can not add new cert to eve (no identity) assert_noop!( - SmithCert::add_cert( - frame_system::RawOrigin::Signed(AccountKeyring::Charlie.to_account_id()).into(), - 3, // charlie - 5 // eve + SmithMembers::certify_smith( + frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into(), + 4 ), - // SmithSubWot::Error::IdtyNotFound, - pallet_duniter_wot::Error::<gdev_runtime::Runtime, pallet_certification::Instance2>::IdtyNotFound, + pallet_smith_members::Error::<Runtime>::CertificationReceiverMustHaveBeenInvited ); }); } @@ -711,6 +702,13 @@ fn test_smith_process() { let alice = AccountKeyring::Alice.to_account_id(); let bob = AccountKeyring::Bob.to_account_id(); let charlie = AccountKeyring::Charlie.to_account_id(); + let dave = AccountKeyring::Dave.to_account_id(); + let dummy_session_keys = gdev_runtime::opaque::SessionKeys { + grandpa: sp_core::ed25519::Public([0u8; 32]).into(), + babe: sp_core::sr25519::Public([0u8; 32]).into(), + im_online: sp_core::sr25519::Public([0u8; 32]).into(), + authority_discovery: sp_core::sr25519::Public([0u8; 32]).into(), + }; // Eve can not request smith membership because not member of the smith wot // no more membership request @@ -718,45 +716,53 @@ fn test_smith_process() { // Dave can request smith membership (currently optional) // no more membership request - // Alice Bob and Charlie can certify Dave - assert_ok!(SmithCert::add_cert( - frame_system::RawOrigin::Signed(alice).into(), - 1, + assert_ok!(SmithMembers::invite_smith( + frame_system::RawOrigin::Signed(alice.clone()).into(), 4 )); - assert_ok!(SmithCert::add_cert( - frame_system::RawOrigin::Signed(bob).into(), - 2, + assert_ok!(SmithMembers::accept_invitation( + frame_system::RawOrigin::Signed(dave).into(), + )); + + // Dave cannot (yet) set his session keys + assert_err!( + AuthorityMembers::set_session_keys( + frame_system::RawOrigin::Signed(AccountKeyring::Dave.to_account_id()).into(), + dummy_session_keys.clone() + ), + pallet_authority_members::Error::<Runtime>::NotMember + ); + + // Alice Bob and Charlie can certify Dave + assert_ok!(SmithMembers::certify_smith( + frame_system::RawOrigin::Signed(alice.clone()).into(), 4 )); - assert_ok!(SmithCert::add_cert( - frame_system::RawOrigin::Signed(charlie).into(), - 3, + assert_ok!(SmithMembers::certify_smith( + frame_system::RawOrigin::Signed(bob.clone()).into(), 4 )); - - // with these three smith certs, Dave can claim membership - assert_ok!(SmithMembership::claim_membership( - frame_system::RawOrigin::Signed(AccountKeyring::Dave.to_account_id()).into(), + assert_ok!(SmithMembers::certify_smith( + frame_system::RawOrigin::Signed(charlie.clone()).into(), + 4 )); + // with these three smith certs, Dave has become smith // Dave is then member of the smith wot assert_eq!( - SmithMembership::membership(4), - Some(sp_membership::MembershipData { - expire_on: 1001, // 1 + 1000 + SmithMembers::smiths(4), + Some(pallet_smith_members::SmithMeta { + status: SmithStatus::Smith, + expires_on: Some(48), + issued_certs: vec![], + received_certs: vec![1, 2, 3], }) ); // Dave can set his (dummy) session keys assert_ok!(AuthorityMembers::set_session_keys( frame_system::RawOrigin::Signed(AccountKeyring::Dave.to_account_id()).into(), - gdev_runtime::opaque::SessionKeys { - grandpa: sp_core::ed25519::Public([0u8; 32]).into(), - babe: sp_core::sr25519::Public([0u8; 32]).into(), - im_online: sp_core::sr25519::Public([0u8; 32]).into(), - authority_discovery: sp_core::sr25519::Public([0u8; 32]).into(), - } + dummy_session_keys )); // Dave can go online @@ -1328,11 +1334,10 @@ fn test_new_account_linked() { }) } #[test] +#[ignore = "what was this test supposed to do?"] fn smith_data_problem() { ExtBuilder::new(1, 3, 4) - .change_parameters(|parameters| { - parameters.smith_cert_validity_period = 3; - }) + .change_parameters(|_parameters| {}) .build() .execute_with(|| { run_to_block(4); @@ -1372,3 +1377,11 @@ fn test_killed_account() { ); }) } + +// TODO: test_smith_member_cant_change_its_idty_address +// TODO: test_smith_member_can_revoke_its_idty +// TODO: test_revoke_idty +// TODO: test_non_smith_can_not_issue_smith_cert +// TODO: test_non_smith_can_not_issue_smith_cert +// TODO: test_non_smith_with_certs_can_not_issue_smith_cert +// TODO: test_revoked_smith_with_certs_can_not_issue_smith_cert diff --git a/runtime/gtest/Cargo.toml b/runtime/gtest/Cargo.toml index 8f59a6ad6a39b54c0c0ca276e78ef76b5b346eb6..574163a2a99de514fb3583c22cced737817c3019 100644 --- a/runtime/gtest/Cargo.toml +++ b/runtime/gtest/Cargo.toml @@ -60,6 +60,7 @@ std = [ 'pallet-atomic-swap/std', 'pallet-authority-discovery/std', 'pallet-authority-members/std', + 'pallet-smith-members/std', 'pallet-babe/std', 'pallet-balances/std', 'pallet-certification/std', @@ -137,6 +138,7 @@ sp-keyring = { git = 'https://github.com/duniter/substrate', branch = 'duniter-s # local common-runtime = { path = "../common", default-features = false } pallet-authority-members = { path = '../../pallets/authority-members', default-features = false } +pallet-smith-members = { path = '../../pallets/smith-members', default-features = false } pallet-certification = { path = '../../pallets/certification', default-features = false } pallet-distance = { path = "../../pallets/distance", default-features = false } pallet-duniter-account = { path = '../../pallets/duniter-account', default-features = false } diff --git a/runtime/gtest/src/lib.rs b/runtime/gtest/src/lib.rs index 88fbd282f83728cf10ee03e282715b7ec34ebf67..9dc7d515947d6285e674e9b9b1c0f03c80966c4c 100644 --- a/runtime/gtest/src/lib.rs +++ b/runtime/gtest/src/lib.rs @@ -66,6 +66,7 @@ use sp_version::NativeVersion; use sp_version::RuntimeVersion; // A few exports that help ease life for downstream crates. +use frame_support::instances::Instance2; pub use frame_support::{ construct_runtime, parameter_types, traits::{EqualPrivilegeOnly, KeyOwnerProofSystem, Randomness}, @@ -211,7 +212,9 @@ impl frame_support::traits::InstanceFilter<RuntimeCall> for ProxyType { // Some calls are never authorized from a proxied account !matches!( c, - RuntimeCall::Cert(..) | RuntimeCall::Identity(..) | RuntimeCall::SmithCert(..) + RuntimeCall::Cert(..) + | RuntimeCall::Identity(..) + | RuntimeCall::SmithMembers(..) ) } ProxyType::TransferOnly => { @@ -268,14 +271,15 @@ construct_runtime!( Quota: pallet_quota::{Pallet, Storage, Config<T>, Event<T>} = 66, // Consensus support - AuthorityMembers: pallet_authority_members::{Pallet, Call, Storage, Config<T>, Event<T>} = 10, - Authorship: pallet_authorship::{Pallet, Storage} = 11, - Offences: pallet_offences::{Pallet, Storage, Event} = 12, - Historical: session_historical::{Pallet} = 13, - Session: pallet_session::{Pallet, Call, Storage, Event, Config<T>} = 14, - Grandpa: pallet_grandpa::{Pallet, Call, Storage, Config, Event, ValidateUnsigned} = 15, - ImOnline: pallet_im_online::{Pallet, Call, Storage, Event<T>, ValidateUnsigned, Config<T>} = 16, - AuthorityDiscovery: pallet_authority_discovery::{Pallet, Config} = 17, + SmithMembers: pallet_smith_members::{Pallet, Call, Storage, Config<T>, Event<T>} = 10, + AuthorityMembers: pallet_authority_members::{Pallet, Call, Storage, Config<T>, Event<T>} = 11, + Authorship: pallet_authorship::{Pallet, Storage} = 12, + Offences: pallet_offences::{Pallet, Storage, Event} = 13, + Historical: session_historical::{Pallet} = 14, + Session: pallet_session::{Pallet, Call, Storage, Event, Config<T>} = 15, + Grandpa: pallet_grandpa::{Pallet, Call, Storage, Config, Event, ValidateUnsigned} = 16, + ImOnline: pallet_im_online::{Pallet, Call, Storage, Event<T>, ValidateUnsigned, Config<T>} = 17, + AuthorityDiscovery: pallet_authority_discovery::{Pallet, Config} = 18, // Governance stuff Sudo: pallet_sudo::{Pallet, Call, Config<T>, Storage, Event<T>} = 20, @@ -293,18 +297,13 @@ construct_runtime!( Cert: pallet_certification::<Instance1>::{Pallet, Call, Config<T>, Storage, Event<T>} = 43, Distance: pallet_distance::{Pallet, Call, Storage, Inherent, Event<T>} = 44, - // Smith Sub-Wot - SmithSubWot: pallet_duniter_wot::<Instance2>::{Pallet} = 50, - SmithMembership: pallet_membership::<Instance2>::{Pallet, Call, Config<T>, Storage, Event<T>} = 52, - SmithCert: pallet_certification::<Instance2>::{Pallet, Call, Config<T>, Storage, Event<T>} = 53, - // Utilities - AtomicSwap: pallet_atomic_swap::{Pallet, Call, Storage, Event<T>} = 60, - Multisig: pallet_multisig::{Pallet, Call, Storage, Event<T>} = 61, - ProvideRandomness: pallet_provide_randomness::{Pallet, Call, Storage, Event} = 62, - Proxy: pallet_proxy::{Pallet, Call, Storage, Event<T>} = 63, - Utility: pallet_utility::{Pallet, Call, Event} = 64, - Treasury: pallet_treasury::{Pallet, Call, Config, Storage, Event<T>} = 65, + AtomicSwap: pallet_atomic_swap::{Pallet, Call, Storage, Event<T>} = 50, + Multisig: pallet_multisig::{Pallet, Call, Storage, Event<T>} = 51, + ProvideRandomness: pallet_provide_randomness::{Pallet, Call, Storage, Event} = 52, + Proxy: pallet_proxy::{Pallet, Call, Storage, Event<T>} = 53, + Utility: pallet_utility::{Pallet, Call, Event} = 54, + Treasury: pallet_treasury::{Pallet, Call, Config, Storage, Event<T>} = 55, } ); diff --git a/runtime/gtest/src/parameters.rs b/runtime/gtest/src/parameters.rs index 443e8cffa589424a5ed1fb69d40121c312e356ea..e91c3df186aedbf5a04c4be8970cf90b09a70eb6 100644 --- a/runtime/gtest/src/parameters.rs +++ b/runtime/gtest/src/parameters.rs @@ -118,26 +118,13 @@ parameter_types! { } /******************/ -/* SMITHS SUB-WOT */ +/* SMITH-MEMBERS */ /******************/ parameter_types! { - pub const SmithWotFirstCertIssuableOn: BlockNumber = DAYS; pub const SmithWotMinCertForMembership: u32 = 3; -} - -// Membership -parameter_types! { - pub const SmithMembershipPeriod: BlockNumber = 73 * DAYS; - pub const SmithPendingMembershipPeriod: BlockNumber = 12 * DAYS; -} - -// Certification -parameter_types! { - pub const SmithCertPeriod: BlockNumber = DAYS; pub const SmithMaxByIssuer: u32 = 100; - pub const SmithMinReceivedCertToBeAbleToIssueCert: u32 = 5; - pub const SmithValidityPeriod: BlockNumber = 146 * DAYS; + pub const InactivityMaxDuration: u32 = 48; } /*************/