Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit ce10c87

Browse files
committed
Refactor code a little bit
1 parent c2ec5bc commit ce10c87

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

client/network/src/block_request_handler.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ mod rep {
4646
use super::ReputationChange as Rep;
4747

4848
/// Reputation change when a peer sent us the same request multiple times.
49-
pub const SAME_REQUEST: Rep = Rep::new(i32::min_value(), "Same block request multiple times");
49+
pub const SAME_REQUEST: Rep = Rep::new_fatal("Same block request multiple times");
5050
}
5151

5252
/// Generates a [`ProtocolConfig`] for the block request protocol, refusing incoming requests.
@@ -65,11 +65,7 @@ pub fn generate_protocol_config(protocol_id: &ProtocolId) -> ProtocolConfig {
6565
// Visibility `pub(crate)` to allow `crate::light_client_requests::sender` to generate block request
6666
// protocol name and send block requests.
6767
pub(crate) fn generate_protocol_name(protocol_id: &ProtocolId) -> String {
68-
let mut s = String::new();
69-
s.push_str("/");
70-
s.push_str(protocol_id.as_ref());
71-
s.push_str("/sync/2");
72-
s
68+
format!("/{}/sync/2", protocol_id.as_ref())
7369
}
7470

7571
/// The key of [`BlockRequestHandler::seen_requests`].
@@ -192,15 +188,15 @@ impl<B: BlockT> BlockRequestHandler<B> {
192188
support_multiple_justifications,
193189
};
194190

195-
let mut reputation_changes = Vec::new();
191+
let mut reputation_change = None;
196192

197193
match self.seen_requests.get_mut(&key) {
198194
Some(SeenRequestsValue::First) => {},
199195
Some(SeenRequestsValue::Fulfilled(ref mut requests)) => {
200196
*requests = requests.saturating_add(1);
201197

202198
if *requests > MAX_NUMBER_OF_SAME_REQUESTS_PER_PEER {
203-
reputation_changes.push(rep::SAME_REQUEST);
199+
reputation_change = Some(rep::SAME_REQUEST);
204200
}
205201
},
206202
None => {
@@ -219,7 +215,7 @@ impl<B: BlockT> BlockRequestHandler<B> {
219215
attributes,
220216
);
221217

222-
let result = if reputation_changes.is_empty() {
218+
let result = if reputation_change.is_none() {
223219
let block_response = self.get_block_response(
224220
attributes,
225221
from_block_id,
@@ -228,7 +224,7 @@ impl<B: BlockT> BlockRequestHandler<B> {
228224
support_multiple_justifications,
229225
)?;
230226

231-
// If any of the blocks contains nay data, we can consider it as successful request.
227+
// If any of the blocks contains any data, we can consider it as successful request.
232228
if block_response
233229
.blocks
234230
.iter()
@@ -253,7 +249,7 @@ impl<B: BlockT> BlockRequestHandler<B> {
253249

254250
pending_response.send(OutgoingResponse {
255251
result,
256-
reputation_changes,
252+
reputation_changes: reputation_change.into_iter().collect(),
257253
sent_feedback: None,
258254
}).map_err(|_| HandleRequestError::SendResponse)
259255
}

0 commit comments

Comments
 (0)