1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
use crate::{
node::{Node, NodeId, NodeRef, NodeRole},
ExtendedBalance, IdentifierT, StakedAssignment,
};
use sp_arithmetic::traits::{Bounded, Zero};
use sp_std::{
collections::btree_map::{BTreeMap, Entry::*},
prelude::*,
vec,
};
type Map<A> = BTreeMap<(A, A), A>;
fn combinations_2<T: Clone>(input: &[T]) -> Vec<(T, T)> {
let n = input.len();
if n < 2 {
return Default::default()
}
let mut comb = Vec::with_capacity(n * (n - 1) / 2);
for i in 0..n {
for j in i + 1..n {
comb.push((input[i].clone(), input[j].clone()))
}
}
comb
}
pub(crate) fn trailing_common<T: Eq>(t1: &[T], t2: &[T]) -> usize {
t1.iter().rev().zip(t2.iter().rev()).take_while(|e| e.0 == e.1).count()
}
fn merge<A: IdentifierT>(voter_root_path: Vec<NodeRef<A>>, target_root_path: Vec<NodeRef<A>>) {
let (shorter_path, longer_path) = if voter_root_path.len() <= target_root_path.len() {
(voter_root_path, target_root_path)
} else {
(target_root_path, voter_root_path)
};
shorter_path
.iter()
.zip(shorter_path.iter().skip(1))
.for_each(|(voter, next)| Node::set_parent_of(&next, &voter));
Node::set_parent_of(&shorter_path[0], &longer_path[0]);
}
fn reduce_4<A: IdentifierT>(assignments: &mut Vec<StakedAssignment<A>>) -> u32 {
let mut combination_map: Map<A> = Map::new();
let mut num_changed: u32 = Zero::zero();
for assignment_index in 0..assignments.len() {
let who = assignments[assignment_index].who.clone();
let distribution_ids = &assignments[assignment_index]
.distribution
.iter()
.map(|(t, _p)| t.clone())
.collect::<Vec<A>>();
let candidate_combinations = combinations_2(distribution_ids);
for (v1, v2) in candidate_combinations {
match combination_map.entry((v1.clone(), v2.clone())) {
Vacant(entry) => {
entry.insert(who.clone());
},
Occupied(mut entry) => {
let other_who = entry.get_mut();
if assignments[assignment_index]
.distribution
.iter()
.filter(|(t, _)| *t == v1 || *t == v2)
.count() != 2
{
continue
}
let maybe_other_assignments = assignments.iter().find(|a| a.who == *other_who);
if maybe_other_assignments.is_none() {
continue
}
let other_assignment =
maybe_other_assignments.expect("value is checked to be 'Some'");
let mut other_cycle_votes =
other_assignment
.distribution
.iter()
.filter_map(|(t, w)| {
if *t == v1 || *t == v2 {
Some((t.clone(), *w))
} else {
None
}
})
.collect::<Vec<(A, ExtendedBalance)>>();
let other_votes_count = other_cycle_votes.len();
debug_assert!(other_votes_count <= 2);
if other_votes_count < 2 {
*other_who = who.clone();
continue
} else if other_votes_count == 2 {
let mut who_cycle_votes: Vec<(A, ExtendedBalance)> = Vec::with_capacity(2);
assignments[assignment_index].distribution.iter().for_each(|(t, w)| {
if *t == v1 || *t == v2 {
who_cycle_votes.push((t.clone(), *w));
}
});
if who_cycle_votes.len() != 2 {
continue
}
if other_cycle_votes[0].0 != who_cycle_votes[0].0 {
other_cycle_votes.swap(0, 1);
}
let mut min_value: ExtendedBalance = Bounded::max_value();
let mut min_index: usize = 0;
let cycle = who_cycle_votes
.iter()
.chain(other_cycle_votes.iter())
.enumerate()
.map(|(index, (t, w))| {
if *w <= min_value {
min_value = *w;
min_index = index;
}
(t.clone(), *w)
})
.collect::<Vec<(A, ExtendedBalance)>>();
let mut increase_indices: Vec<usize> = Vec::new();
let mut decrease_indices: Vec<usize> = Vec::new();
decrease_indices.push(min_index);
if min_index < 2 {
let sibling_index = 1 - min_index;
increase_indices.push(sibling_index);
decrease_indices.push(sibling_index + 2);
increase_indices.push(min_index + 2);
} else {
let sibling_index = 3 - min_index % 2;
increase_indices.push(sibling_index);
decrease_indices.push(sibling_index - 2);
increase_indices.push(min_index - 2);
}
let mut remove_indices: Vec<usize> = Vec::with_capacity(1);
increase_indices.into_iter().for_each(|i| {
let voter = if i < 2 { who.clone() } else { other_who.clone() };
assignments.iter_mut().filter(|a| a.who == voter).for_each(|ass| {
ass.distribution
.iter_mut()
.position(|(t, _)| *t == cycle[i].0)
.map(|idx| {
let next_value =
ass.distribution[idx].1.saturating_add(min_value);
ass.distribution[idx].1 = next_value;
});
});
});
decrease_indices.into_iter().for_each(|i| {
let voter = if i < 2 { who.clone() } else { other_who.clone() };
assignments.iter_mut().filter(|a| a.who == voter).for_each(|ass| {
ass.distribution
.iter_mut()
.position(|(t, _)| *t == cycle[i].0)
.map(|idx| {
let next_value =
ass.distribution[idx].1.saturating_sub(min_value);
if next_value.is_zero() {
ass.distribution.remove(idx);
remove_indices.push(i);
num_changed += 1;
} else {
ass.distribution[idx].1 = next_value;
}
});
});
});
let who_removed = remove_indices.iter().find(|i| **i < 2usize).is_some();
let other_removed =
remove_indices.into_iter().find(|i| *i >= 2usize).is_some();
match (who_removed, other_removed) {
(false, true) => {
*other_who = who.clone();
},
(true, false) => {
},
(true, true) => {
entry.remove();
},
(false, false) => {
panic!("Duplicate voter (or other corrupt input).");
},
}
}
},
}
}
}
num_changed
}
fn reduce_all<A: IdentifierT>(assignments: &mut Vec<StakedAssignment<A>>) -> u32 {
let mut num_changed: u32 = Zero::zero();
let mut tree: BTreeMap<NodeId<A>, NodeRef<A>> = BTreeMap::new();
for assignment_index in 0..assignments.len() {
let voter = assignments[assignment_index].who.clone();
let mut dist_index = 0;
loop {
let maybe_dist = assignments[assignment_index].distribution.get(dist_index);
if maybe_dist.is_none() {
break
}
let (target, _) = maybe_dist.expect("Value checked to be some").clone();
let voter_id = NodeId::from(voter.clone(), NodeRole::Voter);
let target_id = NodeId::from(target.clone(), NodeRole::Target);
let voter_exists = tree.contains_key(&voter_id);
let target_exists = tree.contains_key(&target_id);
let voter_node = tree
.entry(voter_id.clone())
.or_insert_with(|| Node::new(voter_id).into_ref())
.clone();
let target_node = tree
.entry(target_id.clone())
.or_insert_with(|| Node::new(target_id).into_ref())
.clone();
match (voter_exists, target_exists) {
(false, false) => {
Node::set_parent_of(&target_node, &voter_node);
dist_index += 1;
continue
},
(false, true) => {
Node::set_parent_of(&voter_node, &target_node);
dist_index += 1;
continue
},
(true, false) => {
Node::set_parent_of(&target_node, &voter_node);
dist_index += 1;
continue
},
(true, true) => { },
};
let (voter_root, voter_root_path) = Node::root(&voter_node);
let (target_root, target_root_path) = Node::root(&target_node);
if voter_root != target_root {
merge(voter_root_path, target_root_path);
dist_index += 1;
} else {
let common_count = trailing_common(&voter_root_path, &target_root_path);
#[cfg(feature = "std")]
debug_assert_eq!(target_root_path.last().unwrap(), voter_root_path.last().unwrap());
debug_assert!(common_count > 0);
let cycle = target_root_path
.iter()
.take(target_root_path.len() - common_count + 1)
.cloned()
.chain(
voter_root_path
.iter()
.take(voter_root_path.len() - common_count)
.rev()
.cloned(),
)
.collect::<Vec<NodeRef<A>>>();
#[cfg(feature = "std")]
debug_assert_eq!(cycle.len() % 2, 0);
let mut min_value: ExtendedBalance = Bounded::max_value();
let mut min_target: A = Default::default();
let mut min_voter: A = Default::default();
let mut min_index = 0usize;
let mut min_direction = 0u32;
let next_index = |i| {
if i < (cycle.len() - 1) {
i + 1
} else {
0
}
};
let prev_index = |i| {
if i > 0 {
i - 1
} else {
cycle.len() - 1
}
};
for i in 0..cycle.len() {
if cycle[i].borrow().id.role == NodeRole::Voter {
let current = cycle[i].borrow().id.who.clone();
let next = cycle[next_index(i)].borrow().id.who.clone();
let prev = cycle[prev_index(i)].borrow().id.who.clone();
assignments.iter().find(|a| a.who == current).map(|ass| {
ass.distribution.iter().find(|d| d.0 == next).map(|(_, w)| {
if *w < min_value {
min_value = *w;
min_target = next.clone();
min_voter = current.clone();
min_index = i;
min_direction = 1;
}
})
});
assignments.iter().find(|a| a.who == current).map(|ass| {
ass.distribution.iter().find(|d| d.0 == prev).map(|(_, w)| {
if *w < min_value {
min_value = *w;
min_target = prev.clone();
min_voter = current.clone();
min_index = i;
min_direction = 0;
}
})
});
}
}
let target_chunk = target_root_path.len() - common_count;
let min_chain_in_voter = (min_index + min_direction as usize) > target_chunk;
let mut should_inc_counter = true;
let start_operation_add = ((min_index % 2) + min_direction as usize) % 2 == 1;
let mut additional_removed = Vec::new();
for i in 0..cycle.len() {
let current = cycle[i].borrow();
if current.id.role == NodeRole::Voter {
let prev = cycle[prev_index(i)].borrow();
assignments
.iter_mut()
.enumerate()
.filter(|(_, a)| a.who == current.id.who)
.for_each(|(target_ass_index, ass)| {
ass.distribution
.iter_mut()
.position(|(t, _)| *t == prev.id.who)
.map(|idx| {
let next_value = if i % 2 == 0 {
if start_operation_add {
ass.distribution[idx].1.saturating_add(min_value)
} else {
ass.distribution[idx].1.saturating_sub(min_value)
}
} else {
if start_operation_add {
ass.distribution[idx].1.saturating_sub(min_value)
} else {
ass.distribution[idx].1.saturating_add(min_value)
}
};
if next_value.is_zero() {
if target_ass_index == assignment_index {
should_inc_counter = false
}
ass.distribution.remove(idx);
num_changed += 1;
if !(i == min_index && min_direction == 0) {
additional_removed.push((
cycle[i].clone(),
cycle[prev_index(i)].clone(),
));
}
} else {
ass.distribution[idx].1 = next_value;
}
});
});
let next = cycle[next_index(i)].borrow();
assignments
.iter_mut()
.enumerate()
.filter(|(_, a)| a.who == current.id.who)
.for_each(|(target_ass_index, ass)| {
ass.distribution
.iter_mut()
.position(|(t, _)| *t == next.id.who)
.map(|idx| {
let next_value = if i % 2 == 0 {
if start_operation_add {
ass.distribution[idx].1.saturating_sub(min_value)
} else {
ass.distribution[idx].1.saturating_add(min_value)
}
} else {
if start_operation_add {
ass.distribution[idx].1.saturating_add(min_value)
} else {
ass.distribution[idx].1.saturating_sub(min_value)
}
};
if next_value.is_zero() {
if target_ass_index == assignment_index {
should_inc_counter = false
}
ass.distribution.remove(idx);
num_changed += 1;
if !(i == min_index && min_direction == 1) {
additional_removed.push((
cycle[i].clone(),
cycle[next_index(i)].clone(),
));
}
} else {
ass.distribution[idx].1 = next_value;
}
});
});
}
}
let should_reorg = !(min_index == (cycle.len() - 1) && min_direction == 1);
if should_reorg {
let min_edge = vec![min_voter, min_target];
if min_chain_in_voter {
for i in 0..voter_root_path.len() - 1 {
let current = voter_root_path[i].clone().borrow().id.who.clone();
let next = voter_root_path[i + 1].clone().borrow().id.who.clone();
if min_edge.contains(¤t) && min_edge.contains(&next) {
break
}
Node::set_parent_of(&voter_root_path[i + 1], &voter_root_path[i]);
}
Node::set_parent_of(&voter_node, &target_node);
} else {
for i in 0..target_root_path.len() - 1 {
let current = target_root_path[i].clone().borrow().id.who.clone();
let next = target_root_path[i + 1].clone().borrow().id.who.clone();
if min_edge.contains(¤t) && min_edge.contains(&next) {
break
}
Node::set_parent_of(&target_root_path[i + 1], &target_root_path[i]);
}
Node::set_parent_of(&target_node, &voter_node);
}
}
for (r1, r2) in additional_removed {
if Node::is_parent_of(&r1, &r2) {
Node::remove_parent(&r1);
} else if Node::is_parent_of(&r2, &r1) {
Node::remove_parent(&r2);
}
}
if should_inc_counter {
dist_index += 1;
}
}
}
}
num_changed
}
pub fn reduce<A: IdentifierT>(assignments: &mut Vec<StakedAssignment<A>>) -> u32 where {
let mut num_changed = reduce_4(assignments);
num_changed += reduce_all(assignments);
num_changed
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn merging_works() {
let d = Node::new(NodeId::from(1, NodeRole::Target)).into_ref();
let a = Node::new(NodeId::from(2, NodeRole::Target)).into_ref();
let b = Node::new(NodeId::from(3, NodeRole::Target)).into_ref();
let c = Node::new(NodeId::from(4, NodeRole::Target)).into_ref();
let e = Node::new(NodeId::from(5, NodeRole::Target)).into_ref();
let f = Node::new(NodeId::from(6, NodeRole::Target)).into_ref();
Node::set_parent_of(&c, &b);
Node::set_parent_of(&b, &a);
Node::set_parent_of(&a, &d);
Node::set_parent_of(&e, &f);
let path1 = vec![c.clone(), b.clone(), a.clone(), d.clone()];
let path2 = vec![e.clone(), f.clone()];
merge(path1, path2);
assert_eq!(e.borrow().clone().parent.unwrap().borrow().id.who, 4u32);
}
#[test]
fn merge_with_len_one() {
let d = Node::new(NodeId::from(1, NodeRole::Target)).into_ref();
let a = Node::new(NodeId::from(2, NodeRole::Target)).into_ref();
let b = Node::new(NodeId::from(3, NodeRole::Target)).into_ref();
let c = Node::new(NodeId::from(4, NodeRole::Target)).into_ref();
let f = Node::new(NodeId::from(6, NodeRole::Target)).into_ref();
Node::set_parent_of(&c, &b);
Node::set_parent_of(&b, &a);
Node::set_parent_of(&a, &d);
let path1 = vec![c.clone(), b.clone(), a.clone(), d.clone()];
let path2 = vec![f.clone()];
merge(path1, path2);
assert_eq!(f.borrow().clone().parent.unwrap().borrow().id.who, 4u32);
}
#[test]
fn basic_reduce_4_cycle_works() {
use super::*;
let assignments = vec![
StakedAssignment { who: 1, distribution: vec![(10, 25), (20, 75)] },
StakedAssignment { who: 2, distribution: vec![(10, 50), (20, 50)] },
];
let mut new_assignments = assignments.clone();
let num_reduced = reduce_4(&mut new_assignments);
assert_eq!(num_reduced, 1);
assert_eq!(
new_assignments,
vec![
StakedAssignment { who: 1, distribution: vec![(20, 100),] },
StakedAssignment { who: 2, distribution: vec![(10, 75), (20, 25),] },
],
);
}
#[test]
fn basic_reduce_all_cycles_works() {
let mut assignments = vec![
StakedAssignment { who: 1, distribution: vec![(10, 10)] },
StakedAssignment { who: 2, distribution: vec![(10, 15), (20, 5)] },
StakedAssignment { who: 3, distribution: vec![(20, 15), (40, 15)] },
StakedAssignment { who: 4, distribution: vec![(20, 10), (30, 10), (40, 20)] },
StakedAssignment { who: 5, distribution: vec![(20, 20), (30, 10), (40, 20)] },
];
assert_eq!(3, reduce_all(&mut assignments));
assert_eq!(
assignments,
vec![
StakedAssignment { who: 1, distribution: vec![(10, 10),] },
StakedAssignment { who: 2, distribution: vec![(10, 15), (20, 5),] },
StakedAssignment { who: 3, distribution: vec![(20, 30),] },
StakedAssignment { who: 4, distribution: vec![(40, 40),] },
StakedAssignment { who: 5, distribution: vec![(20, 15), (30, 20), (40, 15),] },
],
)
}
#[test]
fn basic_reduce_works() {
let mut assignments = vec![
StakedAssignment { who: 1, distribution: vec![(10, 10)] },
StakedAssignment { who: 2, distribution: vec![(10, 15), (20, 5)] },
StakedAssignment { who: 3, distribution: vec![(20, 15), (40, 15)] },
StakedAssignment { who: 4, distribution: vec![(20, 10), (30, 10), (40, 20)] },
StakedAssignment { who: 5, distribution: vec![(20, 20), (30, 10), (40, 20)] },
];
assert_eq!(3, reduce(&mut assignments));
assert_eq!(
assignments,
vec![
StakedAssignment { who: 1, distribution: vec![(10, 10),] },
StakedAssignment { who: 2, distribution: vec![(10, 15), (20, 5),] },
StakedAssignment { who: 3, distribution: vec![(20, 30),] },
StakedAssignment { who: 4, distribution: vec![(40, 40),] },
StakedAssignment { who: 5, distribution: vec![(20, 15), (30, 20), (40, 15),] },
],
)
}
#[test]
fn should_deal_with_self_vote() {
let mut assignments = vec![
StakedAssignment { who: 1, distribution: vec![(10, 10)] },
StakedAssignment { who: 2, distribution: vec![(10, 15), (20, 5)] },
StakedAssignment { who: 3, distribution: vec![(20, 15), (40, 15)] },
StakedAssignment { who: 4, distribution: vec![(20, 10), (30, 10), (40, 20)] },
StakedAssignment { who: 5, distribution: vec![(20, 20), (30, 10), (40, 20)] },
StakedAssignment { who: 10, distribution: vec![(10, 100)] },
StakedAssignment { who: 20, distribution: vec![(20, 200)] },
];
assert_eq!(3, reduce(&mut assignments));
assert_eq!(
assignments,
vec![
StakedAssignment { who: 1, distribution: vec![(10, 10),] },
StakedAssignment { who: 2, distribution: vec![(10, 15), (20, 5),] },
StakedAssignment { who: 3, distribution: vec![(20, 30),] },
StakedAssignment { who: 4, distribution: vec![(40, 40),] },
StakedAssignment { who: 5, distribution: vec![(20, 15), (30, 20), (40, 15),] },
StakedAssignment { who: 10, distribution: vec![(10, 100)] },
StakedAssignment { who: 20, distribution: vec![(20, 200)] },
],
)
}
#[test]
fn reduce_3_common_votes_same_weight() {
let mut assignments = vec![
StakedAssignment {
who: 4,
distribution: vec![(1000000, 100), (1000002, 100), (1000004, 100)],
},
StakedAssignment {
who: 5,
distribution: vec![(1000000, 100), (1000002, 100), (1000004, 100)],
},
];
reduce_4(&mut assignments);
assert_eq!(
assignments,
vec![
StakedAssignment { who: 4, distribution: vec![(1000000, 200,), (1000004, 100,),] },
StakedAssignment { who: 5, distribution: vec![(1000002, 200,), (1000004, 100,),] },
],
)
}
#[test]
#[should_panic]
fn reduce_panics_on_duplicate_voter() {
let mut assignments = vec![
StakedAssignment { who: 1, distribution: vec![(10, 10), (20, 10)] },
StakedAssignment { who: 1, distribution: vec![(10, 15), (20, 5)] },
StakedAssignment { who: 2, distribution: vec![(10, 15), (20, 15)] },
];
reduce(&mut assignments);
}
#[test]
fn should_deal_with_duplicates_target() {
let mut assignments = vec![
StakedAssignment { who: 1, distribution: vec![(10, 15), (20, 5)] },
StakedAssignment {
who: 2,
distribution: vec![
(10, 15),
(20, 15),
(10, 1),
(20, 1),
],
},
];
reduce(&mut assignments);
assert_eq!(
assignments,
vec![
StakedAssignment { who: 1, distribution: vec![(10, 20),] },
StakedAssignment {
who: 2,
distribution: vec![
(10, 10),
(20, 20),
(10, 1),
(20, 1),
],
},
],
)
}
#[test]
fn bound_should_be_kept() {
let mut assignments = vec![
StakedAssignment {
who: 1,
distribution: vec![(103, 72), (101, 53), (100, 83), (102, 38)],
},
StakedAssignment {
who: 2,
distribution: vec![(103, 18), (101, 36), (102, 54), (100, 94)],
},
StakedAssignment {
who: 3,
distribution: vec![(100, 96), (101, 35), (102, 52), (103, 69)],
},
StakedAssignment {
who: 4,
distribution: vec![(102, 34), (100, 47), (103, 91), (101, 73)],
},
];
let winners = vec![103, 101, 100, 102];
let n = 4;
let m = winners.len() as u32;
let num_reduced = reduce_all(&mut assignments);
assert!(16 - num_reduced <= n + m);
}
}