Skip to content

Commit cae5f7e

Browse files
committed
caching of announcement messages
1 parent 7c1cc1a commit cae5f7e

File tree

3 files changed

+80
-8
lines changed

3 files changed

+80
-8
lines changed

src/ln/msgs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ impl NetAddress {
370370
}
371371
}
372372
}
373-
373+
#[derive(Clone)]
374374
// Only exposed as broadcast of node_announcement should be filtered by node_id
375375
/// The unsigned part of a node_announcement
376376
pub struct UnsignedNodeAnnouncement {
@@ -387,6 +387,7 @@ pub struct UnsignedNodeAnnouncement {
387387
pub(crate) excess_address_data: Vec<u8>,
388388
pub(crate) excess_data: Vec<u8>,
389389
}
390+
#[derive(Clone)]
390391
/// A node_announcement message to be sent or received from a peer
391392
pub struct NodeAnnouncement {
392393
pub(crate) signature: Signature,

src/ln/peer_handler.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,14 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
516516
if self.initial_syncs_sent.load(Ordering::Acquire) < INITIAL_SYNCS_TO_SEND {
517517
self.initial_syncs_sent.fetch_add(1, Ordering::AcqRel);
518518
local_features.set_initial_routing_sync();
519-
//TODO get and send announcements.
519+
let channel_messages = self.message_handler.route_handler.get_all_channel_announcements();
520+
let node_messages = self.message_handler.route_handler.get_all_node_announcements();
521+
for message in channel_messages.iter(){
522+
encode_and_send_msg!(message, 256);
523+
}
524+
for message in node_messages.iter(){
525+
encode_and_send_msg!(message, 257);
526+
}
520527
}
521528
encode_and_send_msg!(msgs::Init {
522529
global_features: msgs::GlobalFeatures::new(),

src/ln/router.rs

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ struct DirectionalChannelInfo {
5555
htlc_minimum_msat: u64,
5656
fee_base_msat: u32,
5757
fee_proportional_millionths: u32,
58+
bitcoin_key: PublicKey,
5859
}
5960

6061
impl std::fmt::Display for DirectionalChannelInfo {
@@ -68,6 +69,8 @@ struct ChannelInfo {
6869
features: GlobalFeatures,
6970
one_to_two: DirectionalChannelInfo,
7071
two_to_one: DirectionalChannelInfo,
72+
//this is cached here so we can send out it later if required by route_init_sync
73+
announcement_message : Option<msgs::ChannelAnnouncement>,
7174
}
7275

7376
impl std::fmt::Display for ChannelInfo {
@@ -91,6 +94,8 @@ struct NodeInfo {
9194
rgb: [u8; 3],
9295
alias: [u8; 32],
9396
addresses: Vec<NetAddress>,
97+
//this is cached here so we can send out it later if required by route_init_sync
98+
announcement_message : Option<msgs::NodeAnnouncement>,
9499
}
95100

96101
impl std::fmt::Display for NodeInfo {
@@ -184,7 +189,7 @@ pub struct RouteHint {
184189
/// Tracks a view of the network, receiving updates from peers and generating Routes to
185190
/// payment destinations.
186191
pub struct Router {
187-
secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
192+
secp_ctx: Secp256k1<secp256k1::All>,
188193
network_map: RwLock<NetworkMap>,
189194
chain_monitor: Arc<ChainWatchInterface>,
190195
logger: Arc<Logger>,
@@ -221,6 +226,7 @@ impl RoutingMessageHandler for Router {
221226
node.rgb = msg.contents.rgb;
222227
node.alias = msg.contents.alias;
223228
node.addresses = msg.contents.addresses.clone();
229+
224230
Ok(msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty() && !msg.contents.features.supports_unknown_bits())
225231
}
226232
}
@@ -279,6 +285,7 @@ impl RoutingMessageHandler for Router {
279285
htlc_minimum_msat: u64::max_value(),
280286
fee_base_msat: u32::max_value(),
281287
fee_proportional_millionths: u32::max_value(),
288+
bitcoin_key : msg.contents.bitcoin_key_1.clone(),
282289
},
283290
two_to_one: DirectionalChannelInfo {
284291
src_node_id: msg.contents.node_id_2.clone(),
@@ -288,7 +295,9 @@ impl RoutingMessageHandler for Router {
288295
htlc_minimum_msat: u64::max_value(),
289296
fee_base_msat: u32::max_value(),
290297
fee_proportional_millionths: u32::max_value(),
291-
}
298+
bitcoin_key : msg.contents.bitcoin_key_2.clone(),
299+
},
300+
announcement_message : Some(msg.clone()),
292301
};
293302

294303
match network.channels.entry(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
@@ -332,6 +341,7 @@ impl RoutingMessageHandler for Router {
332341
rgb: [0; 3],
333342
alias: [0; 32],
334343
addresses: Vec::new(),
344+
announcement_message: None,
335345
});
336346
}
337347
}
@@ -430,13 +440,66 @@ impl RoutingMessageHandler for Router {
430440

431441
impl NetworkTopology for Router {
432442
fn get_all_channel_announcements(&self)-> Result<Vec<msgs::ChannelAnnouncement>, std::fmt::Error>{
433-
unimplemented!();
443+
let result = Vec::new();
444+
let network = self.network_map.read().unwrap();
445+
for (key, value) in network.channels.iter(){
446+
if let Some(i) = value.announcement_message{
447+
result.push(i.clone());
448+
}
449+
}
450+
return Ok(result)
451+
/* #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
452+
return Ok(Vec::new());
453+
#[cfg(feature = "non_bitcoin_chain_hash_routing")]{
454+
let result = Vec::new();
455+
let network = self.network_map.read().unwrap();
456+
for (key, value) in network.channels.iter(){
457+
let (id, chainhash) = key;
458+
let msg_contents = msgs::UnsignedChannelAnnouncement {
459+
features: value.features.clone(),
460+
chain_hash: chainhash,
461+
short_channel_id: id.clone(),
462+
node_id_1: value.one_to_two.src_node_id.clone(),
463+
node_id_2: value.two_to_one.src_node_id.clone(),
464+
bitcoin_key_1: value.one_to_two.bitcoin_key.clone(),
465+
bitcoin_key_2: value.two_to_one.bitcoin_key.clone(),
466+
excess_data: Vec::new()};
467+
let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg_contents.encode()[..])[..]).unwrap();
468+
let msg_signature = self.secp_ctx.sign(&msg_hash, key);
469+
result.push(msgs::ChannelAnnouncement {
470+
signature: msg_signature,
471+
contents: msg_contents,
472+
})
473+
}
474+
return Ok(result)}*/
434475
}
476+
///This function generates a vec of all node_annoucment messages for all nodes it known of
435477
fn get_all_node_announcements(&self)-> Result<Vec<msgs::NodeAnnouncement>, std::fmt::Error>{
436-
unimplemented!();
478+
let result = Vec::new();
479+
let network = self.network_map.read().unwrap();
480+
for (key, value) in network.nodes.iter(){
481+
if let Some(i) = value.announcement_message{
482+
result.push(i.clone());
483+
}
484+
/*let msg_contents = msgs::UnsignedNodeAnnouncement{
485+
features: value.features.clone(),
486+
timestamp: value.last_update,
487+
node_id: key.clone(),
488+
rgb: value.rgb.clone(),
489+
alias: value.alias.clone(),
490+
addresses: value.addresses.clone(),
491+
excess_address_data: Vec::new(),
492+
excess_data: Vec::new(),};
493+
let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg_contents.encode()[..])[..]).unwrap();
494+
let msg_signature = self.secp_ctx.sign(&msg_hash, key);
495+
result.push(msgs::NodeAnnouncement {
496+
signature: msg_signature,
497+
contents: msg_contents,
498+
})*/
499+
}
500+
Ok(result)
437501
}
438502
}
439-
440503
#[derive(Eq, PartialEq)]
441504
struct RouteGraphNode {
442505
pubkey: PublicKey,
@@ -478,9 +541,10 @@ impl Router {
478541
rgb: [0; 3],
479542
alias: [0; 32],
480543
addresses: Vec::new(),
544+
announcement_message: None,
481545
});
482546
Router {
483-
secp_ctx: Secp256k1::verification_only(),
547+
secp_ctx: Secp256k1::new(),
484548
network_map: RwLock::new(NetworkMap {
485549
channels: HashMap::new(),
486550
our_node_id: our_pubkey,

0 commit comments

Comments
 (0)