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

Refactor BlockRequestHandler #8932

Merged
1 commit merged into from
May 31, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions client/network/src/block_request_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod rep {
use super::ReputationChange as Rep;

/// 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.
Expand All @@ -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
// protocol name and send block requests.
pub(crate) fn generate_protocol_name(protocol_id: &ProtocolId) -> String {
let mut s = String::new();
s.push_str("/");
s.push_str(protocol_id.as_ref());
s.push_str("/sync/2");
s
format!("/{}/sync/2", protocol_id.as_ref())
}

/// The key of [`BlockRequestHandler::seen_requests`].
Expand Down Expand Up @@ -192,15 +188,15 @@ impl<B: BlockT> BlockRequestHandler<B> {
support_multiple_justifications,
};

let mut reputation_changes = Vec::new();
let mut reputation_change = None;

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

if *requests > MAX_NUMBER_OF_SAME_REQUESTS_PER_PEER {
reputation_changes.push(rep::SAME_REQUEST);
reputation_change = Some(rep::SAME_REQUEST);
}
},
None => {
Expand All @@ -219,7 +215,7 @@ impl<B: BlockT> BlockRequestHandler<B> {
attributes,
);

let result = if reputation_changes.is_empty() {
let result = if reputation_change.is_none() {
let block_response = self.get_block_response(
attributes,
from_block_id,
Expand All @@ -228,7 +224,7 @@ impl<B: BlockT> BlockRequestHandler<B> {
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
.blocks
.iter()
Expand All @@ -253,7 +249,7 @@ impl<B: BlockT> BlockRequestHandler<B> {

pending_response.send(OutgoingResponse {
result,
reputation_changes,
reputation_changes: reputation_change.into_iter().collect(),
sent_feedback: None,
}).map_err(|_| HandleRequestError::SendResponse)
}
Expand Down