Skip to content

Commit 1c7e142

Browse files
committed
Handle-initial_routing_sync-requests-from-peers-in-their-Init-messages
1 parent 3bcd911 commit 1c7e142

File tree

4 files changed

+139
-18
lines changed

4 files changed

+139
-18
lines changed

src/ln/msgs.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ pub struct AnnouncementSignatures {
327327
}
328328

329329
/// An address which can be used to connect to a remote peer
330-
#[derive(Clone)]
330+
#[derive(Clone, PartialEq)]
331331
pub enum NetAddress {
332332
/// An IPv4 address/port on which the peer is listenting.
333333
IPv4 {
@@ -374,7 +374,7 @@ impl NetAddress {
374374
}
375375
}
376376
}
377-
377+
#[derive(Clone, PartialEq)]
378378
// Only exposed as broadcast of node_announcement should be filtered by node_id
379379
/// The unsigned part of a node_announcement
380380
pub struct UnsignedNodeAnnouncement {
@@ -391,6 +391,7 @@ pub struct UnsignedNodeAnnouncement {
391391
pub(crate) excess_address_data: Vec<u8>,
392392
pub(crate) excess_data: Vec<u8>,
393393
}
394+
#[derive(Clone, PartialEq)]
394395
/// A node_announcement message to be sent or received from a peer
395396
pub struct NodeAnnouncement {
396397
pub(crate) signature: Signature,
@@ -591,6 +592,8 @@ pub trait RoutingMessageHandler : Send + Sync {
591592
fn handle_channel_update(&self, msg: &ChannelUpdate) -> Result<bool, HandleError>;
592593
/// Handle some updates to the route graph that we learned due to an outbound failed payment.
593594
fn handle_htlc_fail_channel_update(&self, update: &HTLCFailChannelUpdate);
595+
///This returns a vec of vec's, one for ChannelAnnouncements, one for ChannelUpdates and one for NodeAnnouncements. It also returns the id of the last channel passed through
596+
fn get_next_announcements(&self, starting_point: u64, batch_amount: u8)->(Vec<(ChannelAnnouncement, ChannelUpdate, NodeAnnouncement, NodeAnnouncement)>, u64);
594597
}
595598

596599
pub(crate) struct OnionRealm0HopData {

src/ln/peer_handler.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::collections::{HashMap,hash_map,LinkedList};
1919
use std::sync::{Arc, Mutex};
2020
use std::sync::atomic::{AtomicUsize, Ordering};
2121
use std::{cmp,error,mem,hash,fmt};
22+
use std::{thread, time};
2223

2324
/// Provides references to trait impls which handle different types of messages.
2425
pub struct MessageHandler {
@@ -102,6 +103,7 @@ struct Peer {
102103
pending_read_buffer: Vec<u8>,
103104
pending_read_buffer_pos: usize,
104105
pending_read_is_header: bool,
106+
last_synced_channel : u64,
105107
}
106108

107109
struct PeerHolder<Descriptor: SocketDescriptor> {
@@ -214,6 +216,7 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
214216
pending_read_buffer: pending_read_buffer,
215217
pending_read_buffer_pos: 0,
216218
pending_read_is_header: false,
219+
last_synced_channel : 0,
217220
}).is_some() {
218221
panic!("PeerManager driver duplicated descriptors!");
219222
};
@@ -248,6 +251,7 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
248251
pending_read_buffer: pending_read_buffer,
249252
pending_read_buffer_pos: 0,
250253
pending_read_is_header: false,
254+
last_synced_channel : 0,
251255
}).is_some() {
252256
panic!("PeerManager driver duplicated descriptors!");
253257
};
@@ -507,6 +511,7 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
507511
if msg.local_features.supports_unknown_bits() { "present" } else { "none" },
508512
if msg.global_features.supports_unknown_bits() { "present" } else { "none" });
509513

514+
let do_they_require_sync = msg.local_features.initial_routing_sync();
510515
peer.their_global_features = Some(msg.global_features);
511516
peer.their_local_features = Some(msg.local_features);
512517

@@ -516,10 +521,30 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
516521
self.initial_syncs_sent.fetch_add(1, Ordering::AcqRel);
517522
local_features.set_initial_routing_sync();
518523
}
524+
//we need to let them know about us, before we can send stuff to them
519525
encode_and_send_msg!(msgs::Init {
520526
global_features: msgs::GlobalFeatures::new(),
521527
local_features,
522528
}, 16);
529+
if do_they_require_sync {
530+
peer.last_synced_channel = 0;
531+
let all_messages_tuple = self.message_handler.route_handler.get_next_announcements(peer.last_synced_channel,5);
532+
while (all_messages_tuple.0).len()>0 {
533+
for i in 0..all_messages_tuple.0.len(){
534+
encode_and_send_msg!(all_messages_tuple.0[i].0.clone(), 256);
535+
encode_and_send_msg!(all_messages_tuple.0[i].2.clone(), 257);
536+
encode_and_send_msg!(all_messages_tuple.0[i].3.clone(), 257);
537+
encode_and_send_msg!(all_messages_tuple.0[i].1.clone(), 258);
538+
}
539+
//we wait to see if the buffer clears before we retrieve more message to send
540+
//we push up to 15 message out, 5 remaining means they have cleared 2/3rds
541+
while peer.pending_outbound_buffer.len()>5{
542+
thread::sleep(time::Duration::from_millis(50));
543+
};
544+
let all_messages_tuple = self.message_handler.route_handler.get_next_announcements(peer.last_synced_channel,5);
545+
peer.last_synced_channel = all_messages_tuple.1;
546+
}
547+
}
523548
}
524549

525550
for msg in self.message_handler.chan_handler.peer_connected(&peer.their_node_id.unwrap()) {

0 commit comments

Comments
 (0)