Skip to content
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
27 changes: 27 additions & 0 deletions crates/humanode-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,36 @@ impl pallet_sudo::Config for Runtime {
type Call = Call;
}

pub struct AuraValidatorSetUpdater;

impl pallet_bioauth::ValidatorSetUpdater for AuraValidatorSetUpdater {
fn update_validators_set(validator_public_keys: &[&[u8]]) {
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
.iter()
.map(|&public_key| {
use core::convert::TryInto;
(
&dummy,
public_key.try_into().expect("key has to be aura id"),
)
})
.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 ValidatorSetUpdater = AuraValidatorSetUpdater;
}

// Create the runtime by composing the FRAME pallets that were previously
Expand Down
20 changes: 19 additions & 1 deletion crates/pallet-bioauth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ pub trait Verifier<S: ?Sized> {
D: AsRef<[u8]> + Send + 'a;
}

/// 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: &[&[u8]]);
}

sp_api::decl_runtime_apis! {

/// We need to provide a trait using decl_runtime_apis! macro to be able to call required methods
Expand All @@ -97,7 +103,7 @@ sp_api::decl_runtime_apis! {
pub mod pallet {
use core::convert::TryInto;

use super::{Authenticate, StoredAuthTicket, Verifier};
use super::{Authenticate, StoredAuthTicket, ValidatorSetUpdater, Verifier};
use frame_support::{dispatch::DispatchResult, pallet_prelude::*, storage::types::ValueQuery};
use frame_system::pallet_prelude::*;
use primitives_auth_ticket::{AuthTicket, OpaqueAuthTicket};
Expand All @@ -111,6 +117,9 @@ pub mod pallet {

/// The public key of the robonode.
type RobonodePublicKey: Verifier<Vec<u8>> + codec::FullCodec + Default + serde_nostd::SerDe;

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

#[pallet::pallet]
Expand Down Expand Up @@ -233,6 +242,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 @@ -307,6 +317,14 @@ 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.as_slice())
.collect::<Vec<_>>();
T::ValidatorSetUpdater::update_validators_set(validator_public_keys.as_slice());
}
}

#[pallet::validate_unsigned]
Expand Down
12 changes: 12 additions & 0 deletions crates/pallet-bioauth/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ impl super::Verifier<Vec<u8>> 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;
Expand Down Expand Up @@ -74,6 +85,7 @@ impl system::Config for Test {
impl pallet_bioauth::Config for Test {
type Event = Event;
type RobonodePublicKey = MockVerifier;
type ValidatorSetUpdater = MockValidatorSetUpdater;
}

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