Skip to content

Commit daa0ec6

Browse files
committed
fix Proper error instead of panic in tx_add_input
1 parent 2655f83 commit daa0ec6

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

lightning/src/ln/channel.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -1459,48 +1459,48 @@ impl<SP: Deref> Channel<SP> where
14591459
}
14601460
}
14611461

1462-
pub fn tx_add_input(&mut self, msg: &msgs::TxAddInput) -> InteractiveTxMessageSendResult {
1462+
pub fn tx_add_input(&mut self, msg: &msgs::TxAddInput) -> Result<InteractiveTxMessageSendResult, &'static str> {
14631463
match &mut self.phase {
1464-
ChannelPhase::UnfundedV2(chan) => chan.tx_add_input(msg),
1464+
ChannelPhase::UnfundedV2(chan) => Ok(chan.tx_add_input(msg)),
14651465
#[cfg(splicing)]
1466-
ChannelPhase::Funded(chan) => chan.tx_add_input(msg),
1467-
_ => panic!("Got tx_add_input in an invalid phase"),
1466+
ChannelPhase::Funded(chan) => Ok(chan.tx_add_input(msg)),
1467+
_ => Err("Got tx_add_input in an invalid phase"),
14681468
}
14691469
}
14701470

1471-
pub fn tx_add_output(&mut self, msg: &msgs::TxAddOutput) -> InteractiveTxMessageSendResult {
1471+
pub fn tx_add_output(&mut self, msg: &msgs::TxAddOutput) -> Result<InteractiveTxMessageSendResult, &'static str> {
14721472
match &mut self.phase {
1473-
ChannelPhase::UnfundedV2(chan) => chan.tx_add_output(msg),
1473+
ChannelPhase::UnfundedV2(chan) => Ok(chan.tx_add_output(msg)),
14741474
#[cfg(splicing)]
1475-
ChannelPhase::Funded(chan) => chan.tx_add_output(msg),
1476-
_ => panic!("Got tx_add_output in an invalid phase"),
1475+
ChannelPhase::Funded(chan) => Ok(chan.tx_add_output(msg)),
1476+
_ => Err("Got tx_add_output in an invalid phase"),
14771477
}
14781478
}
14791479

1480-
pub fn tx_remove_input(&mut self, msg: &msgs::TxRemoveInput) -> InteractiveTxMessageSendResult {
1480+
pub fn tx_remove_input(&mut self, msg: &msgs::TxRemoveInput) -> Result<InteractiveTxMessageSendResult, &'static str> {
14811481
match &mut self.phase {
1482-
ChannelPhase::UnfundedV2(chan) => chan.tx_remove_input(msg),
1482+
ChannelPhase::UnfundedV2(chan) => Ok(chan.tx_remove_input(msg)),
14831483
#[cfg(splicing)]
1484-
ChannelPhase::Funded(chan) => chan.tx_remove_input(msg),
1485-
_ => panic!("Got tx_remove_input in an invalid phase"),
1484+
ChannelPhase::Funded(chan) => Ok(chan.tx_remove_input(msg)),
1485+
_ => Err("Got tx_remove_input in an invalid phase"),
14861486
}
14871487
}
14881488

1489-
pub fn tx_remove_output(&mut self, msg: &msgs::TxRemoveOutput) -> InteractiveTxMessageSendResult {
1489+
pub fn tx_remove_output(&mut self, msg: &msgs::TxRemoveOutput) -> Result<InteractiveTxMessageSendResult, &'static str> {
14901490
match &mut self.phase {
1491-
ChannelPhase::UnfundedV2(chan) => chan.tx_remove_output(msg),
1491+
ChannelPhase::UnfundedV2(chan) => Ok(chan.tx_remove_output(msg)),
14921492
#[cfg(splicing)]
1493-
ChannelPhase::Funded(chan) => chan.tx_remove_output(msg),
1494-
_ => panic!("Got tx_remove_output in an invalid phase"),
1493+
ChannelPhase::Funded(chan) => Ok(chan.tx_remove_output(msg)),
1494+
_ => Err("Got tx_remove_output in an invalid phase"),
14951495
}
14961496
}
14971497

