diff --git a/network/network_head.rs b/network/network_head.rs index 5be4beb3dd9da97748d450d6e0da039bff3b6457..3e248386dd0528e25d7aef123a4ac9c62eb3f220 100644 --- a/network/network_head.rs +++ b/network/network_head.rs @@ -386,7 +386,8 @@ impl NetworkHead { if let Some(head) = heads_cache_copy.get(&self.node_full_id()) { if self.blockstamp().id.0 > head.blockstamp().id.0 || (self.blockstamp().id.0 == head.blockstamp().id.0 - && (self.version() >= head.version() || self.step() <= head.step())) + && self.version() >= head.version() + && self.step() < head.step()) { if let Some(head_mut) = heads_cache.get_mut(&self.node_full_id()) { *head_mut = self.clone(); diff --git a/ws2p/lib.rs b/ws2p/lib.rs index 835e3c0db4aa647f2b0a732e85b3fe5e4042bbbc..91f8bf76def013126dc172bf0a11dadaaf4ff15a 100644 --- a/ws2p/lib.rs +++ b/ws2p/lib.rs @@ -69,6 +69,7 @@ mod ack_message; mod connect_message; pub mod constants; mod ok_message; +pub mod serializer; pub mod ws2p_connection; pub mod ws2p_db; pub mod ws2p_requests; @@ -449,6 +450,28 @@ impl DuniterModule<ed25519::KeyPair, DuniterMessage> for WS2PModule { ws2p_module.send_network_event(&NetworkEvent::ReceiveHeads( vec![ws2p_module.my_head.clone().unwrap()], )); + // Send my head to all connections + let my_json_head = serializer::serialize_head( + ws2p_module.my_head.clone().unwrap(), + ); + trace!("Send my HEAD: {:#?}", my_json_head); + let _results: Result< + (), + websocket::WebSocketError, + > = ws2p_module + .websockets + .iter_mut() + .map(|ws| { + (ws.1).0.send_message(&Message::text( + json!({ + "name": "HEAD", + "body": { + "heads": [my_json_head] + } + }).to_string(), + )) + }) + .collect(); } _ => {} }, @@ -505,7 +528,7 @@ impl DuniterModule<ed25519::KeyPair, DuniterMessage> for WS2PModule { ), ); // Resent to other modules connections that match receive uids - for (node_full_id, (_ep, conn_state)) in + for (node_full_id, (ep, conn_state)) in ws2p_module.ws2p_endpoints.clone() { if let Some(uid_option) = uids.get(&node_full_id.1) { @@ -514,6 +537,7 @@ impl DuniterModule<ed25519::KeyPair, DuniterMessage> for WS2PModule { node_full_id, conn_state as u32, uid_option.clone(), + ep.get_url(false), ), ); } @@ -550,6 +574,12 @@ impl DuniterModule<ed25519::KeyPair, DuniterMessage> for WS2PModule { ws2p_full_id, WS2PConnectionState::Established as u32, ws2p_module.uids_cache.get(&ws2p_full_id.1).cloned(), + ws2p_module + .ws2p_endpoints + .get(&ws2p_full_id) + .unwrap() + .0 + .get_url(false), )); } WS2PSignal::WSError(ws2p_full_id) => { @@ -558,6 +588,12 @@ impl DuniterModule<ed25519::KeyPair, DuniterMessage> for WS2PModule { ws2p_full_id, WS2PConnectionState::WSError as u32, ws2p_module.uids_cache.get(&ws2p_full_id.1).cloned(), + ws2p_module + .ws2p_endpoints + .get(&ws2p_full_id) + .unwrap() + .0 + .get_url(false), )); } WS2PSignal::NegociationTimeout(ws2p_full_id) => { @@ -566,6 +602,12 @@ impl DuniterModule<ed25519::KeyPair, DuniterMessage> for WS2PModule { ws2p_full_id, WS2PConnectionState::Denial as u32, ws2p_module.uids_cache.get(&ws2p_full_id.1).cloned(), + ws2p_module + .ws2p_endpoints + .get(&ws2p_full_id) + .unwrap() + .0 + .get_url(false), )); } WS2PSignal::Timeout(ws2p_full_id) => { @@ -574,6 +616,12 @@ impl DuniterModule<ed25519::KeyPair, DuniterMessage> for WS2PModule { ws2p_full_id, WS2PConnectionState::Close as u32, ws2p_module.uids_cache.get(&ws2p_full_id.1).cloned(), + ws2p_module + .ws2p_endpoints + .get(&ws2p_full_id) + .unwrap() + .0 + .get_url(false), )); } WS2PSignal::PeerCard(_ws2p_full_id, _peer_card, ws2p_endpoints) => { @@ -764,13 +812,12 @@ impl DuniterModule<ed25519::KeyPair, DuniterMessage> for WS2PModule { // Print network consensus match ws2p_module.get_network_consensus() { Ok(consensus_blockstamp) => { - /*writeln!( + debug!( "WS2PModule : get_network_consensus() = {:?}", consensus_blockstamp - );*/ - - while current_blockstamp.id > consensus_blockstamp.id { - warn!("Need to revert !"); + ); + if current_blockstamp.id.0 < (consensus_blockstamp.id.0 + 2) { + warn!("We probably are in a fork branch !"); } } Err(e) => warn!("{:?}", e), @@ -1208,7 +1255,11 @@ impl WS2PModuleDatas { for head in heads { if let Some(head) = NetworkHead::from_json_value(&head) { if head.verify() { - if head.apply(&mut self.heads_cache) { + if (self.my_head.is_none() + || head.node_full_id() + != self.my_head.clone().unwrap().node_full_id()) + && head.apply(&mut self.heads_cache) + { applied_heads.push(head); } } @@ -1391,7 +1442,7 @@ impl WS2PModuleDatas { .1 = WS2PConnectionState::TryToOpenWS; // get endpoint url - let ws_url = endpoint.get_url(); + let ws_url = endpoint.get_url(true); // Create WS2PConnection let mut conn_meta_datas = WS2PConnectionMetaData::new( diff --git a/ws2p/serializer.rs b/ws2p/serializer.rs new file mode 100644 index 0000000000000000000000000000000000000000..f780006811a746eb6e6c93426abd9ed65d7a0f1f --- /dev/null +++ b/ws2p/serializer.rs @@ -0,0 +1,21 @@ +extern crate serde_json; + +use duniter_network::network_head::*; + +use std::ops::Deref; + +pub fn serialize_head(head: NetworkHead) -> serde_json::Value { + match head { + NetworkHead::V2(box_head_v2) => { + let head_v2 = box_head_v2.deref(); + json!({ + "message": head_v2.message.to_string(), + "sig": head_v2.sig.to_string(), + "messageV2": head_v2.message_v2.to_string(), + "sigV2": head_v2.sig_v2.to_string(), + "step": head_v2.step + 1 + }) + } + _ => panic!("HEAD version not supported !"), + } +}