referrerpolicy=no-referrer-when-downgrade

sc_network_light/
light_client_requests.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Helpers for outgoing and incoming light client requests.
20
21use sc_network::{
22	config::ProtocolId, request_responses::IncomingRequest, NetworkBackend, MAX_RESPONSE_SIZE,
23};
24use sp_runtime::traits::Block;
25
26use std::time::Duration;
27
28/// For incoming light client requests.
29pub mod handler;
30
31/// Generate the light client protocol name from the genesis hash and fork id.
32fn generate_protocol_name<Hash: AsRef<[u8]>>(genesis_hash: Hash, fork_id: Option<&str>) -> String {
33	let genesis_hash = genesis_hash.as_ref();
34	if let Some(fork_id) = fork_id {
35		format!("/{}/{}/light/2", array_bytes::bytes2hex("", genesis_hash), fork_id)
36	} else {
37		format!("/{}/light/2", array_bytes::bytes2hex("", genesis_hash))
38	}
39}
40
41/// Generate the legacy light client protocol name from chain specific protocol identifier.
42fn generate_legacy_protocol_name(protocol_id: &ProtocolId) -> String {
43	format!("/{}/light/2", protocol_id.as_ref())
44}
45
46/// Generates a `RequestResponseProtocolConfig` for the light client request protocol, refusing
47/// incoming requests.
48pub fn generate_protocol_config<
49	Hash: AsRef<[u8]>,
50	B: Block,
51	N: NetworkBackend<B, <B as Block>::Hash>,
52>(
53	protocol_id: &ProtocolId,
54	genesis_hash: Hash,
55	fork_id: Option<&str>,
56	inbound_queue: async_channel::Sender<IncomingRequest>,
57) -> N::RequestResponseProtocolConfig {
58	N::request_response_config(
59		generate_protocol_name(genesis_hash, fork_id).into(),
60		std::iter::once(generate_legacy_protocol_name(protocol_id).into()).collect(),
61		1 * 1024 * 1024,
62		MAX_RESPONSE_SIZE,
63		Duration::from_secs(15),
64		Some(inbound_queue),
65	)
66}