Skip to content

Commit 29c8fe5

Browse files
committed
Consider all channel maps in update_partial_channel_config
1 parent fa9d1d1 commit 29c8fe5

File tree

1 file changed

+49
-13
lines changed

1 file changed

+49
-13
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3548,27 +3548,44 @@ where
35483548
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
35493549
let peer_state = &mut *peer_state_lock;
35503550
for channel_id in channel_ids {
3551-
if !peer_state.channel_by_id.contains_key(channel_id) {
3551+
if !peer_state.has_channel(channel_id) {
35523552
return Err(APIError::ChannelUnavailable {
35533553
err: format!("Channel with ID {} was not found for the passed counterparty_node_id {}", log_bytes!(*channel_id), counterparty_node_id),
35543554
});
3555-
}
3555+
};
35563556
}
35573557
for channel_id in channel_ids {
3558-
let channel = peer_state.channel_by_id.get_mut(channel_id).unwrap();
3559-
let mut config = channel.context.config();
3560-
config.apply(config_update);
3561-
if !channel.context.update_config(&config) {
3558+
if let Some(channel) = peer_state.channel_by_id.get_mut(channel_id) {
3559+
let mut config = channel.context.config();
3560+
config.apply(config_update);
3561+
if !channel.context.update_config(&config) {
3562+
continue;
3563+
}
3564+
if let Ok(msg) = self.get_channel_update_for_broadcast(channel) {
3565+
peer_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate { msg });
3566+
} else if let Ok(msg) = self.get_channel_update_for_unicast(channel) {
3567+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendChannelUpdate {
3568+
node_id: channel.context.get_counterparty_node_id(),
3569+
msg,
3570+
});
3571+
}
35623572
continue;
35633573
}
3564-
if let Ok(msg) = self.get_channel_update_for_broadcast(channel) {
3565-
peer_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate { msg });
3566-
} else if let Ok(msg) = self.get_channel_update_for_unicast(channel) {
3567-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendChannelUpdate {
3568-
node_id: channel.context.get_counterparty_node_id(),
3569-
msg,
3574+
3575+
let context = if let Some(channel) = peer_state.inbound_v1_channel_by_id.get_mut(channel_id) {
3576+
&mut channel.context
3577+
} else if let Some(channel) = peer_state.outbound_v1_channel_by_id.get_mut(channel_id) {
3578+
&mut channel.context
3579+
} else {
3580+
return Err(APIError::ChannelUnavailable {
3581+
err: format!("Channel with ID {} was not found for the passed counterparty_node_id {}", log_bytes!(*channel_id), counterparty_node_id),
35703582
});
3571-
}
3583+
};
3584+
let mut config = context.config();
3585+
config.apply(config_update);
3586+
// We update the config, but we MUST NOT broadcast a `channel_update` before `channel_ready`
3587+
// which would be the case for pending inbound/outbound channels.
3588+
context.update_config(&config);
35723589
}
35733590
Ok(())
35743591
}
@@ -10182,6 +10199,25 @@ mod tests {
1018210199
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
1018310200
_ => panic!("expected BroadcastChannelUpdate event"),
1018410201
}
10202+
10203+
// If we provide a channel_id not associated with the peer, we should get an error and no updates
10204+
// should be applied to ensure update atomicity as specified in the API docs.
10205+
let bad_channel_id = [10; 32];
10206+
let current_fee = nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_proportional_millionths;
10207+
let new_fee = current_fee + 100;
10208+
assert!(
10209+
matches!(
10210+
nodes[0].node.update_partial_channel_config(&channel.counterparty.node_id, &[channel.channel_id, bad_channel_id], &ChannelConfigUpdate {
10211+
forwarding_fee_proportional_millionths: Some(new_fee),
10212+
..Default::default()
10213+
}),
10214+
Err(APIError::ChannelUnavailable { err: _ }),
10215+
)
10216+
);
10217+
// Check that the fee hasn't changed for the channel that exists.
10218+
assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_proportional_millionths, current_fee);
10219+
let events = nodes[0].node.get_and_clear_pending_msg_events();
10220+
assert_eq!(events.len(), 0);
1018510221
}
1018610222
}
1018710223

0 commit comments

Comments
 (0)