14
14
// You should have received a copy of the GNU Affero General Public License
15
15
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
16
17
- use std:: cmp:: Ordering ;
18
17
use std:: collections:: { BTreeMap , BinaryHeap } ;
19
18
20
- use rug:: { integer:: Order , Integer } ;
21
-
22
19
use super :: { PriorityMessage , SortitionRound } ;
23
- use crate :: consensus:: Priority ;
24
20
25
21
/// Storing Priorities
26
22
#[ derive( Default ) ]
27
- pub struct SortitionInfoCollector {
28
- priorities : BTreeMap < SortitionRound , BinaryHeap < SortitionInfo > > ,
29
- }
30
-
31
- #[ derive( Debug , Eq , Clone , PartialEq , RlpEncodable , RlpDecodable ) ]
32
- pub struct SortitionInfo {
33
- message : PriorityMessage ,
34
- signer_idx : usize ,
35
- }
36
-
37
- impl Ord for SortitionInfo {
38
- fn cmp ( & self , other : & Self ) -> Ordering {
39
- let self_as_int = Integer :: from_digits ( & self . message . priority ( ) , Order :: MsfBe ) ;
40
- let other_as_int = Integer :: from_digits ( & other. message . priority ( ) , Order :: MsfBe ) ;
41
-
42
- self_as_int. cmp ( & other_as_int)
43
- }
44
- }
45
-
46
- impl PartialOrd for SortitionInfo {
47
- fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
48
- Some ( self . cmp ( other) )
49
- }
50
- }
51
-
52
- impl SortitionInfo {
53
- pub fn from_message_and_idx ( message : & PriorityMessage , signer_idx : usize ) -> Self {
54
- Self {
55
- message : message. clone ( ) ,
56
- signer_idx,
57
- }
58
- }
59
-
60
- pub fn signer_idx ( & self ) -> usize {
61
- self . signer_idx
62
- }
63
-
64
- pub fn message ( & self ) -> & PriorityMessage {
65
- & self . message
66
- }
67
-
68
- pub fn priority ( & self ) -> Priority {
69
- self . message . priority ( )
70
- }
23
+ pub struct PriorityMessageCollector {
24
+ messages : BTreeMap < SortitionRound , BinaryHeap < PriorityMessage > > ,
71
25
}
72
26
73
- impl SortitionInfoCollector {
74
- pub fn insert ( & mut self , info : SortitionInfo , round : SortitionRound ) {
75
- self . priorities . entry ( round) . or_insert_with ( Default :: default) . push ( info) ;
27
+ impl PriorityMessageCollector {
28
+ pub fn insert ( & mut self , info : PriorityMessage , round : SortitionRound ) {
29
+ self . messages . entry ( round) . or_insert_with ( Default :: default) . push ( info) ;
76
30
}
77
31
78
- pub fn get_highest_priority ( & self , round : & SortitionRound ) -> Option < SortitionInfo > {
79
- self . priorities . get ( round) . and_then ( |heap| heap. peek ( ) ) . cloned ( )
32
+ pub fn get_highest_priority_message ( & self , round : & SortitionRound ) -> Option < PriorityMessage > {
33
+ self . messages . get ( round) . and_then ( |heap| heap. peek ( ) ) . cloned ( )
80
34
}
81
35
82
36
/// Throw away priorities older than the given round.
83
37
pub fn throw_away_old ( & mut self , round : & SortitionRound ) {
84
- let new_collector = self . priorities . split_off ( round) ;
38
+ let new_collector = self . messages . split_off ( round) ;
85
39
assert ! ( !new_collector. is_empty( ) ) ;
86
- self . priorities = new_collector;
40
+ self . messages = new_collector;
87
41
}
88
42
}
89
43
@@ -92,53 +46,52 @@ mod sortition_info_collector_tests {
92
46
use super :: * ;
93
47
use crate :: consensus:: { Priority , PriorityInfo , SeedInfo } ;
94
48
95
- fn create_sortition_info_with_priority_and_signer_idx ( priority : Priority , signer_idx : usize ) -> SortitionInfo {
96
- let message = PriorityMessage {
97
- seed : SeedInfo :: from_seed_and_proof ( vec ! [ 0x0 ] , vec ! [ 0x22 ] ) ,
49
+ fn create_message_with_priority_and_signer_idx ( priority : Priority , signer_idx : usize ) -> PriorityMessage {
50
+ PriorityMessage {
51
+ seed : SeedInfo :: from_fields ( signer_idx , vec ! [ 0x0 ] , vec ! [ 0x22 ] ) ,
98
52
priority : PriorityInfo :: create_from_members ( priority, 0 , vec ! [ ] , vec ! [ ] ) ,
99
- } ;
100
- SortitionInfo :: from_message_and_idx ( & message, signer_idx)
53
+ }
101
54
}
102
55
103
56
#[ test]
104
57
fn compare_sortition_info ( ) {
105
- let greater_priority_info_summary = create_sortition_info_with_priority_and_signer_idx ( 0xffu64 . into ( ) , 0 ) ;
106
- let less_priority_info_summary = create_sortition_info_with_priority_and_signer_idx ( 0x7fu64 . into ( ) , 1 ) ;
58
+ let greater_priority_info_summary = create_message_with_priority_and_signer_idx ( 0xffu64 . into ( ) , 0 ) ;
59
+ let less_priority_info_summary = create_message_with_priority_and_signer_idx ( 0x7fu64 . into ( ) , 1 ) ;
107
60
assert ! ( greater_priority_info_summary > less_priority_info_summary) ;
108
61
}
109
62
110
63
#[ test]
111
64
fn compare_sortition_info2 ( ) {
112
- let greater_priority_info_summary = create_sortition_info_with_priority_and_signer_idx ( 0x55555544u64 . into ( ) , 0 ) ;
113
- let less_priority_info_summary = create_sortition_info_with_priority_and_signer_idx ( 0x55555523u64 . into ( ) , 22 ) ;
65
+ let greater_priority_info_summary = create_message_with_priority_and_signer_idx ( 0x55555544u64 . into ( ) , 0 ) ;
66
+ let less_priority_info_summary = create_message_with_priority_and_signer_idx ( 0x55555523u64 . into ( ) , 22 ) ;
114
67
assert ! ( greater_priority_info_summary > less_priority_info_summary) ;
115
68
}
116
69
117
- fn add_fixed_priorities ( collector : & mut SortitionInfoCollector , round : SortitionRound ) {
70
+ fn add_fixed_priorities ( collector : & mut PriorityMessageCollector , round : SortitionRound ) {
118
71
[ 0x55u64 , 0xffu64 , 0x44u64 , 0xeeu64 ]
119
72
. into_iter ( )
120
73
. zip ( [ 1 , 2 , 3 , 4 ] . iter ( ) )
121
- . map ( |( priority, idx) | create_sortition_info_with_priority_and_signer_idx ( ( * priority) . into ( ) , * idx) )
74
+ . map ( |( priority, idx) | create_message_with_priority_and_signer_idx ( ( * priority) . into ( ) , * idx) )
122
75
. for_each ( |sortition_info| collector. insert ( sortition_info, round) ) ;
123
76
}
124
77
125
78
#[ test]
126
79
fn insert_and_get_highest ( ) {
127
- let mut collector: SortitionInfoCollector = Default :: default ( ) ;
80
+ let mut collector: PriorityMessageCollector = Default :: default ( ) ;
128
81
let round = SortitionRound {
129
82
height : 1 ,
130
83
view : 0 ,
131
84
} ;
132
85
add_fixed_priorities ( & mut collector, round) ;
133
86
assert_eq ! (
134
- collector. get_highest_priority ( & round) . unwrap( ) ,
135
- create_sortition_info_with_priority_and_signer_idx ( 0xffu64 . into( ) , 2 )
87
+ collector. get_highest_priority_message ( & round) . unwrap( ) ,
88
+ create_message_with_priority_and_signer_idx ( 0xffu64 . into( ) , 2 )
136
89
) ;
137
90
}
138
91
139
92
#[ test]
140
93
fn throw_away_old ( ) {
141
- let mut collector: SortitionInfoCollector = Default :: default ( ) ;
94
+ let mut collector: PriorityMessageCollector = Default :: default ( ) ;
142
95
let rounds = [ ( 1 , 0 ) , ( 3 , 1 ) , ( 5 , 2 ) , ( 100 , 7 ) , ( 0 , 8 ) ] . into_iter ( ) . map ( |( height, view) | SortitionRound {
143
96
height : * height,
144
97
view : * view,
@@ -152,9 +105,9 @@ mod sortition_info_collector_tests {
152
105
rounds
153
106
. clone ( )
154
107
. filter ( |round| round >= & target_round)
155
- . for_each ( |round_gte| assert ! ( collector. get_highest_priority ( & round_gte) . is_some( ) ) ) ;
108
+ . for_each ( |round_gte| assert ! ( collector. get_highest_priority_message ( & round_gte) . is_some( ) ) ) ;
156
109
rounds
157
110
. filter ( |round| round < & target_round)
158
- . for_each ( |round_lt| assert ! ( collector. get_highest_priority ( & round_lt) . is_none( ) ) )
111
+ . for_each ( |round_lt| assert ! ( collector. get_highest_priority_message ( & round_lt) . is_none( ) ) )
159
112
}
160
113
}
0 commit comments