mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 04:41:03 +00:00
Use parameter_types instead of thread_local for test-setup (#12036)
* Edit to Assets. parameter_types * fixes * Test Fixes. WIP * Edits to pallet-aura * Camel Case Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Implementation of mutate fn * update to pallet-aura * Update to frame-system. Fixes * Update to frame-support-test. CamelCases * Updates to frame- contracts, offences, staking, bounties, child bounties * Edit to mutate fn. Changes to frame-contracts. CamelCase pallet-aura * Edits to frame-contracts & executive * cargo +nightly fmt * unused import removed * unused import removed * cargo +nightly fmt * minor adjustment * updates * updates * cargo +nightly fmt * cargo +nightly fmt * take fn implemented * update * update * Fixes to CallFilter * cargo +nightly fmt * final fixes * Default changed to $value * Update frame/support/src/lib.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This commit is contained in:
@@ -19,8 +19,6 @@
|
||||
|
||||
#![cfg(test)]
|
||||
|
||||
use std::cell::RefCell;
|
||||
|
||||
use frame_support::{
|
||||
parameter_types,
|
||||
traits::{ConstU32, ConstU64},
|
||||
@@ -57,18 +55,18 @@ frame_support::construct_runtime!(
|
||||
}
|
||||
);
|
||||
|
||||
thread_local! {
|
||||
pub static VALIDATORS: RefCell<Option<Vec<u64>>> = RefCell::new(Some(vec![
|
||||
parameter_types! {
|
||||
pub static Validators: Option<Vec<u64>> = Some(vec![
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
]));
|
||||
]);
|
||||
}
|
||||
|
||||
pub struct TestSessionManager;
|
||||
impl pallet_session::SessionManager<u64> for TestSessionManager {
|
||||
fn new_session(_new_index: SessionIndex) -> Option<Vec<u64>> {
|
||||
VALIDATORS.with(|l| l.borrow_mut().take())
|
||||
Validators::mutate(|l| l.take())
|
||||
}
|
||||
fn end_session(_: SessionIndex) {}
|
||||
fn start_session(_: SessionIndex) {}
|
||||
@@ -76,10 +74,8 @@ impl pallet_session::SessionManager<u64> for TestSessionManager {
|
||||
|
||||
impl pallet_session::historical::SessionManager<u64, u64> for TestSessionManager {
|
||||
fn new_session(_new_index: SessionIndex) -> Option<Vec<(u64, u64)>> {
|
||||
VALIDATORS.with(|l| {
|
||||
l.borrow_mut()
|
||||
.take()
|
||||
.map(|validators| validators.iter().map(|v| (*v, *v)).collect())
|
||||
Validators::mutate(|l| {
|
||||
l.take().map(|validators| validators.iter().map(|v| (*v, *v)).collect())
|
||||
})
|
||||
}
|
||||
fn end_session(_: SessionIndex) {}
|
||||
@@ -91,15 +87,15 @@ pub type Extrinsic = TestXt<Call, ()>;
|
||||
type IdentificationTuple = (u64, u64);
|
||||
type Offence = crate::UnresponsivenessOffence<IdentificationTuple>;
|
||||
|
||||
thread_local! {
|
||||
pub static OFFENCES: RefCell<Vec<(Vec<u64>, Offence)>> = RefCell::new(vec![]);
|
||||
parameter_types! {
|
||||
pub static Offences: Vec<(Vec<u64>, Offence)> = vec![];
|
||||
}
|
||||
|
||||
/// A mock offence report handler.
|
||||
pub struct OffenceHandler;
|
||||
impl ReportOffence<u64, IdentificationTuple, Offence> for OffenceHandler {
|
||||
fn report_offence(reporters: Vec<u64>, offence: Offence) -> Result<(), OffenceError> {
|
||||
OFFENCES.with(|l| l.borrow_mut().push((reporters, offence)));
|
||||
Offences::mutate(|l| l.push((reporters, offence)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -183,12 +179,12 @@ impl pallet_authorship::Config for Runtime {
|
||||
type EventHandler = ImOnline;
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
pub static MOCK_CURRENT_SESSION_PROGRESS: RefCell<Option<Option<Permill>>> = RefCell::new(None);
|
||||
parameter_types! {
|
||||
pub static MockCurrentSessionProgress: Option<Option<Permill>> = None;
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
pub static MOCK_AVERAGE_SESSION_LENGTH: RefCell<Option<u64>> = RefCell::new(None);
|
||||
parameter_types! {
|
||||
pub static MockAverageSessionLength: Option<u64> = None;
|
||||
}
|
||||
|
||||
pub struct TestNextSessionRotation;
|
||||
@@ -196,7 +192,7 @@ pub struct TestNextSessionRotation;
|
||||
impl frame_support::traits::EstimateNextSessionRotation<u64> for TestNextSessionRotation {
|
||||
fn average_session_length() -> u64 {
|
||||
// take the mock result if any and return it
|
||||
let mock = MOCK_AVERAGE_SESSION_LENGTH.with(|p| p.borrow_mut().take());
|
||||
let mock = MockAverageSessionLength::mutate(|p| p.take());
|
||||
|
||||
mock.unwrap_or(pallet_session::PeriodicSessions::<Period, Offset>::average_session_length())
|
||||
}
|
||||
@@ -208,7 +204,7 @@ impl frame_support::traits::EstimateNextSessionRotation<u64> for TestNextSession
|
||||
);
|
||||
|
||||
// take the mock result if any and return it
|
||||
let mock = MOCK_CURRENT_SESSION_PROGRESS.with(|p| p.borrow_mut().take());
|
||||
let mock = MockCurrentSessionProgress::mutate(|p| p.take());
|
||||
|
||||
(mock.unwrap_or(estimate), weight)
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ fn should_report_offline_validators() {
|
||||
advance_session();
|
||||
// enact the change and buffer another one
|
||||
let validators = vec![1, 2, 3, 4, 5, 6];
|
||||
VALIDATORS.with(|l| *l.borrow_mut() = Some(validators.clone()));
|
||||
Validators::mutate(|l| *l = Some(validators.clone()));
|
||||
advance_session();
|
||||
|
||||
// when
|
||||
@@ -76,7 +76,7 @@ fn should_report_offline_validators() {
|
||||
advance_session();
|
||||
|
||||
// then
|
||||
let offences = OFFENCES.with(|l| l.replace(vec![]));
|
||||
let offences = Offences::take();
|
||||
assert_eq!(
|
||||
offences,
|
||||
vec![(
|
||||
@@ -96,7 +96,7 @@ fn should_report_offline_validators() {
|
||||
advance_session();
|
||||
|
||||
// then
|
||||
let offences = OFFENCES.with(|l| l.replace(vec![]));
|
||||
let offences = Offences::take();
|
||||
assert_eq!(
|
||||
offences,
|
||||
vec![(
|
||||
@@ -149,7 +149,7 @@ fn should_mark_online_validator_when_heartbeat_is_received() {
|
||||
new_test_ext().execute_with(|| {
|
||||
advance_session();
|
||||
// given
|
||||
VALIDATORS.with(|l| *l.borrow_mut() = Some(vec![1, 2, 3, 4, 5, 6]));
|
||||
Validators::mutate(|l| *l = Some(vec![1, 2, 3, 4, 5, 6]));
|
||||
assert_eq!(Session::validators(), Vec::<u64>::new());
|
||||
// enact the change and buffer another one
|
||||
advance_session();
|
||||
@@ -184,7 +184,7 @@ fn late_heartbeat_and_invalid_keys_len_should_fail() {
|
||||
new_test_ext().execute_with(|| {
|
||||
advance_session();
|
||||
// given
|
||||
VALIDATORS.with(|l| *l.borrow_mut() = Some(vec![1, 2, 3, 4, 5, 6]));
|
||||
Validators::mutate(|l| *l = Some(vec![1, 2, 3, 4, 5, 6]));
|
||||
assert_eq!(Session::validators(), Vec::<u64>::new());
|
||||
// enact the change and buffer another one
|
||||
advance_session();
|
||||
@@ -226,7 +226,7 @@ fn should_generate_heartbeats() {
|
||||
// buffer new validators
|
||||
Session::rotate_session();
|
||||
// enact the change and buffer another one
|
||||
VALIDATORS.with(|l| *l.borrow_mut() = Some(vec![1, 2, 3, 4, 5, 6]));
|
||||
Validators::mutate(|l| *l = Some(vec![1, 2, 3, 4, 5, 6]));
|
||||
Session::rotate_session();
|
||||
|
||||
// when
|
||||
@@ -262,7 +262,7 @@ fn should_cleanup_received_heartbeats_on_session_end() {
|
||||
new_test_ext().execute_with(|| {
|
||||
advance_session();
|
||||
|
||||
VALIDATORS.with(|l| *l.borrow_mut() = Some(vec![1, 2, 3]));
|
||||
Validators::mutate(|l| *l = Some(vec![1, 2, 3]));
|
||||
assert_eq!(Session::validators(), Vec::<u64>::new());
|
||||
|
||||
// enact the change and buffer another one
|
||||
@@ -293,7 +293,7 @@ fn should_mark_online_validator_when_block_is_authored() {
|
||||
new_test_ext().execute_with(|| {
|
||||
advance_session();
|
||||
// given
|
||||
VALIDATORS.with(|l| *l.borrow_mut() = Some(vec![1, 2, 3, 4, 5, 6]));
|
||||
Validators::mutate(|l| *l = Some(vec![1, 2, 3, 4, 5, 6]));
|
||||
assert_eq!(Session::validators(), Vec::<u64>::new());
|
||||
// enact the change and buffer another one
|
||||
advance_session();
|
||||
@@ -330,7 +330,7 @@ fn should_not_send_a_report_if_already_online() {
|
||||
ext.execute_with(|| {
|
||||
advance_session();
|
||||
// given
|
||||
VALIDATORS.with(|l| *l.borrow_mut() = Some(vec![1, 2, 3, 4, 5, 6]));
|
||||
Validators::mutate(|l| *l = Some(vec![1, 2, 3, 4, 5, 6]));
|
||||
assert_eq!(Session::validators(), Vec::<u64>::new());
|
||||
// enact the change and buffer another one
|
||||
advance_session();
|
||||
@@ -393,12 +393,12 @@ fn should_handle_missing_progress_estimates() {
|
||||
Session::rotate_session();
|
||||
|
||||
// enact the change and buffer another one
|
||||
VALIDATORS.with(|l| *l.borrow_mut() = Some(vec![0, 1, 2]));
|
||||
Validators::mutate(|l| *l = Some(vec![0, 1, 2]));
|
||||
Session::rotate_session();
|
||||
|
||||
// we will return `None` on the next call to `estimate_current_session_progress`
|
||||
// and the offchain worker should fallback to checking `HeartbeatAfter`
|
||||
MOCK_CURRENT_SESSION_PROGRESS.with(|p| *p.borrow_mut() = Some(None));
|
||||
MockCurrentSessionProgress::mutate(|p| *p = Some(None));
|
||||
ImOnline::offchain_worker(block);
|
||||
|
||||
assert_eq!(state.read().transactions.len(), 3);
|
||||
@@ -427,26 +427,25 @@ fn should_handle_non_linear_session_progress() {
|
||||
|
||||
// mock the session length as being 10 blocks long,
|
||||
// enact the change and buffer another one
|
||||
VALIDATORS.with(|l| *l.borrow_mut() = Some(vec![0, 1, 2]));
|
||||
Validators::mutate(|l| *l = Some(vec![0, 1, 2]));
|
||||
|
||||
// mock the session length has being 10 which should make us assume the fallback for half
|
||||
// session will be reached by block 5.
|
||||
MOCK_AVERAGE_SESSION_LENGTH.with(|p| *p.borrow_mut() = Some(10));
|
||||
MockAverageSessionLength::mutate(|p| *p = Some(10));
|
||||
|
||||
Session::rotate_session();
|
||||
|
||||
// if we don't have valid results for the current session progres then
|
||||
// we'll fallback to `HeartbeatAfter` and only heartbeat on block 5.
|
||||
MOCK_CURRENT_SESSION_PROGRESS.with(|p| *p.borrow_mut() = Some(None));
|
||||
MockCurrentSessionProgress::mutate(|p| *p = Some(None));
|
||||
assert_eq!(ImOnline::send_heartbeats(2).err(), Some(OffchainErr::TooEarly));
|
||||
|
||||
MOCK_CURRENT_SESSION_PROGRESS.with(|p| *p.borrow_mut() = Some(None));
|
||||
MockCurrentSessionProgress::mutate(|p| *p = Some(None));
|
||||
assert!(ImOnline::send_heartbeats(5).ok().is_some());
|
||||
|
||||
// if we have a valid current session progress then we'll heartbeat as soon
|
||||
// as we're past 80% of the session regardless of the block number
|
||||
MOCK_CURRENT_SESSION_PROGRESS
|
||||
.with(|p| *p.borrow_mut() = Some(Some(Permill::from_percent(81))));
|
||||
MockCurrentSessionProgress::mutate(|p| *p = Some(Some(Permill::from_percent(81))));
|
||||
|
||||
assert!(ImOnline::send_heartbeats(2).ok().is_some());
|
||||
});
|
||||
@@ -464,8 +463,7 @@ fn test_does_not_heartbeat_early_in_the_session() {
|
||||
ext.execute_with(|| {
|
||||
// mock current session progress as being 5%. we only randomly start
|
||||
// heartbeating after 10% of the session has elapsed.
|
||||
MOCK_CURRENT_SESSION_PROGRESS
|
||||
.with(|p| *p.borrow_mut() = Some(Some(Permill::from_float(0.05))));
|
||||
MockCurrentSessionProgress::mutate(|p| *p = Some(Some(Permill::from_float(0.05))));
|
||||
assert_eq!(ImOnline::send_heartbeats(2).err(), Some(OffchainErr::TooEarly));
|
||||
});
|
||||
}
|
||||
@@ -483,9 +481,8 @@ fn test_probability_of_heartbeating_increases_with_session_progress() {
|
||||
let set_test = |progress, random: f64| {
|
||||
// the average session length is 100 blocks, therefore the residual
|
||||
// probability of sending a heartbeat is 1%
|
||||
MOCK_AVERAGE_SESSION_LENGTH.with(|p| *p.borrow_mut() = Some(100));
|
||||
MOCK_CURRENT_SESSION_PROGRESS
|
||||
.with(|p| *p.borrow_mut() = Some(Some(Permill::from_float(progress))));
|
||||
MockAverageSessionLength::mutate(|p| *p = Some(100));
|
||||
MockCurrentSessionProgress::mutate(|p| *p = Some(Some(Permill::from_float(progress))));
|
||||
|
||||
let mut seed = [0u8; 32];
|
||||
let encoded = ((random * Permill::ACCURACY as f64) as u32).encode();
|
||||
|
||||
Reference in New Issue
Block a user