Refactor code a little bit (#8932)

This commit is contained in:
Roman Proskuryakov
2021-05-31 16:05:50 +00:00
committed by GitHub
parent 6d13520da4
commit 587ea2549b
@@ -46,7 +46,7 @@ mod rep {
use super::ReputationChange as Rep; use super::ReputationChange as Rep;
/// Reputation change when a peer sent us the same request multiple times. /// Reputation change when a peer sent us the same request multiple times.
pub const SAME_REQUEST: Rep = Rep::new(i32::min_value(), "Same block request multiple times"); pub const SAME_REQUEST: Rep = Rep::new_fatal("Same block request multiple times");
} }
/// Generates a [`ProtocolConfig`] for the block request protocol, refusing incoming requests. /// Generates a [`ProtocolConfig`] for the block request protocol, refusing incoming requests.
@@ -65,11 +65,7 @@ pub fn generate_protocol_config(protocol_id: &ProtocolId) -> ProtocolConfig {
// Visibility `pub(crate)` to allow `crate::light_client_requests::sender` to generate block request // Visibility `pub(crate)` to allow `crate::light_client_requests::sender` to generate block request
// protocol name and send block requests. // protocol name and send block requests.
pub(crate) fn generate_protocol_name(protocol_id: &ProtocolId) -> String { pub(crate) fn generate_protocol_name(protocol_id: &ProtocolId) -> String {
let mut s = String::new(); format!("/{}/sync/2", protocol_id.as_ref())
s.push_str("/");
s.push_str(protocol_id.as_ref());
s.push_str("/sync/2");
s
} }
/// The key of [`BlockRequestHandler::seen_requests`]. /// The key of [`BlockRequestHandler::seen_requests`].
@@ -192,7 +188,7 @@ impl<B: BlockT> BlockRequestHandler<B> {
support_multiple_justifications, support_multiple_justifications,
}; };
let mut reputation_changes = Vec::new(); let mut reputation_change = None;
match self.seen_requests.get_mut(&key) { match self.seen_requests.get_mut(&key) {
Some(SeenRequestsValue::First) => {}, Some(SeenRequestsValue::First) => {},
@@ -200,7 +196,7 @@ impl<B: BlockT> BlockRequestHandler<B> {
*requests = requests.saturating_add(1); *requests = requests.saturating_add(1);
if *requests > MAX_NUMBER_OF_SAME_REQUESTS_PER_PEER { if *requests > MAX_NUMBER_OF_SAME_REQUESTS_PER_PEER {
reputation_changes.push(rep::SAME_REQUEST); reputation_change = Some(rep::SAME_REQUEST);
} }
}, },
None => { None => {
@@ -219,7 +215,7 @@ impl<B: BlockT> BlockRequestHandler<B> {
attributes, attributes,
); );
let result = if reputation_changes.is_empty() { let result = if reputation_change.is_none() {
let block_response = self.get_block_response( let block_response = self.get_block_response(
attributes, attributes,
from_block_id, from_block_id,
@@ -228,7 +224,7 @@ impl<B: BlockT> BlockRequestHandler<B> {
support_multiple_justifications, support_multiple_justifications,
)?; )?;
// If any of the blocks contains nay data, we can consider it as successful request. // If any of the blocks contains any data, we can consider it as successful request.
if block_response if block_response
.blocks .blocks
.iter() .iter()
@@ -253,7 +249,7 @@ impl<B: BlockT> BlockRequestHandler<B> {
pending_response.send(OutgoingResponse { pending_response.send(OutgoingResponse {
result, result,
reputation_changes, reputation_changes: reputation_change.into_iter().collect(),
sent_feedback: None, sent_feedback: None,
}).map_err(|_| HandleRequestError::SendResponse) }).map_err(|_| HandleRequestError::SendResponse)
} }