Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aura authorities push (3) #122

Merged
merged 2 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions crates/humanode-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,36 @@ impl pallet_bioauth::TryConvert<OpaqueAuthTicket, pallet_bioauth::StoredAuthTick
}
}

pub struct AuraValidatorSetUpdater;

impl pallet_bioauth::ValidatorSetUpdater<AuraId> for AuraValidatorSetUpdater {
fn update_validators_set<'a, I: Iterator<Item = &'a AuraId> + 'a>(validator_public_keys: I)
where
AuraId: 'a,
{
let dummy = <Runtime as frame_system::Config>::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
.map(|public_key| (&dummy, public_key.clone()))
.collect::<Vec<_>>();

<pallet_aura::Pallet<Runtime> as frame_support::traits::OneSessionHandler<
<Runtime as frame_system::Config>::AccountId,
>>::on_new_session(true, authorities.into_iter(), Vec::new().into_iter())
}
}

impl pallet_bioauth::Config for Runtime {
type Event = Event;
type RobonodePublicKey = RobonodePublicKeyWrapper;
type RobonodeSignature = Vec<u8>;
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
Expand Down
21 changes: 20 additions & 1 deletion crates/pallet-bioauth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ pub trait TryConvert<A, B> {
fn try_convert(value: A) -> Result<B, Self::Error>;
}

/// Provides the capability to update the current validators set.
pub trait ValidatorSetUpdater<T> {
/// Updated the validators set for the of consensus.
fn update_validators_set<'a, I: Iterator<Item = &'a T> + 'a>(validator_public_keys: I)
where
T: 'a;
}

/// Authentication extrinsic playload.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Default, Clone, Encode, Decode, Hash, Debug)]
Expand Down Expand Up @@ -92,7 +100,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};
Expand Down Expand Up @@ -127,6 +135,9 @@ pub mod pallet {
Self::OpaqueAuthTicket,
StoredAuthTicket<Self::ValidatorPublicKey>,
>;

/// The validator set updater to invoke at auth the ticket acceptace.
type ValidatorSetUpdater: ValidatorSetUpdater<Self::ValidatorPublicKey>;
}

#[pallet::pallet]
Expand Down Expand Up @@ -253,6 +264,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(())
}
}
Expand Down Expand Up @@ -326,6 +338,13 @@ pub mod pallet {
.propagate(true)
.build()
}

fn issue_validators_set_update(
stored_auth_tickets: &[StoredAuthTicket<T::ValidatorPublicKey>],
) {
let validator_public_keys = stored_auth_tickets.iter().map(|ticket| &ticket.public_key);
T::ValidatorSetUpdater::update_validators_set(validator_public_keys);
}
}

#[pallet::validate_unsigned]
Expand Down
15 changes: 15 additions & 0 deletions crates/pallet-bioauth/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ impl super::Verifier<Vec<u8>> for MockVerifier {
}
}

pub struct MockValidatorSetUpdater;

impl super::ValidatorSetUpdater<Vec<u8>> for MockValidatorSetUpdater {
fn update_validators_set<'a, I: Iterator<Item = &'a Vec<u8>> + 'a>(mut validator_public_keys: I)
where
Vec<u8>: 'a,
{
assert!(
validator_public_keys.next().is_some(),
"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;
Expand Down Expand Up @@ -98,6 +112,7 @@ impl pallet_bioauth::Config for Test {
type ValidatorPublicKey = Vec<u8>;
type OpaqueAuthTicket = MockOpaqueAuthTicket;
type AuthTicketCoverter = MockAuthTicketConverter;
type ValidatorSetUpdater = MockValidatorSetUpdater;
}

// Build genesis storage according to the mock runtime.
Expand Down