forked from ghostchain/ghost-node
		
	tests updated and small tweaks of the lib
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
This commit is contained in:
		
							parent
							
								
									2b738c009b
								
							
						
					
					
						commit
						04a63e234d
					
				
							
								
								
									
										4
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										4
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							@ -3504,7 +3504,7 @@ dependencies = [
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "ghost-claims"
 | 
			
		||||
version = "0.2.2"
 | 
			
		||||
version = "0.2.3"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "frame-benchmarking",
 | 
			
		||||
 "frame-support",
 | 
			
		||||
@ -3834,7 +3834,7 @@ dependencies = [
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "ghost-slow-clap"
 | 
			
		||||
version = "0.3.16"
 | 
			
		||||
version = "0.3.17"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "frame-benchmarking",
 | 
			
		||||
 "frame-support",
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,6 @@
 | 
			
		||||
[package]
 | 
			
		||||
name = "ghost-slow-clap"
 | 
			
		||||
version = "0.3.16"
 | 
			
		||||
version = "0.3.17"
 | 
			
		||||
description = "Applause protocol for the EVM bridge"
 | 
			
		||||
license.workspace = true
 | 
			
		||||
authors.workspace = true
 | 
			
		||||
 | 
			
		||||
@ -4,12 +4,7 @@ use super::*;
 | 
			
		||||
use frame_benchmarking::v1::*;
 | 
			
		||||
 | 
			
		||||
use frame_system::RawOrigin;
 | 
			
		||||
use frame_support::traits::fungible::{Inspect, Mutate};
 | 
			
		||||
 | 
			
		||||
use crate::Pallet as SlowClap;
 | 
			
		||||
 | 
			
		||||
const MAX_CLAPS: u32 = 100;
 | 
			
		||||
const MAX_COMPANIONS: u32 = 20;
 | 
			
		||||
use frame_support::traits::fungible::Inspect;
 | 
			
		||||
 | 
			
		||||
pub fn create_account<T: Config>() -> T::AccountId {
 | 
			
		||||
    let account_bytes = Vec::from([1u8; 32]);
 | 
			
		||||
@ -17,184 +12,40 @@ pub fn create_account<T: Config>() -> T::AccountId {
 | 
			
		||||
        .expect("32 bytes always construct an AccountId32")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn create_companions<T: Config>(
 | 
			
		||||
    total: usize,
 | 
			
		||||
    network_id: NetworkIdOf<T>,
 | 
			
		||||
    user_account: T::AccountId,
 | 
			
		||||
    fee: H256,
 | 
			
		||||
    receiver: H160,
 | 
			
		||||
) -> Result<CompanionId, &'static str> {
 | 
			
		||||
    T::NetworkDataHandler::register(network_id.into(), NetworkData {
 | 
			
		||||
        chain_name: "Ethereum".into(),
 | 
			
		||||
        default_endpoint: 
 | 
			
		||||
            "https://base-mainnet.core.chainstack.com/2fc1de7f08c0465f6a28e3c355e0cb14/".into(),
 | 
			
		||||
            finality_delay: Some(0),
 | 
			
		||||
            release_delay: Some(0),
 | 
			
		||||
            network_type: Default::default(),
 | 
			
		||||
            gatekeeper: b"0x1234567891234567891234567891234567891234".to_vec(),
 | 
			
		||||
            topic_name: b"0x12345678912345678912345678912345678912345678912345678912345678".to_vec(),
 | 
			
		||||
            incoming_fee: 0,
 | 
			
		||||
            outgoing_fee: 0,
 | 
			
		||||
    }).map_err(|_| BenchmarkError::Weightless)?;
 | 
			
		||||
 | 
			
		||||
    let mut last_companion_id = 0;
 | 
			
		||||
    for _ in 0..total {
 | 
			
		||||
        let minimum_balance = <<T as pallet::Config>::Currency>::minimum_balance();
 | 
			
		||||
        let balance = minimum_balance + minimum_balance;
 | 
			
		||||
        let companion = Companion::<NetworkIdOf::<T>, BalanceOf::<T>> { 
 | 
			
		||||
            network_id: network_id.into(), receiver, 
 | 
			
		||||
            fee, amount: balance,
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        let _ = <<T as pallet::Config>::Currency>::mint_into(&user_account, balance);
 | 
			
		||||
        let companion_id = SlowClap::<T>::current_companion_id();
 | 
			
		||||
        let _ = SlowClap::<T>::propose_companion(
 | 
			
		||||
            RawOrigin::Signed(user_account.clone()).into(), 
 | 
			
		||||
            network_id, 
 | 
			
		||||
            companion,
 | 
			
		||||
        )?;
 | 
			
		||||
        last_companion_id = companion_id;
 | 
			
		||||
    }
 | 
			
		||||
    Ok(last_companion_id)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn create_claps<T: Config>(i: u32, j: u32) -> Result<
 | 
			
		||||
    (
 | 
			
		||||
        Vec<crate::Clap<T::AccountId, NetworkIdOf<T>, BalanceOf<T>>>,
 | 
			
		||||
        <T::AuthorityId as RuntimeAppPublic>::Signature,
 | 
			
		||||
    ), 
 | 
			
		||||
    &'static str,
 | 
			
		||||
> {
 | 
			
		||||
benchmarks! {
 | 
			
		||||
    slow_clap {
 | 
			
		||||
        let minimum_balance = <<T as pallet::Config>::Currency>::minimum_balance();
 | 
			
		||||
        let receiver = create_account::<T>();
 | 
			
		||||
        let amount = minimum_balance + minimum_balance;
 | 
			
		||||
    let total_amount = amount.saturating_mul(j.into());
 | 
			
		||||
        let network_id = NetworkIdOf::<T>::default();
 | 
			
		||||
 | 
			
		||||
    let mut claps = Vec::new();
 | 
			
		||||
    let mut companions = BTreeMap::new();
 | 
			
		||||
 | 
			
		||||
        let authorities = vec![T::AuthorityId::generate_pair(None)];
 | 
			
		||||
        let bounded_authorities = 
 | 
			
		||||
            WeakBoundedVec::<_, T::MaxAuthorities>::try_from(authorities.clone())
 | 
			
		||||
            .map_err(|()| "more than the maximum number of keys provided")?;
 | 
			
		||||
        Authorities::<T>::put(bounded_authorities);
 | 
			
		||||
 | 
			
		||||
    for index in 0..j { 
 | 
			
		||||
        companions.insert(
 | 
			
		||||
            index.into(), 
 | 
			
		||||
            amount,
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for _ in 0..i {
 | 
			
		||||
        claps.push(Clap {
 | 
			
		||||
            session_index: 1,
 | 
			
		||||
        let clap = Clap {
 | 
			
		||||
            session_index: 0,
 | 
			
		||||
            authority_index: 0,
 | 
			
		||||
            network_id,
 | 
			
		||||
            transaction_hash: H256::repeat_byte(1u8),
 | 
			
		||||
            block_number: 69,
 | 
			
		||||
            removed: true,
 | 
			
		||||
            receiver: create_account::<T>(),
 | 
			
		||||
            amount: total_amount,
 | 
			
		||||
            companions: companions.clone(),
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
            removed: false,
 | 
			
		||||
            network_id,
 | 
			
		||||
            receiver: receiver.clone(),
 | 
			
		||||
            amount,
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        let authority_id = authorities
 | 
			
		||||
            .get(0usize)
 | 
			
		||||
            .expect("first authority should exist");
 | 
			
		||||
    let encoded_claps = claps.encode();
 | 
			
		||||
    let signature = authority_id.sign(&encoded_claps)
 | 
			
		||||
        let encoded_clap = clap.encode();
 | 
			
		||||
        let signature = authority_id.sign(&encoded_clap)
 | 
			
		||||
            .ok_or("couldn't make signature")?;
 | 
			
		||||
 | 
			
		||||
    Ok((claps, signature))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
benchmarks! {
 | 
			
		||||
    slow_clap {
 | 
			
		||||
        let k in 1 .. MAX_CLAPS;
 | 
			
		||||
        let j in 1 .. MAX_COMPANIONS;
 | 
			
		||||
 | 
			
		||||
        let receiver = H160::repeat_byte(69u8);
 | 
			
		||||
        let fee = H256::repeat_byte(0u8);
 | 
			
		||||
        let user_account: T::AccountId = whitelisted_caller();
 | 
			
		||||
        let network_id = <<T as pallet::Config>::NetworkDataHandler as NetworkDataBasicHandler>::NetworkId::default();
 | 
			
		||||
        
 | 
			
		||||
        let (claps, signature) = create_claps::<T>(k, j)?;
 | 
			
		||||
        let _ = create_companions::<T>(j as usize, network_id, user_account, fee, receiver)?;
 | 
			
		||||
    }: _(RawOrigin::None, claps, signature)
 | 
			
		||||
    }: _(RawOrigin::None, clap, signature)
 | 
			
		||||
    verify {
 | 
			
		||||
        let minimum_balance = <<T as pallet::Config>::Currency>::minimum_balance();
 | 
			
		||||
        let total_amount = (minimum_balance + minimum_balance).saturating_mul(j.into());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    propose_companion {
 | 
			
		||||
        let receiver = H160::repeat_byte(69u8);
 | 
			
		||||
        let fee = H256::repeat_byte(0u8);
 | 
			
		||||
        let user_account: T::AccountId = whitelisted_caller();
 | 
			
		||||
        let network_id = <<T as pallet::Config>::NetworkDataHandler as NetworkDataBasicHandler>::NetworkId::default();
 | 
			
		||||
        // T::NetworkDataHandler::register(network_id.into(), NetworkData {
 | 
			
		||||
        //     chain_name: "Ethereum".into(),
 | 
			
		||||
        //     // https://nd-422-757-666.p2pify.com/0a9d79d93fb2f4a4b1e04695da2b77a7/
 | 
			
		||||
        //     default_endpoint: 
 | 
			
		||||
        //         "https://base-mainnet.core.chainstack.com/2fc1de7f08c0465f6a28e3c355e0cb14/".into(),
 | 
			
		||||
        //     finality_delay: Some(50),
 | 
			
		||||
        //     release_delay: Some(100),
 | 
			
		||||
        //     network_type: Default::default(),
 | 
			
		||||
        //     gatekeeper: b"0x1234567891234567891234567891234567891234".to_vec(),
 | 
			
		||||
        //     topic_name: b"0x12345678912345678912345678912345678912345678912345678912345678".to_vec(),
 | 
			
		||||
        //     incoming_fee: 0,
 | 
			
		||||
        //     outgoing_fee: 0,
 | 
			
		||||
        // }).map_err(|_| BenchmarkError::Weightless)?;
 | 
			
		||||
        let companion_id = create_companions::<T>(1, network_id, user_account.clone(), fee, receiver)?;
 | 
			
		||||
        let companion_id = SlowClap::<T>::current_companion_id();
 | 
			
		||||
        let minimum_balance = <<T as pallet::Config>::Currency>::minimum_balance();
 | 
			
		||||
        let balance = minimum_balance + minimum_balance;
 | 
			
		||||
        let companion = Companion::<NetworkIdOf::<T>, BalanceOf::<T>> { 
 | 
			
		||||
            network_id: network_id.into(), receiver, 
 | 
			
		||||
            fee, amount: balance,
 | 
			
		||||
        };
 | 
			
		||||
        let _ = <<T as pallet::Config>::Currency>::mint_into(&user_account, balance);
 | 
			
		||||
        assert_eq!(SlowClap::<T>::current_companion_id(), companion_id);
 | 
			
		||||
    }: _(RawOrigin::Signed(user_account), network_id.into(), companion)
 | 
			
		||||
    verify {
 | 
			
		||||
        assert_eq!(SlowClap::<T>::current_companion_id(), companion_id + 1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    release_companion {
 | 
			
		||||
        let receiver = H160::repeat_byte(69u8);
 | 
			
		||||
        let fee = H256::repeat_byte(0u8);
 | 
			
		||||
        let user_account: T::AccountId = whitelisted_caller();
 | 
			
		||||
        let network_id = <<T as pallet::Config>::NetworkDataHandler as NetworkDataBasicHandler>::NetworkId::default();
 | 
			
		||||
        let companion_id = create_companions::<T>(1, network_id, user_account.clone(), fee, receiver)?;
 | 
			
		||||
        assert_eq!(SlowClap::<T>::release_blocks(companion_id), BlockNumberFor::<T>::default());
 | 
			
		||||
    }: _(RawOrigin::Signed(user_account), network_id.into(), companion_id)
 | 
			
		||||
    verify {
 | 
			
		||||
        assert_ne!(SlowClap::<T>::release_blocks(companion_id), BlockNumberFor::<T>::default());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    kill_companion {
 | 
			
		||||
        let receiver = H160::repeat_byte(69u8);
 | 
			
		||||
        let fee = H256::repeat_byte(0u8);
 | 
			
		||||
        let user_account: T::AccountId = whitelisted_caller();
 | 
			
		||||
        let network_id = <<T as pallet::Config>::NetworkDataHandler as NetworkDataBasicHandler>::NetworkId::default();
 | 
			
		||||
        let companion_id = create_companions::<T>(1, network_id, user_account.clone(), fee, receiver)?;
 | 
			
		||||
        SlowClap::<T>::release_companion(
 | 
			
		||||
            RawOrigin::Signed(user_account.clone()).into(), 
 | 
			
		||||
            network_id, 
 | 
			
		||||
            companion_id,
 | 
			
		||||
        )?;
 | 
			
		||||
        let block_shift =
 | 
			
		||||
            <<T as pallet::Config>::NetworkDataHandler as NetworkDataInspectHandler<NetworkData>>::get(&network_id)
 | 
			
		||||
            .unwrap()
 | 
			
		||||
            .release_delay
 | 
			
		||||
            .unwrap();
 | 
			
		||||
        frame_system::Pallet::<T>::set_block_number((block_shift + 1).saturated_into());
 | 
			
		||||
    }: _(RawOrigin::Signed(user_account), network_id.into(), companion_id)
 | 
			
		||||
    verify {
 | 
			
		||||
        assert_eq!(SlowClap::<T>::companions(network_id, companion_id), None);
 | 
			
		||||
        assert_eq!(SlowClap::<T>::companion_details(companion_id), None);
 | 
			
		||||
        assert_eq!(SlowClap::<T>::current_companion_id(), companion_id + 1);
 | 
			
		||||
        assert_eq!(<<T as pallet::Config>::Currency>::total_balance(&receiver), amount);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    impl_benchmark_test_suite!(
 | 
			
		||||
 | 
			
		||||
@ -13,7 +13,7 @@ use frame_support::{
 | 
			
		||||
        EstimateNextSessionRotation, ValidatorSet, ValidatorSetWithIdentification, 
 | 
			
		||||
        OneSessionHandler, Get,
 | 
			
		||||
    },
 | 
			
		||||
    PalletId, BoundedSlice, WeakBoundedVec,
 | 
			
		||||
    BoundedSlice, WeakBoundedVec,
 | 
			
		||||
    
 | 
			
		||||
};
 | 
			
		||||
use frame_system::{
 | 
			
		||||
@ -188,12 +188,6 @@ pub struct Clap<AccountId, NetworkId, Balance> {
 | 
			
		||||
    pub amount: Balance,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Default, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
 | 
			
		||||
pub struct AuthorityClapsInfo {
 | 
			
		||||
    pub total: u32,
 | 
			
		||||
    pub individual: BTreeMap<AuthIndex, SessionAuthorityInfo>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Default, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
 | 
			
		||||
pub struct SessionAuthorityInfo {
 | 
			
		||||
    pub claps: u32,
 | 
			
		||||
@ -305,9 +299,6 @@ pub mod pallet {
 | 
			
		||||
        #[pallet::constant]
 | 
			
		||||
        type UnsignedPriority: Get<TransactionPriority>;
 | 
			
		||||
 | 
			
		||||
        #[pallet::constant]
 | 
			
		||||
        type TreasuryPalletId: Get<PalletId>;
 | 
			
		||||
 | 
			
		||||
        type WeightInfo: WeightInfo;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -333,12 +324,11 @@ pub mod pallet {
 | 
			
		||||
    #[pallet::error]
 | 
			
		||||
    pub enum Error<T> {
 | 
			
		||||
        NotAnAuthority,
 | 
			
		||||
        ClapForThePastSession,
 | 
			
		||||
        ClapForWrongSession,
 | 
			
		||||
        CurrentValidatorIsDisabled,
 | 
			
		||||
        AlreadyClapped,
 | 
			
		||||
        UnregisteredClapRemove,
 | 
			
		||||
        TooMuchAuthorities,
 | 
			
		||||
        NotEnoughToApplause,
 | 
			
		||||
        CouldNotAccumulateCommission,
 | 
			
		||||
        CouldNotIncreaseGatekeeperAmount,
 | 
			
		||||
    }
 | 
			
		||||
@ -369,25 +359,13 @@ pub mod pallet {
 | 
			
		||||
        ValueQuery
 | 
			
		||||
    >;
 | 
			
		||||
 | 
			
		||||
    #[pallet::storage]
 | 
			
		||||
    #[pallet::getter(fn authorities_claps)]
 | 
			
		||||
    pub(super) type AuthoritiesClaps<T: Config> = StorageDoubleMap<
 | 
			
		||||
        _,
 | 
			
		||||
        Twox64Concat,
 | 
			
		||||
        T::AuthorityId,
 | 
			
		||||
        Twox64Concat,
 | 
			
		||||
        H256,
 | 
			
		||||
        bool,
 | 
			
		||||
        ValueQuery,
 | 
			
		||||
    >;
 | 
			
		||||
 | 
			
		||||
    #[pallet::storage]
 | 
			
		||||
    #[pallet::getter(fn claps_in_session)]
 | 
			
		||||
    pub(super) type ClapsInSession<T: Config> = StorageMap<
 | 
			
		||||
        _,
 | 
			
		||||
        Twox64Concat,
 | 
			
		||||
        SessionIndex,
 | 
			
		||||
        AuthorityClapsInfo,
 | 
			
		||||
        BTreeMap<AuthIndex, SessionAuthorityInfo>,
 | 
			
		||||
        ValueQuery,
 | 
			
		||||
    >;
 | 
			
		||||
 | 
			
		||||
@ -479,7 +457,7 @@ pub mod pallet {
 | 
			
		||||
                let authorities = Authorities::<T>::get();
 | 
			
		||||
                let authority = match authorities.get(clap.authority_index as usize) {
 | 
			
		||||
                    Some(authority) => authority,
 | 
			
		||||
                    None => return InvalidTransaction::BadProof.into(),
 | 
			
		||||
                    None => return InvalidTransaction::BadSigner.into(),
 | 
			
		||||
                };
 | 
			
		||||
 | 
			
		||||
                let signature_valid = clap.using_encoded(|encoded_clap| {
 | 
			
		||||
@ -518,7 +496,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>::ClapForThePastSession);
 | 
			
		||||
        ensure!(current_session_index == clap.session_index, Error::<T>::ClapForWrongSession);
 | 
			
		||||
 | 
			
		||||
        let authorities = Authorities::<T>::get();
 | 
			
		||||
        ensure!(authorities.get(clap.authority_index as usize).is_some(), Error::<T>::NotAnAuthority);
 | 
			
		||||
@ -543,15 +521,14 @@ impl<T: Config> Pallet<T> {
 | 
			
		||||
        })?;
 | 
			
		||||
 | 
			
		||||
        ClapsInSession::<T>::try_mutate(&clap.session_index, |claps_details| {
 | 
			
		||||
            if claps_details.individual.get(&clap.authority_index).map(|x| x.disabled).unwrap_or_default() {
 | 
			
		||||
            if claps_details.get(&clap.authority_index).map(|x| x.disabled).unwrap_or_default() {
 | 
			
		||||
                return Err(Error::<T>::CurrentValidatorIsDisabled);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            (*claps_details).total.saturating_inc();
 | 
			
		||||
            (*claps_details).individual
 | 
			
		||||
            (*claps_details)
 | 
			
		||||
                .entry(clap.authority_index)
 | 
			
		||||
                .and_modify(|individual| (*individual).claps.saturating_inc())
 | 
			
		||||
                .or_default();
 | 
			
		||||
                .or_insert(SessionAuthorityInfo { claps: 1u32, disabled: false });
 | 
			
		||||
 | 
			
		||||
            Ok(())
 | 
			
		||||
        })?;
 | 
			
		||||
@ -888,7 +865,6 @@ impl<T: Config> Pallet<T> {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        let number_of_claps = ClapsInSession::<T>::get(session_index)
 | 
			
		||||
            .individual
 | 
			
		||||
            .entry(authority_index as AuthIndex)
 | 
			
		||||
            .or_default()
 | 
			
		||||
            .claps;
 | 
			
		||||
@ -905,10 +881,17 @@ impl<T: Config> Pallet<T> {
 | 
			
		||||
        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 clappers");
 | 
			
		||||
                .expect("more than the maximum number of authorities");
 | 
			
		||||
            Authorities::<T>::put(bounded_authorities);
 | 
			
		||||
        }    
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[cfg(test)]
 | 
			
		||||
    fn set_test_authorities(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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<T: Config> sp_runtime::BoundToRuntimeAppPublic for Pallet<T> {
 | 
			
		||||
@ -948,17 +931,16 @@ impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T> {
 | 
			
		||||
        let validators = T::ValidatorSet::validators();
 | 
			
		||||
        let authorities = Authorities::<T>::get();
 | 
			
		||||
 | 
			
		||||
        let claps_details = ClapsInSession::<T>::get(session_index);
 | 
			
		||||
        let total_claps_in_session = claps_details.total;
 | 
			
		||||
        let average_claps = claps_details
 | 
			
		||||
            .individual
 | 
			
		||||
        let (sum_claps, total_claps) = ClapsInSession::<T>::get(&session_index)
 | 
			
		||||
            .iter()
 | 
			
		||||
            .filter(|(_, value)| !value.disabled)
 | 
			
		||||
            .fold(0u32, |acc, (_, value)| acc.saturating_add(value.claps))
 | 
			
		||||
            .checked_div(total_claps_in_session)
 | 
			
		||||
            .unwrap_or_default();
 | 
			
		||||
            .fold((0u32, 0u32), |(sum, total), (_, value)| 
 | 
			
		||||
                (sum.saturating_add(value.claps), total.saturating_add(1))
 | 
			
		||||
            );
 | 
			
		||||
 | 
			
		||||
        // TODO: seems like it's not working
 | 
			
		||||
        let average_claps = sum_claps
 | 
			
		||||
            .checked_div(total_claps)
 | 
			
		||||
            .unwrap_or_default();
 | 
			
		||||
 | 
			
		||||
        let offenders = validators
 | 
			
		||||
            .into_iter()
 | 
			
		||||
@ -988,7 +970,6 @@ impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T> {
 | 
			
		||||
        let session_index = T::ValidatorSet::session_index();
 | 
			
		||||
        ClapsInSession::<T>::mutate(&session_index, |claps_details| {
 | 
			
		||||
            (*claps_details)
 | 
			
		||||
                .individual
 | 
			
		||||
                .entry(validator_index as AuthIndex)
 | 
			
		||||
                .and_modify(|individual| (*individual).disabled = true)
 | 
			
		||||
                .or_insert(SessionAuthorityInfo { claps: 0u32, disabled: true });
 | 
			
		||||
 | 
			
		||||
@ -11,13 +11,16 @@ use pallet_session::historical as pallet_session_historical;
 | 
			
		||||
use sp_runtime::{
 | 
			
		||||
    testing::{TestXt, UintAuthorityId},
 | 
			
		||||
    traits::ConvertInto,
 | 
			
		||||
    BuildStorage, Permill,
 | 
			
		||||
    Permill,
 | 
			
		||||
};
 | 
			
		||||
use sp_staking::{
 | 
			
		||||
    offence::{OffenceError, ReportOffence},
 | 
			
		||||
    SessionIndex,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#[cfg(feature = "runtime-benchmarks")]
 | 
			
		||||
use sp_runtime::BuildStorage;
 | 
			
		||||
 | 
			
		||||
use crate as slow_clap;
 | 
			
		||||
use crate::Config;
 | 
			
		||||
 | 
			
		||||
@ -84,24 +87,22 @@ impl ReportOffence<u64, IdentificationTuple, Offence> for OffenceHandler {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn alice_account_id() -> <Runtime as frame_system::Config>::AccountId { 69 }
 | 
			
		||||
pub fn eve_account_id() -> <Runtime as frame_system::Config>::AccountId { 1337 }
 | 
			
		||||
 | 
			
		||||
pub fn new_test_ext() -> sp_io::TestExternalities {
 | 
			
		||||
    let mut t = frame_system::GenesisConfig::<Runtime>::default()
 | 
			
		||||
    let t = frame_system::GenesisConfig::<Runtime>::default()
 | 
			
		||||
        .build_storage()
 | 
			
		||||
        .unwrap();
 | 
			
		||||
 | 
			
		||||
    pallet_balances::GenesisConfig::<Runtime> {
 | 
			
		||||
        balances: vec![ (alice_account_id(), 100) ],
 | 
			
		||||
    }
 | 
			
		||||
        .assimilate_storage(&mut t)
 | 
			
		||||
        .unwrap();
 | 
			
		||||
 | 
			
		||||
    let mut result = sp_io::TestExternalities::new(t);
 | 
			
		||||
 | 
			
		||||
    result.execute_with(|| {
 | 
			
		||||
        System::set_block_number(1);
 | 
			
		||||
        for i in 1..=3 {
 | 
			
		||||
            System::inc_providers(&i);
 | 
			
		||||
            assert_eq!(Session::set_keys(
 | 
			
		||||
                    RuntimeOrigin::signed(i), 
 | 
			
		||||
                    i.into(), 
 | 
			
		||||
                    vec![],
 | 
			
		||||
            ), Ok(()));
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    result
 | 
			
		||||
@ -167,6 +168,7 @@ impl frame_support::traits::EstimateNextSessionRotation<u64> for TestNextSession
 | 
			
		||||
 | 
			
		||||
impl ghost_networks::Config for Runtime {
 | 
			
		||||
    type RuntimeEvent = RuntimeEvent;
 | 
			
		||||
    type Currency = Balances;
 | 
			
		||||
    type NetworkId = u32;
 | 
			
		||||
    type RegisterOrigin = EnsureRoot<Self::AccountId>;
 | 
			
		||||
    type UpdateOrigin = EnsureRoot<Self::AccountId>;
 | 
			
		||||
@ -199,23 +201,15 @@ impl Config for Runtime {
 | 
			
		||||
    type ReportUnresponsiveness = OffenceHandler;
 | 
			
		||||
 | 
			
		||||
    type MaxAuthorities = ConstU32<5>;
 | 
			
		||||
    type MaxNumberOfClaps = ConstU32<100>;
 | 
			
		||||
    type ApplauseThreshold = ConstU32<0>;
 | 
			
		||||
    type MaxAuthorityInfoInSession = ConstU32<5_000>;
 | 
			
		||||
    type OffenceThreshold = ConstU32<40>;
 | 
			
		||||
    type ApplauseThreshold = ConstU32<50>;
 | 
			
		||||
    type OffenceThreshold = ConstU32<75>;
 | 
			
		||||
    type UnsignedPriority = ConstU64<{ 1 << 20 }>;
 | 
			
		||||
    type TreasuryPalletId = TreasuryPalletId;
 | 
			
		||||
 | 
			
		||||
    type WeightInfo = ();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub type Extrinsic = TestXt<RuntimeCall, ()>;
 | 
			
		||||
 | 
			
		||||
// impl frame_system::offchain::SigningTypes for Runtime {
 | 
			
		||||
//     type Public = <Signature as Verify>::Signer;
 | 
			
		||||
//     type Signature = Signature;
 | 
			
		||||
// }
 | 
			
		||||
 | 
			
		||||
impl<LocalCall> frame_system::offchain::SendTransactionTypes<LocalCall> for Runtime
 | 
			
		||||
where
 | 
			
		||||
    RuntimeCall: From<LocalCall>,
 | 
			
		||||
@ -224,30 +218,16 @@ where
 | 
			
		||||
    type Extrinsic = Extrinsic;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// impl<LocalCall> frame_system::offchain::CreateSignedTransaction<LocalCall> for Runtime 
 | 
			
		||||
// where
 | 
			
		||||
//     RuntimeCall: From<LocalCall>,
 | 
			
		||||
// {
 | 
			
		||||
//     fn create_transaction<C: frame_system::offchain::AppCrypto<Self::Public, Self::Signature>>(
 | 
			
		||||
//             call: Self::OverarchingCall,
 | 
			
		||||
//             _public: Self::Public,
 | 
			
		||||
//             _account: Self::AccountId,
 | 
			
		||||
//             nonce: Self::Nonce,
 | 
			
		||||
//         ) -> Option<(RuntimeCall, <Extrinsic as ExtrinsicT>::SignaturePayload)> {
 | 
			
		||||
//         Some((call, (nonce.into(), ())))
 | 
			
		||||
//     }
 | 
			
		||||
// }
 | 
			
		||||
pub fn advance_session() {
 | 
			
		||||
    let now = System::block_number().max(1);
 | 
			
		||||
    System::set_block_number(now + 1);
 | 
			
		||||
    Session::rotate_session();
 | 
			
		||||
 | 
			
		||||
// pub fn advance_session() {
 | 
			
		||||
//     let now = System::block_number().max(1);
 | 
			
		||||
//     System::set_block_number(now + 1);
 | 
			
		||||
//     Session::rotate_session();
 | 
			
		||||
//
 | 
			
		||||
//     let authorities = Session::validators()
 | 
			
		||||
//         .into_iter()
 | 
			
		||||
//         .map(UintAuthorityId)
 | 
			
		||||
//         .collect();
 | 
			
		||||
//
 | 
			
		||||
//     SlowClap::set_authorities(authorities);
 | 
			
		||||
//     assert_eq!(Session::current_index(), (now / Period::get()) as u32);
 | 
			
		||||
// }
 | 
			
		||||
    let authorities = Session::validators()
 | 
			
		||||
        .into_iter()
 | 
			
		||||
        .map(UintAuthorityId)
 | 
			
		||||
        .collect();
 | 
			
		||||
 | 
			
		||||
    SlowClap::set_test_authorities(authorities);
 | 
			
		||||
    assert_eq!(Session::current_index(), (now / Period::get()) as u32);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Loading…
	
		Reference in New Issue
	
	Block a user