network: Use "one shot" protocol handler. (#3520)

* network: Use "one shot" protocol handler.

Add two new `NetworkBehaviour`s, one handling remote block requests
and another one to handle light client requests (both local and from
remote). The change is motivated by the desire to use multiple
substreams of a single connection for different protocols. To achieve
this, libp2p's `OneShotHandler` is used as a protocol handler in each
behaviour. It will open a fresh substream for the duration of the
request and close it afterwards. For block requests, we currently only
handle incoming requests from remote and tests are missing. For light
client handling we support incoming requests from remote and also
ported a substantial amount of functionality over from
`light_dispatch.rs` (including several tests). However the result lacks
in at least two aspects:

(1) We require external updates w.r.t. the best block per peer and
currently nothing updates this information.
(2) We carry a lot of peer-related state around.

Both aspects could be simplified by externalising peer selection and
just requiring a specific peer ID where the request should be sent to.
We still have to maintain some peer related state due to the way
libp2p's swarm and network behaviour work (e.g. we must make sure to
always issue `NetworkBehaviourAction::SendEvent`s to peers we are
connected to, otherwise the actions die a silent death.

Another change implemented here is the use of protocol buffers as the
encoding for network messages. Certain individual fields of messages
are still SCALE encoded. There has been some discussion about this
in another PR (https://github.com/paritytech/substrate/pull/3452), so
far without resolution.

* Uncomment `Behaviour::light_client_request`.

* Add license headers.
This commit is contained in:
Toralf Wittner
2020-02-12 11:50:52 +01:00
committed by GitHub
parent 173644c8b9
commit 51a45c5d9f
11 changed files with 2436 additions and 27 deletions
+15
View File
@@ -54,15 +54,30 @@ use crate::error;
use util::LruHashSet;
use wasm_timer::Instant;
// Include sources generated from protobuf definitions.
pub mod api {
pub mod v1 {
include!(concat!(env!("OUT_DIR"), "/api.v1.rs"));
pub mod light {
include!(concat!(env!("OUT_DIR"), "/api.v1.light.rs"));
}
}
}
mod legacy_proto;
mod util;
pub mod block_requests;
pub mod message;
pub mod event;
pub mod light_client_handler;
pub mod light_dispatch;
pub mod specialization;
pub mod sync;
pub use block_requests::BlockRequests;
pub use light_client_handler::LightClientHandler;
const REQUEST_TIMEOUT_SEC: u64 = 40;
/// Interval at which we perform time based maintenance
const TICK_TIMEOUT: time::Duration = time::Duration::from_millis(1100);