Skip to content

Commit e3effa4

Browse files
committed
Send initial_routing_sync-filled Init messages to the first 5 peers
1 parent 05e2a7d commit e3effa4

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/ln/msgs.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,16 @@ impl LocalFeatures {
6161
self.flags.len() > 0 && (self.flags[0] & 1) != 0
6262
}
6363

64-
pub fn supports_initial_routing_sync(&self) -> bool {
64+
pub fn initial_routing_sync(&self) -> bool {
6565
self.flags.len() > 0 && (self.flags[0] & (1 << 3)) != 0
6666
}
67+
pub fn set_initial_routing_sync(&mut self) {
68+
if self.flags.len() == 0 {
69+
self.flags.resize(1, 1 << 3);
70+
} else {
71+
self.flags[0] |= 1 << 3;
72+
}
73+
}
6774

6875
pub fn supports_upfront_shutdown_script(&self) -> bool {
6976
self.flags.len() > 0 && (self.flags[0] & (3 << 4)) != 0

src/ln/peer_handler.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use util::events::{EventsProvider,Event};
88

99
use std::collections::{HashMap,LinkedList};
1010
use std::sync::{Arc, Mutex};
11+
use std::sync::atomic::{AtomicUsize, Ordering};
1112
use std::{cmp,error,mem,hash,fmt};
1213

1314
pub struct MessageHandler {
@@ -86,6 +87,7 @@ pub struct PeerManager<Descriptor: SocketDescriptor> {
8687
peers: Mutex<PeerHolder<Descriptor>>,
8788
pending_events: Mutex<Vec<Event>>,
8889
our_node_secret: SecretKey,
90+
initial_syncs_sent: AtomicUsize,
8991
}
9092

9193

@@ -101,6 +103,9 @@ macro_rules! encode_msg {
101103
}
102104
}
103105

106+
//TODO: Really should do something smarter for this
107+
const INITIAL_SYNCS_TO_SEND: usize = 5;
108+
104109
/// Manages and reacts to connection events. You probably want to use file descriptors as PeerIds.
105110
/// PeerIds may repeat, but only after disconnect_event() has been called.
106111
impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
@@ -110,6 +115,7 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
110115
peers: Mutex::new(PeerHolder { peers: HashMap::new(), node_id_to_descriptor: HashMap::new() }),
111116
pending_events: Mutex::new(Vec::new()),
112117
our_node_secret: our_node_secret,
118+
initial_syncs_sent: AtomicUsize::new(0),
113119
}
114120
}
115121

@@ -333,9 +339,14 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
333339
peer.pending_read_is_header = true;
334340

335341
insert_node_id = Some(peer.their_node_id.unwrap());
342+
let mut local_features = msgs::LocalFeatures::new();
343+
if self.initial_syncs_sent.load(Ordering::Acquire) < INITIAL_SYNCS_TO_SEND {
344+
self.initial_syncs_sent.fetch_add(1, Ordering::AcqRel);
345+
local_features.set_initial_routing_sync();
346+
}
336347
encode_and_send_msg!(msgs::Init {
337348
global_features: msgs::GlobalFeatures::new(),
338-
local_features: msgs::LocalFeatures::new(),
349+
local_features,
339350
}, 16);
340351
},
341352
NextNoiseStep::ActThree => {
@@ -381,9 +392,14 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
381392
peer.their_local_features = Some(msg.local_features);
382393

383394
if !peer.outbound {
395+
let mut local_features = msgs::LocalFeatures::new();
396+
if self.initial_syncs_sent.load(Ordering::Acquire) < INITIAL_SYNCS_TO_SEND {
397+
self.initial_syncs_sent.fetch_add(1, Ordering::AcqRel);
398+
local_features.set_initial_routing_sync();
399+
}
384400
encode_and_send_msg!(msgs::Init {
385401
global_features: msgs::GlobalFeatures::new(),
386-
local_features: msgs::LocalFeatures::new(),
402+
local_features,
387403
}, 16);
388404
}
389405
},

0 commit comments

Comments
 (0)