From 1a6ee524b825d8d0f1394eedf80d70db464b25f1 Mon Sep 17 00:00:00 2001 From: MOZGIII Date: Wed, 8 Sep 2021 17:12:52 +0300 Subject: [PATCH 1/2] Rebase #106 --- crates/humanode-runtime/src/lib.rs | 21 +++++++++++++++++++++ crates/pallet-bioauth/src/lib.rs | 22 +++++++++++++++++++++- crates/pallet-bioauth/src/mock.rs | 12 ++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/crates/humanode-runtime/src/lib.rs b/crates/humanode-runtime/src/lib.rs index 2f2367416..aaf4bf904 100644 --- a/crates/humanode-runtime/src/lib.rs +++ b/crates/humanode-runtime/src/lib.rs @@ -331,6 +331,26 @@ impl pallet_bioauth::TryConvert for AuraValidatorSetUpdater { + fn update_validators_set(validator_public_keys: &[&AuraId]) { + let dummy = ::AccountId::default(); + + // clippy is just too dumb here, but we need two iterators passed to `on_new_session` to be + // the same type. The easiest is to use Vec's iter for both. + #[allow(clippy::needless_collect)] + let authorities = validator_public_keys + .iter() + .map(|&public_key| (&dummy, public_key.clone())) + .collect::>(); + + as frame_support::traits::OneSessionHandler< + ::AccountId, + >>::on_new_session(true, authorities.into_iter(), Vec::new().into_iter()) + } +} + impl pallet_bioauth::Config for Runtime { type Event = Event; type RobonodePublicKey = RobonodePublicKeyWrapper; @@ -338,6 +358,7 @@ impl pallet_bioauth::Config for Runtime { type ValidatorPublicKey = AuraId; type OpaqueAuthTicket = primitives_auth_ticket::OpaqueAuthTicket; type AuthTicketCoverter = PrimitiveAuthTicketConverter; + type ValidatorSetUpdater = AuraValidatorSetUpdater; } // Create the runtime by composing the FRAME pallets that were previously diff --git a/crates/pallet-bioauth/src/lib.rs b/crates/pallet-bioauth/src/lib.rs index 8cd945d40..8c1995501 100644 --- a/crates/pallet-bioauth/src/lib.rs +++ b/crates/pallet-bioauth/src/lib.rs @@ -54,6 +54,12 @@ pub trait TryConvert { fn try_convert(value: A) -> Result; } +/// Provides the capability to update the current validators set. +pub trait ValidatorSetUpdater { + /// Updated the validators set for the of consensus. + fn update_validators_set(validator_public_keys: &[&T]); +} + /// Authentication extrinsic playload. #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] #[derive(PartialEq, Eq, PartialOrd, Ord, Default, Clone, Encode, Decode, Hash, Debug)] @@ -92,7 +98,7 @@ sp_api::decl_runtime_apis! { )] #[frame_support::pallet] pub mod pallet { - use crate::{StoredAuthTicket, TryConvert, Verifier}; + use crate::{StoredAuthTicket, TryConvert, ValidatorSetUpdater, Verifier}; use super::Authenticate; use frame_support::{dispatch::DispatchResult, pallet_prelude::*, storage::types::ValueQuery}; @@ -127,6 +133,9 @@ pub mod pallet { Self::OpaqueAuthTicket, StoredAuthTicket, >; + + /// The validator set updater to invoke at auth the ticket acceptace. + type ValidatorSetUpdater: ValidatorSetUpdater; } #[pallet::pallet] @@ -253,6 +262,7 @@ pub mod pallet { Ok(()) => { // Authentication was successfull, add the incoming auth ticket to the list. list.push(stored_auth_ticket); + Self::issue_validators_set_update(list.as_slice()); Ok(()) } } @@ -326,6 +336,16 @@ pub mod pallet { .propagate(true) .build() } + + fn issue_validators_set_update( + stored_auth_tickets: &[StoredAuthTicket], + ) { + let validator_public_keys = stored_auth_tickets + .iter() + .map(|ticket| &ticket.public_key) + .collect::>(); + T::ValidatorSetUpdater::update_validators_set(validator_public_keys.as_slice()); + } } #[pallet::validate_unsigned] diff --git a/crates/pallet-bioauth/src/mock.rs b/crates/pallet-bioauth/src/mock.rs index 4efa5c5e6..3a143ade7 100644 --- a/crates/pallet-bioauth/src/mock.rs +++ b/crates/pallet-bioauth/src/mock.rs @@ -60,6 +60,17 @@ impl super::Verifier> for MockVerifier { } } +pub struct MockValidatorSetUpdater; + +impl super::ValidatorSetUpdater for MockValidatorSetUpdater { + fn update_validators_set(validator_public_keys: &[&[u8]]) { + assert!( + !validator_public_keys.is_empty(), + "We should get non-empty set every time at each of the test cases" + ); + } +} + parameter_types! { pub const BlockHashCount: u64 = 250; pub const SS58Prefix: u8 = 42; @@ -98,6 +109,7 @@ impl pallet_bioauth::Config for Test { type ValidatorPublicKey = Vec; type OpaqueAuthTicket = MockOpaqueAuthTicket; type AuthTicketCoverter = MockAuthTicketConverter; + type ValidatorSetUpdater = MockValidatorSetUpdater; } // Build genesis storage according to the mock runtime. From 43e59dd2120f0b6badf8e96355235f815e5698b9 Mon Sep 17 00:00:00 2001 From: MOZGIII Date: Wed, 8 Sep 2021 17:20:05 +0300 Subject: [PATCH 2/2] Eliminate intermediate Vecs --- crates/humanode-runtime/src/lib.rs | 8 +++++--- crates/pallet-bioauth/src/lib.rs | 11 +++++------ crates/pallet-bioauth/src/mock.rs | 9 ++++++--- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/crates/humanode-runtime/src/lib.rs b/crates/humanode-runtime/src/lib.rs index aaf4bf904..bcc112edf 100644 --- a/crates/humanode-runtime/src/lib.rs +++ b/crates/humanode-runtime/src/lib.rs @@ -334,15 +334,17 @@ impl pallet_bioauth::TryConvert for AuraValidatorSetUpdater { - fn update_validators_set(validator_public_keys: &[&AuraId]) { + fn update_validators_set<'a, I: Iterator + 'a>(validator_public_keys: I) + where + AuraId: 'a, + { let dummy = ::AccountId::default(); // clippy is just too dumb here, but we need two iterators passed to `on_new_session` to be // the same type. The easiest is to use Vec's iter for both. #[allow(clippy::needless_collect)] let authorities = validator_public_keys - .iter() - .map(|&public_key| (&dummy, public_key.clone())) + .map(|public_key| (&dummy, public_key.clone())) .collect::>(); as frame_support::traits::OneSessionHandler< diff --git a/crates/pallet-bioauth/src/lib.rs b/crates/pallet-bioauth/src/lib.rs index 8c1995501..ecb19efd2 100644 --- a/crates/pallet-bioauth/src/lib.rs +++ b/crates/pallet-bioauth/src/lib.rs @@ -57,7 +57,9 @@ pub trait TryConvert { /// Provides the capability to update the current validators set. pub trait ValidatorSetUpdater { /// Updated the validators set for the of consensus. - fn update_validators_set(validator_public_keys: &[&T]); + fn update_validators_set<'a, I: Iterator + 'a>(validator_public_keys: I) + where + T: 'a; } /// Authentication extrinsic playload. @@ -340,11 +342,8 @@ pub mod pallet { fn issue_validators_set_update( stored_auth_tickets: &[StoredAuthTicket], ) { - let validator_public_keys = stored_auth_tickets - .iter() - .map(|ticket| &ticket.public_key) - .collect::>(); - T::ValidatorSetUpdater::update_validators_set(validator_public_keys.as_slice()); + let validator_public_keys = stored_auth_tickets.iter().map(|ticket| &ticket.public_key); + T::ValidatorSetUpdater::update_validators_set(validator_public_keys); } } diff --git a/crates/pallet-bioauth/src/mock.rs b/crates/pallet-bioauth/src/mock.rs index 3a143ade7..0fe0397be 100644 --- a/crates/pallet-bioauth/src/mock.rs +++ b/crates/pallet-bioauth/src/mock.rs @@ -62,10 +62,13 @@ impl super::Verifier> for MockVerifier { pub struct MockValidatorSetUpdater; -impl super::ValidatorSetUpdater for MockValidatorSetUpdater { - fn update_validators_set(validator_public_keys: &[&[u8]]) { +impl super::ValidatorSetUpdater> for MockValidatorSetUpdater { + fn update_validators_set<'a, I: Iterator> + 'a>(mut validator_public_keys: I) + where + Vec: 'a, + { assert!( - !validator_public_keys.is_empty(), + validator_public_keys.next().is_some(), "We should get non-empty set every time at each of the test cases" ); }