+
+ pub struct TestRoutingMessageHandler {
+ pub chan_upds_recvd: AtomicUsize,
+ pub chan_anns_recvd: AtomicUsize,
+ pub chan_anns_sent: AtomicUsize,
+ }
+
+ impl TestRoutingMessageHandler {
+ pub fn new() -> Self {
+ TestRoutingMessageHandler {
+ chan_upds_recvd: AtomicUsize::new(0),
+ chan_anns_recvd: AtomicUsize::new(0),
+ chan_anns_sent: AtomicUsize::new(0),
+ }
+ }
+
+ }
+ impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
+ fn handle_node_announcement(&self, _msg: &msgs::NodeAnnouncement) -> Result<bool, msgs::LightningError> {
+ Err(msgs::LightningError { err: "", action: msgs::ErrorAction::IgnoreError })
+ }
+ fn handle_channel_announcement(&self, _msg: &msgs::ChannelAnnouncement) -> Result<bool, msgs::LightningError> {
+ self.chan_anns_recvd.fetch_add(1, Ordering::AcqRel);
+ Err(msgs::LightningError { err: "", action: msgs::ErrorAction::IgnoreError })
+ }
+ fn handle_channel_update(&self, _msg: &msgs::ChannelUpdate) -> Result<bool, msgs::LightningError> {
+ self.chan_upds_recvd.fetch_add(1, Ordering::AcqRel);
+ Err(msgs::LightningError { err: "", action: msgs::ErrorAction::IgnoreError })
+ }
+ fn handle_htlc_fail_channel_update(&self, _update: &msgs::HTLCFailChannelUpdate) {}
+ fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, Option<msgs::ChannelUpdate>, Option<msgs::ChannelUpdate>)> {
+ let mut chan_anns = Vec::new();
+ const TOTAL_UPDS: u64 = 100;
+ let end: u64 = min(starting_point + batch_amount as u64, TOTAL_UPDS - self.chan_anns_sent.load(Ordering::Acquire) as u64);
+ for i in starting_point..end {
+ let chan_upd_1 = get_dummy_channel_update(i);
+ let chan_upd_2 = get_dummy_channel_update(i);
+ let chan_ann = get_dummy_channel_announcement(i);
+
+ chan_anns.push((chan_ann, Some(chan_upd_1), Some(chan_upd_2)));
+ }
+
+ self.chan_anns_sent.fetch_add(chan_anns.len(), Ordering::AcqRel);
+ chan_anns
+ }
+
+ fn get_next_node_announcements(&self, _starting_point: Option<&PublicKey>, _batch_amount: u8) -> Vec<msgs::NodeAnnouncement> {
+ Vec::new()
+ }
+
+ fn should_request_full_sync(&self, _node_id: &PublicKey) -> bool {
+ true
+ }
+ }
+
+ fn get_dummy_channel_announcement(short_chan_id: u64) -> msgs::ChannelAnnouncement {
+ use bitcoin::secp256k1::ffi::Signature as FFISignature;
+ let secp_ctx = Secp256k1::new();
+ let network = Network::Testnet;
+ let node_1_privkey = SecretKey::from_slice(&[42; 32]).unwrap();
+ let node_2_privkey = SecretKey::from_slice(&[41; 32]).unwrap();
+ 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::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),
+ node_id_2: PublicKey::from_secret_key(&secp_ctx, &node_2_privkey),
+ bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, &node_1_btckey),
+ bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, &node_2_btckey),
+ excess_data: Vec::new(),
+ };
+
+ msgs::ChannelAnnouncement {
+ node_signature_1: Signature::from(FFISignature::new()),
+ node_signature_2: Signature::from(FFISignature::new()),
+ bitcoin_signature_1: Signature::from(FFISignature::new()),
+ bitcoin_signature_2: Signature::from(FFISignature::new()),
+ contents: unsigned_ann,
+ }
+ }
+
+ fn get_dummy_channel_update(short_chan_id: u64) -> msgs::ChannelUpdate {
+ use bitcoin::secp256k1::ffi::Signature as FFISignature;
+ let network = Network::Testnet;
+ msgs::ChannelUpdate {
+ signature: Signature::from(FFISignature::new()),
+ contents: msgs::UnsignedChannelUpdate {
+ chain_hash: genesis_block(network).header.bitcoin_hash(),
+ short_channel_id: short_chan_id,
+ timestamp: 0,
+ flags: 0,
+ cltv_expiry_delta: 0,
+ htlc_minimum_msat: 0,
+ fee_base_msat: 0,
+ fee_proportional_millionths: 0,
+ excess_data: vec![],
+ }
+ }
+ }
+
+ #[test]
+ fn test_do_attempt_write_data() {
+ // Create 2 peers with custom TestRoutingMessageHandlers and connect them.
+ let chan_handlers = create_chan_handlers(2);
+ let mut routing_handlers: Vec<Arc<msgs::RoutingMessageHandler>> = Vec::new();
+ let mut routing_handlers_concrete: Vec<Arc<TestRoutingMessageHandler>> = Vec::new();
+ for _ in 0..2 {
+ let routing_handler = Arc::new(TestRoutingMessageHandler::new());
+ routing_handlers.push(routing_handler.clone());
+ routing_handlers_concrete.push(routing_handler.clone());
+ }
+ let peers = create_network(2, &chan_handlers, Some(&routing_handlers));
+
+ // By calling establish_connect, we trigger do_attempt_write_data between
+ // the peers. Previously this function would mistakenly enter an infinite loop
+ // when there were more channel messages available than could fit into a peer's
+ // buffer. This issue would now be detected by this test (because we use custom
+ // RoutingMessageHandlers that intentionally return more channel messages
+ // than can fit into a peer's buffer).
+ let (mut fd_a, mut fd_b) = establish_connection(&peers[0], &peers[1]);
+
+ // Make each peer to read the messages that the other peer just wrote to them.
+ peers[1].read_event(&mut fd_b, &fd_a.outbound_data.lock().unwrap().split_off(0)).unwrap();
+ peers[0].read_event(&mut fd_a, &fd_b.outbound_data.lock().unwrap().split_off(0)).unwrap();
+
+ // Check that each peer has received the expected number of channel updates and channel
+ // announcements.
+ assert_eq!(routing_handlers_concrete[0].clone().chan_upds_recvd.load(Ordering::Acquire), 100);
+ assert_eq!(routing_handlers_concrete[0].clone().chan_anns_recvd.load(Ordering::Acquire), 50);
+ 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<Arc<msgs::RoutingMessageHandler>> = 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<Arc<msgs::RoutingMessageHandler>> = 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());
+ }
+ }