@@ -18,7 +18,6 @@ use std::collections::{BTreeMap, HashMap, HashSet};
18
18
use std:: iter:: Iterator ;
19
19
20
20
use ckey:: SchnorrSignature ;
21
- use parking_lot:: RwLock ;
22
21
use primitives:: H256 ;
23
22
use rlp:: { Encodable , RlpStream } ;
24
23
@@ -29,7 +28,7 @@ use crate::consensus::BitSet;
29
28
/// Storing all Proposals, Prevotes and Precommits.
30
29
#[ derive( Debug ) ]
31
30
pub struct VoteCollector {
32
- votes : RwLock < BTreeMap < VoteStep , StepCollector > > ,
31
+ votes : BTreeMap < VoteStep , StepCollector > ,
33
32
}
34
33
35
34
#[ derive( Debug , Default ) ]
@@ -113,29 +112,27 @@ impl Default for VoteCollector {
113
112
// Insert dummy entry to fulfill invariant: "only messages newer than the oldest are inserted".
114
113
collector. insert ( Default :: default ( ) , Default :: default ( ) ) ;
115
114
VoteCollector {
116
- votes : RwLock :: new ( collector) ,
115
+ votes : collector,
117
116
}
118
117
}
119
118
}
120
119
121
120
impl VoteCollector {
122
121
/// Insert vote if it is newer than the oldest one.
123
- pub fn vote ( & self , message : ConsensusMessage ) -> Option < DoubleVote > {
124
- self . votes . write ( ) . entry ( * message. round ( ) ) . or_insert_with ( Default :: default) . insert ( message)
122
+ pub fn vote ( & mut self , message : ConsensusMessage ) -> Option < DoubleVote > {
123
+ self . votes . entry ( * message. round ( ) ) . or_insert_with ( Default :: default) . insert ( message)
125
124
}
126
125
127
126
/// Checks if the message should be ignored.
128
127
pub fn is_old_or_known ( & self , message : & ConsensusMessage ) -> bool {
129
- let read_guard = self . votes . read ( ) ;
130
-
131
- let is_known = read_guard. get ( & message. round ( ) ) . map_or ( false , |c| c. messages . contains ( message) ) ;
128
+ let is_known = self . votes . get ( & message. round ( ) ) . map_or ( false , |c| c. messages . contains ( message) ) ;
132
129
if is_known {
133
130
cdebug ! ( ENGINE , "Known message: {:?}." , message) ;
134
131
return true
135
132
}
136
133
137
134
// The reason not using `message.round() <= oldest` is to allow precommit messages on Commit step.
138
- let is_old = read_guard . keys ( ) . next ( ) . map_or ( true , |oldest| message. round ( ) < oldest) ;
135
+ let is_old = self . votes . keys ( ) . next ( ) . map_or ( true , |oldest| message. round ( ) < oldest) ;
139
136
if is_old {
140
137
cdebug ! ( ENGINE , "Old message {:?}." , message) ;
141
138
return true
@@ -145,11 +142,10 @@ impl VoteCollector {
145
142
}
146
143
147
144
/// Throws out messages older than message, leaves message as marker for the oldest.
148
- pub fn throw_out_old ( & self , vote_round : & VoteStep ) {
149
- let mut guard = self . votes . write ( ) ;
150
- let new_collector = guard. split_off ( vote_round) ;
145
+ pub fn throw_out_old ( & mut self , vote_round : & VoteStep ) {
146
+ let new_collector = self . votes . split_off ( vote_round) ;
151
147
assert ! ( !new_collector. is_empty( ) ) ;
152
- * guard = new_collector;
148
+ self . votes = new_collector;
153
149
}
154
150
155
151
/// Collects the signatures and the indices for the given round and hash.
@@ -159,8 +155,7 @@ impl VoteCollector {
159
155
round : & VoteStep ,
160
156
block_hash : & H256 ,
161
157
) -> ( Vec < SchnorrSignature > , Vec < usize > ) {
162
- let guard = self . votes . read ( ) ;
163
- guard
158
+ self . votes
164
159
. get ( round)
165
160
. and_then ( |c| c. block_votes . get ( & Some ( * block_hash) ) )
166
161
. map ( |votes| {
@@ -173,24 +168,23 @@ impl VoteCollector {
173
168
174
169
/// Returns the first signature and the index of its signer for a given round and hash if exists.
175
170
pub fn round_signature ( & self , round : & VoteStep , block_hash : & H256 ) -> Option < SchnorrSignature > {
176
- let guard = self . votes . read ( ) ;
177
- guard
171
+ self . votes
178
172
. get ( round)
179
173
. and_then ( |c| c. block_votes . get ( & Some ( * block_hash) ) )
180
174
. and_then ( |votes| votes. values ( ) . next ( ) . cloned ( ) )
181
175
}
182
176
183
177
/// Count votes which agree with the given message.
184
178
pub fn aligned_votes ( & self , message : & ConsensusMessage ) -> BitSet {
185
- if let Some ( votes) = self . votes . read ( ) . get ( & message. round ( ) ) {
179
+ if let Some ( votes) = self . votes . get ( & message. round ( ) ) {
186
180
votes. count_block ( & message. block_hash ( ) )
187
181
} else {
188
182
Default :: default ( )
189
183
}
190
184
}
191
185
192
186
pub fn block_round_votes ( & self , round : & VoteStep , block_hash : & Option < H256 > ) -> BitSet {
193
- if let Some ( votes) = self . votes . read ( ) . get ( round) {
187
+ if let Some ( votes) = self . votes . get ( round) {
194
188
votes. count_block ( block_hash)
195
189
} else {
196
190
Default :: default ( )
@@ -199,30 +193,29 @@ impl VoteCollector {
199
193
200
194
/// Count all votes collected for a given round.
201
195
pub fn round_votes ( & self , vote_round : & VoteStep ) -> BitSet {
202
- if let Some ( votes) = self . votes . read ( ) . get ( vote_round) {
196
+ if let Some ( votes) = self . votes . get ( vote_round) {
203
197
votes. count ( )
204
198
} else {
205
199
Default :: default ( )
206
200
}
207
201
}
208
202
209
203
pub fn get_block_hashes ( & self , round : & VoteStep ) -> Vec < H256 > {
210
- let guard = self . votes . read ( ) ;
211
- guard. get ( round) . map ( |c| c. block_votes . keys ( ) . cloned ( ) . filter_map ( |x| x) . collect ( ) ) . unwrap_or_else ( Vec :: new)
204
+ self . votes
205
+ . get ( round)
206
+ . map ( |c| c. block_votes . keys ( ) . cloned ( ) . filter_map ( |x| x) . collect ( ) )
207
+ . unwrap_or_else ( Vec :: new)
212
208
}
213
209
214
210
pub fn get_all ( & self ) -> Vec < ConsensusMessage > {
215
- self . votes . read ( ) . iter ( ) . flat_map ( |( _round, collector) | collector. messages . iter ( ) ) . cloned ( ) . collect ( )
211
+ self . votes . iter ( ) . flat_map ( |( _round, collector) | collector. messages . iter ( ) ) . cloned ( ) . collect ( )
216
212
}
217
213
218
214
pub fn get_all_votes_in_round ( & self , round : & VoteStep ) -> Vec < ConsensusMessage > {
219
- let guard = self . votes . read ( ) ;
220
- let c = guard. get ( round) ;
221
- c. map ( |c| c. messages . iter ( ) . cloned ( ) . collect ( ) ) . unwrap_or_default ( )
215
+ self . votes . get ( round) . map ( |c| c. messages . iter ( ) . cloned ( ) . collect ( ) ) . unwrap_or_default ( )
222
216
}
223
217
224
218
pub fn get_all_votes_and_indices_in_round ( & self , round : & VoteStep ) -> Vec < ( usize , ConsensusMessage ) > {
225
- let guard = self . votes . read ( ) ;
226
- guard. get ( round) . map ( |c| c. voted . iter ( ) . map ( |( k, v) | ( * k, v. clone ( ) ) ) . collect ( ) ) . unwrap_or_default ( )
219
+ self . votes . get ( round) . map ( |c| c. voted . iter ( ) . map ( |( k, v) | ( * k, v. clone ( ) ) ) . collect ( ) ) . unwrap_or_default ( )
227
220
}
228
221
}
0 commit comments