Networking bugfixes

This commit is contained in:
arkpar 2016-01-23 02:36:58 +01:00
parent acbb50d700
commit cfb8671b00
8 changed files with 56 additions and 36 deletions

@ -1 +1 @@
Subproject commit dc86e6359675440aea59ddb48648a01c799925d8 Subproject commit e838fd90998fc5502d0b7c9427a4c231f9a6953d

View File

@ -30,7 +30,13 @@ fn setup_log() {
fn main() { fn main() {
setup_log(); setup_log();
let spec = ethereum::new_frontier(); let spec = ethereum::new_frontier();
let mut service = ClientService::start(spec).unwrap(); let mut net_settings = NetworkConfiguration::new();
let args: Vec<_> = env::args().collect();
if args.len() == 2 {
net_settings.boot_nodes.push(args[1].trim_matches('\"').to_string());
}
let mut service = ClientService::start(spec, net_settings).unwrap();
let io_handler = Arc::new(ClientIoHandler { client: service.client(), info: Default::default(), sync: service.sync() }); let io_handler = Arc::new(ClientIoHandler { client: service.client(), info: Default::default(), sync: service.sync() });
service.io().register_handler(io_handler).expect("Error registering IO handler"); service.io().register_handler(io_handler).expect("Error registering IO handler");

View File

@ -162,14 +162,10 @@ impl Client {
let db = Arc::new(DB::open(&opts, state_path.to_str().unwrap()).unwrap()); let db = Arc::new(DB::open(&opts, state_path.to_str().unwrap()).unwrap());
let engine = Arc::new(try!(spec.to_engine())); let engine = Arc::new(try!(spec.to_engine()));
{
let mut state_db = JournalDB::new_with_arc(db.clone()); let mut state_db = JournalDB::new_with_arc(db.clone());
if engine.spec().ensure_db_good(&mut state_db) { if engine.spec().ensure_db_good(&mut state_db) {
state_db.commit(0, &engine.spec().genesis_header().hash(), None).expect("Error commiting genesis state to state DB"); state_db.commit(0, &engine.spec().genesis_header().hash(), None).expect("Error commiting genesis state to state DB");
} }
}
let state_db = JournalDB::new_with_arc(db);
Ok(Arc::new(Client { Ok(Arc::new(Client {
chain: chain, chain: chain,
engine: engine.clone(), engine: engine.clone(),

View File

@ -26,8 +26,8 @@ pub struct ClientService {
impl ClientService { impl ClientService {
/// Start the service in a separate thread. /// Start the service in a separate thread.
pub fn start(spec: Spec) -> Result<ClientService, Error> { pub fn start(spec: Spec, net_config: NetworkConfiguration) -> Result<ClientService, Error> {
let mut net_service = try!(NetworkService::start()); let mut net_service = try!(NetworkService::start(net_config));
info!("Starting {}", net_service.host_info()); info!("Starting {}", net_service.host_info());
info!("Configured for {} using {} engine", spec.name, spec.engine_name); info!("Configured for {} using {} engine", spec.name, spec.engine_name);
let mut dir = env::home_dir().unwrap(); let mut dir = env::home_dir().unwrap();

View File

@ -137,8 +137,8 @@ impl Connection {
pub fn register_socket<Host: Handler>(&self, reg: Token, event_loop: &mut EventLoop<Host>) -> io::Result<()> { pub fn register_socket<Host: Handler>(&self, reg: Token, event_loop: &mut EventLoop<Host>) -> io::Result<()> {
trace!(target: "net", "connection register; token={:?}", reg); trace!(target: "net", "connection register; token={:?}", reg);
event_loop.register(&self.socket, reg, self.interest, PollOpt::edge() | PollOpt::oneshot()).or_else(|e| { event_loop.register(&self.socket, reg, self.interest, PollOpt::edge() | PollOpt::oneshot()).or_else(|e| {
error!("Failed to register {:?}, {:?}", reg, e); debug!("Failed to register {:?}, {:?}", reg, e);
Err(e) Ok(())
}) })
} }
@ -146,8 +146,8 @@ impl Connection {
pub fn update_socket<Host: Handler>(&self, reg: Token, event_loop: &mut EventLoop<Host>) -> io::Result<()> { pub fn update_socket<Host: Handler>(&self, reg: Token, event_loop: &mut EventLoop<Host>) -> io::Result<()> {
trace!(target: "net", "connection reregister; token={:?}", reg); trace!(target: "net", "connection reregister; token={:?}", reg);
event_loop.reregister( &self.socket, reg, self.interest, PollOpt::edge() | PollOpt::oneshot()).or_else(|e| { event_loop.reregister( &self.socket, reg, self.interest, PollOpt::edge() | PollOpt::oneshot()).or_else(|e| {
error!("Failed to reregister {:?}, {:?}", reg, e); debug!("Failed to reregister {:?}, {:?}", reg, e);
Err(e) Ok(())
}) })
} }

View File

@ -28,22 +28,32 @@ const IDEAL_PEERS: u32 = 10;
const MAINTENANCE_TIMEOUT: u64 = 1000; const MAINTENANCE_TIMEOUT: u64 = 1000;
#[derive(Debug)] #[derive(Debug)]
struct NetworkConfiguration { /// Network service configuration
listen_address: SocketAddr, pub struct NetworkConfiguration {
public_address: SocketAddr, /// IP address to listen for incoming connections
nat_enabled: bool, pub listen_address: SocketAddr,
discovery_enabled: bool, /// IP address to advertise
pin: bool, pub public_address: SocketAddr,
/// Enable NAT configuration
pub nat_enabled: bool,
/// Enable discovery
pub discovery_enabled: bool,
/// Pin to boot nodes only
pub pin: bool,
/// List of initial node addresses
pub boot_nodes: Vec<String>,
} }
impl NetworkConfiguration { impl NetworkConfiguration {
fn new() -> NetworkConfiguration { /// Create a new instance of default settings.
pub fn new() -> NetworkConfiguration {
NetworkConfiguration { NetworkConfiguration {
listen_address: SocketAddr::from_str("0.0.0.0:30304").unwrap(), listen_address: SocketAddr::from_str("0.0.0.0:30304").unwrap(),
public_address: SocketAddr::from_str("0.0.0.0:30304").unwrap(), public_address: SocketAddr::from_str("0.0.0.0:30304").unwrap(),
nat_enabled: true, nat_enabled: true,
discovery_enabled: true, discovery_enabled: true,
pin: false, pin: false,
boot_nodes: Vec::new(),
} }
} }
} }
@ -246,8 +256,8 @@ pub struct Host<Message> where Message: Send + Sync + Clone {
} }
impl<Message> Host<Message> where Message: Send + Sync + Clone { impl<Message> Host<Message> where Message: Send + Sync + Clone {
pub fn new() -> Host<Message> { /// Create a new instance
let config = NetworkConfiguration::new(); pub fn new(config: NetworkConfiguration) -> Host<Message> {
let addr = config.listen_address; let addr = config.listen_address;
// Setup the server socket // Setup the server socket
let tcp_listener = TcpListener::bind(&addr).unwrap(); let tcp_listener = TcpListener::bind(&addr).unwrap();
@ -279,13 +289,19 @@ impl<Message> Host<Message> where Message: Send + Sync + Clone {
None => warn!("No public network interface"), None => warn!("No public network interface"),
*/ */
// self.add_node("enode://a9a921de2ff09a9a4d38b623c67b2d6b477a8e654ae95d874750cbbcb31b33296496a7b4421934e2629269e180823e52c15c2b19fc59592ec51ffe4f2de76ed7@127.0.0.1:30303"); let boot_nodes = host.info.read().unwrap().config.boot_nodes.clone();
if boot_nodes.is_empty() {
// GO bootnodes // GO bootnodes
host.add_node("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@52.16.188.185:30303"); // IE host.add_node("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@52.16.188.185:30303"); // IE
host.add_node("enode://de471bccee3d042261d52e9bff31458daecc406142b401d4cd848f677479f73104b9fdeb090af9583d3391b7f10cb2ba9e26865dd5fca4fcdc0fb1e3b723c786@54.94.239.50:30303"); // BR host.add_node("enode://de471bccee3d042261d52e9bff31458daecc406142b401d4cd848f677479f73104b9fdeb090af9583d3391b7f10cb2ba9e26865dd5fca4fcdc0fb1e3b723c786@54.94.239.50:30303"); // BR
host.add_node("enode://1118980bf48b0a3640bdba04e0fe78b1add18e1cd99bf22d53daac1fd9972ad650df52176e7c7d89d1114cfef2bc23a2959aa54998a46afcf7d91809f0855082@52.74.57.123:30303"); // SG host.add_node("enode://1118980bf48b0a3640bdba04e0fe78b1add18e1cd99bf22d53daac1fd9972ad650df52176e7c7d89d1114cfef2bc23a2959aa54998a46afcf7d91809f0855082@52.74.57.123:30303"); // SG
}
else {
for n in boot_nodes {
host.add_node(&n);
}
}
// ETH/DEV cpp-ethereum (poc-9.ethdev.com) // ETH/DEV cpp-ethereum (poc-9.ethdev.com)
//host.add_node("enode://979b7fa28feeb35a4741660a16076f1943202cb72b6af70d327f053e248bab9ba81760f39d0701ef1d8f89cc1fbd2cacba0710a12cd5314d5e0c9021aa3637f9@5.1.83.226:30303");
host host
} }
@ -517,7 +533,8 @@ impl<Message> Host<Message> where Message: Send + Sync + Clone {
} }
fn start_session(&self, token: StreamToken, io: &IoContext<NetworkIoMessage<Message>>) { fn start_session(&self, token: StreamToken, io: &IoContext<NetworkIoMessage<Message>>) {
self.connections.write().unwrap().replace_with(token, |c| { let mut connections = self.connections.write().unwrap();
connections.replace_with(token, |c| {
match Arc::try_unwrap(c).ok().unwrap().into_inner().unwrap() { match Arc::try_unwrap(c).ok().unwrap().into_inner().unwrap() {
ConnectionEntry::Handshake(h) => { ConnectionEntry::Handshake(h) => {
let session = Session::new(h, io, &self.info.read().unwrap()).expect("Session creation error"); let session = Session::new(h, io, &self.info.read().unwrap()).expect("Session creation error");

View File

@ -40,7 +40,7 @@
/// } /// }
/// ///
/// fn main () { /// fn main () {
/// let mut service = NetworkService::<MyMessage>::start().expect("Error creating network service"); /// let mut service = NetworkService::<MyMessage>::start(NetworkConfiguration::new()).expect("Error creating network service");
/// service.register_protocol(Arc::new(MyHandler), "myproto", &[1u8]); /// service.register_protocol(Arc::new(MyHandler), "myproto", &[1u8]);
/// ///
/// // Wait for quit condition /// // Wait for quit condition
@ -71,6 +71,7 @@ pub use network::host::NetworkIoMessage;
pub use network::host::NetworkIoMessage::User as UserMessage; pub use network::host::NetworkIoMessage::User as UserMessage;
/// TODO [arkpar] Please document me /// TODO [arkpar] Please document me
pub use network::error::NetworkError; pub use network::error::NetworkError;
pub use network::host::NetworkConfiguration;
use io::TimerToken; use io::TimerToken;
@ -130,6 +131,6 @@ fn test_net_service() {
} }
} }
let mut service = NetworkService::<MyMessage>::start().expect("Error creating network service"); let mut service = NetworkService::<MyMessage>::start(NetworkConfiguration::new()).expect("Error creating network service");
service.register_protocol(Arc::new(MyHandler), "myproto", &[1u8]).unwrap(); service.register_protocol(Arc::new(MyHandler), "myproto", &[1u8]).unwrap();
} }

View File

@ -1,6 +1,6 @@
use std::sync::*; use std::sync::*;
use error::*; use error::*;
use network::{NetworkProtocolHandler}; use network::{NetworkProtocolHandler, NetworkConfiguration};
use network::error::{NetworkError}; use network::error::{NetworkError};
use network::host::{Host, NetworkIoMessage, ProtocolId}; use network::host::{Host, NetworkIoMessage, ProtocolId};
use io::*; use io::*;
@ -14,9 +14,9 @@ pub struct NetworkService<Message> where Message: Send + Sync + Clone + 'static
impl<Message> NetworkService<Message> where Message: Send + Sync + Clone + 'static { impl<Message> NetworkService<Message> where Message: Send + Sync + Clone + 'static {
/// Starts IO event loop /// Starts IO event loop
pub fn start() -> Result<NetworkService<Message>, UtilError> { pub fn start(config: NetworkConfiguration) -> Result<NetworkService<Message>, UtilError> {
let mut io_service = try!(IoService::<NetworkIoMessage<Message>>::start()); let mut io_service = try!(IoService::<NetworkIoMessage<Message>>::start());
let host = Arc::new(Host::new()); let host = Arc::new(Host::new(config));
let host_info = host.client_version(); let host_info = host.client_version();
info!("NetworkService::start(): id={:?}", host.client_id()); info!("NetworkService::start(): id={:?}", host.client_id());
try!(io_service.register_handler(host)); try!(io_service.register_handler(host));