|
|
|
@@ -44,14 +44,9 @@
|
|
|
|
|
|
|
|
|
|
#![cfg_attr(not(feature = "std"), no_std)]
|
|
|
|
|
|
|
|
|
|
use pallet_timestamp;
|
|
|
|
|
|
|
|
|
|
use sp_std::{result, prelude::*};
|
|
|
|
|
use codec::{Encode, Decode};
|
|
|
|
|
use frame_support::{
|
|
|
|
|
decl_storage, decl_module, Parameter, traits::{Get, FindAuthor},
|
|
|
|
|
ConsensusEngineId,
|
|
|
|
|
};
|
|
|
|
|
use frame_support::{Parameter, traits::{Get, FindAuthor}, ConsensusEngineId};
|
|
|
|
|
use sp_runtime::{
|
|
|
|
|
RuntimeAppPublic,
|
|
|
|
|
traits::{SaturatedConversion, Saturating, Zero, Member, IsMember}, generic::DigestItem,
|
|
|
|
@@ -59,37 +54,67 @@ use sp_runtime::{
|
|
|
|
|
use sp_timestamp::OnTimestampSet;
|
|
|
|
|
use sp_inherents::{InherentIdentifier, InherentData, ProvideInherent, MakeFatalError};
|
|
|
|
|
use sp_consensus_aura::{
|
|
|
|
|
AURA_ENGINE_ID, ConsensusLog, AuthorityIndex,
|
|
|
|
|
AURA_ENGINE_ID, ConsensusLog, AuthorityIndex, Slot,
|
|
|
|
|
inherents::{INHERENT_IDENTIFIER, AuraInherentData},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mod mock;
|
|
|
|
|
mod tests;
|
|
|
|
|
|
|
|
|
|
pub trait Config: pallet_timestamp::Config {
|
|
|
|
|
/// The identifier type for an authority.
|
|
|
|
|
type AuthorityId: Member + Parameter + RuntimeAppPublic + Default;
|
|
|
|
|
}
|
|
|
|
|
pub use pallet::*;
|
|
|
|
|
|
|
|
|
|
decl_storage! {
|
|
|
|
|
trait Store for Module<T: Config> as Aura {
|
|
|
|
|
/// The last timestamp.
|
|
|
|
|
LastTimestamp get(fn last): T::Moment;
|
|
|
|
|
#[frame_support::pallet]
|
|
|
|
|
pub mod pallet {
|
|
|
|
|
use super::*;
|
|
|
|
|
use frame_support::pallet_prelude::*;
|
|
|
|
|
use frame_system::pallet_prelude::*;
|
|
|
|
|
|
|
|
|
|
/// The current authorities
|
|
|
|
|
pub Authorities get(fn authorities): Vec<T::AuthorityId>;
|
|
|
|
|
#[pallet::config]
|
|
|
|
|
pub trait Config: pallet_timestamp::Config + frame_system::Config {
|
|
|
|
|
/// The identifier type for an authority.
|
|
|
|
|
type AuthorityId: Member + Parameter + RuntimeAppPublic + Default + MaybeSerializeDeserialize;
|
|
|
|
|
}
|
|
|
|
|
add_extra_genesis {
|
|
|
|
|
config(authorities): Vec<T::AuthorityId>;
|
|
|
|
|
build(|config| Module::<T>::initialize_authorities(&config.authorities))
|
|
|
|
|
|
|
|
|
|
#[pallet::pallet]
|
|
|
|
|
pub struct Pallet<T>(sp_std::marker::PhantomData<T>);
|
|
|
|
|
|
|
|
|
|
#[pallet::hooks]
|
|
|
|
|
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
|
|
|
|
|
|
|
|
|
|
#[pallet::call]
|
|
|
|
|
impl<T: Config> Pallet<T> {}
|
|
|
|
|
|
|
|
|
|
/// The current authority set.
|
|
|
|
|
#[pallet::storage]
|
|
|
|
|
#[pallet::getter(fn authorities)]
|
|
|
|
|
pub(super) type Authorities<T: Config> = StorageValue<_, Vec<T::AuthorityId>, ValueQuery>;
|
|
|
|
|
|
|
|
|
|
/// The last timestamp we have been notified of.
|
|
|
|
|
#[pallet::storage]
|
|
|
|
|
#[pallet::getter(fn last_timestamp)]
|
|
|
|
|
pub(super) type LastTimestamp<T: Config> = StorageValue<_, T::Moment, ValueQuery>;
|
|
|
|
|
|
|
|
|
|
#[pallet::genesis_config]
|
|
|
|
|
pub struct GenesisConfig<T: Config> {
|
|
|
|
|
pub authorities: Vec<T::AuthorityId>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
|
impl<T: Config> Default for GenesisConfig<T> {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self { authorities: Vec::new() }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[pallet::genesis_build]
|
|
|
|
|
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
|
|
|
|
|
fn build(&self) {
|
|
|
|
|
Pallet::<T>::initialize_authorities(&self.authorities);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
decl_module! {
|
|
|
|
|
pub struct Module<T: Config> for enum Call where origin: T::Origin { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Config> Module<T> {
|
|
|
|
|
impl<T: Config> Pallet<T> {
|
|
|
|
|
fn change_authorities(new: Vec<T::AuthorityId>) {
|
|
|
|
|
<Authorities<T>>::put(&new);
|
|
|
|
|
|
|
|
|
@@ -106,13 +131,20 @@ impl<T: Config> Module<T> {
|
|
|
|
|
<Authorities<T>>::put(authorities);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Determine the Aura slot-duration based on the Timestamp module configuration.
|
|
|
|
|
pub fn slot_duration() -> T::Moment {
|
|
|
|
|
// we double the minimum block-period so each author can always propose within
|
|
|
|
|
// the majority of its slot.
|
|
|
|
|
<T as pallet_timestamp::Config>::MinimumPeriod::get().saturating_mul(2u32.into())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Config> sp_runtime::BoundToRuntimeAppPublic for Module<T> {
|
|
|
|
|
impl<T: Config> sp_runtime::BoundToRuntimeAppPublic for Pallet<T> {
|
|
|
|
|
type Public = T::AuthorityId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Config> pallet_session::OneSessionHandler<T::AccountId> for Module<T> {
|
|
|
|
|
impl<T: Config> pallet_session::OneSessionHandler<T::AccountId> for Pallet<T> {
|
|
|
|
|
type Key = T::AuthorityId;
|
|
|
|
|
|
|
|
|
|
fn on_genesis_session<'a, I: 'a>(validators: I)
|
|
|
|
@@ -128,7 +160,7 @@ impl<T: Config> pallet_session::OneSessionHandler<T::AccountId> for Module<T> {
|
|
|
|
|
// instant changes
|
|
|
|
|
if changed {
|
|
|
|
|
let next_authorities = validators.map(|(_, k)| k).collect::<Vec<_>>();
|
|
|
|
|
let last_authorities = <Module<T>>::authorities();
|
|
|
|
|
let last_authorities = Self::authorities();
|
|
|
|
|
if next_authorities != last_authorities {
|
|
|
|
|
Self::change_authorities(next_authorities);
|
|
|
|
|
}
|
|
|
|
@@ -145,16 +177,15 @@ impl<T: Config> pallet_session::OneSessionHandler<T::AccountId> for Module<T> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Config> FindAuthor<u32> for Module<T> {
|
|
|
|
|
impl<T: Config> FindAuthor<u32> for Pallet<T> {
|
|
|
|
|
fn find_author<'a, I>(digests: I) -> Option<u32> where
|
|
|
|
|
I: 'a + IntoIterator<Item=(ConsensusEngineId, &'a [u8])>
|
|
|
|
|
{
|
|
|
|
|
for (id, mut data) in digests.into_iter() {
|
|
|
|
|
if id == AURA_ENGINE_ID {
|
|
|
|
|
if let Ok(slot) = u64::decode(&mut data) {
|
|
|
|
|
let author_index = slot % Self::authorities().len() as u64;
|
|
|
|
|
return Some(author_index as u32)
|
|
|
|
|
}
|
|
|
|
|
let slot = Slot::decode(&mut data).ok()?;
|
|
|
|
|
let author_index = *slot % Self::authorities().len() as u64;
|
|
|
|
|
return Some(author_index as u32)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -162,7 +193,7 @@ impl<T: Config> FindAuthor<u32> for Module<T> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// We can not implement `FindAuthor` twice, because the compiler does not know if
|
|
|
|
|
/// We can not implement `FindAuthor` twice, because the compiler does not know if
|
|
|
|
|
/// `u32 == T::AuthorityId` and thus, prevents us to implement the trait twice.
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
|
pub struct FindAccountFromAuthorIndex<T, Inner>(sp_std::marker::PhantomData<(T, Inner)>);
|
|
|
|
@@ -175,15 +206,15 @@ impl<T: Config, Inner: FindAuthor<u32>> FindAuthor<T::AuthorityId>
|
|
|
|
|
{
|
|
|
|
|
let i = Inner::find_author(digests)?;
|
|
|
|
|
|
|
|
|
|
let validators = <Module<T>>::authorities();
|
|
|
|
|
let validators = <Pallet<T>>::authorities();
|
|
|
|
|
validators.get(i as usize).map(|k| k.clone())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Find the authority ID of the Aura authority who authored the current block.
|
|
|
|
|
pub type AuraAuthorId<T> = FindAccountFromAuthorIndex<T, Module<T>>;
|
|
|
|
|
pub type AuraAuthorId<T> = FindAccountFromAuthorIndex<T, Pallet<T>>;
|
|
|
|
|
|
|
|
|
|
impl<T: Config> IsMember<T::AuthorityId> for Module<T> {
|
|
|
|
|
impl<T: Config> IsMember<T::AuthorityId> for Pallet<T> {
|
|
|
|
|
fn is_member(authority_id: &T::AuthorityId) -> bool {
|
|
|
|
|
Self::authorities()
|
|
|
|
|
.iter()
|
|
|
|
@@ -191,26 +222,20 @@ impl<T: Config> IsMember<T::AuthorityId> for Module<T> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Config> Module<T> {
|
|
|
|
|
/// Determine the Aura slot-duration based on the Timestamp module configuration.
|
|
|
|
|
pub fn slot_duration() -> T::Moment {
|
|
|
|
|
// we double the minimum block-period so each author can always propose within
|
|
|
|
|
// the majority of its slot.
|
|
|
|
|
<T as pallet_timestamp::Config>::MinimumPeriod::get().saturating_mul(2u32.into())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_timestamp_set(now: T::Moment, slot_duration: T::Moment) {
|
|
|
|
|
let last = Self::last();
|
|
|
|
|
<Self as Store>::LastTimestamp::put(now);
|
|
|
|
|
impl<T: Config> OnTimestampSet<T::Moment> for Pallet<T> {
|
|
|
|
|
fn on_timestamp_set(moment: T::Moment) {
|
|
|
|
|
let last = Self::last_timestamp();
|
|
|
|
|
LastTimestamp::<T>::put(moment);
|
|
|
|
|
|
|
|
|
|
if last.is_zero() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let slot_duration = Self::slot_duration();
|
|
|
|
|
assert!(!slot_duration.is_zero(), "Aura slot duration cannot be zero.");
|
|
|
|
|
|
|
|
|
|
let last_slot = last / slot_duration;
|
|
|
|
|
let cur_slot = now / slot_duration;
|
|
|
|
|
let cur_slot = moment / slot_duration;
|
|
|
|
|
|
|
|
|
|
assert!(last_slot < cur_slot, "Only one block may be authored per slot.");
|
|
|
|
|
|
|
|
|
@@ -218,13 +243,7 @@ impl<T: Config> Module<T> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Config> OnTimestampSet<T::Moment> for Module<T> {
|
|
|
|
|
fn on_timestamp_set(moment: T::Moment) {
|
|
|
|
|
Self::on_timestamp_set(moment, Self::slot_duration())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Config> ProvideInherent for Module<T> {
|
|
|
|
|
impl<T: Config> ProvideInherent for Pallet<T> {
|
|
|
|
|
type Call = pallet_timestamp::Call<T>;
|
|
|
|
|
type Error = MakeFatalError<sp_inherents::Error>;
|
|
|
|
|
const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;
|
|
|
|
|