1498-
pub fn tx_complete(&mut self, msg: &msgs::TxComplete) -> HandleTxCompleteResult {
1498+
pub fn tx_complete(&mut self, msg: &msgs::TxComplete) -> Result<HandleTxCompleteResult, &'static str> {
14991499
match &mut self.phase {
1500-
ChannelPhase::UnfundedV2(chan) => chan.tx_complete(msg),
1500+
ChannelPhase::UnfundedV2(chan) => Ok(chan.tx_complete(msg)),
15011501
#[cfg(splicing)]
1502-
ChannelPhase::Funded(chan) => chan.tx_complete(msg),
1503-
_ => panic!("Got tx_complete in an invalid phase"),
1502+
ChannelPhase::Funded(chan) => Ok(chan.tx_complete(msg)),
1503+
_ => Err("Got tx_complete in an invalid phase"),
15041504
}
15051505
}
15061506

lightning/src/ln/channelmanager.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -8452,25 +8452,25 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
84528452

84538453
fn internal_tx_add_input(&self, counterparty_node_id: PublicKey, msg: &msgs::TxAddInput) -> Result<(), MsgHandleErrInternal> {
84548454
self.internal_tx_msg(&counterparty_node_id, msg.channel_id, |channel: &mut Channel<SP>| {
8455-
Ok(channel.tx_add_input(msg).into_msg_send_event(counterparty_node_id))
8455+
Ok(channel.tx_add_input(msg)?.into_msg_send_event(counterparty_node_id))
84568456
})
84578457
}
84588458

84598459
fn internal_tx_add_output(&self, counterparty_node_id: PublicKey, msg: &msgs::TxAddOutput) -> Result<(), MsgHandleErrInternal> {
84608460
self.internal_tx_msg(&counterparty_node_id, msg.channel_id, |channel: &mut Channel<SP>| {
8461-
Ok(channel.tx_add_output(msg).into_msg_send_event(counterparty_node_id))
8461+
Ok(channel.tx_add_output(msg)?.into_msg_send_event(counterparty_node_id))
84628462
})
84638463
}
84648464

84658465
fn internal_tx_remove_input(&self, counterparty_node_id: PublicKey, msg: &msgs::TxRemoveInput) -> Result<(), MsgHandleErrInternal> {
84668466
self.internal_tx_msg(&counterparty_node_id, msg.channel_id, |channel: &mut Channel<SP>| {
8467-
Ok(channel.tx_remove_input(msg).into_msg_send_event(counterparty_node_id))
8467+
Ok(channel.tx_remove_input(msg)?.into_msg_send_event(counterparty_node_id))
84688468
})
84698469
}
84708470

84718471
fn internal_tx_remove_output(&self, counterparty_node_id: PublicKey, msg: &msgs::TxRemoveOutput) -> Result<(), MsgHandleErrInternal> {
84728472
self.internal_tx_msg(&counterparty_node_id, msg.channel_id, |channel: &mut Channel<SP>| {
8473-
Ok(channel.tx_remove_output(msg).into_msg_send_event(counterparty_node_id))
8473+
Ok(channel.tx_remove_output(msg)?.into_msg_send_event(counterparty_node_id))
84748474
})
84758475
}
84768476

@@ -8487,8 +8487,15 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
84878487
let peer_state = &mut *peer_state_lock;
84888488
match peer_state.channel_by_id.entry(msg.channel_id) {
84898489
hash_map::Entry::Occupied(mut chan_entry) => {
8490-
let (msg_send_event_opt, signing_session_opt) = chan_entry.get_mut().tx_complete(msg)
8491-
.into_msg_send_event_or_signing_session(counterparty_node_id);
8490+
let (msg_send_event_opt, signing_session_opt) = match chan_entry.get_mut().tx_complete(msg) {
8491+
Ok(res) => res.into_msg_send_event_or_signing_session(counterparty_node_id),
8492+
Err(err) => {
8493+
try_channel_entry!(self, peer_state, Err(ChannelError::Close((
8494+
err.into(),
8495+
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
8496+
))), chan_entry)
8497+
}
8498+
};
84928499
if let Some(msg_send_event) = msg_send_event_opt {
84938500
peer_state.pending_msg_events.push(msg_send_event);
84948501
};

0 commit comments

Comments
 (0)