client/network: Use request response for block requests (#7478)

* client/network: Add scaffolding for finality req to use req resp
	#sc

* client/network/src/finality_requests: Remove

* client/network/src/behaviour: Pass request id down to sync

* client/network: Use request response for block requests

* client/network: Move handler logic into *_*_handler.rs

* client/network: Track ongoing finality requests in protocol.rs

* client/network: Remove commented out finalization initialization

* client/network: Add docs for request handlers

* client/network/finality_request_handler: Log errors

* client/network/block_request_handler: Log errors

* client/network: Format

* client/network: Handle block request failure

* protocols/network: Fix tests

* client/network/src/behaviour: Handle request sending errors

* client/network: Move response handling into custom method

* client/network/protocol: Handle block response errors

* client/network/protocol: Remove tracking of obsolete requests

* client/network/protocol: Remove block request start time tracking

This will be handled generically via request-responses.

* client/network/protocol: Refactor on_*_request_started

* client/network: Pass protocol config instead of protocol name

* client/network: Pass protocol config in tests

* client/network/config: Document request response configs

* client/network/src/_request_handler: Document protocol config gen

* client/network/src/protocol: Document Peer request values

* client/network: Rework request response to always use oneshot

* client/network: Unified metric reporting for all request protocols

* client/network: Move protobuf parsing into protocol.rs

* client/network/src/protocol: Return pending events after poll

* client/network: Improve error handling and documentation

* client/network/behaviour: Remove outdated error types

* Update client/network/src/block_request_handler.rs

Co-authored-by: Ashley <ashley.ruglys@gmail.com>

* Update client/network/src/finality_request_handler.rs

Co-authored-by: Ashley <ashley.ruglys@gmail.com>

* client/network/protocol: Reduce reputation on timeout

* client/network/protocol: Refine reputation changes

* client/network/block_request_handler: Set and explain queue length

* client/service: Deny block requests when light client

* client/service: Fix role matching

* client: Enforce line width

* client/network/request_responses: Fix unit tests

* client/network: Expose time to build response via metrics

* client/network/request_responses: Fix early connection closed error

* client/network/protocol: Fix line length

* client/network/protocol: Disconnect on most request failures

* client/network/protocol: Disconnect peer when oneshot is canceled

* client/network/protocol: Disconnect peer even when connection closed

* client/network/protocol: Remove debugging log line

* client/network/request_response: Use Clone::clone for error

* client/network/request_response: Remove outdated comment

With libp2p v0.33.0 libp2p-request-response properly sends inbound
failures on connections being closed.

Co-authored-by: Addie Wagenknecht <addie@nortd.com>
Co-authored-by: Ashley <ashley.ruglys@gmail.com>
This commit is contained in:
Max Inden
2021-01-05 19:20:54 +01:00
committed by GitHub
parent 92f596829d
commit 3f629f743b
17 changed files with 780 additions and 1299 deletions
+26 -2
View File
@@ -29,6 +29,7 @@ use std::{
use libp2p::build_multiaddr;
use log::trace;
use sc_network::block_request_handler::{self, BlockRequestHandler};
use sp_blockchain::{
HeaderBackend, Result as ClientResult,
well_known_cache_keys::{self, Id as CacheKeyId},
@@ -49,6 +50,7 @@ use sp_consensus::block_import::{BlockImport, ImportResult};
use sp_consensus::Error as ConsensusError;
use sp_consensus::{BlockOrigin, ForkChoiceStrategy, BlockImportParams, BlockCheckParams, JustificationImport};
use futures::prelude::*;
use futures::future::BoxFuture;
use sc_network::{NetworkWorker, NetworkService, config::ProtocolId};
use sc_network::config::{NetworkConfiguration, TransportConfig};
use libp2p::PeerId;
@@ -682,6 +684,14 @@ pub trait TestNetFactory: Sized {
network_config.allow_non_globals_in_dht = true;
network_config.notifications_protocols = config.notifications_protocols;
let protocol_id = ProtocolId::from("test-protocol-name");
let block_request_protocol_config = {
let (handler, protocol_config) = BlockRequestHandler::new(protocol_id.clone(), client.clone());
self.spawn_task(handler.run().boxed());
protocol_config
};
let network = NetworkWorker::new(sc_network::config::Params {
role: Role::Full,
executor: None,
@@ -689,11 +699,12 @@ pub trait TestNetFactory: Sized {
chain: client.clone(),
on_demand: None,
transaction_pool: Arc::new(EmptyTransactionPool),
protocol_id: ProtocolId::from("test-protocol-name"),
protocol_id,
import_queue,
block_announce_validator: config.block_announce_validator
.unwrap_or_else(|| Box::new(DefaultBlockAnnounceValidator)),
metrics_registry: None,
block_request_protocol_config,
}).unwrap();
trace!(target: "test_network", "Peer identifier: {}", network.service().local_peer_id());
@@ -757,6 +768,13 @@ pub trait TestNetFactory: Sized {
network_config.listen_addresses = vec![listen_addr.clone()];
network_config.allow_non_globals_in_dht = true;
let protocol_id = ProtocolId::from("test-protocol-name");
// Add block request handler.
let block_request_protocol_config = block_request_handler::generate_protocol_config(
protocol_id.clone(),
);
let network = NetworkWorker::new(sc_network::config::Params {
role: Role::Light,
executor: None,
@@ -764,10 +782,11 @@ pub trait TestNetFactory: Sized {
chain: client.clone(),
on_demand: None,
transaction_pool: Arc::new(EmptyTransactionPool),
protocol_id: ProtocolId::from("test-protocol-name"),
protocol_id,
import_queue,
block_announce_validator: Box::new(DefaultBlockAnnounceValidator),
metrics_registry: None,
block_request_protocol_config,
}).unwrap();
self.mut_peers(|peers| {
@@ -792,6 +811,11 @@ pub trait TestNetFactory: Sized {
});
}
/// Used to spawn background tasks, e.g. the block request protocol handler.
fn spawn_task(&self, f: BoxFuture<'static, ()>) {
async_std::task::spawn(f);
}
/// Polls the testnet until all nodes are in sync.
///
/// Must be executed in a task context.