-
Notifications
You must be signed in to change notification settings - Fork 2.7k
client/network: Use request response for block requests #7478
Changes from 1 commit
69ef3fa
e8df535
3207c53
e3ef56e
544ebfe
8abd813
8da90c5
45de155
456568f
1a6b00e
06dbd98
25e298f
2ed6dd1
aacb507
1a72e0c
1385f5c
8fdf1bd
7460a21
f1e7691
401a7f8
b375b84
107aa83
0535723
7a3d71e
64d2222
9560e32
8b7b32d
109fa3f
d6a6a52
1cd8a71
d5ea27e
9bfa9ca
006fd6e
1bca347
2797595
aaa981c
d1361f2
c19885e
81cadfc
9236113
4c018f1
07c0d2f
8c8bcb2
9481e6d
707c1bd
e05c1ad
df5907f
4096019
24e14c8
94a1a79
db776ca
8e846e0
4c45b4b
5277841
bccc53e
e0ae207
d2c5161
cc1bff0
9acc7ee
34d0351
2cb76ef
d525b96
1da9269
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,8 +32,20 @@ use std::time::Duration; | |
|
|
||
| const LOG_TARGET: &str = "finality-request-handler"; | ||
|
|
||
| // TODO: Document that this is for clients not reponding only. | ||
| pub fn generate_protocol_config(protocol_id: ProtocolId) -> ProtocolConfig { | ||
| ProtocolConfig { | ||
| name: generate_protocol_name(protocol_id).into(), | ||
| max_request_size: 1024 * 1024, | ||
| max_response_size: 1024 * 1024, | ||
| // TODO: What is a sane value here? | ||
| request_timeout: Duration::from_secs(10), | ||
| inbound_queue: None, | ||
|
||
| } | ||
| } | ||
|
|
||
| /// Generate the finality proof protocol name from chain specific protocol identifier. | ||
| pub fn generate_protocol_name(protocol_id: ProtocolId) -> String { | ||
| fn generate_protocol_name(protocol_id: ProtocolId) -> String { | ||
| let mut s = String::new(); | ||
| s.push_str("/"); | ||
| s.push_str(protocol_id.as_ref()); | ||
|
|
@@ -56,14 +68,8 @@ impl<B: BlockT> FinalityRequestHandler<B> { | |
| // TODO: Likeley we want to allow more than 0 buffered requests. Rethink this value. | ||
wheresaddie marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let (tx, rx) = mpsc::channel(0); | ||
|
|
||
| let protocol_config = ProtocolConfig { | ||
| name: generate_protocol_name(protocol_id).into(), | ||
| max_request_size: 1024 * 1024, | ||
| max_response_size: 1024 * 1024, | ||
| // TODO: What is a sane value here? | ||
| request_timeout: Duration::from_secs(10), | ||
| inbound_queue: Some(tx), | ||
| }; | ||
| let mut protocol_config = generate_protocol_config(protocol_id); | ||
| protocol_config.inbound_queue = Some(tx); | ||
|
|
||
| let handler = FinalityRequestHandler { | ||
| proof_provider, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,8 @@ use std::{ | |
| use libp2p::build_multiaddr; | ||
| use log::trace; | ||
| use sc_network::config::FinalityProofProvider; | ||
| use sc_network::block_request_handler::BlockRequestHandler; | ||
| use sc_network::finality_request_handler::FinalityRequestHandler; | ||
| use sp_blockchain::{ | ||
| HeaderBackend, Result as ClientResult, | ||
| well_known_cache_keys::{self, Id as CacheKeyId}, | ||
|
|
@@ -50,6 +52,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, BoxFinalityProofRequestBuilder}; | ||
| use libp2p::PeerId; | ||
|
|
@@ -670,22 +673,42 @@ 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"); | ||
|
|
||
| // Add block request handler. | ||
| let block_request_protocol_config = { | ||
| let (handler, protocol_config) = BlockRequestHandler::new(protocol_id.clone(), client.clone()); | ||
| self.spawn_task(handler.run().boxed()); | ||
| protocol_config | ||
| }; | ||
|
|
||
| // Add finality request handler. | ||
| let finality_request_protocol_config = match self.make_finality_proof_provider(PeersClient::Full(client.clone(), backend.clone())) { | ||
| Some(provider) => { | ||
| let (handler, protocol_config) = FinalityRequestHandler::new(protocol_id.clone(), provider); | ||
| self.spawn_task(handler.run().boxed()); | ||
| protocol_config | ||
| }, | ||
| None => { | ||
| sc_network::finality_request_handler::generate_protocol_config(protocol_id.clone()) | ||
| } | ||
| }; | ||
|
|
||
| let network = NetworkWorker::new(sc_network::config::Params { | ||
| role: Role::Full, | ||
| executor: None, | ||
| network_config, | ||
| chain: client.clone(), | ||
| finality_proof_provider: self.make_finality_proof_provider( | ||
| PeersClient::Full(client.clone(), backend.clone()), | ||
| ), | ||
| finality_proof_request_builder, | ||
| 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, | ||
| finality_request_protocol_config, | ||
| }).unwrap(); | ||
|
|
||
| self.mut_peers(|peers| { | ||
|
|
@@ -750,21 +773,28 @@ 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 = sc_network::block_request_handler::generate_protocol_config(protocol_id.clone()); | ||
|
|
||
| // Add finality request handler. | ||
| let finality_request_protocol_config = sc_network::finality_request_handler::generate_protocol_config(protocol_id.clone()); | ||
|
|
||
| let network = NetworkWorker::new(sc_network::config::Params { | ||
| role: Role::Light, | ||
| executor: None, | ||
| network_config, | ||
| chain: client.clone(), | ||
| finality_proof_provider: self.make_finality_proof_provider( | ||
| PeersClient::Light(client.clone(), backend.clone()) | ||
| ), | ||
| finality_proof_request_builder, | ||
| 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, | ||
| finality_request_protocol_config, | ||
| }).unwrap(); | ||
|
|
||
| self.mut_peers(|peers| { | ||
|
|
@@ -789,6 +819,11 @@ pub trait TestNetFactory: Sized { | |
| }); | ||
| } | ||
|
|
||
| // TODO: Not ideal. Can we do better? | ||
|
||
| 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. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not have this function take a
mpsc::Receiver<IncomingRequest>, given that we're providing one on line74? I feel like this change would make the code easier to read, instead of someone being like 'Oh there's noinbound_queuefor this protocol. Oh wait, nevermind.'.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the answer lies in the fact that this is a "default configuration" and a public function and it is indeed permissible to use a configuration that is not capable of receiving requests. You can find the following comment in other parts of the code:
I guess you're also not supposed to pass in your own
mpsc::Receiver, so either you don't support inbound requests or you must useBlockRequestHandler::new, hence even taking anOption<mpsc::Receiver<IncomingRequest>>may not be desirable for the public API of this module. Just an educated guess.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To extend Roman's comment above ...
We have to support two use-cases:
Supporting outgoing requests as well as incoming requests, e.g. what a full-node would do.
Supporting outgoing requests only, e.g. what a light-client would do.
As you correctly noted above, one differentiates between the two by specifying an
inbound_queueor not (seeProtocolConfigdoc).A
ProtocolConfigfor the first use-case (full-node) can be created viaBlockRequestHandler::new. I decided to haveBlockRequest::newreturn both theBlockRequestHandlerand theProtocolConfigto force callers to create both, i.e. not create aProtocolConfigwith aninbound_queuewithout a handler listening on the other side of that queue. With that in mind, and as Roman already explained above, taking aOption<mpsc::Receiver<IncomingRequest>>ingenerate_protocol_configwould allow callers to misuse the API.A
ProtocolConfigfor the second use-case (light-client) can be created viagenerate_protocol_config.This is not cast in stone. I am grateful for alternative suggestions. From my point of view it is confusing to create a block request
ProtocolConfigvia a function inblock_request_handler.rseven in the case where one explicity does not want to handle incoming requests (light-client).