Skip to content

Commit 4165c0a

Browse files
committed
Fix ChannelManager::accept_inbound_channel error handling
1 parent 0cc0858 commit 4165c0a

File tree

1 file changed

+58
-49
lines changed

1 file changed

+58
-49
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6106,73 +6106,82 @@ where
61066106
// happening and return an error. N.B. that we create channel with an outbound SCID of zero so
61076107
// that we can delay allocating the SCID until after we're sure that the checks below will
61086108
// succeed.
6109-
let mut channel = match peer_state.inbound_channel_request_by_id.remove(temporary_channel_id) {
6109+
let res = match peer_state.inbound_channel_request_by_id.remove(temporary_channel_id) {
61106110
Some(unaccepted_channel) => {
61116111
let best_block_height = self.best_block.read().unwrap().height;
61126112
InboundV1Channel::new(&self.fee_estimator, &self.entropy_source, &self.signer_provider,
61136113
counterparty_node_id.clone(), &self.channel_type_features(), &peer_state.latest_features,
61146114
&unaccepted_channel.open_channel_msg, user_channel_id, &self.default_configuration, best_block_height,
6115-
&self.logger, accept_0conf).map_err(|e| {
6116-
let err_str = e.to_string();
6117-
log_error!(logger, "{}", err_str);
6118-
6119-
APIError::ChannelUnavailable { err: err_str }
6120-
})
6121-
}
6115+
&self.logger, accept_0conf).map_err(|err| MsgHandleErrInternal::from_chan_no_close(err, *temporary_channel_id))
6116+
},
61226117
_ => {
61236118
let err_str = "No such channel awaiting to be accepted.".to_owned();
61246119
log_error!(logger, "{}", err_str);
61256120

6126-
Err(APIError::APIMisuseError { err: err_str })
6121+
return Err(APIError::APIMisuseError { err: err_str });
61276122
}
6128-
}?;
6123+
};
61296124

