Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
bcdf9fa
client/network: Re-enable light_client_handler.rs unit tests
mxinden Jan 8, 2021
f9ef812
client/network: Add scaffolding for light client using req-resp
mxinden Jan 8, 2021
20ee04d
client/network: Make it compile
mxinden Jan 11, 2021
8596782
client/network: Rename OutEvent SendRequest
mxinden Jan 12, 2021
00a092a
client/network: Restructure light client request client and handler
mxinden Jan 12, 2021
1fccb36
client/network: Rename light client request client to sender
mxinden Jan 12, 2021
1a45ab7
client/network: Remove light client prepare_request
mxinden Jan 12, 2021
2bb5740
client/network/src/light: Rework configuration
mxinden Jan 13, 2021
43b30a8
client/network: Formatting
mxinden Jan 13, 2021
a9d8b4f
Merge branch 'paritytech/master' into light-client-request-response
mxinden Jan 13, 2021
5262e2d
client/network/light: Remove RequestId
mxinden Jan 13, 2021
be5a591
client/network/light: Make request functions methods
mxinden Jan 13, 2021
48592e1
client/network/light: Refactor request wrapping
mxinden Jan 13, 2021
8be4473
client/network/light: Fix warnings
mxinden Jan 14, 2021
feeef54
client/network/light: Serialize request in method
mxinden Jan 14, 2021
c893aff
client/network/light: Make returning response a method
mxinden Jan 14, 2021
4a2edde
client/network/light: Depend on request response to timeout requests
mxinden Jan 14, 2021
fcece3a
client/network: Fix test compilation
mxinden Jan 14, 2021
30eb9ae
client/network/light: Re-enable connection test
mxinden Jan 14, 2021
32774d0
client/network/light: Re-enable timeout test
mxinden Jan 15, 2021
db2d9b5
client/network/light: Re-enable incorrect_response test
mxinden Jan 15, 2021
c86671a
client/network/light: Re-enable wrong_response_type test
mxinden Jan 15, 2021
f2d1487
client/network/light: Re-enable retry_count_failures test
mxinden Jan 15, 2021
310efa3
client/network/light: Re-enable issue_request tests
mxinden Jan 15, 2021
6d33c95
client/network/light: Re-enable send_receive tests
mxinden Jan 15, 2021
7efd5c3
client/network/light: Deduplicate test logic
mxinden Jan 18, 2021
2670d1d
client/network/light: Remove unused imports
mxinden Jan 18, 2021
08252ed
client/network/light: Handle request failure
mxinden Jan 18, 2021
671d398
client/network/light: Move generate_protocol_config
mxinden Jan 18, 2021
2e54c01
Merge branch 'paritytech/master' into light-client-request-response
mxinden Jan 18, 2021
984fa95
client/network: Fix test compilation
mxinden Jan 18, 2021
8e24f29
client/network: Rename light client request client to sender
mxinden Jan 19, 2021
31d6791
client/network: Handle too-many-requests error
mxinden Jan 19, 2021
3a04470
Merge branch 'paritytech/master' into light-client-request-response
mxinden Jan 19, 2021
d4a2be6
client/network: Update outdated comments
mxinden Jan 19, 2021
6da6207
client/network/light: Choose any peer if none has best block defined
mxinden Jan 20, 2021
5d1d682
.maintain: Replace sentry-node with local-docker-test-network
mxinden Jan 20, 2021
5a609ec
client/network/light: Handle oneshot cancellation
mxinden Jan 21, 2021
2477f34
client/network/light: Do not reduce retry count on missing peer
mxinden Jan 21, 2021
6dfb7c1
client/network/request-response: Assert in debug request id to be unique
mxinden Jan 21, 2021
b39509a
client/network/light: Choose same limit as block request protocol
mxinden Jan 21, 2021
e5bf6cd
Merge branch 'paritytech/master' into light-client-request-response
mxinden Jan 21, 2021
c4ac336
client/network: Report reputation changes via response
mxinden Jan 22, 2021
74671e3
Merge branch 'paritytech/master' into light-client-request-response
mxinden Jan 27, 2021
c4106c2
Merge branch 'paritytech/master' into light-client-request-response
mxinden Feb 1, 2021
b5d9636
client/network: Remove resolved TODOs
mxinden Feb 1, 2021
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
62 changes: 40 additions & 22 deletions client/network/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{
config::{ProtocolId, Role}, light_client_handler, peer_info, request_responses,
config::{ProtocolId, Role},
discovery::{DiscoveryBehaviour, DiscoveryConfig, DiscoveryOut},
protocol::{message::Roles, CustomMessageOutcome, NotificationsSink, Protocol},
peer_info, request_responses, light_client_requests,
ObservedRole, DhtEvent, ExHashT,
};

