parity-zcash/pbtc/commands/start.rs

53 lines
1.7 KiB
Rust
Raw Normal View History

2016-11-03 09:19:35 -07:00
use std::net::SocketAddr;
2016-12-07 11:39:12 -08:00
use sync::{create_local_sync_node, create_sync_connection_factory};
2016-11-05 07:32:57 -07:00
use message::Services;
use util::{open_db, init_db, node_table_path};
use {config, p2p, PROTOCOL_VERSION, PROTOCOL_MINIMUM};
2016-12-07 05:14:52 -08:00
use super::super::rpc;
2016-11-03 09:19:35 -07:00
pub fn start(cfg: config::Config) -> Result<(), String> {
let mut el = p2p::event_loop();
let db = open_db(&cfg);
try!(init_db(&cfg, &db));
2016-11-24 22:58:04 -08:00
let nodes_path = node_table_path(&cfg);
2016-11-03 09:19:35 -07:00
let p2p_cfg = p2p::Config {
threads: cfg.p2p_threads,
inbound_connections: cfg.inbound_connections,
outbound_connections: cfg.outbound_connections,
2016-11-03 09:19:35 -07:00
connection: p2p::NetConfig {
protocol_version: PROTOCOL_VERSION,
protocol_minimum: PROTOCOL_MINIMUM,
2016-11-03 09:19:35 -07:00
magic: cfg.magic,
local_address: SocketAddr::new("127.0.0.1".parse().unwrap(), cfg.port),
2016-11-05 07:32:57 -07:00
services: Services::default().with_network(true),
user_agent: cfg.user_agent,
2016-11-03 09:19:35 -07:00
start_height: 0,
relay: true,
2016-11-03 09:19:35 -07:00
},
peers: cfg.connect.map_or_else(|| vec![], |x| vec![x]),
2016-11-30 05:37:17 -08:00
seeds: cfg.seednodes,
2016-11-24 22:58:04 -08:00
node_table_path: nodes_path,
internet_protocol: cfg.internet_protocol,
2016-11-03 09:19:35 -07:00
};
2016-11-04 02:36:58 -07:00
let sync_handle = el.handle();
2016-12-12 10:49:01 -08:00
let local_sync_node = create_local_sync_node(&sync_handle, cfg.magic, db.clone());
2016-12-07 11:39:12 -08:00
let sync_connection_factory = create_sync_connection_factory(local_sync_node.clone());
2016-11-03 09:19:35 -07:00
2016-12-12 10:18:43 -08:00
let p2p = try!(p2p::P2P::new(p2p_cfg, sync_connection_factory, el.handle()).map_err(|x| x.to_string()));
2016-12-07 11:39:12 -08:00
let rpc_deps = rpc::Dependencies {
2016-12-12 10:49:01 -08:00
network: cfg.magic,
storage: db,
2016-12-07 11:39:12 -08:00
local_sync_node: local_sync_node,
2016-12-12 10:18:43 -08:00
p2p_context: p2p.context().clone(),
2016-12-07 11:39:12 -08:00
};
let _rpc_server = try!(rpc::new_http(cfg.rpc_config, rpc_deps));
2016-12-07 05:14:52 -08:00
2016-11-03 09:19:35 -07:00
try!(p2p.run().map_err(|_| "Failed to start p2p module"));
el.run(p2p::forever()).unwrap();
Ok(())
}