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
@@ -41,7 +41,7 @@ const REQUEST_TIMEOUT: Duration = Duration::from_secs(15);
/// Default request retry count.
const RETRY_COUNT: usize = 1;
/// Reputation change for a peer when a request timed out.
const TIMEOUT_REPUTATION_CHANGE: i32 = -(1 << 8);
pub(crate) const TIMEOUT_REPUTATION_CHANGE: i32 = -(1 << 8);
/// Trait used by the `LightDispatch` service to communicate messages back to the network.
pub trait LightDispatchNetwork<B: BlockT> {
@@ -692,17 +692,26 @@ pub mod tests {
use crate::message::{self, BlockAttributes, Direction, FromBlock, RequestId};
use libp2p::PeerId;
use super::{REQUEST_TIMEOUT, LightDispatch, LightDispatchNetwork, RequestData, StorageProof};
use sp_test_primitives::{Block, Extrinsic, Header};
use sp_test_primitives::{Block, Header};
struct DummyFetchChecker { ok: bool }
pub(crate) struct DummyFetchChecker<B> {
pub(crate) ok: bool,
_mark: std::marker::PhantomData<B>
}
impl FetchChecker<Block> for DummyFetchChecker {
impl<B> DummyFetchChecker<B> {
pub(crate) fn new(ok: bool) -> Self {
DummyFetchChecker { ok, _mark: std::marker::PhantomData }
}
}
impl<B: BlockT> FetchChecker<B> for DummyFetchChecker<B> {
fn check_header_proof(
&self,
_request: &RemoteHeaderRequest<Header>,
header: Option<Header>,
_request: &RemoteHeaderRequest<B::Header>,
header: Option<B::Header>,
_remote_proof: StorageProof,
) -> ClientResult<Header> {
) -> ClientResult<B::Header> {
match self.ok {
true if header.is_some() => Ok(header.unwrap()),
_ => Err(ClientError::Backend("Test error".into())),
@@ -711,7 +720,7 @@ pub mod tests {
fn check_read_proof(
&self,
request: &RemoteReadRequest<Header>,
request: &RemoteReadRequest<B::Header>,
_: StorageProof,
) -> ClientResult<HashMap<Vec<u8>, Option<Vec<u8>>>> {
match self.ok {
@@ -727,7 +736,7 @@ pub mod tests {
fn check_read_child_proof(
&self,
request: &RemoteReadChildRequest<Header>,
request: &RemoteReadChildRequest<B::Header>,
_: StorageProof,
) -> ClientResult<HashMap<Vec<u8>, Option<Vec<u8>>>> {
match self.ok {
@@ -741,7 +750,7 @@ pub mod tests {
}
}
fn check_execution_proof(&self, _: &RemoteCallRequest<Header>, _: StorageProof) -> ClientResult<Vec<u8>> {
fn check_execution_proof(&self, _: &RemoteCallRequest<B::Header>, _: StorageProof) -> ClientResult<Vec<u8>> {
match self.ok {
true => Ok(vec![42]),
false => Err(ClientError::Backend("Test error".into())),
@@ -750,20 +759,20 @@ pub mod tests {
fn check_changes_proof(
&self,
_: &RemoteChangesRequest<Header>,
_: ChangesProof<Header>
) -> ClientResult<Vec<(NumberFor<Block>, u32)>> {
_: &RemoteChangesRequest<B::Header>,
_: ChangesProof<B::Header>
) -> ClientResult<Vec<(NumberFor<B>, u32)>> {
match self.ok {
true => Ok(vec![(100, 2)]),
true => Ok(vec![(100.into(), 2)]),
false => Err(ClientError::Backend("Test error".into())),
}
}
fn check_body_proof(
&self,
_: &RemoteBodyRequest<Header>,
body: Vec<Extrinsic>
) -> ClientResult<Vec<Extrinsic>> {
_: &RemoteBodyRequest<B::Header>,
body: Vec<B::Extrinsic>
) -> ClientResult<Vec<B::Extrinsic>> {
match self.ok {
true => Ok(body),
false => Err(ClientError::Backend("Test error".into())),
@@ -772,7 +781,7 @@ pub mod tests {
}
fn dummy(ok: bool) -> LightDispatch<Block> {
LightDispatch::new(Arc::new(DummyFetchChecker { ok }))
LightDispatch::new(Arc::new(DummyFetchChecker::new(ok)))
}
fn total_peers(light_dispatch: &LightDispatch<Block>) -> usize {
@@ -791,7 +800,7 @@ pub mod tests {
});
}
fn dummy_header() -> Header {
pub(crate) fn dummy_header() -> Header {
Header {
parent_hash: Default::default(),
number: 0,