use bytes::Bytes;
use futures::channel::oneshot;
use futures::{channel::oneshot, stream::StreamExt};
use libp2p::NetworkBehaviour;
use libp2p::core::{Multiaddr, PeerId, PublicKey};
use libp2p::identify::IdentifyInfo;
Expand Down Expand Up @@ -59,8 +60,6 @@ pub struct Behaviour<B: BlockT, H: ExHashT> {
discovery: DiscoveryBehaviour,
/// Generic request-reponse protocols.
request_responses: request_responses::RequestResponsesBehaviour,
/// Light client request handling.
light_client_handler: light_client_handler::LightClientHandler<B>,

/// Queue of events to produce for the outside.
#[behaviour(ignore)]
Expand All @@ -70,6 +69,10 @@ pub struct Behaviour<B: BlockT, H: ExHashT> {
#[behaviour(ignore)]
role: Role,

/// Light client request handling.
#[behaviour(ignore)]
light_client_request_sender: light_client_requests::sender::LightClientRequestSender<B>,

/// Protocol name used to send out block requests via
/// [`request_responses::RequestResponsesBehaviour`].
#[behaviour(ignore)]
Expand Down Expand Up @@ -174,24 +177,26 @@ impl<B: BlockT, H: ExHashT> Behaviour<B, H> {
role: Role,
user_agent: String,
local_public_key: PublicKey,
light_client_handler: light_client_handler::LightClientHandler<B>,
light_client_request_sender: light_client_requests::sender::LightClientRequestSender<B>,
disco_config: DiscoveryConfig,
// Block request protocol config.
block_request_protocol_config: request_responses::ProtocolConfig,
light_client_request_protocol_config: request_responses::ProtocolConfig,
// All remaining request protocol configs.
mut request_response_protocols: Vec<request_responses::ProtocolConfig>,
) -> Result<Self, request_responses::RegisterError> {
// Extract protocol name and add to `request_response_protocols`.
let block_request_protocol_name = block_request_protocol_config.name.to_string();
request_response_protocols.push(block_request_protocol_config);

request_response_protocols.push(light_client_request_protocol_config);

Ok(Behaviour {
substrate,
peer_info: peer_info::PeerInfoBehaviour::new(user_agent, local_public_key),
discovery: disco_config.finish(),
request_responses:
request_responses::RequestResponsesBehaviour::new(request_response_protocols.into_iter())?,
light_client_handler,
light_client_request_sender,
events: VecDeque::new(),
role,

Expand Down Expand Up @@ -268,8 +273,11 @@ impl<B: BlockT, H: ExHashT> Behaviour<B, H> {
}

/// Issue a light client request.
pub fn light_client_request(&mut self, r: light_client_handler::Request<B>) -> Result<(), light_client_handler::Error> {
self.light_client_handler.request(r)
pub fn light_client_request(
&mut self,
r: light_client_requests::sender::Request<B>,
) -> Result<(), light_client_requests::sender::SendRequestError> {
self.light_client_request_sender.request(r)
}
}

Expand All @@ -289,13 +297,6 @@ fn reported_roles_to_observed_role(local_role: &Role, remote: &PeerId, roles: Ro
}
}

impl<B: BlockT, H: ExHashT> NetworkBehaviourEventProcess<void::Void> for
Behaviour<B, H> {
fn inject_event(&mut self, event: void::Void) {
void::unreachable(event)
}
}

impl<B: BlockT, H: ExHashT> NetworkBehaviourEventProcess<CustomMessageOutcome<B>> for
Behaviour<B, H> {
fn inject_event(&mut self, event: CustomMessageOutcome<B>) {
Expand Down Expand Up @@ -343,12 +344,16 @@ Behaviour<B, H> {
self.events.push_back(BehaviourOut::NotificationsReceived { remote, messages });
},
CustomMessageOutcome::PeerNewBest(peer_id, number) => {
self.light_client_handler.update_best_block(&peer_id, number);
self.light_client_request_sender.update_best_block(&peer_id, number);
}
CustomMessageOutcome::SyncConnected(peer_id) => {
self.light_client_request_sender.inject_connected(peer_id);
self.events.push_back(BehaviourOut::SyncConnected(peer_id))
}
CustomMessageOutcome::SyncDisconnected(peer_id) => {
self.light_client_request_sender.inject_disconnected(peer_id);
self.events.push_back(BehaviourOut::SyncDisconnected(peer_id))
}
CustomMessageOutcome::SyncConnected(peer_id) =>
self.events.push_back(BehaviourOut::SyncConnected(peer_id)),
CustomMessageOutcome::SyncDisconnected(peer_id) =>
self.events.push_back(BehaviourOut::SyncDisconnected(peer_id)),
CustomMessageOutcome::None => {}
}
}
Expand Down Expand Up @@ -443,7 +448,20 @@ impl<B: BlockT, H: ExHashT> NetworkBehaviourEventProcess<DiscoveryOut>
}

impl<B: BlockT, H: ExHashT> Behaviour<B, H> {
fn poll<TEv>(&mut self, _: &mut Context, _: &mut impl PollParameters) -> Poll<NetworkBehaviourAction<TEv, BehaviourOut<B>>> {
fn poll<TEv>(
&mut self,
cx: &mut Context,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<TEv, BehaviourOut<B>>> {
use light_client_requests::sender::OutEvent;
while let Poll::Ready(Some(event)) = self.light_client_request_sender.poll_next_unpin(cx) {
match event {
OutEvent::SendRequest { target, request, pending_response, protocol_name } => {
self.request_responses.send_request(&target, &protocol_name, request, pending_response)
}
}
}

if let Some(event) = self.events.pop_front() {
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event))
}
Expand Down
41 changes: 22 additions & 19 deletions client/network/src/block_request_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const MAX_BLOCKS_IN_RESPONSE: usize = 128;
const MAX_BODY_BYTES: usize = 8 * 1024 * 1024;

/// Generates a [`ProtocolConfig`] for the block request protocol, refusing incoming requests.
pub fn generate_protocol_config(protocol_id: ProtocolId) -> ProtocolConfig {
pub fn generate_protocol_config(protocol_id: &ProtocolId) -> ProtocolConfig {
ProtocolConfig {
name: generate_protocol_name(protocol_id).into(),
max_request_size: 1024 * 1024,
Expand All @@ -50,7 +50,10 @@ pub fn generate_protocol_config(protocol_id: ProtocolId) -> ProtocolConfig {
}

/// Generate the block protocol name from chain specific protocol identifier.
fn generate_protocol_name(protocol_id: ProtocolId) -> String {
//
// 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());
Expand All @@ -66,7 +69,7 @@ pub struct BlockRequestHandler<B> {

impl <B: BlockT> BlockRequestHandler<B> {
/// Create a new [`BlockRequestHandler`].
pub fn new(protocol_id: ProtocolId, client: Arc<dyn Client<B>>) -> (Self, ProtocolConfig) {
pub fn new(protocol_id: &ProtocolId, client: Arc<dyn Client<B>>) -> (Self, ProtocolConfig) {
// Rate of arrival multiplied with the waiting time in the queue equals the queue length.
//
// An average Polkadot sentry node serves less than 5 requests per second. The 95th percentile
Expand All @@ -82,6 +85,22 @@ impl <B: BlockT> BlockRequestHandler<B> {
(Self { client, request_receiver }, protocol_config)
}

/// Run [`BlockRequestHandler`].
pub async fn run(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) {
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(
&self,
payload: Vec<u8>,
Expand Down Expand Up @@ -186,22 +205,6 @@ impl <B: BlockT> BlockRequestHandler<B> {
reputation_changes: Vec::new(),
}).map_err(|_| HandleRequestError::SendResponse)
}

/// Run [`BlockRequestHandler`].
pub async fn run(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) {
Ok(()) => debug!(target: LOG_TARGET, "Handled block request from {}.", peer),
Err(e) => debug!(
target: LOG_TARGET,
"Failed to handle block request from {}: {}",
peer, e,
),
}
}
}
}

#[derive(derive_more::Display, derive_more::From)]
Expand Down
8 changes: 8 additions & 0 deletions client/network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ pub struct Params<B: BlockT, H: ExHashT> {
/// [`block_request_handler::BlockRequestHandler::new`] allowing both outgoing and incoming
/// requests.
pub block_request_protocol_config: RequestResponseConfig,

/// Request response configuration for the light client request protocol.
///
/// Can be constructed either via [`light_client_requests::generate_protocol_config`] allowing
/// outgoing but not incoming requests, or constructed via
/// [`light_client_requests::handler::LightClientRequestHandler::new`] allowing both outgoing
/// and incoming requests.
pub light_client_request_protocol_config: RequestResponseConfig,
}

/// Role of the local node.
Expand Down
13 changes: 12 additions & 1 deletion client/network/src/gossip/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::block_request_handler::BlockRequestHandler;
use crate::light_client_requests::handler::LightClientRequestHandler;
use crate::gossip::QueuedSender;
use crate::{config, Event, NetworkService, NetworkWorker};

Expand Down Expand Up @@ -96,7 +97,16 @@ fn build_test_full_node(network_config: config::NetworkConfiguration)

let block_request_protocol_config = {
let (handler, protocol_config) = BlockRequestHandler::new(
protocol_id.clone(),
&protocol_id,
client.clone(),
);
async_std::task::spawn(handler.run().boxed());
protocol_config
};

let light_client_request_protocol_config = {
let (handler, protocol_config) = LightClientRequestHandler::new(
&protocol_id,
client.clone(),
);
async_std::task::spawn(handler.run().boxed());
Expand All @@ -117,6 +127,7 @@ fn build_test_full_node(network_config: config::NetworkConfiguration)
),
metrics_registry: None,
block_request_protocol_config,
light_client_request_protocol_config,
})
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion client/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ mod behaviour;
mod chain;
mod peer_info;
mod discovery;
mod light_client_handler;
mod on_demand_layer;
mod protocol;
mod request_responses;
Expand All @@ -259,6 +258,7 @@ mod transport;
mod utils;

pub mod block_request_handler;
pub mod light_client_requests;
pub mod config;
pub mod error;
pub mod gossip;
Expand Down
Loading