Skip to content

Commit a68d556

Browse files
committed
Consider all channel maps in update_partial_channel_config
1 parent 42d7837 commit a68d556

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
@@ -3538,27 +3538,44 @@ 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+
return Err(APIError::ChannelUnavailable {
3571+
err: format!("Channel with ID {} was not found for the passed counterparty_node_id {}", log_bytes!(*channel_id), counterparty_node_id),
35603572
});
3561-
}
3573+
};
3574+
let mut config = context.config();
3575+
config.apply(config_update);
3576+
// We update the config, but we MUST NOT broadcast a `channel_update` before `channel_ready`
3577+
// which would be the case for pending inbound/outbound channels.
3578+
context.update_config(&config);
35623579
}
35633580
Ok(())
35643581
}
@@ -10172,6 +10189,25 @@ mod tests {
1017210189
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
1017310190
_ => panic!("expected BroadcastChannelUpdate event"),
1017410191
}
10192+
10193+
// If we provide a channel_id not associated with the peer, we should get an error and no updates
10194+
// should be applied to ensure update atomicity as specified in the API docs.
10195+
let bad_channel_id = [10; 32];
10196+
let current_fee = nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_proportional_millionths;
10197+
let new_fee = current_fee + 100;
10198+
assert!(
10199+
matches!(
10200+
nodes[0].node.update_partial_channel_config(&channel.counterparty.node_id, &[channel.channel_id, bad_channel_id], &ChannelConfigUpdate {
10201+
forwarding_fee_proportional_millionths: Some(new_fee),
10202+
..Default::default()
10203+
}),
10204+
Err(APIError::ChannelUnavailable { err: _ }),
10205+
)
10206+
);
10207+
// Check that the fee hasn't changed for the channel that exists.
10208+
assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_proportional_millionths, current_fee);
10209+
let events = nodes[0].node.get_and_clear_pending_msg_events();
10210+
assert_eq!(events.len(), 0);
1017510211
}
1017610212
}
1017710213

0 commit comments

Comments
 (0)