// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see .
//! Helper for handling (i.e. answering) block requests from a remote peer via the
//! `crate::request_responses::RequestResponsesBehaviour`.
use crate::{
block_relay_protocol::{BlockDownloader, BlockRelayParams, BlockResponseError, BlockServer},
schema::v1::{
block_request::FromBlock as FromBlockSchema, BlockRequest as BlockRequestSchema,
BlockResponse as BlockResponseSchema, BlockResponse, Direction,
},
service::network::NetworkServiceHandle,
LOG_TARGET,
};
use codec::{Decode, DecodeAll, Encode};
use futures::{channel::oneshot, stream::StreamExt};
use log::debug;
use prost::Message;
use schnellru::{ByLength, LruMap};
use sc_client_api::BlockBackend;
use sc_network::{
config::ProtocolId,
request_responses::{IfDisconnected, IncomingRequest, OutgoingResponse, RequestFailure},
service::traits::RequestResponseConfig,
types::ProtocolName,
NetworkBackend,
};
use sc_network_common::sync::message::{BlockAttributes, BlockData, BlockRequest, FromBlock};
use sc_network_types::PeerId;
use sp_blockchain::HeaderBackend;
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, Header, One, Zero},
};
use std::{
cmp::min,
hash::{Hash, Hasher},
sync::Arc,
time::Duration,
};
/// Maximum blocks per response.
pub(crate) const MAX_BLOCKS_IN_RESPONSE: usize = 128;
const MAX_BODY_BYTES: usize = 8 * 1024 * 1024;
const MAX_NUMBER_OF_SAME_REQUESTS_PER_PEER: usize = 2;
mod rep {
use sc_network::ReputationChange as Rep;
/// Reputation change when a peer sent us the same request multiple times.
pub const SAME_REQUEST: Rep = Rep::new_fatal("Same block request multiple times");
/// Reputation change when a peer sent us the same "small" request multiple times.
pub const SAME_SMALL_REQUEST: Rep =
Rep::new(-(1 << 10), "same small block request multiple times");
}
/// Generates a `RequestResponseProtocolConfig` for the block request protocol,
/// refusing incoming requests.
pub fn generate_protocol_config<
Hash: AsRef<[u8]>,
B: BlockT,
N: NetworkBackend::Hash>,
>(
protocol_id: &ProtocolId,
genesis_hash: Hash,
fork_id: Option<&str>,
inbound_queue: async_channel::Sender,
) -> N::RequestResponseProtocolConfig {
N::request_response_config(
generate_protocol_name(genesis_hash, fork_id).into(),
std::iter::once(generate_legacy_protocol_name(protocol_id).into()).collect(),
1024 * 1024,
16 * 1024 * 1024,
Duration::from_secs(20),
Some(inbound_queue),
)
}
/// Generate the block protocol name from the genesis hash and fork id.
fn generate_protocol_name>(genesis_hash: Hash, fork_id: Option<&str>) -> String {
let genesis_hash = genesis_hash.as_ref();
if let Some(fork_id) = fork_id {
format!("/{}/{}/sync/2", array_bytes::bytes2hex("", genesis_hash), fork_id)
} else {
format!("/{}/sync/2", array_bytes::bytes2hex("", genesis_hash))
}
}
/// Generate the legacy block protocol name from chain specific protocol identifier.
fn generate_legacy_protocol_name(protocol_id: &ProtocolId) -> String {
format!("/{}/sync/2", protocol_id.as_ref())
}
/// The key of [`BlockRequestHandler::seen_requests`].
#[derive(Eq, PartialEq, Clone)]
struct SeenRequestsKey {
peer: PeerId,
from: BlockId,
max_blocks: usize,
direction: Direction,
attributes: BlockAttributes,
support_multiple_justifications: bool,
}
#[allow(clippy::derived_hash_with_manual_eq)]
impl Hash for SeenRequestsKey {
fn hash(&self, state: &mut H) {
self.peer.hash(state);
self.max_blocks.hash(state);
self.direction.hash(state);
self.attributes.hash(state);
self.support_multiple_justifications.hash(state);
match self.from {
BlockId::Hash(h) => h.hash(state),
BlockId::Number(n) => n.hash(state),
}
}
}
/// The value of [`BlockRequestHandler::seen_requests`].
enum SeenRequestsValue {
/// First time we have seen the request.
First,
/// We have fulfilled the request `n` times.
Fulfilled(usize),
}
/// The full block server implementation of [`BlockServer`]. It handles
/// the incoming block requests from a remote peer.
pub struct BlockRequestHandler {
client: Arc,
request_receiver: async_channel::Receiver,
/// Maps from request to number of times we have seen this request.
///
/// This is used to check if a peer is spamming us with the same request.
seen_requests: LruMap, SeenRequestsValue>,
}
impl BlockRequestHandler
where
B: BlockT,
Client: HeaderBackend + BlockBackend + Send + Sync + 'static,
{
/// Create a new [`BlockRequestHandler`].
pub fn new::Hash>>(
network: NetworkServiceHandle,
protocol_id: &ProtocolId,
fork_id: Option<&str>,
client: Arc,
num_peer_hint: usize,
) -> BlockRelayParams {
// Reserve enough request slots for one request per peer when we are at the maximum
// number of peers.
let capacity = std::cmp::max(num_peer_hint, 1);
let (tx, request_receiver) = async_channel::bounded(capacity);
let protocol_config = generate_protocol_config::<_, B, N>(
protocol_id,
client
.block_hash(0u32.into())
.ok()
.flatten()
.expect("Genesis block exists; qed"),
fork_id,
tx,
);
let capacity = ByLength::new(num_peer_hint.max(1) as u32 * 2);
let seen_requests = LruMap::new(capacity);
BlockRelayParams {
server: Box::new(Self { client, request_receiver, seen_requests }),
downloader: Arc::new(FullBlockDownloader::new(
protocol_config.protocol_name().clone(),
network,
)),
request_response_config: protocol_config,
}
}
/// Run [`BlockRequestHandler`].
async fn process_requests(&mut self) {
while let Some(request) = self.request_receiver.next().await {
let IncomingRequest { peer, payload, pending_response } = request;
match self.handle_request(payload, pending_response, &peer) {
Ok(()) => debug!(target: LOG_TARGET, "Handled block request from {}.", peer),
Err(e) => debug!(
target: LOG_TARGET,
"Failed to handle block request from {}: {}", peer, e,
),
}
}
}
fn handle_request(
&mut self,
payload: Vec,
pending_response: oneshot::Sender,
peer: &PeerId,
) -> Result<(), HandleRequestError> {
let request = crate::schema::v1::BlockRequest::decode(&payload[..])?;
let from_block_id = match request.from_block.ok_or(HandleRequestError::MissingFromField)? {
FromBlockSchema::Hash(ref h) => {
let h = Decode::decode(&mut h.as_ref())?;
BlockId::::Hash(h)
},
FromBlockSchema::Number(ref n) => {
let n = Decode::decode(&mut n.as_ref())?;
BlockId::::Number(n)
},
};
let max_blocks = if request.max_blocks == 0 {
MAX_BLOCKS_IN_RESPONSE
} else {
min(request.max_blocks as usize, MAX_BLOCKS_IN_RESPONSE)
};
let direction =
i32::try_into(request.direction).map_err(|_| HandleRequestError::ParseDirection)?;
let attributes = BlockAttributes::from_be_u32(request.fields)?;
let support_multiple_justifications = request.support_multiple_justifications;
let key = SeenRequestsKey {
peer: *peer,
max_blocks,
direction,
from: from_block_id,
attributes,
support_multiple_justifications,
};
let mut reputation_change = None;
let small_request = attributes
.difference(BlockAttributes::HEADER | BlockAttributes::JUSTIFICATION)
.is_empty();
match self.seen_requests.get(&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_change = Some(if small_request {
rep::SAME_SMALL_REQUEST
} else {
rep::SAME_REQUEST
});
}
},
None => {
self.seen_requests.insert(key.clone(), SeenRequestsValue::First);
},
}
debug!(
target: LOG_TARGET,
"Handling block request from {peer}: Starting at `{from_block_id:?}` with \
maximum blocks of `{max_blocks}`, reputation_change: `{reputation_change:?}`, \
small_request `{small_request:?}`, direction `{direction:?}` and \
attributes `{attributes:?}`.",
);
let maybe_block_response = if reputation_change.is_none() || small_request {
let block_response = self.get_block_response(
attributes,
from_block_id,
direction,
max_blocks,
support_multiple_justifications,
)?;
// If any of the blocks contains any data, we can consider it as successful request.
if block_response
.blocks
.iter()
.any(|b| !b.header.is_empty() || !b.body.is_empty() || b.is_empty_justification)
{
if let Some(value) = self.seen_requests.get(&key) {
// If this is the first time we have processed this request, we need to change
// it to `Fulfilled`.
if let SeenRequestsValue::First = value {
*value = SeenRequestsValue::Fulfilled(1);
}
}
}
Some(block_response)
} else {
None
};
debug!(
target: LOG_TARGET,
"Sending result of block request from {peer} starting at `{from_block_id:?}`: \
blocks: {:?}, data: {:?}",
maybe_block_response.as_ref().map(|res| res.blocks.len()),
maybe_block_response.as_ref().map(|res| res.encoded_len()),
);
let result = if let Some(block_response) = maybe_block_response {
let mut data = Vec::with_capacity(block_response.encoded_len());
block_response.encode(&mut data)?;
Ok(data)
} else {
Err(())
};
pending_response
.send(OutgoingResponse {
result,
reputation_changes: reputation_change.into_iter().collect(),
sent_feedback: None,
})
.map_err(|_| HandleRequestError::SendResponse)
}
fn get_block_response(
&self,
attributes: BlockAttributes,
mut block_id: BlockId,
direction: Direction,
max_blocks: usize,
support_multiple_justifications: bool,
) -> Result {
let get_header = attributes.contains(BlockAttributes::HEADER);
let get_body = attributes.contains(BlockAttributes::BODY);
let get_indexed_body = attributes.contains(BlockAttributes::INDEXED_BODY);
let get_justification = attributes.contains(BlockAttributes::JUSTIFICATION);
let mut blocks = Vec::new();
let mut total_size: usize = 0;
let client_header_from_block_id =
|block_id: BlockId| -> Result