Skip to content
Snippets Groups Projects
Commit a1b2b76d authored by Éloïs's avatar Éloïs
Browse files

[fix] ws2pv1: request current blockstamp & improve ssl condition

parent dae267df
No related branches found
No related tags found
1 merge request!109Resolve "Fork resolution algorithm"
cyclomatic-complexity-threshold = 76 cyclomatic-complexity-threshold = 77
\ No newline at end of file \ No newline at end of file
...@@ -32,6 +32,7 @@ pub struct WS2PModuleDatas { ...@@ -32,6 +32,7 @@ pub struct WS2PModuleDatas {
pub currency: Option<String>, pub currency: Option<String>,
pub key_pair: Option<KeyPairEnum>, pub key_pair: Option<KeyPairEnum>,
pub conf: WS2PConf, pub conf: WS2PConf,
pub ssl: bool,
pub node_id: NodeId, pub node_id: NodeId,
pub main_thread_channel: ( pub main_thread_channel: (
mpsc::Sender<WS2PThreadSignal>, mpsc::Sender<WS2PThreadSignal>,
...@@ -47,7 +48,35 @@ pub struct WS2PModuleDatas { ...@@ -47,7 +48,35 @@ pub struct WS2PModuleDatas {
pub count_dal_requests: u32, pub count_dal_requests: u32,
} }
#[inline]
#[cfg(not(feature = "ssl"))]
fn ssl() -> bool { false }
#[cfg(feature = "ssl")]
fn ssl() -> bool { true }
impl WS2PModuleDatas { impl WS2PModuleDatas {
pub fn new(
router_sender: mpsc::Sender<RouterThreadMessage<DursMsg>>,
conf: WS2PConf,
node_id: NodeId,
) -> WS2PModuleDatas {
WS2PModuleDatas {
router_sender,
key_pair: None,
currency: None,
conf,
ssl: ssl(),
node_id,
main_thread_channel: mpsc::channel(),
ws2p_endpoints: HashMap::new(),
websockets: HashMap::new(),
requests_awaiting_response: HashMap::new(),
heads_cache: HashMap::new(),
my_head: None,
uids_cache: HashMap::new(),
count_dal_requests: 0,
}
}
pub fn open_db(db_path: &PathBuf) -> Result<sqlite::Connection, sqlite::Error> { pub fn open_db(db_path: &PathBuf) -> Result<sqlite::Connection, sqlite::Error> {
let conn: sqlite::Connection; let conn: sqlite::Connection;
if !db_path.as_path().exists() { if !db_path.as_path().exists() {
...@@ -201,12 +230,13 @@ impl WS2PModuleDatas { ...@@ -201,12 +230,13 @@ impl WS2PModuleDatas {
} else { } else {
break; break;
}; };
if cfg!(feature = "ssl") || ep.port != 443 { if !self.ssl && ep.port == 443 {
continue;
}
self.connect_to_without_checking_quotas(&ep); self.connect_to_without_checking_quotas(&ep);
free_outcoming_rooms -= 1; free_outcoming_rooms -= 1;
} }
} }
}
pub fn connect_to(&mut self, endpoint: &EndpointV1) { pub fn connect_to(&mut self, endpoint: &EndpointV1) {
// Add endpoint to endpoints list (if there isn't already) // Add endpoint to endpoints list (if there isn't already)
match self.ws2p_endpoints.get( match self.ws2p_endpoints.get(
......
...@@ -292,21 +292,11 @@ impl DursModule<DuRsConf, DursMsg> for WS2PModule { ...@@ -292,21 +292,11 @@ impl DursModule<DuRsConf, DursMsg> for WS2PModule {
let start_time = SystemTime::now(); let start_time = SystemTime::now();
// Define WS2PModuleDatas // Define WS2PModuleDatas
let mut ws2p_module = WS2PModuleDatas { let mut ws2p_module = WS2PModuleDatas::new(
router_sender: router_sender.clone(), router_sender.clone(),
key_pair: None,
currency: None,
conf, conf,
node_id: NodeId(soft_meta_datas.conf.my_node_id()), NodeId(soft_meta_datas.conf.my_node_id()),
main_thread_channel: mpsc::channel(), );
ws2p_endpoints: HashMap::new(),
websockets: HashMap::new(),
requests_awaiting_response: HashMap::new(),
heads_cache: HashMap::new(),
my_head: None,
uids_cache: HashMap::new(),
count_dal_requests: 0,
};
// load conf // load conf
let key_pair = match keys { let key_pair = match keys {
...@@ -314,7 +304,7 @@ impl DursModule<DuRsConf, DursMsg> for WS2PModule { ...@@ -314,7 +304,7 @@ impl DursModule<DuRsConf, DursMsg> for WS2PModule {
_ => panic!("WS2PModule fatal error at load_conf() : keys != NetworkKeyPair"), _ => panic!("WS2PModule fatal error at load_conf() : keys != NetworkKeyPair"),
}; };
let mut ws2p_endpoints = HashMap::new(); let mut ws2p_endpoints = HashMap::new();
for ep in ws2p_module.conf.sync_endpoints.clone() { for ep in &ws2p_module.conf.sync_endpoints {
ws2p_endpoints.insert( ws2p_endpoints.insert(
ep.node_full_id() ep.node_full_id()
.expect("Fail to get endpoint node_full_id"), .expect("Fail to get endpoint node_full_id"),
...@@ -411,6 +401,9 @@ impl DursModule<DuRsConf, DursMsg> for WS2PModule { ...@@ -411,6 +401,9 @@ impl DursModule<DuRsConf, DursMsg> for WS2PModule {
let mut current_blockstamp = Blockstamp::default(); let mut current_blockstamp = Blockstamp::default();
let mut next_receiver = 0; let mut next_receiver = 0;
// Request current blockstamp
ws2p_module.send_dal_request(&BlockchainRequest::CurrentBlockstamp());
// Start // Start
ws2p_module.connect_to_know_endpoints(); ws2p_module.connect_to_know_endpoints();
loop { loop {
...@@ -547,14 +540,12 @@ impl DursModule<DuRsConf, DursMsg> for WS2PModule { ...@@ -547,14 +540,12 @@ impl DursModule<DuRsConf, DursMsg> for WS2PModule {
if let DursResContent::BlockchainResponse(ref bc_res) = *res_content if let DursResContent::BlockchainResponse(ref bc_res) = *res_content
{ {
match *bc_res.deref() { match *bc_res.deref() {
BlockchainResponse::CurrentBlock( BlockchainResponse::CurrentBlockstamp(
ref _requester_full_id, ref _requester_id,
ref current_block,
ref current_blockstamp_, ref current_blockstamp_,
) => { ) => {
let _current_block = current_block.deref();
debug!( debug!(
"WS2PModule : receive DALResBc::CurrentBlock({})", "WS2PModule : receive DALResBc::CurrentBlockstamp({})",
current_blockstamp current_blockstamp
); );
current_blockstamp = *current_blockstamp_; current_blockstamp = *current_blockstamp_;
...@@ -753,8 +744,8 @@ impl DursModule<DuRsConf, DursMsg> for WS2PModule { ...@@ -753,8 +744,8 @@ impl DursModule<DuRsConf, DursMsg> for WS2PModule {
match req { match req {
OldNetworkRequest::GetCurrent(ref _req_id, _receiver) => { OldNetworkRequest::GetCurrent(ref _req_id, _receiver) => {
info!( info!(
"WS2PSignal::ReceiveCurrent({}, {:?}, {:#?})", "WS2PSignal::ReceiveCurrent({}, {:?})",
req_id.0, req, response req_id.0, req
); );
if let Some(block) = parse_json_block(&response) { if let Some(block) = parse_json_block(&response) {
ws2p_module.send_network_req_response( ws2p_module.send_network_req_response(
...@@ -770,11 +761,11 @@ impl DursModule<DuRsConf, DursMsg> for WS2PModule { ...@@ -770,11 +761,11 @@ impl DursModule<DuRsConf, DursMsg> for WS2PModule {
} }
OldNetworkRequest::GetBlocks( OldNetworkRequest::GetBlocks(
ref _req_id, ref _req_id,
_receiver, _node_full_id,
_count, count,
from, from,
) => { ) => {
info!("WS2PSignal::ReceiveChunk({}, {:?})", req_id.0, req); info!("WS2PSignal::ReceiveChunk({}, {} blocks from {})", req_id.0, count, from);
if response.is_array() { if response.is_array() {
let mut chunk = Vec::new(); let mut chunk = Vec::new();
for json_block in response.as_array().unwrap() { for json_block in response.as_array().unwrap() {
...@@ -945,6 +936,8 @@ impl DursModule<DuRsConf, DursMsg> for WS2PModule { ...@@ -945,6 +936,8 @@ impl DursModule<DuRsConf, DursMsg> for WS2PModule {
} }
} }
// .. // ..
// Request current blockstamp
ws2p_module.send_dal_request(&BlockchainRequest::CurrentBlockstamp());
} }
} }
Ok(()) Ok(())
......
...@@ -627,7 +627,7 @@ pub fn connect_to_ws2p_endpoint( ...@@ -627,7 +627,7 @@ pub fn connect_to_ws2p_endpoint(
generate_connect_message(currency, key_pair, conn_meta_datas.challenge.clone()); generate_connect_message(currency, key_pair, conn_meta_datas.challenge.clone());
// Log // Log
info!("Try connection to {} ...", ws_url); info!("WS2P: Try connection to {} ...", ws_url);
// Connect to websocket // Connect to websocket
connect(ws_url, |ws| { connect(ws_url, |ws| {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment