improved p2p interface

This commit is contained in:
debris 2016-10-04 12:33:23 +02:00
parent 742e9a6fff
commit ffcb4d5a20
9 changed files with 203 additions and 178 deletions

2
Cargo.lock generated
View File

@ -4,12 +4,10 @@ version = "0.1.0"
dependencies = [
"bitcrypto 0.1.0",
"clap 2.13.0 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"keys 0.1.0",
"message 0.1.0",
"p2p 0.1.0",
"script 0.1.0",
"tokio-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]

View File

@ -7,8 +7,6 @@ description = "Parity bitcoin client."
[dependencies]
clap = { version = "2", features = ["yaml"] }
tokio-core = "0.1"
futures = "0.1"
bitcrypto = { path = "crypto" }
keys = { path = "keys" }

12
p2p/src/config.rs Normal file
View File

@ -0,0 +1,12 @@
use std::net::IpAddr;
use net::Config as NetConfig;
#[derive(Debug)]
pub struct Config {
/// Configuration for every connection.
pub connection: NetConfig,
/// Connect to these nodes to retrieve peer addresses, and disconnect.
pub seednodes: Vec<IpAddr>,
/// Connect only ot these nodes.
pub limited_connect: Option<Vec<IpAddr>>,
}

5
p2p/src/event_loop.rs Normal file
View File

@ -0,0 +1,5 @@
use tokio_core::reactor::Core;
pub fn event_loop() -> Core {
Core::new().unwrap()
}

View File

@ -9,16 +9,22 @@ extern crate message;
extern crate primitives;
extern crate serialization as ser;
mod error;
pub mod io;
pub mod net;
pub mod tcp;
pub mod util;
mod config;
mod error;
mod event_loop;
mod run;
pub const VERSION: u32 = 70_001;
pub const USER_AGENT: &'static str = "pbtc";
pub use primitives::bytes;
pub use config::Config;
pub use error::Error;
pub use event_loop::event_loop;
pub use run::run;

32
p2p/src/run.rs Normal file
View File

@ -0,0 +1,32 @@
use std::thread;
use std::net::SocketAddr;
use futures::{Future, BoxFuture};
use futures::stream::Stream;
use tokio_core::reactor::Handle;
use net::{connect, listen};
use {Config, Error};
pub fn run(config: Config, handle: &Handle) -> Result<BoxFuture<(), Error>, Error> {
for seednode in config.seednodes.clone().into_iter() {
let socket = SocketAddr::new(seednode, config.connection.magic.port());
let connection = connect(&socket, &handle, &config.connection);
thread::spawn(move || {
match connection.wait() {
Ok(_connection) => {
println!("Connected to seednode {}", seednode);
},
Err(err) => {
println!("Connection failed {:?}", err);
}
}
});
}
let listen = try!(listen(&handle, config.connection));
let server = listen.for_each(|connection| {
println!("new connection: {:?}", connection.handshake_result);
Ok(())
}).boxed();
Ok(server)
}

View File

@ -2,8 +2,6 @@
#[macro_use]
extern crate clap;
extern crate tokio_core;
extern crate futures;
extern crate bitcrypto as crypto;
extern crate keys;
@ -13,15 +11,8 @@ extern crate p2p;
mod config;
use std::thread;
use std::net::SocketAddr;
use futures::Future;
use futures::stream::Stream;
use p2p::net::{Config as P2PConfig, connect, listen};
pub fn event_loop() -> tokio_core::reactor::Core {
tokio_core::reactor::Core::new().unwrap()
}
use p2p::{net, event_loop};
fn main() {
match run() {
@ -36,37 +27,22 @@ fn run() -> Result<(), String> {
let cfg = try!(config::parse(&matches));
let mut el = event_loop();
let handle = el.handle();
let p2p_cfg = P2PConfig {
magic: cfg.magic,
local_address: SocketAddr::new("127.0.0.1".parse().unwrap(), cfg.port),
services: Default::default(),
user_agent: "pbtc".into(),
start_height: 0,
relay: false,
let p2p_cfg = p2p::Config {
connection: net::Config {
magic: cfg.magic,
local_address: SocketAddr::new("127.0.0.1".parse().unwrap(), cfg.port),
services: Default::default(),
user_agent: "pbtc".into(),
start_height: 0,
relay: false,
},
seednodes: cfg.seednode.map_or_else(|| vec![], |x| vec![x]),
limited_connect: cfg.connect.map_or(None, |x| Some(vec![x])),
};
if let Some(ip) = cfg.connect {
let connection = connect(&SocketAddr::new(ip, cfg.magic.port()), &handle, &p2p_cfg);
thread::spawn(move || {
match connection.wait() {
Ok(connection) => {
println!("Connected to ip {}", ip);
},
Err(err) => {
println!("Connection failed {:?}", err);
}
}
});
}
let listen = try!(listen(&handle, p2p_cfg).map_err(|_| "Cannot start listening".to_owned()));
let server = listen.for_each(|connection| {
println!("new connection: {:?}", connection.handshake_result);
Ok(())
});
let handle = el.handle();
let server = try!(p2p::run(p2p_cfg, &handle).map_err(|_| "Failed to start p2p module"));
el.run(server).unwrap();
Ok(())
}

View File

@ -2,52 +2,52 @@ digraph dependencies {
N0[label="pbtc",shape=box];
N1[label="bitcrypto",shape=box];
N2[label="clap",shape=box];
N3[label="futures",shape=box];
N4[label="keys",shape=box];
N5[label="message",shape=box];
N6[label="p2p",shape=box];
N7[label="script",shape=box];
N8[label="tokio-core",shape=box];
N9[label="ansi_term",shape=box];
N10[label="arrayvec",shape=box];
N11[label="nodrop",shape=box];
N12[label="odds",shape=box];
N13[label="base58",shape=box];
N14[label="primitives",shape=box];
N15[label="rust-crypto",shape=box];
N16[label="bitflags v0.4.0",shape=box];
N17[label="bitflags v0.7.0",shape=box];
N18[label="byteorder",shape=box];
N19[label="cfg-if",shape=box];
N20[label="chain",shape=box];
N21[label="rustc-serialize",shape=box];
N22[label="serialization",shape=box];
N23[label="libc",shape=box];
N24[label="strsim",shape=box];
N25[label="term_size",shape=box];
N26[label="unicode-segmentation",shape=box];
N27[label="unicode-width",shape=box];
N28[label="vec_map",shape=box];
N29[label="yaml-rust",shape=box];
N30[label="eth-secp256k1",shape=box];
N31[label="gcc",shape=box];
N32[label="rand",shape=box];
N33[label="log",shape=box];
N34[label="kernel32-sys",shape=box];
N35[label="winapi",shape=box];
N36[label="winapi-build",shape=box];
N37[label="lazy_static",shape=box];
N38[label="lazycell",shape=box];
N39[label="mio",shape=box];
N40[label="miow",shape=box];
N41[label="net2",shape=box];
N42[label="nix",shape=box];
N43[label="slab",shape=box];
N44[label="ws2_32-sys",shape=box];
N45[label="rustc_version",shape=box];
N46[label="semver",shape=box];
N47[label="void",shape=box];
N48[label="time",shape=box];
N3[label="keys",shape=box];
N4[label="message",shape=box];
N5[label="p2p",shape=box];
N6[label="script",shape=box];
N7[label="ansi_term",shape=box];
N8[label="arrayvec",shape=box];
N9[label="nodrop",shape=box];
N10[label="odds",shape=box];
N11[label="base58",shape=box];
N12[label="primitives",shape=box];
N13[label="rust-crypto",shape=box];
N14[label="bitflags v0.4.0",shape=box];
N15[label="bitflags v0.7.0",shape=box];
N16[label="byteorder",shape=box];
N17[label="cfg-if",shape=box];
N18[label="chain",shape=box];
N19[label="rustc-serialize",shape=box];
N20[label="serialization",shape=box];
N21[label="libc",shape=box];
N22[label="strsim",shape=box];
N23[label="term_size",shape=box];
N24[label="unicode-segmentation",shape=box];
N25[label="unicode-width",shape=box];
N26[label="vec_map",shape=box];
N27[label="yaml-rust",shape=box];
N28[label="eth-secp256k1",shape=box];
N29[label="gcc",shape=box];
N30[label="rand",shape=box];
N31[label="futures",shape=box];
N32[label="log",shape=box];
N33[label="kernel32-sys",shape=box];
N34[label="winapi",shape=box];
N35[label="winapi-build",shape=box];
N36[label="lazy_static",shape=box];
N37[label="lazycell",shape=box];
N38[label="mio",shape=box];
N39[label="miow",shape=box];
N40[label="net2",shape=box];
N41[label="nix",shape=box];
N42[label="slab",shape=box];
N43[label="ws2_32-sys",shape=box];
N44[label="rustc_version",shape=box];
N45[label="semver",shape=box];
N46[label="void",shape=box];
N47[label="time",shape=box];
N48[label="tokio-core",shape=box];
N49[label="scoped-tls",shape=box];
N0 -> N1[label="",style=dashed];
N0 -> N2[label="",style=dashed];
@ -55,104 +55,102 @@ digraph dependencies {
N0 -> N4[label="",style=dashed];
N0 -> N5[label="",style=dashed];
N0 -> N6[label="",style=dashed];
N0 -> N7[label="",style=dashed];
N0 -> N8[label="",style=dashed];
N1 -> N14[label="",style=dashed];
N1 -> N15[label="",style=dashed];
N2 -> N9[label="",style=dashed];
N2 -> N17[label="",style=dashed];
N1 -> N12[label="",style=dashed];
N1 -> N13[label="",style=dashed];
N2 -> N7[label="",style=dashed];
N2 -> N15[label="",style=dashed];
N2 -> N21[label="",style=dashed];
N2 -> N22[label="",style=dashed];
N2 -> N23[label="",style=dashed];
N2 -> N24[label="",style=dashed];
N2 -> N25[label="",style=dashed];
N2 -> N26[label="",style=dashed];
N2 -> N27[label="",style=dashed];
N2 -> N28[label="",style=dashed];
N2 -> N29[label="",style=dashed];
N3 -> N33[label="",style=dashed];
N3 -> N1[label="",style=dashed];
N3 -> N11[label="",style=dashed];
N3 -> N12[label="",style=dashed];
N3 -> N19[label="",style=dashed];
N3 -> N28[label="",style=dashed];
N3 -> N30[label="",style=dashed];
N3 -> N36[label="",style=dashed];
N4 -> N1[label="",style=dashed];
N4 -> N13[label="",style=dashed];
N4 -> N14[label="",style=dashed];
N4 -> N21[label="",style=dashed];
N4 -> N30[label="",style=dashed];
N4 -> N32[label="",style=dashed];
N4 -> N37[label="",style=dashed];
N4 -> N12[label="",style=dashed];
N4 -> N16[label="",style=dashed];
N4 -> N18[label="",style=dashed];
N4 -> N20[label="",style=dashed];
N5 -> N1[label="",style=dashed];
N5 -> N14[label="",style=dashed];
N5 -> N18[label="",style=dashed];
N5 -> N4[label="",style=dashed];
N5 -> N12[label="",style=dashed];
N5 -> N20[label="",style=dashed];
N5 -> N22[label="",style=dashed];
N5 -> N30[label="",style=dashed];
N5 -> N31[label="",style=dashed];
N5 -> N47[label="",style=dashed];
N5 -> N48[label="",style=dashed];
N6 -> N1[label="",style=dashed];
N6 -> N3[label="",style=dashed];
N6 -> N5[label="",style=dashed];
N6 -> N8[label="",style=dashed];
N6 -> N14[label="",style=dashed];
N6 -> N22[label="",style=dashed];
N6 -> N32[label="",style=dashed];
N6 -> N48[label="",style=dashed];
N7 -> N1[label="",style=dashed];
N7 -> N4[label="",style=dashed];
N7 -> N14[label="",style=dashed];
N7 -> N20[label="",style=dashed];
N7 -> N22[label="",style=dashed];
N8 -> N3[label="",style=dashed];
N8 -> N33[label="",style=dashed];
N8 -> N39[label="",style=dashed];
N8 -> N43[label="",style=dashed];
N8 -> N49[label="",style=dashed];
N10 -> N11[label=""];
N10 -> N12[label=""];
N11 -> N12[label=""];
N14 -> N21[label="",style=dashed];
N15 -> N21[label="",style=dashed];
N15 -> N23[label="",style=dashed];
N15 -> N31[label="",style=dashed];
N15 -> N32[label="",style=dashed];
N15 -> N48[label="",style=dashed];
N20 -> N1[label="",style=dashed];
N20 -> N14[label="",style=dashed];
N20 -> N21[label="",style=dashed];
N20 -> N22[label="",style=dashed];
N22 -> N14[label="",style=dashed];
N22 -> N18[label="",style=dashed];
N25 -> N23[label="",style=dashed];
N25 -> N34[label="",style=dashed];
N25 -> N35[label="",style=dashed];
N30 -> N10[label="",style=dashed];
N6 -> N12[label="",style=dashed];
N6 -> N18[label="",style=dashed];
N6 -> N20[label="",style=dashed];
N8 -> N9[label=""];
N8 -> N10[label=""];
N9 -> N10[label=""];
N12 -> N19[label="",style=dashed];
N13 -> N19[label="",style=dashed];
N13 -> N21[label="",style=dashed];
N13 -> N29[label="",style=dashed];
N13 -> N30[label="",style=dashed];
N13 -> N47[label="",style=dashed];
N18 -> N1[label="",style=dashed];
N18 -> N12[label="",style=dashed];
N18 -> N19[label="",style=dashed];
N18 -> N20[label="",style=dashed];
N20 -> N12[label="",style=dashed];
N20 -> N16[label="",style=dashed];
N23 -> N21[label="",style=dashed];
N23 -> N33[label="",style=dashed];
N23 -> N34[label="",style=dashed];
N28 -> N8[label="",style=dashed];
N28 -> N19[label="",style=dashed];
N28 -> N21[label="",style=dashed];
N28 -> N29[label="",style=dashed];
N28 -> N30[label="",style=dashed];
N30 -> N21[label="",style=dashed];
N30 -> N23[label="",style=dashed];
N30 -> N31[label="",style=dashed];
N30 -> N32[label="",style=dashed];
N32 -> N23[label="",style=dashed];
N34 -> N35[label="",style=dashed];
N34 -> N36[label="",style=dashed];
N39 -> N23[label="",style=dashed];
N39 -> N33[label="",style=dashed];
N39 -> N34[label="",style=dashed];
N39 -> N35[label="",style=dashed];
N39 -> N38[label="",style=dashed];
N39 -> N40[label="",style=dashed];
N39 -> N41[label="",style=dashed];
N39 -> N42[label="",style=dashed];
N39 -> N43[label="",style=dashed];
N40 -> N34[label="",style=dashed];
N40 -> N35[label="",style=dashed];
N40 -> N41[label="",style=dashed];
N40 -> N44[label="",style=dashed];
N41 -> N19[label="",style=dashed];
N41 -> N23[label="",style=dashed];
N41 -> N34[label="",style=dashed];
N41 -> N35[label="",style=dashed];
N41 -> N44[label="",style=dashed];
N42 -> N16[label="",style=dashed];
N42 -> N19[label="",style=dashed];
N42 -> N23[label="",style=dashed];
N42 -> N45[label="",style=dashed];
N42 -> N46[label="",style=dashed];
N42 -> N47[label="",style=dashed];
N44 -> N35[label="",style=dashed];
N44 -> N36[label="",style=dashed];
N45 -> N46[label="",style=dashed];
N48 -> N23[label="",style=dashed];
N48 -> N34[label="",style=dashed];
N48 -> N35[label="",style=dashed];
N31 -> N32[label="",style=dashed];
N33 -> N34[label="",style=dashed];
N33 -> N35[label="",style=dashed];
N38 -> N21[label="",style=dashed];
N38 -> N32[label="",style=dashed];
N38 -> N33[label="",style=dashed];
N38 -> N34[label="",style=dashed];
N38 -> N37[label=""];
N38 -> N39[label=""];
N38 -> N40[label=""];
N38 -> N41[label=""];
N38 -> N42[label="",style=dashed];
N39 -> N33[label=""];
N39 -> N34[label=""];
N39 -> N40[label=""];
N39 -> N43[label=""];
N40 -> N17[label=""];
N40 -> N21[label=""];
N40 -> N33[label=""];
N40 -> N34[label=""];
N40 -> N43[label=""];
N41 -> N14[label=""];
N41 -> N17[label=""];
N41 -> N21[label=""];
N41 -> N44[label=""];
N41 -> N45[label=""];
N41 -> N46[label=""];
N43 -> N34[label=""];
N43 -> N35[label=""];
N44 -> N45[label=""];
N47 -> N21[label="",style=dashed];
N47 -> N33[label="",style=dashed];
N47 -> N34[label="",style=dashed];
N48 -> N31[label="",style=dashed];
N48 -> N32[label="",style=dashed];
N48 -> N38[label="",style=dashed];
N48 -> N42[label="",style=dashed];
N48 -> N49[label="",style=dashed];
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 268 KiB

After

Width:  |  Height:  |  Size: 260 KiB