mirror of
https://git.ghostchain.io/proxmio/ghost-node.git
synced 2025-12-27 03:09:56 +00:00
authorities stored based on the session hash map
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
This commit is contained in:
@@ -13,7 +13,7 @@ use frame_support::{
|
||||
EstimateNextSessionRotation, ValidatorSet, ValidatorSetWithIdentification,
|
||||
OneSessionHandler, Get,
|
||||
},
|
||||
BoundedSlice, WeakBoundedVec,
|
||||
WeakBoundedVec,
|
||||
|
||||
};
|
||||
use frame_system::{
|
||||
@@ -325,7 +325,6 @@ pub mod pallet {
|
||||
pub enum Error<T> {
|
||||
NotEnoughClaps,
|
||||
NotAnAuthority,
|
||||
ClapForWrongSession,
|
||||
CurrentValidatorIsDisabled,
|
||||
AlreadyClapped,
|
||||
UnregisteredClapRemove,
|
||||
@@ -371,20 +370,27 @@ pub mod pallet {
|
||||
>;
|
||||
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn keys)]
|
||||
pub(super) type Authorities<T: Config> =
|
||||
StorageValue<_, WeakBoundedVec<T::AuthorityId, T::MaxAuthorities>, ValueQuery>;
|
||||
#[pallet::getter(fn authorities)]
|
||||
pub(super) type Authorities<T: Config> = StorageMap<
|
||||
_,
|
||||
Twox64Concat,
|
||||
SessionIndex,
|
||||
WeakBoundedVec<T::AuthorityId, T::MaxAuthorities>,
|
||||
ValueQuery,
|
||||
>;
|
||||
|
||||
#[pallet::genesis_config]
|
||||
#[derive(frame_support::DefaultNoBound)]
|
||||
pub struct GenesisConfig<T: Config> {
|
||||
pub keys: Vec<T::AuthorityId>,
|
||||
pub authorities: Vec<T::AuthorityId>,
|
||||
}
|
||||
|
||||
#[pallet::genesis_build]
|
||||
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
|
||||
fn build(&self) {
|
||||
Pallet::<T>::initialize_authorities(&self.keys);
|
||||
if !self.authorities.is_empty() {
|
||||
Pallet::<T>::initialize_authorities(self.authorities.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -465,7 +471,7 @@ pub mod pallet {
|
||||
|
||||
fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
|
||||
if let Call::slow_clap { clap, signature } = call {
|
||||
let authorities = Authorities::<T>::get();
|
||||
let authorities = Authorities::<T>::get(&clap.session_index);
|
||||
let authority = match authorities.get(clap.authority_index as usize) {
|
||||
Some(authority) => authority,
|
||||
None => return InvalidTransaction::BadSigner.into(),
|
||||
@@ -506,10 +512,7 @@ impl<T: Config> Pallet<T> {
|
||||
}
|
||||
|
||||
fn try_slow_clap(clap: &Clap<T::AccountId, NetworkIdOf<T>, BalanceOf<T>>) -> DispatchResult {
|
||||
let current_session_index = T::ValidatorSet::session_index();
|
||||
ensure!(current_session_index == clap.session_index, Error::<T>::ClapForWrongSession);
|
||||
|
||||
let authorities = Authorities::<T>::get();
|
||||
let authorities = Authorities::<T>::get(&clap.session_index);
|
||||
ensure!(authorities.get(clap.authority_index as usize).is_some(), Error::<T>::NotAnAuthority);
|
||||
|
||||
let clap_unique_hash = Self::generate_unique_hash(&clap.receiver, &clap.amount, &clap.network_id);
|
||||
@@ -632,7 +635,7 @@ impl<T: Config> Pallet<T> {
|
||||
|
||||
let enough_authorities = Perbill::from_rational(
|
||||
ReceivedClaps::<T>::get(&received_claps_key).len() as u32,
|
||||
Authorities::<T>::get().len() as u32,
|
||||
Authorities::<T>::get(session_index).len() as u32,
|
||||
) > Perbill::from_percent(T::ApplauseThreshold::get());
|
||||
|
||||
ensure!(enough_authorities, Error::<T>::NotEnoughClaps);
|
||||
@@ -795,7 +798,8 @@ impl<T: Config> Pallet<T> {
|
||||
}
|
||||
|
||||
fn local_authorities() -> impl Iterator<Item = (u32, T::AuthorityId)> {
|
||||
let authorities = Authorities::<T>::get();
|
||||
let session_index = T::ValidatorSet::session_index();
|
||||
let authorities = Authorities::<T>::get(&session_index);
|
||||
let mut local_authorities = T::AuthorityId::all();
|
||||
local_authorities.sort();
|
||||
authorities.into_iter().enumerate().filter_map(move |(index, authority)| {
|
||||
@@ -920,13 +924,13 @@ impl<T: Config> Pallet<T> {
|
||||
authority_deviation < Perbill::from_percent(T::OffenceThreshold::get())
|
||||
}
|
||||
|
||||
fn initialize_authorities(authorities: &[T::AuthorityId]) {
|
||||
if !authorities.is_empty() {
|
||||
assert!(Authorities::<T>::get().is_empty(), "Authorities are already initilized!");
|
||||
let bounded_authorities = BoundedSlice::<'_, _, T::MaxAuthorities>::try_from(authorities)
|
||||
.expect("more than the maximum number of authorities");
|
||||
Authorities::<T>::put(bounded_authorities);
|
||||
}
|
||||
fn initialize_authorities(authorities: Vec<T::AuthorityId>) {
|
||||
let session_index = T::ValidatorSet::session_index();
|
||||
assert!(Authorities::<T>::get(&session_index).is_empty(), "Authorities are already initilized!");
|
||||
let bounded_authorities = WeakBoundedVec::<_, T::MaxAuthorities>::try_from(authorities)
|
||||
.expect("more than the maximum number of authorities");
|
||||
Authorities::<T>::set(&session_index, bounded_authorities);
|
||||
ClapsInSession::<T>::set(&session_index, Default::default());
|
||||
}
|
||||
|
||||
fn calculate_median_claps(session_index: &SessionIndex) -> u32 {
|
||||
@@ -952,10 +956,10 @@ impl<T: Config> Pallet<T> {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn set_test_authorities(authorities: Vec<T::AuthorityId>) {
|
||||
fn set_test_authorities(session_index: SessionIndex, authorities: Vec<T::AuthorityId>) {
|
||||
let bounded_authorities = WeakBoundedVec::<_, T::MaxAuthorities>::try_from(authorities)
|
||||
.expect("more than the maximum number of authorities");
|
||||
Authorities::<T>::put(bounded_authorities);
|
||||
Authorities::<T>::set(session_index, bounded_authorities);
|
||||
}
|
||||
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
@@ -983,23 +987,21 @@ impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T> {
|
||||
I: Iterator<Item = (&'a T::AccountId, T::AuthorityId)>,
|
||||
{
|
||||
let authorities = validators.map(|x| x.1).collect::<Vec<_>>();
|
||||
Self::initialize_authorities(&authorities);
|
||||
Self::initialize_authorities(authorities);
|
||||
}
|
||||
|
||||
fn on_new_session<'a, I: 'a>(_changed: bool, validators: I, _queued_validators: I)
|
||||
where
|
||||
I: Iterator<Item = (&'a T::AccountId, T::AuthorityId)>,
|
||||
{
|
||||
let previous_session = T::ValidatorSet::session_index().saturating_sub(1);
|
||||
let authorities = validators.map(|x| x.1).collect::<Vec<_>>();
|
||||
Self::initialize_authorities(&authorities);
|
||||
ClapsInSession::<T>::set(previous_session, Default::default());
|
||||
Self::initialize_authorities(authorities);
|
||||
}
|
||||
|
||||
fn on_before_session_ending() {
|
||||
let session_index = T::ValidatorSet::session_index();
|
||||
let validators = T::ValidatorSet::validators();
|
||||
let authorities = Authorities::<T>::get();
|
||||
let authorities = Authorities::<T>::get(&session_index);
|
||||
|
||||
let median_claps = Self::calculate_median_claps(&session_index);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user