mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 23:21:06 +00:00
Fix staking fuzzer + make it check in ci. (#5899)
This commit is contained in:
@@ -16,7 +16,7 @@ serde = { version = "1.0.101", optional = true }
|
||||
codec = { package = "parity-scale-codec", version = "1.3.0", default-features = false, features = ["derive"] }
|
||||
sp-std = { version = "2.0.0-dev", default-features = false, path = "../../primitives/std" }
|
||||
sp-phragmen = { version = "2.0.0-dev", default-features = false, path = "../../primitives/phragmen" }
|
||||
sp-io ={ path = "../../primitives/io", default-features = false , version = "2.0.0-dev"}
|
||||
sp-io ={ version = "2.0.0-dev", path = "../../primitives/io", default-features = false }
|
||||
sp-runtime = { version = "2.0.0-dev", default-features = false, path = "../../primitives/runtime" }
|
||||
sp-staking = { version = "2.0.0-dev", default-features = false, path = "../../primitives/staking" }
|
||||
frame-support = { version = "2.0.0-dev", default-features = false, path = "../support" }
|
||||
@@ -44,7 +44,7 @@ pallet-staking-reward-curve = { version = "2.0.0-dev", path = "../staking/rewar
|
||||
substrate-test-utils = { version = "2.0.0-dev", path = "../../test-utils" }
|
||||
frame-benchmarking = { version = "2.0.0-dev", path = "../benchmarking" }
|
||||
rand_chacha = { version = "0.2" }
|
||||
parking_lot = "0.10.0"
|
||||
parking_lot = "0.10.2"
|
||||
env_logger = "0.7.1"
|
||||
hex = "0.4"
|
||||
|
||||
|
||||
@@ -23,8 +23,9 @@ use pallet_staking::testing_utils::{
|
||||
USER, get_seq_phragmen_solution, get_weak_solution, setup_chain_stakers,
|
||||
set_validator_count, signed_account,
|
||||
};
|
||||
use frame_support::assert_ok;
|
||||
use frame_support::{assert_ok, storage::StorageValue};
|
||||
use sp_runtime::{traits::Dispatchable, DispatchError};
|
||||
use sp_core::offchain::{testing::TestOffchainExt, OffchainExt};
|
||||
|
||||
mod mock;
|
||||
|
||||
@@ -40,8 +41,19 @@ enum Mode {
|
||||
WeakerSubmission,
|
||||
}
|
||||
|
||||
pub fn new_test_ext() -> Result<sp_io::TestExternalities, std::string::String> {
|
||||
frame_system::GenesisConfig::default().build_storage::<mock::Test>().map(Into::into)
|
||||
pub fn new_test_ext(iterations: u32) -> sp_io::TestExternalities {
|
||||
let mut ext: sp_io::TestExternalities = frame_system::GenesisConfig::default().build_storage::<mock::Test>().map(Into::into)
|
||||
.expect("Failed to create test externalities.");
|
||||
|
||||
let (offchain, offchain_state) = TestOffchainExt::new();
|
||||
|
||||
let mut seed = [0u8; 32];
|
||||
seed[0..4].copy_from_slice(&iterations.to_le_bytes());
|
||||
offchain_state.write().seed = seed;
|
||||
|
||||
ext.register_extension(OffchainExt::new(offchain));
|
||||
|
||||
ext
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@@ -56,7 +68,7 @@ fn main() {
|
||||
loop {
|
||||
fuzz!(|data: (u32, u32, u32, u32, u32)| {
|
||||
let (mut num_validators, mut num_nominators, mut edge_per_voter, mut to_elect, mode_u32) = data;
|
||||
let ext = new_test_ext();
|
||||
let mut ext = new_test_ext(5);
|
||||
let mode: Mode = unsafe { std::mem::transmute(mode_u32) };
|
||||
num_validators = to_range(num_validators, 50, 1000);
|
||||
num_nominators = to_range(num_nominators, 50, 2000);
|
||||
@@ -73,7 +85,7 @@ fn main() {
|
||||
to_elect,
|
||||
);
|
||||
|
||||
ext.unwrap_or_default().execute_with(|| {
|
||||
ext.execute_with(|| {
|
||||
// initial setup
|
||||
set_validator_count::<Test>(to_elect);
|
||||
pallet_staking::testing_utils::init_active_era();
|
||||
@@ -82,6 +94,7 @@ fn main() {
|
||||
num_nominators,
|
||||
edge_per_voter,
|
||||
);
|
||||
<pallet_staking::EraElectionStatus<Test>>::put(pallet_staking::ElectionStatus::Open(1));
|
||||
|
||||
println!("++ Chain setup done.");
|
||||
|
||||
@@ -140,7 +153,7 @@ fn main() {
|
||||
Mode::WeakerSubmission => {
|
||||
assert_eq!(
|
||||
call.dispatch(caller.into()).unwrap_err().error,
|
||||
DispatchError::Module { index: 0, error: 15, message: Some("PhragmenWeakSubmission") },
|
||||
DispatchError::Module { index: 0, error: 16, message: Some("PhragmenWeakSubmission") },
|
||||
);
|
||||
},
|
||||
// NOTE: so exhaustive pattern doesn't work here.. maybe some rust issue? or due to `#[repr(u32)]`?
|
||||
|
||||
@@ -50,7 +50,7 @@ pub fn random(a: u32, b: u32) -> u32 {
|
||||
pub fn set_validator_count<T: Trait>(to_elect: u32) {
|
||||
ValidatorCount::put(to_elect);
|
||||
MinimumValidatorCount::put(to_elect / 2);
|
||||
<EraElectionStatus<T>>::put(ElectionStatus::Open(T::BlockNumber::from(1u32)));
|
||||
<EraElectionStatus<T>>::put(ElectionStatus::Closed);
|
||||
}
|
||||
|
||||
/// Build an account with the given index.
|
||||
|
||||
Reference in New Issue
Block a user