Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

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

Merged
merged 4 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions client/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ license = "GPL-3.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"

[build-dependencies]
prost-build = "0.6.1"

[dependencies]
bitflags = "1.2.0"
bytes = "0.5.0"
Expand All @@ -24,7 +27,9 @@ linked-hash-map = "0.5.2"
linked_hash_set = "0.1.3"
log = "0.4.8"
lru = "0.4.0"
nohash-hasher = "0.1.3"
parking_lot = "0.10.0"
prost = "0.6.1"
rand = "0.7.2"
rustc-hex = "2.0.1"
sc-block-builder = { version = "0.8", path = "../block-builder" }
Expand All @@ -45,16 +50,21 @@ sp-keyring = { version = "2.0.0", optional = true, path = "../../primitives/keyr
sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" }
substrate-test-client = { version = "2.0.0", optional = true, path = "../../test-utils/client" }
substrate-test-runtime-client = { version = "2.0.0", optional = true, path = "../../test-utils/runtime/client" }
thiserror = "1"
unsigned-varint = { version = "0.3.0", features = ["codec"] }
void = "1.0.2"
zeroize = "1.0.0"

[dev-dependencies]
async-std = "1.5"
assert_matches = "1.3"
env_logger = "0.7.0"
quickcheck = "0.9.0"
rand = "0.7.2"
sp-keyring = { version = "2.0.0", path = "../../primitives/keyring" }
sp-test-primitives = { version = "2.0.0", path = "../../primitives/test-primitives" }
substrate-test-runtime = { version = "2.0.0", path = "../../test-utils/runtime" }
substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" }
tempfile = "3.1.0"

[features]
Expand Down
8 changes: 8 additions & 0 deletions client/network/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const PROTOS: &[&str] = &[
"src/protocol/schema/api.v1.proto",
"src/protocol/schema/light.v1.proto"
];

fn main() {
prost_build::compile_protos(PROTOS, &["src/protocol"]).unwrap();
}
19 changes: 16 additions & 3 deletions client/network/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
Event, protocol::event::DhtEvent
};
use crate::{ExHashT, specialization::NetworkSpecialization};
use crate::protocol::{CustomMessageOutcome, Protocol};
use crate::protocol::{self, light_client_handler, CustomMessageOutcome, Protocol};
use libp2p::NetworkBehaviour;
use libp2p::core::{Multiaddr, PeerId, PublicKey};
use libp2p::kad::record;
Expand All @@ -42,7 +42,10 @@ pub struct Behaviour<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> {
debug_info: debug_info::DebugInfoBehaviour<Substream<StreamMuxerBox>>,
/// Discovers nodes of the network.
discovery: DiscoveryBehaviour<Substream<StreamMuxerBox>>,

/// Block request handling.
block_requests: protocol::BlockRequests<Substream<StreamMuxerBox>, B>,
/// Light client request handling.
light_client_handler: protocol::LightClientHandler<Substream<StreamMuxerBox>, B>,
/// Queue of events to produce for the outside.
#[behaviour(ignore)]
events: Vec<BehaviourOut<B>>,
Expand All @@ -66,6 +69,8 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Behaviour<B, S, H> {
enable_mdns: bool,
allow_private_ipv4: bool,
discovery_only_if_under_num: u64,
block_requests: protocol::BlockRequests<Substream<StreamMuxerBox>, B>,
light_client_handler: protocol::LightClientHandler<Substream<StreamMuxerBox>, B>,
) -> Self {
Behaviour {
substrate,
Expand All @@ -77,7 +82,9 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Behaviour<B, S, H> {
allow_private_ipv4,
discovery_only_if_under_num,
).await,
events: Vec::new(),
block_requests,
light_client_handler,
events: Vec::new()
}
}

Expand Down Expand Up @@ -119,6 +126,12 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Behaviour<B, S, H> {
pub fn put_value(&mut self, key: record::Key, value: Vec<u8>) {
self.discovery.put_value(key, value);
}

/// Issue a light client request.
#[allow(unused)]
pub fn light_client_request(&mut self, r: light_client_handler::Request<B>) -> Result<(), light_client_handler::Error> {
self.light_client_handler.request(r)
}
}

impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> NetworkBehaviourEventProcess<void::Void> for
Expand Down
15 changes: 15 additions & 0 deletions client/network/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading