Skip to content

Commit d3fb619

Browse files
committed
Move initial_routing_sync decision to the Router
PeerManager determines whether the initial_routing_sync feature bit should be set when sending Init messages to peers. Move this to the Router as it is better able to determine if a full sync is needed.
1 parent 2ec7c77 commit d3fb619

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

lightning/src/ln/msgs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,8 @@ pub trait RoutingMessageHandler : Send + Sync {
604604
/// starting at the node *after* the provided publickey and including batch_amount entries.
605605
/// If None is provided for starting_point, we start at the first node.
606606
fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<NodeAnnouncement>;
607+
/// Returns whether a full sync should be requested from a peer.
608+
fn should_request_full_sync(&self, node_id: &PublicKey) -> bool;
607609
}
608610

609611
pub(crate) struct OnionRealm0HopData {

lightning/src/ln/peer_handler.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ pub struct PeerManager<Descriptor: SocketDescriptor, CM: Deref> where CM::Target
189189
peer_counter_low: AtomicUsize,
190190
peer_counter_high: AtomicUsize,
191191

192-
initial_syncs_sent: AtomicUsize,
193192
logger: Arc<Logger>,
194193
}
195194

@@ -212,9 +211,6 @@ macro_rules! encode_msg {
212211
}}
213212
}
214213

215-
//TODO: Really should do something smarter for this
216-
const INITIAL_SYNCS_TO_SEND: usize = 5;
217-
218214
/// Manages and reacts to connection events. You probably want to use file descriptors as PeerIds.
219215
/// PeerIds may repeat, but only after disconnect_event() has been called.
220216
impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where CM::Target: msgs::ChannelMessageHandler {
@@ -236,7 +232,6 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
236232
ephemeral_key_midstate,
237233
peer_counter_low: AtomicUsize::new(0),
238234
peer_counter_high: AtomicUsize::new(0),
239-
initial_syncs_sent: AtomicUsize::new(0),
240235
logger,
241236
}
242237
}
@@ -580,8 +575,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
580575
peer.their_node_id = Some(their_node_id);
581576
insert_node_id!();
582577
let mut features = InitFeatures::supported();
583-
if self.initial_syncs_sent.load(Ordering::Acquire) < INITIAL_SYNCS_TO_SEND {
584-
self.initial_syncs_sent.fetch_add(1, Ordering::AcqRel);
578+
if self.message_handler.route_handler.should_request_full_sync(&peer.their_node_id.unwrap()) {
585579
features.set_initial_routing_sync();
586580
}
587581

@@ -652,8 +646,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
652646

653647
if !peer.outbound {
654648
let mut features = InitFeatures::supported();
655-
if self.initial_syncs_sent.load(Ordering::Acquire) < INITIAL_SYNCS_TO_SEND {
656-
self.initial_syncs_sent.fetch_add(1, Ordering::AcqRel);
649+
if self.message_handler.route_handler.should_request_full_sync(&peer.their_node_id.unwrap()) {
657650
features.set_initial_routing_sync();
658651
}
659652

lightning/src/ln/router.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use util::logger::Logger;
2222

2323
use std::cmp;
2424
use std::sync::{RwLock,Arc};
25+
use std::sync::atomic::{AtomicUsize, Ordering};
2526
use std::collections::{HashMap,BinaryHeap,BTreeMap};
2627
use std::collections::btree_map::Entry as BtreeEntry;
2728
use std;
@@ -347,6 +348,7 @@ pub struct RouteHint {
347348
pub struct Router {
348349
secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
349350
network_map: RwLock<NetworkMap>,
351+
full_syncs_requested: AtomicUsize,
350352
chain_monitor: Arc<ChainWatchInterface>,
351353
logger: Arc<Logger>,
352354
}
@@ -390,6 +392,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, RouterReadArgs> for Router {
390392
Ok(Router {
391393
secp_ctx: Secp256k1::verification_only(),
392394
network_map: RwLock::new(network_map),
395+
full_syncs_requested: AtomicUsize::new(0),
393396
chain_monitor: args.chain_monitor,
394397
logger: args.logger,
395398
})
@@ -406,6 +409,7 @@ macro_rules! secp_verify_sig {
406409
}
407410

408411
impl RoutingMessageHandler for Router {
412+
409413
fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, LightningError> {
410414
let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
411415
secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
@@ -698,6 +702,17 @@ impl RoutingMessageHandler for Router {
698702
}
699703
result
700704
}
705+
706+
fn should_request_full_sync(&self, _node_id: &PublicKey) -> bool {
707+
//TODO: Determine whether to request a full sync based on the network map.
708+
const FULL_SYNCS_TO_REQUEST: usize = 5;
709+
if self.full_syncs_requested.load(Ordering::Acquire) < FULL_SYNCS_TO_REQUEST {
710+
self.full_syncs_requested.fetch_add(1, Ordering::AcqRel);
711+
true
712+
} else {
713+
false
714+
}
715+
}
701716
}
702717

703718
#[derive(Eq, PartialEq)]
@@ -750,6 +765,7 @@ impl Router {
750765
our_node_id: our_pubkey,
751766
nodes: nodes,
752767
}),
768+
full_syncs_requested: AtomicUsize::new(0),
753769
chain_monitor,
754770
logger,
755771
}

lightning/src/util/test_utils.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
155155
fn get_next_node_announcements(&self, _starting_point: Option<&PublicKey>, _batch_amount: u8) -> Vec<msgs::NodeAnnouncement> {
156156
Vec::new()
157157
}
158+
fn should_request_full_sync(&self, _node_id: &PublicKey) -> bool {
159+
true
160+
}
158161
}
159162

160163
pub struct TestLogger {

0 commit comments

Comments
 (0)