Skip to content

Commit a70b1f7

Browse files
committed
Consider all channel maps in update_partial_channel_config
1 parent c2851ea commit a70b1f7

File tree

1 file changed

+53
-13
lines changed

1 file changed

+53
-13
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3538,27 +3538,48 @@ where
35383538
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
35393539
let peer_state = &mut *peer_state_lock;
35403540
for channel_id in channel_ids {
3541-
if !peer_state.channel_by_id.contains_key(channel_id) {
3541+
if !peer_state.has_channel(channel_id) {
35423542
return Err(APIError::ChannelUnavailable {
35433543
err: format!("Channel with ID {} was not found for the passed counterparty_node_id {}", log_bytes!(*channel_id), counterparty_node_id),
35443544
});
3545-
}
3545+
};
35463546
}
35473547
for channel_id in channel_ids {
3548-
let channel = peer_state.channel_by_id.get_mut(channel_id).unwrap();
3549-
let mut config = channel.context.config();
3550-
config.apply(config_update);
3551-
if !channel.context.update_config(&config) {
3548+
if let Some(channel) = peer_state.channel_by_id.get_mut(channel_id) {
3549+
let mut config = channel.context.config();
3550+
config.apply(config_update);
3551+
if !channel.context.update_config(&config) {
3552+
continue;
3553+
}
3554+
if let Ok(msg) = self.get_channel_update_for_broadcast(channel) {
3555+
peer_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate { msg });
3556+
} else if let Ok(msg) = self.get_channel_update_for_unicast(channel) {
3557+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendChannelUpdate {
3558+
node_id: channel.context.get_counterparty_node_id(),
3559+
msg,
3560+
});
3561+
}
35523562
continue;
35533563
}
3554-
if let Ok(msg) = self.get_channel_update_for_broadcast(channel) {
3555-
peer_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate { msg });
3556-
} else if let Ok(msg) = self.get_channel_update_for_unicast(channel) {
3557-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendChannelUpdate {
3558-
node_id: channel.context.get_counterparty_node_id(),
3559-
msg,
3564+
3565+
let context = if let Some(channel) = peer_state.inbound_v1_channel_by_id.get_mut(channel_id) {
3566+
&mut channel.context
3567+
} else if let Some(channel) = peer_state.outbound_v1_channel_by_id.get_mut(channel_id) {
3568+
&mut channel.context
3569+
} else {
3570+
// This should not be reachable as we've already checked for non-existence in the previous channel_id loop.
3571+
debug_assert!(false);
3572+
return Err(APIError::ChannelUnavailable {
3573+
err: format!(
3574+
"Channel with ID {} for passed counterparty_node_id {} disappeared after we confirmed its existence - this should not be reachable!",
3575+
log_bytes!(*channel_id), counterparty_node_id),
35603576
});
3561-
}
3577+
};
3578+
let mut config = context.config();
3579+
config.apply(config_update);
3580+
// We update the config, but we MUST NOT broadcast a `channel_update` before `channel_ready`
3581+
// which would be the case for pending inbound/outbound channels.
3582+
context.update_config(&config);
35623583
}
35633584
Ok(())
35643585
}
@@ -10184,6 +10205,25 @@ mod tests {
1018410205
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
1018510206
_ => panic!("expected BroadcastChannelUpdate event"),
1018610207
}
10208+
10209+
// If we provide a channel_id not associated with the peer, we should get an error and no updates
10210+
// should be applied to ensure update atomicity as specified in the API docs.
10211+
let bad_channel_id = [10; 32];
10212+
let current_fee = nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_proportional_millionths;
10213+
let new_fee = current_fee + 100;
10214+
assert!(
10215+
matches!(
10216+
nodes[0].node.update_partial_channel_config(&channel.counterparty.node_id, &[channel.channel_id, bad_channel_id], &ChannelConfigUpdate {
10217+
forwarding_fee_proportional_millionths: Some(new_fee),
10218+
..Default::default()
10219+
}),
10220+
Err(APIError::ChannelUnavailable { err: _ }),
10221+
)
10222+
);
10223+
// Check that the fee hasn't changed for the channel that exists.
10224+
assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_proportional_millionths, current_fee);
10225+
let events = nodes[0].node.get_and_clear_pending_msg_events();
10226+
assert_eq!(events.len(), 0);
1018710227
}
1018810228
}
1018910229

0 commit comments

Comments
 (0)