Skip to content

Commit aabb65a

Browse files
kianenigmagui1117drahnr
authored
Generic Normalize impl for arithmetic and npos-elections (#6374)
* add normalize * better api for normalize * Some grumbles * Update primitives/arithmetic/src/lib.rs Co-authored-by: Guillaume Thiolliere <[email protected]> * More great review grumbles * Way better doc for everything. * Some improvement * Update primitives/arithmetic/src/lib.rs Co-authored-by: Bernhard Schuster <[email protected]> Co-authored-by: Guillaume Thiolliere <[email protected]> Co-authored-by: Bernhard Schuster <[email protected]>
1 parent 46d90c4 commit aabb65a

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

fuzzer/src/submit_solution.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ enum Mode {
4444
}
4545

4646
pub fn new_test_ext(iterations: u32) -> sp_io::TestExternalities {
47-
let mut ext: sp_io::TestExternalities = frame_system::GenesisConfig::default().build_storage::<mock::Test>().map(Into::into)
47+
let mut ext: sp_io::TestExternalities = frame_system::GenesisConfig::default()
48+
.build_storage::<mock::Test>()
49+
.map(Into::into)
4850
.expect("Failed to create test externalities.");
4951

5052
let (offchain, offchain_state) = TestOffchainExt::new();
@@ -70,38 +72,41 @@ fn main() {
7072
loop {
7173
fuzz!(|data: (u32, u32, u32, u32, u32)| {
7274
let (mut num_validators, mut num_nominators, mut edge_per_voter, mut to_elect, mode_u32) = data;
75+
// always run with 5 iterations.
7376
let mut ext = new_test_ext(5);
7477
let mode: Mode = unsafe { std::mem::transmute(mode_u32) };
7578
num_validators = to_range(num_validators, 50, 1000);
7679
num_nominators = to_range(num_nominators, 50, 2000);
7780
edge_per_voter = to_range(edge_per_voter, 1, 16);
7881
to_elect = to_range(to_elect, 20, num_validators);
82+
7983
let do_reduce = true;
8084

81-
println!("+++ instance with params {} / {} / {} / {:?}({}) / {}",
85+
println!("+++ instance with params {} / {} / {} / {} / {:?}({})",
8286
num_nominators,
8387
num_validators,
8488
edge_per_voter,
89+
to_elect,
8590
mode,
8691
mode_u32,
87-
to_elect,
8892
);
8993

9094
ext.execute_with(|| {
9195
// initial setup
9296
init_active_era();
97+
9398
assert_ok!(create_validators_with_nominators_for_era::<Test>(
9499
num_validators,
95100
num_nominators,
96101
edge_per_voter as usize,
97102
true,
98103
None,
99104
));
105+
100106
<EraElectionStatus<Test>>::put(ElectionStatus::Open(1));
101107
assert!(<Staking<Test>>::create_stakers_snapshot().0);
102-
let origin = RawOrigin::Signed(create_funded_user::<Test>("fuzzer", 0, 100));
103108

104-
println!("++ Chain setup done.");
109+
let origin = RawOrigin::Signed(create_funded_user::<Test>("fuzzer", 0, 100));
105110

106111
// stuff to submit
107112
let (winners, compact, score, size) = match mode {
@@ -141,8 +146,6 @@ fn main() {
141146
}
142147
};
143148

144-
println!("++ Submission ready. Score = {:?}", score);
145-
146149
// must have chosen correct number of winners.
147150
assert_eq!(winners.len() as u32, <Staking<Test>>::validator_count());
148151

src/offchain_election.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ pub fn prepare_submission<T: Trait>(
203203
}
204204

205205
// Convert back to ratio assignment. This takes less space.
206-
let low_accuracy_assignment = sp_npos_elections::assignment_staked_to_ratio(staked);
206+
let low_accuracy_assignment = sp_npos_elections::assignment_staked_to_ratio_normalized(staked)
207+
.map_err(|e| OffchainElectionError::from(e))?;
207208

208209
// convert back to staked to compute the score in the receiver's accuracy. This can be done
209210
// nicer, for now we do it as such since this code is not time-critical. This ensure that the

src/testing_utils.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,8 @@ pub fn get_weak_solution<T: Trait>(
201201
};
202202

203203
// convert back to ratio assignment. This takes less space.
204-
let low_accuracy_assignment: Vec<Assignment<T::AccountId, OffchainAccuracy>> =
205-
staked_assignments
206-
.into_iter()
207-
.map(|sa| sa.into_assignment(true))
208-
.collect();
204+
let low_accuracy_assignment = assignment_staked_to_ratio_normalized(staked_assignments)
205+
.expect("Failed to normalize");
209206

210207
// re-calculate score based on what the chain will decode.
211208
let score = {

0 commit comments

Comments
 (0)