add node side unit test

This commit is contained in:
akildemir
2024-02-09 11:22:56 +03:00
parent ad0ecc5185
commit 24ff866684
7 changed files with 235 additions and 12 deletions

View File

@@ -2,6 +2,8 @@
use core::marker::PhantomData;
use sp_runtime::print;
use scale::{Encode, Decode};
use scale_info::TypeInfo;
@@ -568,6 +570,7 @@ pub mod pallet {
account: T::AccountId,
amount: Amount,
) -> Result<bool, DispatchError> {
print("in daellocate");
// Check it's safe to decrease this set's stake by this amount
let new_total_staked = Self::total_allocated_stake(network)
.unwrap()
@@ -579,6 +582,8 @@ pub mod pallet {
Err(Error::<T>::DeallocationWouldRemoveEconomicSecurity)?;
}
print("passed stake req");
let old_allocation =
Self::allocation((network, account)).ok_or(Error::<T>::NonExistentValidator)?.0;
let new_allocation =
@@ -591,6 +596,8 @@ pub mod pallet {
Err(Error::<T>::DeallocationWouldRemoveParticipant)?;
}
print("passed DeallocationWouldRemoveParticipant");
let decreased_key_shares =
(old_allocation / allocation_per_key_share) > (new_allocation / allocation_per_key_share);
@@ -613,6 +620,8 @@ pub mod pallet {
}
}
print("passed bft");
// If we're not in-set, allow immediate deallocation
if !Self::in_set(network, account) {
Self::deposit_event(Event::AllocationDecreased {
@@ -621,9 +630,12 @@ pub mod pallet {
amount,
delayed_until: None,
});
print("returning ok true");
return Ok(true);
}
print("passed in set");
// Set it to PendingDeallocations, letting it be released upon a future session
// This unwrap should be fine as this account is active, meaning a session has occurred
let to_unlock_on = Self::session_to_unlock_on_for_current_set(network).unwrap();
@@ -635,6 +647,8 @@ pub mod pallet {
Some(Amount(existing.0 + amount.0)),
);
print("passed PendingDeallocations");
Self::deposit_event(Event::AllocationDecreased {
validator: account,
network,
@@ -642,6 +656,7 @@ pub mod pallet {
delayed_until: Some(to_unlock_on),
});
print("return ok false at the end");
Ok(false)
}
@@ -688,16 +703,20 @@ pub mod pallet {
}
pub fn retire_set(set: ValidatorSet) {
let keys = Keys::<T>::take(set).unwrap();
// If the prior prior set didn't report, emit they're retired now
// If the prior set didn't report, emit they're retired now
if PendingSlashReport::<T>::get(set.network).is_some() {
Self::deposit_event(Event::SetRetired {
set: ValidatorSet { network: set.network, session: Session(set.session.0 - 1) },
});
}
// This overwrites the prior value as the prior to-report set's stake presumably just
// unlocked, making their report unenforceable
PendingSlashReport::<T>::set(set.network, Some(keys.0));
// Serai network slashes are handled by BABE/GRANDPA
if set.network != NetworkId::Serai {
// This overwrites the prior value as the prior to-report set's stake presumably just
// unlocked, making their report unenforceable
let keys = Keys::<T>::take(set).unwrap();
PendingSlashReport::<T>::set(set.network, Some(keys.0));
}
// We're retiring this set because the set after it accepted the handover
Self::deposit_event(Event::AcceptedHandover {
@@ -726,6 +745,11 @@ pub mod pallet {
.expect("no Serai participants upon rotate_session");
let prior_serai_session = Self::session(NetworkId::Serai).unwrap();
print("now session:");
print(prior_serai_session.0);
print("now validators: ");
print(now_validators.len());
// TODO: T::SessionHandler::on_before_session_ending() was here.
// end the current serai session.
Self::retire_set(ValidatorSet { network: NetworkId::Serai, session: prior_serai_session });
@@ -736,6 +760,10 @@ pub mod pallet {
// Update Babe and Grandpa
let session = prior_serai_session.0 + 1;
let next_validators = Participants::<T>::get(NetworkId::Serai).unwrap();
print("next session:");
print(session);
print("next validators: ");
print(next_validators.len());
Babe::<T>::enact_epoch_change(
WeakBoundedVec::force_from(
now_validators.iter().copied().map(|(id, w)| (BabeAuthorityId::from(id), w)).collect(),
@@ -750,7 +778,7 @@ pub mod pallet {
Grandpa::<T>::new_session(
true,
session,
next_validators.into_iter().map(|(id, w)| (GrandpaAuthorityId::from(id), w)).collect(),
now_validators.into_iter().map(|(id, w)| (GrandpaAuthorityId::from(id), w)).collect(),
);
// Clear SeraiDisabledIndices, only preserving keys still present in the new session