X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fpeer_handler.rs;h=02bb2625b1519269aad4a8333ddd466ff75e2948;hb=22dded737b6771d9de0eb20abac0d20e31ff929a;hp=b99f2f522709dd56cbe01a5d441bb77f7892b255;hpb=eff8af21103e43f763cb10ae6a75c1543a2d4068;p=rust-lightning diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index b99f2f52..02bb2625 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -548,9 +548,9 @@ impl PeerManager where peer.their_node_id = Some(their_node_id); insert_node_id!(); - let mut features = InitFeatures::supported(); - if self.message_handler.route_handler.should_request_full_sync(&peer.their_node_id.unwrap()) { - features.set_initial_routing_sync(); + let mut features = InitFeatures::known(); + if !self.message_handler.route_handler.should_request_full_sync(&peer.their_node_id.unwrap()) { + features.clear_initial_routing_sync(); } let resp = msgs::Init { features }; @@ -629,10 +629,11 @@ impl PeerManager where return Err(PeerHandleError{ no_connection_possible: false }); } - log_info!(self, "Received peer Init message: data_loss_protect: {}, initial_routing_sync: {}, upfront_shutdown_script: {}, unkown local flags: {}, unknown global flags: {}", + log_info!(self, "Received peer Init message: data_loss_protect: {}, initial_routing_sync: {}, upfront_shutdown_script: {}, static_remote_key: {}, unkown local flags: {}, unknown global flags: {}", if msg.features.supports_data_loss_protect() { "supported" } else { "not supported"}, if msg.features.initial_routing_sync() { "requested" } else { "not requested" }, if msg.features.supports_upfront_shutdown_script() { "supported" } else { "not supported"}, + if msg.features.supports_static_remote_key() { "supported" } else { "not supported"}, if msg.features.supports_unknown_bits() { "present" } else { "none" }, if msg.features.supports_unknown_bits() { "present" } else { "none" }); @@ -642,9 +643,9 @@ impl PeerManager where } if !peer.outbound { - let mut features = InitFeatures::supported(); - if self.message_handler.route_handler.should_request_full_sync(&peer.their_node_id.unwrap()) { - features.set_initial_routing_sync(); + let mut features = InitFeatures::known(); + if !self.message_handler.route_handler.should_request_full_sync(&peer.their_node_id.unwrap()) { + features.clear_initial_routing_sync(); } let resp = msgs::Init { features }; @@ -1247,6 +1248,13 @@ mod tests { (fd_a.clone(), fd_b.clone()) } + fn establish_connection_and_read_events<'a>(peer_a: &PeerManager, peer_b: &PeerManager) -> (FileDescriptor, FileDescriptor) { + let (mut fd_a, mut fd_b) = establish_connection(peer_a, peer_b); + assert_eq!(peer_b.read_event(&mut fd_b, &fd_a.outbound_data.lock().unwrap().split_off(0)).unwrap(), false); + assert_eq!(peer_a.read_event(&mut fd_a, &fd_b.outbound_data.lock().unwrap().split_off(0)).unwrap(), false); + (fd_a.clone(), fd_b.clone()) + } + #[test] fn test_disconnect_peer() { // Simple test which builds a network of PeerManager, connects and brings them to NoiseState::Finished and @@ -1351,7 +1359,7 @@ mod tests { let node_1_btckey = SecretKey::from_slice(&[40; 32]).unwrap(); let node_2_btckey = SecretKey::from_slice(&[39; 32]).unwrap(); let unsigned_ann = msgs::UnsignedChannelAnnouncement { - features: ChannelFeatures::supported(), + features: ChannelFeatures::known(), chain_hash: genesis_block(network).header.bitcoin_hash(), short_channel_id: short_chan_id, node_id_1: PublicKey::from_secret_key(&secp_ctx, &node_1_privkey), @@ -1421,4 +1429,47 @@ mod tests { assert_eq!(routing_handlers_concrete[1].clone().chan_upds_recvd.load(Ordering::Acquire), 100); assert_eq!(routing_handlers_concrete[1].clone().chan_anns_recvd.load(Ordering::Acquire), 50); } + + #[test] + fn limit_initial_routing_sync_requests() { + // Inbound peer 0 requests initial_routing_sync, but outbound peer 1 does not. + { + let chan_handlers = create_chan_handlers(2); + let routing_handlers: Vec> = vec![ + Arc::new(test_utils::TestRoutingMessageHandler::new().set_request_full_sync()), + Arc::new(test_utils::TestRoutingMessageHandler::new()), + ]; + let peers = create_network(2, &chan_handlers, Some(&routing_handlers)); + let (fd_0_to_1, fd_1_to_0) = establish_connection_and_read_events(&peers[0], &peers[1]); + + let peer_0 = peers[0].peers.lock().unwrap(); + let peer_1 = peers[1].peers.lock().unwrap(); + + let peer_0_features = peer_1.peers.get(&fd_1_to_0).unwrap().their_features.as_ref(); + let peer_1_features = peer_0.peers.get(&fd_0_to_1).unwrap().their_features.as_ref(); + + assert!(peer_0_features.unwrap().initial_routing_sync()); + assert!(!peer_1_features.unwrap().initial_routing_sync()); + } + + // Outbound peer 1 requests initial_routing_sync, but inbound peer 0 does not. + { + let chan_handlers = create_chan_handlers(2); + let routing_handlers: Vec> = vec![ + Arc::new(test_utils::TestRoutingMessageHandler::new()), + Arc::new(test_utils::TestRoutingMessageHandler::new().set_request_full_sync()), + ]; + let peers = create_network(2, &chan_handlers, Some(&routing_handlers)); + let (fd_0_to_1, fd_1_to_0) = establish_connection_and_read_events(&peers[0], &peers[1]); + + let peer_0 = peers[0].peers.lock().unwrap(); + let peer_1 = peers[1].peers.lock().unwrap(); + + let peer_0_features = peer_1.peers.get(&fd_1_to_0).unwrap().their_features.as_ref(); + let peer_1_features = peer_0.peers.get(&fd_0_to_1).unwrap().their_features.as_ref(); + + assert!(!peer_0_features.unwrap().initial_routing_sync()); + assert!(peer_1_features.unwrap().initial_routing_sync()); + } + } }