6130-
if accept_0conf {
6131-
// This should have been correctly configured by the call to InboundV1Channel::new.
6132-
debug_assert!(channel.context.minimum_depth().unwrap() == 0);
6133-
} else if channel.context.get_channel_type().requires_zero_conf() {
6134-
let send_msg_err_event = events::MessageSendEvent::HandleError {
6135-
node_id: channel.context.get_counterparty_node_id(),
6136-
action: msgs::ErrorAction::SendErrorMessage{
6137-
msg: msgs::ErrorMessage { channel_id: temporary_channel_id.clone(), data: "No zero confirmation channels accepted".to_owned(), }
6125+
match res {
6126+
Err(err) => {
6127+
mem::drop(peer_state_lock);
6128+
mem::drop(per_peer_state);
6129+
match handle_error!(self, Result::<(), MsgHandleErrInternal>::Err(err), *counterparty_node_id) {
6130+
Ok(_) => unreachable!("`handle_error` only returns Err"),
6131+
Err(e) => {
6132+
return Err(APIError::ChannelUnavailable { err: e.err });
6133+
},
61386134
}
6139-
};
6140-
peer_state.pending_msg_events.push(send_msg_err_event);
6141-
let err_str = "Please use accept_inbound_channel_from_trusted_peer_0conf to accept channels with zero confirmations.".to_owned();
6142-
log_error!(logger, "{}", err_str);
6135+
}
6136+
Ok(mut channel) => {
6137+
if accept_0conf {
6138+
// This should have been correctly configured by the call to InboundV1Channel::new.
6139+
debug_assert!(channel.context.minimum_depth().unwrap() == 0);
6140+
} else if channel.context.get_channel_type().requires_zero_conf() {
6141+
let send_msg_err_event = events::MessageSendEvent::HandleError {
6142+
node_id: channel.context.get_counterparty_node_id(),
6143+
action: msgs::ErrorAction::SendErrorMessage{
6144+
msg: msgs::ErrorMessage { channel_id: temporary_channel_id.clone(), data: "No zero confirmation channels accepted".to_owned(), }
6145+
}
6146+
};
6147+
peer_state.pending_msg_events.push(send_msg_err_event);
6148+
let err_str = "Please use accept_inbound_channel_from_trusted_peer_0conf to accept channels with zero confirmations.".to_owned();
6149+
log_error!(logger, "{}", err_str);
61436150

6144-
return Err(APIError::APIMisuseError { err: err_str });
6145-
} else {
6146-
// If this peer already has some channels, a new channel won't increase our number of peers
6147-
// with unfunded channels, so as long as we aren't over the maximum number of unfunded
6148-
// channels per-peer we can accept channels from a peer with existing ones.
6149-
if is_only_peer_channel && peers_without_funded_channels >= MAX_UNFUNDED_CHANNEL_PEERS {
6150-
let send_msg_err_event = events::MessageSendEvent::HandleError {
6151-
node_id: channel.context.get_counterparty_node_id(),
6152-
action: msgs::ErrorAction::SendErrorMessage{
6153-
msg: msgs::ErrorMessage { channel_id: temporary_channel_id.clone(), data: "Have too many peers with unfunded channels, not accepting new ones".to_owned(), }
6154-
}
6155-
};
6156-
peer_state.pending_msg_events.push(send_msg_err_event);
6157-
let err_str = "Too many peers with unfunded channels, refusing to accept new ones".to_owned();
6158-
log_error!(logger, "{}", err_str);
6151+
return Err(APIError::APIMisuseError { err: err_str });
6152+
} else {
6153+
// If this peer already has some channels, a new channel won't increase our number of peers
6154+
// with unfunded channels, so as long as we aren't over the maximum number of unfunded
6155+
// channels per-peer we can accept channels from a peer with existing ones.
6156+
if is_only_peer_channel && peers_without_funded_channels >= MAX_UNFUNDED_CHANNEL_PEERS {
6157+
let send_msg_err_event = events::MessageSendEvent::HandleError {
6158+
node_id: channel.context.get_counterparty_node_id(),
6159+
action: msgs::ErrorAction::SendErrorMessage{
6160+
msg: msgs::ErrorMessage { channel_id: temporary_channel_id.clone(), data: "Have too many peers with unfunded channels, not accepting new ones".to_owned(), }
6161+
}
6162+
};
6163+
peer_state.pending_msg_events.push(send_msg_err_event);
6164+
let err_str = "Too many peers with unfunded channels, refusing to accept new ones".to_owned();
6165+
log_error!(logger, "{}", err_str);
61596166

6160-
return Err(APIError::APIMisuseError { err: err_str });
6161-
}
6162-
}
6167+
return Err(APIError::APIMisuseError { err: err_str });
6168+
}
6169+
}
61636170

6164-
// Now that we know we have a channel, assign an outbound SCID alias.
6165-
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
6166-
channel.context.set_outbound_scid_alias(outbound_scid_alias);
6171+
// Now that we know we have a channel, assign an outbound SCID alias.
6172+
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
6173+
channel.context.set_outbound_scid_alias(outbound_scid_alias);
61676174

6168-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
6169-
node_id: channel.context.get_counterparty_node_id(),
6170-
msg: channel.accept_inbound_channel(),
6171-
});
6175+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
6176+
node_id: channel.context.get_counterparty_node_id(),
6177+
msg: channel.accept_inbound_channel(),
6178+
});
61726179

6173-
peer_state.channel_by_id.insert(temporary_channel_id.clone(), ChannelPhase::UnfundedInboundV1(channel));
6180+
peer_state.channel_by_id.insert(temporary_channel_id.clone(), ChannelPhase::UnfundedInboundV1(channel));
61746181

6175-
Ok(())
6182+
Ok(())
6183+
},
6184+
}
61766185
}
61776186

61786187
/// Gets the number of peers which match the given filter and do not have any funded, outbound,

0 commit comments

Comments
 (0)