Skip to content

Commit e8a0824

Browse files
valentinewallaceAntoine Riard
and
Antoine Riard
committed
Pass channel updates to ChannelManager and Channel.
This will be used to expose forwarding info for route hints in the next commit. Co-authored-by: Valentine Wallace <[email protected]> Co-authored-by: Antoine Riard <[email protected]>
1 parent 5b28744 commit e8a0824

File tree

6 files changed

+50
-0
lines changed

6 files changed

+50
-0
lines changed

lightning-net-tokio/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ mod tests {
561561
fn handle_revoke_and_ack(&self, _their_node_id: &PublicKey, _msg: &RevokeAndACK) {}
562562
fn handle_update_fee(&self, _their_node_id: &PublicKey, _msg: &UpdateFee) {}
563563
fn handle_announcement_signatures(&self, _their_node_id: &PublicKey, _msg: &AnnouncementSignatures) {}
564+
fn handle_channel_update(&self, _their_node_id: &PublicKey, _msg: &ChannelUpdate) {}
564565
fn peer_disconnected(&self, their_node_id: &PublicKey, _no_connection_possible: bool) {
565566
if *their_node_id == self.expected_pubkey {
566567
self.disconnected_flag.store(true, Ordering::SeqCst);

lightning/src/ln/channel.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4132,6 +4132,20 @@ impl<Signer: Sign> Channel<Signer> {
41324132
}
41334133
}
41344134

4135+
pub fn channel_update(&mut self, msg: &msgs::ChannelUpdate) -> Result<(), ChannelError> {
4136+
let usable_channel_value_msat = (self.channel_value_satoshis - self.counterparty_selected_channel_reserve_satoshis) * 1000;
4137+
if msg.contents.htlc_minimum_msat >= usable_channel_value_msat {
4138+
return Err(ChannelError::Close("Minimum htlc value is greater than channel value".to_string()));
4139+
}
4140+
self.counterparty_forwarding_info = Some(CounterpartyForwardingInfo {
4141+
fee_base_msat: msg.contents.fee_base_msat,
4142+
fee_proportional_millionths: msg.contents.fee_proportional_millionths,
4143+
cltv_expiry_delta: msg.contents.cltv_expiry_delta
4144+
});
4145+
4146+
Ok(())
4147+
}
4148+
41354149
/// Begins the shutdown process, getting a message for the remote peer and returning all
41364150
/// holding cell HTLCs for payment failure.
41374151
pub fn get_shutdown(&mut self) -> Result<(msgs::Shutdown, Vec<(HTLCSource, PaymentHash)>), APIError> {

lightning/src/ln/channelmanager.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2994,6 +2994,29 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
29942994
Ok(())
29952995
}
29962996

2997+
fn internal_channel_update(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelUpdate) -> Result<(), MsgHandleErrInternal> {
2998+
let mut channel_state_lock = self.channel_state.lock().unwrap();
2999+
let channel_state = &mut *channel_state_lock;
3000+
let chan_id = match channel_state.short_to_id.get(&msg.contents.short_channel_id) {
3001+
Some(chan_id) => chan_id.clone(),
3002+
None => {
3003+
// It's not a local channel
3004+
return Ok(())
3005+
}
3006+
};
3007+
match channel_state.by_id.entry(chan_id) {
3008+
hash_map::Entry::Occupied(mut chan) => {
3009+
if chan.get().get_counterparty_node_id() != *counterparty_node_id {
3010+
// TODO: see issue #153, need a consistent behavior on obnoxious behavior from random node
3011+
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), chan_id));
3012+
}
3013+
try_chan_entry!(self, chan.get_mut().channel_update(&msg), channel_state, chan);
3014+
},
3015+
hash_map::Entry::Vacant(_) => unreachable!()
3016+
}
3017+
Ok(())
3018+
}
3019+
29973020
fn internal_channel_reestablish(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<(), MsgHandleErrInternal> {
29983021
let mut channel_state_lock = self.channel_state.lock().unwrap();
29993022
let channel_state = &mut *channel_state_lock;
@@ -3517,6 +3540,11 @@ impl<Signer: Sign, M: Deref + Sync + Send, T: Deref + Sync + Send, K: Deref + Sy
35173540
let _ = handle_error!(self, self.internal_announcement_signatures(counterparty_node_id, msg), *counterparty_node_id);
35183541
}
35193542

3543+
fn handle_channel_update(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelUpdate) {
3544+
let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
3545+
let _ = handle_error!(self, self.internal_channel_update(counterparty_node_id, msg), *counterparty_node_id);
3546+
}
3547+
35203548
fn handle_channel_reestablish(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelReestablish) {
35213549
let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
35223550
let _ = handle_error!(self, self.internal_channel_reestablish(counterparty_node_id, msg), *counterparty_node_id);

lightning/src/ln/msgs.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,9 @@ pub trait ChannelMessageHandler : MessageSendEventsProvider + Send + Sync {
796796
/// Handle an incoming channel_reestablish message from the given peer.
797797
fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &ChannelReestablish);
798798

799+
/// Handle an incoming channel update from the given peer.
800+
fn handle_channel_update(&self, their_node_id: &PublicKey, msg: &ChannelUpdate);
801+
799802
// Error:
800803
/// Handle an incoming error message from the given peer.
801804
fn handle_error(&self, their_node_id: &PublicKey, msg: &ErrorMessage);

lightning/src/ln/peer_handler.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ impl ChannelMessageHandler for ErroringMessageHandler {
142142
fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &msgs::ChannelReestablish) {
143143
ErroringMessageHandler::push_error(self, their_node_id, msg.channel_id);
144144
}
145+
// msgs::ChannelUpdate does not contain the channel_id field, so we just drop them.
146+
fn handle_channel_update(&self, _their_node_id: &PublicKey, _msg: &msgs::ChannelUpdate) {}
145147
fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}
146148
fn peer_connected(&self, _their_node_id: &PublicKey, _msg: &msgs::Init) {}
147149
fn handle_error(&self, _their_node_id: &PublicKey, _msg: &msgs::ErrorMessage) {}
@@ -970,6 +972,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
970972
}
971973
},
972974
wire::Message::ChannelUpdate(msg) => {
975+
self.message_handler.chan_handler.handle_channel_update(&peer.their_node_id.unwrap(), &msg);
973976
let should_forward = match self.message_handler.route_handler.handle_channel_update(&msg) {
974977
Ok(v) => v,
975978
Err(e) => { return Err(e.into()); },

lightning/src/util/test_utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
231231
fn handle_commitment_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::CommitmentSigned) {}
232232
fn handle_revoke_and_ack(&self, _their_node_id: &PublicKey, _msg: &msgs::RevokeAndACK) {}
233233
fn handle_update_fee(&self, _their_node_id: &PublicKey, _msg: &msgs::UpdateFee) {}
234+
fn handle_channel_update(&self, _their_node_id: &PublicKey, _msg: &msgs::ChannelUpdate) {}
234235
fn handle_announcement_signatures(&self, _their_node_id: &PublicKey, _msg: &msgs::AnnouncementSignatures) {}
235236
fn handle_channel_reestablish(&self, _their_node_id: &PublicKey, _msg: &msgs::ChannelReestablish) {}
236237
fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}

0 commit comments

Comments
 (0)